diff --git a/spec/lib/reports/orders_and_fulfillment/orders_cycle_supplier_totals_report_spec.rb b/spec/lib/reports/orders_and_fulfillment/orders_cycle_supplier_totals_report_spec.rb index 25b164fd6a..15e2eb6da0 100644 --- a/spec/lib/reports/orders_and_fulfillment/orders_cycle_supplier_totals_report_spec.rb +++ b/spec/lib/reports/orders_and_fulfillment/orders_cycle_supplier_totals_report_spec.rb @@ -29,6 +29,81 @@ describe Reporting::Reports::OrdersAndFulfillment::OrderCycleSupplierTotals do expect(report_table.length).to eq(1) end + describe "total_units column" do + let(:item) { order.line_items.first } + let(:variant) { item.variant } + + it "contains a sum of total items" do + variant.product.update!(variant_unit: "items", variant_unit_name: "bottle") + variant.update!(unit_value: 6) # six-pack + item.update!(final_weight_volume: nil) # reset unit information + item.update!(quantity: 3) + + expect(table_headers[4]).to eq "Total Units" + expect(report_table[0][4]).to eq 18 # = 3 * 6, three six-packs + end + + it "contains a sum of total weight" do + variant.product.update!(variant_unit: "weight") + variant.update!(unit_value: 200) # grams + item.update!(final_weight_volume: nil) # reset unit information + item.update!(quantity: 3) + + expect(table_headers[4]).to eq "Total Units" + expect(report_table[0][4]).to eq 0.6 # kg (= 3 * 0.2kg) + end + + it "is blank when line items miss a unit" do + # This is not possible with the current code but was possible years ago. + # So I'm using `update_columns` to save invalid data. + # We still have lots of that data in our databases though. + variant.product.update(variant_unit: "items", variant_unit_name: "container") + variant.update_columns(unit_value: nil, unit_description: "vacuum") + item.update!(final_weight_volume: nil) # reset unit information + + expect(table_headers[4]).to eq "Total Units" + expect(report_table[0][4]).to eq " " + end + + it "is summarised" do + expect(report).to receive(:display_summary_row?).and_return(true) + + variant.product.update!(variant_unit: "weight") + variant.update!(unit_value: 200) # grams + item.update!(final_weight_volume: nil) # reset unit information + item.update!(quantity: 3) + + # And a second item to add up with: + item2 = create(:line_item, order: order) + + expect(table_headers[4]).to eq "Total Units" + expect(report_table[0][4]).to eq 0.6 # kg (= 3 * 0.2kg) + expect(report_table[1][4]).to eq 0.001 # 1 gram default value + expect(report_table[2][4]).to eq 0.601 # summary + end + + pending "is blank in summary when one line item misses a unit and another not" do + expect(report).to receive(:display_summary_row?).and_return(true) + + # This is not possible with the current code but was possible years ago. + # So I'm using `update_columns` to save invalid data. + # We still have lots of that data in our databases though. + variant.product.update(variant_unit: "items", variant_unit_name: "container") + variant.update_columns(unit_value: nil, unit_description: "vacuum") + item.update!(final_weight_volume: nil) # reset unit information + + # This second line item will have a default a bigint value. + order.line_items << create(:line_item) + + # Generating the report used to raise: + # > TypeError: no implicit conversion of BigDecimal into String + expect(table_headers[4]).to eq "Total Units" + expect(report_table[0][4]).to eq " " + expect(report_table[1][4]).to eq 0.001 # 1 gram default value + expect(report_table[2][4]).to eq " " # summary + end + end + context "with a VAT/GST-free supplier" do before(:each) do supplier.update(charges_sales_tax: false)