| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Get all currencies |
| 9 |
* |
| 10 |
* @return mixed |
| 11 |
*/ |
| 12 |
function erp_acct_get_all_currencies( $count = false ) { |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
if ( $count ) { |
| 16 |
return $wpdb->get_var( "SELECT count(*) FROM {$wpdb->prefix}erp_acct_currency_info" ); |
| 17 |
} |
| 18 |
|
| 19 |
return $wpdb->get_results( "SELECT id, name, sign FROM {$wpdb->prefix}erp_acct_currency_info", ARRAY_A ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Get currencies dropdown |
| 24 |
* |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
function erp_acct_get_currencies_for_dropdown() { |
| 28 |
$currencies = erp_acct_get_all_currencies(); |
| 29 |
|
| 30 |
$currencies_dropdown = []; |
| 31 |
|
| 32 |
foreach ( $currencies as $currency ) { |
| 33 |
$currencies_dropdown[ $currency['id'] ] = $currency['name'] . ' (' . $currency['sign'] . ')'; |
| 34 |
} |
| 35 |
|
| 36 |
return $currencies_dropdown; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get active currency symbol |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
function erp_acct_get_currency_symbol() { |
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
$active_currency_id = erp_get_currency( true ); |
| 48 |
|
| 49 |
return $wpdb->get_var( |
| 50 |
$wpdb->prepare( |
| 51 |
"SELECT sign FROM {$wpdb->prefix}erp_acct_currency_info WHERE id = %d", |
| 52 |
absint( $active_currency_id ) |
| 53 |
) |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the price format depending on the currency position. |
| 59 |
* |
| 60 |
* Use it for JavaScript |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
function erp_acct_get_price_format() { |
| 65 |
$currency_pos = erp_get_option( 'erp_ac_currency_position', false, 'left' ); |
| 66 |
$format = '%s%v'; |
| 67 |
|
| 68 |
switch ( $currency_pos ) { |
| 69 |
case 'left': |
| 70 |
$format = '%s%v'; |
| 71 |
break; |
| 72 |
|
| 73 |
case 'right': |
| 74 |
$format = '%v%s'; |
| 75 |
break; |
| 76 |
|
| 77 |
case 'left_space': |
| 78 |
$format = '%s %v'; |
| 79 |
break; |
| 80 |
|
| 81 |
case 'right_space': |
| 82 |
$format = '%v %s'; |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
return apply_filters( 'erp_acct_price_format', $format, $currency_pos ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the price format depending on the currency position. |
| 91 |
* |
| 92 |
* Use it for PHP |
| 93 |
* |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
function erp_acct_get_price_format_php() { |
| 97 |
$currency_pos = erp_get_option( 'erp_ac_currency_position', false, 'left' ); |
| 98 |
$format = '%1$s%2$s'; |
| 99 |
|
| 100 |
switch ( $currency_pos ) { |
| 101 |
case 'left': |
| 102 |
$format = '%1$s%2$s'; |
| 103 |
break; |
| 104 |
|
| 105 |
case 'right': |
| 106 |
$format = '%2$s%1$s'; |
| 107 |
break; |
| 108 |
|
| 109 |
case 'left_space': |
| 110 |
$format = '%1$s %2$s'; |
| 111 |
break; |
| 112 |
|
| 113 |
case 'right_space': |
| 114 |
$format = '%2$s %1$s'; |
| 115 |
break; |
| 116 |
} |
| 117 |
|
| 118 |
return apply_filters( 'erp_acct_price_format_php', $format, $currency_pos ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Format the price with a currency symbol. |
| 123 |
* |
| 124 |
* @param float $price |
| 125 |
* @param array $args (default: array()) |
| 126 |
* |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
function erp_acct_get_price( $main_price, $args = [] ) { |
| 130 |
extract( |
| 131 |
apply_filters( |
| 132 |
'erp_acct_price_args', |
| 133 |
wp_parse_args( |
| 134 |
$args, |
| 135 |
[ |
| 136 |
'currency' => erp_get_currency(), |
| 137 |
'decimal_separator' => erp_get_option( 'erp_ac_de_separator', false, '.' ), |
| 138 |
'thousand_separator' => erp_get_option( 'erp_ac_th_separator', false, ',' ), |
| 139 |
'decimals' => absint( erp_get_option( 'erp_ac_nm_decimal', false, 2 ) ), |
| 140 |
'price_format' => erp_acct_get_price_format_php(), |
| 141 |
'symbol' => true, |
| 142 |
'currency_symbol' => erp_acct_get_currency_symbol(), |
| 143 |
] |
| 144 |
) |
| 145 |
) |
| 146 |
); |
| 147 |
|
| 148 |
$price = number_format( abs( $main_price ), $decimals, $decimal_separator, $thousand_separator ); |
| 149 |
$formatted_price = $symbol ? sprintf( $price_format, $currency_symbol, $price ) : $price; |
| 150 |
$formatted_price = ( $main_price < 0 ) ? '(' . $formatted_price . ')' : $formatted_price; |
| 151 |
|
| 152 |
return apply_filters( 'erp_acct_price', $formatted_price, $price, $args ); |
| 153 |
} |
| 154 |
|