parent
67ad202859
commit
085562dbf4
4 changed files with 23 additions and 8 deletions
|
@ -33,7 +33,7 @@ class Finance::InvoicesController < ApplicationController
|
||||||
|
|
||||||
def fill_deliveries_and_orders_collection(invoice_id, supplier_id)
|
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)
|
@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
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|
|
@ -132,8 +132,12 @@ class OrdersController < ApplicationController
|
||||||
unless request.post?
|
unless request.post?
|
||||||
@order_articles = @order.order_articles.ordered_or_member.includes(:article).order('articles.order_number, articles.name')
|
@order_articles = @order.order_articles.ordered_or_member.includes(:article).order('articles.order_number, articles.name')
|
||||||
else
|
else
|
||||||
|
Order.transaction do
|
||||||
s = update_order_amounts
|
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'))
|
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?
|
if current_user.role_orders? || current_user.role_finance?
|
||||||
redirect_to @order
|
redirect_to @order
|
||||||
elsif current_user.role_pickup?
|
elsif current_user.role_pickup?
|
||||||
|
|
|
@ -30,15 +30,22 @@ class Order < ApplicationRecord
|
||||||
|
|
||||||
# Finders
|
# Finders
|
||||||
scope :started, -> { where('starts <= ?', Time.now) }
|
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 :closed, -> { where(state: 'closed').order('ends DESC') }
|
||||||
scope :stockit, -> { where(supplier_id: nil).order('ends DESC') }
|
scope :stockit, -> { where(supplier_id: nil).order('ends DESC') }
|
||||||
scope :recent, -> { order('starts DESC').limit(10) }
|
scope :recent, -> { order('starts DESC').limit(10) }
|
||||||
scope :stock_group_order, -> { group_orders.where(ordergroup_id: nil).first }
|
scope :stock_group_order, -> { group_orders.where(ordergroup_id: nil).first }
|
||||||
scope :with_invoice, -> { where.not(invoice: nil) }
|
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
|
# Allow separate inputs for date and time
|
||||||
# with workaround for https://github.com/einzige/date_time_attribute/issues/14
|
# with workaround for https://github.com/einzige/date_time_attribute/issues/14
|
||||||
include DateTimeAttributeValidate
|
include DateTimeAttributeValidate
|
||||||
|
@ -92,7 +99,11 @@ class Order < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def finished?
|
def finished?
|
||||||
state == "finished"
|
state == "finished" || state == "received"
|
||||||
|
end
|
||||||
|
|
||||||
|
def received?
|
||||||
|
state == "received"
|
||||||
end
|
end
|
||||||
|
|
||||||
def closed?
|
def closed?
|
||||||
|
|
|
@ -21,7 +21,7 @@ class StockArticle < Article
|
||||||
|
|
||||||
def quantity_ordered
|
def quantity_ordered
|
||||||
OrderArticle.where(article_id: id).
|
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
|
end
|
||||||
|
|
||||||
def quantity_history
|
def quantity_history
|
||||||
|
|
Loading…
Reference in a new issue