From 1724e1d62619c22e57d1a5a7ab0f1df9297391b4 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 24 Oct 2023 11:35:57 +0100 Subject: [PATCH 1/7] fix invoice feature flag enabling --- spec/system/admin/invoice_print_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index d5d66774cf..e8d9ccfba8 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -462,7 +462,7 @@ describe ' end context "when invoice feature is enabled" do before do - Flipper.enable(:invoice) + Flipper.enable(:invoices) end it_behaves_like "contains right Payment Description at Checkout information" it_behaves_like "Check display on each invoice: legacy and alternative", false From 5be8ec0c5a271748ebf14dbd6dbe72593f28710b Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 24 Oct 2023 11:53:02 +0100 Subject: [PATCH 2/7] set completed_at on the order used for testing --- spec/system/admin/invoice_print_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index e8d9ccfba8..9615fd7419 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -22,6 +22,7 @@ describe ' let(:order) do create(:order_with_totals_and_distribution, user:, distributor:, + completed_at: 1.day.ago, order_cycle:, state: 'complete', payment_state: 'balance_due') end From 4eedf74ecd6af73b6620592b703601eff91d9158 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 24 Oct 2023 11:59:31 +0100 Subject: [PATCH 3/7] remove unecessary instructions from "contains right Payment Description at Checkout information" 1.When the user tries to print an invoice for an order (that doesn't have any previous invoices), an invoice will be generated automatically. 2.When the user tries to print an invoice for an order without specifing the id of the invoice, he'll get the latest invoice --- spec/system/admin/invoice_print_spec.rb | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index 9615fd7419..04290767d3 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -37,13 +37,7 @@ describe ' end shared_examples "contains right Payment Description at Checkout information" do - let(:url_params) { - if OpenFoodNetwork::FeatureToggle.enabled?(:invoices) - { invoice_id: order.invoices.first.id } - else - {} - end - } + let(:url_params) {{}} let!(:payment_method1) do create(:stripe_sca_payment_method, distributors: [distributor], description: "description1") @@ -54,7 +48,6 @@ describe ' context "with no payment" do it "do not display the payment description information" do - order.invoices.create! login_as_admin visit spree.print_admin_order_path(order, params: url_params) convert_pdf_to_page @@ -68,7 +61,6 @@ describe ' end before do order.save! - order.invoices.create! end it "display the payment description section" do @@ -90,7 +82,6 @@ describe ' payment_method: payment_method2, created_at: 2.days.ago) order.save! - order.invoices.create! end it "display the payment description section and use the one from the completed payment" do @@ -112,7 +103,6 @@ describe ' payment_method: payment_method2, created_at: 1.day.ago) order.save! - order.invoices.create! end it "display the payment description section and use the one from the last payment" do From 13bfbc21e001fc05450a2525e35e9a8d2d93dc27 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 24 Oct 2023 12:09:30 +0100 Subject: [PATCH 4/7] remove unecessary instructions from "Check display on each invoice: legacy and alternative" --- spec/system/admin/invoice_print_spec.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index 04290767d3..07419e0728 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -120,16 +120,9 @@ describe ' user: create(:user, email: "xxxxxx@example.com"), bill_address: create(:address, phone: '1234567890')) end - let(:url_params) { - if OpenFoodNetwork::FeatureToggle.enabled?(:invoices) - { invoice_id: completed_order.invoices.first.id } - else - {} - end - } + let(:url_params) {{}} before do - completed_order.invoices.create! allow(Spree::Config).to receive(:invoice_style2?).and_return(alternative_invoice) login_as_admin visit spree.print_admin_order_path(completed_order, params: url_params) From c759a0f6469b2050b6a05384e1c3b052b90ca0e7 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 26 Oct 2023 10:43:59 +0100 Subject: [PATCH 5/7] move tests under shared_examples "order with tax" to context "when invoice feature is not enabled" --- spec/system/admin/invoice_print_spec.rb | 605 ++++++++++++------------ 1 file changed, 301 insertions(+), 304 deletions(-) diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index 07419e0728..5a14266960 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -37,7 +37,7 @@ describe ' end shared_examples "contains right Payment Description at Checkout information" do - let(:url_params) {{}} + let(:url_params) { {} } let!(:payment_method1) do create(:stripe_sca_payment_method, distributors: [distributor], description: "description1") @@ -120,7 +120,7 @@ describe ' user: create(:user, email: "xxxxxx@example.com"), bill_address: create(:address, phone: '1234567890')) end - let(:url_params) {{}} + let(:url_params) { {} } before do allow(Spree::Config).to receive(:invoice_style2?).and_return(alternative_invoice) @@ -135,306 +135,6 @@ describe ' end end - shared_examples "order with tax" do - let(:user1) { create(:user, enterprises: [distributor]) } - let!(:zone) { create(:zone_with_member) } - let(:address) { create(:address) } - - context "included" do - let(:shipping_tax_rate_included) { - create(:tax_rate, amount: 0.1, included_in_price: true, zone:) - } - let(:enterprise_fee_rate_included) { - create(:tax_rate, amount: 0.15, included_in_price: true, zone:) - } - let(:shipping_tax_category) { - create(:tax_category, tax_rates: [shipping_tax_rate_included]) - } - let(:fee_tax_category) { - create(:tax_category, tax_rates: [enterprise_fee_rate_included]) - } - let!(:shipping_method) { - create(:shipping_method_with, :expensive_name, distributors: [distributor], - tax_category: shipping_tax_category) - } - let!(:enterprise_fee) { - create(:enterprise_fee, enterprise: user1.enterprises.first, - tax_category: fee_tax_category, - calculator: Calculator::FlatRate.new(preferred_amount: 120.0)) - } - let!(:order_cycle) { - create(:simple_order_cycle, - coordinator: distributor, - coordinator_fees: [enterprise_fee], - distributors: [distributor], - variants: [product1.variants.first, product2.variants.first]) - } - - let!(:order1) { - create(:order, order_cycle:, distributor: user1.enterprises.first, - ship_address: address, bill_address: address) - } - let!(:product1) { - create(:taxed_product, zone:, price: 12.54, tax_rate_amount: 0, - included_in_price: true) - } - let!(:product2) { - create(:taxed_product, zone:, price: 500.15, tax_rate_amount: 0.2, - included_in_price: true) - } - - let!(:line_item1) { - create(:line_item, variant: product1.variants.first, price: 12.54, quantity: 1, - order: order1) - } - let!(:line_item2) { - create(:line_item, variant: product2.variants.first, price: 500.15, quantity: 3, - order: order1) - } - - let(:url_params) { - if OpenFoodNetwork::FeatureToggle.enabled?(:invoices) - { invoice_id: order1.invoices.first.id } - else - {} - end - } - - before do - order1.reload - while !order1.delivery? - break if !order1.next! - end - order1.select_shipping_method(shipping_method.id) - order1.recreate_all_fees! - while !order1.payment? - break if !order1.next! - end - - create(:payment, state: "checkout", order: order1, amount: order1.reload.total, - payment_method: create(:payment_method, distributors: [distributor])) - while !order1.complete? - break if !order1.next! - end - order1.invoices.create! - end - - context "legacy invoice" do - before do - allow(Spree::Config).to receive(:invoice_style2?).and_return(false) - login_as_admin - visit spree.print_admin_order_path(order1, params: url_params) - convert_pdf_to_page - end - - it "displays $0.00 when a line item has no tax" do - expect(page).to have_content Spree::Product.first.name - expect(page).to have_content "(1g)" - expect(page).to have_content "1 $0.00 $12.54" - end - - it "displays the taxes correctly" do - # header - expect(page).to have_content "Item Qty GST Price" - # second line item, included tax - expect(page).to have_content Spree::Product.second.name.to_s - expect(page).to have_content "(1g)" # display as - expect(page).to have_content "3 $250.08 $1,500.45" - # Enterprise fee - expect(page).to have_content "Whole order - #{ - enterprise_fee.name - } fee by coordinator " \ - "#{ - user1.enterprises.first.name - } 1 $15.65 (included) $120.00" - # Shipping - expect(page).to have_content "Shipping 1 $9.14 (included) $100.55" - # Order Totals - expect(page).to have_content "GST Total: $274.87" - expect(page).to have_content "Total (Excl. tax): $1,458.67" - expect(page).to have_content "Total (Incl. tax): $1,733.54" - end - end - - context "alternative invoice" do - before do - allow(Spree::Config).to receive(:invoice_style2?).and_return(true) - login_as_admin - visit spree.print_admin_order_path(order1, params: url_params) - convert_pdf_to_page - end - - it "displays the taxes correctly" do - # header - expect(page).to have_content "Item Qty" - expect(page).to have_content "Unit price (Incl. tax)" - expect(page).to have_content "Total price (Incl. tax)" - expect(page).to have_content "Tax rate" - # first line item, no tax - expect(page).to have_content Spree::Product.first.name.to_s - expect(page).to have_content "(1g)" # display as - expect(page).to have_content "1 $12.54 $12.54 0.0%" - # second line item, included tax - expect(page).to have_content Spree::Product.second.name.to_s - expect(page).to have_content "(1g)" # display as - expect(page).to have_content "3 $500.15 $1,500.45 20.0%" - # Enterprise fee - expect(page).to have_content "#{enterprise_fee.name} fee by coordinator " \ - "#{user1.enterprises.first.name} $120.00" - # Shipping - expect(page).to have_content "Shipping $100.55 10.0%" - # Tax totals - expect(page).to have_content "Total tax (10.0%): $9.14 " \ - "Total tax (15.0%): $15.65 Total tax (20.0%): $250.08" - # Order Totals - expect(page).to have_content "Total (Incl. tax): $1,733.54" - expect(page).to have_content "Total (Excl. tax): $1,458.67" - end - end - end - - context "added" do - let(:shipping_tax_rate_added) { - create(:tax_rate, amount: 0.10, included_in_price: false, zone:) - } - let(:enterprise_fee_rate_added) { - create(:tax_rate, amount: 0.15, included_in_price: false, zone:) - } - let(:shipping_tax_category) { - create(:tax_category, tax_rates: [shipping_tax_rate_added]) - } - let(:fee_tax_category) { create(:tax_category, tax_rates: [enterprise_fee_rate_added]) } - let!(:shipping_method) { - create(:shipping_method_with, :expensive_name, distributors: [distributor], - tax_category: shipping_tax_category) - } - let(:enterprise_fee) { - create(:enterprise_fee, enterprise: user1.enterprises.first, - tax_category: fee_tax_category, - calculator: Calculator::FlatRate.new(preferred_amount: 120.0)) - } - let(:order_cycle2) { - create(:simple_order_cycle, coordinator: distributor, - coordinator_fees: [enterprise_fee], - distributors: [distributor], - variants: [product3.variants.first, product4.variants.first]) - } - - let(:order2) { - create(:order, order_cycle: order_cycle2, distributor: user1.enterprises.first, - ship_address: address, bill_address: address) - } - let(:product3) { - create(:taxed_product, zone:, price: 12.54, tax_rate_amount: 0, - included_in_price: false) - } - let(:product4) { - create(:taxed_product, zone:, price: 500.15, tax_rate_amount: 0.2, - included_in_price: false) - } - - let!(:line_item3) { - create(:line_item, variant: product3.variants.first, price: 12.54, quantity: 1, - order: order2) - } - let!(:line_item4) { - create(:line_item, variant: product4.variants.first, price: 500.15, quantity: 3, - order: order2) - } - - before do - order2.reload - while !order2.delivery? - break if !order2.next! - end - order2.select_shipping_method(shipping_method.id) - order2.recreate_all_fees! - while !order2.payment? - break if !order2.next! - end - - create(:payment, state: "checkout", order: order2, amount: order2.reload.total, - payment_method: create(:payment_method, distributors: [distributor])) - while !order2.complete? - break if !order2.next! - end - end - - context "legacy invoice" do - before do - allow(Spree::Config).to receive(:invoice_style2?).and_return(false) - login_as_admin - visit spree.print_admin_order_path(order2) - convert_pdf_to_page - end - it "displays $0.0 when a line item has no tax" do - expect(page).to have_content Spree::Product.first.name - expect(page).to have_content "(1g)" - expect(page).to have_content "1 $0.00 $12.54" - end - - it "displays the added tax on the GST colum" do - expect(page).to have_content Spree::Product.second.name.to_s - expect(page).to have_content "(1g)" # display as - expect(page).to have_content "3 $300.09 $1,500.45" - end - - it "displays the taxes correctly" do - # header - expect(page).to have_content "Item Qty GST Price" - # Enterprise fee - expect(page).to have_content "Whole order - #{ - enterprise_fee.name - } fee by coordinator " \ - "#{ - user1.enterprises.first.name - } 1 $18.00 $120.00" - # Shipping - expect(page).to have_content "Shipping 1 $10.06 $100.55" - # Order Totals - expect(page).to have_content "GST Total: $328.15" - expect(page).to have_content "Total (Excl. tax): $1,733.54" - expect(page).to have_content "Total (Incl. tax): $2,061.69" - end - end - - context "alternative invoice" do - before do - allow(Spree::Config).to receive(:invoice_style2?).and_return(true) - login_as_admin - visit spree.print_admin_order_path(order2) - convert_pdf_to_page - end - it "displays the taxes correctly" do - # header - expect(page).to have_content "Item Qty" - expect(page).to have_content "Unit price (Incl. tax)" - expect(page).to have_content "Total price (Incl. tax)" - expect(page).to have_content "Tax rate" - # first line item, no tax - expect(page).to have_content Spree::Product.first.name.to_s - expect(page).to have_content "(1g)" # display as - expect(page).to have_content "1 $12.54 $12.54 0.0%" - # second line item, included tax - expect(page).to have_content Spree::Product.second.name.to_s - expect(page).to have_content "(1g)" # display as - expect(page).to have_content "3 $500.15 $1,500.45 20.0%" - # Enterprise fee - expect(page).to have_content "#{enterprise_fee.name} fee by coordinator " \ - "#{user1.enterprises.first.name} $120.00" - # Shipping - expect(page).to have_content "Shipping $100.55 10.0%" - # Tax totals - expect(page).to have_content "Total tax (10.0%): $10.06 " \ - "Total tax (15.0%): $18.00 Total tax (20.0%): $300.09" - # Order Totals - expect(page).to have_content "Total (Incl. tax): $2,061.69" - expect(page).to have_content "Total (Excl. tax): $1,733.54" - end - end - end - end - context "when invoice feature is not enabled" do before do Flipper.disable(:invoices) @@ -442,7 +142,305 @@ describe ' it_behaves_like "contains right Payment Description at Checkout information" it_behaves_like "Check display on each invoice: legacy and alternative", false it_behaves_like "Check display on each invoice: legacy and alternative", true - it_behaves_like "order with tax" + describe "order with taxes" do + let(:user1) { create(:user, enterprises: [distributor]) } + let!(:zone) { create(:zone_with_member) } + let(:address) { create(:address) } + + context "included" do + let(:shipping_tax_rate_included) { + create(:tax_rate, amount: 0.1, included_in_price: true, zone:) + } + let(:enterprise_fee_rate_included) { + create(:tax_rate, amount: 0.15, included_in_price: true, zone:) + } + let(:shipping_tax_category) { + create(:tax_category, tax_rates: [shipping_tax_rate_included]) + } + let(:fee_tax_category) { + create(:tax_category, tax_rates: [enterprise_fee_rate_included]) + } + let!(:shipping_method) { + create(:shipping_method_with, :expensive_name, distributors: [distributor], + tax_category: shipping_tax_category) + } + let!(:enterprise_fee) { + create(:enterprise_fee, enterprise: user1.enterprises.first, + tax_category: fee_tax_category, + calculator: Calculator::FlatRate.new(preferred_amount: 120.0)) + } + let!(:order_cycle) { + create(:simple_order_cycle, + coordinator: distributor, + coordinator_fees: [enterprise_fee], + distributors: [distributor], + variants: [product1.variants.first, product2.variants.first]) + } + + let!(:order1) { + create(:order, order_cycle:, distributor: user1.enterprises.first, + ship_address: address, bill_address: address) + } + let!(:product1) { + create(:taxed_product, zone:, price: 12.54, tax_rate_amount: 0, + included_in_price: true) + } + let!(:product2) { + create(:taxed_product, zone:, price: 500.15, tax_rate_amount: 0.2, + included_in_price: true) + } + + let!(:line_item1) { + create(:line_item, variant: product1.variants.first, price: 12.54, quantity: 1, + order: order1) + } + let!(:line_item2) { + create(:line_item, variant: product2.variants.first, price: 500.15, quantity: 3, + order: order1) + } + + let(:url_params) { + if OpenFoodNetwork::FeatureToggle.enabled?(:invoices) + { invoice_id: order1.invoices.first.id } + else + {} + end + } + + before do + order1.reload + while !order1.delivery? + break if !order1.next! + end + order1.select_shipping_method(shipping_method.id) + order1.recreate_all_fees! + while !order1.payment? + break if !order1.next! + end + + create(:payment, state: "checkout", order: order1, amount: order1.reload.total, + payment_method: create(:payment_method, distributors: [distributor])) + while !order1.complete? + break if !order1.next! + end + order1.invoices.create! + end + + context "legacy invoice" do + before do + allow(Spree::Config).to receive(:invoice_style2?).and_return(false) + login_as_admin + visit spree.print_admin_order_path(order1, params: url_params) + convert_pdf_to_page + end + + it "displays $0.00 when a line item has no tax" do + expect(page).to have_content Spree::Product.first.name + expect(page).to have_content "(1g)" + expect(page).to have_content "1 $0.00 $12.54" + end + + it "displays the taxes correctly" do + # header + expect(page).to have_content "Item Qty GST Price" + # second line item, included tax + expect(page).to have_content Spree::Product.second.name.to_s + expect(page).to have_content "(1g)" # display as + expect(page).to have_content "3 $250.08 $1,500.45" + # Enterprise fee + expect(page).to have_content "Whole order - #{ + enterprise_fee.name + } fee by coordinator " \ + "#{ + user1.enterprises.first.name + } 1 $15.65 (included) $120.00" + # Shipping + expect(page).to have_content "Shipping 1 $9.14 (included) $100.55" + # Order Totals + expect(page).to have_content "GST Total: $274.87" + expect(page).to have_content "Total (Excl. tax): $1,458.67" + expect(page).to have_content "Total (Incl. tax): $1,733.54" + end + end + + context "alternative invoice" do + before do + allow(Spree::Config).to receive(:invoice_style2?).and_return(true) + login_as_admin + visit spree.print_admin_order_path(order1, params: url_params) + convert_pdf_to_page + end + + it "displays the taxes correctly" do + # header + expect(page).to have_content "Item Qty" + expect(page).to have_content "Unit price (Incl. tax)" + expect(page).to have_content "Total price (Incl. tax)" + expect(page).to have_content "Tax rate" + # first line item, no tax + expect(page).to have_content Spree::Product.first.name.to_s + expect(page).to have_content "(1g)" # display as + expect(page).to have_content "1 $12.54 $12.54 0.0%" + # second line item, included tax + expect(page).to have_content Spree::Product.second.name.to_s + expect(page).to have_content "(1g)" # display as + expect(page).to have_content "3 $500.15 $1,500.45 20.0%" + # Enterprise fee + expect(page).to have_content "#{enterprise_fee.name} fee by coordinator " \ + "#{user1.enterprises.first.name} $120.00" + # Shipping + expect(page).to have_content "Shipping $100.55 10.0%" + # Tax totals + expect(page).to have_content "Total tax (10.0%): $9.14 " \ + "Total tax (15.0%): $15.65 Total tax (20.0%): $250.08" + # Order Totals + expect(page).to have_content "Total (Incl. tax): $1,733.54" + expect(page).to have_content "Total (Excl. tax): $1,458.67" + end + end + end + + context "added" do + let(:shipping_tax_rate_added) { + create(:tax_rate, amount: 0.10, included_in_price: false, zone:) + } + let(:enterprise_fee_rate_added) { + create(:tax_rate, amount: 0.15, included_in_price: false, zone:) + } + let(:shipping_tax_category) { + create(:tax_category, tax_rates: [shipping_tax_rate_added]) + } + let(:fee_tax_category) { create(:tax_category, tax_rates: [enterprise_fee_rate_added]) } + let!(:shipping_method) { + create(:shipping_method_with, :expensive_name, distributors: [distributor], + tax_category: shipping_tax_category) + } + let(:enterprise_fee) { + create(:enterprise_fee, enterprise: user1.enterprises.first, + tax_category: fee_tax_category, + calculator: Calculator::FlatRate.new(preferred_amount: 120.0)) + } + let(:order_cycle2) { + create(:simple_order_cycle, coordinator: distributor, + coordinator_fees: [enterprise_fee], + distributors: [distributor], + variants: [product3.variants.first, product4.variants.first]) + } + + let(:order2) { + create(:order, order_cycle: order_cycle2, distributor: user1.enterprises.first, + ship_address: address, bill_address: address) + } + let(:product3) { + create(:taxed_product, zone:, price: 12.54, tax_rate_amount: 0, + included_in_price: false) + } + let(:product4) { + create(:taxed_product, zone:, price: 500.15, tax_rate_amount: 0.2, + included_in_price: false) + } + + let!(:line_item3) { + create(:line_item, variant: product3.variants.first, price: 12.54, quantity: 1, + order: order2) + } + let!(:line_item4) { + create(:line_item, variant: product4.variants.first, price: 500.15, quantity: 3, + order: order2) + } + + before do + order2.reload + while !order2.delivery? + break if !order2.next! + end + order2.select_shipping_method(shipping_method.id) + order2.recreate_all_fees! + while !order2.payment? + break if !order2.next! + end + + create(:payment, state: "checkout", order: order2, amount: order2.reload.total, + payment_method: create(:payment_method, distributors: [distributor])) + while !order2.complete? + break if !order2.next! + end + end + + context "legacy invoice" do + before do + allow(Spree::Config).to receive(:invoice_style2?).and_return(false) + login_as_admin + visit spree.print_admin_order_path(order2) + convert_pdf_to_page + end + it "displays $0.0 when a line item has no tax" do + expect(page).to have_content Spree::Product.first.name + expect(page).to have_content "(1g)" + expect(page).to have_content "1 $0.00 $12.54" + end + + it "displays the added tax on the GST colum" do + expect(page).to have_content Spree::Product.second.name.to_s + expect(page).to have_content "(1g)" # display as + expect(page).to have_content "3 $300.09 $1,500.45" + end + + it "displays the taxes correctly" do + # header + expect(page).to have_content "Item Qty GST Price" + # Enterprise fee + expect(page).to have_content "Whole order - #{ + enterprise_fee.name + } fee by coordinator " \ + "#{ + user1.enterprises.first.name + } 1 $18.00 $120.00" + # Shipping + expect(page).to have_content "Shipping 1 $10.06 $100.55" + # Order Totals + expect(page).to have_content "GST Total: $328.15" + expect(page).to have_content "Total (Excl. tax): $1,733.54" + expect(page).to have_content "Total (Incl. tax): $2,061.69" + end + end + + context "alternative invoice" do + before do + allow(Spree::Config).to receive(:invoice_style2?).and_return(true) + login_as_admin + visit spree.print_admin_order_path(order2) + convert_pdf_to_page + end + it "displays the taxes correctly" do + # header + expect(page).to have_content "Item Qty" + expect(page).to have_content "Unit price (Incl. tax)" + expect(page).to have_content "Total price (Incl. tax)" + expect(page).to have_content "Tax rate" + # first line item, no tax + expect(page).to have_content Spree::Product.first.name.to_s + expect(page).to have_content "(1g)" # display as + expect(page).to have_content "1 $12.54 $12.54 0.0%" + # second line item, included tax + expect(page).to have_content Spree::Product.second.name.to_s + expect(page).to have_content "(1g)" # display as + expect(page).to have_content "3 $500.15 $1,500.45 20.0%" + # Enterprise fee + expect(page).to have_content "#{enterprise_fee.name} fee by coordinator " \ + "#{user1.enterprises.first.name} $120.00" + # Shipping + expect(page).to have_content "Shipping $100.55 10.0%" + # Tax totals + expect(page).to have_content "Total tax (10.0%): $10.06 " \ + "Total tax (15.0%): $18.00 Total tax (20.0%): $300.09" + # Order Totals + expect(page).to have_content "Total (Incl. tax): $2,061.69" + expect(page).to have_content "Total (Excl. tax): $1,733.54" + end + end + end + end end context "when invoice feature is enabled" do before do @@ -451,7 +449,6 @@ describe ' it_behaves_like "contains right Payment Description at Checkout information" it_behaves_like "Check display on each invoice: legacy and alternative", false it_behaves_like "Check display on each invoice: legacy and alternative", true - it_behaves_like "order with tax" end end From e37f9e54fdb41a481dc47b941a994e85e7aa57e1 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 26 Oct 2023 11:19:03 +0100 Subject: [PATCH 6/7] reimplement "order with taxes" tests under "invoices feature enabled" --- spec/system/admin/invoice_print_spec.rb | 215 ++++++++++++++++++++++++ 1 file changed, 215 insertions(+) diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index 5a14266960..504996e4bb 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -449,6 +449,221 @@ describe ' it_behaves_like "contains right Payment Description at Checkout information" it_behaves_like "Check display on each invoice: legacy and alternative", false it_behaves_like "Check display on each invoice: legacy and alternative", true + describe "order with taxes" do + let(:user1) { create(:user, enterprises: [distributor]) } + let!(:zone) { create(:zone_with_member) } + let(:address) { create(:address) } + + context "included" do + let(:shipping_tax_rate_included) { + create(:tax_rate, amount: 0.1, included_in_price: true, zone:) + } + let(:enterprise_fee_rate_included) { + create(:tax_rate, amount: 0.15, included_in_price: true, zone:) + } + let(:shipping_tax_category) { + create(:tax_category, tax_rates: [shipping_tax_rate_included]) + } + let(:fee_tax_category) { + create(:tax_category, tax_rates: [enterprise_fee_rate_included]) + } + let!(:shipping_method_name) { "SM1" } + let!(:shipping_method) { + create(:shipping_method_with, :expensive_name, name: shipping_method_name, distributors: [distributor], + tax_category: shipping_tax_category) + } + let!(:enterprise_fee) { + create(:enterprise_fee, enterprise: user1.enterprises.first, + tax_category: fee_tax_category, + calculator: Calculator::FlatRate.new(preferred_amount: 120.0)) + } + let!(:order_cycle) { + create(:simple_order_cycle, + coordinator: distributor, + coordinator_fees: [enterprise_fee], + distributors: [distributor], + variants: [product1.variants.first, product2.variants.first]) + } + + let!(:order1) { + create(:order, order_cycle:, distributor: user1.enterprises.first, + ship_address: address, bill_address: address) + } + let!(:product1) { + create(:taxed_product, zone:, price: 12.54, tax_rate_amount: 0, + included_in_price: true) + } + let!(:product2) { + create(:taxed_product, zone:, price: 500.15, tax_rate_amount: 0.2, + included_in_price: true) + } + + let!(:line_item1) { + create(:line_item, variant: product1.variants.first, price: 12.54, quantity: 1, + order: order1) + } + let!(:line_item2) { + create(:line_item, variant: product2.variants.first, price: 500.15, quantity: 3, + order: order1) + } + + let(:url_params) { {} } + + before do + order1.reload + while !order1.delivery? + break if !order1.next! + end + order1.select_shipping_method(shipping_method.id) + order1.recreate_all_fees! + while !order1.payment? + break if !order1.next! + end + + create(:payment, state: "checkout", order: order1, amount: order1.reload.total, + payment_method: create(:payment_method, distributors: [distributor])) + while !order1.complete? + break if !order1.next! + end + order1.invoices.create! + login_as_admin + visit spree.print_admin_order_path(order1, params: url_params) + convert_pdf_to_page + end + + it "displays the taxes correctly" do + # header + expect(page).to have_content "Item Qty" + expect(page).to have_content "Weight / VOL." + expect(page).to have_content "Price Per unit (Excl. tax)" + expect(page).to have_content "Total price (Excl. tax)" + expect(page).to have_content "Tax rate" + expect(page).to have_content "Total price (Incl. tax)" + # first line item, no tax + expect(page).to have_content Spree::Product.first.name.to_s + expect(page).to have_content "($12,540.00 / kg)" # unit price + expect(page).to have_content "1 1g $12.54 $12.54 0.0% $12.54" + # # second line item, included tax + expect(page).to have_content Spree::Product.second.name.to_s + expect(page).to have_content "($500,150.00 / kg)" # unit price + expect(page).to have_content "3 1g $416.79 $1,250.37 20.0% $1,500.45" + # Enterprise fee + expect(page).to have_content "#{enterprise_fee.name} fee by $104.35 15.0% $120.00" + expect(page).to have_content "coordinator #{user1.enterprises.first.name}" + # Shipping + expect(page).to have_content "Shipping ( Type: $91.41 10.0% $100.55" + expect(page).to have_content "#{shipping_method_name} )" + # Tax totals + expect(page).to have_content "Total tax (10.0%): $9.14 " \ + "Total tax (15.0%): $15.65 Total tax (20.0%): $250.08" + # Order Totals + expect(page).to have_content "Total (Incl. tax): $1,733.54" + expect(page).to have_content "Total (Excl. tax): $1,458.67" + end + end + + context "added" do + let(:shipping_tax_rate_added) { + create(:tax_rate, amount: 0.10, included_in_price: false, zone:) + } + let(:enterprise_fee_rate_added) { + create(:tax_rate, amount: 0.15, included_in_price: false, zone:) + } + let(:shipping_tax_category) { + create(:tax_category, tax_rates: [shipping_tax_rate_added]) + } + let(:fee_tax_category) { create(:tax_category, tax_rates: [enterprise_fee_rate_added]) } + let!(:shipping_method_name) { "SM2" } + let!(:shipping_method) { + create(:shipping_method_with, :expensive_name, name: shipping_method_name, distributors: [distributor], + tax_category: shipping_tax_category) + } + let(:enterprise_fee) { + create(:enterprise_fee, enterprise: user1.enterprises.first, + tax_category: fee_tax_category, + calculator: Calculator::FlatRate.new(preferred_amount: 120.0)) + } + let(:order_cycle2) { + create(:simple_order_cycle, coordinator: distributor, + coordinator_fees: [enterprise_fee], + distributors: [distributor], + variants: [product3.variants.first, product4.variants.first]) + } + + let(:order2) { + create(:order, order_cycle: order_cycle2, distributor: user1.enterprises.first, + ship_address: address, bill_address: address) + } + let(:product3) { + create(:taxed_product, zone:, price: 12.54, tax_rate_amount: 0, + included_in_price: false) + } + let(:product4) { + create(:taxed_product, zone:, price: 500.15, tax_rate_amount: 0.2, + included_in_price: false) + } + + let!(:line_item3) { + create(:line_item, variant: product3.variants.first, price: 12.54, quantity: 1, + order: order2) + } + let!(:line_item4) { + create(:line_item, variant: product4.variants.first, price: 500.15, quantity: 3, + order: order2) + } + + before do + order2.reload + while !order2.delivery? + break if !order2.next! + end + order2.select_shipping_method(shipping_method.id) + order2.recreate_all_fees! + while !order2.payment? + break if !order2.next! + end + + create(:payment, state: "checkout", order: order2, amount: order2.reload.total, + payment_method: create(:payment_method, distributors: [distributor])) + while !order2.complete? + break if !order2.next! + end + login_as_admin + visit spree.print_admin_order_path(order2) + convert_pdf_to_page + end + + it "displays the taxes correctly" do + # header + expect(page).to have_content "Item Qty" + expect(page).to have_content "Weight / VOL." + expect(page).to have_content "Price Per unit (Excl. tax)" + expect(page).to have_content "Total price (Excl. tax)" + expect(page).to have_content "Tax rate" + expect(page).to have_content "Total price (Incl. tax)" + # first line item, no tax + expect(page).to have_content Spree::Product.first.name.to_s + expect(page).to have_content "($12,540.00 / kg)" # unit price + expect(page).to have_content "1 1g $12.54 $12.54 0.0% $12.54" + # second line item, included tax + expect(page).to have_content Spree::Product.second.name.to_s + expect(page).to have_content "($500,150.00 / kg)" # unit price + expect(page).to have_content "3 1g $500.15 $1,500.45 20.0% $1,800.54" + # Enterprise fee + expect(page).to have_content "#{enterprise_fee.name} fee by $120.00 15.0% $138.00" + expect(page).to have_content "coordinator #{user1.enterprises.first.name}" + # Shipping + expect(page).to have_content "Shipping ( Type: $100.55 10.0% $110.61" + expect(page).to have_content "#{shipping_method_name} )" + # Tax totals + expect(page).to have_content "Total tax (10.0%): $10.06 " \ + "Total tax (15.0%): $18.00 Total tax (20.0%): $300.09" + # Order Totals + expect(page).to have_content "Total (Incl. tax): $2,061.69" + expect(page).to have_content "Total (Excl. tax): $1,733.54" + end + end + end end end From b3b86c428cb0016e92750a40eb98679cb4cdd431 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 30 Oct 2023 22:13:01 +0100 Subject: [PATCH 7/7] test tax totals on the printed version of the invoice (v3) --- spec/system/admin/invoice_print_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index 504996e4bb..3e713c38ba 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -469,7 +469,8 @@ describe ' } let!(:shipping_method_name) { "SM1" } let!(:shipping_method) { - create(:shipping_method_with, :expensive_name, name: shipping_method_name, distributors: [distributor], + create(:shipping_method_with, :expensive_name, name: shipping_method_name, + distributors: [distributor], tax_category: shipping_tax_category) } let!(:enterprise_fee) { @@ -556,6 +557,7 @@ describe ' # Tax totals expect(page).to have_content "Total tax (10.0%): $9.14 " \ "Total tax (15.0%): $15.65 Total tax (20.0%): $250.08" + expect(page).to have_content "Total tax: $274.87" # Order Totals expect(page).to have_content "Total (Incl. tax): $1,733.54" expect(page).to have_content "Total (Excl. tax): $1,458.67" @@ -575,7 +577,8 @@ describe ' let(:fee_tax_category) { create(:tax_category, tax_rates: [enterprise_fee_rate_added]) } let!(:shipping_method_name) { "SM2" } let!(:shipping_method) { - create(:shipping_method_with, :expensive_name, name: shipping_method_name, distributors: [distributor], + create(:shipping_method_with, :expensive_name, name: shipping_method_name, + distributors: [distributor], tax_category: shipping_tax_category) } let(:enterprise_fee) { @@ -658,6 +661,7 @@ describe ' # Tax totals expect(page).to have_content "Total tax (10.0%): $10.06 " \ "Total tax (15.0%): $18.00 Total tax (20.0%): $300.09" + expect(page).to have_content "Total tax: $328.15" # Order Totals expect(page).to have_content "Total (Incl. tax): $2,061.69" expect(page).to have_content "Total (Excl. tax): $1,733.54"