Add an option to automatically finish an order

This commit is contained in:
Patrick Gansterer 2017-10-12 00:20:30 +02:00
parent c3927e4013
commit 564492afe4
10 changed files with 77 additions and 3 deletions

View file

@ -117,8 +117,7 @@ class OrdersController < ApplicationController
# Send a order to the supplier.
def send_result_to_supplier
order = Order.find(params[:id])
Mailer.order_result_supplier(@current_user, order).deliver_now
order.update!(last_sent_mail: Time.now)
order.send_to_supplier!(@current_user)
redirect_to order, notice: I18n.t('orders.send_to_supplier.notice')
rescue => error
redirect_to order, alert: I18n.t('errors.general_msg', :msg => error.message)

View file

@ -16,6 +16,8 @@ class Order < ActiveRecord::Base
belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_user_id'
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
enum end_action: { no_end_action: 0, auto_close: 1, auto_close_and_send: 2, auto_close_and_send_min_quantity: 3 }
# Validations
validates_presence_of :starts
validate :starts_before_ends, :include_articles
@ -263,6 +265,34 @@ class Order < ActiveRecord::Base
update_attributes! state: 'closed', updated_by: user
end
def send_to_supplier!(user)
Mailer.order_result_supplier(user, self).deliver_now
update!(last_sent_mail: Time.now)
end
def do_end_action!
if auto_close?
finish!(created_by)
elsif auto_close_and_send?
finish!(created_by)
send_to_supplier!(created_by)
elsif auto_close_and_send_min_quantity?
finish!(created_by)
send_to_supplier!(created_by) if order.sum >= order.supplier.min_order_quantity
end
end
def self.finish_ended!
orders = Order.where.not(end_action: Order.end_actions[:no_end_action]).where(state: 'open').where('ends <= ?', DateTime.now)
orders.each do |order|
begin
order.do_end_action!
rescue => error
ExceptionNotifier.notify_exception(error, data: {order_id: order.id})
end
end
end
protected
def starts_before_ends

View file

@ -5,6 +5,9 @@
= f.input :boxfill, as: :date_picker_time if @order.is_boxfill_useful?
= f.input :ends, as: :date_picker_time
= f.input :pickup, as: :date_picker, input_html: {class: 'input-small'}
= f.input :end_action, collection: Order.end_actions,include_blank: false,
input_html: {class: 'input-xxlarge'}, value_method: ->(k){ k.first },
label_method: ->(k){ t("activerecord.attributes.order.end_actions.#{k.first}") }
= f.input :note, input_html: {rows: 2, class: 'input-xxlarge'}
%h2= t '.title'