When creating new product via /admin/products/new use BigDecimal

to avoid bad rounded multiplication

example here:
700*0.001 = 0.7000000000000001

+ update specs as well
This commit is contained in:
Jean-Baptiste Bellet
2022-08-09 16:23:45 +02:00
parent 4452ad2c51
commit d53b2b94b9
2 changed files with 11 additions and 2 deletions

View File

@@ -32,7 +32,7 @@ angular.module("admin.products")
if match
$scope.product.master.unit_value = PriceParser.parse(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_value = window.bigDecimal.multiply($scope.product.master.unit_value, $scope.product.variant_unit_scale, 2) if $scope.product.master.unit_value && $scope.product.variant_unit_scale
$scope.product.master.unit_description = match[3]
else
value = $scope.product.master.unit_value

View File

@@ -17,7 +17,8 @@ describe "unitsCtrl", ->
inject ($rootScope, $controller, VariantUnitManager) ->
scope = $rootScope
ctrl = $controller 'unitsCtrl', {$scope: scope, VariantUnitManager: VariantUnitManager}
window.bigDecimal = jasmine.createSpyObj "bigDecimal", ["multiply"]
window.bigDecimal.multiply.and.callFake (a, b, c) -> a * b
describe "interpretting variant_unit_with_scale", ->
it "splits string with one underscore and stores the two parts", ->
@@ -93,3 +94,11 @@ describe "unitsCtrl", ->
scope.processUnitValueWithDescription()
expect(scope.product.master.unit_value).toEqual 22.22
expect(scope.product.master.unit_description).toEqual "things"
it "handles nice rounded division", ->
# this is a bit absurd, but it assure use that bigDecimal is called
window.bigDecimal.multiply.and.returnValue 0.7
scope.product.master.unit_value_with_description = "700"
scope.product.variant_unit_scale = 0.001
scope.processUnitValueWithDescription()
expect(scope.product.master.unit_value).toEqual 0.7