From 05437e2a568177b34c94dc5ad24b8dcf3cbf586b Mon Sep 17 00:00:00 2001 From: Paul Mackay Date: Sun, 9 Nov 2014 11:17:46 +0000 Subject: [PATCH] #275: start on producer email. --- Gemfile | 2 +- Gemfile.lock | 6 +++++ .../admin/order_cycles_controller.rb | 21 +++++++++++++++ app/mailers/producer_mailer.rb | 13 +++++++++ app/views/admin/order_cycles/edit.html.haml | 4 +++ .../order_cycles/notifications.html.haml | 2 ++ .../order_cycle_report.html.haml | 27 +++++++++++++++++++ config/routes.rb | 2 ++ 8 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 app/mailers/producer_mailer.rb create mode 100644 app/views/admin/order_cycles/notifications.html.haml create mode 100644 app/views/producer_mailer/order_cycle_report.html.haml diff --git a/Gemfile b/Gemfile index 2e03d65a35..7ac484f4d0 100644 --- a/Gemfile +++ b/Gemfile @@ -43,7 +43,7 @@ gem 'spinjs-rails' gem 'rack-ssl', :require => 'rack/ssl' gem 'custom_error_message', :github => 'jeremydurham/custom-err-msg' gem 'angularjs-file-upload-rails', '~> 1.1.0' - +gem 'delayed_job_active_record' gem 'foreigner' gem 'immigrant' diff --git a/Gemfile.lock b/Gemfile.lock index c1a2c66522..3c36b410a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -221,6 +221,11 @@ GEM debugger-ruby_core_source (~> 1.2.3) debugger-linecache (1.2.0) debugger-ruby_core_source (1.2.3) + delayed_job (4.0.4) + activesupport (>= 3.0, < 4.2) + delayed_job_active_record (4.0.2) + activerecord (>= 3.0, < 4.2) + delayed_job (>= 3.0, < 4.1) devise (2.2.8) bcrypt-ruby (~> 3.0) orm_adapter (~> 0.1) @@ -521,6 +526,7 @@ DEPENDENCIES db2fog debugger-linecache deface! + delayed_job_active_record factory_girl_rails foreigner foundation-icons-sass-rails diff --git a/app/controllers/admin/order_cycles_controller.rb b/app/controllers/admin/order_cycles_controller.rb index 63daf918de..f11b8c8dfa 100644 --- a/app/controllers/admin/order_cycles_controller.rb +++ b/app/controllers/admin/order_cycles_controller.rb @@ -70,6 +70,27 @@ module Admin redirect_to main_app.admin_order_cycles_path, :notice => "Your order cycle #{@order_cycle.name} has been cloned." end + OrderCycleNotificationJob = Struct.new(:order_cycle) do + def perform + puts order_cycle + @suppliers = order_cycle.suppliers + puts @suppliers + @suppliers.each { |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver } + end + end + + # Send notifications to all producers who are part of the order cycle + def notifications + puts 'notify_producers!' + + @order_cycle = OrderCycle.find params[:id] + # Delayed::Job.enqueue OrderCycleNotificationJob.new(@order_cycle) + job = OrderCycleNotificationJob.new(@order_cycle) + job.perform + + render 'notifications' + end + protected def collection diff --git a/app/mailers/producer_mailer.rb b/app/mailers/producer_mailer.rb new file mode 100644 index 0000000000..d479a20d5a --- /dev/null +++ b/app/mailers/producer_mailer.rb @@ -0,0 +1,13 @@ +require 'devise/mailers/helpers' +class ProducerMailer < ActionMailer::Base Spree::BaseMailer + include Devise::Mailers::Helpers + + def order_cycle_report(producer, order_cycle) + @producer = producer + @coordinator = order_cycle.coordinator + @order_cycle = order_cycle + subject = "[Open Food Network] Order cycle report for " + mail(to: @producer.email, from: from_address, subject: subject) + end + +end diff --git a/app/views/admin/order_cycles/edit.html.haml b/app/views/admin/order_cycles/edit.html.haml index 9bf0a8ca31..0f64420a83 100644 --- a/app/views/admin/order_cycles/edit.html.haml +++ b/app/views/admin/order_cycles/edit.html.haml @@ -1,4 +1,8 @@ %h1 Edit Order Cycle += content_for :page_actions do + %li + = button_to "Notify producers", main_app.admin_order_cycle_path + '/notifications', :id => 'admin_notify_producers' + = form_for [main_app, :admin, @order_cycle], :url => '', :html => {:class => 'ng order_cycle', 'ng-app' => 'order_cycle', 'ng-controller' => 'AdminEditOrderCycleCtrl', 'ng-submit' => 'submit()'} do |f| = render 'form', :f => f diff --git a/app/views/admin/order_cycles/notifications.html.haml b/app/views/admin/order_cycles/notifications.html.haml new file mode 100644 index 0000000000..b1e9e95f52 --- /dev/null +++ b/app/views/admin/order_cycles/notifications.html.haml @@ -0,0 +1,2 @@ + +Email sent. diff --git a/app/views/producer_mailer/order_cycle_report.html.haml b/app/views/producer_mailer/order_cycle_report.html.haml new file mode 100644 index 0000000000..0f67acf075 --- /dev/null +++ b/app/views/producer_mailer/order_cycle_report.html.haml @@ -0,0 +1,27 @@ +Dear #{@producer.name}, + +We now have all the consumer orders for the food drop on #{}. +Please deliver to #{coordinator.address} between 9:30am and 10:30am. 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) + +Orders summary +============== + +Here is a summary of the orders for your products: + +- @order_cycle.exchange_for_distributor.each do |exchange| + + +Detailed orders breakdown +========================= + + + +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/config/routes.rb b/config/routes.rb index ca4a6b59ff..2e5aa0ffc8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,6 +40,8 @@ Openfoodnetwork::Application.routes.draw do resources :order_cycles do post :bulk_update, on: :collection, as: :bulk_update get :clone, on: :member + + post 'notifications', on: :member end resources :enterprises do