Add option to send mails when an order has been received

pull/26/head
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)

View File

@ -1283,6 +1283,18 @@ de:
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:
subject: Neue Bestellung für %{name}
text: |
@ -1608,6 +1620,7 @@ de:
notify:
negative_balance: Informiere mich, falls meine Bestellgruppe ins Minus rutscht.
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.
profile:
email_is_public: E-Mail ist für Mitglieder sichtbar.

View File

@ -1294,6 +1294,18 @@ en:
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:
subject: New order for %{name}
text: |
@ -1630,6 +1642,7 @@ en:
notify:
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_received: Inform me about delivery details (after order has been received).
upcoming_tasks: Remind me of upcoming tasks.
profile:
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
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
let(:admin) { create :user, groups:[create(:workgroup, role_finance: true)] }
let(:article) { create :article, unit_quantity: 3 }