2009-01-14 12:46:01 +01:00
|
|
|
# A GroupOrder represents an Order placed by an Ordergroup.
|
2019-01-13 07:05:54 +01:00
|
|
|
class GroupOrder < ApplicationRecord
|
2016-06-04 21:37:47 +02:00
|
|
|
include FindEachWithOrder
|
2011-06-19 15:30:33 +02:00
|
|
|
|
|
|
|
attr_accessor :group_order_articles_attributes
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :order
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :ordergroup, optional: true
|
2009-01-06 11:49:19 +01:00
|
|
|
has_many :group_order_articles, :dependent => :destroy
|
|
|
|
has_many :order_articles, :through => :group_order_articles
|
2019-11-04 04:33:43 +01:00
|
|
|
has_one :financial_transaction
|
2020-08-01 02:49:15 +02:00
|
|
|
belongs_to :updated_by, optional: true, class_name: 'User', foreign_key: 'updated_by_user_id'
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
validates_presence_of :order_id
|
|
|
|
validates_numericality_of :price
|
2021-03-01 15:27:26 +01:00
|
|
|
validates_uniqueness_of :ordergroup_id, :scope => :order_id # order groups can only order once per order
|
2009-01-06 11:49:19 +01:00
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
scope :in_open_orders, -> { joins(:order).merge(Order.open) }
|
|
|
|
scope :in_finished_orders, -> { joins(:order).merge(Order.finished_not_closed) }
|
2019-01-28 06:08:04 +01:00
|
|
|
scope :stock, -> { where(ordergroup: 0) }
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2014-02-20 15:04:53 +01:00
|
|
|
scope :ordered, -> { includes(:ordergroup).order('groups.name') }
|
2013-09-17 00:25:38 +02:00
|
|
|
|
2018-10-13 16:27:24 +02:00
|
|
|
def self.ransackable_attributes(auth_object = nil)
|
|
|
|
%w(id price)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ransackable_associations(auth_object = nil)
|
|
|
|
%w(order group_order_articles)
|
|
|
|
end
|
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
# Generate some data for the javascript methods in ordering view
|
|
|
|
def load_data
|
|
|
|
data = {}
|
2017-10-29 20:44:05 +01:00
|
|
|
data[:account_balance] = ordergroup.nil? ? BigDecimal.new('+Infinity') : ordergroup.account_balance
|
2016-02-25 02:27:17 +01:00
|
|
|
data[:available_funds] = ordergroup.nil? ? BigDecimal.new('+Infinity') : ordergroup.get_available_funds(self)
|
2011-06-19 15:30:33 +02:00
|
|
|
|
2023-02-15 16:06:12 +01:00
|
|
|
|
2011-06-19 19:56:04 +02:00
|
|
|
# load prices and other stuff....
|
2011-06-19 15:30:33 +02:00
|
|
|
data[:order_articles] = {}
|
2013-01-26 15:18:35 +01:00
|
|
|
order.articles_grouped_by_category.each do |article_category, order_articles|
|
|
|
|
order_articles.each do |order_article|
|
2013-03-12 11:26:14 +01:00
|
|
|
# Get the result of last time ordering, if possible
|
|
|
|
goa = group_order_articles.detect { |goa| goa.order_article_id == order_article.id }
|
|
|
|
|
|
|
|
# Build hash with relevant data
|
2013-01-26 15:18:35 +01:00
|
|
|
data[:order_articles][order_article.id] = {
|
2021-03-01 15:27:26 +01:00
|
|
|
:price => order_article.article.fc_price,
|
|
|
|
:unit => order_article.article.unit_quantity,
|
|
|
|
:quantity => (goa ? goa.quantity : 0),
|
|
|
|
:others_quantity => order_article.quantity - (goa ? goa.quantity : 0),
|
|
|
|
:used_quantity => (goa ? goa.result(:quantity) : 0),
|
|
|
|
:tolerance => (goa ? goa.tolerance : 0),
|
|
|
|
:others_tolerance => order_article.tolerance - (goa ? goa.tolerance : 0),
|
|
|
|
:used_tolerance => (goa ? goa.result(:tolerance) : 0),
|
|
|
|
:total_price => (goa ? goa.total_price : 0),
|
|
|
|
:missing_units => order_article.missing_units,
|
|
|
|
:quantity_available => (order.stockit? ? order_article.article.quantity_available : 0)
|
2013-01-26 15:18:35 +01:00
|
|
|
}
|
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
end
|
|
|
|
|
2023-02-15 16:06:12 +01:00
|
|
|
# add counts from the previous group order
|
|
|
|
if previous_group_order
|
|
|
|
previous_group_order.group_order_articles.each do |goa|
|
2023-02-15 16:30:45 +01:00
|
|
|
order_article_id = OrderArticle.find_by(order: order, article: goa.order_article.article)&.id
|
|
|
|
puts " order ID: #{order.id}"
|
|
|
|
puts " article ID: #{goa.order_article.article.id}"
|
|
|
|
puts "ID: #{order_article_id}"
|
2023-02-15 16:06:12 +01:00
|
|
|
data[:order_articles][order_article_id] ||= {}
|
|
|
|
data[:order_articles][order_article_id][:previous_quantity] = goa.quantity
|
|
|
|
data[:order_articles][order_article_id][:previous_tolerance] = goa.tolerance
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
puts data
|
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2023-02-15 16:06:12 +01:00
|
|
|
def previous_group_order
|
2023-02-15 16:30:45 +01:00
|
|
|
previous_order = ordergroup.orders.where(supplier: order.supplier).where.not(id: order.id).recent.first
|
2023-02-15 16:06:12 +01:00
|
|
|
return nil unless previous_order
|
|
|
|
|
|
|
|
ordergroup.group_orders.find_by(order_id: previous_order.id)
|
|
|
|
end
|
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
def save_group_order_articles
|
|
|
|
for order_article in order.order_articles
|
|
|
|
# Find the group_order_article, create a new one if necessary...
|
2014-02-20 15:04:53 +01:00
|
|
|
group_order_article = group_order_articles.where(order_article_id: order_article.id).first_or_create
|
2011-06-19 15:30:33 +02:00
|
|
|
|
|
|
|
# Get ordered quantities and update group_order_articles/_quantities...
|
2015-09-23 22:38:20 +02:00
|
|
|
if group_order_articles_attributes
|
2021-03-01 15:27:26 +01:00
|
|
|
quantities = group_order_articles_attributes.fetch(order_article.id.to_s, { :quantity => 0, :tolerance => 0 })
|
2015-09-23 22:38:20 +02:00
|
|
|
group_order_article.update_quantities(quantities[:quantity].to_i, quantities[:tolerance].to_i)
|
|
|
|
end
|
2011-06-19 15:30:33 +02:00
|
|
|
|
|
|
|
# Also update results for the order_article
|
|
|
|
logger.debug "[save_group_order_articles] update order_article.results!"
|
|
|
|
order_article.update_results!
|
|
|
|
end
|
|
|
|
|
|
|
|
# set attributes to nil to avoid and infinite loop of
|
|
|
|
end
|
|
|
|
|
|
|
|
# Updates the "price" attribute.
|
|
|
|
def update_price!
|
2012-12-16 21:46:24 +01:00
|
|
|
total = group_order_articles.includes(:order_article => [:article, :article_price]).to_a.sum(&:total_price)
|
2009-01-29 01:57:51 +01:00
|
|
|
update_attribute(:price, total)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
2011-06-19 15:30:33 +02:00
|
|
|
# Save GroupOrder and updates group_order_articles/quantities accordingly
|
|
|
|
def save_ordering!
|
|
|
|
transaction do
|
|
|
|
save!
|
|
|
|
save_group_order_articles
|
|
|
|
update_price!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-25 02:27:17 +01:00
|
|
|
def ordergroup_name
|
2016-08-12 15:56:45 +02:00
|
|
|
ordergroup ? ordergroup.name : I18n.t('model.group_order.stock_ordergroup_name', :user => updated_by.try(:name) || '?')
|
2016-02-25 02:27:17 +01:00
|
|
|
end
|
|
|
|
|
2020-03-19 00:22:20 +01:00
|
|
|
def total
|
|
|
|
return price + transport if transport
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2020-03-19 00:22:20 +01:00
|
|
|
price
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|