Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/v1/event_procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index
params: event_procedure_permitted_query_params
).event_procedures

total_amount_cents = EventProcedures::TotalAmountCents.call(total_amount_cents_params)
total_amount_cents = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures)

render json: {
total: total_amount_cents.total,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/pdf_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def event_procedures_pdf
scope: authorized_scope,
params: permitted_query_params
).event_procedures
total_amount_cents = EventProcedures::TotalAmountCents.call(total_amount_cents_params)
total_amount_cents = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures)

PdfGeneratorService.new(
relation: event_procedures,
Expand Down
27 changes: 12 additions & 15 deletions app/operations/event_procedures/total_amount_cents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@

module EventProcedures
class TotalAmountCents < Actor
input :user_id, type: Integer
input :month, type: String, allow_nil: true
input :year, type: String, allow_nil: true
input :event_procedures

output :total, type: String
output :payd, type: String
output :unpaid, type: String

def call
self.total = calculate_total
self.payd = calculate_paid
self.unpaid = calculate_unpaid
end

private
procedures_by_id = Procedure.where(id: event_procedures.pluck(:procedure_id)).index_by(&:id)

def calculate_total
Money.new(SumAmountQuery.call(user_id:, month:, year:), "BRL").format
self.total = calculate_amount(event_procedures, procedures_by_id)
self.payd = calculate_amount(event_procedures.select(&:payd), procedures_by_id)
self.unpaid = calculate_amount(event_procedures.reject(&:payd), procedures_by_id)
end

def calculate_paid
Money.new(SumAmountQuery.call(user_id:, month:, year:, payd: true), "BRL").format
def convert_money(amount_cents)
Money.new(amount_cents, "BRL").format
end

def calculate_unpaid
Money.new(SumAmountQuery.call(user_id:, month:, year:, payd: false), "BRL").format
def calculate_amount(filtered_event_procedures, procedures_by_id)
total_cents = filtered_event_procedures.sum do |event_procedure|
procedures_by_id[event_procedure.procedure_id]&.amount_cents.to_i
end
convert_money(total_cents)
end
end
end
86 changes: 13 additions & 73 deletions spec/operations/event_procedures/total_amount_cents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,31 @@
describe ".result" do
it "is successful" do
user = create(:user)

expect(described_class.result(user_id: user.id, month: nil, year: nil)).to be_success
event_procedure = create(:event_procedure, user_id: user.id)
expect(described_class.result(event_procedures: [event_procedure])).to be_success
end

it "returns the amount_cents total, paid and unpaid of event_procedures" do
user = create(:user)
_payd_amount_cents = create_list(
procedure_5000 = create(:procedure, amount_cents: 5000)
procedure_2000 = create(:procedure, amount_cents: 2000)
payd_amount_cents = create_list(
:event_procedure, 3,
procedure: create(:procedure, amount_cents: 5000),
procedure: procedure_5000,
user: user,
payd: true
)
_unpayd_event_procedure = create(
:event_procedure,
procedure: create(:procedure, amount_cents: 2000),
unpayd_event_procedure = create_list(
:event_procedure, 2,
procedure: procedure_2000,
user: user,
payd: false
)
total_amount_cents = described_class.call(user_id: user.id, month: nil, year: nil)

expect(total_amount_cents.total).to eq("R$170.00")
event_procedures = payd_amount_cents + unpayd_event_procedure
total_amount_cents = described_class.call(event_procedures: event_procedures)
expect(total_amount_cents.total).to eq("R$190.00")
expect(total_amount_cents.payd).to eq("R$150.00")
expect(total_amount_cents.unpaid).to eq("R$20.00")
end

it "calculates the total, payd, and unpaid amounts for a specific month" do
user = create(:user)
procedure_1000 = create(:procedure, amount_cents: 1000)
procedure_2000 = create(:procedure, amount_cents: 2000)

_event_procedure_jan = create(
:event_procedure, procedure: procedure_1000,
date: "31/01/2023",
payd: true,
user: user
)
_payd_event_procedure_feb = create(
:event_procedure, procedure: procedure_2000,
date: "25/02/2023",
payd: true,
user: user
)
_unpayd_event_procedure_feb = create(
:event_procedure, procedure: procedure_1000,
date: "25/02/2023",
payd: false,
user: user
)

total_amount_cents = described_class.call(user_id: user.id, month: "2", year: nil)

expect(total_amount_cents.total).to eq("R$30.00")
expect(total_amount_cents.payd).to eq("R$20.00")
expect(total_amount_cents.unpaid).to eq("R$10.00")
end

it "calculates the total, payd, and unpaid amounts for a specific year" do
user = create(:user)
procedure_1000 = create(:procedure, amount_cents: 1000)
procedure_2000 = create(:procedure, amount_cents: 2000)

_event_procedure_2023 = create(
:event_procedure, procedure: procedure_1000,
date: "31/01/2023",
payd: true,
user: user
)
_payd_event_procedure_2024 = create(
:event_procedure, procedure: procedure_2000,
date: "25/02/2024",
payd: true,
user: user
)
_unpayd_event_procedure_2024 = create(
:event_procedure, procedure: procedure_1000,
date: "25/02/2024",
payd: false,
user: user
)

total_amount_cents = described_class.call(user_id: user.id, month: nil, year: "2024")

expect(total_amount_cents.total).to eq("R$30.00")
expect(total_amount_cents.payd).to eq("R$20.00")
expect(total_amount_cents.unpaid).to eq("R$10.00")
expect(total_amount_cents.unpaid).to eq("R$40.00")
end
end
end
2 changes: 1 addition & 1 deletion spec/pdfs/event_procedures_report_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
it "generates a report with the correct content" do
user = create(:user)
pdf = Prawn::Document.new
amount = EventProcedures::TotalAmountCents.call(user_id: user.id, month: nil, year: nil)
event_procedures = create_list(:event_procedure, 3, user_id: user.id)
amount = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures)

described_class.new(
pdf: pdf, amount: amount, items: event_procedures, title: "Procedimentos", email: user.email
Expand Down
6 changes: 2 additions & 4 deletions spec/pdfs/footer_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
RSpec.describe FooterPdf, type: :pdf do
it "generates a footer with the correct content" do
user = create(:user)
create_list(:event_procedure, 3, user_id: user.id)
event_procedures = create_list(:event_procedure, 3, user_id: user.id)
total_amount_cents = EventProcedures::TotalAmountCents.call(
user_id: user.id,
month: nil,
year: nil
event_procedures: event_procedures
)
pdf = Prawn::Document.new

Expand Down
2 changes: 1 addition & 1 deletion spec/services/pdf_generator_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
it "generates event_procedures pdf" do
user = create(:user)
event_procedures = create_list(:event_procedure, 11)
amount = EventProcedures::TotalAmountCents.call(user_id: event_procedures.first.user_id, month: nil, year: nil)
amount = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures)
pdf = described_class.new(
relation: event_procedures, amount: amount, entity_name: "event_procedures", email: user.email
).generate_pdf
Expand Down