Add stock group order

This allows us to add additional items to an order, which do not belong
a specific user, but will be put into stock. The benefit of this change
is that we use the same order for ordergroups and stock.
This commit is contained in:
Patrick Gansterer 2016-02-25 02:27:17 +01:00 committed by wvengen
parent b56df39623
commit 9c4d9d5c20
8 changed files with 42 additions and 11 deletions

View file

@ -10,7 +10,6 @@ class GroupOrder < ActiveRecord::Base
belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by_user_id"
validates_presence_of :order_id
validates_presence_of :ordergroup_id
validates_numericality_of :price
validates_uniqueness_of :ordergroup_id, :scope => :order_id # order groups can only order once per order
@ -22,7 +21,7 @@ class GroupOrder < ActiveRecord::Base
# Generate some data for the javascript methods in ordering view
def load_data
data = {}
data[:available_funds] = ordergroup.get_available_funds(self)
data[:available_funds] = ordergroup.nil? ? BigDecimal.new('+Infinity') : ordergroup.get_available_funds(self)
# load prices and other stuff....
data[:order_articles] = {}
@ -87,4 +86,8 @@ class GroupOrder < ActiveRecord::Base
end
end
def ordergroup_name
ordergroup ? ordergroup.name : I18n.t('model.group_order.stock_ordergroup_name', :user => updated_by.name)
end
end

View file

@ -31,6 +31,7 @@ class Order < ActiveRecord::Base
scope :closed, -> { where(state: 'closed').order('ends DESC') }
scope :stockit, -> { where(supplier_id: 0).order('ends DESC') }
scope :recent, -> { order('starts DESC').limit(10) }
scope :stock_group_order, -> { group_orders.where(ordergroup_id: nil).first }
# Allow separate inputs for date and time
# with workaround for https://github.com/einzige/date_time_attribute/issues/14
@ -125,6 +126,10 @@ class Order < ActiveRecord::Base
group_orders.where(:ordergroup_id => ordergroup.id).first
end
def stock_group_order
group_orders.where(:ordergroup_id => nil).first
end
# Returns OrderArticles in a nested Array, grouped by category and ordered by article name.
# The array has the following form:
# e.g: [["drugs",[teethpaste, toiletpaper]], ["fruits" => [apple, banana, lemon]]]
@ -234,8 +239,10 @@ class Order < ActiveRecord::Base
transaction do # Start updating account balances
for group_order in gos
price = group_order.price * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, transaction_note, user)
if group_order.ordergroup
price = group_order.price * -1 # decrease! account balance
group_order.ordergroup.add_financial_transaction!(price, transaction_note, user)
end
end
if stockit? # Decreases the quantity of stock_articles