-
Notifications
You must be signed in to change notification settings - Fork 0
/
sales_summary.rb
359 lines (337 loc) · 8.97 KB
/
sales_summary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
class SalesSummary < ApplicationRecord
belongs_to :tasting_room
has_one :region, through: :tasting_room
has_one :winery, through: :tasting_room
#validates :num_of_tasters, presence: true
## These are used to calculate the Sales Data
##
##
##
def percent_tasters_purcahased
if num_of_tasters.present? && num_of_purchasers.present?
(num_of_purchasers.to_f / num_of_tasters.to_f * 100.0).round(1)
else
nil #0.0
end
end
def percent_club_signup
if num_of_club_signups.present? && num_of_purchasers.present?
(num_of_club_signups.to_f / num_of_purchasers.to_f * 100.0).round(1)
else
nil #0.0
end
end
def sales_per_taster
if sales_in_dollars.present? && num_of_tasters.present?
(sales_in_dollars.to_f / num_of_tasters.to_f).round(2)
else
nil #0.0
end
end
def sales_per_purchase
if sales_in_dollars.present? && num_of_purchasers.present? && num_of_purchasers > 0
(sales_in_dollars.to_f / num_of_purchasers.to_f).round(2)
#elsif sales_in_dollars.present?
# sales_in_dollars
else
nil #0.0
end
end
## returns the average for the region
def avg_tasters
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.num_of_tasters.present?
count += 1
total += ss.num_of_tasters
end
end
end
if count > 0
avg = total / count
else
0
end
end
## returns the difference for this sales summary from the region average
def num_of_tasters_percent_different
#(family winery - average) / family winery
if self.num_of_tasters.blank?
0
else
(num_of_tasters.to_f / avg_tasters.to_f).to_f * 100.0
end
end
## all sales summaries from this winery for this year upto the date of this sales summary
def ytd_num_of_tasters
winery = self.winery
tasting_room = self.tasting_room
year = self.year
month = self.month
ytd_ss = tasting_room.sales_summaries.where('year = ? AND month <= ?', year, month)
total = 0
ytd_ss.each do |ss|
total += ss.num_of_tasters if ss.num_of_tasters.present?
end
return total
end
def avg_purchasers
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.num_of_purchasers.present?
count += 1
total += ss.num_of_purchasers
end
end
end
if count > 0
avg = total / count
else
0
end
end
def num_of_purchasers_percent_different
#(family winery - average) / family winery
if self.num_of_purchasers.blank?
0
else
(num_of_purchasers.to_f / avg_purchasers.to_f).to_f * 100.0
end
end
## calc the number of purchasers year to date
## all sales summaries from this winery for this year upto the date of this sales summary
def ytd_num_of_purchasers
winery = self.winery
tasting_room = self.tasting_room
year = self.year
month = self.month
ytd_ss = tasting_room.sales_summaries.where('year = ? AND month <= ?', year, month)
total = 0
ytd_ss.each do |ss|
total += ss.num_of_purchasers if ss.num_of_purchasers.present?
end
return total
end
def avg_club_signups
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.num_of_club_signups.present?
count += 1
total += ss.num_of_club_signups
end
end
end
if count > 0
avg = total / count
else
0
end
end
def ytd_num_of_club_signups
winery = self.winery
tasting_room = self.tasting_room
year = self.year
month = self.month
ytd = tasting_room.sales_summaries.where('year = ? AND month <= ?', year, month)
total = 0
ytd.each do |ss|
total += ss.num_of_club_signups if ss.num_of_club_signups.present?
end
return total
end
def num_of_club_signups_percent_different
if self.num_of_club_signups.blank?
0
else
(num_of_club_signups.to_f / avg_club_signups.to_f).to_f * 100.0
end
end
def avg_sales_in_dollars
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.sales_in_dollars.present?
count += 1
total += ss.sales_in_dollars
end
end
end
if count > 0
avg = total / count
else
0
end
end
def sales_in_dollars_percent_different
if sales_in_dollars.blank?
0
else
(sales_in_dollars.to_f / avg_sales_in_dollars.to_f).to_f * 100.0
end
end
def ytd_sales_in_dollars
winery = self.winery
tasting_room = self.tasting_room
year = self.year
month = self.month
ytd = tasting_room.sales_summaries.where('year = ? AND month <= ?', year, month)
total = 0
ytd.each do |ss|
total += ss.sales_in_dollars if ss.sales_in_dollars.present?
end
return total
end
## conversion ratio
def avg_tasters_purchased
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.percent_tasters_purcahased.present?
count += 1
total += ss.percent_tasters_purcahased
end
end
end
if count > 0
avg = total / count
else
0.0
end
end
def conversion_percent_different
if self.percent_tasters_purcahased.blank?
0
else
(percent_tasters_purcahased.to_f / avg_tasters_purchased.to_f).to_f * 100.0
end
end
def avg_club_conversion
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.percent_club_signup.present? && ss.num_of_purchasers != 0
count += 1
total += ss.percent_club_signup
end
end
end
if count > 0
avg = total / count
else
0
end
end
def club_conversion_percent_different
if percent_club_signup.blank?
0
else
(percent_club_signup.to_f / avg_club_conversion.to_f).to_f * 100.0
end
end
def avg_sales_per_taster
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.sales_per_taster.present?
count += 1
total += ss.sales_per_taster
end
end
end
if count > 0
avg = total / count
else
0
end
end
def sales_per_taster_percent_different
if sales_per_taster.blank?
0
else
(sales_per_taster.to_f / avg_sales_per_taster.to_f).to_f * 100.0
end
end
def avg_sales_per_purchase
total = 0
count = 0
month = self.month
year = self.year
region.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
if ss.sales_per_purchase.present?
count += 1
total += ss.sales_per_purchase
end
end
end
if count > 0
avg = total / count
else
0
end
end
def sales_per_purchase_percent_different
if self.sales_per_purchase.blank?
0
else
(sales_per_purchase.to_f / avg_sales_per_purchase.to_f).to_f * 100.0
end
end
# variance. NOT USED
def num_of_tasters_variance
#(family winery - average) / family winery
(num_of_tasters - avg_tasters).to_f / num_of_tasters.to_f * 100.0
end
def num_of_purchasers_variance
#(family winery - average) / family winery
(num_of_purchasers - avg_purchasers).to_f / num_of_purchasers.to_f * 100.0
end
def num_of_club_signups_variance
#(family winery - average) / family winery
(num_of_club_signups - avg_club_signups).to_f / num_of_club_signups.to_f * 100.0
end
def sales_in_dollars_variance
#(family winery - average) / family winery
(sales_in_dollars - avg_sales_in_dollars).to_f / sales_in_dollars.to_f * 100.0
end
def conversion_variance
#(family winery - average) / family winery
(percent_tasters_purcahased - avg_tasters_purchased).to_f / percent_tasters_purcahased.to_f * 100.0
end
def club_conversion_variance
#(family winery - average) / family winery
(percent_club_signup - avg_club_conversion).to_f / percent_club_signup.to_f * 100.0
end
def sales_per_taster_variance
#(family winery - average) / family winery
(sales_per_taster - avg_sales_per_taster).to_f / sales_per_taster.to_f * 100.0
end
def sales_per_purchase_variance
#(family winery - average) / family winery
(sales_per_purchase - avg_sales_per_purchase).to_f / sales_per_purchase.to_f * 100.0
end
end