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'

View file

@ -82,6 +82,12 @@ de:
boxfill: Kistenfüllen ab
closed_by: Abgerechnet von
created_by: Erstellt von
end_action: Endeaktion
end_actions:
auto_close: Bestellung beenden
auto_close_and_send: Bestellung beenden und an Lieferantin schicken
auto_close_and_send_min_quantity: Bestellung beenden und an Lieferantin schicken sofern die Mindestbestellmenge erreicht wurde
no_end_action: Keine automatische Aktion
ends: Endet am
name: Lieferant
note: Notiz

View file

@ -82,6 +82,12 @@ en:
boxfill: Fill boxes after
closed_by: Settled by
created_by: Created by
end_action: End action
end_actions:
auto_close: Close the order
auto_close_and_send: Close the order and send it to the supplier
auto_close_and_send_min_quantity: Close the order and send it to the supplier if the minimum quantity has been reached
no_end_action: No automatic action
ends: Ends at
name: Supplier
note: Note

View file

@ -11,3 +11,8 @@ end
every :sunday, :at => '7:14 am' do
rake "multicoops:run TASK=foodsoft:create_upcoming_periodic_tasks"
end
# Finish ended orders
every 1.minute do
rake "multicoops:run TASK=foodsoft:finish_ended_orders"
end

View file

@ -0,0 +1,5 @@
class AddEndActionToOrder < ActiveRecord::Migration
def change
add_column :orders, :end_action, :integer, default: 0, null: false
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171001000000) do
ActiveRecord::Schema.define(version: 20171001020000) do
create_table "article_categories", force: :cascade do |t|
t.string "name", limit: 255, default: "", null: false
@ -253,6 +253,7 @@ ActiveRecord::Schema.define(version: 20171001000000) do
t.date "pickup"
t.integer "invoice_id"
t.datetime "last_sent_mail"
t.integer "end_action", default: 0, null: false
end
add_index "orders", ["state"], name: "index_orders_on_state", using: :btree

View file

@ -1,6 +1,11 @@
# put in here all foodsoft tasks
# => :environment loads the environment an gives easy access to the application
namespace :foodsoft do
desc "Finish ended orders"
task :finish_ended_orders => :environment do
Order.finish_ended!
end
desc "Notify users of upcoming tasks"
task :notify_upcoming_tasks => :environment do
tasks = Task.where(done: false, due_date: 1.day.from_now.to_date)

View file

@ -2,6 +2,20 @@ require_relative '../spec_helper'
describe Order do
it 'automaticly finishes ended' do
create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.from_now
create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.ago
create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.from_now, end_action: :auto_close
order = create :order, created_by: User.first, starts: Date.yesterday, ends: 1.hour.ago, end_action: :auto_close
Order.finish_ended!
order.reload
expect(Order.open.count).to eq 3
expect(Order.finished.count).to eq 1
expect(order).to be_finished
end
it 'needs a supplier' do
expect(build(:order, supplier: nil)).to be_invalid
end