| 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 |
if ( $_GET['page'] === 'erp-accounting-sales' ) { |
| 80 |
$label = __( 'Received', 'erp' ); |
| 81 |
} else { |
| 82 |
$label = __( 'Paid', 'erp' ); |
| 83 |
} |
| 84 |
|
| 85 |
break; |
| 86 |
|
| 87 |
case 'paid': |
| 88 |
$label = __( 'Completed', 'erp' ); |
| 89 |
break; |
| 90 |
|
| 91 |
case 'awaiting_payment': |
| 92 |
$url = admin_url( 'admin.php?page=' . $slug . '&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id ); |
| 93 |
|
| 94 |
if ( $_GET['page'] === 'erp-accounting-sales' ) { |
| 95 |
$label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Awaiting Payment', 'erp' ) ); |
| 96 |
} else { |
| 97 |
$label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Payment Pending', 'erp' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
break; |
| 101 |
|
| 102 |
case 'overdue': |
| 103 |
$label = __( 'Overdue', 'erp' ); |
| 104 |
break; |
| 105 |
|
| 106 |
case 'deleted': |
| 107 |
$label = __( 'Trash', 'erp' ); |
| 108 |
break; |
| 109 |
|
| 110 |
case 'partial': |
| 111 |
if ( $_GET['page'] === 'erp-accounting-sales' ) { |
| 112 |
$label = __( 'Partially Received', 'erp' ); |
| 113 |
} else { |
| 114 |
$label = __( 'Partially Paid', 'erp' ); |
| 115 |
} |
| 116 |
|
| 117 |
break; |
| 118 |
|
| 119 |
case 'void': |
| 120 |
$label = __( 'Void', 'erp' ); |
| 121 |
break; |
| 122 |
|
| 123 |
case 'pending': |
| 124 |
$url = admin_url( 'admin.php?page=' . $slug . '&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id ); |
| 125 |
$label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Awaiting Approval', 'erp' ) ); |
| 126 |
break; |
| 127 |
|
| 128 |
case 'draft': |
| 129 |
$url = admin_url( 'admin.php?page=' . $slug . '&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id ); |
| 130 |
$label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Draft', 'erp' ) ); |
| 131 |
break; |
| 132 |
|
| 133 |
case 'awaiting_approval': |
| 134 |
$url = admin_url( 'admin.php?page=' . $slug . '&action=new&type=' . $items->form_type . '&transaction_id=' . $items->id ); |
| 135 |
$label = sprintf( '<a href="%1s">%2s</a>', $url, __( 'Awaiting Approval', 'erp' ) ); |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
return apply_filters( 'erp_ac_status_labels', $label, $status ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get currency symbol |
| 144 |
* |
| 145 |
* @since 0.1 |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
function erp_ac_get_currency_symbol() { |
| 150 |
$currency = erp_ac_get_currency(); |
| 151 |
|
| 152 |
return erp_get_currency_symbol( $currency ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get currency |
| 157 |
* |
| 158 |
* @since 0.1 |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
function erp_ac_get_currency() { |
| 163 |
return erp_get_currency(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get the price format depending on the currency position. |
| 168 |
* |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
function erp_ac_get_price_format() { |
| 172 |
$currency_pos = erp_get_option( 'erp_ac_currency_position', false, 'left' ); |
| 173 |
$format = '%1$s%2$s'; |
| 174 |
|
| 175 |
switch ( $currency_pos ) { |
| 176 |
case 'left' : |
| 177 |
$format = '%1$s%2$s'; |
| 178 |
break; |
| 179 |
case 'right' : |
| 180 |
$format = '%2$s%1$s'; |
| 181 |
break; |
| 182 |
case 'left_space' : |
| 183 |
$format = '%1$s %2$s'; |
| 184 |
break; |
| 185 |
case 'right_space' : |
| 186 |
$format = '%2$s %1$s'; |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
return apply_filters( 'erp_ac_price_format', $format, $currency_pos ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get the price format depending on the currency position. |
| 195 |
* |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
function erp_ac_get_symbol_format_for_label( $label ) { |
| 199 |
$currency_pos = erp_get_option( 'erp_ac_currency_position', false, 'left' ); |
| 200 |
$format = '(%1$s)%2$s'; |
| 201 |
|
| 202 |
switch ( $currency_pos ) { |
| 203 |
case 'left' : |
| 204 |
$format = '(%1$s)%2$s'; |
| 205 |
break; |
| 206 |
case 'right' : |
| 207 |
$format = '%2$s(%1$s)'; |
| 208 |
break; |
| 209 |
case 'left_space' : |
| 210 |
$format = '(%1$s) %2$s'; |
| 211 |
break; |
| 212 |
case 'right_space' : |
| 213 |
$format = '%2$s (%1$s)'; |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
$print_label = sprintf( $format, erp_ac_get_currency_symbol(), $label ); |
| 218 |
|
| 219 |
return apply_filters( 'erp_ac_label_symbol_format', $print_label, $format, $label ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Return the thousand separator for prices. |
| 224 |
* |
| 225 |
* @since 2.3 |
| 226 |
* |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
function erp_ac_get_price_thousand_separator() { |
| 230 |
$separator = stripslashes( erp_get_option( 'erp_ac_th_separator', false, ',' ) ); |
| 231 |
|
| 232 |
return $separator; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Return the decimal separator for prices. |
| 237 |
* |
| 238 |
* @since 2.3 |
| 239 |
* |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
function erp_ac_get_price_decimal_separator() { |
| 243 |
$separator = stripslashes( erp_get_option( 'erp_ac_de_separator', false, '.' ) ); |
| 244 |
|
| 245 |
return $separator ? $separator : '.'; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Return the number of decimals after the decimal point. |
| 250 |
* |
| 251 |
* @since 2.3 |
| 252 |
* |
| 253 |
* @return int |
| 254 |
*/ |
| 255 |
function erp_ac_get_price_decimals() { |
| 256 |
return absint( erp_get_option( 'erp_ac_nm_decimal', false, 2 ) ); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Format the price with a currency symbol. |
| 261 |
* |
| 262 |
* @param float $price |
| 263 |
* |
| 264 |
* @param array $args (default: array()) |
| 265 |
* |
| 266 |
* @return string |
| 267 |
*/ |
| 268 |
function erp_ac_get_price( $main_price, $args = array() ) { |
| 269 |
extract( apply_filters( 'erp_ac_price_args', wp_parse_args( $args, array( |
| 270 |
'currency' => erp_ac_get_currency(), |
| 271 |
'decimal_separator' => erp_ac_get_price_decimal_separator(), |
| 272 |
'thousand_separator' => erp_ac_get_price_thousand_separator(), |
| 273 |
'decimals' => erp_ac_get_price_decimals(), |
| 274 |
'price_format' => erp_ac_get_price_format(), |
| 275 |
'symbol' => true, |
| 276 |
'currency_symbol' => erp_ac_get_currency_symbol() |
| 277 |
) ) ) ); |
| 278 |
|
| 279 |
$price = number_format( abs( $main_price ), $decimals, $decimal_separator, $thousand_separator ); |
| 280 |
$formatted_price = $symbol ? sprintf( $price_format, $currency_symbol, $price ) : $price; |
| 281 |
$formatted_price = ( $main_price < 0 ) ? '(' . $formatted_price . ')' : $formatted_price; |
| 282 |
|
| 283 |
return apply_filters( 'erp_ac_price', $formatted_price, $price, $args ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* @since 1.2.3 |
| 288 |
* |
| 289 |
* @param $price |
| 290 |
* @param array $args |
| 291 |
* |
| 292 |
* @return mixed|void |
| 293 |
* |
| 294 |
*/ |
| 295 |
function erp_ac_get_price_for_field( $price, $args = array() ) { |
| 296 |
extract( apply_filters( 'erp_ac_price_args', wp_parse_args( $args, array( |
| 297 |
'currency' => erp_ac_get_currency(), |
| 298 |
'decimal_separator' => erp_ac_get_price_decimal_separator(), |
| 299 |
'thousand_separator' => '', |
| 300 |
'decimals' => erp_ac_get_price_decimals(), |
| 301 |
'price_format' => erp_ac_get_price_format(), |
| 302 |
'symbol' => true, |
| 303 |
'currency_symbol' => erp_ac_get_currency_symbol() |
| 304 |
) ) ) ); |
| 305 |
|
| 306 |
$price = number_format( $price, $decimals, $decimal_separator, $thousand_separator ); |
| 307 |
|
| 308 |
$formatted_price = $symbol ? sprintf( $price_format, $currency_symbol, $price ) : $price; |
| 309 |
|
| 310 |
return apply_filters( 'erp_ac_price', $formatted_price, $price, $args ); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
function erp_ac_format_decimal( $number, $dp = false, $trim_zeros = false) { |
| 315 |
$locale = localeconv(); |
| 316 |
$decimals = array( erp_ac_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] ); |
| 317 |
|
| 318 |
// Remove locale from string |
| 319 |
if ( ! is_float( $number ) ) { |
| 320 |
$number = sanitize_text_field( str_replace( $decimals, '.', $number ) ); |
| 321 |
} |
| 322 |
if ( false !== $dp ) { |
| 323 |
$dp = intval( '' == $dp ? erp_ac_get_price_decimals() : $dp ); |
| 324 |
$number = number_format( floatval( $number ), $dp, '.', '' ); |
| 325 |
// DP is false - don't use number format, just return a string in our format |
| 326 |
} elseif ( is_float( $number ) ) { |
| 327 |
// DP is false - don't use number format, just return a string using whatever is given. Remove scientific notation using sprintf. |
| 328 |
$number = str_replace( $decimals, '.', sprintf( '%.' . 4 . 'f', $number ) ); |
| 329 |
// We already had a float, so trailing zeros are not needed. |
| 330 |
$trim_zeros = true; |
| 331 |
} |
| 332 |
|
| 333 |
if ( is_float( $number ) ) { |
| 334 |
$number = sanitize_text_field( str_replace( $decimals, '.', strval( $number ) ) ); |
| 335 |
} |
| 336 |
|
| 337 |
if ( $trim_zeros && strstr( $number, '.' ) ) { |
| 338 |
$number = rtrim( rtrim( $number, '0' ), '.' ); |
| 339 |
} |
| 340 |
|
| 341 |
return $number; |
| 342 |
} |
| 343 |
|
| 344 |
function erp_ac_remove_thousand_sep( $number ) { |
| 345 |
$sep = erp_ac_get_price_thousand_separator(); |
| 346 |
if ( $sep ) { |
| 347 |
$number = str_replace( $sep, '', $number ); |
| 348 |
} |
| 349 |
|
| 350 |
return $number; |
| 351 |
} |
| 352 |
|
| 353 |
function erp_ac_get_customer( $id ) { |
| 354 |
return erp_get_people( $id ); |
| 355 |
} |
| 356 |
|
| 357 |
function erp_ac_message( $message_ke = false ) { |
| 358 |
$message = array( |
| 359 |
'confirm' => __( 'Are you sure!', 'erp' ), |
| 360 |
'new_customer' => __( 'New Customer', 'erp' ), |
| 361 |
'new_vendor' => __( 'New Vendor', 'erp' ), |
| 362 |
'new' => __( 'Create New', 'erp' ), |
| 363 |
'transaction' => __( 'Transaction History', 'erp' ), |
| 364 |
'processing' => __( 'Processing please wait!', 'erp' ), |
| 365 |
'new_tax' => __( 'Tax Rates', 'erp' ), |
| 366 |
'tax_item' => __( 'Tax item details', 'erp' ), |
| 367 |
'tax_update' => __( 'Tax Update', 'erp' ), |
| 368 |
'tax_deleted' => __( 'Your tax record has been deleted successfully', 'erp' ), |
| 369 |
'delete' => __( 'Are you sure you want to delete this? This cannot be undone.', 'erp' ), |
| 370 |
'void' => __( 'Are you sure you want to mark this transaction as void? This action can not be reversed!', 'erp' ), |
| 371 |
'restore' => __( 'Yes, restore it!', 'erp' ), |
| 372 |
'cancel' => __( 'Cancel', 'erp' ), |
| 373 |
'error' => __( 'Error!', 'erp' ), |
| 374 |
'alreadyExist' => __( 'Already exists as a customer or vendor', 'erp' ), |
| 375 |
'transaction_status' => __( 'Transaction Status', 'erp' ), |
| 376 |
'submit' => __( 'Submit', 'erp' ), |
| 377 |
'redo' => __( 'Yes, redo it!', 'erp' ), |
| 378 |
'yes' => __( 'Yes, do it!', 'erp' ), |
| 379 |
'no_result' => __( 'No Result Found!', 'erp' ), |
| 380 |
'search' => __( 'Search', 'erp' ) |
| 381 |
); |
| 382 |
|
| 383 |
if ( $message_ke ) { |
| 384 |
return apply_filters( 'erp_ac_message', $message[ $message_ke ] ); |
| 385 |
} else { |
| 386 |
return apply_filters( 'erp_ac_message', $message ); |
| 387 |
} |
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
function erp_ac_get_version() { |
| 392 |
return wperpac()->version; |
| 393 |
} |
| 394 |
|
| 395 |
function erp_ac_pagination( $total, $limit, $pagenum ) { |
| 396 |
$num_of_pages = ceil( $total / $limit ); |
| 397 |
$page_links = paginate_links( array( |
| 398 |
'base' => add_query_arg( 'pagenum', '%#%' ), |
| 399 |
'format' => '', |
| 400 |
'prev_text' => __( '«', 'aag' ), |
| 401 |
'next_text' => __( '»', 'aag' ), |
| 402 |
'add_args' => false, |
| 403 |
'total' => $num_of_pages, |
| 404 |
'current' => $pagenum |
| 405 |
) ); |
| 406 |
|
| 407 |
if ( $page_links ) { |
| 408 |
echo '<div class="tablenav"><div class="tablenav-pages">' . $page_links . '</div></div>'; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Get invoice prefix |
| 414 |
* |
| 415 |
* @param string $form_type |
| 416 |
* @param int $id |
| 417 |
* |
| 418 |
* @since 1.1.2 |
| 419 |
* |
| 420 |
* @return string |
| 421 |
*/ |
| 422 |
function erp_ac_get_auto_generated_invoice( $form_type ) { |
| 423 |
$invoice_number = erp_ac_generate_invoice_id( $form_type ); |
| 424 |
$invoice_number = erp_ac_invoice_num_str_pad( $invoice_number ); |
| 425 |
$prefix = erp_ac_get_invoice_format( $form_type ); |
| 426 |
|
| 427 |
return str_replace( '{id}', $invoice_number, $prefix ); |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Get invoice prefix |
| 432 |
* |
| 433 |
* @param string $form_type |
| 434 |
* |
| 435 |
* @since 1.1.2 |
| 436 |
* |
| 437 |
* @return string |
| 438 |
*/ |
| 439 |
function erp_ac_get_invoice_format( $form_type ) { |
| 440 |
if ( $form_type == 'invoice' ) { |
| 441 |
return erp_get_option( 'erp_ac_invoice', false, 'INV-{id}' ); |
| 442 |
} else if ( $form_type == 'payment' ) { |
| 443 |
return erp_get_option( 'erp_ac_payment', false, 'SPN-{id}' ); |
| 444 |
} else if ( $form_type == 'journal' ) { |
| 445 |
return erp_get_option( 'erp_ac_journal', false, 'JRNN-{id}' ); |
| 446 |
} |
| 447 |
|
| 448 |
return false; |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Check invoice existance, in exist then generate new |
| 453 |
* |
| 454 |
* @param string $form_type |
| 455 |
* @param string $invoice |
| 456 |
* |
| 457 |
* @since 1.1.2 |
| 458 |
* |
| 459 |
* @return string |
| 460 |
*/ |
| 461 |
function erp_ac_check_invoice_existance( $type, $invoice_number, $prefix ) { |
| 462 |
|
| 463 |
$invoice_number = erp_ac_invoice_num_str_pad( $invoice_number ); |
| 464 |
$invoice = str_replace( '{id}', $invoice_number, $prefix ); |
| 465 |
$form_type = ''; |
| 466 |
|
| 467 |
if ( $type == 'erp_ac_invoice' ) { |
| 468 |
$form_type = 'invoice'; |
| 469 |
} else if ( $type == 'erp_ac_payment' ) { |
| 470 |
$form_type = 'payment'; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
$check_number = \WeDevs\ERP\Accounting\Model\Transaction::select( [ 'invoice_number' ] )->where( 'invoice_number', '=', $invoice )->where( 'form_type', '=', $form_type )->get()->toArray(); |
| 475 |
|
| 476 |
if ( $check_number ) { |
| 477 |
$check_number = \WeDevs\ERP\Accounting\Model\Transaction::select( [ 'invoice_number' ] )->where( 'form_type', '=', $form_type )->get()->toArray(); |
| 478 |
$check_number = wp_list_pluck( $check_number, 'invoice_number' ); |
| 479 |
$check_status = true; |
| 480 |
|
| 481 |
while ( $check_status ) { |
| 482 |
$invoice_number = $invoice_number + 1; |
| 483 |
$invoice_number = erp_ac_invoice_num_str_pad( $invoice_number ); |
| 484 |
$invoice = str_replace( '{id}', $invoice_number, $prefix ); |
| 485 |
|
| 486 |
if ( ! in_array( $invoice, $check_number ) ) { |
| 487 |
$check_status = false; |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
return $invoice; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Str pad for invoice number |
| 497 |
* |
| 498 |
* @param int $invoice_number |
| 499 |
* |
| 500 |
* @since 1.1.2 |
| 501 |
* |
| 502 |
* @return string |
| 503 |
*/ |
| 504 |
function erp_ac_invoice_num_str_pad( $invoice_number ) { |
| 505 |
return str_pad( $invoice_number, 4, '0', STR_PAD_LEFT ); |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Generate invoice id |
| 510 |
* |
| 511 |
* @param string $form_type |
| 512 |
* |
| 513 |
* @return int |
| 514 |
*/ |
| 515 |
function erp_ac_generate_invoice_id( $form_type = '' ) { |
| 516 |
|
| 517 |
$invoice_number = false; |
| 518 |
|
| 519 |
if ( $form_type == 'invoice' ) { |
| 520 |
$invoice_number = get_option( 'erp_ac_sales_invoice_number', 1 ); |
| 521 |
|
| 522 |
} else if ( $form_type == 'payment' ) { |
| 523 |
$invoice_number = get_option( 'erp_ac_sales_payment_number', 1 ); |
| 524 |
|
| 525 |
} else if ( $form_type == 'journal' ) { |
| 526 |
$invoice_number = get_option( 'erp_ac_journal_number', 1 ); |
| 527 |
|
| 528 |
} |
| 529 |
|
| 530 |
return $invoice_number; //str_pad( $invoice_number, 4, '0', STR_PAD_LEFT ); |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* Update Invoice number |
| 535 |
* |
| 536 |
* @param string $form_type |
| 537 |
* |
| 538 |
* @return void |
| 539 |
*/ |
| 540 |
function erp_ac_update_invoice_number( $form_type ) { |
| 541 |
|
| 542 |
$invoice_number = ''; |
| 543 |
if ( $form_type == 'invoice' ) { |
| 544 |
$invoice_number = get_option( 'erp_ac_sales_invoice_number', 1 ); |
| 545 |
} else if ( $form_type == 'payment' ) { |
| 546 |
$invoice_number = get_option( 'erp_ac_sales_payment_number', 1 ); |
| 547 |
} |
| 548 |
$get_invoice_number = WeDevs\ERP\Accounting\Model\Transaction::select( 'invoice_number' ) |
| 549 |
->where( 'form_type', '=', $form_type ) |
| 550 |
->where( 'invoice_number', '>=', $invoice_number ) |
| 551 |
->get()->toArray(); |
| 552 |
$get_invoice_number = wp_list_pluck( $get_invoice_number, 'invoice_number' ); |
| 553 |
$status = true; |
| 554 |
|
| 555 |
while ( $status ) { |
| 556 |
if ( in_array( $invoice_number, $get_invoice_number ) ) { |
| 557 |
$invoice_number = $invoice_number + 1; |
| 558 |
} else { |
| 559 |
$status = false; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
if ( $form_type == 'invoice' ) { |
| 564 |
update_option( 'erp_ac_sales_invoice_number', $invoice_number ); |
| 565 |
} else if ( $form_type == 'payment' ) { |
| 566 |
update_option( 'erp_ac_sales_payment_number', $invoice_number ); |
| 567 |
} |
| 568 |
|
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* Get default invoice prefix |
| 573 |
* |
| 574 |
* @param string $type |
| 575 |
* |
| 576 |
* @since 1.1.2 |
| 577 |
* |
| 578 |
* @return mixed string or array |
| 579 |
*/ |
| 580 |
function erp_ac_get_default_invoice_prefix( $type = false ) { |
| 581 |
|
| 582 |
$prefix = [ |
| 583 |
'erp_ac_payment' => 'SPN-{id}', |
| 584 |
'erp_ac_invoice' => 'INV-{id}', |
| 585 |
'erp_ac_payment_voucher' => 'EVN-{id}', |
| 586 |
'erp_ac_vendor_credit' => 'ECN-{id}', |
| 587 |
'erp_ac_journal' => 'JRNN-{id}' |
| 588 |
]; |
| 589 |
|
| 590 |
return $type ? $prefix[ $type ] : $prefix; |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* Get unique transaction hash for sharing to customer |
| 595 |
* |
| 596 |
* @param object $transaction |
| 597 |
* @param string $algo |
| 598 |
* |
| 599 |
* @since 1.1.2 |
| 600 |
* @return string |
| 601 |
*/ |
| 602 |
function erp_ac_get_invoice_link_hash( $transaction = '', $algo = 'sha256' ) { |
| 603 |
|
| 604 |
if ( $transaction ) { |
| 605 |
|
| 606 |
$to_hash = $transaction->id . $transaction->form_type . $transaction->invoice_number; |
| 607 |
$hash_string = hash( $algo, $to_hash ); |
| 608 |
} |
| 609 |
|
| 610 |
return $hash_string; |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Varify transaction hash |
| 615 |
* |
| 616 |
* @param object $transaction |
| 617 |
* @param string $hash_to_verify |
| 618 |
* |
| 619 |
* @since 1.1.2 |
| 620 |
* @return bool |
| 621 |
*/ |
| 622 |
function erp_ac_verify_invoice_link_hash( $transaction = '', $hash_to_verify = '', $algo = 'sha256' ) { |
| 623 |
|
| 624 |
if ( $transaction && $hash_to_verify ) { |
| 625 |
|
| 626 |
$to_hash = $transaction['id'] . $transaction['form_type'] . $transaction['invoice_number']; |
| 627 |
$hash_original = hash( $algo, $to_hash ); |
| 628 |
|
| 629 |
if ( $hash_original === $hash_to_verify ) { |
| 630 |
return true; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
return false; |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* Callback to template_redirect hook |
| 639 |
* Shows template when invoice readonly link is called |
| 640 |
* |
| 641 |
* @since 1.1.2 |
| 642 |
* @return mixed |
| 643 |
*/ |
| 644 |
function erp_ac_readonly_invoice_template() { |
| 645 |
|
| 646 |
$query = isset( $_REQUEST['query'] ) ? esc_attr( $_REQUEST['query'] ) : ''; |
| 647 |
$transaction_id = isset( $_REQUEST['trans_id'] ) ? intval( $_REQUEST['trans_id'] ) : ''; |
| 648 |
$auth_id = isset( $_REQUEST['auth'] ) ? esc_attr( $_REQUEST['auth'] ) : ''; |
| 649 |
$verified = false; |
| 650 |
|
| 651 |
if ( ! $query || ! $transaction_id || ! $auth_id ) { |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
$transaction = erp_ac_get_transaction( $transaction_id ); |
| 656 |
|
| 657 |
if ( $transaction ) { |
| 658 |
$verified = erp_ac_verify_invoice_link_hash( $transaction, $auth_id ); |
| 659 |
} |
| 660 |
|
| 661 |
if ( $verified ) { |
| 662 |
include WPERP_ACCOUNTING_VIEWS . '/sales/template-invoice-readonly.php'; |
| 663 |
exit(); |
| 664 |
} |
| 665 |
|
| 666 |
return; |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
* Get invoice number and format fron transaction submit value |
| 671 |
* |
| 672 |
* @param string $submit_invoice |
| 673 |
* @param string $invoice_format |
| 674 |
* |
| 675 |
* @return array |
| 676 |
*/ |
| 677 |
function erp_ac_get_invoice_num_fromat_from_submit_invoice( $submit_invoice, $invoice_format ) { |
| 678 |
//was found |
| 679 |
$pattern = str_replace( '{id}', '([0-9]+)', $invoice_format ); // INV-([0-9])+-INV |
| 680 |
|
| 681 |
preg_match( "/${pattern}/", $submit_invoice, $match ); |
| 682 |
|
| 683 |
$id = isset( $match[1] ) ? $match[1] : false; |
| 684 |
$check_invoice = false; |
| 685 |
|
| 686 |
if ( $id === false ) { |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
$check_invoice = str_replace( '{id}', $id, $invoice_format ); |
| 691 |
|
| 692 |
$invoice_number = $check_invoice == $submit_invoice ? intval( $id ) : 0; |
| 693 |
|
| 694 |
return $invoice_number; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* Get invoice number |
| 699 |
* |
| 700 |
* @param int $invoice_number |
| 701 |
* @param string $invoice_number |
| 702 |
* |
| 703 |
* @return string |
| 704 |
*/ |
| 705 |
function erp_ac_get_invoice_number( $invoice_number, $invoice_format ) { |
| 706 |
if ( $invoice_number != 0 ) { |
| 707 |
return str_replace( '{id}', erp_ac_invoice_num_str_pad( $invoice_number ), $invoice_format ); |
| 708 |
} else { |
| 709 |
return $invoice_format; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* Prevent redirect to woocommerce my account page |
| 715 |
* |
| 716 |
* @param boolean $prevent_access |
| 717 |
* |
| 718 |
* @since 1.1.18 |
| 719 |
* |
| 720 |
* @return boolean |
| 721 |
*/ |
| 722 |
function erp_ac_wc_prevent_admin_access( $prevent_access ) { |
| 723 |
if ( current_user_can( erp_ac_get_manager_role() ) ) { |
| 724 |
return false; |
| 725 |
} |
| 726 |
|
| 727 |
return $prevent_access; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
/** |
| 732 |
* Redirect accounting role based user to their page |
| 733 |
* |
| 734 |
* @since 1.2.5 |
| 735 |
* |
| 736 |
* @param $redirect_to |
| 737 |
* @param $roles |
| 738 |
* |
| 739 |
* @return string |
| 740 |
*/ |
| 741 |
function erp_ac_login_redirect( $redirect_to, $roles ) { |
| 742 |
$employee_role = erp_ac_get_manager_role(); |
| 743 |
|
| 744 |
if ( in_array( $employee_role, $roles ) ) { |
| 745 |
$redirect_to = get_admin_url( null, 'admin.php?page=erp-accounting' ); |
| 746 |
} |
| 747 |
|
| 748 |
return $redirect_to; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Auto create customer when creating CRM contact/company |
| 753 |
* |
| 754 |
* @param $customer |
| 755 |
* @param $customer_id |
| 756 |
* @param $data |
| 757 |
* |
| 758 |
* @since 1.2.7 |
| 759 |
* |
| 760 |
* @return void |
| 761 |
*/ |
| 762 |
function erp_ac_customer_create_from_crm( $customer, $customer_id, $data ) { |
| 763 |
|
| 764 |
$customer_auto_import = (int) erp_get_option( 'customer_auto_import', false, 0 ); |
| 765 |
$crm_user_type = erp_get_option( 'crm_user_type', false, [] ); // Contact or Company |
| 766 |
|
| 767 |
if ( ! $customer_auto_import ) { |
| 768 |
return; |
| 769 |
} |
| 770 |
|
| 771 |
if ( ! count( $crm_user_type ) ) { |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
// no need to add wordpress `user id` again |
| 776 |
// [ `user id` already added when contact is created ] |
| 777 |
$data['is_wp_user'] = false; |
| 778 |
$data['wp_user_id'] = ''; |
| 779 |
|
| 780 |
if ( ! isset( $data['company'] ) ) { |
| 781 |
// the created crm user type is NOT a company |
| 782 |
if ( ! in_array( 'contact', $crm_user_type ) ) { |
| 783 |
return; |
| 784 |
} |
| 785 |
} else { |
| 786 |
if ( ! in_array( 'company', $crm_user_type ) ) { |
| 787 |
return; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
$data['people_id'] = $customer_id; |
| 792 |
$data['type'] = 'customer'; |
| 793 |
|
| 794 |
erp_convert_to_people( $data ); |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* Accounting customer auto create from crm |
| 799 |
* |
| 800 |
* @since 1.2.8 |
| 801 |
* |
| 802 |
* @param int $contact_id |
| 803 |
* @param object $data |
| 804 |
* |
| 805 |
* @return void |
| 806 |
*/ |
| 807 |
function erp_ac_customer_auto_create_from_crm( $contact_id, $data ) { |
| 808 |
return erp_ac_customer_create_from_crm( [], $contact_id, $data ); |
| 809 |
} |
| 810 |
|
| 811 |
|