abstract-order-ability.php
1 day ago
change-invoice-status.php
1 day ago
change-order-status.php
1 day ago
create-invoice.php
1 day ago
create-order.php
4 months ago
delete-order.php
1 day ago
get-invoice.php
1 day ago
get-order-price-breakdown.php
1 day ago
get-order.php
1 day ago
get-transaction.php
1 day ago
list-invoices.php
1 day ago
list-orders.php
1 day ago
list-transactions.php
1 day ago
refund-transaction.php
1 day ago
update-order.php
1 day ago
list-invoices.php
109 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 | // The customer filter and the per-agent record scope both narrow the set of allowed |
| 59 | // order_ids, so they must be intersected into a single where_in(): calling where_in() |
| 60 | // twice on the same column overwrites the earlier constraint, it does not AND them. |
| 61 | $order_id_constraint = null; |
| 62 | |
| 63 | if ( ! empty( $args['customer_id'] ) ) { |
| 64 | global $wpdb; |
| 65 | $orders_table = LATEPOINT_TABLE_ORDERS; |
| 66 | $order_id_constraint = array_map( |
| 67 | 'intval', |
| 68 | $wpdb->get_col( |
| 69 | $wpdb->prepare( "SELECT id FROM {$orders_table} WHERE customer_id = %d", (int) $args['customer_id'] ) |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | // Invoices have no own record-scope — restrict to orders the current user may access. |
| 75 | $allowed_order_ids = $this->allowed_order_ids(); |
| 76 | if ( ! is_null( $allowed_order_ids ) ) { |
| 77 | $order_id_constraint = is_null( $order_id_constraint ) |
| 78 | ? $allowed_order_ids |
| 79 | : array_values( array_intersect( $order_id_constraint, $allowed_order_ids ) ); |
| 80 | } |
| 81 | |
| 82 | if ( ! is_null( $order_id_constraint ) ) { |
| 83 | if ( empty( $order_id_constraint ) ) { |
| 84 | return [ |
| 85 | 'invoices' => [], |
| 86 | 'total' => 0, |
| 87 | 'page' => $page, |
| 88 | 'per_page' => $per_page, |
| 89 | ]; |
| 90 | } |
| 91 | $query->where_in( 'order_id', $order_id_constraint ); |
| 92 | } |
| 93 | |
| 94 | $invoices = ( clone $query ) |
| 95 | ->order_by( 'created_at DESC' ) |
| 96 | ->set_limit( $per_page ) |
| 97 | ->set_offset( $offset ) |
| 98 | ->get_results_as_models(); |
| 99 | $total = $query->count(); |
| 100 | |
| 101 | return [ |
| 102 | 'invoices' => array_map( [ $this, 'serialize_invoice' ], $invoices ), |
| 103 | 'total' => (int) $total, |
| 104 | 'page' => $page, |
| 105 | 'per_page' => $per_page, |
| 106 | ]; |
| 107 | } |
| 108 | } |
| 109 |