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

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

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