mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
add UnitPrices service
This commit is contained in:
committed by
Jean-Baptiste Bellet
parent
b494cd2f8b
commit
7abb3868fc
@@ -0,0 +1,25 @@
|
||||
angular.module("admin.products").factory "UnitPrices", (VariantUnitManager) ->
|
||||
class UnitPrices
|
||||
@price: (price, scale, unit_type, unit_value) ->
|
||||
price / @denominator(unit_value, variant_unit)
|
||||
|
||||
@denominator: (scale, unit_type, unit_value) ->
|
||||
unit = @unit(scale, unit_type)
|
||||
if unit == "lb"
|
||||
unit_value / 453.6
|
||||
else if unit == "kg"
|
||||
unit_value / 1000
|
||||
else
|
||||
unit_value
|
||||
|
||||
@unit: (scale, unit_type, variant_unit_name = '') ->
|
||||
if VariantUnitManager.systemOfMeasurement(scale, unit_type) == "imperial"
|
||||
"lb"
|
||||
else if unit_type == "weight"
|
||||
"kg"
|
||||
else if unit_type == "volume"
|
||||
"L"
|
||||
else if variant_unit_name.length > 0
|
||||
variant_unit_name
|
||||
else
|
||||
"item"
|
||||
@@ -67,3 +67,9 @@ angular.module("admin.products").factory "VariantUnitManager", (availableUnits)
|
||||
scaleSystem = @units[unitType][scale]['system']
|
||||
(parseFloat(scale) for scale, scaleInfo of @units[unitType] when scaleInfo['system'] == scaleSystem).sort (a, b) ->
|
||||
a - b
|
||||
|
||||
@systemOfMeasurement: (scale, unitType) ->
|
||||
if @units[unitType][scale]
|
||||
@units[unitType][scale]['system']
|
||||
else
|
||||
'custom'
|
||||
|
||||
Reference in New Issue
Block a user