From 375bdc0586ee8f3e1c7a97611ae9c6610de000ca Mon Sep 17 00:00:00 2001 From: Paul Mackay Date: Sun, 16 Nov 2014 07:14:29 +0000 Subject: [PATCH] #275: Change report format. Improved mailer. --- app/mailers/producer_mailer.rb | 25 +++++++++++---- ...html.haml => order_cycle_report.text.haml} | 21 ++++++------ spec/mailers/producer_mailer_spec.rb | 32 +++++++++++++++++++ 3 files changed, 62 insertions(+), 16 deletions(-) rename app/views/producer_mailer/{order_cycle_report.html.haml => order_cycle_report.text.haml} (59%) create mode 100644 spec/mailers/producer_mailer_spec.rb diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index 6a8265f279..9666ed8b0e 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -1,5 +1,6 @@ - +require 'devise/mailers/helpers' class ProducerMailer < Spree::BaseMailer + include Devise::Mailers::Helpers def order_cycle_report(producer, order_cycle) @producer = producer @@ -7,12 +8,24 @@ class ProducerMailer < Spree::BaseMailer @order_cycle = order_cycle # TODO: consider what happens if there is more than one distributor - first_producer = @order_cycle.distributors[0] - @distribution_date = @order_cycle.pickup_time_for first_producer - # puts @distribution_date + if @order_cycle.distributors.count > 0 + first_producer = @order_cycle.distributors[0] + @distribution_date = @order_cycle.pickup_time_for first_producer + end - subject = "[Open Food Network] Order cycle report for #{@distribution_date}" - mail(to: @producer.email, from: from_address, subject: subject) + subject = "[#{Spree::Config[:site_name]}] Order cycle report for #{@distribution_date}" + + @orders = Spree::Order.complete.not_state(:canceled).managed_by(@producer.owner) + @line_items = [] + @orders.each do |o| + @line_items += o.line_items.managed_by(@producer.owner) + end + + mail(to: @producer.email, + from: from_address, + subject: subject, + reply_to: @coordinator.email, + cc: @coordinator.email) end end diff --git a/app/views/producer_mailer/order_cycle_report.html.haml b/app/views/producer_mailer/order_cycle_report.text.haml similarity index 59% rename from app/views/producer_mailer/order_cycle_report.html.haml rename to app/views/producer_mailer/order_cycle_report.text.haml index 3f19e7eb5f..3e6767be7c 100644 --- a/app/views/producer_mailer/order_cycle_report.html.haml +++ b/app/views/producer_mailer/order_cycle_report.text.haml @@ -1,21 +1,22 @@ Dear #{@producer.name}, - +\ We now have all the consumer orders for the food drop on #{@distribution_date}. Please deliver to #{@coordinator.address.address1}, #{@coordinator.address.city}, #{@coordinator.address.zipcode} during the regular delivery time. If this is not convenient then please call #{@coordinator.phone}. -NB If you have to arrange a different delivery day and time, the school has requested that you do not come on site during drop off/pick up times (8:45-9:15 and 15:00-15:30) +Note: If you have to arrange a different delivery day and time, it is requested that you do not come on site during drop off/pick up times. +\ Orders summary -============== - +================ +\ Here is a summary of the orders for your products: +\ +- @line_items.each do |item| + #{item.variant.sku} #{raw(item.variant.product.supplier.name)} #{raw(item.variant.product.name)} #{raw(item.variant.options_text)} (QTY: #{item.quantity}) @ #{item.single_money} = #{item.display_amount} -/ - @order_cycle.exchange_for_distributor.each do |exchange| - - +\ Detailed orders breakdown -========================= - +=========================== Please confirm that you have got this email. @@ -23,5 +24,5 @@ Please confirm that you have got this email. Please send me an invoice for this amount so we can send you payment. If you need to phone on the day please call #{@coordinator.phone}. - +\ Thanks and best wishes - #{@coordinator.name} diff --git a/spec/mailers/producer_mailer_spec.rb b/spec/mailers/producer_mailer_spec.rb new file mode 100644 index 0000000000..07ce507dc3 --- /dev/null +++ b/spec/mailers/producer_mailer_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe ProducerMailer do + let(:p) { create(:simple_product, supplier: s) } + let(:supplier) { create(:supplier_enterprise) } + let(:order_cycle) { create(:simple_order_cycle) } + + after do + ActionMailer::Base.deliveries.clear + end + + before do + ActionMailer::Base.delivery_method = :test + ActionMailer::Base.perform_deliveries = true + ActionMailer::Base.deliveries = [] + end + + it "should send an email when an order cycle is closed" do + ProducerMailer.order_cycle_report(supplier, order_cycle).deliver + ActionMailer::Base.deliveries.count.should == 3 + end + + it "sets a reply-to of the enterprise email" do + ProducerMailer.order_cycle_report(supplier, order_cycle).deliver + ActionMailer::Base.deliveries.last.reply_to.should == [supplier.email] + end + + it "ccs the enterprise" do + ProducerMailer.order_cycle_report(supplier, order_cycle).deliver + ActionMailer::Base.deliveries.last.cc.should == [supplier.email] + end +end