Skip to content

Commit

Permalink
#779 Received state for orders (PR #789, #779)
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi authored Feb 3, 2021
1 parent 67ad202 commit 085562d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/controllers/finance/invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def form_on_supplier_id_change

def fill_deliveries_and_orders_collection(invoice_id, supplier_id)
@deliveries_collection = Delivery.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ?)', invoice_id, supplier_id).order(:date)
@orders_collection = Order.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ? AND state = ?)', invoice_id, supplier_id, 'finished').order(:ends)
@orders_collection = Order.where('invoice_id = ? OR (invoice_id IS NULL AND supplier_id = ? AND state IN (?))', invoice_id, supplier_id, %w[finished received]).order(:ends)
end

def create
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ def receive
unless request.post?
@order_articles = @order.order_articles.ordered_or_member.includes(:article).order('articles.order_number, articles.name')
else
s = update_order_amounts
flash[:notice] = (s ? I18n.t('orders.receive.notice', :msg => s) : I18n.t('orders.receive.notice_none'))
Order.transaction do
s = update_order_amounts
@order.update_attribute(:state, 'received') if @order.state != 'received'

flash[:notice] = (s ? I18n.t('orders.receive.notice', :msg => s) : I18n.t('orders.receive.notice_none'))
end
if current_user.role_orders? || current_user.role_finance?
redirect_to @order
elsif current_user.role_pickup?
Expand Down
19 changes: 15 additions & 4 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@ class Order < ApplicationRecord

# Finders
scope :started, -> { where('starts <= ?', Time.now) }
scope :open, -> { where(state: 'open').order('ends DESC') }
scope :finished, -> { where("orders.state = 'finished' OR orders.state = 'closed'").order('ends DESC') }
scope :finished_not_closed, -> { where(state: 'finished').order('ends DESC') }
scope :closed, -> { where(state: 'closed').order('ends DESC') }
scope :stockit, -> { where(supplier_id: nil).order('ends DESC') }
scope :recent, -> { order('starts DESC').limit(10) }
scope :stock_group_order, -> { group_orders.where(ordergroup_id: nil).first }
scope :with_invoice, -> { where.not(invoice: nil) }

# State related finders
# Diagram for `Order.state` looks like this:
# * -> open -> finished (-> received) -> closed
# So orders can
# 1. ...only transition in one direction (e.g. an order that has been `finished` currently cannot be reopened)
# 2. ...be set to `closed` when having the `finished` state. (`received` is optional)
scope :open, -> { where(state: 'open').order('ends DESC') }
scope :finished, -> { where(state: %w[finished received closed]).order('ends DESC') }
scope :finished_not_closed, -> { where(state: %w[finished received]).order('ends DESC') }

# Allow separate inputs for date and time
# with workaround for https://github.com/einzige/date_time_attribute/issues/14
include DateTimeAttributeValidate
Expand Down Expand Up @@ -92,7 +99,11 @@ def open?
end

def finished?
state == "finished"
state == "finished" || state == "received"
end

def received?
state == "received"
end

def closed?
Expand Down
2 changes: 1 addition & 1 deletion app/models/stock_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def quantity_available

def quantity_ordered
OrderArticle.where(article_id: id).
joins(:order).where(orders: {state: ['open', 'finished']}).sum(:units_to_order)
joins(:order).where(orders: {state: %w[open finished received]}).sum(:units_to_order)
end

def quantity_history
Expand Down

0 comments on commit 085562d

Please sign in to comment.