From 5582160e734e22bf7706b4e090447a97b09fc2d2 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 7 Feb 2019 22:46:20 +0000 Subject: [PATCH 1/2] Add localizeCurrency filter to admin and inject currencyConfig data --- .../admin/subscriptions/subscriptions.js.coffee | 2 +- .../utils/filters/localize_currency.js.coffee | 14 ++++++++++++++ app/helpers/admin/injection_helper.rb | 4 ++++ .../admin/add_currency_config.html.haml.deface | 3 +++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/admin/utils/filters/localize_currency.js.coffee create mode 100644 app/overrides/spree/layouts/admin/add_currency_config.html.haml.deface diff --git a/app/assets/javascripts/admin/subscriptions/subscriptions.js.coffee b/app/assets/javascripts/admin/subscriptions/subscriptions.js.coffee index 9e2c3d5696..129f20b7aa 100644 --- a/app/assets/javascripts/admin/subscriptions/subscriptions.js.coffee +++ b/app/assets/javascripts/admin/subscriptions/subscriptions.js.coffee @@ -1 +1 @@ -angular.module("admin.subscriptions", ['ngResource','admin.indexUtils','admin.dropdown']) +angular.module("admin.subscriptions", ['ngResource','admin.indexUtils','admin.dropdown', 'admin.utils']) diff --git a/app/assets/javascripts/admin/utils/filters/localize_currency.js.coffee b/app/assets/javascripts/admin/utils/filters/localize_currency.js.coffee new file mode 100644 index 0000000000..25d97828b4 --- /dev/null +++ b/app/assets/javascripts/admin/utils/filters/localize_currency.js.coffee @@ -0,0 +1,14 @@ +angular.module("admin.utils").filter "localizeCurrency", (currencyConfig)-> + # Convert number to string currency using injected currency configuration. + (amount) -> + # Set country code (eg. "US"). + currency_code = if currencyConfig.display_currency then " " + currencyConfig.currency else "" + # Set decimal points, 2 or 0 if hide_cents. + decimals = if currencyConfig.hide_cents == "true" then 0 else 2 + # Set format if the currency symbol should come after the number, otherwise (default) use the locale setting. + format = if currencyConfig.symbol_position == "after" then "%n %u" else undefined + # We need to use parseFloat as the amount should come in as a string. + amount = parseFloat(amount) + + # Build the final price string. + I18n.toCurrency(amount, {precision: decimals, unit: currencyConfig.symbol, format: format}) + currency_code diff --git a/app/helpers/admin/injection_helper.rb b/app/helpers/admin/injection_helper.rb index 0e93a4affc..022a27c7a2 100644 --- a/app/helpers/admin/injection_helper.rb +++ b/app/helpers/admin/injection_helper.rb @@ -65,6 +65,10 @@ module Admin admin_inject_json_ams_array opts[:module], "columns", column_preferences, Api::Admin::ColumnPreferenceSerializer end + def admin_inject_currency_config + admin_inject_json_ams 'admin.utils', "currencyConfig", {}, Api::CurrencyConfigSerializer + end + def admin_inject_enterprise_permissions permissions = {can_manage_shipping_methods: can?(:manage_shipping_methods, @enterprise), diff --git a/app/overrides/spree/layouts/admin/add_currency_config.html.haml.deface b/app/overrides/spree/layouts/admin/add_currency_config.html.haml.deface new file mode 100644 index 0000000000..0ab8f96c55 --- /dev/null +++ b/app/overrides/spree/layouts/admin/add_currency_config.html.haml.deface @@ -0,0 +1,3 @@ +/ insert_before "div#wrapper" + += admin_inject_currency_config From f9ea93933ec7b3899cb755ea167beb8749e8fd82 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 7 Feb 2019 22:49:55 +0000 Subject: [PATCH 2/2] Use localizeCurrency filter in subscriptions --- app/views/admin/subscriptions/_orders_panel.html.haml | 2 +- app/views/admin/subscriptions/_review.html.haml | 8 ++++---- .../subscriptions/_subscription_line_items.html.haml | 10 +++++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/views/admin/subscriptions/_orders_panel.html.haml b/app/views/admin/subscriptions/_orders_panel.html.haml index 7314ee244d..d99ab196e8 100644 --- a/app/views/admin/subscriptions/_orders_panel.html.haml +++ b/app/views/admin/subscriptions/_orders_panel.html.haml @@ -19,7 +19,7 @@ %div{ ng: { bind: "::orderCycleCloses(proxyOrder.order_cycle_id)" } } %td.text-center %span.state{ ng: { class: "proxyOrder.state", bind: 'stateText(proxyOrder.state)' } } - %td.text-center{ ng: { bind: '(proxyOrder.total || subscription.estimatedTotal()) | currency' } } + %td.text-center{ ng: { bind: '(proxyOrder.total || subscription.estimatedTotal()) | localizeCurrency' } } %td.actions %a.edit-order.icon-edit.no-text{ href: '{{::proxyOrder.edit_path}}', target: '_blank', 'ofn-with-tip' => t(:edit_order), confirm_order_edit: true } %a.cancel-order.icon-remove.no-text{ href: 'javascript:void(0)', ng: { hide: "proxyOrder.state == 'canceled'", click: "cancelOrder(proxyOrder)" }, 'ofn-with-tip' => t(:cancel_order) } diff --git a/app/views/admin/subscriptions/_review.html.haml b/app/views/admin/subscriptions/_review.html.haml index 7e97bd02aa..4d06cb7859 100644 --- a/app/views/admin/subscriptions/_review.html.haml +++ b/app/views/admin/subscriptions/_review.html.haml @@ -75,9 +75,9 @@ .description {{ item.description }} .not-in-open-and-upcoming-order-cycles-warning{ ng: { if: '!item.in_open_and_upcoming_order_cycles' } } = t(".no_open_or_upcoming_order_cycle") - %td.price.align-center {{ item.price_estimate | currency }} + %td.price.align-center {{ item.price_estimate | localizeCurrency }} %td.quantity {{ item.quantity }} - %td.total.align-center {{ (item.price_estimate * item.quantity) | currency }} + %td.total.align-center {{ (item.price_estimate * item.quantity) | localizeCurrency }} %tbody#subtotal.no-border-top{"data-hook" => "admin_order_form_subtotal"} %tr#subtotal-row %td{:colspan => "3"} @@ -85,7 +85,7 @@ = t(:subtotal) \: %td.total.align-center - %span {{ subscription.estimatedSubtotal() | currency }} + %span {{ subscription.estimatedSubtotal() | localizeCurrency }} %tbody#order-total.grand-total.no-border-top{"data-hook" => "admin_order_form_total"} %tr %td{:colspan => "3"} @@ -93,6 +93,6 @@ = t(:order_total_price) \: %td.total.align-center - %span#order_form_total {{ subscription.estimatedTotal() | currency }} + %span#order_form_total {{ subscription.estimatedTotal() | localizeCurrency }} %p.notice = t "this_is_an_estimate", scope: 'admin.subscriptions.subscription_line_items' diff --git a/app/views/admin/subscriptions/_subscription_line_items.html.haml b/app/views/admin/subscriptions/_subscription_line_items.html.haml index b80b95a85b..fdb5f10fdf 100644 --- a/app/views/admin/subscriptions/_subscription_line_items.html.haml +++ b/app/views/admin/subscriptions/_subscription_line_items.html.haml @@ -19,10 +19,10 @@ .description {{ item.description }} .not-in-open-and-upcoming-order-cycles-warning{ ng: { if: '!item.in_open_and_upcoming_order_cycles' } } = t(".not_in_open_and_upcoming_order_cycles_warning") - %td.price.align-center {{ item.price_estimate | currency }} + %td.price.align-center {{ item.price_estimate | localizeCurrency }} %td.quantity %input{ name: 'quantity', type: 'number', min: 0, ng: { model: 'item.quantity' } } - %td.total.align-center {{ (item.price_estimate * item.quantity) | currency }} + %td.total.align-center {{ (item.price_estimate * item.quantity) | localizeCurrency }} %td.actions %a.delete-item.icon-trash.no-text{ ng: { click: 'removeSubscriptionLineItem(item)'}, :href => "javascript:void(0)" } %tbody#subtotal.no-border-top{"data-hook" => "admin_order_form_subtotal"} @@ -32,7 +32,7 @@ = t(:subtotal) \: %td.total.align-center - %span#order_subtotal {{ subscription.estimatedSubtotal() | currency }} + %span#order_subtotal {{ subscription.estimatedSubtotal() | localizeCurrency }} %td.actions %tbody#fees.no-border-top{ ng: { show: "subscription.estimatedFees() > 0" } } %tr#fees-row @@ -41,7 +41,7 @@ = t(:fees) \: %td.total.align-center - %span#order_fees {{ subscription.estimatedFees() | currency }} + %span#order_fees {{ subscription.estimatedFees() | localizeCurrency }} %td.actions %tbody#order-total.grand-total.no-border-top{"data-hook" => "admin_order_form_total"} %tr @@ -50,7 +50,7 @@ = t(:order_total_price) \: %td.total.align-center - %span#order_form_total {{ subscription.estimatedTotal() | currency }} + %span#order_form_total {{ subscription.estimatedTotal() | localizeCurrency }} %td.actions %p.notice = t ".this_is_an_estimate"