Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to automatically finish an order #495

Merged
merged 1 commit into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ def finish
# 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't forget to change this!

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)
Expand Down
30 changes: 30 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -263,6 +265,34 @@ def close_direct!(user)
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
Expand Down
3 changes: 3 additions & 0 deletions app/views/orders/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 6 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be that this task takes more than a minute to run? Would this interfere? Just to check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's very unlikely that it takes so long (but i'll address this problem with the new time zone aware scheduler anyway later). it it really takes that long finish! will fail, because the order is already finished and nothing happens

5 changes: 5 additions & 0 deletions db/migrate/20171001020000_add_end_action_to_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddEndActionToOrder < ActiveRecord::Migration
def change
add_column :orders, :end_action, :integer, default: 0, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -244,6 +244,7 @@
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
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/foodsoft.rake
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 14 additions & 0 deletions spec/models/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

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
Expand Down