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
|
# File 'app/my_lib/modules/owners/reports/package_report.rb', line 13
def package_report_by(time_type)
reservations = active_reservations_filter_by(time_type)
type = if time_type == 'day'
:date_format
else
:start_time_format
end
Rails.cache.fetch("#{self.class}:#{time_type}:#{restaurants_cache_key}:#{reservations.cache_key}:#{I18n.locale}") do
reservations_group = reservations.group_by(&type)
keys = reservations_group.keys.sort
total_package_covers = {}
grand_total_covers = 0
packages = {}
delivery = 0
row_data = keys.map do |key|
packages[key] ||= {}
reservations_group[key].each do |reservation|
next if reservation.package_obj.blank?
if reservation.service_type == :dine_in
reservation.package_obj.formatted_packages.each do |item|
klass = item[:type].constantize
hash_id = CityHash.hash32(item[:name])
packages[key][hash_id] ||= {}
quantity = packages[key][hash_id][:quantity].presence || 0
adder = reservation.party_size
packages[key][hash_id] = { id: hash_id, name: item[:name], quantity: quantity + adder }
end
else
reservation.package_obj.formatted_packages.each do |item|
delivery += 1
hash_id = CityHash.hash32(item[:name])
packages[key][hash_id] ||= {}
quantity = packages[key][hash_id][:quantity].presence || 0
packages[key][hash_id] = { id: hash_id, name: item[:name], quantity: quantity + item[:quantity] }
end
end
end
total = 0
data = packages[key].values.map do |p|
hash_id = CityHash.hash32(p[:name])
covers = if packages[key][hash_id].present?
packages[key][hash_id][:quantity].to_i
else
0
end
total += covers
total_package_covers[hash_id] ||= {}
total_package_covers[hash_id][:total_covers] ||= 0
total_package_covers[hash_id][:total_covers] += covers
grand_total_covers += covers
{
id: p[:id],
name: p[:name],
covers: covers
}
end
time_format = if time_type == 'day'
key.to_date.strftime('%d-%b')
else
key
end
[time_format, data, total]
end
package_result = {}
packages.each do |_key, value|
value.values.each do |pack|
if package_result[pack[:id]].nil?
package_result[pack[:id]] = pack
else
package_result[pack[:id]][:quantity] += pack[:quantity]
end
end
end
[
row_data,
total_package_covers.to_a,
grand_total_covers,
package_result.values,
delivery
]
end
end
|