From 615c001c28c0e1bb70056f76b74db890cb8f5630 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 24 Oct 2023 09:10:22 +0100 Subject: [PATCH 1/2] implement Invoice#previous_invoice --- app/models/invoice.rb | 4 ++++ app/models/invoice/data_presenter.rb | 2 +- spec/models/invoice_spec.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index ef8d627a7e..55524521b2 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -30,4 +30,8 @@ class Invoice < ApplicationRecord def display_number "#{order.distributor.id}-#{number}" end + + def previous_invoice + order.invoices.where("id < ?", id).first + end end diff --git a/app/models/invoice/data_presenter.rb b/app/models/invoice/data_presenter.rb index c9f0e5de42..8c5c4dbbab 100644 --- a/app/models/invoice/data_presenter.rb +++ b/app/models/invoice/data_presenter.rb @@ -5,7 +5,7 @@ class Invoice include ::ActionView::Helpers::NumberHelper attr_reader :invoice - delegate :display_number, :data, to: :invoice + delegate :display_number, :data, :previous_invoice, to: :invoice delegate :date, to: :invoice, prefix: true FINALIZED_NON_SUCCESSFUL_STATES = %w(canceled returned).freeze diff --git a/spec/models/invoice_spec.rb b/spec/models/invoice_spec.rb index 81dad4005e..b5a3fd94a8 100644 --- a/spec/models/invoice_spec.rb +++ b/spec/models/invoice_spec.rb @@ -18,4 +18,19 @@ RSpec.describe Invoice, type: :model do expect(invoice.data).to eq(Invoice::OrderSerializer.new(order).serializable_hash) end end + + describe "previous_invoice" do + it "should return the previous invoice" do + invoice1 = create(:invoice, order:, number: 1, created_at: 3.days.ago) + invoice2 = create(:invoice, order:, number: 2, created_at: 2.days.ago) + invoice3 = create(:invoice, order:, number: 3, created_at: 1.day.ago) + expect(invoice3.previous_invoice).to eq(invoice2) + expect(invoice2.previous_invoice).to eq(invoice1) + end + + it "should return nil if there is no previous invoice" do + invoice = create(:invoice, order:) + expect(invoice.previous_invoice).to be_nil + end + end end From 915afd85571091237b4e113f876cae74c79529f7 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 24 Oct 2023 09:10:44 +0100 Subject: [PATCH 2/2] update the invoice v3 template --- .../spree/admin/orders/invoice4.html.haml | 2 ++ config/locales/en.yml | 1 + spec/system/admin/invoice_print_spec.rb | 33 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/app/views/spree/admin/orders/invoice4.html.haml b/app/views/spree/admin/orders/invoice4.html.haml index 69c8ec9e8f..bf7eee3679 100644 --- a/app/views/spree/admin/orders/invoice4.html.haml +++ b/app/views/spree/admin/orders/invoice4.html.haml @@ -46,6 +46,8 @@ %br = "#{t :invoice_number}:" = @order.display_number + - if @order.previous_invoice.present? + = "#{t :invoice_cancel_and_replace_invoice} #{ @order.previous_invoice.display_number}" %br = t :invoice_issued_on = l @order.invoice_date diff --git a/config/locales/en.yml b/config/locales/en.yml index 05ee256012..a6bae84eed 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1991,6 +1991,7 @@ en: invoice_column_price_per_unit_without_taxes: "Price Per unit (Excl. tax)" invoice_column_tax_rate: "Tax rate" invoice_tax_total: "GST Total:" + invoice_cancel_and_replace_invoice: "cancels and replaces invoice" tax_invoice: "TAX INVOICE" tax_total: "Total tax (%{rate}):" invoice_shipping_type: "Type:" diff --git a/spec/system/admin/invoice_print_spec.rb b/spec/system/admin/invoice_print_spec.rb index 24649c98ce..5e51aae36b 100644 --- a/spec/system/admin/invoice_print_spec.rb +++ b/spec/system/admin/invoice_print_spec.rb @@ -662,6 +662,39 @@ describe ' end end end + describe "Rendering previous invoice number" do + context "Order doesn't have previous invoices" do + it "should display the invoice number" do + login_as_admin + visit spree.print_admin_order_path(order, params: {}) + + convert_pdf_to_page + expect(page).to have_content "#{order.distributor_id}-#{order.invoices.first.number}" + end + end + + context "Order has previous invoices" do + before do + OrderInvoiceGenerator.new(order).generate_or_update_latest_invoice + first_line_item = order.line_items.first + order.line_items.first.update(quantity: first_line_item.quantity + 1) + end + + it "should display the invoice number along with the latest invoice number" do + login_as_admin + visit spree.print_admin_order_path(order, params: {}) + + expect(order.invoices.count).to eq(2) + + new_invoice_number = "#{order.distributor_id}-#{order.invoices.first.number}" + canceled_invoice_number = "#{order.distributor_id}-#{order.invoices.last.number}" + + convert_pdf_to_page + expect(page).to have_content "#{new_invoice_number} cancels and replaces invoice #{ + canceled_invoice_number}" + end + end + end end end