Skip to content

Commit

Permalink
update finances reporting system
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Dec 12, 2023
1 parent 570c9a8 commit a7a6308
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 250 deletions.
17 changes: 5 additions & 12 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-11-21 13:11:04 UTC using RuboCop version 1.56.2.
# on 2023-12-12 20:30:29 UTC using RuboCop version 1.56.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 6
# Offense count: 4
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 37
Max: 22

# Offense count: 12
# Offense count: 3
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 32

# Offense count: 6
# Configuration parameters: EnforcedStyle, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, camelCase
Naming/VariableName:
Exclude:
- 'app/utils/analytics/finances/project_calculator.rb'
Max: 16
17 changes: 11 additions & 6 deletions app/controllers/analytics/finances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ class FinancesController < ApplicationController
before_action :set_project, only: %i[index]

def index
@finances = analytics_class.new(params[:start_date],
params[:end_date], @project).data

render json: @finances
@finances = if @project
Analytics::Finances::Models::FinancialStatementsOfWork.new(@project, start_date, end_date)
else
Analytics::Finances::Models::FinancialProjects.new(start_date, end_date)
end
end

private
Expand All @@ -17,8 +18,12 @@ def set_project
@project = Project.where(id: params[:project_id]).first
end

def analytics_class
@project ? Analytics::ProjectFinancesAnalytics : Analytics::CompanyFinancesAnalytics
def start_date
params[:start_date].to_datetime.beginning_of_day
end

def end_date
params[:end_date].to_datetime.end_of_day
end
end
end
81 changes: 0 additions & 81 deletions app/utils/analytics/company_finances_analytics.rb

This file was deleted.

76 changes: 26 additions & 50 deletions app/utils/analytics/finances/finances_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,24 @@ def initialize(statement_of_work, start_date, end_date)
@start_date = start_date
@end_date = end_date

@names = {}
@expected_income_hash = Hash.new(0)
@executed_income_hash = Hash.new(0)

@expected_cost_hash = Hash.new(0)
@executed_cost_hash = Hash.new(0)

@expected_hours_hash = Hash.new(0)
@executed_hours_hash = Hash.new(0)
@paid_time_off_hours_hash = Hash.new(0)

@slug = Hash.new(0)

@financial_item = []
calculate!
end

def details
@names.keys.map do |name|
{
name:,
executed_hours: @executed_hours_hash[name],
expected_hours: [@expected_hours_hash[name], 0].max,

executed_income: @executed_income_hash[name],
expected_income: @expected_income_hash[name],

paid_time_off_hours: @paid_time_off_hours_hash[name],

executed_cost: @executed_cost_hash[name],
expected_cost: @expected_cost_hash[name],

slug: @slug[name]
}
end
@financial_item
end

def calculate!
assignments.each do |assignment|
user_name = assignment.user.name

@names[user_name] = user_name
@expected_income_hash[user_name] += assigned_expected_income(assignment)
@executed_income_hash[user_name] += assigned_executed_income(assignment)

@expected_cost_hash[user_name] += assignment_expected_cost(assignment)
@executed_cost_hash[user_name] += assignment_executed_cost(assignment)

@executed_hours_hash[user_name] += executed_hours(assignment)

@paid_time_off_hours_hash[user_name] = paid_time_off_hours(assignment)
@expected_hours_hash[user_name] += expected_hours(assignment)

@slug[user_name] = assignment.user.slug
financial_resource_assignment = financial_item_by_name(assignment.user.name, assignment.user.slug)
financial_resource_assignment.add_expected_income(assigned_expected_income(assignment))
financial_resource_assignment.add_executed_income(assigned_executed_income(assignment))
financial_resource_assignment.add_expected_cost(assignment_expected_cost(assignment))
financial_resource_assignment.add_executed_cost(assignment_executed_cost(assignment))
financial_resource_assignment.add_executed_hours(executed_hours(assignment))
financial_resource_assignment.add_expected_hours(expected_hours(assignment))
financial_resource_assignment.add_paid_time_off_hours(paid_time_off_hours(assignment))
end
end

Expand All @@ -76,16 +41,16 @@ def requirements
end

def expected_hours(assignment)
[TimeEntries::ExpectedHours.new(assignment, @start_date, @end_date).data - paid_time_off_hours(assignment),
0].max
hours = Analytics::TimeEntries::ExpectedHours.new(assignment, @start_date, @end_date).data
[hours - paid_time_off_hours(assignment), 0].max
end

def paid_time_off_hours(assignment)
TimeEntries::PaidTimeOffHours.new(assignment, @start_date, @end_date, TimeOffType.all).data
Analytics::TimeEntries::PaidTimeOffHours.new(assignment, @start_date, @end_date, TimeOffType.all).data
end

def executed_hours(assignment)
TimeEntries::CompleteWorkedHours.new(assignment, @start_date, @end_date).data
Analytics::TimeEntries::CompleteWorkedHours.new(assignment, @start_date, @end_date).data
end

def assignment_expected_cost(assignment)
Expand All @@ -103,14 +68,25 @@ def assignment_expected_cost(assignment)
end

def assignment_executed_cost(assignment)
entries = TimeEntries::CompleteWorkedHours.new(assignment, @start_date, @end_date).time_entries
entries = Analytics::TimeEntries::CompleteWorkedHours.new(assignment, @start_date, @end_date).time_entries
entries.map do |time_entry|
date = time_entry.date
salary = assignment.user.salary_on_date(date)

time_entry.hours * (salary&.hourly_cost || 0)
end.sum
end

def financial_item_by_name(name, slug)
financial_resource = @financial_item.find { |assignment| assignment.name == name }

return financial_resource if financial_resource

financial_resource = Analytics::Finances::Models::FinancialItem.new(name, slug)

@financial_item << financial_resource
financial_resource
end
end
end
end
4 changes: 2 additions & 2 deletions app/utils/analytics/finances/fixed_bid_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def total_expected_income
end

def total_expected_cost
@expected_cost_hash.values.sum
@financial_item.sum(&:expected_cost)
end

def total_executed_cost
@executed_cost_hash.values.sum
@financial_item.sum(&:executed_cost)
end

def total_payments
Expand Down
4 changes: 2 additions & 2 deletions app/utils/analytics/finances/maintenance_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def total_expected_income
end

def total_expected_cost
@expected_cost_hash.values.sum
@financial_item.sum(&:expected_cost)
end

def total_executed_cost
@executed_cost_hash.values.sum
@financial_item.sum(&:executed_cost)
end

private
Expand Down
69 changes: 69 additions & 0 deletions app/utils/analytics/finances/models/financial_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

module Analytics
module Finances
module Models
class FinancialItem
attr_accessor :name,
:slug,
:expected_hours,
:executed_hours,
:expected_income,
:executed_income,
:paid_time_off_hours,
:executed_cost,
:expected_cost

def initialize(name, slug)
@name = name
@slug = slug
@expected_hours = 0
@executed_hours = 0
@expected_income = 0
@executed_income = 0
@paid_time_off_hours = 0
@executed_cost = 0
@expected_cost = 0
end

def add_executed_income(amount)
@executed_income += amount
end

def add_expected_income(amount)
@expected_income += amount
end

def add_executed_cost(amount)
@executed_cost += amount
end

def add_expected_cost(amount)
@expected_cost += amount
end

def add_executed_hours(amount)
@executed_hours += amount
end

def add_expected_hours(amount)
@expected_hours += amount
end

def add_paid_time_off_hours(amount)
@paid_time_off_hours += amount
end

def merge(financial_item)
add_executed_income(financial_item.executed_income)
add_expected_income(financial_item.expected_income)
add_executed_cost(financial_item.executed_cost)
add_expected_cost(financial_item.expected_cost)
add_executed_hours(financial_item.executed_hours)
add_expected_hours(financial_item.expected_hours)
add_paid_time_off_hours(financial_item.paid_time_off_hours)
end
end
end
end
end
30 changes: 30 additions & 0 deletions app/utils/analytics/finances/models/financial_projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Analytics
module Finances
module Models
class FinancialProjects < FinancialReport
def calculate!
projects.each do |project|
finances = Analytics::Finances::Models::FinancialStatementsOfWork.new(project, @start_date, @end_date)
add_executed_income(finances.total_executed_income)
add_expected_income(finances.total_expected_income)
add_executed_cost(finances.total_executed_cost)
add_expected_cost(finances.total_expected_cost)

finances.financial_items.each do |financial_item|
existing_item = financial_item_by_name(project.name, project.slug)
existing_item.merge(financial_item)
end
end
end

private

def projects
Project.active_in_period(@start_date, @end_date)
end
end
end
end
end
Loading

0 comments on commit a7a6308

Please sign in to comment.