Class: Ordergroup
Overview
Ordergroups can order, they are “children” of the class Group
Ordergroup have the following attributes, in addition to Group
Constant Summary
collapse
- APPLE_MONTH_AGO =
How many month back we will count tasks and orders sum
6
Instance Attribute Summary
Attributes included from CustomFields
#custom_fields
Attributes inherited from Group
#user_tokens
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Group
#deleted?, #mark_as_deleted, #member?
#mark_as_deleted
Class Method Details
.avg_jobs_per_euro ⇒ Object
149
150
151
152
153
154
155
156
|
# File 'app/models/ordergroup.rb', line 149
def self.avg_jobs_per_euro
stats = Ordergroup.pluck(:stats)
begin
stats.sum { |s| s[:jobs_size].to_f } / stats.sum { |s| s[:orders_sum].to_f }
rescue StandardError
0
end
end
|
.custom_fields ⇒ Object
45
46
47
48
49
50
|
# File 'app/models/ordergroup.rb', line 45
def self.custom_fields
fields = FoodsoftConfig[:custom_fields] && FoodsoftConfig[:custom_fields][:ordergroup]
return [] unless fields
fields.map(&:deep_symbolize_keys)
end
|
.include_transaction_class_sum ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
|
# File 'app/models/ordergroup.rb', line 33
def self.include_transaction_class_sum
columns = ['groups.*']
FinancialTransactionClass.all.find_each do |c|
columns << "sum(CASE financial_transaction_types.financial_transaction_class_id WHEN #{c.id} THEN financial_transactions.amount ELSE 0 END) AS sum_of_class_#{c.id}"
end
select(columns.join(', '))
.joins('LEFT JOIN financial_transactions ON groups.id = financial_transactions.ordergroup_id')
.joins('LEFT JOIN financial_transaction_types ON financial_transaction_types.id = financial_transactions.financial_transaction_type_id')
.group('groups.id')
end
|
.sort_by_param(param) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'app/models/ordergroup.rb', line 162
def self.sort_by_param(param)
param ||= 'name'
sort_param_map = {
'name' => 'name',
'name_reverse' => 'name DESC',
'members_count' => 'count(users.id)',
'members_count_reverse' => 'count(users.id) DESC',
'last_user_activity' => 'max(users.last_activity)',
'last_user_activity_reverse' => 'max(users.last_activity) DESC',
'last_order' => 'max(orders.starts)',
'last_order_reverse' => 'max(orders.starts) DESC'
}
result = self
result = result.left_joins(:users).group('groups.id') if param.starts_with?('members_count', 'last_user_activity')
result = result.left_joins(:orders).group('groups.id') if param.starts_with?('last_order')
result.order(Arel.sql(sort_param_map[param]))
end
|
Instance Method Details
#account_updated ⇒ Object
158
159
160
|
# File 'app/models/ordergroup.rb', line 158
def account_updated
financial_transactions.last.try(:created_on) || created_on
end
|
#add_financial_transaction!(amount, note, user, transaction_type, link = nil, group_order = nil) ⇒ Object
Creates a new FinancialTransaction for this Ordergroup and updates the account_balance accordingly. Throws an exception if it fails.
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'app/models/ordergroup.rb', line 87
def add_financial_transaction!(amount, note, user, transaction_type, link = nil, group_order = nil)
transaction do
t = FinancialTransaction.new(ordergroup: self, amount: amount, note: note, user: user,
financial_transaction_type: transaction_type, financial_link: link, group_order: group_order)
t.save!
update_balance!
if t.amount < 0 && account_balance < 0 && account_balance - t.amount >= 0
NotifyNegativeBalanceJob.perform_later(self,
t)
end
t
end
end
|
#apples ⇒ Object
This is the ordergroup job per euro performance in comparison to the hole foodcoop average
129
130
131
132
133
|
# File 'app/models/ordergroup.rb', line 129
def apples
((avg_jobs_per_euro / Ordergroup.avg_jobs_per_euro) * 100).to_i
rescue StandardError
0
end
|
#avg_jobs_per_euro ⇒ Object
121
122
123
124
125
|
# File 'app/models/ordergroup.rb', line 121
def avg_jobs_per_euro
stats[:jobs_size].to_f / stats[:orders_sum].to_f
rescue StandardError
0
end
|
25
26
27
|
# File 'app/models/ordergroup.rb', line 25
def contact
"#{contact_phone} (#{contact_person})"
end
|
#financial_transaction_class_balance(klass) ⇒ Object
78
79
80
81
82
83
|
# File 'app/models/ordergroup.rb', line 78
def financial_transaction_class_balance(klass)
financial_transactions
.joins(:financial_transaction_type)
.where(financial_transaction_types: { financial_transaction_class_id: klass })
.sum(:amount)
end
|
#get_available_funds(exclude = nil) ⇒ Object
Returns the available funds for this order group (the account_balance minus price of all non-closed GroupOrders of this group).
74
75
76
|
# File 'app/models/ordergroup.rb', line 74
def get_available_funds(exclude = nil)
account_balance - value_of_open_orders(exclude) - value_of_finished_orders(exclude)
end
|
#last_order ⇒ Object
the most recent order this ordergroup was participating in
60
61
62
|
# File 'app/models/ordergroup.rb', line 60
def last_order
orders.order('orders.starts DESC').first
end
|
#last_user_activity ⇒ Object
52
53
54
55
56
57
|
# File 'app/models/ordergroup.rb', line 52
def last_user_activity
last_active_user = users.order('users.last_activity DESC').first
return unless last_active_user
last_active_user.last_activity
end
|
#non_members ⇒ Object
29
30
31
|
# File 'app/models/ordergroup.rb', line 29
def non_members
User.natural_order.all.reject { |u| users.include?(u) || u.ordergroup }
end
|
#not_enough_apples? ⇒ Boolean
If the the option stop_ordering_under is set, the ordergroup is only allowed to participate in an order, when the apples value is above the configured amount. The restriction can be deactivated for each ordergroup. Only ordergroups, which have participated in more than 5 orders in total and more than 2 orders in apple time period
139
140
141
142
143
144
145
146
|
# File 'app/models/ordergroup.rb', line 139
def not_enough_apples?
FoodsoftConfig[:use_apple_points] &&
FoodsoftConfig[:stop_ordering_under].present? &&
!ignore_apple_restriction &&
apples < FoodsoftConfig[:stop_ordering_under] &&
group_orders.count > 5 &&
group_orders.joins(:order).merge(Order.finished).where('orders.ends >= ?', APPLE_MONTH_AGO.month.ago).count > 2
end
|
#update_balance! ⇒ Object
113
114
115
116
117
118
119
|
# File 'app/models/ordergroup.rb', line 113
def update_balance!
new_account_balance = financial_transactions
.joins(financial_transaction_type: [:financial_transaction_class])
.where({ financial_transaction_classes: { ignore_for_account_balance: false } })
.sum(:amount)
update_attribute :account_balance, new_account_balance
end
|
#update_stats! ⇒ Object
102
103
104
105
106
107
108
109
110
111
|
# File 'app/models/ordergroup.rb', line 102
def update_stats!
jobs = users.to_a.sum { |u| u.tasks.done.where('updated_on > ?', APPLE_MONTH_AGO.month.ago).sum(:duration) }
orders_sum = group_orders.includes(:order).merge(Order.finished).where('orders.ends >= ?',
APPLE_MONTH_AGO.month.ago).references(:orders).sum(:price)
@readonly = false update_attribute(:stats, { jobs_size: jobs, orders_sum: orders_sum })
end
|
#value_of_finished_orders(exclude = nil) ⇒ Object
68
69
70
|
# File 'app/models/ordergroup.rb', line 68
def value_of_finished_orders(exclude = nil)
group_orders.in_finished_orders.reject { |go| go == exclude }.collect(&:price).sum
end
|
#value_of_open_orders(exclude = nil) ⇒ Object
64
65
66
|
# File 'app/models/ordergroup.rb', line 64
def value_of_open_orders(exclude = nil)
group_orders.in_open_orders.reject { |go| go == exclude }.collect(&:price).sum
end
|