ReceivingTaskFilters.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Filters;
  3. use App\DeliveryAppointmentCar;
  4. use App\Traits\ModelSearchWay;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Http\Request;
  7. class ReceivingTaskFilters{
  8. use ModelSearchWay;
  9. protected $request;
  10. protected $queryBuilder;
  11. protected $deliveryAppointmentCarQuery;
  12. protected $receiving;
  13. protected $filters = [
  14. 'id','owner_id','warehouse_id','appointment_number','created_at_start','created_at_end','number'
  15. ];
  16. protected $params = [];
  17. public function __construct(Request $request)
  18. {
  19. $this->request = $request;
  20. $this->params = $request->all();
  21. }
  22. public function apply($builder)
  23. {
  24. $this->queryBuilder = $builder;
  25. $filters = array_filter($this->request->only($this->filters));
  26. foreach ($filters as $filter => $value) {
  27. if (method_exists($this, $filter)) {
  28. $this->$filter($value, $this->queryBuilder);
  29. }
  30. }
  31. $this->afterApply();
  32. return $this->queryBuilder;
  33. }
  34. public function getDeliveryAppointmentCarQuery(): Builder
  35. {
  36. if (!$this->deliveryAppointmentCarQuery){
  37. $this->deliveryAppointmentCarQuery = DeliveryAppointmentCar::query()->select('id');
  38. }
  39. return $this->deliveryAppointmentCarQuery;
  40. }
  41. public function afterApply(){
  42. if ($this->deliveryAppointmentCarQuery){
  43. $this->queryBuilder->whereIn('delivery_appointment_car_id',$this->deliveryAppointmentCarQuery);
  44. }
  45. }
  46. public function id($ids){
  47. $this->searchWay($this->queryBuilder,$ids,'id');
  48. }
  49. public function owner_id($owner_id){
  50. $this->searchWay($this->queryBuilder,$owner_id,'owner_id');
  51. }
  52. public function warehouse_id($warehouse_id){
  53. $this->searchWay($this->queryBuilder,$warehouse_id,'warehouse_id');
  54. }
  55. public function appointment_number($appointment_number){
  56. $this->searchWay($this->getDeliveryAppointmentCarQuery(),$appointment_number,'appointment_number');
  57. }
  58. public function number($number){
  59. $this->searchWay($this->queryBuilder,$number,'number');
  60. }
  61. public function created_at_start($created_at_start){
  62. $this->queryBuilder->where('created_at','>=',$created_at_start." 00:00:00");
  63. }
  64. public function created_at_end($created_at_end){
  65. $this->queryBuilder->where('created_at','<=',$created_at_end." 23:59:59");
  66. }
  67. }