PluginProbe
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce / 1.2.6
ERP: Complete HR, Accounting & CRM Suite Built for WooCommerce v1.2.6
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.php

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

697 lines 19.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function erp_ac_get_expense_form_types() {
4 $form_types = [
5 'payment_voucher' => [
6 'name' => 'payment_voucher',
7 'label' => __( 'Payment Voucher', 'erp' ),
8 'description' => __( 'A purchase that has been made through bank or cash.', 'erp' ),
9 'type' => 'credit'
10 ],
11 'vendor_credit' => [
12 'name' => 'vendor_credit',
13 'label' => __( 'Vendor Credit', 'erp' ),
14 'description' => __( 'A purchase that has been made as credit from vendor.', 'erp' ),
15 'type' => 'credit'
16 ],
17 // 'deleted' => [
18 // 'name' => 'trash',
19 // 'label' => __( 'Trash', 'erp' ),
20 // 'description' => __( '', 'erp' ),
21 // 'type' => 'debit'
22 // ],
23 ];
24
25 return apply_filters( 'erp_ac_get_expense_form_types', $form_types );
26 }
27
28 function erp_ac_get_sales_form_types() {
29 $form_types = [
30 'payment' => [
31 'name' => 'payment',
32 'label' => __( 'Payment', 'erp' ),
33 'description' => __( '', 'erp' ),
34 'type' => 'debit'
35 ],
36 'invoice' => [
37 'name' => 'invoice',
38 'label' => __( 'Invoice', 'erp' ),
39 'description' => __( '', 'erp' ),
40 'type' => 'debit'
41 ],
42 // 'deleted' => [
43 // 'name' => 'trash',
44 // 'label' => __( 'Trash', 'erp' ),
45 // 'description' => __( '', 'erp' ),
46 // 'type' => 'debit'
47 // ],
48 ];
49
50 return apply_filters( 'erp_ac_get_sales_form_types', $form_types );
51 }
52
53 function erp_ac_get_bank_form_types() {
54 $form_types = [
55 'bank' => [
56 'name' => 'bank',
57 'label' => __( 'Bank', 'erp' ),
58 'description' => __( '', 'erp' ),
59 'type' => 'credit'
60 ],
61 ];
62
63 return apply_filters( 'erp_ac_get_bank_form_types', $form_types );
64 }
65
66 /**
67 * Get transaction status label
68 *
69 * @param string $status
70 *
71 * @return string
72 */
73 function erp_ac_get_status_label( $items, $slug ) {
74 $label = '';
75 $status = $items->status;
76
77 switch ( $status ) {
78 case 'closed':
79 $label = __( 'Paid', 'erp' );
80 break;
81
82 case 'paid':
83 $label = __( 'Paid', 'erp' );
84 break;
85
86 case 'awaiting_payment':
87 $url = admin_url( 'admin.php?page='.$slug.'&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id );
88 $label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Awaiting for Payment', 'erp' ) );
89 //$label = __( 'Awaiting for Payment', 'erp' );
90 break;
91
92 case 'overdue':
93 $label = __( 'Overdue', 'erp' );
94 break;
95
96 case 'deleted':
97 $label = __( 'Trash', 'erp' );
98 break;
99
100 case 'partial':
101 $label = __( 'Partially Paid', 'erp' );
102 break;
103
104 case 'void':
105 $label = __( 'Void', 'erp' );
106 break;
107
108 case 'pending':
109 $url = admin_url( 'admin.php?page='.$slug.'&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id );
110 $label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Awaiting for approval', 'erp' ) );
111 break;
112
113 case 'draft':
114 $url = admin_url( 'admin.php?page='.$slug.'&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id );
115 $label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Draft', 'erp' ) );
116 break;
117
118 case 'awaiting_approval':
119 $url = admin_url( 'admin.php?page='.$slug.'&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id );
120 $label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Awaiting for approval', 'erp' ) );
121 break;
122 }
123
124 return apply_filters( 'erp_ac_status_labels', $label, $status );
125 }
126
127 /**
128 * Get currency symbol
129 *
130 * @since 0.1
131 *
132 * @return string
133 */
134 function erp_ac_get_currency_symbol() {
135 $currency = erp_ac_get_currency();
136
137 return erp_get_currency_symbol( $currency );
138 }
139
140 /**
141 * Get currency
142 *
143 * @since 0.1
144 *
145 * @return string
146 */
147 function erp_ac_get_currency() {
148 return erp_get_currency();
149 }
150
151 /**
152 * Get the price format depending on the currency position.
153 *
154 * @return string
155 */
156 function erp_ac_get_price_format() {
157 $currency_pos = erp_get_option( 'erp_ac_currency_position', false, 'left' );
158 $format = '%1$s%2$s';
159
160 switch ( $currency_pos ) {
161 case 'left' :
162 $format = '%1$s%2$s';
163 break;
164 case 'right' :
165 $format = '%2$s%1$s';
166 break;
167 case 'left_space' :
168 $format = '%1$s&nbsp;%2$s';
169 break;
170 case 'right_space' :
171 $format = '%2$s&nbsp;%1$s';
172 break;
173 }
174
175 return apply_filters( 'erp_ac_price_format', $format, $currency_pos );
176 }
177
178 /**
179 * Get the price format depending on the currency position.
180 *
181 * @return string
182 */
183 function erp_ac_get_symbol_format_for_label( $label ) {
184 $currency_pos = erp_get_option( 'erp_ac_currency_position', false, 'left' );
185 $format = '(%1$s)%2$s';
186
187 switch ( $currency_pos ) {
188 case 'left' :
189 $format = '(%1$s)%2$s';
190 break;
191 case 'right' :
192 $format = '%2$s(%1$s)';
193 break;
194 case 'left_space' :
195 $format = '(%1$s)&nbsp;%2$s';
196 break;
197 case 'right_space' :
198 $format = '%2$s&nbsp;(%1$s)';
199 break;
200 }
201
202 $print_label = sprintf( $format, erp_ac_get_currency_symbol() , $label );
203
204 return apply_filters( 'erp_ac_label_symbol_format', $print_label, $format, $label );
205 }
206
207 /**
208 * Return the thousand separator for prices.
209 *
210 * @since 2.3
211 *
212 * @return string
213 */
214 function erp_ac_get_price_thousand_separator() {
215 $separator = stripslashes( erp_get_option( 'erp_ac_th_separator', false, ',' ) );
216 return $separator;
217 }
218
219 /**
220 * Return the decimal separator for prices.
221 *
222 * @since 2.3
223 *
224 * @return string
225 */
226 function erp_ac_get_price_decimal_separator() {
227 $separator = stripslashes( erp_get_option( 'erp_ac_de_separator', false, '.' ) );
228 return $separator ? $separator : '.';
229 }
230
231 /**
232 * Return the number of decimals after the decimal point.
233 *
234 * @since 2.3
235 *
236 * @return int
237 */
238 function erp_ac_get_price_decimals() {
239 return absint( erp_get_option( 'erp_ac_nm_decimal', false, 2 ) );
240 }
241
242 /**
243 * Format the price with a currency symbol.
244 *
245 * @param float $price
246 *
247 * @param array $args (default: array())
248 *
249 * @return string
250 */
251 function erp_ac_get_price( $main_price, $args = array() ) {
252 extract( apply_filters( 'erp_ac_price_args', wp_parse_args( $args, array(
253 'currency' => erp_ac_get_currency(),
254 'decimal_separator' => erp_ac_get_price_decimal_separator(),
255 'thousand_separator' => erp_ac_get_price_thousand_separator(),
256 'decimals' => erp_ac_get_price_decimals(),
257 'price_format' => erp_ac_get_price_format(),
258 'symbol' => true,
259 'currency_symbol' => erp_ac_get_currency_symbol()
260 ) ) ) );
261
262 $price = number_format( abs( $main_price ), $decimals, $decimal_separator, $thousand_separator );
263 $formatted_price = $symbol ? sprintf( $price_format, $currency_symbol, $price ) : $price;
264 $formatted_price = ( $main_price < 0 ) ? '(' . $formatted_price . ')' : $formatted_price;
265
266 return apply_filters( 'erp_ac_price', $formatted_price, $price, $args );
267 }
268
269 function erp_ac_get_price_for_field( $price, $args = array() ) {
270 extract( apply_filters( 'erp_ac_price_args', wp_parse_args( $args, array(
271 'currency' => erp_ac_get_currency(),
272 'decimal_separator' => erp_ac_get_price_decimal_separator(),
273 'thousand_separator' => '',
274 'decimals' => erp_ac_get_price_decimals(),
275 'price_format' => erp_ac_get_price_format(),
276 'symbol' => true,
277 'currency_symbol' => erp_ac_get_currency_symbol()
278 ) ) ) );
279
280 $price = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
281
282 $formatted_price = $symbol ? sprintf( $price_format, $currency_symbol, $price ) : $price;
283 return apply_filters( 'erp_ac_price', $formatted_price, $price, $args );
284 }
285
286 function erp_ac_format_decimal( $number ) {
287 $locale = localeconv();
288
289 $decimals = array( erp_ac_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] );
290
291 // Remove locale from string
292 if ( ! is_float( $number ) ) {
293 $number = sanitize_text_field( str_replace( $decimals, '.', $number ) );
294 }
295
296 if ( is_float( $number ) ) {
297 $number = sanitize_text_field( str_replace( $decimals, '.', strval( $number ) ) );
298 }
299
300 return $number;
301 }
302
303 function erp_ac_get_customer($id) {
304 return erp_get_people( $id );
305 }
306
307 function erp_ac_message( $message_ke = false ) {
308 $message = array(
309 'confirm' => __( 'Are you sure!', 'erp' ),
310 'new_customer' => __( 'New Customer', 'erp' ),
311 'new_vendor' => __( 'New Vendor', 'erp' ),
312 'new' => __( 'Create New', 'erp' ),
313 'transaction' => __( 'Transaction History', 'erp' ),
314 'processing' => __( 'Processing please wait!', 'erp' ),
315 'new_tax' => __( 'Tax Rates', 'erp' ),
316 'tax_item' => __( 'Tax item details', 'erp' ),
317 'tax_update' => __( 'Tax Update', 'erp' ),
318 'tax_deleted' => __( 'Your tax record has been deleted successfully', 'erp' ),
319 'delete' => __( 'Are you sure you want to delete this? This cannot be undone.', 'erp' ),
320 'void' => __( 'Are you sure you want to mark this transaction as void? This action can not be reversed!', 'erp' ),
321 'restore' => __( 'Yes, restore it!', 'erp' ),
322 'cancel' => __( 'Cancel', 'erp' ),
323 'error' => __( 'Error!', 'erp' ),
324 'alreadyExist' => __( 'Already exists as a customer or vendor', 'erp' ),
325 'transaction_status' => __( 'Transaction Status', 'erp' ),
326 'submit' => __( 'Submit', 'erp' ),
327 'redo' => __( 'Yes, redo it!', 'erp' ),
328 'yes' => __( 'Yes, do it!', 'erp' ),
329 'no_result' => __( 'No Result Found!', 'erp' ),
330 'search' => __( 'Search', 'erp' )
331 );
332
333 if ( $message_ke ) {
334 return apply_filters( 'erp_ac_message', $message[$message_ke] );
335 } else {
336 return apply_filters( 'erp_ac_message', $message );
337 }
338
339 }
340
341 function erp_ac_get_version() {
342 return wperpac()->version;
343 }
344
345 function erp_ac_pagination( $total, $limit, $pagenum ) {
346 $num_of_pages = ceil( $total / $limit );
347 $page_links = paginate_links( array(
348 'base' => add_query_arg( 'pagenum', '%#%' ),
349 'format' => '',
350 'prev_text' => __( '&laquo;', 'aag' ),
351 'next_text' => __( '&raquo;', 'aag' ),
352 'add_args' => false,
353 'total' => $num_of_pages,
354 'current' => $pagenum
355 ) );
356
357 if ( $page_links ) {
358 echo '<div class="tablenav"><div class="tablenav-pages">' . $page_links . '</div></div>';
359 }
360 }
361
362 /**
363 * Get invoice prefix
364 *
365 * @param string $form_type
366 * @param int $id
367 *
368 * @since 1.1.2
369 *
370 * @return string
371 */
372 function erp_ac_get_auto_generated_invoice( $form_type ) {
373 $invoice_number = erp_ac_generate_invoice_id( $form_type );
374 $invoice_number = erp_ac_invoice_num_str_pad( $invoice_number );
375 $prefix = erp_ac_get_invoice_format( $form_type );
376 return str_replace( '{id}', $invoice_number, $prefix );
377 }
378
379 /**
380 * Get invoice prefix
381 *
382 * @param string $form_type
383 *
384 * @since 1.1.2
385 *
386 * @return string
387 */
388 function erp_ac_get_invoice_format( $form_type ) {
389 if ( $form_type == 'invoice' ) {
390 return erp_get_option( 'erp_ac_invoice', false, 'INV-{id}' );
391 } else if ( $form_type == 'payment' ) {
392 return erp_get_option( 'erp_ac_payment', false, 'SPN-{id}' );
393 } else if ( $form_type == 'journal' ) {
394 return erp_get_option( 'erp_ac_journal', false, 'JRNN-{id}' );
395 }
396
397 return false;
398 }
399
400 /**
401 * Check invoice existance, in exist then generate new
402 *
403 * @param string $form_type
404 * @param string $invoice
405 *
406 * @since 1.1.2
407 *
408 * @return string
409 */
410 function erp_ac_check_invoice_existance( $type, $invoice_number, $prefix ) {
411
412 $invoice_number = erp_ac_invoice_num_str_pad( $invoice_number );
413 $invoice = str_replace( '{id}', $invoice_number, $prefix );
414 $form_type = '';
415
416 if ( $type == 'erp_ac_invoice' ) {
417 $form_type = 'invoice';
418 } else if ( $type == 'erp_ac_payment' ) {
419 $form_type = 'payment';
420 }
421
422
423 $check_number = \WeDevs\ERP\Accounting\Model\Transaction::select(['invoice_number'])->where( 'invoice_number', '=', $invoice )->where('form_type', '=', $form_type )->get()->toArray();
424
425 if ( $check_number ) {
426 $check_number = \WeDevs\ERP\Accounting\Model\Transaction::select(['invoice_number'])->where('form_type', '=', $form_type )->get()->toArray();
427 $check_number = wp_list_pluck( $check_number, 'invoice_number' );
428 $check_status = true;
429
430 while ( $check_status ) {
431 $invoice_number = $invoice_number + 1;
432 $invoice_number = erp_ac_invoice_num_str_pad( $invoice_number );
433 $invoice = str_replace( '{id}', $invoice_number, $prefix );
434
435 if ( ! in_array( $invoice, $check_number ) ) {
436 $check_status = false;
437 }
438 }
439 }
440
441 return $invoice;
442 }
443
444 /**
445 * Str pad for invoice number
446 *
447 * @param int $invoice_number
448 *
449 * @since 1.1.2
450 *
451 * @return string
452 */
453 function erp_ac_invoice_num_str_pad( $invoice_number ) {
454 return str_pad( $invoice_number, 4, '0', STR_PAD_LEFT );
455 }
456
457 /**
458 * Generate invoice id
459 *
460 * @param string $form_type
461 *
462 * @return int
463 */
464 function erp_ac_generate_invoice_id( $form_type = '' ) {
465
466 $invoice_number = false;
467
468 if ( $form_type == 'invoice' ) {
469 $invoice_number = get_option( 'erp_ac_sales_invoice_number', 1 );
470
471 } else if ( $form_type == 'payment' ) {
472 $invoice_number = get_option( 'erp_ac_sales_payment_number', 1 );
473
474 } else if ( $form_type == 'journal' ) {
475 $invoice_number = get_option( 'erp_ac_journal_number', 1 );
476
477 }
478
479 return $invoice_number; //str_pad( $invoice_number, 4, '0', STR_PAD_LEFT );
480 }
481
482 /**
483 * Update Invoice number
484 *
485 * @param string $form_type
486 *
487 * @return void
488 */
489 function erp_ac_update_invoice_number( $form_type ) {
490
491 $invoice_number = '';
492 if ( $form_type == 'invoice' ) {
493 $invoice_number = get_option( 'erp_ac_sales_invoice_number', 1 );
494 } else if ( $form_type == 'payment' ) {
495 $invoice_number = get_option( 'erp_ac_sales_payment_number', 1 );
496 }
497 $get_invoice_number = WeDevs\ERP\Accounting\Model\Transaction::select('invoice_number')
498 ->where( 'form_type', '=', $form_type )
499 ->where( 'invoice_number', '>=', $invoice_number )
500 ->get()->toArray();
501 $get_invoice_number = wp_list_pluck( $get_invoice_number, 'invoice_number' );
502 $status = true;
503
504 while( $status ) {
505 if ( in_array( $invoice_number, $get_invoice_number ) ) {
506 $invoice_number = $invoice_number + 1;
507 } else {
508 $status = false;
509 }
510 }
511
512 if ( $form_type == 'invoice' ) {
513 update_option( 'erp_ac_sales_invoice_number', $invoice_number );
514 } else if ( $form_type == 'payment' ) {
515 update_option( 'erp_ac_sales_payment_number', $invoice_number );
516 }
517
518 }
519
520 /**
521 * Get default invoice prefix
522 *
523 * @param string $type
524 *
525 * @since 1.1.2
526 *
527 * @return mixed string or array
528 */
529 function erp_ac_get_default_invoice_prefix( $type = false ) {
530
531 $prefix = [
532 'erp_ac_payment' => 'SPN-{id}',
533 'erp_ac_invoice' => 'INV-{id}',
534 'erp_ac_payment_voucher' => 'EVN-{id}',
535 'erp_ac_vendor_credit' => 'ECN-{id}',
536 'erp_ac_journal' => 'JRNN-{id}'
537 ];
538
539 return $type ? $prefix[$type] : $prefix;
540 }
541
542 /**
543 * Get unique transaction hash for sharing to customer
544 *
545 * @param object $transaction
546 * @param string $algo
547 * @since 1.1.2
548 * @return string
549 */
550 function erp_ac_get_invoice_link_hash( $transaction = '', $algo = 'sha256' ) {
551
552 if ( $transaction ) {
553
554 $to_hash = $transaction->id . $transaction->form_type . $transaction->invoice_number;
555 $hash_string = hash( $algo, $to_hash );
556 }
557
558 return $hash_string;
559 }
560
561 /**
562 * Varify transaction hash
563 *
564 * @param object $transaction
565 * @param string $hash_to_verify
566 * @since 1.1.2
567 * @return bool
568 */
569 function erp_ac_verify_invoice_link_hash( $transaction = '', $hash_to_verify = '', $algo = 'sha256' ) {
570
571 if ( $transaction && $hash_to_verify ) {
572
573 $to_hash = $transaction['id'] . $transaction['form_type'] . $transaction['invoice_number'];
574 $hash_original = hash( $algo, $to_hash );
575
576 if ( $hash_original === $hash_to_verify ) {
577 return true;
578 }
579 }
580
581 return false;
582 }
583
584 /**
585 * Callback to template_redirect hook
586 * Shows template when invoice readonly link is called
587 *
588 * @since 1.1.2
589 * @return mixed
590 */
591 function erp_ac_readonly_invoice_template() {
592
593 $query = isset( $_REQUEST['query'] ) ? esc_attr( $_REQUEST['query'] ) : '';
594 $transaction_id = isset( $_REQUEST['trans_id'] ) ? intval( $_REQUEST['trans_id'] ) : '';
595 $auth_id = isset( $_REQUEST['auth'] ) ? esc_attr( $_REQUEST['auth'] ) : '';
596 $verified = false;
597
598 if ( !$query || !$transaction_id || !$auth_id ) {
599 return;
600 }
601
602 $transaction = erp_ac_get_transaction( $transaction_id );
603
604 if ( $transaction ) {
605 $verified = erp_ac_verify_invoice_link_hash( $transaction, $auth_id );
606 }
607
608 if ( $verified ) {
609 include WPERP_ACCOUNTING_VIEWS . '/sales/template-invoice-readonly.php';
610 exit();
611 }
612
613 return;
614 }
615
616 /**
617 * Get invoice number and format fron transaction submit value
618 *
619 * @param string $submit_invoice
620 * @param string $invoice_format
621 *
622 * @return array
623 */
624 function erp_ac_get_invoice_num_fromat_from_submit_invoice( $submit_invoice, $invoice_format ) {
625 //was found
626 $pattern = str_replace( '{id}', '([0-9]+)', $invoice_format ); // INV-([0-9])+-INV
627
628 preg_match( "/${pattern}/", $submit_invoice, $match );
629
630 $id = isset( $match[1] ) ? $match[1] : false;
631 $check_invoice = false;
632
633 if ( $id === false ) {
634 return 0;
635 }
636
637 $check_invoice = str_replace( '{id}', $id, $invoice_format );
638
639 $invoice_number = $check_invoice == $submit_invoice ? intval( $id ) : 0;
640
641 return $invoice_number;
642 }
643
644 /**
645 * Get invoice number
646 *
647 * @param int $invoice_number
648 * @param string $invoice_number
649 *
650 * @return string
651 */
652 function erp_ac_get_invoice_number( $invoice_number, $invoice_format ) {
653 if ( $invoice_number != 0 ) {
654 return str_replace( '{id}', erp_ac_invoice_num_str_pad( $invoice_number ), $invoice_format );
655 } else {
656 return $invoice_format;
657 }
658 }
659
660 /**
661 * Prevent redirect to woocommerce my account page
662 *
663 * @param boolean $prevent_access
664 *
665 * @since 1.1.18
666 *
667 * @return boolean
668 */
669 function erp_ac_wc_prevent_admin_access( $prevent_access ) {
670 if ( current_user_can( erp_ac_get_manager_role() ) ) {
671 return false;
672 }
673
674 return $prevent_access;
675 }
676
677
678 /**
679 * Redirect accounting role based user to their page
680 *
681 * @since 1.2.5
682 *
683 * @param $redirect_to
684 * @param $roles
685 *
686 * @return string
687 */
688 function erp_ac_login_redirect( $redirect_to, $roles ) {
689 $employee_role = erp_ac_get_manager_role();
690
691 if ( in_array( $employee_role, $roles ) ) {
692 $redirect_to = get_admin_url( null, 'admin.php?page=erp-accounting' );
693 }
694
695 return $redirect_to;
696 }
697