ProcurementFilters.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. 'id','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. $this->beforeApply();
  34. foreach ($filters as $filter => $value) {
  35. if (method_exists($this, $filter)) {
  36. $this->$filter($value, $this->queryBuilder);
  37. }
  38. }
  39. $this->afterApply();
  40. return $this->queryBuilder;
  41. }
  42. private function isSearchLike($str):bool
  43. {
  44. if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
  45. return true;
  46. }
  47. return false;
  48. }
  49. private function searchWay($query, $param, $column)
  50. {
  51. if ($this->isSearchLike($param)) {
  52. $query->where($column, 'like', $param);
  53. } else {
  54. $query->whereIn($column, array_filter(preg_split('/[,, ]+/is', $param)));
  55. }
  56. return $query;
  57. }
  58. private function getMaterialQuery():Builder
  59. {
  60. if(!$this->materialQuery)
  61. $this->materialQuery = Material::query()->selectRaw('id');
  62. return $this->materialQuery;
  63. }
  64. private function getOwnerMaterialQuery():Builder
  65. {
  66. if(!$this->ownerMaterialQuery)
  67. $this->ownerMaterialQuery = OwnerMaterial::query()->selectRaw('id');
  68. return $this->ownerMaterialQuery;
  69. }
  70. private function getOwnerQuery():Builder
  71. {
  72. if(!$this->ownerQuery)
  73. $this->ownerQuery = Owner::query()->selectRaw('id');
  74. return $this->ownerQuery;
  75. }
  76. private function getSupplierQuery():Builder
  77. {
  78. if(!$this->supplierQuery)
  79. $this->supplierQuery = Supplier::query()->selectRaw('id');
  80. return $this->supplierQuery;
  81. }
  82. private function getCustomerQuery():Builder
  83. {
  84. if(!$this->customerQuery)
  85. $this->customerQuery = Customer::query()->selectRaw('id');
  86. return $this->customerQuery;
  87. }
  88. public function created_at_start($created_at)
  89. {
  90. $this->queryBuilder->where('created_at','>=',"{$created_at} 00:00:00");
  91. }
  92. public function created_at_end($created_at)
  93. {
  94. $this->queryBuilder->where('created_at','<=',"{$created_at} 23:59:59");
  95. }
  96. public function company_name($company_name)
  97. {
  98. $this->getCustomerQuery()->where('company_name','like',$company_name);
  99. }
  100. public function id($id)
  101. {
  102. $this->queryBuilder->whereIn('id',$id);
  103. }
  104. public function owner_id($owner_id)
  105. {
  106. $this->searchWay($this->getOwnerQuery(),$owner_id,'owner_id');
  107. }
  108. public function supplier_id($supplier_id)
  109. {
  110. $this->searchWay($this->getSupplierQuery(),$supplier_id,'id');
  111. }
  112. public function material_id($material_id)
  113. {
  114. $this->searchWay($this->getMaterialQuery(),$material_id,'id');
  115. }
  116. public function beforeApply()
  117. {
  118. if(isset($this->params['data'])){
  119. $ids = explode(',',$this->params['data']);
  120. $this->id($ids);
  121. }
  122. }
  123. public function afterApply()
  124. {
  125. if($this->customerQuery)
  126. $this->getOwnerQuery()->whereIn('customer_id',$this->customerQuery);
  127. if($this->ownerQuery)
  128. $this->getOwnerMaterialQuery()->whereIn('owner_id',$this->ownerQuery);
  129. if($this->materialQuery)
  130. $this->getOwnerMaterialQuery()->whereIn('material_id',$this->materialQuery);
  131. if($this->ownerMaterialQuery)
  132. $this->queryBuilder->whereIn('owner_material_id',$this->ownerMaterialQuery);
  133. if($this->supplierQuery)
  134. $this->queryBuilder->whereIn('supplier_id',$this->supplierQuery);
  135. }
  136. }