-
Notifications
You must be signed in to change notification settings - Fork 0
/
region.rb
231 lines (221 loc) · 5.85 KB
/
region.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
class Region < ApplicationRecord
## This File has most of the heavy lifting for finding the region averages
## These methods reference the sales summaries for each region
## and the sales summaries have functions for calculating values that calculated from the associated DB fields
## this file so far has mainly 2 functions that are written specificically for each category/field of Sales Data.
## the 2 functions are Average and Number Reporting.
attachment :profile_image
belongs_to :county
has_many :tasting_rooms
has_many :wineries, through: :tasting_rooms
## the avg function returns the average of this category/field in this region for this time period
def avg_tasters(month, year)
total = 0
count = 0
self.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
## the reporting function returns the number of wineries that have reported data for this category/field
def reporting_tasters(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.num_of_tasters.present?
end
end
count
end
def avg_purchasers(month, year)
total = 0
count = 0
self.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 reporting_purchasers(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.num_of_purchasers.present?
end
end
count
end
def avg_club_signups(month, year)
total = 0
count = 0
self.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
total
end
end
def reporting_club_signups(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.num_of_club_signups.present?
end
end
count
end
def avg_sales_in_dollars(month, year)
total = 0
count = 0
self.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
total
end
end
def reporting_sales_in_dollars(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.sales_in_dollars.present?
end
end
count
end
def avg_tasters_purchased(month, year)
total = 0
count = 0
self.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
total
end
end
def reporting_tasters_purchased(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.percent_tasters_purcahased.present?
end
end
count
end
def avg_club_conversion(month, year)
total = 0
count = 0
self.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
total
end
end
def reporting_club_conversion(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.percent_club_signup.present? && ss.num_of_purchasers != 0
end
end
count
end
def avg_sales_per_taster(month, year)
total = 0
count = 0
self.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
total
end
end
def reporting_sales_per_taster(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.sales_per_taster.present? #sales_in_dollars.present? && num_of_tasters.present? #
end
end
count
end
def avg_sales_per_purchase(month, year)
total = 0
count = 0
self.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
total
end
end
def reporting_sales_per_purchase(month, year)
count = 0
self.tasting_rooms.each do |tr|
tr.sales_summaries.where(month: month, year: year).each do |ss|
count += 1 if ss.sales_per_purchase.present?
end
end
count
end
end