From f34356cecd78c05e937f7d14a1d9b15618c988e5 Mon Sep 17 00:00:00 2001 From: sabinopa Date: Tue, 16 Jul 2024 16:14:46 -0300 Subject: [PATCH 1/9] adiciona cobrancas avulsas ao calculo da fatura Co-authored-by: Angelo Maia --- app/models/bill_calculator.rb | 22 +++- db/schema.rb | 1 - spec/models/bill_calculator_spec.rb | 100 ++++++++++++++----- spec/requests/apis/single_charge_api_spec.rb | 33 ++++++ 4 files changed, 129 insertions(+), 27 deletions(-) diff --git a/app/models/bill_calculator.rb b/app/models/bill_calculator.rb index 23c53a00..4b6a6ae2 100644 --- a/app/models/bill_calculator.rb +++ b/app/models/bill_calculator.rb @@ -2,7 +2,8 @@ class BillCalculator def self.calculate_total_fees(unit) base_fees = calculate_base_fees(unit.unit_type_id) shared_fees = calculate_shared_fees(unit.id) - base_fees + shared_fees + single_charges = calculate_single_charges(unit.id) + base_fees + shared_fees + single_charges end def self.calculate_base_fees(unit_type_id) @@ -29,6 +30,15 @@ def self.calculate_shared_fees(unit_id) total_value end + def self.calculate_single_charges(unit_id) + total_value = 0 + charges = get_last_month_single_charges(unit_id) + charges.each do |charge| + total_value += charge.value_cents + end + total_value + end + def self.get_last_month_fractions(unit_id) now = Time.zone.now last_month = now.last_month @@ -51,6 +61,14 @@ def self.get_last_month_values(unit_type_id) .where(base_fees: { charge_day: start_of_last_month..end_of_last_month }) end + def self.get_last_month_single_charges(unit_id) + now = Time.zone.now + last_month = now.last_month + start_of_last_month = last_month.beginning_of_month + end_of_last_month = last_month.end_of_month + SingleCharge.where(unit_id:, issue_date: start_of_last_month..end_of_last_month) + end + def self.check_recurrence(base_fee) recurrence = base_fee.recurrence @@ -85,5 +103,5 @@ def self.check_monthly_recurrence(base_fee) end private_class_method :check_monthly_recurrence, :check_yearly_recurrence, :check_recurrence, :get_last_month_values, - :get_last_month_fractions + :get_last_month_fractions, :get_last_month_single_charges end diff --git a/db/schema.rb b/db/schema.rb index 3d0ea47f..ea0ddb54 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,6 @@ # # It's strongly recommended that you check this file into your version control system. - ActiveRecord::Schema[7.1].define(version: 2024_07_16_004427) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false diff --git a/spec/models/bill_calculator_spec.rb b/spec/models/bill_calculator_spec.rb index 9c8014e9..78cbcc4a 100644 --- a/spec/models/bill_calculator_spec.rb +++ b/spec/models/bill_calculator_spec.rb @@ -3,28 +3,34 @@ describe BillCalculator do context '.calculate_total_fees' do it 'e retorna valor total da fatura' do - condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + condos = [] + condos << Condo.new(id: 1, name: 'Condo Test', city: 'City Test') unit_types = [] unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, - condo_id: 1) + condo_id: condos.first.id) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, - total_value: 30_000_00, condo_id: condo.id) + total_value: 30_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) - base_fee = create(:base_fee, condo_id: 1) + base_fee = create(:base_fee, condo_id: condos.first.id) create(:value, price_cents: 100_00, base_fee_id: base_fee.id) - allow(Condo).to receive(:find).and_return(condo) allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) - allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:find_all_by_condo).and_return(units) + allow(Condo).to receive(:all).and_return(condos) + allow(Condo).to receive(:find).and_return(condos.first) + allow(UnitType).to receive(:all).and_return(unit_types) + allow(UnitType).to receive(:find).and_return(unit_types.first) + allow(Unit).to receive(:all).and_return(units) + allow(Unit).to receive(:find).and_return(units.first) + allow(CommonArea).to receive(:all).and_return([]) + SingleCharge.create!(charge_type: :fine, value_cents: 100_11, description: 'Multa por barulho', + issue_date: 5.days.from_now.to_date, unit_id: units.first.id, condo_id: condos.first.id) travel_to 35.days.from_now do - Bill.new(unit_id: units.first.id, issue_date: Time.zone.today, due_date: Time.zone.today) - fees = BillCalculator.calculate_total_fees(units.first) - expect(fees).to eq 400_00 + expect(fees).to eq 500_11 end end @@ -40,11 +46,11 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:find_all_by_condo).and_return(units) - Bill.new(unit_id: units.first.id, issue_date: Time.zone.today, due_date: Time.zone.today) - - fees = BillCalculator.calculate_total_fees(units.first) + travel_to 35.days.from_now do + fees = BillCalculator.calculate_total_fees(units.first) - expect(fees).to eq 0 + expect(fees).to eq 0 + end end end @@ -67,8 +73,6 @@ allow(Unit).to receive(:find_all_by_condo).and_return(units) travel_to 35.days.from_now do - Bill.new(unit_id: units.first.id, issue_date: Time.zone.today, due_date: Time.zone.today) - fees = BillCalculator.calculate_base_fees(unit_types.first) expect(fees).to eq 100_00 @@ -87,11 +91,11 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:find_all_by_condo).and_return(units) - Bill.new(unit_id: units.first.id, issue_date: Time.zone.today, due_date: Time.zone.today) - - fees = BillCalculator.calculate_base_fees(unit_types.first) + travel_to 35.days.from_now do + fees = BillCalculator.calculate_base_fees(unit_types.first) - expect(fees).to eq 0 + expect(fees).to eq 0 + end end end @@ -114,8 +118,6 @@ allow(Unit).to receive(:find_all_by_condo).and_return(units) travel_to 35.days.from_now do - Bill.new(unit_id: units.first.id, issue_date: Time.zone.today, due_date: Time.zone.today) - fees = BillCalculator.calculate_shared_fees(unit_types.first) expect(fees).to eq 300_00 @@ -134,11 +136,61 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:find_all_by_condo).and_return(units) - Bill.new(unit_id: units.first.id, issue_date: Time.zone.today, due_date: Time.zone.today) + travel_to 35.days.from_now do + fees = BillCalculator.calculate_shared_fees(unit_types.first) - fees = BillCalculator.calculate_shared_fees(unit_types.first) + expect(fees).to eq 0 + end + end + end - expect(fees).to eq 0 + context '.calculate_single_charges' do + it 'e retorna valores de cobranças avulsas da fatura' do + condos = [] + condos << Condo.new(id: 1, name: 'Condo Test', city: 'City Test') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 40, description: 'Apartamento 1 quarto', ideal_fraction: 0.5, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 40, floor: 1, number: 1, unit_type_id: 1) + units << Unit.new(id: 2, area: 40, floor: 1, number: 2, unit_type_id: 1) + common_areas = [] + common_areas << CommonArea.new(id: 1, name: 'Academia', + description: 'Uma academia raíz com ventilador apenas para os marombas', + max_occupancy: 20, rules: 'Não pode ser frango', condo_id: 1) + allow(Condo).to receive(:all).and_return(condos) + allow(Condo).to receive(:find).and_return(condos.first) + allow(UnitType).to receive(:all).and_return(unit_types) + allow(UnitType).to receive(:find).and_return(unit_types.first) + allow(Unit).to receive(:all).and_return(units) + allow(Unit).to receive(:find).and_return(units.first) + allow(CommonArea).to receive(:all).and_return(common_areas) + SingleCharge.create!(charge_type: :fine, value_cents: 100_11, description: 'Multa por barulho', + issue_date: 5.days.from_now.to_date, unit_id: units.first.id, condo_id: condos.first.id) + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_single_charges(unit_types.first) + + expect(fees).to eq 100_11 + end + end + + it 'nao possui cobranças avulsas' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, + condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_single_charges(unit_types.first) + + expect(fees).to eq 0 + end end end end diff --git a/spec/requests/apis/single_charge_api_spec.rb b/spec/requests/apis/single_charge_api_spec.rb index 00329fa0..85c45246 100644 --- a/spec/requests/apis/single_charge_api_spec.rb +++ b/spec/requests/apis/single_charge_api_spec.rb @@ -42,6 +42,7 @@ unit_types << UnitType.new(id: 1, area: 40, description: 'Apartamento 1 quarto', ideal_fraction: 0.5, condo_id: 1) units = [] units << Unit.new(id: 1, area: 40, floor: 1, number: 1, unit_type_id: 1) + common_areas = [] allow(Condo).to receive(:all).and_return(condos) allow(Condo).to receive(:find).and_return(condos.first) allow(UnitType).to receive(:all).and_return(unit_types) @@ -202,5 +203,37 @@ expect(SingleCharge.count).to eq 1 expect(SingleCharge.last.common_area_id).to eq nil end + + it 'recebe multa com id de área comum ' do + condos = [] + condos << Condo.new(id: 1, name: 'Condo Test', city: 'City Test') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 40, description: 'Apartamento 1 quarto', ideal_fraction: 0.5, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 40, floor: 1, number: 1, unit_type_id: 1) + common_areas = [] + common_areas << CommonArea.new(id: 1, name: 'Academia', + description: 'Uma academia raíz com ventilador apenas para os marombas', + max_occupancy: 20, rules: 'Não pode ser frango', condo_id: 1) + allow(Condo).to receive(:all).and_return(condos) + allow(Condo).to receive(:find).and_return(condos.first) + allow(UnitType).to receive(:all).and_return(unit_types) + allow(UnitType).to receive(:find).and_return(unit_types.first) + allow(Unit).to receive(:all).and_return(units) + allow(Unit).to receive(:find).and_return(units.first) + allow(CommonArea).to receive(:all).and_return(common_areas) + allow(CommonArea).to receive(:find).and_return(common_areas.first) + + single_charge_params = { single_charge: { condo_id: condos.first.id, unit_id: units.first.id, + value_cents: 300_00, issue_date: 5.days.from_now.to_date, + description: 'Multa por barulho após as 23h', + charge_type: :other } } + + post api_v1_single_charges_path, params: single_charge_params + + expect(response.status).to eq 422 + expect(SingleCharge.count).to eq 0 + expect(response.body).to include 'Tipo de Cobrança inválido' + end end end From b2fb222c4657ce3c9d5fc40e6beb2520de22d222 Mon Sep 17 00:00:00 2001 From: sabinopa Date: Tue, 16 Jul 2024 16:37:27 -0300 Subject: [PATCH 2/9] =?UTF-8?q?ignora=20taxas,=20contas=20e=20cobran=C3=A7?= =?UTF-8?q?as=20canceladas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Angelo Maia --- app/models/bill_calculator.rb | 4 +- spec/models/bill_calculator_spec.rb | 46 ++++++++++++++++++++ spec/requests/apis/single_charge_api_spec.rb | 1 - 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/app/models/bill_calculator.rb b/app/models/bill_calculator.rb index 4b6a6ae2..ab6c5e7c 100644 --- a/app/models/bill_calculator.rb +++ b/app/models/bill_calculator.rb @@ -48,6 +48,7 @@ def self.get_last_month_fractions(unit_id) .joins(:shared_fee) .where(shared_fees: { issue_date: ...now }) .where(shared_fees: { issue_date: start_of_last_month..end_of_last_month }) + .where(shared_fees: { status: :active }) end def self.get_last_month_values(unit_type_id) @@ -59,6 +60,7 @@ def self.get_last_month_values(unit_type_id) .joins(:base_fee) .where(base_fees: { charge_day: ...now }) .where(base_fees: { charge_day: start_of_last_month..end_of_last_month }) + .where(base_fees: { status: :active }) end def self.get_last_month_single_charges(unit_id) @@ -66,7 +68,7 @@ def self.get_last_month_single_charges(unit_id) last_month = now.last_month start_of_last_month = last_month.beginning_of_month end_of_last_month = last_month.end_of_month - SingleCharge.where(unit_id:, issue_date: start_of_last_month..end_of_last_month) + SingleCharge.where(unit_id:, issue_date: start_of_last_month..end_of_last_month, status: :active) end def self.check_recurrence(base_fee) diff --git a/spec/models/bill_calculator_spec.rb b/spec/models/bill_calculator_spec.rb index 78cbcc4a..63b793e3 100644 --- a/spec/models/bill_calculator_spec.rb +++ b/spec/models/bill_calculator_spec.rb @@ -193,4 +193,50 @@ end end end + + context 'ignora taxas, contas e cobranças canceladas' do + it 'com sucesso' do + condos = [] + condos << Condo.new(id: 1, name: 'Condo Test', city: 'City Test') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, + condo_id: condos.first.id) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + shared_fee_canceled = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + total_value: 30_000_000_00, condo_id: condos.first.id) + create(:shared_fee_fraction, shared_fee: shared_fee_canceled, unit_id: 1, value_cents: 30_000_00) + shared_fee_canceled.canceled! + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + total_value: 30_000_00, condo_id: condos.first.id) + create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) + base_fee = create(:base_fee, condo_id: condos.first.id) + create(:value, price_cents: 100_00, base_fee_id: base_fee.id) + base_fee_canceled = create(:base_fee, condo_id: condos.first.id) + create(:value, price_cents: 333_00, base_fee_id: base_fee_canceled.id) + base_fee_canceled.canceled! + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + allow(Condo).to receive(:all).and_return(condos) + allow(Condo).to receive(:find).and_return(condos.first) + allow(UnitType).to receive(:all).and_return(unit_types) + allow(UnitType).to receive(:find).and_return(unit_types.first) + allow(Unit).to receive(:all).and_return(units) + allow(Unit).to receive(:find).and_return(units.first) + allow(CommonArea).to receive(:all).and_return([]) + SingleCharge.create!(charge_type: :fine, value_cents: 100_11, description: 'Multa por barulho', + issue_date: 5.days.from_now.to_date, unit_id: units.first.id, condo_id: condos.first.id) + single_charge_canceled = SingleCharge.create!(charge_type: :fine, value_cents: 555_55, + description: 'Multa por barulho', + issue_date: 5.days.from_now.to_date, unit_id: units.first.id, + condo_id: condos.first.id) + single_charge_canceled.canceled! + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_total_fees(units.first) + + expect(fees).to eq 500_11 + end + end + end end diff --git a/spec/requests/apis/single_charge_api_spec.rb b/spec/requests/apis/single_charge_api_spec.rb index 85c45246..a41ad721 100644 --- a/spec/requests/apis/single_charge_api_spec.rb +++ b/spec/requests/apis/single_charge_api_spec.rb @@ -42,7 +42,6 @@ unit_types << UnitType.new(id: 1, area: 40, description: 'Apartamento 1 quarto', ideal_fraction: 0.5, condo_id: 1) units = [] units << Unit.new(id: 1, area: 40, floor: 1, number: 1, unit_type_id: 1) - common_areas = [] allow(Condo).to receive(:all).and_return(condos) allow(Condo).to receive(:find).and_return(condos.first) allow(UnitType).to receive(:all).and_return(unit_types) From 2592d8a4ea4a828fc98e9238896827237a352403 Mon Sep 17 00:00:00 2001 From: sabinopa Date: Tue, 16 Jul 2024 17:26:41 -0300 Subject: [PATCH 3/9] cria validacao de recorrencias Co-authored-by: Angelo Maia --- app/models/bill_calculator.rb | 13 +++-- spec/models/bill_calculator_spec.rb | 79 +++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 7 deletions(-) diff --git a/app/models/bill_calculator.rb b/app/models/bill_calculator.rb index ab6c5e7c..8a4bf93b 100644 --- a/app/models/bill_calculator.rb +++ b/app/models/bill_calculator.rb @@ -75,9 +75,8 @@ def self.check_recurrence(base_fee) recurrence = base_fee.recurrence return check_monthly_recurrence(base_fee) if %w[bimonthly monthly biweekly].include?(recurrence) - return check_yearly_recurrence(base_fee) if %w[semiannual yearly].include?(recurrence) - false + check_yearly_recurrence(base_fee) if %w[semi_annual yearly].include?(recurrence) end def self.check_yearly_recurrence(base_fee) @@ -85,10 +84,10 @@ def self.check_yearly_recurrence(base_fee) charge_day = base_fee.charge_day recurrence = base_fee.recurrence - return ((current_date.month - charge_day.month) % 6).zero? if recurrence == 'semiannual' - return current_date.year != charge_day.year if recurrence == 'yearly' + last_month = current_date.last_month + return ((last_month.month - charge_day.month) % 6).zero? if recurrence == 'semi_annual' - false + last_month != charge_day.month if recurrence == 'yearly' end def self.check_monthly_recurrence(base_fee) @@ -97,8 +96,8 @@ def self.check_monthly_recurrence(base_fee) recurrence = base_fee.recurrence if recurrence == 'bimonthly' - return current_date.month.even? if charge_day.month.even? - return current_date.month.odd? if charge_day.month.odd? + return current_date.last_month.month.even? if charge_day.month.even? + return current_date.last_month.month.odd? if charge_day.month.odd? end true diff --git a/spec/models/bill_calculator_spec.rb b/spec/models/bill_calculator_spec.rb index 63b793e3..246d8322 100644 --- a/spec/models/bill_calculator_spec.rb +++ b/spec/models/bill_calculator_spec.rb @@ -99,6 +99,85 @@ end end + context '.check_recurrence' do + it 'e confere recorrencia para gerar fatura' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', + ideal_fraction: 0.1, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee_yearly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :yearly) + create(:value, price_cents: 100_33, base_fee_id: base_fee_yearly.id) + base_fee_semi_annual = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :semi_annual) + create(:value, price_cents: 100_15, base_fee_id: base_fee_semi_annual.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_base_fees(unit_types.first) + + expect(fees).to eq 200_48 + end + end + + it 'e confere recorrencia quinzenal para gerar fatura' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', + ideal_fraction: 0.1, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee_biweekly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :biweekly) + create(:value, price_cents: 100_33, base_fee_id: base_fee_biweekly.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_base_fees(unit_types.first) + + expect(fees).to eq 200_66 + end + end + end + + it 'e confere recorrencia bimestral para gerar fatura' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', + ideal_fraction: 0.1, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee_bimonthly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :bimonthly) + create(:value, price_cents: 100_33, base_fee_id: base_fee_bimonthly.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_base_fees(unit_types.first) + + expect(fees).to eq 100_33 + end + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_base_fees(unit_types.first) + + expect(fees).to eq 100_33 + end + + travel_to 35.days.from_now do + fees = BillCalculator.calculate_base_fees(unit_types.first) + + expect(fees).to eq 100_33 + end + end + context '.calculate_shared_fees' do it 'e retorna valores de taxas compartilhadas da fatura' do condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') From 51aef706f7b9755bdda80a06f1ede8d9d262f9cf Mon Sep 17 00:00:00 2001 From: sabinopa Date: Wed, 17 Jul 2024 12:11:49 -0300 Subject: [PATCH 4/9] corrige logica de recorrecia no calculo de base fee Co-authored-by: Angelo Maia --- app/models/base_fee_calculator.rb | 63 +++++++ app/models/bill_calculator.rb | 64 +------ spec/jobs/generate_monthly_bill_job_spec.rb | 10 +- spec/models/base_fee_calculator_spec.rb | 183 ++++++++++++++++++++ spec/models/bill_calculator_spec.rb | 124 ------------- 5 files changed, 253 insertions(+), 191 deletions(-) create mode 100644 app/models/base_fee_calculator.rb create mode 100644 spec/models/base_fee_calculator_spec.rb diff --git a/app/models/base_fee_calculator.rb b/app/models/base_fee_calculator.rb new file mode 100644 index 00000000..871c0606 --- /dev/null +++ b/app/models/base_fee_calculator.rb @@ -0,0 +1,63 @@ +class BaseFeeCalculator + def self.total_value(unit_type_id) + total_value = 0 + + get_last_month_values(unit_type_id).each do |value| + next unless check_recurrence(value.base_fee) + + total_value += if value.base_fee.biweekly? + (value.price_cents * 2) + else + value.price_cents + end + end + total_value + end + + def self.get_last_month_values(unit_type_id) + now = Time.zone.now + last_month = now.last_month + start_of_last_month = last_month.beginning_of_month + end_of_last_month = last_month.end_of_month + Value.where(unit_type_id:) + .joins(:base_fee) + .where(base_fees: { charge_day: ...end_of_last_month }) + .where(base_fees: { status: :active }) + end + + def self.check_recurrence(base_fee) + recurrence = base_fee.recurrence + + return check_monthly_recurrence(base_fee) if %w[bimonthly monthly biweekly].include?(recurrence) + + check_yearly_recurrence(base_fee) if %w[semi_annual yearly].include?(recurrence) + end + + def self.check_yearly_recurrence(base_fee) + current_date = Time.zone.today + charge_day = base_fee.charge_day + recurrence = base_fee.recurrence + + last_month = current_date.last_month + return ((last_month.month - charge_day.month) % 6).zero? if recurrence == 'semi_annual' + + return last_month.month == charge_day.month if recurrence == 'yearly' + + false + end + + def self.check_monthly_recurrence(base_fee) + current_date = Time.zone.today + charge_day = base_fee.charge_day + recurrence = base_fee.recurrence + + if recurrence == 'bimonthly' + return current_date.last_month.month.even? if charge_day.month.even? + return current_date.last_month.month.odd? if charge_day.month.odd? + end + + true + end + + private_class_method :check_monthly_recurrence, :check_yearly_recurrence, :check_recurrence, :get_last_month_values +end diff --git a/app/models/bill_calculator.rb b/app/models/bill_calculator.rb index 8a4bf93b..b9c6d493 100644 --- a/app/models/bill_calculator.rb +++ b/app/models/bill_calculator.rb @@ -1,26 +1,11 @@ class BillCalculator def self.calculate_total_fees(unit) - base_fees = calculate_base_fees(unit.unit_type_id) + base_fees = BaseFeeCalculator.total_value(unit.unit_type_id) shared_fees = calculate_shared_fees(unit.id) single_charges = calculate_single_charges(unit.id) base_fees + shared_fees + single_charges end - def self.calculate_base_fees(unit_type_id) - total_value = 0 - - get_last_month_values(unit_type_id).each do |value| - next unless check_recurrence(value.base_fee) - - total_value += if value.base_fee.biweekly? - (value.price_cents * 2) - else - value.price_cents - end - end - total_value - end - def self.calculate_shared_fees(unit_id) total_value = 0 fractions = get_last_month_fractions(unit_id) @@ -51,18 +36,6 @@ def self.get_last_month_fractions(unit_id) .where(shared_fees: { status: :active }) end - def self.get_last_month_values(unit_type_id) - now = Time.zone.now - last_month = now.last_month - start_of_last_month = last_month.beginning_of_month - end_of_last_month = last_month.end_of_month - Value.where(unit_type_id:) - .joins(:base_fee) - .where(base_fees: { charge_day: ...now }) - .where(base_fees: { charge_day: start_of_last_month..end_of_last_month }) - .where(base_fees: { status: :active }) - end - def self.get_last_month_single_charges(unit_id) now = Time.zone.now last_month = now.last_month @@ -71,38 +44,5 @@ def self.get_last_month_single_charges(unit_id) SingleCharge.where(unit_id:, issue_date: start_of_last_month..end_of_last_month, status: :active) end - def self.check_recurrence(base_fee) - recurrence = base_fee.recurrence - - return check_monthly_recurrence(base_fee) if %w[bimonthly monthly biweekly].include?(recurrence) - - check_yearly_recurrence(base_fee) if %w[semi_annual yearly].include?(recurrence) - end - - def self.check_yearly_recurrence(base_fee) - current_date = Time.zone.today - charge_day = base_fee.charge_day - recurrence = base_fee.recurrence - - last_month = current_date.last_month - return ((last_month.month - charge_day.month) % 6).zero? if recurrence == 'semi_annual' - - last_month != charge_day.month if recurrence == 'yearly' - end - - def self.check_monthly_recurrence(base_fee) - current_date = Time.zone.today - charge_day = base_fee.charge_day - recurrence = base_fee.recurrence - - if recurrence == 'bimonthly' - return current_date.last_month.month.even? if charge_day.month.even? - return current_date.last_month.month.odd? if charge_day.month.odd? - end - - true - end - - private_class_method :check_monthly_recurrence, :check_yearly_recurrence, :check_recurrence, :get_last_month_values, - :get_last_month_fractions, :get_last_month_single_charges + private_class_method :get_last_month_fractions, :get_last_month_single_charges end diff --git a/spec/jobs/generate_monthly_bill_job_spec.rb b/spec/jobs/generate_monthly_bill_job_spec.rb index b67a5ed1..0c9cab06 100644 --- a/spec/jobs/generate_monthly_bill_job_spec.rb +++ b/spec/jobs/generate_monthly_bill_job_spec.rb @@ -140,14 +140,14 @@ condo_id: 1) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - first_shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + first_shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Date.today, total_value: 20_000_00, condo_id: condos.first.id) - second_shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 45.days.from_now.to_date, + second_shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 1.month.from_now, total_value: 13_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee: first_shared_fee, unit_id: 1, value_cents: 200_00) create(:shared_fee_fraction, shared_fee: second_shared_fee, unit_id: 1, value_cents: 130_00) - first_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 10.days.from_now) - second_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 45.days.from_now) + first_base_fee = create(:base_fee, condo_id: 1, recurrence: :yearly, charge_day: Date.today) + second_base_fee = create(:base_fee, condo_id: 1, recurrence: :yearly, charge_day: 1.month.from_now) create(:value, price_cents: 150_00, base_fee_id: first_base_fee.id) create(:value, price_cents: 111_11, base_fee_id: second_base_fee.id) allow(Condo).to receive(:all).and_return(condos) @@ -156,7 +156,7 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:find_all_by_condo).and_return(units) - travel_to 65.days.from_now do + travel_to 2.months.from_now do units.each do |unit| condo_id = unit_types.first.condo_id GenerateMonthlyBillJob.perform_now(unit, condo_id) diff --git a/spec/models/base_fee_calculator_spec.rb b/spec/models/base_fee_calculator_spec.rb new file mode 100644 index 00000000..ed2ea501 --- /dev/null +++ b/spec/models/base_fee_calculator_spec.rb @@ -0,0 +1,183 @@ +require 'rails_helper' + +describe BaseFeeCalculator do + context '.calculate_base_fees' do + it 'e retorna valores de taxas condominiais da fatura' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, + condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + total_value: 30_000_00, condo_id: condo.id) + create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) + base_fee = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now) + create(:value, price_cents: 100_00, base_fee_id: base_fee.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 100_00 + end + end + + it 'e nao possui taxas condominiais' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, + condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 0 + end + end + end + + context '.check_recurrence' do + it 'e confere recorrencia anual e semestral para gerar fatura' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', + ideal_fraction: 0.1, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee_yearly = create(:base_fee, condo_id: 1, charge_day: Date.today, recurrence: :yearly) + create(:value, price_cents: 100_33, base_fee_id: base_fee_yearly.id) + base_fee_semi_annual = create(:base_fee, condo_id: 1, charge_day: Date.today, recurrence: :semi_annual) + create(:value, price_cents: 100_15, base_fee_id: base_fee_semi_annual.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 1.month.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 200_48 + end + + travel_to 2.months.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 0 + end + + travel_to 7.months.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 100_15 + end + + travel_to 13.months.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 200_48 + end + end + + it 'e confere recorrencia quinzenal e mensal para gerar fatura' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', + ideal_fraction: 0.1, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee_biweekly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :biweekly) + create(:value, price_cents: 100_33, base_fee_id: base_fee_biweekly.id) + base_fee_monthly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :monthly) + create(:value, price_cents: 50_12, base_fee_id: base_fee_monthly.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 250_78 + end + + travel_to 35.days.from_now.next_month do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 250_78 + end + end + + it 'e confere recorrencia bimestral para gerar fatura' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', + ideal_fraction: 0.1, condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee_bimonthly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :bimonthly) + create(:value, price_cents: 100_33, base_fee_id: base_fee_bimonthly.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 35.days.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 100_33 + end + + travel_to 35.days.from_now.next_month do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 0 + end + + travel_to 35.days.from_now.next_month.next_month do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 100_33 + end + end + end + + context '.check_limited_fees' do + xit 'retorna valores de uma taxa limitada' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, + condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee = create(:base_fee, condo_id: 1, charge_day: Date.today, limited: true, + installments: 10, recurrence: :monthly) + create(:value, price_cents: 100_00, base_fee_id: base_fee.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 1.month.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 100_00 + end + + travel_to 13.months.from_now do + fees = BaseFeeCalculator.total_value(unit_types.first) + + expect(fees).to eq 0 + end + end + end +end \ No newline at end of file diff --git a/spec/models/bill_calculator_spec.rb b/spec/models/bill_calculator_spec.rb index 246d8322..a6d5158f 100644 --- a/spec/models/bill_calculator_spec.rb +++ b/spec/models/bill_calculator_spec.rb @@ -54,130 +54,6 @@ end end - context '.calculate_base_fees' do - it 'e retorna valores de taxas condominiais da fatura' do - condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') - unit_types = [] - unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, - condo_id: 1) - units = [] - units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, - total_value: 30_000_00, condo_id: condo.id) - create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) - base_fee = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now) - create(:value, price_cents: 100_00, base_fee_id: base_fee.id) - allow(Condo).to receive(:find).and_return(condo) - allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) - allow(Unit).to receive(:find).and_return(units.first) - allow(Unit).to receive(:find_all_by_condo).and_return(units) - - travel_to 35.days.from_now do - fees = BillCalculator.calculate_base_fees(unit_types.first) - - expect(fees).to eq 100_00 - end - end - - it 'e nao possui taxas condominiais' do - condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') - unit_types = [] - unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, - condo_id: 1) - units = [] - units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - allow(Condo).to receive(:find).and_return(condo) - allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) - allow(Unit).to receive(:find).and_return(units.first) - allow(Unit).to receive(:find_all_by_condo).and_return(units) - - travel_to 35.days.from_now do - fees = BillCalculator.calculate_base_fees(unit_types.first) - - expect(fees).to eq 0 - end - end - end - - context '.check_recurrence' do - it 'e confere recorrencia para gerar fatura' do - condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') - unit_types = [] - unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', - ideal_fraction: 0.1, condo_id: 1) - units = [] - units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - base_fee_yearly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :yearly) - create(:value, price_cents: 100_33, base_fee_id: base_fee_yearly.id) - base_fee_semi_annual = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :semi_annual) - create(:value, price_cents: 100_15, base_fee_id: base_fee_semi_annual.id) - allow(Condo).to receive(:find).and_return(condo) - allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) - allow(Unit).to receive(:find).and_return(units.first) - allow(Unit).to receive(:find_all_by_condo).and_return(units) - - travel_to 35.days.from_now do - fees = BillCalculator.calculate_base_fees(unit_types.first) - - expect(fees).to eq 200_48 - end - end - - it 'e confere recorrencia quinzenal para gerar fatura' do - condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') - unit_types = [] - unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', - ideal_fraction: 0.1, condo_id: 1) - units = [] - units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - base_fee_biweekly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :biweekly) - create(:value, price_cents: 100_33, base_fee_id: base_fee_biweekly.id) - allow(Condo).to receive(:find).and_return(condo) - allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) - allow(Unit).to receive(:find).and_return(units.first) - allow(Unit).to receive(:find_all_by_condo).and_return(units) - - travel_to 35.days.from_now do - fees = BillCalculator.calculate_base_fees(unit_types.first) - - expect(fees).to eq 200_66 - end - end - end - - it 'e confere recorrencia bimestral para gerar fatura' do - condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') - unit_types = [] - unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', - ideal_fraction: 0.1, condo_id: 1) - units = [] - units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - base_fee_bimonthly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :bimonthly) - create(:value, price_cents: 100_33, base_fee_id: base_fee_bimonthly.id) - allow(Condo).to receive(:find).and_return(condo) - allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) - allow(Unit).to receive(:find).and_return(units.first) - allow(Unit).to receive(:find_all_by_condo).and_return(units) - - travel_to 35.days.from_now do - fees = BillCalculator.calculate_base_fees(unit_types.first) - - expect(fees).to eq 100_33 - end - - travel_to 35.days.from_now do - fees = BillCalculator.calculate_base_fees(unit_types.first) - - expect(fees).to eq 100_33 - end - - travel_to 35.days.from_now do - fees = BillCalculator.calculate_base_fees(unit_types.first) - - expect(fees).to eq 100_33 - end - end - context '.calculate_shared_fees' do it 'e retorna valores de taxas compartilhadas da fatura' do condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') From 598f959d54e86144e52b2de357354947a660c3fd Mon Sep 17 00:00:00 2001 From: sabinopa Date: Wed, 17 Jul 2024 17:51:29 -0300 Subject: [PATCH 5/9] implementa taxas limitadas ao calculo da fatura Co-authored-by: Angelo Maia --- app/models/base_fee.rb | 3 +- app/models/base_fee_calculator.rb | 30 +++++++----- ...20240717190806_add_counter_to_base_fees.rb | 5 ++ db/schema.rb | 3 +- spec/factories/base_fees.rb | 1 + spec/jobs/generate_monthly_bill_job_spec.rb | 37 ++++++++++++++- spec/models/base_fee_calculator_spec.rb | 47 +++++++++++++++---- 7 files changed, 101 insertions(+), 25 deletions(-) create mode 100644 db/migrate/20240717190806_add_counter_to_base_fees.rb diff --git a/app/models/base_fee.rb b/app/models/base_fee.rb index 5e209a25..e14192ec 100644 --- a/app/models/base_fee.rb +++ b/app/models/base_fee.rb @@ -5,12 +5,13 @@ class BaseFee < ApplicationRecord accepts_nested_attributes_for :values validates :name, :description, :charge_day, presence: true validates :interest_rate, numericality: { greater_than_or_equal_to: 0 } - validate :date_is_future? + validate :date_is_future?, on: :create validate :installments_apply? validates :installments, numericality: { greater_than: 0, allow_nil: true } enum status: { active: 0, + paid: 3, canceled: 5 } diff --git a/app/models/base_fee_calculator.rb b/app/models/base_fee_calculator.rb index 871c0606..b7f146a9 100644 --- a/app/models/base_fee_calculator.rb +++ b/app/models/base_fee_calculator.rb @@ -1,23 +1,18 @@ class BaseFeeCalculator def self.total_value(unit_type_id) - total_value = 0 + get_last_month_values(unit_type_id).sum do |value| + next 0 unless check_recurrence(value.base_fee) - get_last_month_values(unit_type_id).each do |value| - next unless check_recurrence(value.base_fee) - - total_value += if value.base_fee.biweekly? - (value.price_cents * 2) - else - value.price_cents - end + fee = value.base_fee + total = fee.biweekly? ? (value.price_cents * 2) : value.price_cents + update_base_fee_counter(fee) if fee.limited? + total end - total_value end def self.get_last_month_values(unit_type_id) now = Time.zone.now last_month = now.last_month - start_of_last_month = last_month.beginning_of_month end_of_last_month = last_month.end_of_month Value.where(unit_type_id:) .joins(:base_fee) @@ -59,5 +54,16 @@ def self.check_monthly_recurrence(base_fee) true end - private_class_method :check_monthly_recurrence, :check_yearly_recurrence, :check_recurrence, :get_last_month_values + def self.update_base_fee_counter(base_fee) + count = base_fee.counter + count += 1 + base_fee.counter = count + + base_fee.paid! if base_fee.counter == base_fee.installments + + base_fee.save + end + + private_class_method :check_monthly_recurrence, :check_yearly_recurrence, :check_recurrence, + :get_last_month_values, :update_base_fee_counter end diff --git a/db/migrate/20240717190806_add_counter_to_base_fees.rb b/db/migrate/20240717190806_add_counter_to_base_fees.rb new file mode 100644 index 00000000..a60be040 --- /dev/null +++ b/db/migrate/20240717190806_add_counter_to_base_fees.rb @@ -0,0 +1,5 @@ +class AddCounterToBaseFees < ActiveRecord::Migration[7.1] + def change + add_column :base_fees, :counter, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index f206228b..c84fbb35 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_07_17_042832) do +ActiveRecord::Schema[7.1].define(version: 2024_07_17_190806) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -77,6 +77,7 @@ t.integer "condo_id" t.integer "installments" t.integer "status", default: 0 + t.integer "counter", default: 0 end create_table "bills", force: :cascade do |t| diff --git a/spec/factories/base_fees.rb b/spec/factories/base_fees.rb index 088be193..005263c2 100644 --- a/spec/factories/base_fees.rb +++ b/spec/factories/base_fees.rb @@ -9,5 +9,6 @@ recurrence { :monthly } condo_id { nil } installments { nil } + counter { 0 } end end diff --git a/spec/jobs/generate_monthly_bill_job_spec.rb b/spec/jobs/generate_monthly_bill_job_spec.rb index 0c9cab06..a9711d2e 100644 --- a/spec/jobs/generate_monthly_bill_job_spec.rb +++ b/spec/jobs/generate_monthly_bill_job_spec.rb @@ -140,13 +140,13 @@ condo_id: 1) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - first_shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Date.today, + first_shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.today, total_value: 20_000_00, condo_id: condos.first.id) second_shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 1.month.from_now, total_value: 13_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee: first_shared_fee, unit_id: 1, value_cents: 200_00) create(:shared_fee_fraction, shared_fee: second_shared_fee, unit_id: 1, value_cents: 130_00) - first_base_fee = create(:base_fee, condo_id: 1, recurrence: :yearly, charge_day: Date.today) + first_base_fee = create(:base_fee, condo_id: 1, recurrence: :yearly, charge_day: Time.zone.today) second_base_fee = create(:base_fee, condo_id: 1, recurrence: :yearly, charge_day: 1.month.from_now) create(:value, price_cents: 150_00, base_fee_id: first_base_fee.id) create(:value, price_cents: 111_11, base_fee_id: second_base_fee.id) @@ -169,6 +169,39 @@ end end + it 'e retorna os valores de todas as contas fixas mensais' do + condos = [] + condos << Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, + condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + first_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: Time.zone.today) + second_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 1.month.from_now) + third_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 2.months.from_now) + create(:value, price_cents: 100_00, base_fee_id: first_base_fee.id) + create(:value, price_cents: 111_11, base_fee_id: second_base_fee.id) + create(:value, price_cents: 333_33, base_fee_id: third_base_fee.id) + allow(Condo).to receive(:all).and_return(condos) + allow(Condo).to receive(:find).and_return(condos.first) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + travel_to 5.months.from_now do + units.each do |unit| + condo_id = unit_types.first.condo_id + GenerateMonthlyBillJob.perform_now(unit, condo_id) + end + + expect(Bill.count).to eq 1 + expect(Bill.first.issue_date).to eq Time.zone.today.beginning_of_month + expect(Bill.first.due_date).to eq Time.zone.today.beginning_of_month + 9.days + expect(Bill.first.total_value_cents).to eq 544_44 + end + end + it 'e não possui taxas para a fatura atual' do condos = [] condos << Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') diff --git a/spec/models/base_fee_calculator_spec.rb b/spec/models/base_fee_calculator_spec.rb index ed2ea501..37a39ed4 100644 --- a/spec/models/base_fee_calculator_spec.rb +++ b/spec/models/base_fee_calculator_spec.rb @@ -54,9 +54,9 @@ ideal_fraction: 0.1, condo_id: 1) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - base_fee_yearly = create(:base_fee, condo_id: 1, charge_day: Date.today, recurrence: :yearly) + base_fee_yearly = create(:base_fee, condo_id: 1, charge_day: Time.zone.today, recurrence: :yearly) create(:value, price_cents: 100_33, base_fee_id: base_fee_yearly.id) - base_fee_semi_annual = create(:base_fee, condo_id: 1, charge_day: Date.today, recurrence: :semi_annual) + base_fee_semi_annual = create(:base_fee, condo_id: 1, charge_day: Time.zone.today, recurrence: :semi_annual) create(:value, price_cents: 100_15, base_fee_id: base_fee_semi_annual.id) allow(Condo).to receive(:find).and_return(condo) allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) @@ -152,14 +152,14 @@ end context '.check_limited_fees' do - xit 'retorna valores de uma taxa limitada' do + it 'retorna valores de uma taxa limitada mensal' do condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') unit_types = [] unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, condo_id: 1) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - base_fee = create(:base_fee, condo_id: 1, charge_day: Date.today, limited: true, + base_fee = create(:base_fee, condo_id: 1, charge_day: Time.zone.today, limited: true, installments: 10, recurrence: :monthly) create(:value, price_cents: 100_00, base_fee_id: base_fee.id) allow(Condo).to receive(:find).and_return(condo) @@ -167,17 +167,46 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:find_all_by_condo).and_return(units) - travel_to 1.month.from_now do + 10.times do |i| + travel_to Time.zone.today + (i + 1).months do + fees = BaseFeeCalculator.total_value(unit_types.first) + expect(fees).to eq 100_00 + end + end + + travel_to Time.zone.today + 13.months do fees = BaseFeeCalculator.total_value(unit_types.first) + expect(fees).to eq 0 + end + end - expect(fees).to eq 100_00 + it 'retorna valores de uma taxa limitada bimestral' do + condo = Condo.new(id: 1, name: 'Prédio lindo', city: 'Cidade maravilhosa') + unit_types = [] + unit_types << UnitType.new(id: 1, area: 30, description: 'Apartamento 1 quarto', ideal_fraction: 0.1, + condo_id: 1) + units = [] + units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) + base_fee = create(:base_fee, condo_id: 1, charge_day: Time.zone.today, limited: true, + installments: 10, recurrence: :bimonthly) + create(:value, price_cents: 100_00, base_fee_id: base_fee.id) + allow(Condo).to receive(:find).and_return(condo) + allow(UnitType).to receive(:find_all_by_condo).and_return(unit_types) + allow(Unit).to receive(:find).and_return(units.first) + allow(Unit).to receive(:find_all_by_condo).and_return(units) + + 20.times do |i| + travel_to Time.zone.today + (i + 1).months do + fees = BaseFeeCalculator.total_value(unit_types.first) + expect(fees).to eq 0 if i.odd? + expect(fees).to eq 100_00 if i.even? + end end - travel_to 13.months.from_now do + travel_to Time.zone.today + 13.months do fees = BaseFeeCalculator.total_value(unit_types.first) - expect(fees).to eq 0 end end end -end \ No newline at end of file +end From 2e8b393cf9009cd2e0337eda2192585d6b92b1af Mon Sep 17 00:00:00 2001 From: sabinopa Date: Thu, 18 Jul 2024 10:21:27 -0300 Subject: [PATCH 6/9] =?UTF-8?q?adiciona=20aluguel=20ao=20c=C3=A1lculo=20de?= =?UTF-8?q?=20fatura.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Angelo Maia --- app/models/bill_calculator.rb | 18 +++++++++-- spec/factories/base_fees.rb | 2 +- spec/factories/shared_fees.rb | 2 +- spec/factories/single_charges.rb | 2 +- spec/models/base_fee_calculator_spec.rb | 24 +++++++-------- spec/models/bill_calculator_spec.rb | 40 ++++++++++++++----------- 6 files changed, 54 insertions(+), 34 deletions(-) diff --git a/app/models/bill_calculator.rb b/app/models/bill_calculator.rb index b9c6d493..98039f6e 100644 --- a/app/models/bill_calculator.rb +++ b/app/models/bill_calculator.rb @@ -3,7 +3,8 @@ def self.calculate_total_fees(unit) base_fees = BaseFeeCalculator.total_value(unit.unit_type_id) shared_fees = calculate_shared_fees(unit.id) single_charges = calculate_single_charges(unit.id) - base_fees + shared_fees + single_charges + rent_fee = check_rent_fee(unit.id) + base_fees + shared_fees + single_charges + rent_fee end def self.calculate_shared_fees(unit_id) @@ -44,5 +45,18 @@ def self.get_last_month_single_charges(unit_id) SingleCharge.where(unit_id:, issue_date: start_of_last_month..end_of_last_month, status: :active) end - private_class_method :get_last_month_fractions, :get_last_month_single_charges + def self.check_rent_fee(unit_id) + rent_fee = get_last_month_rent_fee(unit_id) + rent_fee&.value_cents || 0 + end + + def self.get_last_month_rent_fee(unit_id) + now = Time.zone.now + last_month = now.last_month + start_of_last_month = last_month.beginning_of_month + end_of_last_month = last_month.end_of_month + RentFee.find_by(unit_id:, issue_date: start_of_last_month..end_of_last_month, status: :active) + end + + private_class_method :get_last_month_fractions, :get_last_month_single_charges, :get_last_month_rent_fee end diff --git a/spec/factories/base_fees.rb b/spec/factories/base_fees.rb index 005263c2..adf325d3 100644 --- a/spec/factories/base_fees.rb +++ b/spec/factories/base_fees.rb @@ -5,7 +5,7 @@ interest_rate { 2 } late_fine { 20 } limited { false } - charge_day { 10.days.from_now } + charge_day { Time.zone.now } recurrence { :monthly } condo_id { nil } installments { nil } diff --git a/spec/factories/shared_fees.rb b/spec/factories/shared_fees.rb index 18105d08..e332bc3b 100644 --- a/spec/factories/shared_fees.rb +++ b/spec/factories/shared_fees.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :shared_fee do description { 'Conta de Luz' } - issue_date { 10.days.from_now.to_date } + issue_date { Time.zone.now } total_value_cents { 1000 } condo_id { nil } end diff --git a/spec/factories/single_charges.rb b/spec/factories/single_charges.rb index dc89321e..b2671386 100644 --- a/spec/factories/single_charges.rb +++ b/spec/factories/single_charges.rb @@ -2,7 +2,7 @@ factory :single_charge do unit_id { 1 } value_cents { 5000 } - issue_date { 5.days.from_now.to_date } + issue_date { Time.zone.now } description { 'Detalhes de uma cobrança avulsa ' } charge_type { 0 } condo_id { 1 } diff --git a/spec/models/base_fee_calculator_spec.rb b/spec/models/base_fee_calculator_spec.rb index f33b6dd7..2d6212d2 100644 --- a/spec/models/base_fee_calculator_spec.rb +++ b/spec/models/base_fee_calculator_spec.rb @@ -9,17 +9,17 @@ unit_ids: [1]) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.now, total_value: 30_000_00, condo_id: condo.id) create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) - base_fee = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now) + base_fee = create(:base_fee, condo_id: 1, charge_day: Time.zone.now) create(:value, price_cents: 100_00, base_fee_id: base_fee.id) allow(Condo).to receive(:find).and_return(condo) allow(UnitType).to receive(:all).and_return(unit_types) allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 100_00 @@ -38,7 +38,7 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 0 @@ -95,22 +95,22 @@ unit_ids: [1]) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - base_fee_biweekly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :biweekly) + base_fee_biweekly = create(:base_fee, condo_id: 1, charge_day: Time.zone.now, recurrence: :biweekly) create(:value, price_cents: 100_33, base_fee_id: base_fee_biweekly.id) - base_fee_monthly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :monthly) + base_fee_monthly = create(:base_fee, condo_id: 1, charge_day: Time.zone.now, recurrence: :monthly) create(:value, price_cents: 50_12, base_fee_id: base_fee_monthly.id) allow(Condo).to receive(:find).and_return(condo) allow(UnitType).to receive(:all).and_return(unit_types) allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 250_78 end - travel_to 35.days.from_now.next_month do + travel_to 2.months.from_now do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 250_78 @@ -124,26 +124,26 @@ unit_ids: [1]) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - base_fee_bimonthly = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now, recurrence: :bimonthly) + base_fee_bimonthly = create(:base_fee, condo_id: 1, charge_day: Time.zone.now, recurrence: :bimonthly) create(:value, price_cents: 100_33, base_fee_id: base_fee_bimonthly.id) allow(Condo).to receive(:find).and_return(condo) allow(UnitType).to receive(:all).and_return(unit_types) allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 100_33 end - travel_to 35.days.from_now.next_month do + travel_to 2.months.from_now do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 0 end - travel_to 35.days.from_now.next_month.next_month do + travel_to 3.months.from_now do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 100_33 diff --git a/spec/models/bill_calculator_spec.rb b/spec/models/bill_calculator_spec.rb index 13cfd1db..ebcf7f18 100644 --- a/spec/models/bill_calculator_spec.rb +++ b/spec/models/bill_calculator_spec.rb @@ -3,6 +3,10 @@ describe BillCalculator do context '.calculate_total_fees' do it 'e retorna valor total da fatura' do + cpf = CPF.generate + allow(Faraday).to receive(:get).and_return(instance_double('Faraday::Response', success?: true)) + create(:property_owner, email: 'propertyownertest@mail.com', password: '123456', + document_number: cpf) condos = [] condos << Condo.new(id: 1, name: 'Condo Test', city: 'City Test') unit_types = [] @@ -11,22 +15,24 @@ units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: '11', unit_type_id: 1, condo_id: 1, condo_name: 'Prédio lindo', tenant_id: 1, owner_id: 1, description: 'Com varanda') - shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.today, total_value: 30_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) - base_fee = create(:base_fee, condo_id: condos.first.id) + base_fee = create(:base_fee, condo_id: condos.first.id, charge_day: Time.zone.today) create(:value, price_cents: 100_00, base_fee_id: base_fee.id) + create(:rent_fee, owner_id: 1, tenant_id: 1, unit_id: 1, value_cents: 120_000, + issue_date: 1.day.from_now, fine_cents: 5000, fine_interest: 10, condo_id: 1) allow(Condo).to receive(:find).and_return(condos.first) allow(UnitType).to receive(:all).and_return(unit_types) allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) SingleCharge.create!(charge_type: :fine, value_cents: 100_11, description: 'Multa por barulho', - issue_date: 5.days.from_now.to_date, unit_id: units.first.id, condo_id: condos.first.id) + issue_date: 1.day.from_now, unit_id: units.first.id, condo_id: condos.first.id) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BillCalculator.calculate_total_fees(units.first) - expect(fees).to eq 500_11 + expect(fees).to eq 170_011 end end @@ -43,7 +49,7 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BillCalculator.calculate_total_fees(units.first) expect(fees).to eq 0 @@ -60,17 +66,17 @@ units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: '11', unit_type_id: 1, condo_id: 1, condo_name: 'Prédio lindo', tenant_id: 1, owner_id: 1, description: 'Com varanda') - shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.now, total_value: 30_000_00, condo_id: condo.id) create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) - base_fee = create(:base_fee, condo_id: 1, charge_day: 10.days.from_now) + base_fee = create(:base_fee, condo_id: 1, charge_day: Time.zone.now) create(:value, price_cents: 100_00, base_fee_id: base_fee.id) allow(Condo).to receive(:find).and_return(condo) allow(UnitType).to receive(:all).and_return(unit_types) allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BillCalculator.calculate_shared_fees(unit_types.first) expect(fees).to eq 300_00 @@ -90,7 +96,7 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BillCalculator.calculate_shared_fees(unit_types.first) expect(fees).to eq 0 @@ -121,7 +127,7 @@ SingleCharge.create!(charge_type: :fine, value_cents: 100_11, description: 'Multa por barulho', issue_date: 5.days.from_now.to_date, unit_id: units.first.id, condo_id: condos.first.id) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BillCalculator.calculate_single_charges(unit_types.first) expect(fees).to eq 100_11 @@ -141,7 +147,7 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BillCalculator.calculate_single_charges(unit_types.first) expect(fees).to eq 0 @@ -158,11 +164,11 @@ condo_id: condos.first.id) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - shared_fee_canceled = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + shared_fee_canceled = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.now, total_value: 30_000_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee: shared_fee_canceled, unit_id: 1, value_cents: 30_000_00) shared_fee_canceled.canceled! - shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.now, total_value: 30_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) base_fee = create(:base_fee, condo_id: condos.first.id) @@ -177,14 +183,14 @@ allow(Unit).to receive(:find).and_return(units.first) allow(CommonArea).to receive(:all).and_return([]) SingleCharge.create!(charge_type: :fine, value_cents: 100_11, description: 'Multa por barulho', - issue_date: 5.days.from_now.to_date, unit_id: units.first.id, condo_id: condos.first.id) + issue_date: Time.zone.now, unit_id: units.first.id, condo_id: condos.first.id) single_charge_canceled = SingleCharge.create!(charge_type: :fine, value_cents: 555_55, description: 'Multa por barulho', - issue_date: 5.days.from_now.to_date, unit_id: units.first.id, + issue_date: Time.zone.now, unit_id: units.first.id, condo_id: condos.first.id) single_charge_canceled.canceled! - travel_to 35.days.from_now do + travel_to 1.month.from_now do fees = BillCalculator.calculate_total_fees(units.first) expect(fees).to eq 500_11 From ce4893619b4caf131e661eae755d592a3ffe5f42 Mon Sep 17 00:00:00 2001 From: sabinopa Date: Thu, 18 Jul 2024 10:38:01 -0300 Subject: [PATCH 7/9] ajusta datas do teste de jobs apos o merge Co-authored-by: Angelo Maia --- spec/jobs/generate_monthly_bill_job_spec.rb | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/spec/jobs/generate_monthly_bill_job_spec.rb b/spec/jobs/generate_monthly_bill_job_spec.rb index 98697e35..cb77a028 100644 --- a/spec/jobs/generate_monthly_bill_job_spec.rb +++ b/spec/jobs/generate_monthly_bill_job_spec.rb @@ -13,7 +13,7 @@ units << Unit.new(id: 1, area: 100, floor: 1, number: '11', unit_type_id: 1, condo_id: 1, condo_name: 'Prédio lindo', tenant_id: 1, owner_id: 1, description: 'Com varanda') - shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.now, total_value: 30_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) base_fee = create(:base_fee, condo_id: 1) @@ -24,7 +24,7 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do units.each do |unit| GenerateMonthlyBillJob.perform_now(unit, condo.id) end @@ -56,9 +56,9 @@ condo_name: 'Outro prédio', tenant_id: 2, owner_id: 3, description: 'Com varanda') second_units << Unit.new(id: 4, area: 400, floor: 4, number: '41', unit_type_id: 2, condo_id: 2, condo_name: 'Outro prédio', tenant_id: 2, owner_id: 4, description: 'Com varanda') - first_shared_fee = create(:shared_fee, description: 'Conta de Luz', issue_date: 10.days.from_now.to_date, + first_shared_fee = create(:shared_fee, description: 'Conta de Luz', issue_date: Time.zone.now, total_value: 30_000_00, condo_id: condos.first.id) - second_shared_fee = create(:shared_fee, description: 'Conta de Agua', issue_date: 5.days.from_now.to_date, + second_shared_fee = create(:shared_fee, description: 'Conta de Agua', issue_date: Time.zone.now, total_value: 25_000_00, condo_id: condos.last.id) create(:shared_fee_fraction, shared_fee: first_shared_fee, unit_id: 1, value_cents: 111_00) @@ -81,7 +81,7 @@ second_units.last) allow(Unit).to receive(:all).and_return(first_units, second_units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do first_units.each do |unit| condo_id = first_unit_types.first.condo_id GenerateMonthlyBillJob.perform_now(unit, condo_id) @@ -110,12 +110,12 @@ units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: '11', unit_type_id: 1, condo_id: 1, condo_name: 'Prédio lindo', tenant_id: 1, owner_id: 1, description: 'Com varanda') - shared_fee = create(:shared_fee, description: 'Descrição', issue_date: 10.days.from_now.to_date, + shared_fee = create(:shared_fee, description: 'Descrição', issue_date: Time.zone.now, total_value: 30_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee:, unit_id: 1, value_cents: 300_00) - first_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 10.days.from_now) - second_base_fee = create(:base_fee, condo_id: 1, recurrence: :biweekly, charge_day: 2.days.from_now) - third_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 45.days.from_now) + first_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: Time.zone.now) + second_base_fee = create(:base_fee, condo_id: 1, recurrence: :biweekly, charge_day: Time.zone.now) + third_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 2.months.from_now) create(:value, price_cents: 100_00, base_fee_id: first_base_fee.id) create(:value, price_cents: 200_00, base_fee_id: second_base_fee.id) create(:value, price_cents: 90_00, base_fee_id: third_base_fee.id) @@ -125,7 +125,7 @@ allow(Unit).to receive(:find).and_return(units.first) allow(Unit).to receive(:all).and_return(units) - travel_to 35.days.from_now do + travel_to 1.month.from_now do units.each do |unit| condo_id = unit_types.first.condo_id GenerateMonthlyBillJob.perform_now(unit, condo_id) @@ -153,7 +153,7 @@ total_value: 13_000_00, condo_id: condos.first.id) create(:shared_fee_fraction, shared_fee: first_shared_fee, unit_id: 1, value_cents: 200_00) create(:shared_fee_fraction, shared_fee: second_shared_fee, unit_id: 1, value_cents: 130_00) - first_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: Time.zone.now) + first_base_fee = create(:base_fee, condo_id: 1, recurrence: :yearly, charge_day: Time.zone.now) second_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 1.month.from_now) create(:value, price_cents: 150_00, base_fee_id: first_base_fee.id) create(:value, price_cents: 111_11, base_fee_id: second_base_fee.id) @@ -183,7 +183,7 @@ unit_ids: [1]) units = [] units << Unit.new(id: 1, area: 100, floor: 1, number: 1, unit_type_id: 1) - first_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: Time.zone.today) + first_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: Time.zone.now) second_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 1.month.from_now) third_base_fee = create(:base_fee, condo_id: 1, recurrence: :monthly, charge_day: 2.months.from_now) create(:value, price_cents: 100_00, base_fee_id: first_base_fee.id) From 9f535529afe0ef6163b8801c29da929c8ca06cf9 Mon Sep 17 00:00:00 2001 From: sabinopa Date: Thu, 18 Jul 2024 15:03:32 -0300 Subject: [PATCH 8/9] ajusta teste para verificar counter Co-authored-by: Angelo Maia --- spec/models/base_fee_calculator_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/models/base_fee_calculator_spec.rb b/spec/models/base_fee_calculator_spec.rb index 2d6212d2..f56e7fd5 100644 --- a/spec/models/base_fee_calculator_spec.rb +++ b/spec/models/base_fee_calculator_spec.rb @@ -171,12 +171,14 @@ travel_to Time.zone.today + (i + 1).months do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 100_00 + expect(BaseFee.find(base_fee.id).counter).to eq i + 1 end end travel_to Time.zone.today + 13.months do fees = BaseFeeCalculator.total_value(unit_types.first) expect(fees).to eq 0 + expect(BaseFee.find(base_fee.id).paid?).to eq true end end From 8baf21391c68f648b049f2b9be143158a3b17f74 Mon Sep 17 00:00:00 2001 From: sabinopa Date: Thu, 18 Jul 2024 15:42:48 -0300 Subject: [PATCH 9/9] =?UTF-8?q?Corrige=20testes=20ap=C3=B3s=20o=20merge.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Angelo Maia --- app/models/unit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/unit.rb b/app/models/unit.rb index ed042513..c149716d 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -44,7 +44,7 @@ def self.find_all_by_owner(cpf) end def calculate_values(bill) - bill.base_fee_value_cents = BillCalculator.calculate_base_fees(unit_type_id) + bill.base_fee_value_cents = BaseFeeCalculator.total_value(unit_type_id) bill.shared_fee_value_cents = BillCalculator.calculate_shared_fees(id) bill.total_value_cents = BillCalculator.calculate_total_fees(self) bill