activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
2 months ago
auth_helper.php
2 months ago
blocks_helper.php
3 months ago
booking_helper.php
2 months ago
bricks_helper.php
3 months ago
bundles_helper.php
3 months ago
calendar_helper.php
3 months ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
1 month ago
customer_import_helper.php
3 months ago
database_helper.php
3 months ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
2 months ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
2 months ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
2 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
2 months ago
plugin_version_update_helper.php
2 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
replacer_helper.php
3 months ago
resource_helper.php
3 months ago
roles_helper.php
3 months ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
2 months ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
2 months ago
sms_helper.php
3 months ago
steps_helper.php
2 months ago
stripe_connect_helper.php
2 months ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
2 months ago
timeline_helper.php
2 months ago
transaction_helper.php
3 months ago
transaction_intent_helper.php
3 months ago
util_helper.php
3 months ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
3 months ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
money_helper.php
82 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 ) ) { |
| 20 | $amount = 0; |
| 21 | } |
| 22 | $amount = number_format( $amount, $decimals, $decimal_separator, $thousand_separator ); |
| 23 | if ( $hide_zero_decimals ) { |
| 24 | $zeros = ''; |
| 25 | switch ( $decimals ) { |
| 26 | case '1': |
| 27 | $zeros = '0'; |
| 28 | break; |
| 29 | case '2': |
| 30 | $zeros = '00'; |
| 31 | break; |
| 32 | case '3': |
| 33 | $zeros = '000'; |
| 34 | break; |
| 35 | case '4': |
| 36 | $zeros = '0000'; |
| 37 | break; |
| 38 | } |
| 39 | $amount = str_replace( $decimal_separator . $zeros, '', $amount ); |
| 40 | } |
| 41 | if ( $include_currency ) { |
| 42 | $amount = implode( '', array( OsSettingsHelper::get_settings_value( 'currency_symbol_before' ), $amount, OsSettingsHelper::get_settings_value( 'currency_symbol_after' ) ) ); |
| 43 | } |
| 44 | $amount = apply_filters( 'latepoint_format_price', $amount, $include_currency, $hide_zero_decimals ); |
| 45 | return $amount; |
| 46 | } |
| 47 | |
| 48 | // formats amount to be used in input money fields |
| 49 | public static function to_money_field_format( $amount ) { |
| 50 | return self::format_price( (float) $amount, false, false ); |
| 51 | } |
| 52 | |
| 53 | // amount stripped from any formatting like currency symbol, thousand separator, just numbers and decimal separator is left |
| 54 | public static function convert_amount_from_money_input_to_db_format( $amount ) { |
| 55 | $decimal_separator = OsSettingsHelper::get_settings_value( 'decimal_separator', '.' ); |
| 56 | // Ensure non-negative amount to prevent financial manipulation |
| 57 | // For non-negative ^\\d, For negative ^-\\d |
| 58 | $amount = preg_replace( '/[^-\\d' . $decimal_separator . ']+/', '', $amount ); |
| 59 | // 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 |
| 60 | if ( $decimal_separator != '.' ) { |
| 61 | $amount = str_replace( $decimal_separator, '.', $amount ); |
| 62 | } |
| 63 | $amount = self::pad_to_db_format( $amount ); |
| 64 | return $amount; |
| 65 | } |
| 66 | |
| 67 | public static function convert_value_from_percent_input_to_db_format( $value ) { |
| 68 | $decimal_separator = OsSettingsHelper::get_settings_value( 'decimal_separator', '.' ); |
| 69 | $value = preg_replace( '/[^-\\d' . $decimal_separator . ']+/', '', $value ); |
| 70 | // database is using dot as a decimal separator, if latepoint is not using dot for input - convert it to dot to store in db |
| 71 | if ( $decimal_separator != '.' ) { |
| 72 | $value = str_replace( $decimal_separator, '.', $value ); |
| 73 | } |
| 74 | $value = self::pad_to_db_format( $value ); |
| 75 | return $value; |
| 76 | } |
| 77 | |
| 78 | public static function pad_to_db_format( $amount ): string { |
| 79 | return number_format( (float) $amount, 4, '.', '' ); |
| 80 | } |
| 81 | } |
| 82 |