Make weight calculator compute 0 for variants with unit different from weight

This commit is contained in:
luisramos0
2019-06-18 13:29:17 +01:00
parent e8eeb3d5dc
commit 160b535e2f
2 changed files with 10 additions and 1 deletions

View File

@@ -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

View File

@@ -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