Merge pull request #11704 from abdellani/add-reference-to-previous-invoices

a new invoice should refer the latest invoice.
This commit is contained in:
Rachel Arnould
2023-12-14 16:05:08 +01:00
committed by GitHub
6 changed files with 56 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@@ -1993,6 +1993,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:"

View File

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

View File

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