Make weight calculator use line_item.final_weight_volume rather than variant.weight for cases where the final weight is set manually in the BOM

This commit is contained in:
luisramos0
2019-05-31 15:06:29 +01:00
parent 187fbd788b
commit 82955b9fe5
2 changed files with 21 additions and 6 deletions

View File

@@ -13,7 +13,13 @@ module Calculator
def compute(object)
line_items = line_items_for object
total_weight = line_items.sum { |li| ((li.variant.andand.weight || 0) * li.quantity) }
total_weight = line_items.sum do |line_item|
if line_item.final_weight_volume.present?
line_item.final_weight_volume / 1000
else
((line_item.variant.andand.weight || 0) * line_item.quantity)
end
end
total_weight * preferred_per_kg
end
end

View File

@@ -18,13 +18,22 @@ describe Calculator::Weight do
expect(subject.compute(order)).to eq((10 * 1 + 20 * 3) * 10)
end
it "computes shipping cost for a line item" do
variant = build(:variant, weight: 10)
describe "line item with variant weight" do
let(:variant) { build(:variant, weight: 10) }
let(:line_item) { build(:line_item, variant: variant, quantity: 2) }
line_item = build(:line_item, variant: variant, quantity: 2)
before { subject.set_preference(:per_kg, 10) }
subject.set_preference(:per_kg, 10)
expect(subject.compute(line_item)).to eq(10 * 2 * 10)
it "computes shipping cost for a line item" do
expect(subject.compute(line_item)).to eq(10 * 2 * 10)
end
describe "and with final_weight_volume defined" do
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
end
end
it "computes shipping cost for an object with an order" do