Skip to content

Commit

Permalink
TMP: log everything
Browse files Browse the repository at this point in the history
  • Loading branch information
rgarner committed Feb 6, 2025
1 parent ff7d601 commit c1ec22e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
6 changes: 5 additions & 1 deletion app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,11 @@ def forecasted_total_for_report_financial_quarter(report:)
end

def variance_for_report_financial_quarter(report:)
@variance_for_report_financial_quarter ||= actual_total_for_report_financial_quarter(report: report) - forecasted_total_for_report_financial_quarter(report: report)
@variance_for_report_financial_quarter ||=
(
actual_total_for_report_financial_quarter(report: report).tap { puts "actual_total_for_report_financial_quarter(report:) => #{_1}" } -
forecasted_total_for_report_financial_quarter(report: report).tap { puts "forecasted_total_for_report_financial_quarter(report:) => #{_1}"}
).tap { puts "variance_for_report_financial_quarter(report:) == #{_1}" }
end

def latest_forecasts
Expand Down
1 change: 1 addition & 0 deletions app/services/forecast_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def set_value(value)

return if latest_entry&.value == value

puts "Setting forecast to #{value}"
if record_history?
update_history(latest_entry, value)
else
Expand Down
7 changes: 4 additions & 3 deletions app/services/transaction_overview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ def value_for_report_quarter(activity)
end

def value_for(financial_quarter:, financial_year:, activity:)
puts "financial_quarter: #{financial_quarter}, financial_year: #{financial_year}, parent_activity: #{activity.id}"
actual_relation
.where(
financial_quarter: financial_quarter,
financial_year: financial_year,
parent_activity: activity
)
.sum(:value)
).tap { puts _1.to_sql }
.pluck(:value).tap { puts _1.inspect }.reduce(:+).tap { puts "pluck(:value).reduce(:+) => #{_1}"}
end

private

def actual_relation
scope
.joins(:report)
.merge(Report.historically_up_to(@report))
.merge(Report.historically_up_to(@report)).tap { puts _1.map { |m| [m.class, m.value] }.join("/") }
end

def scope
Expand Down
9 changes: 7 additions & 2 deletions spec/models/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,7 @@
describe "#variance_for_report_financial_quarter" do
before do
start_of_third_quarter = Date.parse("2020-10-01")
puts "Travelling to #{start_of_third_quarter}"
travel_to start_of_third_quarter
end

Expand All @@ -1524,8 +1525,12 @@
reporting_cycle.tick

report = Report.for_activity(project).find_by(financial_quarter: 3)
create(:actual, parent_activity: project, value: 100, report: report, date: Date.today)
create(:actual, parent_activity: project, value: 200, report: report, date: Date.today)
puts "activity_spec/report = #{report.inspect}"
a1 = create(:actual, parent_activity: project, value: 100, report: report, date: Date.today)
a2 = create(:actual, parent_activity: project, value: 200, report: report, date: Date.today)

pp [a1.id, a1.parent_activity.id, a1.value, a1.date, a1.financial_year, a1.financial_quarter]
pp [a2.id, a2.parent_activity.id, a2.value, a2.date, a2.financial_year, a2.financial_quarter]

expect(project.variance_for_report_financial_quarter(report: report)).to eq(-1200)
end
Expand Down
14 changes: 14 additions & 0 deletions spec/support/log_failing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
RSpec.configure do |config|
config.around(:each, :xfailing) do |example|
original_logger = ActiveRecord::Base.logger

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::DEBUG

begin
example.run
ensure
ActiveRecord::Base.logger = original_logger
end
end
end
4 changes: 4 additions & 0 deletions spec/support/reporting_cycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ def tick
private

def approve_report
puts "Approving report"
@report&.update!(state: :approved)
end

def create_report
@report = Report.new(fund: @activity.associated_fund, organisation: @activity.organisation)

puts "Creating report for #{@financial_year.to_i}, Q#{@financial_quarter.to_i}"
@report.created_at = FinancialQuarter.new(@financial_year.to_i, @financial_quarter.to_i).start_date
@report.financial_quarter = @financial_quarter
@report.financial_year = @financial_year
Expand All @@ -32,11 +34,13 @@ def create_report
end

def increment_quarter
print "Incrementing quarter #{@financial_year.to_i} Q#{@financial_quarter.to_i} -> "
@financial_quarter += 1

if @financial_quarter > 4
@financial_year += 1
@financial_quarter = 1
end
puts "#{@financial_year.to_i} Q#{@financial_quarter.to_i}"
end
end

0 comments on commit c1ec22e

Please sign in to comment.