Handle comma as decimal separator in the unit value field

- Add comma as a decimal separator in the regexp
 - Do not use parseFloat but our `unlocalizeCurrencyFilter` which is more tolerant
This commit is contained in:
Jean-Baptiste Bellet
2021-06-08 09:51:30 +02:00
parent 0cb2739139
commit d3c2158121
2 changed files with 21 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
angular.module("admin.products")
.controller "unitsCtrl", ($scope, VariantUnitManager, OptionValueNamer, UnitPrices) ->
.controller "unitsCtrl", ($scope, VariantUnitManager, OptionValueNamer, UnitPrices, unlocalizeCurrencyFilter) ->
$scope.product = { master: {} }
$scope.product.master.product = $scope.product
$scope.placeholder_text = ""
@@ -26,9 +26,9 @@ angular.module("admin.products")
$scope.processUnitValueWithDescription = ->
if $scope.product.master.hasOwnProperty("unit_value_with_description")
match = $scope.product.master.unit_value_with_description.match(/^([\d\.]+(?= *|$)|)( *)(.*)$/)
match = $scope.product.master.unit_value_with_description.match(/^([\d\.,]+(?= *|$)|)( *)(.*)$/)
if match
$scope.product.master.unit_value = parseFloat(match[1])
$scope.product.master.unit_value = unlocalizeCurrencyFilter(match[1])
$scope.product.master.unit_value = null if isNaN($scope.product.master.unit_value)
$scope.product.master.unit_value *= $scope.product.variant_unit_scale if $scope.product.master.unit_value && $scope.product.variant_unit_scale
$scope.product.master.unit_description = match[3]

View File

@@ -75,3 +75,21 @@ describe "unitsCtrl", ->
scope.processUnitValueWithDescription()
expect(scope.product.master.unit_value).toEqual 123
expect(scope.product.master.unit_description).toEqual "54 boxes"
it "handle final point as decimal separator", ->
scope.product.master.unit_value_with_description = "22.22"
scope.processUnitValueWithDescription()
expect(scope.product.master.unit_value).toEqual 22.22
expect(scope.product.master.unit_description).toEqual ""
it "handle comma as decimal separator", ->
scope.product.master.unit_value_with_description = "22,22"
scope.processUnitValueWithDescription()
expect(scope.product.master.unit_value).toEqual 22.22
expect(scope.product.master.unit_description).toEqual ""
it "handle comma as decimal separator with description", ->
scope.product.master.unit_value_with_description = "22,22 things"
scope.processUnitValueWithDescription()
expect(scope.product.master.unit_value).toEqual 22.22
expect(scope.product.master.unit_description).toEqual "things"