95 lines
3.7 KiB
PHP
95 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\Company;
|
|
use App\Models\Category;
|
|
use App\Models\CompanyCategory;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class CompanyService
|
|
{
|
|
|
|
public function datatable($request)
|
|
{
|
|
if ($request->ajax()) {
|
|
$data = Company::with('category.category')->latest()->select('*');
|
|
if ($request->dropdownValue != null && $request->dropdownValue != 'orderby') {
|
|
$where = $request->dropdownValue;
|
|
$data->where('status', $where);
|
|
};
|
|
if ($request->dropdownValue == 'orderby') {
|
|
$data->orderBy('id', 'desc');
|
|
};
|
|
return Datatables::of($data)->addIndexColumn()
|
|
->editColumn('created_at', function ($row) {
|
|
$formattedDate = $row->created_at->format('d/m/Y');
|
|
return '<div class="badge badge-light fw-bold">' . $formattedDate . '</div>';
|
|
})
|
|
->editColumn('company_logo', function ($row) {
|
|
return '<div><img class="w-100" src="/public/uploads/manufactures_company/logo/' . $row->getAttributes()['company_logo'] . '"/></div>';
|
|
})
|
|
->editColumn('company_name', function ($row) {
|
|
return '<div>' . $row->company_name . '</div>';
|
|
})
|
|
// ->editColumn('categories', function ($row) {
|
|
// $s = '';
|
|
// foreach ($row->category as $cat) {
|
|
// $s .= trim($cat->category->pluck('category_name'), '[]"') . ',';
|
|
// }
|
|
// return '<div>' . $s . '</div>';
|
|
// })
|
|
->addColumn('action', function ($row) {
|
|
$status = $row->status == 1 ? 'checked' : 'null';
|
|
|
|
$btn = '<div class="text-end d-flex align-items-center justify-content-around">
|
|
<a type="button" class="action_icon edit_company_btn" id="edit_company_btn" onclick="getCompany(' . $row->id . ')" data-id="" data-company_name="" data-company_logo="">
|
|
<i class="fa-solid fs-3 fa-pen-to-square"></i>
|
|
</a>
|
|
|
|
<a class="action_icon delete" onclick="deleteCompanyModal(' . $row->id . ')" data-id="{{$row->id}}">
|
|
<i class="fa-solid fa-trash"></i>
|
|
</a>
|
|
<a class="action_icon">
|
|
<div class="on-off-toggle">
|
|
<input class="on-off-toggle__input is_active" type="checkbox" id="bopis' . $row->id . '" data-id="' . $row->id . '" onclick="companyStatus(' . $row->id . ',' . $row->status . ')" value="' . $row->status . '" ' . $status . '/>
|
|
<label for="bopis' . $row->id . '" class="on-off-toggle__slider"></label>
|
|
</div>
|
|
</a>
|
|
</div>
|
|
';
|
|
return $btn;
|
|
})
|
|
->rawColumns(['company_logo', 'company_name', 'categories', 'created_at', 'action'])
|
|
->make(true);
|
|
}
|
|
}
|
|
|
|
public function getCategories()
|
|
{
|
|
return Category::active()->pluck('category_name', 'id');
|
|
}
|
|
|
|
public function getActiveCompanies()
|
|
{
|
|
return Company::where('status',true)->get(['id','company_name']);
|
|
}
|
|
|
|
public function getCompany($id)
|
|
{
|
|
return Company::find($id);
|
|
}
|
|
|
|
public function getCompanyCategory($id)
|
|
{
|
|
return CompanyCategory::where('companies_id', $id)->pluck('categories_id');
|
|
}
|
|
|
|
public function updateStatus($request)
|
|
{
|
|
return Company::where('id', $request->id)->update([
|
|
'status' => $request->status == 1 ? 0 : 1
|
|
]);
|
|
}
|
|
}
|