mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Both sumUnitValues and sumMaxUnitValues should handle imperial units
This commit is contained in:
@@ -128,14 +128,20 @@ angular.module("admin.lineItems").controller 'LineItemsCtrl', ($scope, $timeout,
|
||||
$scope.selectedUnitsProduct = unitsProduct
|
||||
$scope.selectedUnitsVariant = unitsVariant
|
||||
|
||||
$scope.getScale = (lineItem) ->
|
||||
if lineItem.units_product && lineItem.units_variant && (lineItem.units_product.variant_unit == "weight" || lineItem.units_product.variant_unit == "volume")
|
||||
VariantUnitManager.getScale(lineItem.units_variant.unit_value, lineItem.units_product.variant_unit)
|
||||
else
|
||||
1
|
||||
|
||||
$scope.sumUnitValues = ->
|
||||
sum = $scope.filteredLineItems?.reduce (sum,lineItem) ->
|
||||
sum + lineItem.final_weight_volume
|
||||
sum = $scope.filteredLineItems?.reduce (sum, lineItem) ->
|
||||
sum + $scope.roundToThreeDecimals(lineItem.final_weight_volume / $scope.getScale(lineItem))
|
||||
, 0
|
||||
|
||||
$scope.sumMaxUnitValues = ->
|
||||
sum = $scope.filteredLineItems?.reduce (sum,lineItem) ->
|
||||
sum + lineItem.max_quantity * lineItem.units_variant.unit_value
|
||||
sum + lineItem.max_quantity * $scope.roundToThreeDecimals(lineItem.units_variant.unit_value / $scope.getScale(lineItem))
|
||||
, 0
|
||||
|
||||
$scope.roundToThreeDecimals = (value) ->
|
||||
|
||||
@@ -213,6 +213,15 @@ describe "LineItemsCtrl", ->
|
||||
]
|
||||
expect(scope.sumUnitValues()).toEqual 30
|
||||
|
||||
it "returns the sum of the final_weight_volumes for line_items with both metric and imperial units", ->
|
||||
scope.filteredLineItems = [
|
||||
{ final_weight_volume: 907.2, units_product: { variant_unit: "weight" }, units_variant: { unit_value: 453.6 } }
|
||||
{ final_weight_volume: 2000, units_product: { variant_unit: "weight" }, units_variant: { unit_value: 1000 } }
|
||||
{ final_weight_volume: 56.7, units_product: { variant_unit: "weight" }, units_variant: { unit_value: 28.35 } }
|
||||
{ final_weight_volume: 2, units_product: { variant_unit: "volume" }, units_variant: { unit_value: 1.0 } }
|
||||
]
|
||||
expect(scope.sumUnitValues()).toEqual 8
|
||||
|
||||
describe "sumMaxUnitValues()", ->
|
||||
it "returns the sum of the product of unit_value and maxOf(max_quantity, pristine quantity) for specified line_items", ->
|
||||
scope.filteredLineItems = [
|
||||
|
||||
Reference in New Issue
Block a user