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 / transactions.php

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

1,414 lines 45.7 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 sales transactions
9 *
10 * @param array $args
11 *
12 * @return mixed
13 */
14 function erp_acct_get_sales_transactions( $args = [] ) {
15 global $wpdb;
16
17 $defaults = [
18 'number' => 20,
19 'offset' => 0,
20 'order' => 'DESC',
21 'count' => false,
22 'customer_id' => false,
23 's' => '',
24 'status' => '',
25 ];
26
27 $args = wp_parse_args( $args, $defaults );
28
29 $limit = '';
30
31 $where = "WHERE (voucher.type = 'invoice' OR voucher.type = 'payment')";
32
33 if ( ! empty( $args['customer_id'] ) ) {
34 $where .= " AND invoice.customer_id = {$args['customer_id']} OR invoice_receipt.customer_id = {$args['customer_id']} ";
35 }
36
37 if ( ! empty( $args['start_date'] ) ) {
38 $where .= " AND invoice.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}' OR invoice_receipt.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
39 }
40
41 if ( empty( $args['status'] ) ) {
42 $where .= '';
43 } else {
44 if ( ! empty( $args['status'] ) ) {
45 $where .= " AND invoice.status={$args['status']} OR invoice_receipt.status={$args['status']} ";
46 }
47 }
48
49 if ( -1 !== $args['number'] ) {
50 $limit = "LIMIT {$args['number']} OFFSET {$args['offset']}";
51 }
52
53 $sql = 'SELECT';
54
55 if ( $args['count'] ) {
56 $sql .= ' COUNT( DISTINCT voucher.id ) AS total_number';
57 } else {
58 $sql .= ' voucher.id,
59 voucher.type,
60 voucher.editable,
61 invoice.customer_id AS inv_cus_id,
62 invoice.customer_name AS inv_cus_name,
63 invoice_receipt.customer_name AS pay_cus_name,
64 invoice.trn_date AS invoice_trn_date,
65 invoice_receipt.trn_date AS payment_trn_date,
66 invoice_receipt.ref,
67 invoice.due_date,
68 invoice.estimate,
69 (invoice.amount + invoice.tax) - invoice.discount AS sales_amount,
70 SUM(invoice_account_detail.debit - invoice_account_detail.credit) AS due,
71 invoice_receipt.amount AS payment_amount,
72 invoice.status as inv_status,
73 invoice_receipt.status as pay_status';
74 }
75
76 $sql .= " FROM {$wpdb->prefix}erp_acct_voucher_no AS voucher
77 LEFT JOIN {$wpdb->prefix}erp_acct_invoices AS invoice ON invoice.voucher_no = voucher.id
78 LEFT JOIN {$wpdb->prefix}erp_acct_invoice_receipts AS invoice_receipt ON invoice_receipt.voucher_no = voucher.id
79 LEFT JOIN {$wpdb->prefix}erp_acct_invoice_account_details AS invoice_account_detail ON invoice_account_detail.invoice_no = invoice.voucher_no
80 {$where} GROUP BY voucher.id ORDER BY voucher.id {$args['order']} {$limit}";
81
82 if ( $args['count'] ) {
83 $wpdb->get_results( $sql );
84
85 return $wpdb->num_rows;
86 }
87
88 // error_log(print_r($sql, true));
89 return $wpdb->get_results( $sql, ARRAY_A );
90 }
91
92 /**
93 * Get sales chart status
94 *
95 * @param array $args
96 *
97 * @return array|object|null
98 */
99 function erp_acct_get_sales_chart_status( $args = [] ) {
100 global $wpdb;
101
102 $where = 'WHERE invoice.estimate<>1';
103
104 if ( ! empty( $args['start_date'] ) ) {
105 $where .= " AND invoice.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
106 }
107
108 if ( ! empty( $args['people_id'] ) ) {
109 $where .= " AND invoice.customer_id = {$args['people_id']} ";
110 }
111
112 $sql = "SELECT COUNT(invoice.status) AS sub_total, status_type.type_name
113 FROM {$wpdb->prefix}erp_acct_trn_status_types AS status_type
114 LEFT JOIN {$wpdb->prefix}erp_acct_invoices AS invoice ON invoice.status = status_type.id {$where}
115 GROUP BY status_type.id HAVING COUNT(invoice.status) > 0 ORDER BY status_type.type_name ASC";
116
117 // error_log(print_r($sql, true));
118 return $wpdb->get_results( $sql, ARRAY_A );
119 }
120
121 /**
122 * Get sales chart payment
123 *
124 * @param array $args
125 *
126 * @return array|object|void|null
127 */
128 function erp_acct_get_sales_chart_payment( $args = [] ) {
129 global $wpdb;
130
131 $where = ' WHERE invoice.estimate<>1 AND invoice.status<>1';
132
133 if ( ! empty( $args['start_date'] ) ) {
134 $where .= " AND invoice.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
135 }
136
137 if ( ! empty( $args['people_id'] ) ) {
138 $where .= " AND invoice.customer_id = {$args['people_id']} ";
139 }
140
141 $sql = "SELECT SUM(credit) as received, SUM(balance) AS outstanding
142 FROM ( SELECT invoice.voucher_no, SUM(invoice_acc_detail.credit) AS credit, SUM( invoice_acc_detail.debit - invoice_acc_detail.credit) AS balance
143 FROM {$wpdb->prefix}erp_acct_invoices AS invoice
144 LEFT JOIN {$wpdb->prefix}erp_acct_invoice_account_details AS invoice_acc_detail ON invoice.voucher_no = invoice_acc_detail.invoice_no {$where}
145 GROUP BY invoice.voucher_no) AS get_amount";
146
147 // error_log(print_r($sql, true));
148 return $wpdb->get_row( $sql, ARRAY_A );
149 }
150
151 /**
152 * Get bill chart data
153 *
154 * @param array $args
155 *
156 * @return array|object|null
157 */
158 function erp_acct_get_bill_chart_data( $args = [] ) {
159 global $wpdb;
160
161 $where = ' WHERE bill.status != 1';
162
163 if ( ! empty( $args['start_date'] ) ) {
164 $where .= " AND bill.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
165 }
166
167 if ( ! empty( $args['people_id'] ) ) {
168 $where .= " AND bill.vendor_id = {$args['people_id']} ";
169 }
170
171 $sql = "SELECT SUM(debit) as paid, ABS(SUM(balance)) AS payable
172 FROM ( SELECT bill.voucher_no, SUM(bill_acc_detail.debit) AS debit, SUM( bill_acc_detail.debit - bill_acc_detail.credit) AS balance
173 FROM {$wpdb->prefix}erp_acct_bills AS bill
174 LEFT JOIN {$wpdb->prefix}erp_acct_bill_account_details AS bill_acc_detail ON bill.voucher_no = bill_acc_detail.bill_no {$where}
175 GROUP BY bill.voucher_no) AS get_amount";
176
177 return $wpdb->get_row( $sql, ARRAY_A );
178 }
179
180 /**
181 * Get bill chart status
182 *
183 * @param array $args
184 *
185 * @return array|object|null
186 */
187 function erp_acct_get_bill_chart_status( $args = [] ) {
188 global $wpdb;
189
190 $where = '';
191
192 if ( ! empty( $args['start_date'] ) ) {
193 $where .= "WHERE bill.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
194 }
195
196 if ( ! empty( $args['people_id'] ) ) {
197 $where .= " AND bill.vendor_id = {$args['people_id']} ";
198 }
199
200 $sql = "SELECT status_type.type_name, COUNT(bill.status) AS sub_total
201 FROM {$wpdb->prefix}erp_acct_trn_status_types AS status_type
202 LEFT JOIN {$wpdb->prefix}erp_acct_bills AS bill ON bill.status = status_type.id {$where}
203 GROUP BY status_type.id
204 HAVING sub_total > 0
205 ORDER BY status_type.type_name ASC";
206
207 return $wpdb->get_results( $sql, ARRAY_A );
208 }
209
210 /**
211 * Get expense chart data
212 *
213 * @param array $args
214 *
215 * @return array|object|null
216 */
217 function erp_acct_get_purchase_chart_data( $args = [] ) {
218 global $wpdb;
219
220 $where = ' WHERE purchase.purchase_order<>1 AND purchase.status<>1';
221
222 if ( ! empty( $args['start_date'] ) ) {
223 $where .= " AND purchase.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
224 }
225
226 if ( ! empty( $args['people_id'] ) ) {
227 $where .= " AND purchase.vendor_id = {$args['people_id']} ";
228 }
229
230 $sql = "SELECT SUM(debit) as paid, ABS(SUM(balance)) AS payable
231 FROM ( SELECT purchase.voucher_no, SUM(purchase_acc_detail.debit) AS debit, SUM( purchase_acc_detail.debit - purchase_acc_detail.credit) AS balance
232 FROM {$wpdb->prefix}erp_acct_purchase AS purchase
233 LEFT JOIN {$wpdb->prefix}erp_acct_purchase_account_details AS purchase_acc_detail ON purchase.voucher_no = purchase_acc_detail.purchase_no {$where}
234 GROUP BY purchase.voucher_no) AS get_amount";
235
236 $result = $wpdb->get_row( $sql, ARRAY_A );
237
238 return $result;
239 }
240
241 /**
242 * Get expense chart status
243 *
244 * @param array $args
245 *
246 * @return array|object|null
247 */
248 function erp_acct_get_purchase_chart_status( $args = [] ) {
249 global $wpdb;
250
251 $where = 'WHERE purchase.purchase_order<>1';
252
253 if ( ! empty( $args['start_date'] ) ) {
254 $where .= " AND purchase.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
255 }
256
257 if ( ! empty( $args['people_id'] ) ) {
258 $where .= " AND purchase.vendor_id = {$args['people_id']} ";
259 }
260
261 $sql = "SELECT status_type.type_name, COUNT(purchase.status) AS sub_total
262 FROM {$wpdb->prefix}erp_acct_trn_status_types AS status_type
263 LEFT JOIN {$wpdb->prefix}erp_acct_purchase AS purchase ON purchase.status = status_type.id {$where}
264 GROUP BY status_type.id
265 HAVING sub_total > 0
266 ORDER BY status_type.type_name ASC";
267
268 $result = $wpdb->get_results( $sql, ARRAY_A );
269
270 return $result;
271 }
272
273 /**
274 * Get expense chart data
275 *
276 * @param array $args
277 *
278 * @return array|object|null
279 */
280 function erp_acct_get_expense_chart_data( $args = [] ) {
281 global $wpdb;
282
283 $where = '';
284
285 if ( ! empty( $args['start_date'] ) ) {
286 $where .= "WHERE bill.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
287 }
288
289 if ( ! empty( $args['people_id'] ) ) {
290 $where .= " AND bill.people_id = {$args['people_id']} ";
291 }
292
293 $sql = "SELECT SUM(balance) as paid, 0 AS payable
294 FROM ( SELECT bill.voucher_no, bill_acc_detail.amount AS balance
295 FROM {$wpdb->prefix}erp_acct_expenses AS bill
296 LEFT JOIN {$wpdb->prefix}erp_acct_expense_details AS bill_acc_detail ON bill.voucher_no = bill_acc_detail.trn_no {$where} HAVING balance > 0 ) AS get_amount";
297
298 return $wpdb->get_row( $sql, ARRAY_A );
299 }
300
301 /**
302 * Get expense chart status
303 *
304 * @param array $args
305 *
306 * @return array|object|null
307 */
308 function erp_acct_get_expense_chart_status( $args = [] ) {
309 global $wpdb;
310
311 $where = '';
312
313 if ( ! empty( $args['start_date'] ) ) {
314 $where .= "WHERE bill.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
315 }
316
317 if ( ! empty( $args['people_id'] ) ) {
318 $where .= " AND bill.people_id = {$args['people_id']} ";
319 }
320
321 $sql = "SELECT status_type.type_name, COUNT(bill.status) AS sub_total
322 FROM {$wpdb->prefix}erp_acct_trn_status_types AS status_type
323 LEFT JOIN {$wpdb->prefix}erp_acct_expenses AS bill ON bill.status = status_type.id {$where}
324 GROUP BY status_type.id
325 HAVING sub_total > 0
326 ORDER BY status_type.type_name ASC";
327
328 return $wpdb->get_row( $sql, ARRAY_A );
329 }
330
331 /**
332 * Get Income Expense Chart data for dashbaord
333 *
334 * @return array|object|null
335 */
336 function erp_acct_get_income_expense_chart_data() {
337 $income_chart_id = 4; //Default db value
338 $expense_chart_id = 5; //Default db value
339
340 //Generate current month data
341
342 $incomes = erp_acct_get_daily_balance_by_chart_id( $income_chart_id, 'current' );
343 $incomes_monthly = erp_acct_format_daily_data_to_yearly_data( $incomes );
344 $expenses = erp_acct_get_daily_balance_by_chart_id( $expense_chart_id, 'current' );
345 $expenses_monthly = erp_acct_format_daily_data_to_yearly_data( $expenses );
346
347 $this_month = [
348 'labels' => array_keys( $incomes_monthly ),
349 'income' => array_values( $incomes_monthly ),
350 'expense' => array_values( $expenses_monthly ),
351 ];
352
353 //Generate last month data
354
355 $incomes = erp_acct_get_daily_balance_by_chart_id( $income_chart_id, 'last' );
356 $incomes_monthly = erp_acct_format_daily_data_to_yearly_data( $incomes );
357 $expenses = erp_acct_get_daily_balance_by_chart_id( $expense_chart_id, 'last' );
358 $expenses_monthly = erp_acct_format_daily_data_to_yearly_data( $expenses );
359
360 $last_month = [
361 'labels' => array_keys( $incomes_monthly ),
362 'income' => array_values( $incomes_monthly ),
363 'expense' => array_values( $expenses_monthly ),
364 ];
365
366 $current_year = date( 'Y' );
367 $start_date = $current_year . '-01-01';
368 $end_date = $current_year . '-12-31';
369
370 $incomes = erp_acct_get_monthly_balance_by_chart_id( $start_date, $end_date, $income_chart_id );
371 $income_data = erp_acct_format_monthly_data_to_yearly_data( $incomes );
372
373 $expenses = erp_acct_get_monthly_balance_by_chart_id( $start_date, $end_date, $expense_chart_id );
374 $expense_data = erp_acct_format_monthly_data_to_yearly_data( $expenses );
375
376 $this_year = [
377 'labels' => array_keys( $income_data ),
378 'income' => array_values( $income_data ),
379 'expense' => array_values( $expense_data ),
380 ];
381
382 //Generate last year data
383 $last_year = $current_year - 1;
384 $start_date = $last_year . '-01-01';
385 $end_date = $last_year . '-12-31';
386
387 $incomes = erp_acct_get_monthly_balance_by_chart_id( $start_date, $end_date, $income_chart_id );
388 $income_data = erp_acct_format_monthly_data_to_yearly_data( $incomes );
389
390 $expenses = erp_acct_get_monthly_balance_by_chart_id( $start_date, $end_date, $expense_chart_id );
391 $expense_data = erp_acct_format_monthly_data_to_yearly_data( $expenses );
392 $last_yr = [
393 'labels' => array_keys( $income_data ),
394 'income' => array_values( $income_data ),
395 'expense' => array_values( $expense_data ),
396 ];
397
398 return [
399 'thisMonth' => $this_month,
400 'lastMonth' => $last_month,
401 'thisYear' => $this_year,
402 'lastYear' => $last_yr,
403 ];
404 }
405
406 /**
407 * Get Balance amount for given chart of account in time range
408 *
409 * @param $start_date
410 * @param $end_date
411 * @param $chart_id
412 *
413 * @return array|object|null
414 */
415 function erp_acct_get_monthly_balance_by_chart_id( $start_date, $end_date, $chart_id ) {
416 global $wpdb;
417
418 $ledger_details = $wpdb->prefix . 'erp_acct_ledger_details';
419 $ledgers = $wpdb->prefix . 'erp_acct_ledgers';
420 $chart_of_accs = $wpdb->prefix . 'erp_acct_chart_of_accounts';
421
422 $query = "Select Month(ld.trn_date) as month, SUM( ld.debit-ld.credit ) as balance
423 From $ledger_details as ld
424 Inner Join $ledgers as al on al.id = ld.ledger_id
425 Inner Join $chart_of_accs as ca on ca.id = al.chart_id
426 Where ca.id = %d
427 AND ld.trn_date BETWEEN %s AND %s
428 Group By Month(ld.trn_date)";
429
430 $results = $wpdb->get_results( $wpdb->prepare( $query, $chart_id, $start_date, $end_date ), ARRAY_A );
431
432 return $results;
433 }
434
435 /**
436 * Format Monthly result to Yearly data
437 *
438 * @param $result
439 *
440 * @return array
441 */
442 function erp_acct_format_monthly_data_to_yearly_data( $result ) {
443 $default_year_data = [
444 'Jan' => 0,
445 'Feb' => 0,
446 'Mar' => 0,
447 'Apr' => 0,
448 'May' => 0,
449 'Jun' => 0,
450 'Jul' => 0,
451 'Aug' => 0,
452 'Sep' => 0,
453 'Oct' => 0,
454 'Nov' => 0,
455 'Dec' => 0,
456 ];
457
458 $result = array_map(
459 function ( $item ) {
460 $item['month'] = date( 'M', mktime( 0, 0, 0, $item['month'] ) );
461 $item['balance'] = abs( $item['balance'] );
462
463 return $item;
464 },
465 $result
466 );
467
468 $labels = wp_list_pluck( $result, 'month' );
469 $balance = wp_list_pluck( $result, 'balance' );
470
471 $this_yr_data = array_combine( $labels, $balance );
472
473 $this_yr_data = wp_parse_args( $this_yr_data, $default_year_data );
474
475 return $this_yr_data;
476 }
477
478 /**
479 * Get Balance amount for given chart of account in time range
480 *
481 * @param $chart_id
482 * @param string $month
483 *
484 * @return array|object|null
485 */
486 function erp_acct_get_daily_balance_by_chart_id( $chart_id, $month = 'current' ) {
487 global $wpdb;
488 $start_date = null;
489 $end_date = null;
490
491 switch ( $month ) {
492 case 'current':
493 $start_date = date( 'Y-m-d', strtotime( 'first day of this month' ) );
494 $end_date = date( 'Y-m-d', strtotime( 'last day of this month' ) );
495 break;
496
497 case 'last':
498 $start_date = date( 'Y-m-d', strtotime( 'first day of previous month' ) );
499 $end_date = date( 'Y-m-d', strtotime( 'last day of previous month' ) );
500 break;
501 default:
502 break;
503 }
504
505 $ledger_details = $wpdb->prefix . 'erp_acct_ledger_details';
506 $ledgers = $wpdb->prefix . 'erp_acct_ledgers';
507 $chart_of_accs = $wpdb->prefix . 'erp_acct_chart_of_accounts';
508
509 $query = "Select ld.trn_date as day, SUM( ld.debit-ld.credit ) as balance
510 From $ledger_details as ld
511 Inner Join $ledgers as al on al.id = ld.ledger_id
512 Inner Join $chart_of_accs as ca on ca.id = al.chart_id
513 Where ca.id = %d
514 AND ld.trn_date BETWEEN %s AND %s
515 Group By ld.trn_date";
516
517 $results = $wpdb->get_results( $wpdb->prepare( $query, $chart_id, $start_date, $end_date ), ARRAY_A );
518
519 return $results;
520 }
521
522 /**
523 * Format Daily result to Yearly data
524 *
525 * @param $result
526 *
527 * @return array
528 */
529 function erp_acct_format_daily_data_to_yearly_data( $result ) {
530 $result = array_map(
531 function ( $item ) {
532 $item['day'] = date( 'd-m', strtotime( $item['day'] ) );
533 $item['balance'] = abs( $item['balance'] );
534
535 return $item;
536 },
537 $result
538 );
539
540 $labels = wp_list_pluck( $result, 'day' );
541 $balance = wp_list_pluck( $result, 'balance' );
542
543 $monthly_data = array_combine( $labels, $balance );
544
545 return $monthly_data;
546 }
547
548 /**
549 * Get all Expenses
550 *
551 * @param array $args
552 *
553 * @return mixed
554 */
555 function erp_acct_get_expense_transactions( $args = [] ) {
556 global $wpdb;
557
558 $defaults = [
559 'number' => 20,
560 'offset' => 0,
561 'order' => 'DESC',
562 'count' => false,
563 'vendor_id' => false,
564 's' => '',
565 'status' => '',
566 ];
567
568 $args = wp_parse_args( $args, $defaults );
569
570 $limit = '';
571
572 $where = "WHERE (voucher.type = 'pay_bill' OR voucher.type = 'bill' OR voucher.type = 'expense' OR voucher.type = 'check' ) ";
573
574 if ( ! empty( $args['vendor_id'] ) ) {
575 $where .= " AND bill.vendor_id = {$args['vendor_id']} OR pay_bill.vendor_id = {$args['vendor_id']} ";
576 }
577
578 if ( ! empty( $args['start_date'] ) ) {
579 $where .= " AND bill.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}' OR pay_bill.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
580 }
581
582 if ( 0 === $args['status'] ) {
583 $where .= '';
584 } else {
585 if ( ! empty( $args['status'] ) ) {
586 $where .= " AND bill.status={$args['status']} OR pay_bill.status={$args['status']} OR expense.status={$args['status']} ";
587 }
588 }
589
590 if ( -1 !== $args['number'] ) {
591 $limit = "LIMIT {$args['number']} OFFSET {$args['offset']}";
592 }
593
594 $sql = 'SELECT';
595
596 if ( $args['count'] ) {
597 $sql .= ' COUNT( DISTINCT voucher.id ) AS total_number';
598 } else {
599 $sql .= ' voucher.id,
600 voucher.type,
601 bill.vendor_id AS vendor_id,
602 bill.vendor_name AS vendor_name,
603 pay_bill.vendor_name AS pay_bill_vendor_name,
604 expense.people_name AS expense_people_name,
605 bill.trn_date AS bill_trn_date,
606 pay_bill.trn_date AS pay_bill_trn_date,
607 expense.trn_date AS expense_trn_date,
608 bill.due_date,
609 bill.amount,
610 bill.ref,
611 expense.ref AS exp_ref,
612 pay_bill.amount as pay_bill_amount,
613 expense.amount as expense_amount,
614 SUM(bill_acct_details.debit - bill_acct_details.credit) AS due,
615 bill.status as bill_status,
616 pay_bill.status as pay_bill_status,
617 expense.status as expense_status';
618 }
619
620 $sql .= " FROM {$wpdb->prefix}erp_acct_voucher_no AS voucher
621 LEFT JOIN {$wpdb->prefix}erp_acct_bills AS bill ON bill.voucher_no = voucher.id
622 LEFT JOIN {$wpdb->prefix}erp_acct_pay_bill AS pay_bill ON pay_bill.voucher_no = voucher.id
623 LEFT JOIN {$wpdb->prefix}erp_acct_bill_account_details AS bill_acct_details ON bill_acct_details.bill_no = bill.voucher_no
624 LEFT JOIN {$wpdb->prefix}erp_acct_expenses AS expense ON expense.voucher_no = voucher.id
625 LEFT JOIN {$wpdb->prefix}erp_acct_expense_checks AS cheque ON cheque.trn_no = voucher.id
626 {$where}
627 GROUP BY voucher.id
628 ORDER BY voucher.id {$args['order']} {$limit}";
629
630 if ( $args['count'] ) {
631 $wpdb->get_results( $sql );
632
633 return $wpdb->num_rows;
634 }
635
636 // error_log(print_r($sql, true));
637 return $wpdb->get_results( $sql, ARRAY_A );
638 }
639
640 /**
641 * Get all Purchases
642 *
643 * @param array $args
644 *
645 * @return mixed
646 */
647 function erp_acct_get_purchase_transactions( $args = [] ) {
648 global $wpdb;
649
650 $defaults = [
651 'number' => 20,
652 'offset' => 0,
653 'order' => 'DESC',
654 'count' => false,
655 'vendor_id' => false,
656 's' => '',
657 'status' => '',
658 ];
659
660 $args = wp_parse_args( $args, $defaults );
661
662 $limit = '';
663
664 $where = "WHERE (voucher.type = 'pay_purchase' OR voucher.type = 'purchase')";
665
666 if ( ! empty( $args['vendor_id'] ) ) {
667 $where .= " AND purchase.vendor_id = {$args['vendor_id']} OR pay_purchase.vendor_id = {$args['vendor_id']} ";
668 }
669
670 if ( ! empty( $args['start_date'] ) ) {
671 $where .= " AND purchase.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}' OR pay_purchase.trn_date BETWEEN '{$args['start_date']}' AND '{$args['end_date']}'";
672 }
673
674 if ( empty( $args['status'] ) ) {
675 $where .= '';
676 } else {
677 if ( ! empty( $args['status'] ) ) {
678 $where .= " AND purchase.status={$args['status']} OR pay_purchase.status={$args['status']} ";
679 }
680 }
681
682 if ( -1 !== $args['number'] ) {
683 $limit = "LIMIT {$args['number']} OFFSET {$args['offset']}";
684 }
685
686 $sql = 'SELECT';
687
688 if ( $args['count'] ) {
689 $sql .= ' COUNT( DISTINCT voucher.id ) AS total_number';
690 } else {
691 $sql .= ' voucher.id,
692 voucher.type,
693 purchase.vendor_id as vendor_id,
694 purchase.vendor_name AS vendor_name,
695 pay_purchase.vendor_name AS pay_bill_vendor_name,
696 purchase.trn_date AS bill_trn_date,
697 pay_purchase.trn_date AS pay_bill_trn_date,
698 purchase.due_date,
699 purchase.amount,
700 purchase.ref,
701 purchase.purchase_order,
702 pay_purchase.amount as pay_bill_amount,
703 ABS(SUM(purchase_acct_details.debit - purchase_acct_details.credit)) AS due,
704 purchase.status AS purchase_status,
705 pay_purchase.status AS pay_purchase_status';
706 }
707
708 $sql .= " FROM {$wpdb->prefix}erp_acct_voucher_no AS voucher
709 LEFT JOIN {$wpdb->prefix}erp_acct_purchase AS purchase ON purchase.voucher_no = voucher.id
710 LEFT JOIN {$wpdb->prefix}erp_acct_pay_purchase AS pay_purchase ON pay_purchase.voucher_no = voucher.id
711 LEFT JOIN {$wpdb->prefix}erp_acct_purchase_account_details AS purchase_acct_details ON purchase_acct_details.purchase_no = purchase.voucher_no
712 {$where} GROUP BY voucher.id ORDER BY voucher.id {$args['order']} {$limit}";
713
714 if ( $args['count'] ) {
715 $wpdb->get_results( $sql );
716
717 return $wpdb->num_rows;
718 }
719
720 // error_log(print_r($sql, true));
721 return $wpdb->get_results( $sql, ARRAY_A );
722 }
723
724 /**
725 * Generate transaction pdf by voucher_no
726 *
727 * @return void
728 */
729 function erp_acct_generate_transaction_pdf( $voucher_no ) {
730 $transaction = erp_acct_get_transaction( $voucher_no );
731 $filename = erp_acct_get_pdf_filename( $voucher_no );
732
733 erp_acct_generate_pdf( [], $transaction, $filename, 'F' );
734 }
735
736 /**
737 * Generate all transaction pdfs
738 *
739 * @return void
740 */
741 function erp_acct_generate_transaction_pdfs() {
742 global $wpdb;
743
744 $voucher_nos = $wpdb->get_results( "SELECT id, type FROM {$wpdb->prefix}erp_acct_voucher_no", ARRAY_A );
745
746 for ( $i = 0; $i < count( $voucher_nos ); $i++ ) {
747 if ( 'journal' === $voucher_nos[ $i ]['type'] ) {
748 continue;
749 }
750
751 $transaction = erp_acct_get_transaction( $voucher_nos[ $i ]['id'] );
752 $filename = erp_acct_get_pdf_filename( $voucher_nos[ $i ]['id'] );
753 erp_acct_generate_pdf( [], $transaction, $filename, 'F' );
754 }
755 }
756
757 /**
758 * Generate pdf
759 *
760 * @param $request
761 * @param $transaction
762 * @param string $file_name
763 * @param string $output_method
764 *
765 * @return bool
766 */
767 function erp_acct_generate_pdf( $request, $transaction, $file_name = '', $output_method = 'D' ) {
768 if ( ! is_plugin_active( 'erp-pdf-invoice/wp-erp-pdf.php' ) ) {
769 return;
770 }
771
772 if ( is_array( $transaction ) ) {
773 $transaction = (object) $transaction;
774 }
775
776 $company = new \WeDevs\ERP\Company();
777 $theme_color = erp_get_option( 'erp_ac_pdf_theme_color', false, '#9e9e9e' );
778
779 $user_id = null;
780 $trn_id = null;
781 $type = erp_acct_get_transaction_type( $transaction->voucher_no );
782
783 if ( ! empty( $request ) ) {
784 $receiver = isset( $request['receiver'] ) ? $request['receiver'] : $transaction->email;
785 $subject = isset( $request['subject'] ) ? $request['subject'] : $transaction->subject;
786 $body = isset( $request['message'] ) ? $request['message'] : $request['body'];
787 $attach_pdf = isset( $request['attachment'] ) && 'on' === $request['attachment'] ? true : false;
788 }
789
790 if ( ! empty( $transaction->customer_id ) ) {
791 $user_id = $transaction->customer_id;
792 }
793
794 if ( ! empty( $transaction->vendor_id ) ) {
795 $user_id = $transaction->vendor_id;
796 }
797
798 if ( ! empty( $transaction->people_id ) ) {
799 $user_id = $transaction->people_id;
800 }
801 $user = new \WeDevs\ERP\People( intval( $user_id ) );
802
803 if ( ! defined( 'WPERP_PDF_VERSION' ) ) {
804 wp_die( esc_html__( 'ERP PDF extension is not installed. Please install the extension for PDF support', 'erp' ) );
805 }
806
807 //Create a new instance
808 $trn_pdf = new \WeDevs\ERP_PDF\PDF_Invoicer( 'A4', '$', 'en' );
809
810 //Set theme color
811 $trn_pdf->set_theme_color( $theme_color );
812
813 //Set your logo
814 $logo_id = (int) $company->logo;
815
816 if ( $logo_id ) {
817 $image = wp_get_attachment_image_src( $logo_id, 'medium' );
818 $url = $image[0];
819 $trn_pdf->set_logo( $url );
820 }
821
822 if ( ! empty( $transaction->voucher_no ) ) {
823 $trn_id = $transaction->voucher_no;
824 } elseif ( ! empty( $transaction->trn_no ) ) {
825 $trn_id = $transaction->trn_no;
826 }
827
828 //Set type
829 $trn_pdf->set_type( erp_acct_get_transaction_type( $trn_id ) );
830
831 // Set barcode
832 if ( $trn_id ) {
833 $trn_pdf->set_barcode( $trn_id );
834 }
835
836 // Set reference
837 if ( $trn_id ) {
838 $trn_pdf->set_reference( $trn_id, __( 'Transaction Number', 'erp' ) );
839 }
840
841 // Set Issue Date
842 $date = ! empty( $transaction->trn_date ) ? $transaction->trn_date : $transaction->date;
843 $trn_pdf->set_reference( erp_format_date( $date ), __( 'Transaction Date', 'erp' ) );
844
845 // Set from Address
846 $from_address = explode( '<br/>', $company->get_formatted_address() );
847 array_unshift( $from_address, $company->name );
848
849 $trn_pdf->set_from_title( __( 'FROM', 'erp' ) );
850 $trn_pdf->set_from( $from_address );
851
852 // Set to Address
853 $to_address = array_values( erp_acct_get_people_address( $user_id ) );
854
855 if ( empty( $to_address ) ) {
856 $to_address = erp_get_people( $user_id )->email;
857 }
858 array_unshift( $to_address, $user->get_full_name() );
859
860 $trn_pdf->set_to_title( __( 'TO', 'erp' ) );
861 $trn_pdf->set_to_address( $to_address );
862
863 /* Customize columns based on transaction type */
864 if ( 'invoice' == $type ) {
865 // Set Date Due
866 $trn_pdf->set_reference( erp_format_date( $transaction->due_date ), __( 'Due Date', 'erp' ) );
867
868 // Set Column Headers
869 $trn_pdf->set_table_headers( [ __( 'PRODUCT', 'erp' ), __( 'QUANTITY', 'erp' ), __( 'UNIT PRICE', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
870
871 // Add Table Items
872 foreach ( $transaction->line_items as $line ) {
873 $trn_pdf->add_item( [ $line['name'], $line['qty'], erp_acct_get_price( $line['unit_price'] ), erp_acct_get_price( $line['item_total'] ) ] );
874 }
875
876 $trn_pdf->add_badge( __( 'PENDING', 'erp' ) );
877 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), erp_acct_get_price( $transaction->amount ) );
878 $trn_pdf->add_total( __( 'DISCOUNT', 'erp' ), erp_acct_get_price( $transaction->discount ) );
879 $trn_pdf->add_total( __( 'TAX', 'erp' ), erp_acct_get_price( $transaction->tax ) );
880 $trn_pdf->add_total( __( 'TOTAL DUE', 'erp' ), erp_acct_get_price( $transaction->amount + $transaction->tax - $transaction->discount ) );
881
882 // Add particulars
883 if ( $transaction->particulars ) {
884 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
885 $trn_pdf->add_paragraph( $transaction->particulars );
886 }
887 }
888
889 if ( 'payment' === $type ) {
890 // Set Column Headers
891 $trn_pdf->set_table_headers( [ __( 'INNVOICE NO', 'erp' ), __( 'TRN DATE', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
892
893 // Add Table Items
894 foreach ( $transaction->line_items as $line ) {
895 $trn_pdf->add_item( [ $line['invoice_no'], $transaction->trn_date, $line['amount'] ] );
896 }
897
898 // Add particulars
899 if ( $transaction->particulars ) {
900 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
901 $trn_pdf->add_paragraph( $transaction->particulars );
902 }
903
904 $trn_pdf->add_badge( __( 'PAID', 'erp' ) );
905 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->amount );
906 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->amount );
907 }
908
909 if ( 'bill' === $type ) {
910 // Set Column Headers
911 $trn_pdf->set_table_headers( [ __( 'BILL NO', 'erp' ), __( 'BILL DATE', 'erp' ), __( 'DUE DATE', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
912
913 // Add Table Items
914 foreach ( $transaction->bill_details as $line ) {
915 $trn_pdf->add_item( [ $line['id'], $transaction->trn_date, $transaction->due_date, $line['amount'] ] );
916 }
917
918 // Add particulars
919 if ( $transaction->particulars ) {
920 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
921 $trn_pdf->add_paragraph( $transaction->particulars );
922 }
923
924 $trn_pdf->add_badge( __( 'PENDING', 'erp' ) );
925 $trn_pdf->add_total( __( 'DUE', 'erp' ), erp_acct_get_bill_due( $transaction->voucher_no ) );
926 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->amount );
927 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->amount );
928 }
929
930 if ( 'pay_bill' === $type ) {
931 // Set Column Headers
932 $trn_pdf->set_table_headers( [ __( 'BILL NO', 'erp' ), __( 'DUE DATE', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
933
934 // Add Table Items
935 foreach ( $transaction->bill_details as $line ) {
936 $trn_pdf->add_item( [ $line['bill_no'], $transaction->trn_date, $line['amount'] ] );
937 }
938
939 // Add particulars
940 if ( $transaction->particulars ) {
941 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
942 $trn_pdf->add_paragraph( $transaction->particulars );
943 }
944
945 $trn_pdf->add_badge( __( 'PAID', 'erp' ) );
946 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->amount );
947 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->amount );
948 }
949
950 if ( 'purchase' === $type ) {
951 // Set Column Headers
952 $trn_pdf->set_table_headers( [ __( 'PRODUCT', 'erp' ), __( 'QUANTITY', 'erp' ), __( 'COST PRICE', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
953
954 // Add Table Items
955 foreach ( $transaction->line_items as $line ) {
956 $trn_pdf->add_item( [ $line['name'], $line['qty'], $line['cost_price'], $line['amount'] ] );
957 }
958
959 // Add particulars
960 if ( $transaction->particulars ) {
961 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
962 $trn_pdf->add_paragraph( $transaction->particulars );
963 }
964
965 $trn_pdf->add_badge( __( 'PENDING', 'erp' ) );
966 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->amount );
967 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->amount );
968 }
969
970 if ( 'pay_purchase' === $type ) {
971 // Set Column Headers
972 $trn_pdf->set_table_headers( [ __( 'PURCHASE NO', 'erp' ), __( 'DUE DATE', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
973
974 // Add Table Items
975 foreach ( $transaction->purchase_details as $line ) {
976 $trn_pdf->add_item( [ $line['purchase_no'], $transaction->due_date, $line['amount'] ] );
977 }
978
979 // Add particulars
980 if ( $transaction->particulars ) {
981 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
982 $trn_pdf->add_paragraph( $transaction->particulars );
983 }
984
985 $trn_pdf->add_badge( __( 'PAID', 'erp' ) );
986 $trn_pdf->add_total( __( 'DUE', 'erp' ), $transaction->due );
987 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->amount );
988 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->amount );
989 }
990
991 if ( 'expense' === $type ) {
992 // Set Column Headers
993 $trn_pdf->set_table_headers( [ __( 'EXPENSE NO', 'erp' ), __( 'EXPENSE DATE', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
994
995 // Add Table Items
996 foreach ( $transaction->bill_details as $line ) {
997 $trn_pdf->add_item( [ $line['trn_no'], $transaction->trn_date, $line['amount'] ] );
998 }
999
1000 // Add particulars
1001 if ( $transaction->particulars ) {
1002 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
1003 $trn_pdf->add_paragraph( $transaction->particulars );
1004 }
1005
1006 $trn_pdf->add_badge( __( 'PAID', 'erp' ) );
1007 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->amount );
1008 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->amount );
1009 }
1010
1011 if ( 'check' === $type ) {
1012 // Set Column Headers
1013 $trn_pdf->set_table_headers( [ __( 'CHECK NO', 'erp' ), __( 'CHECK DATE', 'erp' ), __( 'PAY TO', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
1014
1015 // Add Table Items
1016 foreach ( $transaction->bill_details as $line ) {
1017 $trn_pdf->add_item( [ $line['check_no'], $transaction->trn_date, $transaction->pay_to, $line['amount'] ] );
1018 }
1019
1020 // Add particulars
1021 if ( $transaction->particulars ) {
1022 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
1023 $trn_pdf->add_paragraph( $transaction->particulars );
1024 }
1025
1026 $trn_pdf->add_badge( __( 'PAID', 'erp' ) );
1027 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->total );
1028 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->total );
1029 }
1030
1031 if ( 'transfer_voucher' === $type ) {
1032 $type = __( 'Transfer Voucher', 'erp' );
1033 // Set Column Headers
1034 $trn_pdf->set_table_headers( [ __( 'VOUCHER NO', 'erp' ), __( 'ACCOUNT FROM', 'erp' ), __( 'AMOUNT', 'erp' ), __( 'ACCOUNT TO', 'erp' ) ] );
1035
1036 $trn_pdf->add_item( [ $transaction->voucher_no, $transaction->ac_from, $transaction->amount, $transaction->ac_to ] );
1037
1038 // Add particulars
1039 if ( $transaction->particulars ) {
1040 $trn_pdf->add_title( __( 'Notes', 'erp' ) );
1041 $trn_pdf->add_paragraph( $transaction->particulars );
1042 }
1043
1044 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->balance );
1045 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->balance );
1046 }
1047
1048 if ( 'people_trn' === $type ) {
1049 $type = __( 'People Transaction', 'erp' );
1050 // Set Column Headers
1051 $trn_pdf->set_table_headers( [ __( 'VOUCHER NO', 'erp' ), __( 'PARTICULARS', 'erp' ), __( 'AMOUNT', 'erp' ) ] );
1052
1053 $trn_pdf->add_item( [ $transaction->voucher_no, $transaction->particulars, $transaction->balance ] );
1054
1055 $trn_pdf->add_total( __( 'SUB TOTAL', 'erp' ), $transaction->balance );
1056 $trn_pdf->add_total( __( 'TOTAL', 'erp' ), $transaction->balance );
1057 }
1058
1059 $trn_pdf->render( $file_name, $output_method );
1060 $file_name = isset( $attach_pdf ) ? $file_name : '';
1061
1062 return $file_name;
1063 }
1064
1065 /**
1066 * Generate and send pdf
1067 *
1068 * @param $request
1069 * @param $transaction
1070 * @param $file_name
1071 * @param string $output_method
1072 *
1073 * @return bool
1074 */
1075 function erp_acct_send_email_with_pdf_attached( $request, $transaction, $file_name, $output_method = 'D' ) {
1076 if ( ! is_plugin_active( 'erp-pdf-invoice/wp-erp-pdf.php' ) ) {
1077 return;
1078 }
1079
1080 $trn_email = new \WeDevs\ERP\Accounting\Includes\Classes\Send_Email();
1081 $user_id = null;
1082 $trn_id = null;
1083 $result = [];
1084
1085 $type = isset( $request['type'] ) ? $request['type'] : erp_acct_get_transaction_type( $transaction->voucher_no );
1086 $receiver = isset( $request['receiver'] ) ? $request['receiver'] : [];
1087 // translators: %s: type
1088 $subject = isset( $request['subject'] ) ? $request['subject'] : sprintf( __( 'Transaction alert for %s', 'erp' ), $request['type'] );
1089 $body = isset( $request['message'] ) ? $request['message'] : __( 'Thank you for the transaction', 'erp' );
1090 // $attach_pdf = isset( $request['attachment'] ) && 'on' === $request['attachment'] ? true : false;
1091
1092 $pdf_file = erp_acct_generate_pdf( $request, $transaction, $file_name, 'F' );
1093
1094 if ( $pdf_file ) {
1095 $result = $trn_email->trigger( $receiver, $subject, $body, $pdf_file );
1096 } else {
1097 wp_die( esc_html__( 'PDF not generated!', 'erp' ) );
1098 }
1099
1100 return $result;
1101 }
1102
1103 /*
1104 * Send pdf on transaction
1105 */
1106 add_action( 'erp_acct_new_transaction_sales', 'erp_acct_send_email_on_transaction', 10, 2 );
1107 add_action( 'erp_acct_new_transaction_payment', 'erp_acct_send_email_on_transaction', 10, 2 );
1108 add_action( 'erp_acct_new_transaction_bill', 'erp_acct_send_email_on_transaction', 10, 2 );
1109 add_action( 'erp_acct_new_transaction_pay_bill', 'erp_acct_send_email_on_transaction', 10, 2 );
1110 add_action( 'erp_acct_new_transaction_purchase', 'erp_acct_send_email_on_transaction', 10, 2 );
1111 add_action( 'erp_acct_new_transaction_pay_purchase', 'erp_acct_send_email_on_transaction', 10, 2 );
1112 add_action( 'erp_acct_new_transaction_expense', 'erp_acct_send_email_on_transaction', 10, 2 );
1113 add_action( 'erp_acct_new_transaction_estimate', 'erp_acct_send_email_on_transaction', 10, 2 );
1114 add_action( 'erp_acct_new_transaction_purchase_order', 'erp_acct_send_email_on_transaction', 10, 2 );
1115
1116 /**
1117 * Send pdf on transaction
1118 *
1119 * @param $voucher_no
1120 * @param $transaction
1121 *
1122 * @return bool
1123 */
1124 function erp_acct_send_email_on_transaction( $voucher_no, $transaction ) {
1125 if ( ! is_plugin_active( 'erp-pdf-invoice/wp-erp-pdf.php' ) ) {
1126 return;
1127 }
1128
1129 $user_id = null;
1130 $trn_id = null;
1131 $request = [];
1132 $result = [];
1133
1134 $request['type'] = ! empty( $transaction['type'] ) ? $transaction['type'] : erp_acct_get_transaction_type( $voucher_no );
1135 $request['receiver'][] = ! empty( $transaction['email'] ) ? $transaction['email'] : [];
1136 // translators: %s: type
1137
1138 $file_name = erp_acct_get_pdf_filename( $voucher_no );
1139 $pdf_file = erp_acct_generate_pdf( $request, $transaction, $file_name, 'F' );
1140
1141 if ( $pdf_file ) {
1142 switch ( current_action() ) {
1143 case 'erp_acct_new_transaction_sales':
1144 $email_type = 'Transactional_Email';
1145 break;
1146
1147 case 'erp_acct_new_transaction_payment':
1148 $email_type = 'Transactional_Email_Payments';
1149 break;
1150
1151 case 'erp_acct_new_transaction_bill':
1152 $email_type = 'Transactional_Email_Bill';
1153 break;
1154
1155 case 'erp_acct_new_transaction_pay_bill':
1156 $email_type = 'Transactional_Email_Pay_Bill';
1157 break;
1158
1159 case 'erp_acct_new_transaction_purchase':
1160 $email_type = 'Transactional_Email_Purchase';
1161 break;
1162
1163 case 'erp_acct_new_transaction_pay_purchase':
1164 $email_type = 'Transactional_Email_Pay_Purchase';
1165 break;
1166
1167 case 'erp_acct_new_transaction_expense':
1168 $email_type = 'Transactional_Email_Expense';
1169 break;
1170
1171 case 'erp_acct_new_transaction_estimate':
1172 $email_type = 'Transactional_Email_Estimate';
1173 break;
1174
1175 case 'erp_acct_new_transaction_purchase_order':
1176 $email_type = 'Transactional_Email_Purchase_Order';
1177 break;
1178 default:
1179 $email_type = 'Transactional_Email';
1180 }
1181
1182 acct_send_email( $request['receiver'], $pdf_file, $email_type, $voucher_no );
1183 } else {
1184 wp_die( esc_html__( 'PDF not generated!', 'erp' ) );
1185 }
1186 }
1187
1188 /**
1189 * Send accounting emails to receivers
1190 *
1191 * @param $receiver
1192 * @param $pdf
1193 * @param $type
1194 *
1195 * @return bool
1196 */
1197 function acct_send_email( $receiver, $pdf_file, $email_type, $voucher_no ) {
1198 $emailer = wperp()->emailer->get_email( $email_type );
1199 $company = new \WeDevs\ERP\Company();
1200
1201 if ( is_a( $emailer, '\WeDevs\ERP\Email' ) ) {
1202 if ( is_array( $receiver ) ) {
1203 foreach ( $receiver as $email ) {
1204 $emailer->trigger( $email, $pdf_file, $voucher_no, $company );
1205 }
1206 } else {
1207 $emailer->trigger( $receiver, $pdf_file, $voucher_no, $company );
1208 }
1209 }
1210 }
1211
1212 /**
1213 * Get voucher type by id
1214 *
1215 * @param $voucher_no
1216 *
1217 * @return string|null
1218 */
1219 function erp_acct_get_transaction_type( $voucher_no ) {
1220 global $wpdb;
1221
1222 return $wpdb->get_var( $wpdb->prepare( "SELECT type FROM {$wpdb->prefix}erp_acct_voucher_no WHERE id = %d", $voucher_no ) );
1223 }
1224
1225 /**
1226 * @param $transaction_id
1227 *
1228 * @return mixed
1229 */
1230 function erp_acct_get_transaction( $transaction_id ) {
1231 $transaction = [];
1232
1233 $transaction_type = erp_acct_get_transaction_type( $transaction_id );
1234 $link_hash = erp_acct_get_invoice_link_hash( $transaction_id, $transaction_type );
1235 $readonly_url = add_query_arg(
1236 [
1237 'query' => 'readonly_invoice',
1238 'trans_id' => $transaction_id,
1239 'auth' => $link_hash,
1240 ],
1241 site_url()
1242 );
1243
1244 switch ( $transaction_type ) {
1245 case 'invoice':
1246 $transaction = erp_acct_get_invoice( $transaction_id );
1247 break;
1248
1249 case 'payment':
1250 $transaction = erp_acct_get_payment( $transaction_id );
1251 break;
1252
1253 case 'bill':
1254 $transaction = erp_acct_get_bill( $transaction_id );
1255 break;
1256
1257 case 'pay_bill':
1258 $transaction = erp_acct_get_pay_bill( $transaction_id );
1259 break;
1260
1261 case 'purchase':
1262 $transaction = erp_acct_get_purchase( $transaction_id );
1263 break;
1264
1265 case 'pay_purchase':
1266 $transaction = erp_acct_get_pay_purchase( $transaction_id );
1267 break;
1268
1269 case 'expense':
1270 case 'check':
1271 $transaction = erp_acct_get_expense( $transaction_id );
1272 break;
1273
1274 case 'transfer_voucher':
1275 $transaction = erp_acct_get_single_voucher( $transaction_id );
1276 break;
1277 default:
1278 break;
1279 }
1280
1281 $transaction['type'] = $transaction_type;
1282 $transaction['readonly_url'] = $readonly_url;
1283
1284 return $transaction;
1285 }
1286
1287 /**
1288 * Varify transaction hash
1289 *
1290 * @param $transaction_id
1291 * @param string $transaction_type
1292 * @param string $hash_to_verify
1293 * @param string $algo
1294 *
1295 * @return bool
1296 */
1297 function erp_acct_verify_invoice_link_hash( $transaction_id, $transaction_type, $hash_to_verify = '', $algo = 'sha256' ) {
1298 if ( $transaction_id && $transaction_type && $hash_to_verify ) {
1299 $to_hash = $transaction_id . $transaction_type;
1300 $hash_original = hash( $algo, $to_hash );
1301
1302 if ( $hash_original === $hash_to_verify ) {
1303 return true;
1304 }
1305 }
1306
1307 return false;
1308 }
1309
1310 /**
1311 * Get unique transaction hash for sharing
1312 *
1313 * @param $transaction_id
1314 * @param string $transaction_type
1315 * @param string $algo
1316 *
1317 * @return string
1318 */
1319 function erp_acct_get_invoice_link_hash( $transaction_id, $transaction_type, $algo = 'sha256' ) {
1320 $hash_string = '';
1321
1322 if ( $transaction_id && $transaction_type ) {
1323 $to_hash = $transaction_id . $transaction_type;
1324 $hash_string = hash( $algo, $to_hash );
1325 }
1326
1327 return $hash_string;
1328 }
1329
1330 /**
1331 * Get pdf file name
1332 *
1333 * @param $voucher_no
1334 *
1335 * @return string
1336 */
1337 function erp_acct_get_pdf_filename( $voucher_no ) {
1338 $inv_dir = WP_CONTENT_DIR . '/uploads/erp-pdfs/';
1339
1340 if ( ! file_exists( $inv_dir ) ) {
1341 mkdir( $inv_dir, 0777, true );
1342 }
1343
1344 $pdf_file = $inv_dir . "voucher_{$voucher_no}.pdf";
1345
1346 return $pdf_file;
1347 }
1348
1349 /**
1350 * Insert data into `erp_acct_people_trn_details` table
1351 *
1352 * @param $voucher_no
1353 * @param $transaction
1354 */
1355 function erp_acct_insert_data_into_people_trn_details( $transaction, $voucher_no ) {
1356 global $wpdb;
1357
1358 $data = [];
1359
1360 if ( ! empty( $transaction['customer_id'] ) ) {
1361 $people_id = $transaction['customer_id'];
1362 } else {
1363 if ( ! empty( $transaction['vendor_id'] ) ) {
1364 $people_id = $transaction['vendor_id'];
1365 } else {
1366 $people_id = $transaction['people_id'];
1367 }
1368 }
1369
1370 $date = ! empty( $transaction['trn_date'] ) ? $transaction['trn_date'] : $transaction['date'];
1371
1372 $wpdb->insert(
1373 $wpdb->prefix . 'erp_acct_people_trn_details',
1374 [
1375 'people_id' => $people_id,
1376 'voucher_no' => $voucher_no,
1377 'debit' => $transaction['dr'],
1378 'credit' => $transaction['cr'],
1379 'trn_date' => $date,
1380 'particulars' => $transaction['particulars'],
1381 'created_at' => $transaction['created_at'],
1382 'created_by' => $transaction['created_by'],
1383 'updated_at' => $transaction['updated_at'],
1384 'updated_by' => $transaction['updated_by'],
1385 ]
1386 );
1387 }
1388
1389 /**
1390 * Update data into `erp_acct_people_trn_details` table
1391 *
1392 * @param $transaction
1393 * @param $voucher_no
1394 */
1395 function erp_acct_update_data_into_people_trn_details( $transaction, $voucher_no ) {
1396 global $wpdb;
1397
1398 $wpdb->delete( $wpdb->prefix . 'erp_acct_people_trn_details', [ 'voucher_no' => $voucher_no ] );
1399 }
1400
1401 /**
1402 * Return url from a absolute path
1403 *
1404 * @param $voucher_no
1405 *
1406 * @return string
1407 */
1408 function erp_acct_pdf_abs_path_to_url( $voucher_no ) {
1409 $upload_url = wp_upload_dir();
1410 $url = $upload_url['baseurl'] . '/erp-pdfs/' . "voucher_{$voucher_no}.pdf";
1411
1412 return esc_url_raw( $url );
1413 }
1414