abstract-booking-ability.php
6 days ago
approve-booking.php
3 months ago
cancel-booking.php
3 months ago
change-booking-status.php
6 days ago
create-booking.php
3 months ago
delete-booking.php
6 days ago
get-booking-stats.php
6 days ago
get-booking-statuses.php
3 months ago
get-booking.php
6 days ago
get-bookings-for-date.php
3 months ago
get-bookings-per-day.php
6 days ago
get-upcoming-bookings.php
3 months ago
list-bookings.php
3 months ago
reschedule-booking.php
6 days ago
update-booking.php
6 days ago
create-booking.php
113 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; } |
| 4 | |
| 5 | class LatePointAbilityCreateBooking extends LatePointAbstractBookingAbility { |
| 6 | |
| 7 | protected function configure(): void { |
| 8 | $this->id = 'latepoint/create-booking'; |
| 9 | $this->label = __( 'Create booking', 'latepoint' ); |
| 10 | $this->description = __( 'Creates a new booking for a customer with a specific service, agent, date and time. May trigger booking confirmation notifications.', 'latepoint' ); |
| 11 | $this->permission = 'booking__create'; |
| 12 | $this->read_only = false; |
| 13 | $this->destructive = false; |
| 14 | $this->idempotent = false; |
| 15 | } |
| 16 | |
| 17 | public function get_input_schema(): array { |
| 18 | return [ |
| 19 | 'type' => 'object', |
| 20 | 'properties' => [ |
| 21 | 'customer_id' => [ |
| 22 | 'type' => 'integer', |
| 23 | 'description' => __( 'Customer ID.', 'latepoint' ), |
| 24 | ], |
| 25 | 'service_id' => [ |
| 26 | 'type' => 'integer', |
| 27 | 'description' => __( 'Service ID.', 'latepoint' ), |
| 28 | ], |
| 29 | 'agent_id' => [ |
| 30 | 'type' => 'integer', |
| 31 | 'description' => __( 'Agent ID.', 'latepoint' ), |
| 32 | ], |
| 33 | 'location_id' => [ |
| 34 | 'type' => 'integer', |
| 35 | 'description' => __( 'Location ID.', 'latepoint' ), |
| 36 | ], |
| 37 | 'start_date' => [ |
| 38 | 'type' => 'string', |
| 39 | 'format' => 'date', |
| 40 | 'description' => __( 'Booking date (Y-m-d).', 'latepoint' ), |
| 41 | ], |
| 42 | 'start_time' => [ |
| 43 | 'type' => 'integer', |
| 44 | 'description' => __( 'Start time (minutes from midnight).', 'latepoint' ), |
| 45 | ], |
| 46 | 'end_time' => [ |
| 47 | 'type' => 'integer', |
| 48 | 'description' => __( 'End time (minutes from midnight).', 'latepoint' ), |
| 49 | ], |
| 50 | 'status' => [ |
| 51 | 'type' => 'string', |
| 52 | 'enum' => [ 'approved', 'pending' ], |
| 53 | 'default' => 'approved', |
| 54 | 'description' => __( 'Initial booking status.', 'latepoint' ), |
| 55 | ], |
| 56 | 'notes' => [ |
| 57 | 'type' => 'string', |
| 58 | 'description' => __( 'Internal booking notes.', 'latepoint' ), |
| 59 | ], |
| 60 | ], |
| 61 | 'required' => [ 'customer_id', 'service_id', 'start_date', 'start_time' ], |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | public function get_output_schema(): array { |
| 66 | return $this->booking_output_schema(); |
| 67 | } |
| 68 | |
| 69 | public function execute( array $args ) { |
| 70 | $order = new OsOrderModel(); |
| 71 | $order->customer_id = (int) $args['customer_id']; |
| 72 | $order->status = LATEPOINT_ORDER_STATUS_OPEN; |
| 73 | if ( ! $order->save() ) { |
| 74 | return new WP_Error( 'save_failed', __( 'Failed to create order.', 'latepoint' ), [ 'status' => 422 ] ); |
| 75 | } |
| 76 | |
| 77 | $order_item = new OsOrderItemModel(); |
| 78 | $order_item->order_id = $order->id; |
| 79 | if ( ! $order_item->save() ) { |
| 80 | return new WP_Error( 'save_failed', __( 'Failed to create order item.', 'latepoint' ), [ 'status' => 422 ] ); |
| 81 | } |
| 82 | |
| 83 | $booking = new OsBookingModel(); |
| 84 | $booking->customer_id = (int) $args['customer_id']; |
| 85 | $booking->service_id = (int) $args['service_id']; |
| 86 | $booking->order_item_id = $order_item->id; |
| 87 | $booking->start_date = sanitize_text_field( $args['start_date'] ); |
| 88 | $booking->start_time = (int) $args['start_time']; |
| 89 | |
| 90 | if ( ! empty( $args['agent_id'] ) ) { |
| 91 | $booking->agent_id = (int) $args['agent_id']; |
| 92 | } |
| 93 | if ( ! empty( $args['location_id'] ) ) { |
| 94 | $booking->location_id = (int) $args['location_id']; |
| 95 | } |
| 96 | if ( ! empty( $args['end_time'] ) ) { |
| 97 | $booking->end_time = (int) $args['end_time']; |
| 98 | } |
| 99 | |
| 100 | $booking->status = isset( $args['status'] ) ? sanitize_text_field( $args['status'] ) : LATEPOINT_BOOKING_STATUS_APPROVED; |
| 101 | |
| 102 | if ( ! $booking->save() ) { |
| 103 | return new WP_Error( |
| 104 | 'save_failed', |
| 105 | __( 'Failed to create booking.', 'latepoint' ), |
| 106 | WP_DEBUG ? [ 'errors' => $booking->get_error_messages() ] : [ 'status' => 422 ] |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return $this->serialize_booking( new OsBookingModel( $booking->id ) ); |
| 111 | } |
| 112 | } |
| 113 |