parent
67ad202859
commit
085562dbf4
4 changed files with 23 additions and 8 deletions
|
|
@ -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
|
||||
|
|
@ -92,7 +99,11 @@ class Order < ApplicationRecord
|
|||
end
|
||||
|
||||
def finished?
|
||||
state == "finished"
|
||||
state == "finished" || state == "received"
|
||||
end
|
||||
|
||||
def received?
|
||||
state == "received"
|
||||
end
|
||||
|
||||
def closed?
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class StockArticle < Article
|
|||
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue