/
activity_actuals_columns.rb
114 lines (93 loc) · 3.16 KB
/
activity_actuals_columns.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
class Export::ActivityActualsColumns
def initialize(activities:, include_breakdown: false, report: nil)
@activities = activities
@include_breakdown = include_breakdown
@report = report
end
def headers
return [] if @activities.empty?
return [] if financial_quarter_range.nil?
financial_quarter_range.map { |financial_quarter|
if @include_breakdown
[
"Actual spend #{financial_quarter}",
"Refund #{financial_quarter}",
"Actual net #{financial_quarter}"
]
else
["Actual net #{financial_quarter}"]
end
}.flatten
end
def rows
return [] if @activities.empty?
return activities_with_no_data if financial_quarter_range.nil?
@_rows ||= @activities.map { |activity|
actual_and_refund_data(activity)
}.to_h
end
def last_financial_quarter
return nil if financial_quarter_range.nil?
financial_quarter_range.max
end
def rows_for_last_financial_quarter
rows.each_with_object({}) { |(key, values), obj| obj[key] = values.last }
end
private
def activities_with_no_data
@_rows ||= @activities.map { |activity|
[activity.id, []]
}.to_h
end
def actual_and_refund_data(activity)
build_columns(all_totals_for_activity(activity), activity)
end
def build_columns(totals, activity)
columns = financial_quarter_range.map { |fq|
actual_overview = Export::FinancialQuarterActivityTotals.new(type: :actual, activity: activity, totals: totals, financial_quarter: fq)
refund_overview = Export::FinancialQuarterActivityTotals.new(type: :refund, activity: activity, totals: totals, financial_quarter: fq)
net_total = actual_overview.net_total + refund_overview.net_total
if @include_breakdown
[actual_overview.net_total, refund_overview.net_total, net_total]
else
[net_total]
end
}
[activity.id, columns.flatten]
end
def all_totals_for_activity(activity)
Export::AllActivityTotals.new(activity: activity).call
end
def reports_up_to(report)
return if report.nil?
Report.historically_up_to(report).pluck(:id)
end
def actual_spend
actual_spend_scope = Actual.where(parent_activity_id: activity_ids)
actual_spend_scope = actual_spend_scope.where(report_id: reports_up_to(@report)) unless @report.nil?
@_actual_spend ||= actual_spend_scope
end
def refunds
refund_scope = Refund.where(parent_activity_id: activity_ids)
refund_scope = refund_scope.where(report_id: reports_up_to(@report)) unless @report.nil?
@_refunds ||= refund_scope
end
def activity_ids
@activity_ids ||= @activities.pluck(:id)
end
def all_financial_quarters_with_actuals
return [] unless actual_spend.present?
actual_spend.map(&:own_financial_quarter).uniq
end
def all_financial_quarters_with_refunds
return [] unless refunds.present?
refunds.map(&:own_financial_quarter).uniq
end
def financial_quarters
all_financial_quarters_with_actuals + all_financial_quarters_with_refunds
end
def financial_quarter_range
return nil if financial_quarters.empty?
@_financial_quarter_range ||= Range.new(*financial_quarters.minmax)
end
end