ProcurementFilters.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Filters;
  3. use App\Customer;
  4. use App\Material;
  5. use App\Owner;
  6. use App\OwnerMaterial;
  7. use App\Supplier;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Http\Request;
  10. class ProcurementFilters
  11. {
  12. protected $request;
  13. protected $queryBuilder;
  14. protected $ownerMaterialQuery;
  15. protected $materialQuery;
  16. protected $ownerQuery;
  17. protected $supplierQuery;
  18. protected $customerQuery;
  19. protected $array_filter;
  20. protected $filters = [
  21. 'owner_id','material_id','company_name','created_at_start','created_at_end','supplier_id'
  22. ];
  23. protected $params = [];
  24. public function __construct(Request $request)
  25. {
  26. $this->request = $request;
  27. $this->params = $request->all();
  28. }
  29. public function apply($builder)
  30. {
  31. $this->queryBuilder = $builder;
  32. $filters = array_filter($this->request->only($this->filters));
  33. foreach ($filters as $filter => $value) {
  34. if (method_exists($this, $filter)) {
  35. $this->$filter($value, $this->queryBuilder);
  36. }
  37. }
  38. $this->afterApply();
  39. return $this->queryBuilder;
  40. }
  41. private function isSearchLike($str):bool
  42. {
  43. if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
  44. return true;
  45. }
  46. return false;
  47. }
  48. private function searchWay($query, $param, $column)
  49. {
  50. if ($this->isSearchLike($param)) {
  51. $query->where($column, 'like', $param);
  52. } else {
  53. $query->whereIn($column, array_filter(preg_split('/[,, ]+/is', $param)));
  54. }
  55. return $query;
  56. }
  57. private function getMaterialQuery():Builder
  58. {
  59. if(!$this->materialQuery)
  60. $this->materialQuery = Material::query()->selectRaw('id');
  61. return $this->materialQuery;
  62. }
  63. private function getOwnerMaterialQuery():Builder
  64. {
  65. if(!$this->ownerMaterialQuery)
  66. $this->ownerMaterialQuery = OwnerMaterial::query()->selectRaw('id');
  67. return $this->ownerMaterialQuery;
  68. }
  69. private function getOwnerQuery():Builder
  70. {
  71. if(!$this->ownerQuery)
  72. $this->ownerQuery = Owner::query()->selectRaw('id');
  73. return $this->ownerQuery;
  74. }
  75. private function getSupplierQuery():Builder
  76. {
  77. if(!$this->supplierQuery)
  78. $this->supplierQuery = Supplier::query()->selectRaw('id');
  79. return $this->supplierQuery;
  80. }
  81. private function getCustomerQuery():Builder
  82. {
  83. if(!$this->customerQuery)
  84. $this->customerQuery = Customer::query()->selectRaw('id');
  85. return $this->customerQuery;
  86. }
  87. public function created_at_start($created_at)
  88. {
  89. $this->queryBuilder->where('created_at','>=',"{$created_at} 00:00:00");
  90. }
  91. public function created_at_end($created_at)
  92. {
  93. $this->queryBuilder->where('created_at','<=',"{$created_at} 23:59:59");
  94. }
  95. public function company_name($company_name)
  96. {
  97. $this->getCustomerQuery()->where('company_name','like',$company_name);
  98. }
  99. public function owner_id($owner_id)
  100. {
  101. $this->searchWay($this->getOwnerQuery(),$owner_id,'owner_id');
  102. }
  103. public function supplier_id($supplier_id)
  104. {
  105. $this->searchWay($this->getSupplierQuery(),$supplier_id,'supplier_id');
  106. }
  107. public function material_id($material_id)
  108. {
  109. $this->searchWay($this->getMaterialQuery(),$material_id,'id');
  110. }
  111. public function afterApply()
  112. {
  113. if($this->customerQuery)
  114. $this->getOwnerQuery()->whereIn('customer_id',$this->customerQuery);
  115. if($this->ownerQuery)
  116. $this->getOwnerMaterialQuery()->whereIn('owner_id',$this->ownerQuery);
  117. if($this->materialQuery)
  118. $this->getOwnerMaterialQuery()->whereIn('material_id',$this->materialQuery);
  119. if($this->ownerMaterialQuery)
  120. $this->queryBuilder->whereIn('owner_material_id',$this->ownerMaterialQuery);
  121. }
  122. }