Skip to content

Commit

Permalink
Merge pull request #12996 from chahmedejaz/bugfix/12993-fix-order-cyc…
Browse files Browse the repository at this point in the history
…le-report-text-version

Order cycle mail reports to producers display different data in html and txt versions
  • Loading branch information
filipefurtad0 authored Dec 9, 2024
2 parents 118ed91 + 42940f4 commit 7ca5927
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
18 changes: 9 additions & 9 deletions app/views/producer_mailer/order_cycle_report.text.haml
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
#{t :producer_mail_greeting} #{@producer.name},
#{t :producer_mail_greeting} #{raw(@producer.name)},
\
= t :producer_mail_text_before
\
- @distributors_pickup_times.each do |distributor_name, pickup_time|
\- #{distributor_name} (#{pickup_time})
\- #{raw(distributor_name)} (#{pickup_time})
\
- if @receival_instructions
= t :producer_mail_delivery_instructions
= @receival_instructions
= raw(@receival_instructions)
\
Orders summary
================
\
= t :producer_mail_order_text
\
- @grouped_line_items.each_pair do |product_and_full_name, line_items|
#{line_items.first.variant.sku} - #{raw(line_items.first.variant.supplier.name)} - #{raw(product_and_full_name)} (QTY: #{line_items.sum(&:quantity)}) @ #{line_items.first.single_money} = #{Spree::Money.new(line_items.sum(&:total), currency: line_items.first.currency)}
#{line_items.first.variant.sku} - #{raw(line_items.first.variant.supplier.name)} - #{raw(product_and_full_name)} (#{t(:producer_mail_qty)}: #{line_items.sum(&:quantity)}) @ #{line_items.first.single_money} = #{Spree::Money.new(line_items.sum(&:total), currency: line_items.first.currency)} (#{t(:with_tax_incl, amount: Spree::Money.new(line_items.sum(&:included_tax), currency: line_items.first.currency))})
\
\
#{t :total}: #{@total}
#{t :total}: #{@total} (#{t(:with_tax_incl, amount: @tax_total)})
\
- if @customer_grouped_line_items
- if @customer_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])}
#{line_item[:sku]} - #{raw(line_item[:supplier_name])} - #{raw(line_item[:product_and_full_name])} (#{t(:producer_mail_qty)}: #{line_item[:quantity]}) - #{raw(line_item[:first_name])} #{raw(line_item[:last_name])}
\
\
= t :producer_mail_text_after

#{t :producer_mail_signoff},
#{@coordinator.name}
#{@coordinator.address.address1}, #{@coordinator.address.city}, #{@coordinator.address.zipcode}
#{raw(@coordinator.name)}
#{raw(@coordinator.address.address1)}, #{raw(@coordinator.address.city)}, #{raw(@coordinator.address.zipcode)}
#{@coordinator.phone}
#{@coordinator.contact.email}
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ en:
sku: "SKU"
subtotal: "Subtotal"
tax_rate: "Tax rate"
with_tax_incl: "%{amount} tax incl."
producer_mail_qty: QTY
validators:
date_time_string_validator:
not_string_error: "must be a string"
Expand Down
40 changes: 40 additions & 0 deletions spec/mailers/producer_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
# Tax for p1 line items
expect(body_as_html(mail).find("table.order-summary tr", text: p1.name))
.to have_selector("td.tax", text: "$2.73")
expect(
product_line_from_order_summary_text(mail, p1.name)
).to include("($2.73 tax incl.)")
end

it "does not include incomplete orders" do
Expand Down Expand Up @@ -149,20 +152,27 @@

it "adds customer names table" do
expect(body_as_html(mail).find(".order-summary.customer-order")).not_to be_nil
expect(customer_details_summary_text(mail)).to be_present
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)
expect(
product_line_from_details_summary_text(mail, product_name)
).to include(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)
expect(
product_line_from_details_summary_text(mail, product_name)
).to include(first_name)
end

it "it orders list via last name" do
Expand All @@ -171,6 +181,7 @@
create(:order, :with_line_item, distributor: d1, order_cycle:, state: 'complete',
bill_address: FactoryBot.create(:address, last_name: "smith"))
expect(mail.body.encoded).to match(/.*Abby.*Doe.*smith/m)
expect(customer_details_summary_text(mail)).to include('Abby', 'Doe', 'smith')
end
end

Expand All @@ -183,6 +194,7 @@
expect {
body_as_html(mail).find(".order-summary.customer-order")
}.to raise_error(Capybara::ElementNotFound)
expect(customer_details_summary_text(mail)).to be_nil
end
end

Expand Down Expand Up @@ -238,4 +250,32 @@ def body_lines_including(mail, str)
def body_as_html(mail)
Capybara.string(mail.html_part.body.encoded)
end

def body_as_text(mail)
mail.text_part.body.decoded
end

def customer_details_summary_text(mail)
body_as_text(mail)
.split(I18n.t(:producer_mail_order_customer_text))
.second
end

def product_line_from_details_summary_text(mail, product_name)
summary = customer_details_summary_text(mail)
product_line_by_summary(summary, product_name)
end

def product_line_from_order_summary_text(mail, product_name)
summary = body_as_text(mail)
.split(I18n.t(:producer_mail_order_customer_text))
.first
product_line_by_summary(summary, product_name)
end

def product_line_by_summary(summary, product_name)
return '' unless summary

summary.lines.find { |line| line.include?(product_name) } || ''
end
end

0 comments on commit 7ca5927

Please sign in to comment.