From 4a9858c739503c70cb3226a1d1413b2576cfd3d4 Mon Sep 17 00:00:00 2001 From: Kaio Magalhaes Date: Mon, 1 Jul 2024 16:00:29 -0300 Subject: [PATCH] add project progress controller --- .rubocop_todo.yml | 6 ++--- .../analytics/project_progress_controller.rb | 25 +++++++++++++++++++ app/models/retainer_contract_model.rb | 13 +++++++++- config/routes.rb | 1 + 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 app/controllers/analytics/project_progress_controller.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 216e67d..a24ca54 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-06-05 18:08:13 UTC using RuboCop version 1.56.2. +# on 2024-07-01 18:59:17 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 @@ -21,7 +21,7 @@ Metrics/AbcSize: # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. # AllowedMethods: refine Metrics/BlockLength: - Max: 26 + Max: 27 # Offense count: 1 # Configuration parameters: AllowedMethods, AllowedPatterns. @@ -63,7 +63,7 @@ Style/OptionalBooleanParameter: - 'app/models/retainer_contract_model.rb' - 'app/utils/analytics/finances/models/financial_statements_of_work.rb' -# Offense count: 11 +# Offense count: 12 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. # URISchemes: http, https diff --git a/app/controllers/analytics/project_progress_controller.rb b/app/controllers/analytics/project_progress_controller.rb new file mode 100644 index 0000000..f2b8e55 --- /dev/null +++ b/app/controllers/analytics/project_progress_controller.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module Analytics + class ProjectProgressController < ApplicationController + before_action :set_project, only: %i[index] + before_action :set_statement_of_work, only: %i[index] + + def index + render json: { + contract_hours: @statement_of_work.contract_model.contract_total_hours, + consumed_hours: @statement_of_work.contract_model.consumed_hours + } + end + + private + + def set_project + @project = Project.where(id: params[:project_id]).first + end + + def set_statement_of_work + @statement_of_work = StatementOfWork.where(id: params[:statement_of_work_id]).first + end + end +end diff --git a/app/models/retainer_contract_model.rb b/app/models/retainer_contract_model.rb index 6a3eeea..633e16f 100644 --- a/app/models/retainer_contract_model.rb +++ b/app/models/retainer_contract_model.rb @@ -16,6 +16,17 @@ class RetainerContractModel < ApplicationRecord has_one :statement_of_work, as: :contract_model, dependent: :destroy + def contract_total_hours + expected_hours_per_period * months_in_period(statement_of_work.start_date, statement_of_work.end_date) + end + + def consumed_hours + assignments = statement_of_work.requirements.map(&:assignments).flatten + assignments.sum do |assignment| + worked_hours(assignment, statement_of_work.start_date, statement_of_work.end_date) + end + end + def expected_income(start_date, end_date) revenue_per_period * months_in_period(start_date, end_date) end @@ -23,7 +34,7 @@ def expected_income(start_date, end_date) def months_in_period(start_date, end_date) return 1 if start_date.month == end_date.month && start_date.year == end_date.year - ((end_date.year * 12) + end_date.month) - ((start_date.year * 12) + start_date.month) + ((end_date.year * 12) + end_date.month) - ((start_date.year * 12) + start_date.month) + 1 end def assignment_executed_income(_assignment, _start_date, _end_date) diff --git a/config/routes.rb b/config/routes.rb index 88e41b8..bf4cd36 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,6 +29,7 @@ namespace :analytics do resources :time_entries, only: [:index] resources :finances, only: [:index] + resources :project_progress, only: [:index] end resources :users