From 41d5ca7861f1635cbd7bd6235943a84083e075a3 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Sat, 21 Oct 2023 08:21:50 +0100 Subject: [PATCH 1/3] update the invoice number calculation --- app/services/order_invoice_generator.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/services/order_invoice_generator.rb b/app/services/order_invoice_generator.rb index a6d30b33d0..1d91fec8da 100644 --- a/app/services/order_invoice_generator.rb +++ b/app/services/order_invoice_generator.rb @@ -9,7 +9,7 @@ class OrderInvoiceGenerator if comparator.can_generate_new_invoice? order.invoices.create!( date: Time.zone.today, - number: order.invoices.count + 1, + number: total_invoices_created_by_distributor + 1, data: invoice_data ) elsif comparator.can_update_latest_invoice? @@ -31,4 +31,8 @@ class OrderInvoiceGenerator def invoice_data @invoice_data ||= InvoiceDataGenerator.new(order).generate end + + def total_invoices_created_by_distributor + Invoice.joins(:order).where(order: { distributor: order.distributor }).count + end end From fea910f8b6de11b7be78aaff391c9bc9b35997ec Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Sat, 21 Oct 2023 08:22:12 +0100 Subject: [PATCH 2/3] update the invoice number rendering --- app/models/invoice.rb | 4 ++++ app/models/invoice/data_presenter.rb | 4 ++-- app/views/spree/admin/invoices/_invoices_table.html.haml | 2 +- app/views/spree/admin/orders/invoice4.html.haml | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index cf12c401dc..ef8d627a7e 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -26,4 +26,8 @@ class Invoice < ApplicationRecord def cancel_previous_invoices order.invoices.where.not(id:).update_all(cancelled: true) end + + def display_number + "#{order.distributor.id}-#{number}" + end end diff --git a/app/models/invoice/data_presenter.rb b/app/models/invoice/data_presenter.rb index 692c99486b..c9f0e5de42 100644 --- a/app/models/invoice/data_presenter.rb +++ b/app/models/invoice/data_presenter.rb @@ -5,8 +5,8 @@ class Invoice include ::ActionView::Helpers::NumberHelper attr_reader :invoice - delegate :data, to: :invoice - delegate :number, :date, to: :invoice, prefix: true + delegate :display_number, :data, to: :invoice + delegate :date, to: :invoice, prefix: true FINALIZED_NON_SUCCESSFUL_STATES = %w(canceled returned).freeze diff --git a/app/views/spree/admin/invoices/_invoices_table.html.haml b/app/views/spree/admin/invoices/_invoices_table.html.haml index 4c876319cd..d1b6c114eb 100644 --- a/app/views/spree/admin/invoices/_invoices_table.html.haml +++ b/app/views/spree/admin/invoices/_invoices_table.html.haml @@ -14,7 +14,7 @@ %td.align-center.created_at = invoice.presenter.display_date %td.align-center.label - = invoice.number + = invoice.display_number %td.align-center.label = invoice.presenter.total %td.align-center.label diff --git a/app/views/spree/admin/orders/invoice4.html.haml b/app/views/spree/admin/orders/invoice4.html.haml index e65e7c10b9..69c8ec9e8f 100644 --- a/app/views/spree/admin/orders/invoice4.html.haml +++ b/app/views/spree/admin/orders/invoice4.html.haml @@ -45,7 +45,7 @@ %td{ :align => "left" } %br = "#{t :invoice_number}:" - = @order.invoice_number + = @order.display_number %br = t :invoice_issued_on = l @order.invoice_date From 4a22224c7361d44f62d0a760b0fd8d2fc6e60a62 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 23 Oct 2023 16:34:50 +0100 Subject: [PATCH 3/3] fix existing tests --- spec/system/admin/order_spec.rb | 3 ++- spec/system/admin/orders/invoices_spec.rb | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 1d586e3e2d..5776adb41a 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -1078,10 +1078,11 @@ describe ' ].join(" ").upcase } + let(:invoice_number){ "#{order.distributor_id}-1" } let(:table_contents) { [ Invoice.first.created_at.strftime('%B %d, %Y').to_s, - "1", + invoice_number, "0.0", "Active", "Download" diff --git a/spec/system/admin/orders/invoices_spec.rb b/spec/system/admin/orders/invoices_spec.rb index da7086ebf2..5527eac11a 100644 --- a/spec/system/admin/orders/invoices_spec.rb +++ b/spec/system/admin/orders/invoices_spec.rb @@ -170,10 +170,13 @@ describe ' describe 'listing invoices' do let(:date){ Time.current.to_date } + let(:first_invoice){ "#{distributor.id}-1" } + let(:second_invoice){ "#{distributor.id}-2" } + let(:row1){ [ I18n.l(date, format: :long), - "2", + second_invoice, order.total, "Active", "Download" @@ -183,7 +186,7 @@ describe ' let(:row2){ [ I18n.l(date, format: :long), - "1", + first_invoice, order.total, "Cancelled", "Download"