abstract-order-ability.php
3 months ago
change-invoice-status.php
3 months ago
change-order-status.php
3 months ago
create-invoice.php
3 months ago
create-order.php
3 months ago
delete-order.php
3 months ago
get-invoice.php
3 months ago
get-order-price-breakdown.php
3 months ago
get-order.php
3 months ago
get-transaction.php
3 months ago
list-invoices.php
3 months ago
list-orders.php
3 months ago
list-transactions.php
3 months ago
refund-transaction.php
3 months ago
update-order.php
3 months ago
list-invoices.php
91 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityListInvoices extends LatePointAbstractOrderAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/list-invoices'; |
| 10 | $this->label = __( 'List invoices', 'latepoint' ); |
| 11 | $this->description = __( 'Returns a paginated list of invoices with optional filters.', 'latepoint' ); |
| 12 | $this->permission = 'booking__view'; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function get_input_schema(): array { |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => array_merge( |
| 20 | [ |
| 21 | 'order_id' => [ 'type' => 'integer' ], |
| 22 | 'customer_id' => [ 'type' => 'integer' ], |
| 23 | 'status' => [ 'type' => 'string' ], |
| 24 | ], |
| 25 | self::pagination() |
| 26 | ), |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | public function get_output_schema(): array { |
| 31 | return [ |
| 32 | 'type' => 'object', |
| 33 | 'properties' => [ |
| 34 | 'invoices' => [ |
| 35 | 'type' => 'array', |
| 36 | 'items' => $this->invoice_output_schema(), |
| 37 | ], |
| 38 | 'total' => [ 'type' => 'integer' ], |
| 39 | 'page' => [ 'type' => 'integer' ], |
| 40 | 'per_page' => [ 'type' => 'integer' ], |
| 41 | ], |
| 42 | ]; |
| 43 | } |
| 44 | |
| 45 | public function execute( array $args ) { |
| 46 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 47 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 48 | $offset = ( $page - 1 ) * $per_page; |
| 49 | |
| 50 | $query = new OsInvoiceModel(); |
| 51 | |
| 52 | if ( ! empty( $args['order_id'] ) ) { |
| 53 | $query->where( [ 'order_id' => (int) $args['order_id'] ] ); |
| 54 | } |
| 55 | if ( ! empty( $args['status'] ) ) { |
| 56 | $query->where( [ 'status' => sanitize_text_field( $args['status'] ) ] ); |
| 57 | } |
| 58 | if ( ! empty( $args['customer_id'] ) ) { |
| 59 | global $wpdb; |
| 60 | $customer_id = (int) $args['customer_id']; |
| 61 | $orders_table = LATEPOINT_TABLE_ORDERS; |
| 62 | $order_ids = $wpdb->get_col( |
| 63 | $wpdb->prepare( "SELECT id FROM {$orders_table} WHERE customer_id = %d", $customer_id ) |
| 64 | ); |
| 65 | if ( empty( $order_ids ) ) { |
| 66 | return [ |
| 67 | 'invoices' => [], |
| 68 | 'total' => 0, |
| 69 | 'page' => $page, |
| 70 | 'per_page' => $per_page, |
| 71 | ]; |
| 72 | } |
| 73 | $query->where_in( 'order_id', $order_ids ); |
| 74 | } |
| 75 | |
| 76 | $total = ( clone $query )->count(); |
| 77 | $invoices = $query |
| 78 | ->order_by( 'created_at DESC' ) |
| 79 | ->set_limit( $per_page ) |
| 80 | ->set_offset( $offset ) |
| 81 | ->get_results_as_models(); |
| 82 | |
| 83 | return [ |
| 84 | 'invoices' => array_map( [ $this, 'serialize_invoice' ], $invoices ), |
| 85 | 'total' => (int) $total, |
| 86 | 'page' => $page, |
| 87 | 'per_page' => $per_page, |
| 88 | ]; |
| 89 | } |
| 90 | } |
| 91 |