| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Filters;
- use App\Customer;
- use App\Material;
- use App\Owner;
- use App\OwnerMaterial;
- use App\Supplier;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Http\Request;
- class ProcurementFilters
- {
- protected $request;
- protected $queryBuilder;
- protected $ownerMaterialQuery;
- protected $materialQuery;
- protected $ownerQuery;
- protected $supplierQuery;
- protected $customerQuery;
- protected $array_filter;
- protected $filters = [
- 'owner_id','material_id','company_name','created_at_start','created_at_end','supplier_id'
- ];
- protected $params = [];
- public function __construct(Request $request)
- {
- $this->request = $request;
- $this->params = $request->all();
- }
- public function apply($builder)
- {
- $this->queryBuilder = $builder;
- $filters = array_filter($this->request->only($this->filters));
- foreach ($filters as $filter => $value) {
- if (method_exists($this, $filter)) {
- $this->$filter($value, $this->queryBuilder);
- }
- }
- $this->afterApply();
- return $this->queryBuilder;
- }
- private function isSearchLike($str):bool
- {
- if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
- return true;
- }
- return false;
- }
- private function searchWay($query, $param, $column)
- {
- if ($this->isSearchLike($param)) {
- $query->where($column, 'like', $param);
- } else {
- $query->whereIn($column, array_filter(preg_split('/[,, ]+/is', $param)));
- }
- return $query;
- }
- private function getMaterialQuery():Builder
- {
- if(!$this->materialQuery)
- $this->materialQuery = Material::query()->selectRaw('id');
- return $this->materialQuery;
- }
- private function getOwnerMaterialQuery():Builder
- {
- if(!$this->ownerMaterialQuery)
- $this->ownerMaterialQuery = OwnerMaterial::query()->selectRaw('id');
- return $this->ownerMaterialQuery;
- }
- private function getOwnerQuery():Builder
- {
- if(!$this->ownerQuery)
- $this->ownerQuery = Owner::query()->selectRaw('id');
- return $this->ownerQuery;
- }
- private function getSupplierQuery():Builder
- {
- if(!$this->supplierQuery)
- $this->supplierQuery = Supplier::query()->selectRaw('id');
- return $this->supplierQuery;
- }
- private function getCustomerQuery():Builder
- {
- if(!$this->customerQuery)
- $this->customerQuery = Customer::query()->selectRaw('id');
- return $this->customerQuery;
- }
- public function created_at_start($created_at)
- {
- $this->queryBuilder->where('created_at','>=',"{$created_at} 00:00:00");
- }
- public function created_at_end($created_at)
- {
- $this->queryBuilder->where('created_at','<=',"{$created_at} 23:59:59");
- }
- public function company_name($company_name)
- {
- $this->getCustomerQuery()->where('company_name','like',$company_name);
- }
- public function owner_id($owner_id)
- {
- $this->searchWay($this->getOwnerQuery(),$owner_id,'owner_id');
- }
- public function supplier_id($supplier_id)
- {
- $this->searchWay($this->getSupplierQuery(),$supplier_id,'supplier_id');
- }
- public function material_id($material_id)
- {
- $this->searchWay($this->getMaterialQuery(),$material_id,'id');
- }
- public function afterApply()
- {
- if($this->customerQuery)
- $this->getOwnerQuery()->whereIn('customer_id',$this->customerQuery);
- if($this->ownerQuery)
- $this->getOwnerMaterialQuery()->whereIn('owner_id',$this->ownerQuery);
- if($this->materialQuery)
- $this->getOwnerMaterialQuery()->whereIn('material_id',$this->materialQuery);
- if($this->ownerMaterialQuery)
- $this->queryBuilder->whereIn('owner_material_id',$this->ownerMaterialQuery);
- }
- }
|