From 038296692998911be68e95cd2d9e1b71016e8e9b Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Thu, 31 Jan 2019 11:29:52 -0500 Subject: [PATCH 1/2] Update dollars filter and formatDollars() to display cent values --- atst/filters.py | 9 --------- js/lib/dollars.js | 7 ++++--- templates/task_orders/new/review.html | 10 +++++----- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/atst/filters.py b/atst/filters.py index 6eeb4229..61c42f8c 100644 --- a/atst/filters.py +++ b/atst/filters.py @@ -12,14 +12,6 @@ def iconSvg(name): def dollars(value): - try: - numberValue = float(value) - except ValueError: - numberValue = 0 - return "${:,.0f}".format(numberValue) - - -def dollarsWithCents(value): try: numberValue = float(value) except ValueError: @@ -107,7 +99,6 @@ def normalizeOrder(title): def register_filters(app): app.jinja_env.filters["iconSvg"] = iconSvg app.jinja_env.filters["dollars"] = dollars - app.jinja_env.filters["dollarsWithCents"] = dollarsWithCents app.jinja_env.filters["usPhone"] = usPhone app.jinja_env.filters["readableInteger"] = readableInteger app.jinja_env.filters["getOptionLabel"] = getOptionLabel diff --git a/js/lib/dollars.js b/js/lib/dollars.js index a65e5767..1f959e38 100644 --- a/js/lib/dollars.js +++ b/js/lib/dollars.js @@ -1,8 +1,9 @@ export const formatDollars = (value, cents = true) => { if (typeof value === 'number') { - return cents - ? `$${value.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,')}` - : `$${value.toFixed(0).replace(/\d(?=(\d{3})+(?!\d))/g, '$&,')}` + return value.toLocaleString('us-US', { + style: 'currency', + currency: 'USD', + }) } return '' } diff --git a/templates/task_orders/new/review.html b/templates/task_orders/new/review.html index bfd90827..cc4c3081 100644 --- a/templates/task_orders/new/review.html +++ b/templates/task_orders/new/review.html @@ -132,7 +132,7 @@

{{ "task_orders.new.review.to_value"| translate }}

{% if task_order.budget %} - {{ task_order.budget | dollarsWithCents }} + {{ task_order.budget | dollars }} {% endif %} @@ -140,7 +140,7 @@

{{ "task_orders.new.review.clin_1"| translate }}

{% if task_order.clin_01 %} - {{ task_order.clin_01 | dollarsWithCents }} + {{ task_order.clin_01 | dollars }} {% else %} {{ RequiredLabel() }} {% endif %} @@ -155,7 +155,7 @@ {% if task_order.clin_02 and config.CLASSIFIED %} - {{ task_order.clin_02 | dollarsWithCents or RequiredLabel() }} + {{ task_order.clin_02 | dollars or RequiredLabel() }} {% endif %} @@ -163,7 +163,7 @@

{{ "task_orders.new.review.clin_3"| translate }}

{% if task_order.clin_03 %} - {{ task_order.clin_03 | dollarsWithCents or RequiredLabel() }} + {{ task_order.clin_03 | dollars or RequiredLabel() }} {% else %} {{ RequiredLabel() }} {% endif %} @@ -178,7 +178,7 @@ {% if task_order.clin_04 and config.CLASSIFIED %} - {{ task_order.clin_04 | dollarsWithCents or RequiredLabel() }} + {{ task_order.clin_04 | dollars or RequiredLabel() }} {% endif %} From 63bcae0bd462b3f8e8ce0c3f767c17a2909f5f64 Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Fri, 1 Feb 2019 10:25:34 -0500 Subject: [PATCH 2/2] Add trailing zeros to cents when focus moves --- js/components/text_input.js | 4 ++++ js/lib/dollars.js | 5 +++++ tests/test_filters.py | 10 +++++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/js/components/text_input.js b/js/components/text_input.js index e5f21e4c..002884a2 100644 --- a/js/components/text_input.js +++ b/js/components/text_input.js @@ -1,5 +1,6 @@ import MaskedInput, { conformToMask } from 'vue-text-mask' import inputValidations from '../lib/input_validations' +import { formatDollars } from '../lib/dollars' export default { name: 'textinput', @@ -78,6 +79,9 @@ export default { onChange: function(e) { // Only invalidate the field when it blurs this._checkIfValid({ value: e.target.value, invalidate: true }) + if (this.validation === 'dollars') { + this.value = formatDollars(this._rawValue(e.target.value)) + } }, // diff --git a/js/lib/dollars.js b/js/lib/dollars.js index 1f959e38..5ef65828 100644 --- a/js/lib/dollars.js +++ b/js/lib/dollars.js @@ -4,6 +4,11 @@ export const formatDollars = (value, cents = true) => { style: 'currency', currency: 'USD', }) + } else if (typeof value === 'string') { + return parseFloat(value).toLocaleString('us-US', { + style: 'currency', + currency: 'USD', + }) } return '' } diff --git a/tests/test_filters.py b/tests/test_filters.py index 5cfff00e..5c091faa 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -7,11 +7,11 @@ from atst.models import AuditEvent @pytest.mark.parametrize( "input,expected", [ - ("0", "$0"), - ("123.00", "$123"), - ("1234567", "$1,234,567"), - ("-1234", "$-1,234"), - ("one", "$0"), + ("0", "$0.00"), + ("123.00", "$123.00"), + ("1234567", "$1,234,567.00"), + ("-1234", "$-1,234.00"), + ("one", "$0.00"), ], ) def test_dollar_fomatter(input, expected):