mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
WIP: Making final_weight_volume adjustment a bit nicer
This commit is contained in:
@@ -77,6 +77,10 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [
|
||||
line_item.checked = false
|
||||
line_item.supplier = $scope.matchObject $scope.suppliers, line_item.supplier, null
|
||||
line_item.order = orderWithoutLineItems
|
||||
line_item.original_final_weight_volume = line_item.final_weight_volume
|
||||
line_item.original_quantity = line_item.quantity
|
||||
line_item.original_price = line_item.price
|
||||
|
||||
lineItems.concat order.line_items
|
||||
, []
|
||||
|
||||
@@ -164,13 +168,11 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [
|
||||
$scope.orderCycleFilter = $scope.orderCycles[0].id
|
||||
$scope.quickSearch = ""
|
||||
|
||||
$scope.weightAdjustedPrice = (lineItem, oldValue) ->
|
||||
if oldValue <= 0
|
||||
oldValue = lineItem.units_variant.unit_value * line_item.quantity
|
||||
if lineItem.final_weight_volume <= 0
|
||||
lineItem.final_weight_volume = lineItem.units_variant.unit_value * lineItem.quantity
|
||||
lineItem.price = lineItem.price * lineItem.final_weight_volume / oldValue
|
||||
#$scope.bulk_order_form.line_item.price.$setViewValue($scope.bulk_order_form.line_item.price.$viewValue)
|
||||
$scope.weightAdjustedPrice = (lineItem) ->
|
||||
if lineItem.final_weight_volume > 0
|
||||
unit_value = lineItem.final_weight_volume / lineItem.quantity
|
||||
original_unit_value = lineItem.original_final_weight_volume / lineItem.original_quantity
|
||||
lineItem.price = lineItem.original_price * (unit_value / original_unit_value)
|
||||
|
||||
$scope.unitValueLessThanZero = (lineItem) ->
|
||||
if lineItem.units_variant.unit_value <= 0
|
||||
@@ -178,12 +180,10 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [
|
||||
else
|
||||
false
|
||||
|
||||
$scope.updateOnQuantity = (lineItem, oldQuantity) ->
|
||||
if lineItem.quantity <= 0
|
||||
lineItem.quantity = 1
|
||||
# reset price to original unit value
|
||||
lineItem.price = lineItem.price * (oldQuantity * lineItem.units_variant.unit_value) / lineItem.final_weight_volume
|
||||
lineItem.final_weight_volume = lineItem.units_variant.unit_value * lineItem.quantity
|
||||
$scope.updateOnQuantity = (lineItem) ->
|
||||
if lineItem.quantity > 0
|
||||
lineItem.final_weight_volume = lineItem.original_final_weight_volume * lineItem.quantity / lineItem.original_quantity
|
||||
$scope.weightAdjustedPrice(lineItem)
|
||||
|
||||
$scope.$watch "orderCycleFilter", (newVal, oldVal) ->
|
||||
unless $scope.orderCycleFilter == "0" || angular.equals(newVal, oldVal)
|
||||
|
||||
@@ -146,10 +146,10 @@
|
||||
%td.variant{ 'ng-show' => 'columns.variant.visible' }
|
||||
%a{ :href => '#', 'ng-click' => "setSelectedUnitsVariant(line_item.units_product,line_item.units_variant)" } {{ line_item.units_variant.full_name }}
|
||||
%td.quantity{ 'ng-show' => 'columns.quantity.visible' }
|
||||
%input{ :type => 'number', :name => 'quantity', :id => 'quantity', :value => 'line_item.quantity', 'ng-model' => "line_item.quantity", 'ng-change' => "updateOnQuantity(line_item, {{ line_item.quantity }})", 'obj-for-update' => "line_item", "attr-for-update" => "quantity" }
|
||||
%input{ :type => 'number', :name => 'quantity', :id => 'quantity', 'ng-model' => "line_item.quantity", 'ng-change' => "updateOnQuantity(line_item)", 'obj-for-update' => "line_item", "attr-for-update" => "quantity" }
|
||||
%td.max{ 'ng-show' => 'columns.max.visible' } {{ line_item.max_quantity }}
|
||||
%td.final_weight_volume{ 'ng-show' => 'columns.final_weight_volume.visible' }
|
||||
%input{ :type => 'text', :name => 'final_weight_volume', :id => 'final_weight_volume', :value => 'line_item.final_weight_volume', 'ng-model' => "line_item.final_weight_volume", 'ng-readonly' => "unitValueLessThanZero(line_item)", 'ng-change' => "weightAdjustedPrice(line_item, {{ line_item.final_weight_volume }})", 'obj-for-update' => "line_item", "attr-for-update" => "final_weight_volume" }
|
||||
%input{ :type => 'text', :name => 'final_weight_volume', :id => 'final_weight_volume', 'ng-model' => "line_item.final_weight_volume", 'ng-readonly' => "unitValueLessThanZero(line_item)", 'ng-change' => "weightAdjustedPrice(line_item)", 'obj-for-update' => "line_item", "attr-for-update" => "final_weight_volume" }
|
||||
%td.price{ 'ng-show' => 'columns.price.visible' }
|
||||
%input{ :type => 'text', :name => 'price', :id => 'price', :value => '{{ line_item.price * line_item.quantity | currency }}', 'ng-readonly' => "true", 'obj-for-update' => "line_item", "attr-for-update" => "price" }
|
||||
%td.actions
|
||||
|
||||
@@ -351,49 +351,53 @@ describe "AdminOrderMgmtCtrl", ->
|
||||
expect(scope.formattedValueWithUnitName(2000,unitsVariant)).toEqual "2 kg"
|
||||
|
||||
describe "updating the price upon updating the weight of a line item", ->
|
||||
|
||||
it "resets the weight if the weight is set to zero", ->
|
||||
scope.filteredLineItems = [
|
||||
{ units_variant: { unit_value: 100 }, price: 2, quantity: 1, final_weight_volume: 0 }
|
||||
]
|
||||
expect(scope.weightAdjustedPrice(scope.filteredLineItems[0], 100)).toEqual scope.filteredLineItems[0].price
|
||||
|
||||
it "updates the price if the weight is changed", ->
|
||||
scope.filteredLineItems = [
|
||||
{ units_variant: { unit_value: 100 }, price: 2, final_weight_volume: 200 }
|
||||
{ original_price: 2.00, price: 2.00, original_quantity: 1, quantity: 1, original_final_weight_volume: 2000, final_weight_volume: 4000 }
|
||||
]
|
||||
old_value = scope.filteredLineItems[0].units_variant.unit_value
|
||||
new_value = scope.filteredLineItems[0].final_weight_volume
|
||||
sp = scope.filteredLineItems[0].price * new_value / old_value
|
||||
expect(scope.weightAdjustedPrice(scope.filteredLineItems[0], old_value)).toEqual sp
|
||||
scope.weightAdjustedPrice(scope.filteredLineItems[0])
|
||||
expect(scope.filteredLineItems[0].price).toEqual 4.00
|
||||
|
||||
it "updates the weight if the quantity is changed", ->
|
||||
it "doesn't update the price if the weight <= 0", ->
|
||||
scope.filteredLineItems = [
|
||||
{ units_variant: { unit_value: 150 }, price: 1, final_weight_volume: 100, quantity: 2 }
|
||||
{ original_price: 2.00, price: 2.00, original_quantity: 1, quantity: 1, original_final_weight_volume: 2000, final_weight_volume: 0 }
|
||||
]
|
||||
old_value = 1
|
||||
nw = scope.filteredLineItems[0].units_variant.unit_value * scope.filteredLineItems[0].quantity
|
||||
scope.updateOnQuantity(scope.filteredLineItems[0], old_value)
|
||||
expect(scope.filteredLineItems[0].final_weight_volume).toEqual nw
|
||||
scope.weightAdjustedPrice(scope.filteredLineItems[0])
|
||||
expect(scope.filteredLineItems[0].price).toEqual 2.00
|
||||
|
||||
it "updates the price if the quantity is changed", ->
|
||||
it "doesn't update the price if the weight is an empty string", ->
|
||||
scope.filteredLineItems = [
|
||||
{ units_variant: { unit_value: 150 }, price: 21, final_weight_volume: 100, quantity: 2 }
|
||||
{ original_price: 2.00, price: 2.00, original_quantity: 1, quantity: 1, original_final_weight_volume: 2000, final_weight_volume: "" }
|
||||
]
|
||||
old_value = 1
|
||||
np = scope.filteredLineItems[0].price * (old_value * scope.filteredLineItems[0].units_variant.unit_value) / scope.filteredLineItems[0].final_weight_volume
|
||||
scope.updateOnQuantity(scope.filteredLineItems[0], old_value)
|
||||
expect(scope.filteredLineItems[0].price).toEqual np
|
||||
scope.weightAdjustedPrice(scope.filteredLineItems[0])
|
||||
expect(scope.filteredLineItems[0].price).toEqual 2.00
|
||||
|
||||
describe "updating final_weight_volume upon updating the quantity for a line_item", ->
|
||||
beforeEach ->
|
||||
spyOn(scope, "weightAdjustedPrice")
|
||||
|
||||
it "doesn't update the price if the weight is not changed", ->
|
||||
it "updates the weight if the quantity is changed, then calls weightAdjustedPrice()", ->
|
||||
scope.filteredLineItems = [
|
||||
{ units_variant: { unit_value: 100 }, price: 2, final_weight_volume: 100 }
|
||||
{ original_price: 2.00, price: 2.00, original_quantity: 1, quantity: 2, original_final_weight_volume: 2000, final_weight_volume: 0 }
|
||||
]
|
||||
old_value = scope.filteredLineItems[0].final_weight_volume
|
||||
new_value = scope.filteredLineItems[0].final_weight_volume
|
||||
sp = scope.filteredLineItems[0].price
|
||||
expect(scope.weightAdjustedPrice(scope.filteredLineItems[0], old_value)).toEqual sp
|
||||
scope.updateOnQuantity(scope.filteredLineItems[0])
|
||||
expect(scope.filteredLineItems[0].final_weight_volume).toEqual 4000
|
||||
expect(scope.weightAdjustedPrice).toHaveBeenCalled()
|
||||
|
||||
it "doesn't update the weight if the quantity <= 0", ->
|
||||
scope.filteredLineItems = [
|
||||
{ original_price: 2.00, price: 2.00, original_quantity: 1, quantity: 0, original_final_weight_volume: 2000, final_weight_volume: 1000 }
|
||||
]
|
||||
scope.updateOnQuantity(scope.filteredLineItems[0])
|
||||
expect(scope.filteredLineItems[0].final_weight_volume).toEqual 1000
|
||||
|
||||
it "doesn't update the weight if the quantity is an empty string", ->
|
||||
scope.filteredLineItems = [
|
||||
{ original_price: 2.00, price: 2.00, original_quantity: 1, quantity: "", original_final_weight_volume: 2000, final_weight_volume: 1000 }
|
||||
]
|
||||
scope.updateOnQuantity(scope.filteredLineItems[0])
|
||||
expect(scope.filteredLineItems[0].final_weight_volume).toEqual 1000
|
||||
|
||||
|
||||
describe "Auxiliary functions", ->
|
||||
describe "getting a zero filled two digit number", ->
|
||||
|
||||
Reference in New Issue
Block a user