Skip to content

Commit

Permalink
Closes foodcoops#779
Browse files Browse the repository at this point in the history
  • Loading branch information
lentschi committed Jan 31, 2021
1 parent 67ad202 commit ff8e4eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 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
10 changes: 7 additions & 3 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ 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 :finished, -> { where(state: %w[finished received closed]).order('ends DESC') }
scope :finished_not_closed, -> { where(state: %w[finished received]).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) }
Expand Down Expand Up @@ -92,7 +92,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 ff8e4eb

Please sign in to comment.