diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index f135e16dfd..66a47d91c9 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -38,6 +38,7 @@ class ProducerMailer < Spree::BaseMailer @receival_instructions = @order_cycle.receival_instructions_for(@producer) @total = total_from_line_items(line_items) @tax_total = tax_total_from_line_items(line_items) + @customer_line_items = set_customer_data(line_items) end def subject @@ -74,4 +75,18 @@ class ProducerMailer < Spree::BaseMailer def tax_total_from_line_items(line_items) Spree::Money.new line_items.to_a.sum(&:included_tax) end + + def set_customer_data(line_items) + return unless @coordinator.preferred_show_customer_names_to_suppliers + line_items.map do |line_item| + { + sku: line_item.variant.sku, + supplier_name: line_item.product.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 + } + end.sort_by { |line_item| [line_item[:last_name].downcase, line_item[:first_name].downcase] } + end end diff --git a/app/views/producer_mailer/order_cycle_report.html.haml b/app/views/producer_mailer/order_cycle_report.html.haml index 9a21317d54..d4aea36453 100644 --- a/app/views/producer_mailer/order_cycle_report.html.haml +++ b/app/views/producer_mailer/order_cycle_report.html.haml @@ -59,6 +59,39 @@ #{@total} %td.text-right #{@tax_total} +- if @customer_line_items + %p + = t :producer_mail_order_customer_text + %table.order-summary.customer-order + %thead + %tr + %th + = t :sku + %th + = t :supplier + %th + = t :product + %th.text-right + = t :quantity + %th.text-right + = t :first_name + %th.text-right + = t :last_name + %tbody + - @customer_line_items.each do |line_item| + %tr + %td + #{line_item[:sku]} + %td + #{raw(line_item[:supplier_name])} + %td + #{raw(line_item[:product_and_full_name])} + %td.text-right + #{line_item[:quantity]} + %td + #{raw(line_item[:first_name])} + %td + #{raw(line_item[:last_name])} %p = t :producer_mail_text_after %p diff --git a/app/views/producer_mailer/order_cycle_report.text.haml b/app/views/producer_mailer/order_cycle_report.text.haml index 84ccf0dfd8..2cfc55cd76 100644 --- a/app/views/producer_mailer/order_cycle_report.text.haml +++ b/app/views/producer_mailer/order_cycle_report.text.haml @@ -20,6 +20,13 @@ Orders summary \ #{t :total}: #{@total} \ +- if @customer_grouped_line_items + = t :producer_mail_order_customer_text + \ + - @customer_line_items.each do |line_item| + #{line_item[:sku]} - #{raw(line_item[:supplier_name])} - #{raw(line_item[:product_and_full_name])} (QTY: #{line_item[:quantity]}) - #{raw(line_item[:first_name])} #{raw(line_item[:last_name])} +\ +\ = t :producer_mail_text_after #{t :producer_mail_signoff}, diff --git a/config/locales/en.yml b/config/locales/en.yml index 9f934a9110..623aba949a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1815,6 +1815,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using producer_mail_delivery_instructions: "Stock pickup/delivery instructions:" producer_mail_text_after: "" producer_mail_signoff: "Thanks and best wishes" + producer_mail_order_customer_text: "Here is a summary of the orders grouped by customers" shopping_oc_closed: Orders are closed shopping_oc_closed_description: "Please wait until the next cycle opens (or contact us directly to see if we can accept any late orders)" diff --git a/spec/mailers/producer_mailer_spec.rb b/spec/mailers/producer_mailer_spec.rb index a83a683ac0..e3a22d4ab2 100644 --- a/spec/mailers/producer_mailer_spec.rb +++ b/spec/mailers/producer_mailer_spec.rb @@ -128,6 +128,44 @@ describe ProducerMailer, type: :mailer do expect(mail.body.encoded).to include(p1.name) end + context 'when flag preferred_show_customer_names_to_suppliers is true' do + before do + order_cycle.coordinator.set_preference(:show_customer_names_to_suppliers, true) + end + + it "adds customer names table" do + expect(body_as_html(mail).find(".order-summary.customer-order")).to_not be_nil + end + + it "displays last name for each order" do + product_name = order.line_items.first.product.name + last_name = order.billing_address.lastname + expect(body_as_html(mail).find("table.order-summary.customer-order tr", text: product_name)).to have_selector("td", text: last_name) + end + + it "displays first name for each order" do + product_name = order.line_items.first.product.name + first_name = order.billing_address.firstname + expect(body_as_html(mail).find("table.order-summary.customer-order tr", text: product_name)).to have_selector("td", text: first_name) + end + + it "it orders list via last name" do + create(:order, :with_line_item, distributor: d1, order_cycle: order_cycle, state: 'complete', bill_address: FactoryBot.create(:address, last_name: "Abby")) + create(:order, :with_line_item, distributor: d1, order_cycle: order_cycle, state: 'complete', bill_address: FactoryBot.create(:address, last_name: "maggie")) + expect(mail.body.encoded).to match(/.*Abby.*Doe.*maggie/m) + end + end + + context 'when flag preferred_show_customer_names_to_suppliers is false' do + before do + order_cycle.coordinator.set_preference(:show_customer_names_to_suppliers, false) + end + + it "does not add customer names table" do + expect { body_as_html(mail).find(".order-summary.customer-order") }.to raise_error(Capybara::ElementNotFound) + end + end + private def body_lines_including(mail, str)