Add transport costs to Order
This commit is contained in:
parent
8cb70d819e
commit
4c567fece1
16 changed files with 141 additions and 6 deletions
|
|
@ -1,7 +1,7 @@
|
|||
# encoding: utf-8
|
||||
#
|
||||
class Order < ApplicationRecord
|
||||
attr_accessor :ignore_warnings
|
||||
attr_accessor :ignore_warnings, :transport_distribution
|
||||
|
||||
# Associations
|
||||
has_many :order_articles, :dependent => :destroy
|
||||
|
|
@ -17,6 +17,7 @@ class Order < ApplicationRecord
|
|||
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id'
|
||||
|
||||
enum end_action: { no_end_action: 0, auto_close: 1, auto_close_and_send: 2, auto_close_and_send_min_quantity: 3 }
|
||||
enum transport_distribution: [:skip, :ordergroup, :price, :articles]
|
||||
|
||||
# Validations
|
||||
validates_presence_of :starts
|
||||
|
|
@ -25,6 +26,7 @@ class Order < ApplicationRecord
|
|||
|
||||
# Callbacks
|
||||
after_save :save_order_articles, :update_price_of_group_orders
|
||||
before_validation :distribute_transport
|
||||
|
||||
# Finders
|
||||
scope :started, -> { where('starts <= ?', Time.now) }
|
||||
|
|
@ -258,7 +260,7 @@ class Order < ApplicationRecord
|
|||
transaction do # Start updating account balances
|
||||
for group_order in gos
|
||||
if group_order.ordergroup
|
||||
price = group_order.price * -1 # decrease! account balance
|
||||
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
|
||||
|
|
@ -347,6 +349,27 @@ class Order < ApplicationRecord
|
|||
|
||||
private
|
||||
|
||||
def distribute_transport
|
||||
return unless group_orders.any?
|
||||
case transport_distribution.try(&:to_i)
|
||||
when Order.transport_distributions[:ordergroup] then
|
||||
amount = transport / group_orders.size
|
||||
group_orders.each do |go|
|
||||
go.transport = amount.ceil(2)
|
||||
end
|
||||
when Order.transport_distributions[:price] then
|
||||
amount = transport / group_orders.sum(:price)
|
||||
group_orders.each do |go|
|
||||
go.transport = (amount * go.price).ceil(2)
|
||||
end
|
||||
when Order.transport_distributions[:articles] then
|
||||
amount = transport / group_orders.includes(:group_order_articles).sum(:result)
|
||||
group_orders.each do |go|
|
||||
go.transport = (amount * go.group_order_articles.sum(:result)).ceil(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# 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.
|
||||
def update_price_of_group_orders
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue