Split order close method (PR #829)

This commit is contained in:
wvengen 2021-02-17 18:42:52 +01:00 committed by GitHub
parent 69732cca0d
commit f7c7b56bd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,7 +25,7 @@ class Order < ApplicationRecord
validate :keep_ordered_articles validate :keep_ordered_articles
# Callbacks # Callbacks
after_save :save_order_articles, :update_price_of_group_orders after_save :save_order_articles, :update_price_of_group_orders!
before_validation :distribute_transport before_validation :distribute_transport
# Finders # Finders
@ -270,19 +270,11 @@ class Order < ApplicationRecord
# Sets order.status to 'close' and updates all Ordergroup.account_balances # Sets order.status to 'close' and updates all Ordergroup.account_balances
def close!(user, transaction_type = nil) def close!(user, transaction_type = nil)
raise I18n.t('orders.model.error_closed') if closed? raise I18n.t('orders.model.error_closed') if closed?
transaction_note = I18n.t('orders.model.notice_close', :name => name,
:ends => ends.strftime(I18n.t('date.formats.default')))
gos = group_orders.includes(:ordergroup) # Fetch group_orders update_price_of_group_orders!
gos.each { |group_order| group_order.update_price! } # Update prices of group_orders
transaction do # Start updating account balances transaction do # Start updating account balances
for group_order in gos charge_group_orders!(user, transaction_type)
if group_order.ordergroup
price = group_order.total * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, transaction_note, user, transaction_type, nil, group_order)
end
end
if stockit? # Decreases the quantity of stock_articles if stockit? # Decreases the quantity of stock_articles
for oa in order_articles.includes(:article) for oa in order_articles.includes(:article)
@ -391,8 +383,21 @@ class Order < ApplicationRecord
# Updates the "price" attribute of GroupOrders or GroupOrderResults # Updates the "price" attribute of GroupOrders or GroupOrderResults
# This will be either the maximum value of a current order or the actual order value of a finished order. # This will be either the maximum value of a current order or the actual order value of a finished order.
def update_price_of_group_orders def update_price_of_group_orders!
group_orders.each { |group_order| group_order.update_price! } group_orders.each(&:update_price!)
end end
def charge_group_orders!(user, transaction_type = nil)
note = transaction_note
group_orders.includes(:ordergroup).each do |group_order|
if group_order.ordergroup
price = group_order.price * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, note, user, transaction_type, nil, group_order)
end
end
end
def transaction_note
I18n.t('orders.model.notice_close', name: name, ends: ends.strftime(I18n.t('date.formats.default')))
end
end end