activities_helper.php
1 year ago
agent_helper.php
1 year ago
auth_helper.php
9 months ago
blocks_helper.php
1 year ago
booking_helper.php
1 year ago
bricks_helper.php
1 year ago
bundles_helper.php
1 year ago
calendar_helper.php
9 months ago
carts_helper.php
1 year ago
connector_helper.php
1 year ago
csv_helper.php
9 months ago
customer_helper.php
9 months ago
customer_import_helper.php
9 months ago
database_helper.php
9 months ago
debug_helper.php
1 year ago
defaults_helper.php
1 year ago
elementor_helper.php
1 year ago
email_helper.php
9 months ago
encrypt_helper.php
1 year ago
events_helper.php
1 year ago
form_helper.php
9 months ago
icalendar_helper.php
1 year ago
image_helper.php
1 year ago
invoices_helper.php
9 months ago
license_helper.php
1 year ago
location_helper.php
1 year ago
marketing_systems_helper.php
1 year ago
meeting_systems_helper.php
1 year ago
menu_helper.php
9 months ago
meta_helper.php
1 year ago
migrations_helper.php
1 year ago
money_helper.php
1 year ago
notifications_helper.php
9 months ago
order_intent_helper.php
9 months ago
orders_helper.php
1 year ago
otp_helper.php
9 months ago
pages_helper.php
1 year ago
params_helper.php
1 year ago
payments_helper.php
1 year ago
price_breakdown_helper.php
1 year ago
process_jobs_helper.php
1 year ago
processes_helper.php
1 year ago
replacer_helper.php
1 year ago
resource_helper.php
1 year ago
roles_helper.php
9 months ago
router_helper.php
1 year ago
service_helper.php
1 year ago
sessions_helper.php
1 year ago
settings_helper.php
9 months ago
short_links_systems_helper.php
9 months ago
shortcodes_helper.php
9 months ago
sms_helper.php
1 year ago
steps_helper.php
9 months ago
stripe_connect_helper.php
9 months ago
styles_helper.php
9 months ago
support_topics_helper.php
1 year ago
time_helper.php
9 months ago
timeline_helper.php
9 months ago
transaction_helper.php
1 year ago
transaction_intent_helper.php
1 year ago
util_helper.php
9 months ago
version_specific_updates_helper.php
9 months ago
whatsapp_helper.php
1 year ago
work_periods_helper.php
1 year ago
wp_datetime.php
1 year ago
wp_user_helper.php
1 year ago
money_helper.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | class OsMoneyHelper { |
| 4 | |
| 5 | |
| 6 | /** |
| 7 | * @param $amount |
| 8 | * @param $include_currency |
| 9 | * @param $hide_zero_decimals |
| 10 | * @return string |
| 11 | * |
| 12 | * Formats amount from database format (99999.0000) to requested format, optionally can include currency symbol and strip zero cents |
| 13 | * |
| 14 | */ |
| 15 | public static function format_price($amount, $include_currency = true, $hide_zero_decimals = true): string{ |
| 16 | $decimal_separator = OsSettingsHelper::get_settings_value('decimal_separator', '.'); |
| 17 | $thousand_separator = OsSettingsHelper::get_settings_value('thousand_separator', ','); |
| 18 | $decimals = OsSettingsHelper::get_settings_value('number_of_decimals', '2'); |
| 19 | if(empty($amount)) $amount = 0; |
| 20 | $amount = number_format($amount, $decimals, $decimal_separator, $thousand_separator); |
| 21 | if($hide_zero_decimals){ |
| 22 | $zeros = ''; |
| 23 | switch($decimals){ |
| 24 | case '1': $zeros = '0'; break; |
| 25 | case '2': $zeros = '00'; break; |
| 26 | case '3': $zeros = '000'; break; |
| 27 | case '4': $zeros = '0000'; break; |
| 28 | } |
| 29 | $amount = str_replace($decimal_separator.$zeros, '', $amount); |
| 30 | } |
| 31 | if($include_currency) $amount = implode('', array(OsSettingsHelper::get_settings_value('currency_symbol_before'), $amount, OsSettingsHelper::get_settings_value('currency_symbol_after'))); |
| 32 | $amount = apply_filters('latepoint_format_price', $amount, $include_currency, $hide_zero_decimals); |
| 33 | return $amount; |
| 34 | } |
| 35 | |
| 36 | // formats amount to be used in input money fields |
| 37 | public static function to_money_field_format($amount){ |
| 38 | return self::format_price((float)$amount, false, false); |
| 39 | } |
| 40 | |
| 41 | // amount stripped from any formatting like currency symbol, thousand separator, just numbers and decimal separator is left |
| 42 | public static function convert_amount_from_money_input_to_db_format($amount){ |
| 43 | $decimal_separator = OsSettingsHelper::get_settings_value('decimal_separator', '.'); |
| 44 | $amount = preg_replace('/[^-\\d'.$decimal_separator.']+/', '', $amount); |
| 45 | // database is using dot as a decimal separator, if latepoint is not using dot for currency input - convert it to dot to store in db |
| 46 | if($decimal_separator != '.') $amount = str_replace($decimal_separator, '.', $amount); |
| 47 | $amount = self::pad_to_db_format($amount); |
| 48 | return $amount; |
| 49 | } |
| 50 | |
| 51 | public static function convert_value_from_percent_input_to_db_format($value){ |
| 52 | $decimal_separator = OsSettingsHelper::get_settings_value('decimal_separator', '.'); |
| 53 | $value = preg_replace('/[^-\\d'.$decimal_separator.']+/', '', $value); |
| 54 | // database is using dot as a decimal separator, if latepoint is not using dot for input - convert it to dot to store in db |
| 55 | if($decimal_separator != '.') $value = str_replace($decimal_separator, '.', $value); |
| 56 | $value = self::pad_to_db_format($value); |
| 57 | return $value; |
| 58 | } |
| 59 | |
| 60 | public static function pad_to_db_format($amount) : string{ |
| 61 | return number_format((float)$amount, 4, '.', ''); |
| 62 | } |
| 63 | |
| 64 | } |