From 57c30591bc30e6ab24ccb8801350924c08032e02 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Tue, 15 Apr 2025 02:05:51 +0500 Subject: [PATCH 1/3] 13128: add order number in OC report - Make Customer Code independant of Customer Names setting --- app/mailers/producer_mailer.rb | 12 ++++--- .../order_cycle_report.html.haml | 22 ++++++++----- config/locales/en.yml | 1 + spec/mailers/producer_mailer_spec.rb | 32 ++++++++++++++++--- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index 104e3adc41..1cc79e3f2f 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -77,11 +77,12 @@ class ProducerMailer < ApplicationMailer end def customer_data(line_items) - return unless @coordinator.show_customer_names_to_suppliers? - + @display_customer_names = @coordinator.show_customer_names_to_suppliers? @display_business_name = false + line_items.map do |line_item| - customer_code = line_item.order.customer&.code + order = line_item.order + customer_code = order.customer&.code @display_business_name = true if customer_code.present? { @@ -89,9 +90,10 @@ class ProducerMailer < ApplicationMailer supplier_name: line_item.variant.supplier.name, product_and_full_name: line_item.product_and_full_name, quantity: line_item.quantity, - first_name: line_item.order.billing_address.first_name, - last_name: line_item.order.billing_address.last_name, + first_name: order.billing_address.first_name, + last_name: order.billing_address.last_name, business_name: customer_code, + order_number: order.number } end.sort_by { |line_item| [line_item[:last_name].downcase, line_item[:first_name].downcase] } end diff --git a/app/views/producer_mailer/order_cycle_report.html.haml b/app/views/producer_mailer/order_cycle_report.html.haml index 645d9c7cd1..14a04daef8 100644 --- a/app/views/producer_mailer/order_cycle_report.html.haml +++ b/app/views/producer_mailer/order_cycle_report.html.haml @@ -77,13 +77,16 @@ = t :product %th.text-right = t :quantity - %th.text-right - = t :first_name - %th.text-right - = t :last_name + - if @display_customer_names + %th.text-right + = t :first_name + %th.text-right + = t :last_name - if @display_business_name %th.text-right = t :business_name + %th.text-right + = t :oc_report_order_number %tbody - @customer_line_items.each do |line_item| %tr @@ -96,13 +99,16 @@ = line_item[:product_and_full_name] %td.text-right = line_item[:quantity] - %td - = line_item[:first_name] - %td - = line_item[:last_name] + - if @display_customer_names + %td + = line_item[:first_name] + %td + = line_item[:last_name] - if @display_business_name %td = line_item[:business_name] + %td + = line_item[:order_number] %p = t :producer_mail_text_after %p diff --git a/config/locales/en.yml b/config/locales/en.yml index 64f868c1e1..8970156519 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3482,6 +3482,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using pending: Pending shipped: Shipped business_name: Business Name + oc_report_order_number: Order Number js: saving: 'Saving...' changes_saved: 'Changes saved.' diff --git a/spec/mailers/producer_mailer_spec.rb b/spec/mailers/producer_mailer_spec.rb index 67ef14c86d..ade47093ac 100644 --- a/spec/mailers/producer_mailer_spec.rb +++ b/spec/mailers/producer_mailer_spec.rb @@ -190,6 +190,28 @@ RSpec.describe ProducerMailer, type: :mailer do end end end + + context "validate order number" do + let(:table_header) do + html_body(mail).find("table.order-summary.customer-order thead") + end + + it 'displays order number for the customer' do + expect(table_header).to have_selector("th", text: 'Order Number') + expect( + html_body(mail).find("table.order-summary.customer-order tbody tr") + ).to have_selector("td", text: order.number) + expect(customer_details_summary_text(mail)).to include(order.number) + end + end + + it "adds customer names in the table" do + html_body(mail).find(".order-summary.customer-order").tap do |table| + expect(table).to have_selector("th", text: "First Name") + expect(table).to have_selector("th", text: "Last Name") + end + expect(customer_details_summary_text(mail)).to be_present + end end context 'when flag show_customer_names_to_suppliers is false' do @@ -197,10 +219,12 @@ RSpec.describe ProducerMailer, type: :mailer do order_cycle.coordinator.show_customer_names_to_suppliers = false end - it "does not add customer names table" do - expect { - parsed_email.find(".order-summary.customer-order") - }.to raise_error(Capybara::ElementNotFound) + it "does not add customer names in the table" do + html_body(mail).find(".order-summary.customer-order").tap do |table| + expect(table).not_to have_selector("th", text: "First Name") + expect(table).not_to have_selector("th", text: "Last Name") + end + expect(customer_details_summary_text(mail)).to be_present end end From d9c15cf41452460f8b0ddef6ed3780d5fd4b4e71 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Thu, 24 Apr 2025 16:09:28 +0500 Subject: [PATCH 2/3] update locale to use lazy loading --- app/views/producer_mailer/order_cycle_report.html.haml | 2 +- config/locales/en.yml | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/views/producer_mailer/order_cycle_report.html.haml b/app/views/producer_mailer/order_cycle_report.html.haml index 14a04daef8..96408edb2b 100644 --- a/app/views/producer_mailer/order_cycle_report.html.haml +++ b/app/views/producer_mailer/order_cycle_report.html.haml @@ -86,7 +86,7 @@ %th.text-right = t :business_name %th.text-right - = t :oc_report_order_number + = t '.order_number' %tbody - @customer_line_items.each do |line_item| %tr diff --git a/config/locales/en.yml b/config/locales/en.yml index 8970156519..de4909d95f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -418,6 +418,8 @@ en: producer_mailer: order_cycle: subject: "Order cycle report for %{producer}" + order_cycle_report: + order_number: "Order Number" provider_settings: "Provider settings" report_mailer: report_ready: @@ -3482,7 +3484,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using pending: Pending shipped: Shipped business_name: Business Name - oc_report_order_number: Order Number js: saving: 'Saving...' changes_saved: 'Changes saved.' From 6f9b5e2c5429d12346f4635a26f54d0d4b0847f5 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Mon, 28 Apr 2025 14:54:41 +0500 Subject: [PATCH 3/3] fix specs - add a new class .line-items in the orders-summary to better differentiate the line-items summary vs customer-details. This css class is not defined, just .customer-order for customer details. It's only used to make a differentiation in the specs - use the updated `parsed_email` memoized helper method for mail body content --- .../producer_mailer/order_cycle_report.html.haml | 2 +- spec/mailers/producer_mailer_spec.rb | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/app/views/producer_mailer/order_cycle_report.html.haml b/app/views/producer_mailer/order_cycle_report.html.haml index 96408edb2b..585f01d4f2 100644 --- a/app/views/producer_mailer/order_cycle_report.html.haml +++ b/app/views/producer_mailer/order_cycle_report.html.haml @@ -15,7 +15,7 @@ = @receival_instructions %p = t :producer_mail_order_text - %table.order-summary + %table.order-summary.line-items %thead %tr %th diff --git a/spec/mailers/producer_mailer_spec.rb b/spec/mailers/producer_mailer_spec.rb index ade47093ac..e0f01764d1 100644 --- a/spec/mailers/producer_mailer_spec.rb +++ b/spec/mailers/producer_mailer_spec.rb @@ -79,7 +79,7 @@ RSpec.describe ProducerMailer, type: :mailer do end it "contains an aggregated list of produce in alphabetical order" do - rows = parsed_email.all('table.order-summary tbody tr:not(.total-row)') + rows = parsed_email.all('table.order-summary.line-items tbody tr:not(.total-row)') actual = rows.map do |row| row.all('td').map { |td| td.text.strip } end @@ -153,7 +153,6 @@ RSpec.describe ProducerMailer, type: :mailer do end it "displays last name and first name for each order" do - product_name = order.line_items.first.product.name last_name = order.billing_address.lastname first_name = order.billing_address.firstname row = parsed_email.find("table.order-summary.customer-order tbody tr") @@ -193,24 +192,22 @@ RSpec.describe ProducerMailer, type: :mailer do context "validate order number" do let(:table_header) do - html_body(mail).find("table.order-summary.customer-order thead") + parsed_email.find("table.order-summary.customer-order thead") end it 'displays order number for the customer' do expect(table_header).to have_selector("th", text: 'Order Number') expect( - html_body(mail).find("table.order-summary.customer-order tbody tr") + parsed_email.find("table.order-summary.customer-order tbody tr") ).to have_selector("td", text: order.number) - expect(customer_details_summary_text(mail)).to include(order.number) end end it "adds customer names in the table" do - html_body(mail).find(".order-summary.customer-order").tap do |table| + parsed_email.find(".order-summary.customer-order").tap do |table| expect(table).to have_selector("th", text: "First Name") expect(table).to have_selector("th", text: "Last Name") end - expect(customer_details_summary_text(mail)).to be_present end end @@ -220,11 +217,10 @@ RSpec.describe ProducerMailer, type: :mailer do end it "does not add customer names in the table" do - html_body(mail).find(".order-summary.customer-order").tap do |table| + parsed_email.find(".order-summary.customer-order").tap do |table| expect(table).not_to have_selector("th", text: "First Name") expect(table).not_to have_selector("th", text: "Last Name") end - expect(customer_details_summary_text(mail)).to be_present end end