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-ledgers.php

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

681 lines 22.2 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 Ledgers_Accounts_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/ledgers';
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_all_ledger_accounts' ],
40 'args' => $this->get_collection_params(),
41 'permission_callback' => function ( $request ) {
42 return current_user_can( 'erp_ac_view_account_lists' );
43 },
44 ],
45 [
46 'methods' => WP_REST_Server::CREATABLE,
47 'callback' => [ $this, 'create_ledger_account' ],
48 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
49 'permission_callback' => function ( $request ) {
50 return current_user_can( 'erp_ac_create_account' );
51 },
52 ],
53 'schema' => [ $this, 'get_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_ledger_account' ],
64 'args' => [
65 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
66 ],
67 'permission_callback' => function ( $request ) {
68 return current_user_can( 'erp_ac_view_single_account' );
69 },
70 ],
71 [
72 'methods' => WP_REST_Server::EDITABLE,
73 'callback' => [ $this, 'update_ledger_account' ],
74 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
75 'permission_callback' => function ( $request ) {
76 return current_user_can( 'erp_ac_edit_account' );
77 },
78 ],
79 [
80 'methods' => WP_REST_Server::DELETABLE,
81 'callback' => [ $this, 'delete_ledger_account' ],
82 'permission_callback' => function ( $request ) {
83 return current_user_can( 'erp_ac_delete_account' );
84 },
85 ],
86 'schema' => [ $this, 'get_item_schema' ],
87 ]
88 );
89
90 register_rest_route(
91 $this->namespace,
92 '/' . $this->rest_base . '/(?P<chart_id>[\d]+)' . '/accounts',
93 [
94 [
95 'methods' => WP_REST_Server::READABLE,
96 'callback' => [ $this, 'get_ledger_accounts_by_chart' ],
97 'args' => $this->get_collection_params(),
98 'permission_callback' => function ( $request ) {
99 return current_user_can( 'erp_ac_view_account_lists' );
100 },
101 ],
102 ]
103 );
104
105 register_rest_route(
106 $this->namespace,
107 '/' . $this->rest_base . '/accounts',
108 [
109 [
110 'methods' => WP_REST_Server::READABLE,
111 'callback' => [ $this, 'get_chart_accounts' ],
112 'args' => $this->get_collection_params(),
113 'permission_callback' => function ( $request ) {
114 return current_user_can( 'erp_ac_view_account_lists' );
115 },
116 ],
117 ]
118 );
119
120 register_rest_route(
121 $this->namespace,
122 '/' . $this->rest_base . '/bank-accounts',
123 [
124 [
125 'methods' => WP_REST_Server::READABLE,
126 'callback' => [ $this, 'get_bank_accounts' ],
127 'args' => $this->get_collection_params(),
128 'permission_callback' => function ( $request ) {
129 return current_user_can( 'erp_ac_view_account_lists' );
130 },
131 ],
132 ]
133 );
134
135 register_rest_route(
136 $this->namespace,
137 '/' . $this->rest_base . '/cash-accounts',
138 [
139 [
140 'methods' => WP_REST_Server::READABLE,
141 'callback' => [ $this, 'get_cash_accounts' ],
142 'args' => $this->get_collection_params(),
143 'permission_callback' => function ( $request ) {
144 return current_user_can( 'erp_ac_view_account_lists' );
145 },
146 ],
147 ]
148 );
149
150 /*
151 register_rest_route(
152 $this->namespace,
153 '/' . $this->rest_base . '/categories/(?P<chart_id>[\d]+)',
154 [
155 [
156 'methods' => WP_REST_Server::READABLE,
157 'callback' => [ $this, 'get_ledger_categories' ],
158 'args' => $this->get_collection_params(),
159 'permission_callback' => function( $request ) {
160 return current_user_can( 'erp_ac_view_account_lists' );
161 },
162 ],
163 [
164 'methods' => WP_REST_Server::CREATABLE,
165 'callback' => [ $this, 'create_ledger_category' ],
166 // 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
167 'permission_callback' => function( $request ) {
168 return current_user_can( 'erp_ac_create_account' );
169 },
170 ],
171 ]
172 );
173
174 register_rest_route(
175 $this->namespace,
176 '/' . $this->rest_base . '/categories/(?P<id>[\d]+)',
177 [
178 [
179 'methods' => WP_REST_Server::EDITABLE,
180 'callback' => [ $this, 'update_ledger_category' ],
181 // 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
182 'permission_callback' => function( $request ) {
183 return current_user_can( 'erp_ac_edit_account' );
184 },
185 ],
186 [
187 'methods' => WP_REST_Server::DELETABLE,
188 'callback' => [ $this, 'delete_ledger_category' ],
189 'permission_callback' => function( $request ) {
190 return current_user_can( 'erp_ac_delete_account' );
191 },
192 ],
193 ]
194 );
195 */
196 }
197
198 /**
199 * Get all the ledgers of a particular chart_id
200 *
201 * @param WP_REST_Request $request
202 *
203 * @return WP_Error|WP_REST_Response
204 */
205 public function get_all_ledger_accounts( $request ) {
206 $formatted_items = [];
207 $additional_fields = [];
208
209 $additional_fields['namespace'] = $this->namespace;
210 $additional_fields['rest_base'] = $this->rest_base;
211
212 $ledgers = erp_acct_get_ledgers_with_balances();
213
214 foreach ( $ledgers as $ledger ) {
215 $data = $this->prepare_ledger_for_response( $ledger, $request, $additional_fields );
216 $formatted_items[] = $this->prepare_response_for_collection( $data );
217 }
218
219 $response = rest_ensure_response( $formatted_items );
220 $response = $this->format_collection_response( $response, $request, count( $ledgers ) );
221
222 $response->set_status( 200 );
223
224 return $response;
225 }
226
227 /**
228 * Get all the ledgers of a particular chart_id
229 *
230 * @param WP_REST_Request $request
231 *
232 * @return WP_Error|WP_REST_Response
233 */
234 public function get_ledger_accounts_by_chart( $request ) {
235 $id = $request['chart_id'];
236
237 if ( empty( $id ) ) {
238 return new WP_Error( 'rest_empty_chart_id', __( 'Chart ID is Empty.' ), [ 'status' => 400 ] );
239 }
240
241 $items = erp_acct_get_ledgers_by_chart_id( $id );
242
243 $response = rest_ensure_response( $items );
244 $response->set_status( 200 );
245
246 return $response;
247 }
248
249 /**
250 * Get a specific ledger
251 *
252 * @param WP_REST_Request $request
253 *
254 * @return WP_Error|WP_REST_Response
255 */
256 public function get_ledger_account( $request ) {
257 global $wpdb;
258 $items = [];
259
260 $id = (int) $request['id'];
261
262 if ( empty( $id ) ) {
263 return new WP_Error( 'rest_ledger_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
264 }
265
266 $item = erp_acct_get_ledger( $id );
267
268 $result = $this->prepare_item_for_response( $item, $request );
269 $response = rest_ensure_response( $item );
270
271 return $response;
272 }
273
274 /**
275 * Create an ledger
276 *
277 * @param WP_REST_Request $request
278 *
279 * @return WP_Error|WP_REST_Request
280 */
281 public function create_ledger_account( $request ) {
282 global $wpdb;
283
284 $exist = $wpdb->get_var( $wpdb->prepare( "SELECT name FROM {$wpdb->prefix}erp_acct_ledgers WHERE name = %s", $request['name'] ) );
285
286 if ( $exist ) {
287 return new WP_Error( 'rest_ledger_name_already_exist', __( 'Name already exist.' ), [ 'status' => 404 ] );
288 }
289
290 $item = $this->prepare_item_for_database( $request );
291
292 $result = erp_acct_insert_ledger( $item );
293
294 $this->add_log( $result, 'add' );
295
296 $request->set_param( 'context', 'edit' );
297 $response = $this->prepare_item_for_response( $result, $request );
298 $response = rest_ensure_response( $response );
299 $response->set_status( 200 );
300 //$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $result['id'] ) ) );
301
302 return $response;
303 }
304
305 /**
306 * Update an ledger
307 *
308 * @param WP_REST_Request $request
309 *
310 * @return WP_Error|WP_REST_Request
311 */
312 public function update_ledger_account( $request ) {
313 global $wpdb;
314
315 $id = (int) $request['id'];
316
317 if ( empty( $id ) ) {
318 return new WP_Error( 'rest_ledger_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
319 }
320
321 $item = $this->prepare_item_for_database( $request );
322
323 $result = erp_acct_update_ledger( $item, $id );
324
325 $request->set_param( 'context', 'edit' );
326 $response = $this->prepare_item_for_response( $result, $request );
327 $response = rest_ensure_response( $response );
328 $response->set_status( 201 );
329 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
330
331 return $response;
332 }
333
334 /**
335 * Delete an account
336 *
337 * @param WP_REST_Request $request
338 *
339 * @return WP_Error|WP_REST_Request
340 */
341 public function delete_ledger_account( $request ) {
342 global $wpdb;
343
344 $id = (int) $request['id'];
345
346 if ( empty( $id ) ) {
347 return new WP_Error( 'rest_ledger_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
348 }
349
350 $wpdb->delete( "{$wpdb->prefix}erp_acct_ledgers", [ 'id' => $id ] );
351
352 return new WP_REST_Response( true, 204 );
353 }
354
355 /**
356 * Get chart of accounts
357 *
358 * @param WP_REST_REQUEST $request
359 *
360 * @return WP_ERROR|WP_REST_REQUEST
361 */
362 public function get_chart_accounts( $request ) {
363 $accounts = erp_acct_get_all_charts();
364
365 $response = rest_ensure_response( $accounts );
366
367 $response->set_status( 200 );
368
369 return $response;
370 }
371
372 /**
373 * Get a collection of bank accounts
374 *
375 * @param WP_REST_Request $request
376 *
377 * @return WP_Error|WP_REST_Response
378 */
379 public function get_bank_accounts( $request ) {
380 $items = erp_acct_get_banks( true, false, false );
381
382 if ( empty( $items ) ) {
383 return new WP_Error( 'rest_empty_accounts', __( 'Bank accounts are empty.' ), [ 'status' => 204 ] );
384 }
385
386 $formatted_items = [];
387
388 foreach ( $items as $item ) {
389 $additional_fields = [];
390
391 $data = $this->prepare_bank_item_for_response( $item, $request, $additional_fields );
392 $formatted_items[] = $this->prepare_response_for_collection( $data );
393 }
394
395 $response = rest_ensure_response( $formatted_items );
396 $response = $this->format_collection_response( $response, $request, 0 );
397
398 return $response;
399 }
400
401 /**
402 * Get cash accounts
403 *
404 * @param WP_REST_Request $request
405 *
406 * @return WP_Error|WP_REST_Response
407 */
408 public function get_cash_accounts( $request ) {
409 $args = [];
410 $args['start_date'] = date( 'Y-m-d' );
411
412 $closest_fy_date = erp_acct_get_closest_fn_year_date( $args['start_date'] );
413 $args['start_date'] = $closest_fy_date['start_date'];
414 $args['end_date'] = $closest_fy_date['end_date'];
415
416 $ledger_map = \WeDevs\ERP\Accounting\Includes\Classes\Ledger_Map::get_instance();
417 $ledger_id = $ledger_map->get_ledger_id_by_slug( 'cash' );
418
419 $c_balance = get_ledger_balance_with_opening_balance( $ledger_id, $args['start_date'], $args['end_date'] );
420
421 $item[] = [
422 'id' => $ledger_id,
423 'name' => 'Cash',
424 'obalance' => $c_balance['obalance'],
425 'balance' => $c_balance['balance'],
426 'total_debit' => $c_balance['total_debit'],
427 'total_credit' => $c_balance['total_credit'],
428 ];
429 $additional_fields = [];
430 $data = $this->prepare_bank_item_for_response( $item, $request, $additional_fields );
431
432 $response = rest_ensure_response( $data );
433 $response = $this->format_collection_response( $response, $request, 0 );
434
435 return $response;
436 }
437
438 /**
439 * Get ledger categories
440 *
441 * @param WP_REST_REQUEST $request
442 *
443 * @return WP_ERROR|WP_REST_REQUEST
444 */
445 public function get_ledger_categories( $request ) {
446 $chart_id = absint( $request['chart_id'] );
447
448 $categories = erp_acct_get_ledger_categories( $chart_id );
449
450 $response = rest_ensure_response( $categories );
451
452 $response->set_status( 200 );
453
454 return $response;
455 }
456
457 /**
458 * Create ledger categories
459 *
460 * @param WP_REST_REQUEST $request
461 *
462 * @return WP_ERROR|WP_REST_REQUEST
463 */
464 public function create_ledger_category( $request ) {
465 $category = erp_acct_create_ledger_category( $request );
466
467 if ( ! $category ) {
468 return new WP_Error( 'rest_ledger_already_exist', __( 'Category already exist.' ), [ 'status' => 404 ] );
469 }
470
471 $response = rest_ensure_response( $category );
472
473 $response->set_status( 200 );
474
475 return $response;
476 }
477
478 /**
479 * Update ledger categories
480 *
481 * @param WP_REST_REQUEST $request
482 *
483 * @return WP_ERROR|WP_REST_REQUEST
484 */
485 public function update_ledger_category( $request ) {
486 $id = (int) $request['id'];
487
488 if ( empty( $id ) ) {
489 return new WP_Error( 'rest_ledger_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
490 }
491
492 $category = erp_acct_update_ledger_category( $request );
493
494 if ( ! $category ) {
495 return new WP_Error( 'rest_ledger_already_exist', __( 'Category already exist.' ), [ 'status' => 404 ] );
496 }
497
498 $response = rest_ensure_response( $category );
499
500 $response->set_status( 200 );
501
502 return $response;
503 }
504
505 /**
506 * Remove category
507 */
508 public function delete_ledger_category( $request ) {
509 $id = (int) $request['id'];
510
511 if ( empty( $id ) ) {
512 return new WP_Error( 'rest_payment_invalid_id', __( 'Invalid resource id.' ), [ 'status' => 404 ] );
513 }
514
515 erp_acct_delete_ledger_category( $id );
516
517 return new WP_REST_Response( true, 204 );
518 }
519
520 /**
521 * Log when ledger is created
522 *
523 * @param $data
524 * @param $action
525 */
526 public function add_log( $data, $action ) {
527 $data = (array) $data;
528 erp_log()->add(
529 [
530 'component' => 'Accounting',
531 'sub_component' => __( 'Ledger Account', 'erp' ),
532 'old_value' => '',
533 'new_value' => '',
534 // translators: %1$s: name, %2$s: id
535 'message' => sprintf( __( 'A ledger account named %1$s has been created for %2$s', 'erp' ), $data['name'], erp_acct_get_people_name_by_people_id( $data['people_id'] ) ),
536 'changetype' => $action,
537 'created_by' => get_current_user_id(),
538 ]
539 );
540 }
541
542 /**
543 * Prepare a single item for create or update
544 *
545 * @param WP_REST_Request $request request object
546 *
547 * @return array $prepared_item
548 */
549 protected function prepare_item_for_database( $request ) {
550 $prepared_item = [];
551
552 $prepared_item['chart_id'] = ! empty( $request['chart_id'] ) ? (int) $request['chart_id'] : '';
553 $prepared_item['category_id'] = ! empty( $request['category_id'] ) ? (int) $request['category_id'] : null;
554 $prepared_item['name'] = ! empty( $request['name'] ) ? $request['name'] : '';
555 $prepared_item['code'] = ! empty( $request['code'] ) ? $request['code'] : null;
556
557 return $prepared_item;
558 }
559
560 /**
561 * Prepare a single user output for response
562 *
563 * @param object $item
564 * @param WP_REST_Request $request request object
565 * @param array $additional_fields (optional)
566 *
567 * @return WP_REST_Response $response response data
568 */
569 public function prepare_item_for_response( $item, $request, $additional_fields = [] ) {
570 $item = (object) $item;
571
572 $data = [
573 'id' => $item->id,
574 'chart_id' => $item->chart_id,
575 'category_id' => $item->category_id,
576 'name' => $item->name,
577 'slug' => $item->slug,
578 'code' => $item->code,
579 'trn_count' => erp_acct_get_ledger_trn_count( $item->id ),
580 'system' => $item->system,
581 'balance' => erp_acct_get_ledger_balance( $item->id ),
582 ];
583
584 $data = array_merge( $data, $additional_fields );
585
586 // Wrap the data in a response object
587 $response = rest_ensure_response( $data );
588
589 return $response;
590 }
591
592 /**
593 * Prepare a single user output for response
594 *
595 * @param object $item
596 * @param WP_REST_Request $request request object
597 * @param array $additional_fields (optional)
598 *
599 * @return WP_REST_Response $response response data
600 */
601 public function prepare_ledger_for_response( $item, $request, $additional_fields = [] ) {
602 $item = (array) $item;
603
604 $data = array_merge( $item, $additional_fields );
605
606 // Wrap the data in a response object
607 $response = rest_ensure_response( $data );
608
609 return $response;
610 }
611
612 /**
613 * @param $item
614 * @param $request
615 * @param $additional_fields
616 *
617 * @return mixed|WP_REST_Response
618 */
619 public function prepare_bank_item_for_response( $item, $request, $additional_fields ) {
620 $item = (array) $item;
621
622 $data = array_merge( $item, $additional_fields );
623
624 // Wrap the data in a response object
625 $response = rest_ensure_response( $data );
626
627 return $response;
628 }
629
630 /**
631 * Get the User's schema, conforming to JSON Schema
632 *
633 * @return array
634 */
635 public function get_item_schema() {
636 $schema = [
637 '$schema' => 'http://json-schema.org/draft-04/schema#',
638 'title' => 'chart of account',
639 'type' => 'object',
640 'properties' => [
641 'id' => [
642 'description' => __( 'Unique identifier for the resource.' ),
643 'type' => 'integer',
644 'context' => [ 'embed', 'view', 'edit' ],
645 'readonly' => true,
646 ],
647 'chart_id' => [
648 'description' => __( 'Type for the resource.' ),
649 'type' => 'integer',
650 'context' => [ 'edit' ],
651 'required' => true,
652 ],
653 'category_id' => [
654 'description' => __( 'Code for the resource.' ),
655 'type' => 'integer',
656 'context' => [ 'edit' ],
657 ],
658 'name' => [
659 'description' => __( 'Name for the resource.' ),
660 'type' => 'string',
661 'context' => [ 'edit' ],
662 'arg_options' => [
663 'sanitize_callback' => 'sanitize_text_field',
664 ],
665 'required' => true,
666 ],
667 'code' => [
668 'description' => __( 'Description for the resource.' ),
669 'type' => 'string',
670 'context' => [ 'edit' ],
671 'arg_options' => [
672 'sanitize_callback' => 'sanitize_text_field',
673 ],
674 ],
675 ],
676 ];
677
678 return $schema;
679 }
680 }
681