| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright (c) 2021 LatePoint LLC. All rights reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace LatePoint\Misc; |
| 7 |
|
| 8 |
class Filter { |
| 9 |
public $service_id = 0; |
| 10 |
public $agent_id = 0; |
| 11 |
public $location_id = 0; |
| 12 |
|
| 13 |
public $connections = []; |
| 14 |
|
| 15 |
public ?int $week_day = null; |
| 16 |
public ?string $date_from = null; |
| 17 |
public ?string $date_to = null; |
| 18 |
|
| 19 |
public ?int $start_time = null; |
| 20 |
public ?int $end_time = null; |
| 21 |
|
| 22 |
public array $statuses = []; |
| 23 |
public array $exclude_booking_ids = []; |
| 24 |
public bool $consider_cart_items = false; |
| 25 |
public bool $exact_match = false; |
| 26 |
|
| 27 |
function __construct( array $args = [] ) { |
| 28 |
$allowed_args = [ |
| 29 |
'service_id', |
| 30 |
'agent_id', |
| 31 |
'location_id', |
| 32 |
'connections', |
| 33 |
'date_from', |
| 34 |
'date_to', |
| 35 |
'start_time', |
| 36 |
'end_time', |
| 37 |
'week_day', |
| 38 |
'statuses', |
| 39 |
'exclude_booking_ids', |
| 40 |
'exact_match', |
| 41 |
]; |
| 42 |
foreach ( $args as $key => $arg ) { |
| 43 |
if ( in_array( $key, $allowed_args ) ) { |
| 44 |
$this->$key = $arg; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function build_query_args_for_blocked_periods(): array { |
| 50 |
|
| 51 |
$query_args = []; |
| 52 |
|
| 53 |
// if connections are passed - query by connection |
| 54 |
if ( $this->connections ) { |
| 55 |
$connection_conditions = []; |
| 56 |
foreach ( $this->connections as $connection ) { |
| 57 |
$connection_conditions[] = [ |
| 58 |
'AND' => [ |
| 59 |
'agent_id' => [ 0, $connection->agent_id ], |
| 60 |
'service_id' => [ 0, $connection->service_id ], |
| 61 |
'location_id' => [ 0, $connection->location_id ], |
| 62 |
], |
| 63 |
]; |
| 64 |
} |
| 65 |
$query_args['AND'][] = [ 'OR' => $connection_conditions ]; |
| 66 |
} else { |
| 67 |
// Service query |
| 68 |
if ( $this->exact_match ) { |
| 69 |
// search only for schedules that belong to passed service_id |
| 70 |
$query_args['service_id'] = $this->service_id; |
| 71 |
} else { |
| 72 |
$query_args['service_id'] = array_unique( is_array( $this->service_id ) ? array_merge( $this->service_id, [ 0 ] ) : [ $this->service_id, 0 ] ); |
| 73 |
} |
| 74 |
|
| 75 |
// Location query |
| 76 |
if ( $this->exact_match ) { |
| 77 |
// search only for schedules that belong to passed location_id |
| 78 |
$query_args['location_id'] = $this->location_id; |
| 79 |
} else { |
| 80 |
$query_args['location_id'] = array_unique( is_array( $this->location_id ) ? array_merge( $this->location_id, [ 0 ] ) : [ $this->location_id, 0 ] ); |
| 81 |
} |
| 82 |
|
| 83 |
// Agent query |
| 84 |
if ( $this->exact_match ) { |
| 85 |
// search only for schedules that belong to passed agent_id |
| 86 |
$query_args['agent_id'] = $this->agent_id; |
| 87 |
} else { |
| 88 |
$query_args['agent_id'] = array_unique( is_array( $this->agent_id ) ? array_merge( $this->agent_id, [ 0 ] ) : [ $this->agent_id, 0 ] ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
if ( $this->date_from ) { |
| 94 |
$query_args['start_date >='] = $this->date_from; |
| 95 |
$query_args['start_date <='] = $this->date_to; |
| 96 |
} |
| 97 |
|
| 98 |
return $query_args; |
| 99 |
} |
| 100 |
|
| 101 |
public static function create_from_booking_request( BookingRequest $booking_request ): Filter { |
| 102 |
return new self( |
| 103 |
[ |
| 104 |
'date_from' => $booking_request->start_date, |
| 105 |
'start_time' => $booking_request->start_time, |
| 106 |
'end_time' => $booking_request->end_time, |
| 107 |
'agent_id' => $booking_request->agent_id, |
| 108 |
'location_id' => $booking_request->location_id, |
| 109 |
'service_id' => $booking_request->service_id, |
| 110 |
] |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
|