Add option to send mails when an order has been received

This commit is contained in:
lentschi 2021-02-27 17:24:25 +01:00 committed by GitHub
parent d45256145d
commit 9a7d4bf07d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 2 deletions

View file

@ -138,6 +138,7 @@ class OrdersController < ApplicationController
flash[:notice] = (s ? I18n.t('orders.receive.notice', :msg => s) : I18n.t('orders.receive.notice_none'))
end
Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'received_order', @order.id)
if current_user.role_orders? || current_user.role_finance?
redirect_to @order
elsif current_user.role_pickup?

View file

@ -61,6 +61,19 @@ class Mailer < ActionMailer::Base
subject: I18n.t('mailer.order_result.subject', name: group_order.order.name)
end
# Sends order received info for specific Ordergroup
def order_received(user, group_order)
@order = group_order.order
@group_order = group_order
order_articles = @order.order_articles.reject { |oa| oa.units_received.nil? }
@abundant_articles = order_articles.select { |oa| oa.difference_received_ordered.positive? }
@scarce_articles = order_articles.select { |oa| oa.difference_received_ordered.negative? }
mail to: user,
subject: I18n.t('mailer.order_received.subject', name: group_order.order.name)
end
# Sends order result to the supplier
def order_result_supplier(user, order, options = {})
@user = user

View file

@ -202,6 +202,10 @@ class OrderArticle < ApplicationRecord
group_order_articles.any? {|goa| goa.result_manually_changed?}
end
def difference_received_ordered
(units_received || 0) - units_to_order
end
private
def article_and_price_exist

View file

@ -0,0 +1,13 @@
= raw t '.text0', ordergroup: @group_order.ordergroup_name, order: @order.name
- unless @abundant_articles.empty?
= raw "===\n" + t('.abundant_articles') + ":"
- @abundant_articles.each do |order_article|
- article = order_article.article
= raw t('.article_details', name: article.name, ordered: order_article.units_to_order, received: order_article.units_received, unit: article.unit)
- unless @scarce_articles.empty?
= raw "===\n" + t('.scarce_articles') + ":"
- @scarce_articles.each do |order_article|
- article = order_article.article
= raw t('.article_details', name: article.name, ordered: order_article.units_to_order, received: order_article.units_received, unit: article.unit)

View file

@ -35,14 +35,15 @@
= profile.input 'email_is_public', as: :boolean, label: false, input_html: { checked: f.object.settings.profile['email_is_public'] }
- if FoodsoftConfig[:use_nick]
= profile.input 'name_is_public', as: :boolean, label: false, input_html: { checked: f.object.settings.profile['name_is_public'] }
.settings-group
%div{class: 'control-group'}
%h5{class: 'controls'}
= t 'simple_form.labels.settings.settings_group.messages'
= s.simple_fields_for :notify, defaults: { inline_label: true, label: false } do |notify|
= notify.input 'order_finished', as: :boolean, input_html: { checked: f.object.settings.notify['order_finished'] }
= notify.input 'order_received', as: :boolean, input_html: { checked: f.object.settings.notify['order_received'] }
= notify.input 'negative_balance', as: :boolean, input_html: { checked: f.object.settings.notify['negative_balance'] }
= notify.input 'upcoming_tasks', as: :boolean, input_html: { checked: f.object.settings.notify['upcoming_tasks'] }

View file

@ -22,6 +22,21 @@ class UserNotifier
end
end
def self.received_order(args)
order_id = args.first
Order.find(order_id).group_orders.each do |group_order|
next if group_order.ordergroup.nil?
group_order.ordergroup.users.each do |user|
next unless user.settings.notify['order_received']
Mailer.deliver_now_with_user_locale user do
Mailer.order_received(user, group_order)
end
end
end
end
# If this order group's account balance is made negative by the given/last transaction,
# a message is sent to all users who have enabled notification.
def self.negative_balance(args)