Class: GroupOrder

Inherits:
ApplicationRecord show all
Includes:
FindEachWithOrder
Defined in:
app/models/group_order.rb

Overview

A GroupOrder represents an Order placed by an Ordergroup.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#group_order_articles_attributesObject

Returns the value of attribute group_order_articles_attributes.



5
6
7
# File 'app/models/group_order.rb', line 5

def group_order_articles_attributes
  @group_order_articles_attributes
end

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



28
29
30
# File 'app/models/group_order.rb', line 28

def self.ransackable_associations(_auth_object = nil)
  %w[order group_order_articles]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



24
25
26
# File 'app/models/group_order.rb', line 24

def self.ransackable_attributes(_auth_object = nil)
  %w[id price]
end

Instance Method Details

#load_dataObject

Generate some data for the javascript methods in ordering view



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/group_order.rb', line 33

def load_data
  data = {}
  data[:account_balance] = ordergroup.nil? ? BigDecimal('+Infinity') : ordergroup.
  data[:available_funds] = ordergroup.nil? ? BigDecimal('+Infinity') : ordergroup.get_available_funds(self)

  # load prices and other stuff....
  data[:order_articles] = {}
  order.articles_grouped_by_category.each do |, order_articles|
    order_articles.each do |order_article|
      # 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
      data[:order_articles][order_article.id] = {
        price: order_article.article_version.fc_group_order_price,
        unit: order_article.article_version.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,
        ratio_group_order_unit_supplier_unit: order_article.article_version.convert_quantity(1,
                                                                                             order_article.article_version.supplier_order_unit, order_article.article_version.group_order_unit),
        quantity_available: (order.stockit? ? order_article.article_version.article.quantity_available : 0),
        minimum_order_quantity: if order_article.article_version.minimum_order_quantity
                                  order_article.article_version.convert_quantity(
                                    order_article.article_version.minimum_order_quantity, order_article.article_version.supplier_order_unit, order_article.article_version.group_order_unit
                                  )
                                end
      }
    end
  end

  data
end

#ordergroup_nameObject



106
107
108
# File 'app/models/group_order.rb', line 106

def ordergroup_name
  ordergroup ? ordergroup.name : I18n.t('model.group_order.stock_ordergroup_name', user: updated_by.try(:name) || '?')
end

#save_group_order_articlesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/group_order.rb', line 72

def save_group_order_articles
  for order_article in order.order_articles
    # Find the group_order_article, create a new one if necessary...
    group_order_article = group_order_articles.where(order_article_id: order_article.id).first_or_create

    # Get ordered quantities and update group_order_articles/_quantities...
    if group_order_articles_attributes
      quantities = group_order_articles_attributes.fetch(order_article.id.to_s, { quantity: 0, tolerance: 0 })
      group_order_article.update_quantities(quantities[:quantity].to_f, quantities[:tolerance].to_f)
    end

    # 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

#save_ordering!Object

Save GroupOrder and updates group_order_articles/quantities accordingly



98
99
100
101
102
103
104
# File 'app/models/group_order.rb', line 98

def save_ordering!
  transaction do
    save!
    save_group_order_articles
    update_price!
  end
end

#totalObject



110
111
112
113
114
# File 'app/models/group_order.rb', line 110

def total
  return price + transport if transport

  price
end

#update_price!Object

Updates the “price” attribute.



92
93
94
95
# File 'app/models/group_order.rb', line 92

def update_price!
  total = group_order_articles.includes(order_article: :article_version).to_a.sum(&:total_price)
  update_attribute(:price, total)
end