PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.6.7
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.6.7
1.17.10 1.17.9 1.17.8 1.17.7 1.17.6 1.17.5 1.17.4 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.3.0 1.3.1 1.3.11 1.3.12 1.3.13 1.3.14 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 All 144 releases
erp / modules / accounting / api / class-rest-api-pay-purchases.php

class-rest-api-pay-purchases.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/accounting/api/class-rest-api-pay-purchases.php

535 lines 18.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WeDevs\ERP\Accounting\API;
4
5 use WP_Error;
6 use WP_REST_Response;
7 use WP_REST_Server;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 class Pay_Purchases_Controller extends \WeDevs\ERP\API\REST_Controller {
14
15 /**
16 * Endpoint namespace.
17 *
18 * @var string
19 */
20 protected $namespace = 'erp/v1';
21
22 /**
23 * Route base.
24 *
25 * @var string
26 */
27 protected $rest_base = 'accounting/v1/pay-purchases';
28
29 /**
30 * Register the routes for the objects of the controller.
31 */
32 public function register_routes() {
33 register_rest_route(
34 $this->namespace,
35 '/' . $this->rest_base,
36 [
37 [
38 'methods' => WP_REST_Server::READABLE,
39 'callback' => [ $this, 'get_pay_purchases' ],
40 'args' => [],
41 'permission_callback' => function ( $request ) {
42 return current_user_can( 'erp_ac_view_expense' );
43 },
44 ],
45 [
46 'methods' => WP_REST_Server::CREATABLE,
47 'callback' => [ $this, 'create_pay_purchase' ],
48 'args' => [],
49 'permission_callback' => function ( $request ) {
50 return current_user_can( 'erp_ac_create_expenses_voucher' );
51 },
52 ],
53 'schema' => [ $this, 'get_public_item_schema' ],
54 ]
55 );
56
57 register_rest_route(
58 $this->namespace,
59 '/' . $this->rest_base . '/(?P<id>[\d]+)',
60 [
61 [
62 'methods' => WP_REST_Server::READABLE,
63 'callback' => [ $this, 'get_pay_purchase' ],
64 'args' => [],
65 'permission_callback' => function ( $request ) {
66 return current_user_can( 'erp_ac_view_expense' );
67 },
68 ],
69 [
70 'methods' => WP_REST_Server::EDITABLE,
71 'callback' => [ $this, 'update_pay_purchase' ],
72 'args' => [],
73 'permission_callback' => function ( $request ) {
74 return current_user_can( 'erp_ac_create_expenses_voucher' );
75 },
76 ],
77 'schema' => [ $this, 'get_public_item_schema' ],
78 ]
79 );
80
81 register_rest_route(
82 $this->namespace,
83 '/' . $this->rest_base . '/(?P<id>[\d]+)' . '/void',
84 [
85 [
86 'methods' => WP_REST_Server::CREATABLE,
87 'callback' => [ $this, 'void_pay_purchase' ],
88 'args' => [],
89 'permission_callback' => function ( $request ) {
90 return current_user_can( 'erp_ac_publish_expenses_voucher' );
91 },
92 ],
93 ]
94 );
95 }
96
97 /**
98 * Get a collection of pay_purchases
99 *
100 * @param WP_REST_Request $request
101 *
102 * @return WP_Error|WP_REST_Response
103 */
104 public function get_pay_purchases( $request ) {
105 $args = [
106 'number' => (int) isset( $request['per_page'] ) ? $request['per_page'] : 20,
107 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
108 ];
109
110 $formatted_items = [];
111 $additional_fields = [];
112
113 $additional_fields['namespace'] = $this->namespace;
114 $additional_fields['rest_base'] = $this->rest_base;
115
116 $pay_purchase_data = erp_acct_get_pay_purchases( $args );
117 $total_items = erp_acct_get_pay_purchases(
118 [
119 'count' => true,
120 'number' => -1,
121 ]
122 );
123
124 foreach ( $pay_purchase_data as $item ) {
125 if ( isset( $request['include'] ) ) {
126 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
127
128 if ( in_array( 'created_by', $include_params, true ) ) {
129 $item['created_by'] = $this->get_user( $item['created_by'] );
130 }
131 }
132
133 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
134 $formatted_items[] = $this->prepare_response_for_collection( $data );
135 }
136
137 $response = rest_ensure_response( $formatted_items );
138 $response = $this->format_collection_response( $response, $request, $total_items );
139
140 $response->set_status( 200 );
141
142 return $response;
143 }
144
145 /**
146 * Get a pay_purchase
147 *
148 * @param WP_REST_Request $request
149 *
150 * @return WP_Error|WP_REST_Response
151 */
152 public function get_pay_purchase( $request ) {
153 $id = (int) $request['id'];
154
155 if ( empty( $id ) ) {
156 return new WP_Error( 'rest_pay_purchase_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
157 }
158
159 $item = erp_acct_get_pay_purchase( $id );
160
161 $additional_fields['namespace'] = $this->namespace;
162 $additional_fields['rest_base'] = $this->rest_base;
163
164 $item = $this->prepare_item_for_response( $item, $request, $additional_fields );
165
166 $response = rest_ensure_response( $item );
167
168 $response->set_status( 200 );
169
170 return $response;
171 }
172
173 /**
174 * Create a pay_purchase
175 *
176 * @param WP_REST_Request $request
177 *
178 * @return WP_Error|WP_REST_Response
179 */
180 public function create_pay_purchase( $request ) {
181 $additional_fields = [];
182 $pay_purchase_data = $this->prepare_item_for_database( $request );
183
184 $items = $request['purchase_details'];
185 $item_total = [];
186
187 foreach ( $items as $key => $item ) {
188 $item_total[ $key ] = $item['line_total'];
189 }
190
191 $pay_purchase_data['amount'] = array_sum( $item_total );
192
193 $pay_purchase_data = erp_acct_insert_pay_purchase( $pay_purchase_data );
194
195 $this->add_log( $pay_purchase_data, 'add' );
196
197 $additional_fields['namespace'] = $this->namespace;
198 $additional_fields['rest_base'] = $this->rest_base;
199
200 $pay_purchase_data = $this->prepare_item_for_response( $pay_purchase_data, $request, $additional_fields );
201
202 $response = rest_ensure_response( $pay_purchase_data );
203
204 $response->set_status( 201 );
205
206 return $response;
207 }
208
209 /**
210 * Update a pay_purchase
211 *
212 * @param WP_REST_Request $request
213 *
214 * @return WP_Error|WP_REST_Response
215 */
216 public function update_pay_purchase( $request ) {
217 $id = (int) $request['id'];
218
219 if ( empty( $id ) ) {
220 return new WP_Error( 'rest_pay_purchase_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
221 }
222
223 $pay_purchase_data = $this->prepare_item_for_database( $request );
224
225 $items = $request['purchase_details'];
226 $item_total = [];
227
228 foreach ( $items as $key => $item ) {
229 $item_total[ $key ] = $item['line_total'];
230 }
231
232 $pay_purchase_data['amount'] = array_sum( $item_total );
233
234 $pay_purchase_id = erp_acct_update_pay_purchase( $pay_purchase_data, $id );
235
236 $response = rest_ensure_response( $pay_purchase_data );
237 $response = $this->format_collection_response( $response, $request, 1 );
238
239 return $response;
240 }
241
242 /**
243 * Void a pay_purchase
244 *
245 * @param WP_REST_Request $request
246 *
247 * @return WP_Error|WP_REST_Request
248 */
249 public function void_pay_purchase( $request ) {
250 $id = (int) $request['id'];
251
252 if ( empty( $id ) ) {
253 return new WP_Error( 'rest_pay_purchase_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
254 }
255
256 erp_acct_void_pay_purchase( $id );
257
258 return new WP_REST_Response( true, 204 );
259 }
260
261 /**
262 * Log when purchase payment is created
263 *
264 * @param $data
265 * @param $action
266 */
267 public function add_log( $data, $action ) {
268 erp_log()->add(
269 [
270 'component' => 'Accounting',
271 'sub_component' => __( 'Pay Purchase', 'erp' ),
272 'old_value' => '',
273 'new_value' => '',
274 // translators: %1$s: amount, %2$s: id
275 'message' => sprintf( __( 'A purchase payment of %1$s has been created for %2$s', 'erp' ), $data['amount'], erp_acct_get_people_name_by_people_id( $data['people_id'] ) ),
276 'changetype' => $action,
277 'created_by' => get_current_user_id(),
278 ]
279 );
280 }
281
282 /**
283 * Prepare a single item for create or update
284 *
285 * @param WP_REST_Request $request request object
286 *
287 * @return array $prepared_item
288 */
289 protected function prepare_item_for_database( $request ) {
290 $prepared_item = [];
291
292 if ( isset( $request['vendor_id'] ) ) {
293 $prepared_item['vendor_id'] = $request['vendor_id'];
294 }
295
296 if ( isset( $request['ref'] ) ) {
297 $prepared_item['ref'] = $request['ref'];
298 }
299
300 if ( isset( $request['trn_date'] ) ) {
301 $prepared_item['trn_date'] = $request['trn_date'];
302 }
303
304 if ( isset( $request['purchase_details'] ) ) {
305 $prepared_item['purchase_details'] = $request['purchase_details'];
306 }
307
308 if ( isset( $request['amount'] ) ) {
309 $prepared_item['amount'] = $request['amount'];
310 }
311
312 if ( isset( $request['ref'] ) ) {
313 $prepared_item['ref'] = $request['ref'];
314 }
315
316 if ( isset( $request['trn_by'] ) ) {
317 $prepared_item['trn_by'] = $request['trn_by'];
318 }
319
320 if ( isset( $request['particulars'] ) ) {
321 $prepared_item['particulars'] = $request['particulars'];
322 }
323
324 if ( isset( $request['status'] ) ) {
325 $prepared_item['status'] = $request['status'];
326 }
327
328 if ( isset( $request['attachments'] ) ) {
329 $prepared_item['attachments'] = maybe_serialize( $request['attachments'] );
330 }
331
332 if ( isset( $request['billing_address'] ) ) {
333 $prepared_item['billing_address'] = maybe_serialize( $request['billing_address'] );
334 }
335
336 if ( isset( $request['type'] ) ) {
337 $prepared_item['voucher_type'] = $request['type'];
338 }
339
340 if ( isset( $request['check_no'] ) ) {
341 $prepared_item['check_no'] = $request['check_no'];
342 }
343
344 if ( isset( $request['deposit_to'] ) ) {
345 $prepared_item['deposit_to'] = $request['deposit_to'];
346 }
347
348 if ( isset( $request['name'] ) ) {
349 $prepared_item['name'] = $request['name'];
350 }
351
352 if ( isset( $request['bank'] ) ) {
353 $prepared_item['bank'] = $request['bank'];
354 }
355
356 return $prepared_item;
357 }
358
359 /**
360 * Prepare a single user output for response
361 *
362 * @param array|object $item
363 * @param WP_REST_Request $request request object
364 * @param array $additional_fields (optional)
365 *
366 * @return WP_REST_Response $response response data
367 */
368 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
369 $item = (object) $item;
370
371 $data = [
372 'id' => (int) $item->id,
373 'voucher_no' => (int) $item->voucher_no,
374 'vendor_id' => (int) $item->vendor_id,
375 'trn_date' => $item->trn_date,
376 'purchase_details' => $item->purchase_details,
377 'amount' => (float) $item->amount,
378 'particulars' => $item->particulars,
379 'attachments' => maybe_unserialize( $item->attachments ),
380 'status' => $item->status,
381 'created_at' => $item->created_at,
382 'trn_by' => erp_acct_get_payment_method_by_id( $item->trn_by )->name,
383 ];
384
385 $data = array_merge( $data, $additional_fields );
386
387 // Wrap the data in a response object
388 $response = rest_ensure_response( $data );
389
390 $response = $this->add_links( $response, $item, $additional_fields );
391
392 return $response;
393 }
394
395 /**
396 * Get the User's schema, conforming to JSON Schema
397 *
398 * @return array
399 */
400 public function get_item_schema() {
401 $schema = [
402 '$schema' => 'http://json-schema.org/draft-04/schema#',
403 'title' => 'pay_purchase',
404 'type' => 'object',
405 'properties' => [
406 'id' => [
407 'description' => __( 'Unique identifier for the resource.' ),
408 'type' => 'integer',
409 'context' => [ 'embed', 'view', 'edit' ],
410 'readonly' => true,
411 ],
412 'voucher_no' => [
413 'description' => __( 'Voucher no. for the resource.' ),
414 'type' => 'integer',
415 'context' => [ 'edit' ],
416 ],
417 'type' => [
418 'description' => __( 'Type for the resource.' ),
419 'type' => 'string',
420 'context' => [ 'edit' ],
421 'arg_options' => [
422 'sanitize_callback' => 'sanitize_text_field',
423 ],
424 ],
425 'vendor_id' => [
426 'description' => __( 'Vendor id for the resource.' ),
427 'type' => 'integer',
428 'context' => [ 'edit' ],
429 'arg_options' => [
430 'sanitize_callback' => 'sanitize_text_field',
431 ],
432 'required' => true,
433 ],
434 'trn_date' => [
435 'description' => __( 'Date for the resource.' ),
436 'type' => 'string',
437 'context' => [ 'edit' ],
438 'arg_options' => [
439 'sanitize_callback' => 'sanitize_text_field',
440 ],
441 'required' => true,
442 ],
443 'purchase_details' => [
444 'description' => __( 'List of line items data.', 'erp' ),
445 'type' => 'array',
446 'context' => [ 'view', 'edit' ],
447 'properties' => [
448 'id' => [
449 'description' => __( 'Product id.', 'erp' ),
450 'type' => 'string',
451 'context' => [ 'view', 'edit' ],
452 ],
453 'voucher_no' => [
454 'description' => __( 'Product type.', 'erp' ),
455 'type' => 'string',
456 'context' => [ 'view', 'edit' ],
457 ],
458 'due_date' => [
459 'description' => __( 'Unit price.', 'erp' ),
460 'type' => 'integer',
461 'context' => [ 'view', 'edit' ],
462 ],
463 'total' => [
464 'description' => __( 'Discount.', 'erp' ),
465 'type' => 'number',
466 'context' => [ 'view', 'edit' ],
467 ],
468 'due' => [
469 'description' => __( 'Discount.', 'erp' ),
470 'type' => 'number',
471 'context' => [ 'view', 'edit' ],
472 ],
473 'line_total' => [
474 'description' => __( 'Item total.' ),
475 'type' => 'number',
476 'context' => [ 'edit' ],
477 ],
478 ],
479 ],
480 'check_no' => [
481 'description' => __( 'Check no for the resource.' ),
482 'type' => 'string',
483 'context' => [ 'edit' ],
484 'arg_options' => [
485 'sanitize_callback' => 'sanitize_text_field',
486 ],
487 ],
488 'name' => [
489 'description' => __( 'Check name for the resource.' ),
490 'type' => 'string',
491 'context' => [ 'edit' ],
492 'arg_options' => [
493 'sanitize_callback' => 'sanitize_text_field',
494 ],
495 ],
496 'type' => [
497 'description' => __( 'Type for the resource.' ),
498 'type' => 'string',
499 'context' => [ 'edit' ],
500 'arg_options' => [
501 'sanitize_callback' => 'sanitize_text_field',
502 ],
503 ],
504 'status' => [
505 'description' => __( 'Status for the resource.' ),
506 'type' => 'integer',
507 'context' => [ 'edit' ],
508 ],
509 'particulars' => [
510 'description' => __( 'Particulars for the resource.' ),
511 'type' => 'string',
512 'context' => [ 'edit' ],
513 'arg_options' => [
514 'sanitize_callback' => 'sanitize_text_field',
515 ],
516 ],
517 'deposit_to' => [
518 'description' => __( 'Status for the resource.' ),
519 'type' => 'integer',
520 'context' => [ 'edit' ],
521 'required' => true,
522 ],
523 'trn_by' => [
524 'description' => __( 'Status for the resource.' ),
525 'type' => 'integer',
526 'context' => [ 'edit' ],
527 'required' => true,
528 ],
529 ],
530 ];
531
532 return $schema;
533 }
534 }
535