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
abstract-order-ability.php
143 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | abstract class LatePointAbstractOrderAbility extends LatePointAbstractAbility { |
| 7 | |
| 8 | public function serialize_order( OsOrderModel $o ): array { |
| 9 | return [ |
| 10 | 'id' => (int) $o->id, |
| 11 | 'status' => $o->status ?? '', |
| 12 | 'payment_status' => $o->payment_status ?? '', |
| 13 | 'fulfillment_status' => $o->fulfillment_status ?? '', |
| 14 | 'customer_id' => (int) ( $o->customer_id ?? 0 ), |
| 15 | 'subtotal' => (float) ( $o->subtotal ?? 0 ), |
| 16 | 'total' => (float) ( $o->total ?? 0 ), |
| 17 | 'notes' => $o->customer_comment ?? '', |
| 18 | 'created_at' => ! empty( $o->created_at ) ? date( 'c', strtotime( $o->created_at ) ) : '', |
| 19 | 'updated_at' => ! empty( $o->updated_at ) ? date( 'c', strtotime( $o->updated_at ) ) : '', |
| 20 | ]; |
| 21 | } |
| 22 | |
| 23 | public function serialize_invoice( OsInvoiceModel $i ): array { |
| 24 | $order = $i->order_id ? new OsOrderModel( (int) $i->order_id ) : new OsOrderModel(); |
| 25 | $customer_id = ( $order && ! $order->is_new_record() ) ? (int) $order->customer_id : 0; |
| 26 | return [ |
| 27 | 'id' => (int) $i->id, |
| 28 | 'order_id' => (int) ( $i->order_id ?? 0 ), |
| 29 | 'customer_id' => $customer_id, |
| 30 | 'status' => $i->status ?? '', |
| 31 | 'subtotal' => (float) ( $i->charge_amount ?? 0 ), |
| 32 | 'total' => (float) ( $i->charge_amount ?? 0 ), |
| 33 | 'due_date' => ! empty( $i->due_at ) ? date( 'c', strtotime( $i->due_at ) ) : '', |
| 34 | 'created_at' => ! empty( $i->created_at ) ? date( 'c', strtotime( $i->created_at ) ) : '', |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | public function serialize_transaction( OsTransactionModel $t ): array { |
| 39 | return [ |
| 40 | 'id' => (int) $t->id, |
| 41 | 'order_id' => (int) ( $t->order_id ?? 0 ), |
| 42 | 'customer_id' => (int) ( $t->customer_id ?? 0 ), |
| 43 | 'status' => $t->status ?? '', |
| 44 | 'payment_method' => $t->payment_method ?? '', |
| 45 | 'amount' => (float) ( $t->amount ?? 0 ), |
| 46 | 'created_at' => ! empty( $t->created_at ) ? date( 'c', strtotime( $t->created_at ) ) : '', |
| 47 | ]; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Authorizes access to an order by ID using the same agent/location/service |
| 52 | * record-scoping the admin order screens enforce ( OsOrderModel::filter_allowed_records() ). |
| 53 | * Orders, invoices and transactions all scope to their parent order. Admins always pass. |
| 54 | * |
| 55 | * @param int $order_id |
| 56 | * @return true|\WP_Error |
| 57 | */ |
| 58 | protected function authorize_order( int $order_id ) { |
| 59 | $allowed_order = ( new OsOrderModel() ) |
| 60 | ->where( [ LATEPOINT_TABLE_ORDERS . '.id' => absint( $order_id ) ] ) |
| 61 | ->filter_allowed_records() |
| 62 | ->set_limit( 1 ) |
| 63 | ->get_results_as_models(); |
| 64 | if ( ! $allowed_order ) { |
| 65 | return new WP_Error( |
| 66 | 'forbidden', |
| 67 | __( 'You are not allowed to access this record.', 'latepoint' ), |
| 68 | [ 'status' => 403 ] |
| 69 | ); |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Order IDs the current user is allowed to access, or null when unrestricted. |
| 76 | * |
| 77 | * Invoices and transactions have no own record-scope, so their list abilities |
| 78 | * must be filtered by parent order. Returns null for admins / unrestricted |
| 79 | * users (no filtering needed), otherwise an array (possibly empty) of order IDs. |
| 80 | * Only the id column is selected to keep the lookup cheap. |
| 81 | * |
| 82 | * @return int[]|null |
| 83 | */ |
| 84 | protected function allowed_order_ids(): ?array { |
| 85 | if ( OsRolesHelper::are_all_records_allowed() ) { |
| 86 | return null; |
| 87 | } |
| 88 | $query = new OsOrderModel(); |
| 89 | $query->filter_allowed_records(); |
| 90 | $query->clear_select()->select( LATEPOINT_TABLE_ORDERS . '.id' ); |
| 91 | return array_map( fn( $row ) => (int) $row->id, $query->get_results() ); |
| 92 | } |
| 93 | |
| 94 | protected function order_output_schema(): array { |
| 95 | return [ |
| 96 | 'type' => 'object', |
| 97 | 'properties' => [ |
| 98 | 'id' => [ 'type' => 'integer' ], |
| 99 | 'status' => [ 'type' => 'string' ], |
| 100 | 'payment_status' => [ 'type' => 'string' ], |
| 101 | 'fulfillment_status' => [ 'type' => 'string' ], |
| 102 | 'customer_id' => [ 'type' => 'integer' ], |
| 103 | 'subtotal' => [ 'type' => 'number' ], |
| 104 | 'total' => [ 'type' => 'number' ], |
| 105 | 'notes' => [ 'type' => 'string' ], |
| 106 | 'created_at' => [ 'type' => 'string' ], |
| 107 | 'updated_at' => [ 'type' => 'string' ], |
| 108 | ], |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | protected function invoice_output_schema(): array { |
| 113 | return [ |
| 114 | 'type' => 'object', |
| 115 | 'properties' => [ |
| 116 | 'id' => [ 'type' => 'integer' ], |
| 117 | 'order_id' => [ 'type' => 'integer' ], |
| 118 | 'customer_id' => [ 'type' => 'integer' ], |
| 119 | 'status' => [ 'type' => 'string' ], |
| 120 | 'subtotal' => [ 'type' => 'number' ], |
| 121 | 'total' => [ 'type' => 'number' ], |
| 122 | 'due_date' => [ 'type' => 'string' ], |
| 123 | 'created_at' => [ 'type' => 'string' ], |
| 124 | ], |
| 125 | ]; |
| 126 | } |
| 127 | |
| 128 | protected function transaction_output_schema(): array { |
| 129 | return [ |
| 130 | 'type' => 'object', |
| 131 | 'properties' => [ |
| 132 | 'id' => [ 'type' => 'integer' ], |
| 133 | 'order_id' => [ 'type' => 'integer' ], |
| 134 | 'customer_id' => [ 'type' => 'integer' ], |
| 135 | 'status' => [ 'type' => 'string' ], |
| 136 | 'payment_method' => [ 'type' => 'string' ], |
| 137 | 'amount' => [ 'type' => 'number' ], |
| 138 | 'created_at' => [ 'type' => 'string' ], |
| 139 | ], |
| 140 | ]; |
| 141 | } |
| 142 | } |
| 143 |