2014-06-18 13:28:19 +02:00
|
|
|
# ActionMailer class that handles all emails for Foodsoft.
|
2011-05-11 10:53:18 +02:00
|
|
|
class Mailer < ActionMailer::Base
|
2013-09-22 14:38:56 +02:00
|
|
|
# XXX Quick fix to allow the use of show_user. Proper take would be one of
|
|
|
|
# (1) Use draper, decorate user
|
|
|
|
# (2) Create a helper with this method, include here and in ApplicationHelper
|
|
|
|
helper :application
|
|
|
|
include ApplicationHelper
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
layout 'email' # Use views/layouts/email.txt.erb
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2017-08-18 00:03:25 +02:00
|
|
|
default from: "#{I18n.t('layouts.foodsoft')} <#{FoodsoftConfig[:email_sender]}>",
|
|
|
|
'X-Auto-Response-Suppress' => 'All'
|
|
|
|
|
2011-05-11 10:53:18 +02:00
|
|
|
# Sends an email with instructions on how to reset the password.
|
|
|
|
# Assumes user.setResetPasswordToken has been successfully called already.
|
2012-12-23 17:38:04 +01:00
|
|
|
def reset_password(user)
|
|
|
|
@user = user
|
2012-10-08 11:51:56 +02:00
|
|
|
@link = new_password_url(id: @user.id, token: @user.reset_password_token)
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2017-08-18 00:21:52 +02:00
|
|
|
mail to: user,
|
2017-08-18 00:11:36 +02:00
|
|
|
subject: I18n.t('mailer.reset_password.subject', username: show_user(user))
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
2017-08-18 00:03:25 +02:00
|
|
|
|
2011-05-11 10:53:18 +02:00
|
|
|
# Sends an invite email.
|
2012-12-23 17:38:04 +01:00
|
|
|
def invite(invite)
|
|
|
|
@invite = invite
|
2012-10-08 11:51:56 +02:00
|
|
|
@link = accept_invitation_url(token: @invite.token)
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2017-08-18 00:21:52 +02:00
|
|
|
mail to: invite.email,
|
2017-08-18 00:11:36 +02:00
|
|
|
subject: I18n.t('mailer.invite.subject')
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Notify user of upcoming task.
|
|
|
|
def upcoming_tasks(user, task)
|
|
|
|
@user = user
|
|
|
|
@task = task
|
2019-11-11 11:26:55 +01:00
|
|
|
@next_tasks = Task.order(:due_date).next_assigned_tasks_for(user)
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2017-08-18 00:21:52 +02:00
|
|
|
mail to: user,
|
2017-08-18 00:11:36 +02:00
|
|
|
subject: I18n.t('mailer.upcoming_tasks.subject')
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
2020-03-06 11:46:39 +01:00
|
|
|
# Sends a welcome email with instructions on how to reset the password.
|
|
|
|
# Assumes user.setResetPasswordToken has been successfully called already.
|
|
|
|
def welcome(user)
|
|
|
|
@user = user
|
|
|
|
@additional_text = additonal_welcome_text(user)
|
|
|
|
@link = new_password_url(id: @user.id, token: @user.reset_password_token)
|
|
|
|
|
|
|
|
mail to: user,
|
|
|
|
subject: I18n.t('mailer.welcome.subject')
|
|
|
|
end
|
|
|
|
|
2023-07-24 10:50:35 +02:00
|
|
|
# Sends automatically generated invoicesfor group orders to ordergroup members
|
|
|
|
def group_order_invoice(group_order_invoice, user)
|
|
|
|
@user = user
|
|
|
|
@group_order_invoice = group_order_invoice
|
|
|
|
@group_order = group_order_invoice.group_order
|
|
|
|
@supplier = @group_order.order.supplier.name
|
|
|
|
@group = @group_order.ordergroup
|
|
|
|
add_group_order_invoice_attachments(group_order_invoice)
|
|
|
|
mail to: user,
|
|
|
|
subject: I18n.t('mailer.group_order_invoice.subject', group: @group.name, supplier: @supplier)
|
|
|
|
end
|
|
|
|
|
2011-05-11 10:53:18 +02:00
|
|
|
# Sends order result for specific Ordergroup
|
|
|
|
def order_result(user, group_order)
|
|
|
|
@order = group_order.order
|
|
|
|
@group_order = group_order
|
|
|
|
|
2017-08-18 00:21:52 +02:00
|
|
|
mail to: user,
|
2017-08-18 00:11:36 +02:00
|
|
|
subject: I18n.t('mailer.order_result.subject', name: group_order.order.name)
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
2021-02-27 17:24:25 +01:00
|
|
|
# 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,
|
2021-03-01 15:27:26 +01:00
|
|
|
subject: I18n.t('mailer.order_received.subject', name: group_order.order.name)
|
2021-02-27 17:24:25 +01:00
|
|
|
end
|
|
|
|
|
2017-08-11 20:58:51 +02:00
|
|
|
# Sends order result to the supplier
|
|
|
|
def order_result_supplier(user, order, options = {})
|
|
|
|
@user = user
|
|
|
|
@order = order
|
|
|
|
@supplier = order.supplier
|
|
|
|
|
|
|
|
add_order_result_attachments order, options
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
subject = I18n.t('mailer.order_result_supplier.subject', name: order.supplier.name)
|
2017-08-11 20:58:51 +02:00
|
|
|
subject += " (#{I18n.t('activerecord.attributes.order.pickup')}: #{format_date(order.pickup)})" if order.pickup
|
|
|
|
|
|
|
|
mail to: order.supplier.email,
|
|
|
|
cc: user,
|
|
|
|
reply_to: user,
|
|
|
|
subject: subject
|
|
|
|
end
|
|
|
|
|
2011-05-11 10:53:18 +02:00
|
|
|
# Notify user if account balance is less than zero
|
2021-03-01 15:27:26 +01:00
|
|
|
def negative_balance(user, transaction)
|
2011-05-11 10:53:18 +02:00
|
|
|
@group = user.ordergroup
|
|
|
|
@transaction = transaction
|
|
|
|
|
2017-08-18 00:21:52 +02:00
|
|
|
mail to: user,
|
2017-08-18 00:11:36 +02:00
|
|
|
subject: I18n.t('mailer.negative_balance.subject')
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
2012-12-23 17:38:04 +01:00
|
|
|
def feedback(user, feedback)
|
2011-05-11 10:53:18 +02:00
|
|
|
@user = user
|
2011-05-11 15:14:39 +02:00
|
|
|
@feedback = feedback
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2021-12-28 19:40:26 +01:00
|
|
|
mail to: feedback_recipients,
|
2017-08-18 00:25:17 +02:00
|
|
|
from: user,
|
2017-08-18 00:38:26 +02:00
|
|
|
subject: I18n.t('mailer.feedback.subject')
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
2012-08-24 11:11:40 +02:00
|
|
|
def not_enough_users_assigned(task, user)
|
2011-05-11 10:53:18 +02:00
|
|
|
@task = task
|
|
|
|
@user = user
|
2012-08-24 11:11:40 +02:00
|
|
|
|
2017-08-18 00:21:52 +02:00
|
|
|
mail to: user,
|
2017-08-18 00:11:36 +02:00
|
|
|
subject: I18n.t('mailer.not_enough_users_assigned.subject', task: task.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mail(args)
|
2018-06-11 15:30:05 +02:00
|
|
|
args[:message_id] ||= "<#{Mail.random_tag}@#{default_url_options[:host]}>"
|
2017-08-18 00:11:36 +02:00
|
|
|
args[:subject] = "[#{FoodsoftConfig[:name]}] #{args[:subject]}"
|
2017-08-18 00:21:52 +02:00
|
|
|
|
2017-08-18 00:25:17 +02:00
|
|
|
if args[:from].is_a? User
|
2017-10-02 16:00:06 +02:00
|
|
|
args[:reply_to] ||= args[:from]
|
2023-05-12 13:01:12 +02:00
|
|
|
args[:from] =
|
|
|
|
format_address(FoodsoftConfig[:email_sender], I18n.t('mailer.from_via_foodsoft', name: show_user(args[:from])))
|
2017-08-18 00:25:17 +02:00
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
%i[bcc cc reply_to sender to].each do |k|
|
2017-08-18 00:21:52 +02:00
|
|
|
user = args[k]
|
2017-10-12 01:19:19 +02:00
|
|
|
args[k] = format_address(user.email, show_user(user)) if user.is_a? User
|
2017-08-18 00:21:52 +02:00
|
|
|
end
|
|
|
|
|
2017-10-02 19:06:08 +02:00
|
|
|
if contact_email = FoodsoftConfig[:contact][:email]
|
2017-10-12 01:19:19 +02:00
|
|
|
args[:reply_to] ||= format_address(contact_email, FoodsoftConfig[:name])
|
2017-10-02 19:06:08 +02:00
|
|
|
end
|
|
|
|
|
2017-08-19 16:24:02 +02:00
|
|
|
reply_email_domain = FoodsoftConfig[:reply_email_domain]
|
2017-10-09 11:47:27 +02:00
|
|
|
if reply_email_domain && !args[:return_path] && args[:to].is_a?(String)
|
2018-12-11 15:43:36 +01:00
|
|
|
address = Mail::Parsers::AddressListsParser.parse(args[:to]).addresses.first
|
2017-08-19 16:24:02 +02:00
|
|
|
args[:return_path] = "<#{FoodsoftConfig.scope}.bounce+#{address.local}=#{address.domain}@#{reply_email_domain}>"
|
|
|
|
end
|
|
|
|
|
2017-08-18 00:11:36 +02:00
|
|
|
super
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
2012-10-08 11:51:56 +02:00
|
|
|
|
2017-08-18 01:04:41 +02:00
|
|
|
def self.deliver_now_with_user_locale(user, &block)
|
|
|
|
I18n.with_locale(user.settings['profile']['language']) do
|
2023-05-12 13:01:12 +02:00
|
|
|
deliver_now(&block)
|
2017-08-18 01:04:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-23 23:00:33 +01:00
|
|
|
def self.deliver_now_with_default_locale(&block)
|
|
|
|
I18n.with_locale(FoodsoftConfig[:default_locale]) do
|
2023-05-12 13:01:12 +02:00
|
|
|
deliver_now(&block)
|
2019-01-23 23:00:33 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-18 01:04:41 +02:00
|
|
|
def self.deliver_now
|
|
|
|
message = yield
|
|
|
|
message.deliver_now
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
MailDeliveryStatus.create email: message.to[0], message: e.message
|
2017-08-18 01:04:41 +02:00
|
|
|
end
|
|
|
|
|
2017-08-11 20:58:51 +02:00
|
|
|
# separate method to allow plugins to mess with the attachments
|
|
|
|
def add_order_result_attachments(order, options = {})
|
|
|
|
attachments['order.pdf'] = OrderFax.new(order, options).to_pdf
|
|
|
|
attachments['order.csv'] = OrderCsv.new(order, options).to_csv
|
|
|
|
end
|
|
|
|
|
2023-07-24 10:50:35 +02:00
|
|
|
def add_group_order_invoice_attachments(group_order_invoice)
|
|
|
|
attachment_name = group_order_invoice.name + '.pdf'
|
|
|
|
attachments[attachment_name] = GroupOrderInvoicePdf.new(group_order_invoice.load_data_for_invoice).to_pdf
|
|
|
|
end
|
|
|
|
|
2020-03-06 11:46:39 +01:00
|
|
|
# separate method to allow plugins to mess with the text
|
2023-05-12 13:01:12 +02:00
|
|
|
def additonal_welcome_text(user); end
|
2020-03-06 11:46:39 +01:00
|
|
|
|
2017-10-12 01:19:19 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def format_address(email, name)
|
|
|
|
address = Mail::Address.new email
|
|
|
|
address.display_name = name
|
|
|
|
address.format
|
|
|
|
end
|
2021-12-28 19:40:26 +01:00
|
|
|
|
|
|
|
# use the (new) feedback_recipients option, but fallback to error_recipients for backwards compatibility
|
|
|
|
def feedback_recipients
|
|
|
|
FoodsoftConfig[:notification][:feedback_recipients] || FoodsoftConfig[:notification][:error_recipients]
|
|
|
|
end
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|