mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Make weight calculator compute 0 for variants with unit different from weight
This commit is contained in:
@@ -16,6 +16,8 @@ module Calculator
|
||||
total_weight(line_items) * preferred_per_kg
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def total_weight(line_items)
|
||||
line_items.sum do |line_item|
|
||||
line_item_weight(line_item)
|
||||
@@ -23,6 +25,7 @@ module Calculator
|
||||
end
|
||||
|
||||
def line_item_weight(line_item)
|
||||
return 0 if line_item.variant.product.andand.variant_unit != 'weight'
|
||||
if line_item.final_weight_volume.present?
|
||||
# Divided by 1000 because grams is the base weight unit and the calculator price is per_kg
|
||||
line_item.final_weight_volume / 1000
|
||||
|
||||
@@ -29,10 +29,16 @@ describe Calculator::Weight do
|
||||
end
|
||||
|
||||
describe "and with final_weight_volume defined" do
|
||||
before { line_item.update_attribute :final_weight_volume, '18000' }
|
||||
|
||||
it "computes fee using final_weight_volume, not the variant weight" do
|
||||
line_item.final_weight_volume = "18000"
|
||||
expect(subject.compute(line_item)).to eq(10 * 18)
|
||||
end
|
||||
|
||||
it "returns zero for variant where unit type is not weight" do
|
||||
line_item.variant.product.update_attribute :variant_unit, 'items'
|
||||
expect(subject.compute(line_item)).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user