![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/medad.corals.io/Corals/modules/Medad/DataTables/Scopes/ |
<?php namespace Corals\Modules\Medad\DataTables\Scopes; use Corals\Modules\Medad\Constants\CompanyTypeConstants; use Corals\Modules\Medad\Facades\Medad; use Yajra\DataTables\Contracts\DataTableScope; class AuthUserCompanyDataScope implements DataTableScope { protected $target; public function __construct($target) { $this->target = $target; } public function apply($query) { $companyUser = Medad::getUserCompany(); if (!$companyUser) { $query->whereNull('id'); return; } $companyType = Medad::getCurrentCompanyType(); $table = $query->getModel()->getTable(); if ($this->target == 'company') { $query->where('medad_companies.parent_id', $companyUser->id); } elseif ($this->target == 'project') { $query->whereHas('branch', function ($branchQB) use ($companyUser) { $branchQB->where('parent_id', $companyUser->id); }); } elseif ($this->target == 'companyRelation') { $query->where('medad_company_relation.company_id', $companyUser->id); } elseif ($this->target == 'vehicle') { $query->where('medad_vehicles.company_id', $companyUser->id); } elseif (in_array($this->target, ['quotation', 'purchaseOrder', 'deliveryNote', 'invoice', 'transaction'])) { $query->where(sprintf('%s.%s_company_id', $table, $companyType), $companyUser->id); $this->applyStatusQuery($query, $table, $companyUser); } elseif ($this->target == 'quotationRequest') { if ($companyType == CompanyTypeConstants::SUPPLIER) { $query->whereHas('suppliers', function ($byQuery) use ($companyUser) { $byQuery->where('medad_quotation_request_suppliers.company_id', $companyUser->id); }); } elseif ($companyType == CompanyTypeConstants::CUSTOMER) { $query->where('medad_quotation_requests.customer_company_id', $companyUser->id); } $this->applyStatusQuery($query, $table, $companyUser); } } protected function applyStatusQuery($query, $table, $companyUser) { $query->whereRaw(sprintf( 'case WHEN %s.owner_company_id != %s THEN %s.status != "draft" else %s.status IS NOT null END', $table, $companyUser->id, $table, $table) ); } }