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')) flash[:notice] = (s ? I18n.t('orders.receive.notice', :msg => s) : I18n.t('orders.receive.notice_none'))
end end
Resque.enqueue(UserNotifier, FoodsoftConfig.scope, 'received_order', @order.id)
if current_user.role_orders? || current_user.role_finance? if current_user.role_orders? || current_user.role_finance?
redirect_to @order redirect_to @order
elsif current_user.role_pickup? 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) subject: I18n.t('mailer.order_result.subject', name: group_order.order.name)
end 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 # Sends order result to the supplier
def order_result_supplier(user, order, options = {}) def order_result_supplier(user, order, options = {})
@user = user @user = user

View file

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

@ -43,6 +43,7 @@
= s.simple_fields_for :notify, defaults: { inline_label: true, label: false } do |notify| = 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_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 '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'] } = 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
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, # 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. # a message is sent to all users who have enabled notification.
def self.negative_balance(args) def self.negative_balance(args)

View file

@ -1283,6 +1283,18 @@ de:
Viele Grüße von %{foodcoop} Viele Grüße von %{foodcoop}
order_received:
subject: 'Bestellung entgegengenommen: %{name}'
text0: |
Liebe Bestellgruppe %{ordergroup},
die Bestellung für "%{order}" wurde geliefert.
abundant_articles: Zuviel geliefert
scarce_articles: Zuwenig geliefert
article_details: |
o %{name}:
-- Bestellt: %{ordered} x %{unit}
-- Geliefert: %{received} x %{unit}
order_result_supplier: order_result_supplier:
subject: Neue Bestellung für %{name} subject: Neue Bestellung für %{name}
text: | text: |
@ -1608,6 +1620,7 @@ de:
notify: notify:
negative_balance: Informiere mich, falls meine Bestellgruppe ins Minus rutscht. negative_balance: Informiere mich, falls meine Bestellgruppe ins Minus rutscht.
order_finished: Informiere mich über meine Bestellergebnisse (nach Ende der Bestellung). order_finished: Informiere mich über meine Bestellergebnisse (nach Ende der Bestellung).
order_received: Informiere mich über Lieferdetails (nachdem die Ware entgegengenommen wurde).
upcoming_tasks: Erinnere mich an anstehende Aufgaben. upcoming_tasks: Erinnere mich an anstehende Aufgaben.
profile: profile:
email_is_public: E-Mail ist für Mitglieder sichtbar. email_is_public: E-Mail ist für Mitglieder sichtbar.

View file

@ -1294,6 +1294,18 @@ en:
Kind regards from %{foodcoop}. Kind regards from %{foodcoop}.
order_received:
subject: 'Order delivery registered: %{name}'
text0: |
Dear %{ordergroup},
the order for "%{order}" has been received.
abundant_articles: Received too much
scarce_articles: Received too little
article_details: |
o %{name}:
-- Ordered: %{ordered} x %{unit}
-- Received: %{received} x %{unit}
order_result_supplier: order_result_supplier:
subject: New order for %{name} subject: New order for %{name}
text: | text: |
@ -1630,6 +1642,7 @@ en:
notify: notify:
negative_balance: Inform me when my ordergroup has a negative balance. negative_balance: Inform me when my ordergroup has a negative balance.
order_finished: Inform me about my order result (when the order is closed). order_finished: Inform me about my order result (when the order is closed).
order_received: Inform me about delivery details (after order has been received).
upcoming_tasks: Remind me of upcoming tasks. upcoming_tasks: Remind me of upcoming tasks.
profile: profile:
email_is_public: Email is visible for other members. email_is_public: Email is visible for other members.

View file

@ -33,6 +33,20 @@ describe OrderArticle do
expect(oa.units).to eq oa.units_received expect(oa.units).to eq oa.units_received
end end
it 'calculates the difference of received articles to ordered articles' do
oa.units_received = 10
oa.units_to_order = 2
expect(oa.difference_received_ordered).to eq(8)
oa.units_received = 2
oa.units_to_order = 10
expect(oa.difference_received_ordered).to eq(-8)
oa.units_received = nil
oa.units_to_order = 10
expect(oa.difference_received_ordered).to eq(-10)
end
describe 'redistribution' do describe 'redistribution' do
let(:admin) { create :user, groups:[create(:workgroup, role_finance: true)] } let(:admin) { create :user, groups:[create(:workgroup, role_finance: true)] }
let(:article) { create :article, unit_quantity: 3 } let(:article) { create :article, unit_quantity: 3 }