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 / includes / functions / rec-payments.php

rec-payments.php in ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce 1.6.7, at modules/accounting/includes/functions/rec-payments.php

652 lines 19.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /**
8 * Get all payments
9 *
10 * @return mixed
11 */
12 function erp_acct_get_payments( $args = [] ) {
13 global $wpdb;
14
15 $defaults = [
16 'number' => 20,
17 'offset' => 0,
18 'orderby' => 'id',
19 'order' => 'DESC',
20 'count' => false,
21 's' => '',
22 ];
23
24 $args = wp_parse_args( $args, $defaults );
25
26 $limit = '';
27
28 if ( '-1' !== $args['number'] ) {
29 $limit = "LIMIT {$args['number']} OFFSET {$args['offset']}";
30 }
31
32 $sql = 'SELECT';
33 $sql .= $args['count'] ? ' COUNT( id ) as total_number ' : ' * ';
34 $sql .= "FROM {$wpdb->prefix}erp_acct_invoice_receipts ORDER BY {$args['orderby']} {$args['order']} {$limit}";
35
36 if ( $args['count'] ) {
37 return $wpdb->get_var( $sql );
38 }
39
40 $payment_data = $wpdb->get_results( $sql, ARRAY_A );
41
42 return $payment_data;
43 }
44
45 /**
46 * Get a single payment
47 *
48 * @param $invoice_no
49 *
50 * @return mixed
51 */
52 function erp_acct_get_payment( $invoice_no ) {
53 global $wpdb;
54
55 $sql = "SELECT
56
57 pay_inv.id,
58 pay_inv.voucher_no,
59 pay_inv.customer_id,
60 pay_inv.customer_name,
61 pay_inv.trn_date,
62 pay_inv.amount,
63 pay_inv.trn_by,
64 pay_inv.trn_by_ledger_id,
65 pay_inv.particulars,
66 pay_inv.attachments,
67 pay_inv.status,
68 pay_inv.created_at,
69 pay_inv.transaction_charge,
70
71 pay_inv_detail.invoice_no,
72 pay_inv_detail.amount as pay_inv_detail_amount,
73
74 ledger_detail.particulars,
75 ledger_detail.debit,
76 ledger_detail.credit
77
78 from {$wpdb->prefix}erp_acct_invoice_receipts as pay_inv
79
80 LEFT JOIN {$wpdb->prefix}erp_acct_invoice_receipts_details as pay_inv_detail ON pay_inv.voucher_no = pay_inv_detail.voucher_no
81 LEFT JOIN {$wpdb->prefix}erp_acct_ledger_details as ledger_detail ON pay_inv.voucher_no = ledger_detail.trn_no
82
83 WHERE pay_inv.voucher_no = {$invoice_no}";
84
85 $row = $wpdb->get_row( $sql, ARRAY_A );
86
87 $row['line_items'] = erp_acct_format_payment_line_items( $invoice_no );
88 $row['pdf_link'] = erp_acct_pdf_abs_path_to_url( $invoice_no );
89
90 return $row;
91 }
92
93 /**
94 * Insert payment info
95 *
96 * @param $data
97 *
98 * @return mixed
99 */
100 function erp_acct_insert_payment( $data ) {
101 global $wpdb;
102
103 $created_by = get_current_user_id();
104 $data['created_at'] = date( 'Y-m-d H:i:s' );
105 $data['created_by'] = $created_by;
106 $voucher_no = null;
107 $currency = erp_get_currency( true );
108
109 try {
110 $wpdb->query( 'START TRANSACTION' );
111
112 $wpdb->insert(
113 $wpdb->prefix . 'erp_acct_voucher_no',
114 [
115 'type' => 'payment',
116 'currency' => $currency,
117 'created_at' => $data['created_at'],
118 'created_by' => $data['created_by'],
119 'updated_at' => isset( $data['updated_at'] ) ? $data['updated_at'] : '',
120 'updated_by' => isset( $data['updated_by'] ) ? $data['updated_by'] : '',
121 ]
122 );
123
124 $voucher_no = $wpdb->insert_id;
125
126 $payment_data = erp_acct_get_formatted_payment_data( $data, $voucher_no );
127
128 // check transaction charge
129 $transaction_charge = 0;
130
131 if ( isset( $payment_data['bank_trn_charge'] ) && 0 < (float) $payment_data['bank_trn_charge'] && 2 === (int) $payment_data['trn_by'] ) {
132 $transaction_charge = (float) $payment_data['bank_trn_charge'];
133 }
134
135 $wpdb->insert(
136 $wpdb->prefix . 'erp_acct_invoice_receipts',
137 [
138 'voucher_no' => $voucher_no,
139 'customer_id' => $payment_data['customer_id'],
140 'customer_name' => $payment_data['customer_name'],
141 'trn_date' => $payment_data['trn_date'],
142 'particulars' => $payment_data['particulars'],
143 'amount' => $payment_data['amount'],
144 'transaction_charge' => $transaction_charge,
145 'ref' => $payment_data['ref'],
146 'trn_by' => $payment_data['trn_by'],
147 'attachments' => $payment_data['attachments'],
148 'status' => $payment_data['status'],
149 'trn_by_ledger_id' => $payment_data['deposit_to'],
150 'created_at' => $payment_data['created_at'],
151 'created_by' => $payment_data['created_by'],
152 'updated_at' => $payment_data['updated_at'],
153 'updated_by' => $payment_data['updated_by'],
154 ]
155 );
156
157 $items = $payment_data['line_items'];
158
159 // for bank transaction charge
160 $deduct_unit_for_trn_charge = 0;
161
162 if ( $transaction_charge ) {
163 $deduct_unit_for_trn_charge = $transaction_charge / $payment_data['amount'];
164 }
165
166 foreach ( $items as $key => $item ) {
167 $total = 0;
168 $bank_transaction_charge = $deduct_unit_for_trn_charge ? $deduct_unit_for_trn_charge * $item['line_total'] : 0;
169
170 $invoice_no[ $key ] = $payment_data['invoice_no'];
171 $total += $item['line_total'];
172
173 $payment_data['amount'] = $total - $bank_transaction_charge;
174
175 erp_acct_insert_payment_line_items( $payment_data, $item, $voucher_no );
176 }
177
178 if ( isset( $payment_data['trn_by'] ) && 3 === $payment_data['trn_by'] ) {
179 erp_acct_insert_check_data( $payment_data );
180 }
181
182 // add transaction charge entry to ledger
183 if ( $transaction_charge ) {
184 erp_acct_insert_bank_transaction_charge_into_ledger( $payment_data );
185 }
186
187 $data['dr'] = 0;
188 $data['cr'] = $payment_data['amount'];
189 erp_acct_insert_data_into_people_trn_details( $data, $voucher_no );
190
191 do_action( 'erp_acct_after_payment_create', $payment_data, $voucher_no );
192
193 $wpdb->query( 'COMMIT' );
194 } catch ( Exception $e ) {
195 $wpdb->query( 'ROLLBACK' );
196
197 return new WP_error( 'payment-exception', $e->getMessage() );
198 }
199
200 foreach ( $items as $key => $item ) {
201 erp_acct_change_invoice_status( $item['invoice_no'] );
202 }
203
204 $payment = erp_acct_get_payment( $voucher_no );
205
206 $payment['email'] = erp_get_people_email( $data['customer_id'] );
207
208 do_action( 'erp_acct_new_transaction_payment', $voucher_no, $payment );
209
210 return $payment;
211 }
212
213 /**
214 * Insert payment line items
215 *
216 * @param $data
217 * @param $invoice_no
218 * @param $voucher_no
219 * @param $due
220 *
221 * @return int
222 */
223 function erp_acct_insert_payment_line_items( $data, $item, $voucher_no ) {
224 global $wpdb;
225
226 $payment_data = erp_acct_get_formatted_payment_data( $data, $voucher_no, $item['invoice_no'] );
227 $created_by = get_current_user_id();
228 $payment_data['created_at'] = date( 'Y-m-d H:i:s' );
229 $payment_data['created_by'] = $created_by;
230
231 $wpdb->insert(
232 $wpdb->prefix . 'erp_acct_invoice_receipts_details',
233 [
234 'voucher_no' => $voucher_no,
235 'invoice_no' => $item['invoice_no'],
236 'amount' => $item['line_total'],
237 'created_at' => $payment_data['created_at'],
238 'created_by' => $payment_data['created_by'],
239 'updated_at' => $payment_data['updated_at'],
240 'updated_by' => $payment_data['updated_by'],
241 ]
242 );
243
244 if ( 1 === $payment_data['status'] ) {
245 return;
246 }
247
248 $wpdb->insert(
249 $wpdb->prefix . 'erp_acct_invoice_account_details',
250 [
251 'invoice_no' => $item['invoice_no'],
252 'trn_no' => $voucher_no,
253 'trn_date' => $payment_data['trn_date'],
254 'particulars' => $payment_data['particulars'],
255 'debit' => 0,
256 'credit' => $item['line_total'],
257 'created_at' => $payment_data['created_at'],
258 'created_by' => $payment_data['created_by'],
259 'updated_at' => $payment_data['updated_at'],
260 'updated_by' => $payment_data['updated_by'],
261 ]
262 );
263
264 erp_acct_insert_payment_data_into_ledger( $payment_data );
265
266 return $voucher_no;
267 }
268
269 /**
270 * Update payment data
271 *
272 * @param $data
273 * @param $invoice_no
274 *
275 * @return mixed
276 */
277 function erp_acct_update_payment( $data, $voucher_no ) {
278 global $wpdb;
279
280 $updated_by = get_current_user_id();
281 $data['updated_at'] = date( 'Y-m-d H:i:s' );
282 $data['updated_by'] = $updated_by;
283
284 try {
285 $wpdb->query( 'START TRANSACTION' );
286
287 $payment_data = erp_acct_get_formatted_payment_data( $data, $voucher_no );
288
289 $wpdb->update(
290 $wpdb->prefix . 'erp_acct_invoice_receipts',
291 [
292 'trn_date' => $payment_data['trn_date'],
293 'particulars' => $payment_data['particulars'],
294 'amount' => $payment_data['amount'],
295 'trn_by' => $payment_data['trn_by'],
296 'trn_by_ledger_id' => $payment_data['deposit_to'],
297 'created_at' => $payment_data['created_at'],
298 'created_by' => $payment_data['created_by'],
299 'updated_at' => $payment_data['updated_at'],
300 'updated_by' => $payment_data['updated_by'],
301 ],
302 [
303 'voucher_no' => $voucher_no,
304 ]
305 );
306
307 $items = $payment_data['line_items'];
308
309 foreach ( $items as $key => $item ) {
310 $total = 0;
311
312 $invoice_no[ $key ] = $item['invoice_id'];
313 $total += $item['line_total'];
314
315 $payment_data['amount'] = $total;
316
317 erp_acct_update_payment_line_items( $payment_data, $voucher_no, $invoice_no[ $key ] );
318 }
319
320 if ( isset( $payment_data['trn_by'] ) && 3 === $payment_data['trn_by'] ) {
321 erp_acct_insert_check_data( $payment_data );
322 }
323
324 $wpdb->query( 'COMMIT' );
325 } catch ( Exception $e ) {
326 $wpdb->query( 'ROLLBACK' );
327
328 return new WP_error( 'payment-exception', $e->getMessage() );
329 }
330
331 foreach ( $items as $key => $item ) {
332 erp_acct_change_invoice_status( $item['invoice_no'] );
333 }
334
335 return erp_acct_get_payment( $voucher_no );
336 }
337
338 /**
339 * Insert payment line items
340 *
341 * @param $data
342 * @param $invoice_no
343 * @param $voucher_no
344 * @param $due
345 *
346 * @return int
347 */
348 function erp_acct_update_payment_line_items( $data, $invoice_no, $voucher_no ) {
349 global $wpdb;
350
351 $payment_data = erp_acct_get_formatted_payment_data( $data, $voucher_no, $invoice_no );
352
353 $wpdb->update(
354 $wpdb->prefix . 'erp_acct_invoice_receipts_details',
355 [
356 'voucher_no' => $voucher_no,
357 'amount' => $payment_data['amount'],
358 'created_at' => $payment_data['created_at'],
359 'created_by' => $payment_data['created_by'],
360 'updated_at' => $payment_data['updated_at'],
361 'updated_by' => $payment_data['updated_by'],
362 ],
363 [
364 'invoice_no' => $invoice_no,
365 ]
366 );
367
368 if ( 1 === $payment_data['status'] ) {
369 return;
370 }
371
372 $wpdb->update(
373 $wpdb->prefix . 'erp_acct_invoice_account_details',
374 [
375 'trn_no' => $voucher_no,
376 'particulars' => $payment_data['particulars'],
377 'trn_date' => $payment_data['trn_date'],
378 'debit' => 0,
379 'credit' => $payment_data['amount'],
380 'created_at' => $payment_data['created_at'],
381 'created_by' => $payment_data['created_by'],
382 'updated_at' => $payment_data['updated_at'],
383 'updated_by' => $payment_data['updated_by'],
384 ],
385 [
386 'invoice_no' => $invoice_no,
387 ]
388 );
389
390 erp_acct_insert_payment_data_into_ledger( $payment_data );
391
392 return $voucher_no;
393 }
394
395 /**
396 * Get formatted payment data
397 *
398 * @param $data
399 * @param $voucher_no
400 * @param $invoice_no
401 *
402 * @return mixed
403 */
404 function erp_acct_get_formatted_payment_data( $data, $voucher_no, $invoice_no = 0 ) {
405 $payment_data = [];
406
407 // We can pass the name from view... to reduce query load
408 $user_info = erp_get_people( $data['customer_id'] );
409 $company = new \WeDevs\ERP\Company();
410
411 $payment_data['voucher_no'] = ! empty( $voucher_no ) ? $voucher_no : 0;
412 $payment_data['invoice_no'] = ! empty( $invoice_no ) ? $invoice_no : 0;
413 $payment_data['customer_id'] = isset( $data['customer_id'] ) ? $data['customer_id'] : null;
414 $payment_data['customer_name'] = isset( $user_info ) ? $user_info->first_name . ' ' . $user_info->last_name : '';
415 $payment_data['trn_date'] = isset( $data['trn_date'] ) ? $data['trn_date'] : date( 'Y-m-d' );
416 $payment_data['line_items'] = isset( $data['line_items'] ) ? $data['line_items'] : [];
417 $payment_data['created_at'] = date( 'Y-m-d' );
418 $payment_data['amount'] = isset( $data['amount'] ) ? $data['amount'] : 0;
419 $payment_data['bank_trn_charge'] = isset( $data['bank_trn_charge'] ) ? $data['bank_trn_charge'] : 0;
420 $payment_data['ref'] = isset( $data['ref'] ) ? $data['ref'] : null;
421 $payment_data['attachments'] = isset( $data['attachments'] ) ? $data['attachments'] : '';
422 $payment_data['voucher_type'] = isset( $data['type'] ) ? $data['type'] : '';
423 // translators: %s: voucher_no
424 $payment_data['particulars'] = ! empty( $data['particulars'] ) ? $data['particulars'] : sprintf( __( 'Invoice receipt created with voucher no %s', 'erp' ), $voucher_no );
425 $payment_data['trn_by'] = isset( $data['trn_by'] ) ? $data['trn_by'] : '';
426 $payment_data['deposit_to'] = isset( $data['deposit_to'] ) ? $data['deposit_to'] : null;
427 $payment_data['status'] = isset( $data['status'] ) ? $data['status'] : null;
428 $payment_data['check_no'] = isset( $data['check_no'] ) ? $data['check_no'] : 0;
429 $payment_data['pay_to'] = isset( $user_info ) ? $user_info->first_name . ' ' . $user_info->last_name : '';
430 $payment_data['name'] = isset( $data['name'] ) ? $data['name'] : $company->name;
431 $payment_data['bank'] = isset( $data['bank'] ) ? $data['bank'] : '';
432 $payment_data['voucher_type'] = isset( $data['type'] ) ? $data['type'] : '';
433 $payment_data['created_at'] = isset( $data['created_at'] ) ? $data['created_at'] : null;
434 $payment_data['created_by'] = isset( $data['created_by'] ) ? $data['created_by'] : '';
435 $payment_data['updated_at'] = isset( $data['updated_at'] ) ? $data['updated_at'] : null;
436 $payment_data['updated_by'] = isset( $data['updated_by'] ) ? $data['updated_by'] : '';
437
438 return $payment_data;
439 }
440
441 /**
442 * Delete a payment
443 *
444 * @param $id
445 *
446 * @return void
447 */
448 function erp_acct_delete_payment( $id ) {
449 global $wpdb;
450
451 $wpdb->delete( $wpdb->prefix . 'erp_acct_invoice_receipts', [ 'voucher_no' => $id ] );
452 $wpdb->delete( $wpdb->prefix . 'erp_acct_invoice_receipts_details', [ 'voucher_no' => $id ] );
453 $wpdb->delete( $wpdb->prefix . 'erp_acct_invoice_account_details', [ 'invoice_no' => $id ] );
454 }
455
456 /**
457 * Void a payment
458 *
459 * @param $id
460 *
461 * @return void
462 */
463 function erp_acct_void_payment( $id ) {
464 global $wpdb;
465
466 if ( ! $id ) {
467 return;
468 }
469
470 $wpdb->update(
471 $wpdb->prefix . 'erp_acct_invoice_receipts',
472 [
473 'status' => 8,
474 ],
475 [ 'voucher_no' => $id ]
476 );
477
478 $wpdb->delete( $wpdb->prefix . 'erp_acct_ledger_details', [ 'trn_no' => $id ] );
479 $wpdb->delete( $wpdb->prefix . 'erp_acct_invoice_account_details', [ 'trn_no' => $id ] );
480 }
481
482 /**
483 * Update invoice status after a payment
484 *
485 * @param $invoice_no
486 *
487 * @return void
488 */
489 function erp_acct_change_invoice_status( $invoice_no ) {
490 global $wpdb;
491
492 $due = (float) erp_acct_get_invoice_due( $invoice_no );
493
494 if ( 0.00 === $due ) {
495 $wpdb->update(
496 $wpdb->prefix . 'erp_acct_invoices',
497 [
498 'status' => 4,
499 ],
500 [ 'voucher_no' => $invoice_no ]
501 );
502 } else {
503 $wpdb->update(
504 $wpdb->prefix . 'erp_acct_invoices',
505 [
506 'status' => 5,
507 ],
508 [ 'voucher_no' => $invoice_no ]
509 );
510 }
511 }
512
513 /**
514 * Insert Payment/s data into ledger
515 *
516 * @param array $payment_data
517 *
518 * @return mixed
519 */
520 function erp_acct_insert_payment_data_into_ledger( $payment_data ) {
521 global $wpdb;
522
523 if ( 1 === $payment_data['status'] || ( isset( $payment_data['trn_by'] ) && 4 === $payment_data['trn_by'] ) ) {
524 return;
525 }
526
527 // Insert amount in ledger_details
528 $wpdb->insert(
529 $wpdb->prefix . 'erp_acct_ledger_details',
530 [
531 'ledger_id' => $payment_data['deposit_to'],
532 'trn_no' => $payment_data['voucher_no'],
533 'particulars' => $payment_data['particulars'],
534 'debit' => $payment_data['amount'],
535 'credit' => 0,
536 'trn_date' => $payment_data['trn_date'],
537 'created_at' => $payment_data['created_at'],
538 'created_by' => $payment_data['created_by'],
539 'updated_at' => $payment_data['updated_at'],
540 'updated_by' => $payment_data['updated_by'],
541 ]
542 );
543 }
544
545 /**
546 * Insert Payment/s data into "Bank Transaction Charge"
547 *
548 * @param array $payment_data
549 *
550 * @return mixed
551 */
552 function erp_acct_insert_bank_transaction_charge_into_ledger( $payment_data ) {
553 global $wpdb;
554
555 if ( 1 === $payment_data['status'] || ( isset( $payment_data['trn_by'] ) && 4 === $payment_data['trn_by'] ) ) {
556 return;
557 }
558
559 // Insert amount in ledger_details
560 // get ledger id of "Bank Transaction Charge"
561 $ledger_data = erp_acct_get_ledger_by( 'slug', 'bank_transaction_charge' );
562
563 if ( empty( $ledger_data ) ) {
564 return;
565 }
566 $wpdb->insert(
567 $wpdb->prefix . 'erp_acct_ledger_details',
568 [
569 'ledger_id' => $ledger_data['id'],
570 'trn_no' => $payment_data['voucher_no'],
571 'particulars' => $payment_data['particulars'],
572 'debit' => $payment_data['bank_trn_charge'],
573 'credit' => 0,
574 'trn_date' => $payment_data['trn_date'],
575 'created_at' => $payment_data['created_at'],
576 'created_by' => $payment_data['created_by'],
577 'updated_at' => $payment_data['updated_at'],
578 'updated_by' => $payment_data['updated_by'],
579 ]
580 );
581 }
582
583 /**
584 * Update Payment/s data into ledger
585 *
586 * @param array $payment_data
587 * @param int $invoice_no
588 *
589 * @return mixed
590 */
591 function erp_acct_update_payment_data_in_ledger( $payment_data, $invoice_no ) {
592 global $wpdb;
593
594 if ( 1 === $payment_data['status'] || ( isset( $payment_data['trn_by'] ) && 4 === $payment_data['trn_by'] ) ) {
595 return;
596 }
597
598 // Update amount in ledger_details
599 $wpdb->update(
600 $wpdb->prefix . 'erp_acct_ledger_details',
601 [
602 'ledger_id' => $payment_data['deposit_to'],
603 'particulars' => $payment_data['particulars'],
604 'debit' => $payment_data['amount'],
605 'credit' => 0,
606 'trn_date' => $payment_data['trn_date'],
607 'created_at' => $payment_data['created_at'],
608 'created_by' => $payment_data['created_by'],
609 'updated_at' => $payment_data['updated_at'],
610 'updated_by' => $payment_data['updated_by'],
611 ],
612 [
613 'trn_no' => $invoice_no,
614 ]
615 );
616 }
617
618 /**
619 * Get Payment count
620 *
621 * @return int
622 */
623 function erp_acct_get_payment_count() {
624 global $wpdb;
625
626 $row = $wpdb->get_row( 'SELECT COUNT(*) as count FROM ' . $wpdb->prefix . 'erp_acct_invoice_receipts' );
627
628 return $row->count;
629 }
630
631 /**
632 * Format payment line items
633 *
634 * @param string $invoice
635 *
636 * @return array
637 */
638 function erp_acct_format_payment_line_items( $invoice = 'all' ) {
639 global $wpdb;
640
641 $sql = 'SELECT id, voucher_no, invoice_no, amount ';
642
643 if ( 'all' === $invoice ) {
644 $invoice_sql = '';
645 } else {
646 $invoice_sql = 'WHERE voucher_no = ' . $invoice;
647 }
648 $sql .= "FROM {$wpdb->prefix}erp_acct_invoice_receipts_details {$invoice_sql}";
649
650 return $wpdb->get_results( $sql, ARRAY_A );
651 }
652