From b5cf47d306b27a2f371568ffdf9f8805f46f9d56 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 22 May 2024 20:23:56 +1000 Subject: [PATCH] Fix producer mailer --- app/mailers/producer_mailer.rb | 11 ++++++----- .../producer_mailer/order_cycle_report.html.haml | 2 +- .../producer_mailer/order_cycle_report.text.haml | 2 +- spec/mailers/producer_mailer_spec.rb | 13 +++++++------ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index 4752141c42..99c5f0f4f5 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -60,11 +60,12 @@ class ProducerMailer < ApplicationMailer def line_items_from(order_cycle, producer) @line_items ||= Spree::LineItem. - includes(variant: [:product]). + includes(variant: :product). + joins(variant: :product). from_order_cycle(order_cycle). - sorted_by_name_and_unit_value. - merge(Spree::Product.with_deleted.in_supplier(producer)). - merge(Spree::Order.by_state(["complete", "resumed"])) + merge(Spree::Variant.with_deleted.where(supplier: producer)). + merge(Spree::Order.by_state(["complete", "resumed"])). + sorted_by_name_and_unit_value end def total_from_line_items(line_items) @@ -81,7 +82,7 @@ class ProducerMailer < ApplicationMailer line_items.map do |line_item| { sku: line_item.variant.sku, - supplier_name: line_item.product.supplier.name, + 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, diff --git a/app/views/producer_mailer/order_cycle_report.html.haml b/app/views/producer_mailer/order_cycle_report.html.haml index c44742a4b1..4b85b9ce44 100644 --- a/app/views/producer_mailer/order_cycle_report.html.haml +++ b/app/views/producer_mailer/order_cycle_report.html.haml @@ -40,7 +40,7 @@ = line_items.first.variant.sku - if @distributors_pickup_times.many? %td - = line_items.first.product.supplier.name + = line_items.first.variant.supplier.name %td = product_and_full_name %td.text-right diff --git a/app/views/producer_mailer/order_cycle_report.text.haml b/app/views/producer_mailer/order_cycle_report.text.haml index 2cfc55cd76..adf77e8eb8 100644 --- a/app/views/producer_mailer/order_cycle_report.text.haml +++ b/app/views/producer_mailer/order_cycle_report.text.haml @@ -15,7 +15,7 @@ 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.product.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)} (QTY: #{line_items.sum(&:quantity)}) @ #{line_items.first.single_money} = #{Spree::Money.new(line_items.sum(&:total), currency: line_items.first.currency)} \ \ #{t :total}: #{@total} diff --git a/spec/mailers/producer_mailer_spec.rb b/spec/mailers/producer_mailer_spec.rb index b4e1be284a..fe1bbe2d8b 100644 --- a/spec/mailers/producer_mailer_spec.rb +++ b/spec/mailers/producer_mailer_spec.rb @@ -16,13 +16,14 @@ RSpec.describe ProducerMailer, type: :mailer do let(:d1) { create(:distributor_enterprise, charges_sales_tax: true) } let(:d2) { create(:distributor_enterprise) } let(:p1) { - create(:product, name: "Zebra", price: 12.34, supplier: s1, tax_category_id: tax_category.id) + create(:product, name: "Zebra", price: 12.34, supplier_id: s1.id, + tax_category_id: tax_category.id) } - let(:p2) { create(:product, name: "Aardvark", price: 23.45, supplier: s2) } - let(:p3) { create(:product, name: "Banana", price: 34.56, supplier: s1) } - let(:p4) { create(:product, name: "coffee", price: 45.67, supplier: s1) } - let(:p5) { create(:product, name: "Daffodil", price: 56.78, supplier: s1) } - let(:p6) { create(:product, name: "Eggs", price: 67.89, supplier: s1) } + let(:p2) { create(:product, name: "Aardvark", price: 23.45, supplier_id: s2.id) } + let(:p3) { create(:product, name: "Banana", price: 34.56, supplier_id: s1.id) } + let(:p4) { create(:product, name: "coffee", price: 45.67, supplier_id: s1.id) } + let(:p5) { create(:product, name: "Daffodil", price: 56.78, supplier_id: s1.id) } + let(:p6) { create(:product, name: "Eggs", price: 67.89, supplier_id: s1.id) } let(:order_cycle) { create(:simple_order_cycle) } let!(:incoming_exchange) { order_cycle.exchanges.create! sender: s1, receiver: d1, incoming: true,