diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb index dc9033e5bc..5aacb9e03e 100644 --- a/app/controllers/admin/order_cycles_controller.rb +++ b/app/controllers/admin/order_cycles_controller.rb @@ -70,9 +70,12 @@ module Admin end # Send notifications to all producers who are part of the order cycle - def notifications + def notify_producers @order_cycle = OrderCycle.find params[:id] Delayed::Job.enqueue OrderCycleNotificationJob.new(@order_cycle) + + flash[:notice] = 'Emails to be sent to producers have been queued for sending.' + format.html { redirect_to admin_order_cycles_path } end diff --git a/app/jobs/order_cycle_notification_job.rb b/app/jobs/order_cycle_notification_job.rb index 2e66a31da5..ea8b09f1fe 100644 --- a/app/jobs/order_cycle_notification_job.rb +++ b/app/jobs/order_cycle_notification_job.rb @@ -1,8 +1,7 @@ OrderCycleNotificationJob = Struct.new(:order_cycle) do def perform - @suppliers = order_cycle.suppliers - @suppliers.each { |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver } + order_cycle.suppliers.each { |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver } end end diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb index 9d8f6fe1bc..8cf4efacb1 100644 --- a/app/mailers/producer_mailer.rb +++ b/app/mailers/producer_mailer.rb @@ -14,12 +14,11 @@ class ProducerMailer < Spree::BaseMailer # subject += " for #{@distribution_date}" if @distribution_date.present? # end - # TODO: what if producer handling orders into multiple order cycles? - @orders = Spree::Order.complete.not_state(:canceled).managed_by(@producer.owner) - # puts @orders.size + @line_items = Spree::LineItem. + joins(:order => :order_cycle, :variant => :product). + where('order_cycles.id = ?', order_cycle). + where('spree_products.supplier_id = ?', producer) - # Create a single flat list of all line items - @line_items = @orders.map(&:line_items).flatten # Arrange the items in a hash to group quantities @line_items = @line_items.inject({}) do |lis, li| lis[li.variant] ||= {line_item: li, quantity: 0} diff --git a/app/views/admin/order_cycles/edit.html.haml b/app/views/admin/order_cycles/edit.html.haml index 05c53ab91d..af41002d1e 100644 --- a/app/views/admin/order_cycles/edit.html.haml +++ b/app/views/admin/order_cycles/edit.html.haml @@ -2,7 +2,7 @@ = content_for :page_actions do %li - = button_to "Notify producers", main_app.notifications_admin_order_cycle_path, :id => 'admin_notify_producers' + = button_to "Notify producers", main_app.notify_producers_admin_order_cycle_path, :id => 'admin_notify_producers' - ng_controller = order_cycles_simple_view ? 'AdminSimpleEditOrderCycleCtrl' : 'AdminEditOrderCycleCtrl' diff --git a/app/views/admin/order_cycles/notifications.html.haml b/app/views/admin/order_cycles/notifications.html.haml deleted file mode 100644 index b1e9e95f52..0000000000 --- a/app/views/admin/order_cycles/notifications.html.haml +++ /dev/null @@ -1,2 +0,0 @@ - -Email sent. diff --git a/app/views/producer_mailer/order_cycle_report.text.haml b/app/views/producer_mailer/order_cycle_report.text.haml index 0b50e59d4a..43723aafa3 100644 --- a/app/views/producer_mailer/order_cycle_report.text.haml +++ b/app/views/producer_mailer/order_cycle_report.text.haml @@ -12,7 +12,7 @@ Orders summary Here is a summary of the orders for your products: \ - @line_items.each_pair do |variant, data| - #{variant.sku} #{raw(variant.product.supplier.name)} #{raw(variant.product.name)} #{raw(variant.options_text)} (QTY: #{data[:quantity]}) @ #{data[:line_item].single_money} = #{variant.display_amount} + #{variant.sku} #{raw(variant.product.supplier.name)} #{raw(variant.product.name)} #{raw(variant.options_text)} (QTY: #{data[:quantity]}) @ #{data[:line_item].single_money} = #{data[:line_item].display_amount} \ Detailed orders breakdown diff --git a/spec/jobs/order_cycle_notification_job_spec.rb b/spec/jobs/order_cycle_notification_job_spec.rb new file mode 100644 index 0000000000..77fbf0c430 --- /dev/null +++ b/spec/jobs/order_cycle_notification_job_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe OrderCycleNotificationJob do + let(:order_cycle) { create(:order_cycle) } + + it 'sends a mail to each supplier' do + mail = double() + allow(mail).to receive(:deliver) + mailer = double('ProducerMailer') + expect(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail) + job = OrderCycleNotificationJob.new(order_cycle) + job.perform + end +end diff --git a/spec/mailers/producer_mailer_spec.rb b/spec/mailers/producer_mailer_spec.rb index 33b52d0985..19a10f4336 100644 --- a/spec/mailers/producer_mailer_spec.rb +++ b/spec/mailers/producer_mailer_spec.rb @@ -5,8 +5,8 @@ describe ProducerMailer do let(:s2) { create(:supplier_enterprise, address: create(:address)) } let(:d1) { create(:distributor_enterprise, address: create(:address)) } let(:d2) { create(:distributor_enterprise, address: create(:address)) } - let(:p1) { create(:product, price: 12.34, distributors: [d1], supplier: s1) } - let(:p2) { create(:product, price: 23.45, distributors: [d2], supplier: s2) } + let(:p1) { create(:product, price: 12.34, supplier: s1) } + let(:p2) { create(:product, price: 23.45, supplier: s2) } let(:order_cycle) { create(:simple_order_cycle) } let!(:order) do order = create(:order, distributor: d1, order_cycle: order_cycle, state: 'complete')