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

#779 Received state for orders #789

Merged
merged 3 commits into from
Feb 3, 2021
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
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