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.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 1.3.7 All 143 releases
erp / modules / accounting / api / class-rest-api-bills.php

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

634 lines 21.5 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 Bills_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/bills';
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_bills' ],
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_bill' ],
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_bill' ],
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_bill' ],
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 . '/due' . '/(?P<id>[\d]+)',
84 [
85 [
86 'methods' => WP_REST_Server::READABLE,
87 'callback' => [ $this, 'due_bills' ],
88 'args' => $this->get_collection_params(),
89 'permission_callback' => function ( $request ) {
90 return current_user_can( 'erp_ac_create_expenses_voucher' );
91 },
92 ],
93 ]
94 );
95
96 register_rest_route(
97 $this->namespace,
98 '/' . $this->rest_base . '/(?P<id>[\d]+)' . '/void',
99 [
100 [
101 'methods' => WP_REST_Server::CREATABLE,
102 'callback' => [ $this, 'void_bill' ],
103 'args' => [],
104 'permission_callback' => function ( $request ) {
105 return current_user_can( 'erp_ac_publish_expenses_voucher' );
106 },
107 ],
108 ]
109 );
110
111 register_rest_route(
112 $this->namespace,
113 '/' . $this->rest_base . '/overview-payable',
114 [
115 [
116 'methods' => WP_REST_Server::READABLE,
117 'callback' => [ $this, 'get_overview_payables' ],
118 'args' => [],
119 'permission_callback' => function ( $request ) {
120 return current_user_can( 'erp_ac_view_sales_summary' );
121 },
122 ],
123 ]
124 );
125 }
126
127 /**
128 * Get a collection of bills
129 *
130 * @param WP_REST_Request $request
131 *
132 * @return WP_Error|WP_REST_Response
133 */
134 public function get_bills( $request ) {
135 $args = [
136 'number' => isset( $request['per_page'] ) ? $request['per_page'] : 20,
137 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
138 ];
139
140 $formatted_items = [];
141 $additional_fields = [];
142
143 $additional_fields['namespace'] = $this->namespace;
144 $additional_fields['rest_base'] = $this->rest_base;
145
146 $bill_data = erp_acct_get_bills( $args );
147 $total_items = erp_acct_get_bills(
148 [
149 'count' => true,
150 'number' => -1,
151 ]
152 );
153
154 foreach ( $bill_data as $item ) {
155 if ( isset( $request['include'] ) ) {
156 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
157
158 if ( in_array( 'created_by', $include_params, true ) ) {
159 $item['created_by'] = $this->get_user( $item['created_by'] );
160 }
161 }
162
163 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
164 $formatted_items[] = $this->prepare_response_for_collection( $data );
165 }
166
167 $response = rest_ensure_response( $formatted_items );
168 $response = $this->format_collection_response( $response, $request, $total_items );
169
170 $response->set_status( 200 );
171
172 return $response;
173 }
174
175 /**
176 * Get a bill
177 *
178 * @param WP_REST_Request $request
179 *
180 * @return WP_Error|WP_REST_Response
181 */
182 public function get_bill( $request ) {
183 $id = (int) $request['id'];
184
185 if ( empty( $id ) ) {
186 return new WP_Error( 'rest_bill_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
187 }
188
189 $bill_data = erp_acct_get_bill( $id );
190 // $bill_data['id'] = $id;
191
192 // $bill_data['created_by'] = $this->get_user( $bill_data['created_by'] );
193
194 $additional_fields['namespace'] = $this->namespace;
195 $additional_fields['rest_base'] = $this->rest_base;
196
197 $data = $this->prepare_item_for_response( $bill_data, $request, $additional_fields );
198 $formatted_items = $this->prepare_response_for_collection( $data );
199 $response = rest_ensure_response( $formatted_items );
200
201 $response->set_status( 200 );
202
203 return $response;
204 }
205
206 /**
207 * Create a bill
208 *
209 * @param WP_REST_Request $request
210 *
211 * @return WP_Error|WP_REST_Response
212 */
213 public function create_bill( $request ) {
214 $bill_data = $this->prepare_item_for_database( $request );
215
216 $item_total = [];
217 $additional_fields = [];
218
219 $items = $request['bill_details'];
220
221 foreach ( $items as $key => $item ) {
222 $item_total[ $key ] = $item['amount'];
223 }
224
225 $bill_data['attachments'] = maybe_serialize( $bill_data['attachments'] );
226 $bill_data['billing_address'] = isset( $bill_data['billing_address'] ) ? maybe_serialize( $bill_data['billing_address'] ) : '';
227 $bill_data['amount'] = array_sum( $item_total );
228
229 $bill = erp_acct_insert_bill( $bill_data );
230
231 $this->add_log( $bill, 'add' );
232
233 // $bill_data['voucher_no'] = $bill_id;
234 $additional_fields['namespace'] = $this->namespace;
235 $additional_fields['rest_base'] = $this->rest_base;
236
237 $bill_data = $this->prepare_item_for_response( $bill, $request, $additional_fields );
238
239 $response = rest_ensure_response( $bill_data );
240 $response->set_status( 201 );
241
242 return $response;
243 }
244
245 /**
246 * Update a bill
247 *
248 * @param WP_REST_Request $request
249 *
250 * @return WP_Error|WP_REST_Response
251 */
252 public function update_bill( $request ) {
253 $id = (int) $request['id'];
254
255 if ( empty( $id ) ) {
256 return new WP_Error( 'rest_bill_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
257 }
258
259 $can_edit = erp_acct_check_voucher_edit_state( $id );
260
261 if ( ! $can_edit ) {
262 return new WP_Error( 'rest_bill_invalid_edit', __( 'Invalid edit permission for update.' ), [ 'status' => 403 ] );
263 }
264
265 $bill_data = $this->prepare_item_for_database( $request );
266
267 $item_total = [];
268 $additional_fields = [];
269
270 $items = $request['bill_details'];
271
272 foreach ( $items as $key => $item ) {
273 $item_total[ $key ] = $item['amount'];
274 }
275
276 $bill_data['attachments'] = maybe_serialize( $bill_data['attachments'] );
277 $bill_data['billing_address'] = isset( $bill_data['billing_address'] ) ? maybe_serialize( $bill_data['billing_address'] ) : '';
278 $bill_data['amount'] = array_sum( $item_total );
279
280 $bill = erp_acct_update_bill( $bill_data, $id );
281
282 $this->add_log( $bill, 'update' );
283
284 // $bill_data['voucher_no'] = $bill_id;
285 $additional_fields['namespace'] = $this->namespace;
286 $additional_fields['rest_base'] = $this->rest_base;
287
288 $bill_data = $this->prepare_item_for_response( $bill, $request, $additional_fields );
289
290 $response = rest_ensure_response( $bill_data );
291 $response->set_status( 200 );
292
293 return $response;
294 }
295
296 /**
297 * Void a bill
298 *
299 * @param WP_REST_Request $request
300 *
301 * @return WP_Error|WP_REST_Request
302 */
303 public function void_bill( $request ) {
304 $id = (int) $request['id'];
305
306 if ( empty( $id ) ) {
307 return new WP_Error( 'rest_bill_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
308 }
309
310 erp_acct_void_bill( $id );
311
312 return new WP_REST_Response( true, 204 );
313 }
314
315 /**
316 * Get a collection of bills with due of a people
317 *
318 * @param WP_REST_Request $request
319 *
320 * @return WP_Error|WP_REST_Response
321 */
322 public function due_bills( $request ) {
323 $id = (int) $request['id'];
324
325 if ( empty( $id ) ) {
326 return new WP_Error( 'rest_bill_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
327 }
328
329 $args = [
330 'number' => ! empty( $request['per_page'] ) ? $request['per_page'] : 20,
331 'offset' => ( $request['per_page'] * ( $request['page'] - 1 ) ),
332 ];
333
334 $formatted_items = [];
335 $additional_fields = [];
336
337 $additional_fields['namespace'] = $this->namespace;
338 $additional_fields['rest_base'] = $this->rest_base;
339
340 $bill_data = erp_acct_get_due_bills_by_people( [ 'people_id' => $id ] );
341 $total_items = erp_acct_get_due_bills_by_people(
342 [
343 'people_id' => $id,
344 'count' => true,
345 'number' => -1,
346 ]
347 );
348
349 foreach ( $bill_data as $item ) {
350 if ( isset( $request['include'] ) ) {
351 $include_params = explode( ',', str_replace( ' ', '', $request['include'] ) );
352
353 if ( in_array( 'created_by', $include_params, true ) ) {
354 $item['created_by'] = $this->get_user( $item['created_by'] );
355 }
356 }
357
358 $data = $this->prepare_item_for_response( $item, $request, $additional_fields );
359 $formatted_items[] = $this->prepare_response_for_collection( $data );
360 }
361
362 $response = rest_ensure_response( $formatted_items );
363 $response = $this->format_collection_response( $response, $request, $total_items );
364
365 $response->set_status( 200 );
366
367 return $response;
368 }
369
370 /**
371 * Get Dashboard Payable segments
372 *
373 * @param $request
374 *
375 * @return mixed|WP_REST_Response
376 */
377 public function get_overview_payables( $request ) {
378 $items = erp_acct_get_payables_overview();
379 $response = rest_ensure_response( $items );
380
381 $response->set_status( 200 );
382
383 return $response;
384 }
385
386 /**
387 * Log when bill is created
388 *
389 * @param $data
390 * @param $action
391 */
392 public function add_log( $data, $action ) {
393 erp_log()->add(
394 [
395 'component' => 'Accounting',
396 'sub_component' => __( 'Bill', 'erp' ),
397 'old_value' => '',
398 'new_value' => '',
399 // translators: %1$s: amount, %2$s: id
400 'message' => sprintf( __( 'A bill of %1$s has been created for %2$s', 'erp' ), $data['amount'], erp_acct_get_people_name_by_people_id( $data['vendor_id'] ) ),
401 'changetype' => $action,
402 'created_by' => get_current_user_id(),
403 ]
404 );
405 }
406
407 /**
408 * Prepare a single item for create or update
409 *
410 * @param WP_REST_Request $request request object
411 *
412 * @return array $prepared_item
413 */
414 protected function prepare_item_for_database( $request ) {
415 $prepared_item = [];
416
417 if ( isset( $request['vendor_id'] ) ) {
418 $prepared_item['vendor_id'] = $request['vendor_id'];
419 }
420
421 if ( isset( $request['trn_date'] ) ) {
422 $prepared_item['trn_date'] = $request['trn_date'];
423 }
424
425 if ( isset( $request['due_date'] ) ) {
426 $prepared_item['due_date'] = $request['due_date'];
427 }
428
429 if ( isset( $request['amount'] ) ) {
430 $prepared_item['total'] = (int) $request['amount'];
431 }
432
433 if ( isset( $request['due'] ) ) {
434 $prepared_item['due'] = (int) $request['due'];
435 }
436
437 if ( isset( $request['trn_no'] ) ) {
438 $prepared_item['trn_no'] = $request['trn_no'];
439 }
440
441 if ( isset( $request['trn_by'] ) ) {
442 $prepared_item['trn_by'] = $request['trn_by'];
443 }
444
445 if ( isset( $request['bill_details'] ) ) {
446 $prepared_item['bill_details'] = $request['bill_details'];
447 }
448
449 if ( isset( $request['status'] ) ) {
450 $prepared_item['status'] = $request['status'];
451 }
452
453 if ( isset( $request['particulars'] ) ) {
454 $prepared_item['particulars'] = $request['particulars'];
455 }
456
457 if ( isset( $request['attachments'] ) ) {
458 $prepared_item['attachments'] = $request['attachments'];
459 }
460
461 if ( isset( $request['billing_address'] ) ) {
462 $prepared_item['billing_address'] = maybe_serialize( $request['billing_address'] );
463 }
464
465 if ( isset( $request['ref'] ) ) {
466 $prepared_item['ref'] = $request['ref'];
467 }
468
469 $prepared_item['request'] = $request;
470
471 return $prepared_item;
472 }
473
474 /**
475 * Prepare a single user output for response
476 *
477 * @param array|object $item
478 * @param WP_REST_Request $request request object
479 * @param array $additional_fields (optional)
480 *
481 * @return WP_REST_Response $response response data
482 */
483 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
484 $item = (object) $item;
485
486 $data = [
487 'id' => (int) $item->id,
488 'editable' => ! empty( $item->editable ) ? (int) $item->editable : 1,
489 'voucher_no' => (int) $item->voucher_no,
490 'vendor_id' => (int) $item->vendor_id,
491 'vendor_name' => $item->vendor_name,
492 'trn_date' => $item->trn_date,
493 'due_date' => $item->due_date,
494 'billing_address' => ! empty( $item->billing_address ) ? $item->billing_address : erp_acct_get_people_address( $item->vendor_id ),
495 'bill_details' => ! empty( $item->bill_details ) ? $item->bill_details : [],
496 'amount' => (int) $item->amount,
497 'due' => ! empty( $item->due ) ? $item->due : $item->amount,
498 'ref' => ! empty( $item->ref ) ? $item->ref : '',
499 'particulars' => $item->particulars,
500 'status' => $item->status,
501 'created_at' => $item->created_at,
502 'attachments' => maybe_unserialize( $item->attachments ),
503 ];
504
505 $data = array_merge( $data, $additional_fields );
506
507 // Wrap the data in a response object
508 $response = rest_ensure_response( $data );
509
510 $response = $this->add_links( $response, $item, $additional_fields );
511
512 return $response;
513 }
514
515 /**
516 * Get the User's schema, conforming to JSON Schema
517 *
518 * @return array
519 */
520 public function get_item_schema() {
521 $schema = [
522 '$schema' => 'http://json-schema.org/draft-04/schema#',
523 'title' => 'bill',
524 'type' => 'object',
525 'properties' => [
526 'id' => [
527 'description' => __( 'Unique identifier for the resource.' ),
528 'type' => 'integer',
529 'context' => [ 'embed', 'view', 'edit' ],
530 'readonly' => true,
531 ],
532 'voucher_no' => [
533 'description' => __( 'Voucher no. for the resource.' ),
534 'type' => 'integer',
535 'context' => [ 'edit' ],
536 ],
537 'vendor_id' => [
538 'description' => __( 'People id for the resource.' ),
539 'type' => 'integer',
540 'context' => [ 'edit' ],
541 'arg_options' => [
542 'sanitize_callback' => 'sanitize_text_field',
543 ],
544 'required' => true,
545 ],
546 'trn_date' => [
547 'description' => __( 'Date for the resource.' ),
548 'type' => 'string',
549 'context' => [ 'edit' ],
550 'arg_options' => [
551 'sanitize_callback' => 'sanitize_text_field',
552 ],
553 'required' => true,
554 ],
555 'due_date' => [
556 'description' => __( 'Due date for the resource.' ),
557 'type' => 'string',
558 'context' => [ 'edit' ],
559 'arg_options' => [
560 'sanitize_callback' => 'sanitize_text_field',
561 ],
562 'required' => true,
563 ],
564 'ref' => [
565 'description' => __( 'Reference number for the resource.' ),
566 'type' => 'string',
567 'context' => [ 'edit' ],
568 'arg_options' => [
569 'sanitize_callback' => 'sanitize_text_field',
570 ],
571 ],
572 'billing_address' => [
573 'description' => __( 'Billing address for the resource.' ),
574 'type' => 'string',
575 'context' => [ 'edit' ],
576 'arg_options' => [
577 'sanitize_callback' => 'sanitize_text_field',
578 ],
579 'required' => true,
580 ],
581 'bill_details' => [
582 'description' => __( 'List of line items data.', 'erp' ),
583 'type' => 'array',
584 'context' => [ 'view', 'edit' ],
585 'properties' => [
586 'ledger_id' => [
587 'description' => __( 'Ledger id.', 'erp' ),
588 'type' => 'integer',
589 'context' => [ 'view', 'edit' ],
590 ],
591 'description' => [
592 'description' => __( 'Item Particular.', 'erp' ),
593 'type' => 'string',
594 'context' => [ 'view', 'edit' ],
595 'arg_options' => [
596 'sanitize_callback' => 'sanitize_text_field',
597 ],
598 ],
599 'amount' => [
600 'description' => __( 'Bill Amount', 'erp' ),
601 'type' => 'number',
602 'context' => [ 'view', 'edit' ],
603 ],
604 ],
605 ],
606 'status' => [
607 'description' => __( 'Status for the resource.' ),
608 'type' => 'integer',
609 'context' => [ 'edit' ],
610 'required' => true,
611 ],
612 'type' => [
613 'description' => __( 'Item Type.', 'erp' ),
614 'type' => 'string',
615 'context' => [ 'edit' ],
616 'arg_options' => [
617 'sanitize_callback' => 'sanitize_text_field',
618 ],
619 ],
620 'particulars' => [
621 'description' => __( 'Bill Particular.', 'erp' ),
622 'type' => 'string',
623 'context' => [ 'edit' ],
624 'arg_options' => [
625 'sanitize_callback' => 'sanitize_text_field',
626 ],
627 ],
628 ],
629 ];
630
631 return $schema;
632 }
633 }
634