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

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

626 lines 18.0 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 taxes
9 *
10 * @return mixed
11 */
12 function erp_acct_get_all_tax_rates( $args = [] ) {
13 global $wpdb;
14
15 $defaults = [
16 'number' => 20,
17 'offset' => 0,
18 'orderby' => 'tax.id',
19 'order' => 'ASC',
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( DISTINCT tax.id ) as total_number ' : ' DISTINCT tax.id, tax.tax_rate_name, tax.tax_number, tax.default ';
34 $sql .= "FROM {$wpdb->prefix}erp_acct_taxes AS tax INNER JOIN {$wpdb->prefix}erp_acct_tax_cat_agency as cat_agency on tax.id = cat_agency.tax_id ORDER BY {$args['orderby']} {$args['order']} {$limit}";
35
36 if ( $args['count'] ) {
37 return $wpdb->get_var( $sql );
38 }
39
40 return $wpdb->get_results( $sql, ARRAY_A );
41 }
42
43 /**
44 * Get an single tax
45 *
46 * @param $tax_no
47 *
48 * @return mixed
49 */
50 function erp_acct_get_tax_rate( $tax_no ) {
51 global $wpdb;
52
53 $sql = "SELECT
54
55 tax.id,
56 tax.tax_rate_name,
57 tax.tax_number,
58 tax.default,
59 tax.created_at,
60 tax.created_by,
61 tax.updated_at,
62 tax.updated_by,
63
64 tax_item.tax_id,
65 tax_item.component_name,
66 tax_item.agency_id,
67 tax_item.tax_cat_id
68
69 FROM {$wpdb->prefix}erp_acct_taxes AS tax
70 LEFT JOIN {$wpdb->prefix}erp_acct_tax_cat_agency AS tax_item ON tax.id = tax_item.tax_id
71
72 WHERE tax.id = {$tax_no} LIMIT 1";
73
74 $row = $wpdb->get_row( $sql, ARRAY_A );
75
76 $row['tax_components'] = erp_acct_format_tax_line_items( $tax_no );
77
78 for ( $i = 0; $i < count( $row['tax_components'] ); $i++ ) {
79 $row['tax_components'][ $i ]['agency'] = null; // we'll fill that later from VUE
80 $row['tax_components'][ $i ]['category'] = null; // we'll fill that later from VUE
81
82 $row['tax_components'][ $i ]['agency_name'] = erp_acct_get_tax_agency_by_id( $row['tax_components'][ $i ]['agency_id'] );
83 $row['tax_components'][ $i ]['tax_cat_name'] = erp_acct_get_tax_category_by_id( $row['tax_components'][ $i ]['tax_cat_id'] );
84 }
85
86 return $row;
87 }
88
89 /**
90 * Insert tax data
91 *
92 * @param $data
93 *
94 * @return int
95 */
96 function erp_acct_insert_tax_rate( $data ) {
97 global $wpdb;
98
99 $created_by = get_current_user_id();
100 $data['created_at'] = date( 'Y-m-d H:i:s' );
101 $data['created_by'] = $created_by;
102
103 $tax_data = erp_acct_get_formatted_tax_data( $data );
104
105 $items = $data['tax_components'];
106
107 $tax_id = (int) $data['tax_rate_name'];
108
109 foreach ( $items as $key => $item ) {
110 $wpdb->insert(
111 $wpdb->prefix . 'erp_acct_tax_cat_agency',
112 [
113 'tax_id' => $tax_id,
114 'component_name' => $item['component_name'],
115 'tax_cat_id' => $item['tax_category_id'],
116 'agency_id' => $item['agency_id'],
117 'tax_rate' => $item['tax_rate'],
118 'created_at' => $tax_data['created_at'],
119 'created_by' => $tax_data['created_by'],
120 'updated_at' => $tax_data['updated_at'],
121 'updated_by' => $tax_data['updated_by'],
122 ]
123 );
124 }
125
126 return $tax_id;
127 }
128
129 /**
130 * Update tax data
131 *
132 * @param $data
133 *
134 * @return int
135 */
136 function erp_acct_update_tax_rate( $data, $id ) {
137 global $wpdb;
138
139 $updated_by = get_current_user_id();
140 $data['updated_at'] = date( 'Y-m-d H:i:s' );
141 $data['updated_by'] = $updated_by;
142
143 $tax_data = erp_acct_get_formatted_tax_data( $data );
144
145 $wpdb->update(
146 $wpdb->prefix . 'erp_acct_taxes',
147 [
148 'tax_rate_id' => $tax_data['tax_rate_id'],
149 'tax_number' => $tax_data['tax_number'],
150 'default' => $tax_data['default'],
151 'created_at' => $tax_data['created_at'],
152 'created_by' => $tax_data['created_by'],
153 'updated_at' => $tax_data['updated_at'],
154 'updated_by' => $tax_data['updated_by'],
155 ],
156 [
157 'id' => $id,
158 ]
159 );
160
161 if ( ! empty( $tax_data['default'] ) && $tax_data['default'] ) {
162 $results = $wpdb->get_results( 'UPDATE ' . $wpdb->prefix . 'erp_acct_taxes' . ' SET `default`=0' );
163 }
164
165 $items = $data['tax_components'];
166
167 foreach ( $items as $key => $item ) {
168 $wpdb->update(
169 $wpdb->prefix . 'erp_acct_tax_cat_agency',
170 [
171 'component_name' => $item['component_name'],
172 'tax_cat_id' => $item['tax_cat_id'],
173 'agency_id' => $item['agency_id'],
174 'tax_rate' => $item['tax_rate'],
175 'created_at' => $tax_data['created_at'],
176 'created_by' => $tax_data['created_by'],
177 'updated_at' => $tax_data['updated_at'],
178 'updated_by' => $tax_data['updated_by'],
179 ],
180 [
181 'tax_id' => $id,
182 ]
183 );
184 }
185
186 return $id;
187 }
188
189 /**
190 * Update tax data
191 *
192 * @param $data
193 *
194 * @return int
195 */
196 function erp_acct_quick_edit_tax_rate( $data, $id ) {
197 global $wpdb;
198
199 $updated_by = get_current_user_id();
200 $data['updated_at'] = date( 'Y-m-d H:i:s' );
201 $data['updated_by'] = $updated_by;
202
203 $tax_data = erp_acct_get_formatted_tax_data( $data );
204
205 if ( ! empty( $tax_data['default'] ) && 1 === $tax_data['default'] ) {
206 $results = $wpdb->get_results( 'UPDATE ' . $wpdb->prefix . 'erp_acct_taxes' . ' SET `default`=0' );
207 }
208
209 $wpdb->update(
210 $wpdb->prefix . 'erp_acct_taxes',
211 [
212 'tax_number' => $tax_data['tax_number'],
213 'default' => $tax_data['default'],
214 'updated_at' => $tax_data['updated_at'],
215 'updated_by' => $tax_data['updated_by'],
216 ],
217 [
218 'id' => $id,
219 ]
220 );
221
222 return $id;
223 }
224
225 /**
226 * Add line item of a tax rate
227 *
228 * @param $data
229 *
230 * @return int
231 */
232 function erp_acct_add_tax_rate_line( $data ) {
233 global $wpdb;
234
235 $updated_by = get_current_user_id();
236 $data['updated_at'] = date( 'Y-m-d H:i:s' );
237 $data['updated_by'] = $updated_by;
238
239 $tax_data = erp_acct_get_formatted_tax_line_data( $data );
240
241 $wpdb->insert(
242 $wpdb->prefix . 'erp_acct_tax_cat_agency',
243 [
244 'tax_id' => $tax_data['tax_id'],
245 'component_name' => $tax_data['component_name'],
246 'tax_cat_id' => $tax_data['tax_cat_id'],
247 'agency_id' => $tax_data['agency_id'],
248 'tax_rate' => $tax_data['tax_rate'],
249 'created_at' => $tax_data['created_at'],
250 'created_by' => $tax_data['created_by'],
251 'updated_at' => $tax_data['updated_at'],
252 'updated_by' => $tax_data['updated_by'],
253 ]
254 );
255
256 return $tax_data['tax_id'];
257 }
258
259 /**
260 * Update line item of a tax rate
261 *
262 * @param $data
263 *
264 * @return int
265 */
266 function erp_acct_edit_tax_rate_line( $data ) {
267 global $wpdb;
268
269 $updated_by = get_current_user_id();
270 $data['updated_at'] = date( 'Y-m-d H:i:s' );
271 $data['updated_by'] = $updated_by;
272
273 $tax_data = erp_acct_get_formatted_tax_line_data( $data );
274
275 $wpdb->update(
276 $wpdb->prefix . 'erp_acct_tax_cat_agency',
277 [
278 'component_name' => $tax_data['component_name'],
279 'tax_cat_id' => $tax_data['tax_cat_id'],
280 'agency_id' => $tax_data['agency_id'],
281 'tax_rate' => $tax_data['tax_rate'],
282 'created_at' => $tax_data['created_at'],
283 'created_by' => $tax_data['created_by'],
284 'updated_at' => $tax_data['updated_at'],
285 'updated_by' => $tax_data['updated_by'],
286 ],
287 [
288 'id' => $tax_data['db_id'],
289 ]
290 );
291
292 return $tax_data['db_id'];
293 }
294
295 /**
296 * Delete an tax rate line
297 *
298 * @param $line_no
299 *
300 * @return int
301 */
302 function erp_acct_delete_tax_rate_line( $line_no ) {
303 global $wpdb;
304
305 $wpdb->delete( $wpdb->prefix . 'erp_acct_tax_cat_agency', [ 'id' => $line_no ] );
306
307 return $line_no;
308 }
309
310 /**
311 * Delete an tax
312 *
313 * @param $tax_no
314 *
315 * @return int
316 */
317 function erp_acct_delete_tax_rate( $tax_no ) {
318 global $wpdb;
319
320 $wpdb->delete( $wpdb->prefix . 'erp_acct_taxes', [ 'id' => $tax_no ] );
321
322 return $tax_no;
323 }
324
325 /**
326 * Get all tax payments
327 *
328 * @return mixed
329 */
330 function erp_acct_get_tax_pay_records( $args = [] ) {
331 global $wpdb;
332
333 $defaults = [
334 'number' => 20,
335 'offset' => 0,
336 'orderby' => 'id',
337 'order' => 'DESC',
338 'count' => false,
339 's' => '',
340 ];
341
342 $args = wp_parse_args( $args, $defaults );
343
344 $limit = '';
345
346 if ( -1 !== $args['number'] ) {
347 $limit = "LIMIT {$args['number']} OFFSET {$args['offset']}";
348 }
349
350 $sql = 'SELECT';
351 $sql .= $args['count'] ? ' COUNT( id ) as total_number ' : ' * ';
352 $sql .= "FROM {$wpdb->prefix}erp_acct_tax_pay ORDER BY {$args['orderby']} {$args['order']} {$limit}";
353
354 if ( $args['count'] ) {
355 return $wpdb->get_var( $sql );
356 }
357
358 return $wpdb->get_results( $sql, ARRAY_A );
359 }
360
361 /**
362 * Get a single tax payment
363 *
364 * @return mixed
365 */
366 function erp_acct_get_tax_pay_record( $voucher_no ) {
367 global $wpdb;
368
369 $row = $wpdb->get_row(
370 $wpdb->prepare(
371 "SELECT
372 tax.id,
373 tax.voucher_no,
374 tax.particulars,
375 tax.amount,
376 tax.trn_date,
377 tax.voucher_type,
378 tax.trn_by,
379 tax.agency_id,
380 tax.ledger_id,
381 tax.created_at
382 FROM {$wpdb->prefix}erp_acct_tax_pay AS tax
383 WHERE tax.voucher_no = %d LIMIT 1",
384 $voucher_no
385 ),
386 ARRAY_A
387 );
388
389 return $row;
390 }
391
392 /**
393 * Make a tax payment
394 *
395 * @param $data
396 *
397 * @return array
398 */
399 function erp_acct_pay_tax( $data ) {
400 global $wpdb;
401
402 $created_by = get_current_user_id();
403 $data['created_at'] = date( 'Y-m-d H:i:s' );
404 $data['created_by'] = $created_by;
405 $currency = erp_get_currency( true );
406
407 $wpdb->insert(
408 $wpdb->prefix . 'erp_acct_voucher_no',
409 [
410 'type' => 'tax_payment',
411 'currency' => $currency,
412 'created_at' => $data['created_at'],
413 'created_by' => $data['created_by'],
414 'updated_at' => isset( $data['updated_at'] ) ? $data['updated_at'] : null,
415 'updated_by' => isset( $data['updated_by'] ) ? $data['updated_by'] : null,
416 ]
417 );
418
419 $voucher_no = $wpdb->insert_id;
420
421 $tax_data = erp_acct_get_formatted_tax_data( $data );
422
423 $wpdb->insert(
424 $wpdb->prefix . 'erp_acct_tax_pay',
425 [
426 'voucher_no' => $voucher_no,
427 'trn_date' => $tax_data['trn_date'],
428 // translators: %s: voucher_no
429 'particulars' => ! empty( $tax_data['particulars'] ) ? $tax_data['particulars'] : sprintf( __( 'Invoice created with voucher no %s', 'erp' ), $voucher_no ),
430 'amount' => $tax_data['amount'],
431 'voucher_type' => $tax_data['voucher_type'],
432 'trn_by' => $tax_data['trn_by'],
433 'agency_id' => $tax_data['agency_id'],
434 'ledger_id' => $tax_data['ledger_id'],
435 'created_at' => $tax_data['created_at'],
436 'created_by' => $tax_data['created_by'],
437 'updated_at' => $tax_data['updated_at'],
438 'updated_by' => $tax_data['updated_by'],
439 ]
440 );
441
442 if ( 'debit' === $tax_data['voucher_type'] ) {
443 $debit = $tax_data['amount'];
444 $credit = 0;
445 } else {
446 $debit = 0;
447 $credit = $tax_data['amount'];
448 }
449
450 // insert data into {$wpdb->prefix}erp_acct_tax_agency_details
451 $wpdb->insert(
452 $wpdb->prefix . 'erp_acct_tax_agency_details',
453 [
454 'agency_id' => $tax_data['agency_id'],
455 'trn_no' => $voucher_no,
456 'trn_date' => $tax_data['trn_date'],
457 // translators: %s: voucher_no
458 'particulars' => ! empty( $tax_data['particulars'] ) ? $tax_data['particulars'] : sprintf( __( 'Invoice created with voucher no %s', 'erp' ), $voucher_no ),
459 'debit' => $debit,
460 'credit' => $credit,
461 'created_at' => $tax_data['created_at'],
462 'created_by' => $tax_data['created_by'],
463 ]
464 );
465
466 $tax_data['voucher_no'] = $voucher_no;
467
468 erp_acct_insert_tax_pay_data_into_ledger( $tax_data );
469
470 $tax_pay = erp_acct_get_tax_pay_record( $voucher_no );
471
472 return $tax_pay;
473 }
474
475 /**
476 * Insert Tax pay data into ledger
477 *
478 * @param array $invoice_data
479 *
480 * @return mixed
481 */
482 function erp_acct_insert_tax_pay_data_into_ledger( $tax_data ) {
483 global $wpdb;
484
485 if ( 'debit' === $tax_data['voucher_type'] ) {
486 $debit = 0;
487 $credit = $tax_data['amount'];
488 } else {
489 $debit = $tax_data['amount'];
490 $credit = 0;
491 }
492
493 // Insert amount in ledger_details
494 $wpdb->insert(
495 $wpdb->prefix . 'erp_acct_ledger_details',
496 [
497 'ledger_id' => $tax_data['ledger_id'],
498 'trn_no' => $tax_data['voucher_no'],
499 'particulars' => $tax_data['particulars'],
500 'debit' => $debit,
501 'credit' => $credit,
502 'trn_date' => $tax_data['trn_date'],
503 'created_at' => $tax_data['created_at'],
504 'created_by' => $tax_data['created_by'],
505 'updated_at' => $tax_data['updated_at'],
506 'updated_by' => $tax_data['updated_by'],
507 ]
508 );
509 }
510
511 /**
512 * Format payment line items
513 *
514 * @param string $tax
515 *
516 * @return array
517 */
518 function erp_acct_format_tax_line_items( $tax = 'all' ) {
519 global $wpdb;
520
521 $sql = 'SELECT id as db_id, tax_id, component_name, agency_id, tax_cat_id, tax_rate';
522
523 if ( 'all' === $tax ) {
524 $tax_sql = '';
525 } else {
526 $tax_sql = 'WHERE tax_id = ' . $tax;
527 }
528 $sql .= " FROM {$wpdb->prefix}erp_acct_tax_cat_agency {$tax_sql} ORDER BY tax_id";
529
530 $results = $wpdb->get_results( $sql, ARRAY_A );
531
532 return $results;
533 }
534
535 /**
536 * Get formatted tax data
537 *
538 * @param $data
539 * @param $voucher_no
540 *
541 * @return mixed
542 */
543 function erp_acct_get_formatted_tax_data( $data ) {
544 $tax_data = [];
545
546 $tax_data['tax_rate_id'] = isset( $data['tax_rate_name'] ) ? $data['tax_rate_name'] : '';
547 $tax_data['tax_rate'] = isset( $data['tax_rate'] ) ? $data['tax_rate'] : 0;
548 $tax_data['tax_id'] = isset( $data['tax_id'] ) ? $data['tax_id'] : 0;
549 $tax_data['trn_by'] = isset( $data['trn_by'] ) ? $data['trn_by'] : '';
550 $tax_data['tax_category_id'] = isset( $data['tax_category_id'] ) ? $data['tax_category_id'] : 0;
551 $tax_data['agency_id'] = isset( $data['agency_id'] ) ? $data['agency_id'] : 0;
552 $tax_data['agency_name'] = isset( $data['agency_name'] ) ? $data['agency_name'] : '';
553 $tax_data['tax_cat_name'] = isset( $data['tax_cat_name'] ) ? $data['tax_cat_name'] : '';
554 $tax_data['tax_components'] = isset( $data['tax_components'] ) ? $data['tax_components'] : [];
555 $tax_data['created_at'] = date( 'Y-m-d' );
556 $tax_data['created_by'] = isset( $data['created_by'] ) ? $data['created_by'] : '';
557 $tax_data['updated_at'] = isset( $data['updated_at'] ) ? $data['updated_at'] : null;
558 $tax_data['updated_by'] = isset( $data['updated_by'] ) ? $data['updated_by'] : '';
559 $tax_data['name'] = isset( $data['name'] ) ? $data['name'] : '';
560 $tax_data['description'] = isset( $data['description'] ) ? $data['description'] : '';
561 $tax_data['voucher_no'] = isset( $data['voucher_no'] ) ? $data['voucher_no'] : '';
562 $tax_data['trn_date'] = isset( $data['trn_date'] ) ? $data['trn_date'] : date( 'Y-m-d' );
563 $tax_data['tax_period'] = isset( $data['tax_period'] ) ? $data['tax_period'] : '';
564 $tax_data['particulars'] = isset( $data['particulars'] ) ? $data['particulars'] : '';
565 $tax_data['amount'] = isset( $data['amount'] ) ? $data['amount'] : '';
566 $tax_data['ledger_id'] = isset( $data['ledger_id'] ) ? $data['ledger_id'] : '';
567 $tax_data['voucher_type'] = isset( $data['voucher_type'] ) ? $data['voucher_type'] : '';
568
569 return $tax_data;
570 }
571
572 /**
573 * Get formatted tax data
574 *
575 * @param $data
576 * @param $voucher_no
577 *
578 * @return mixed
579 */
580 function erp_acct_get_formatted_tax_line_data( $data ) {
581 $tax_data = [];
582
583 $tax_data['tax_id'] = isset( $data['tax_id'] ) ? $data['tax_id'] : '';
584 $tax_data['db_id'] = isset( $data['db_id'] ) ? $data['db_id'] : '';
585 $tax_data['rate_id'] = isset( $data['rate_id'] ) ? $data['rate_id'] : '';
586 $tax_data['component_name'] = isset( $data['component_name'] ) ? $data['component_name'] : '';
587 $tax_data['agency_id'] = isset( $data['agency_id'] ) ? $data['agency_id'] : 0;
588 $tax_data['tax_cat_id'] = isset( $data['tax_cat_id'] ) ? $data['tax_cat_id'] : 0;
589 $tax_data['tax_rate'] = isset( $data['tax_rate'] ) ? $data['tax_rate'] : 0;
590 $tax_data['created_at'] = isset( $data['created_at'] ) ? $data['created_at'] : '';
591 $tax_data['created_by'] = isset( $data['created_by'] ) ? $data['created_by'] : '';
592 $tax_data['updated_at'] = isset( $data['updated_at'] ) ? $data['updated_at'] : null;
593 $tax_data['updated_by'] = isset( $data['updated_by'] ) ? $data['updated_by'] : '';
594
595 return $tax_data;
596 }
597
598 /**
599 * Tax summary
600 */
601 function erp_acct_tax_summary() {
602 global $wpdb;
603
604 return $wpdb->get_results(
605 "SELECT
606 tax.id AS tax_rate_id,
607 tax.tax_rate_name,
608 tax.default,
609 tca.tax_cat_id,
610 sum(tca.tax_rate) AS tax_rate
611 FROM {$wpdb->prefix}erp_acct_tax_cat_agency AS tca
612 INNER JOIN {$wpdb->prefix}erp_acct_taxes AS tax ON tax.id = tca.tax_id
613 GROUP BY tca.tax_cat_id, tax.id order by tax_cat_id",
614 ARRAY_A
615 );
616 }
617
618 /**
619 * Get default tax rate name id
620 */
621 function erp_acct_get_default_tax_rate_name_id() {
622 global $wpdb;
623
624 return $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}erp_acct_taxes WHERE `default` = 1" );
625 }
626