Skip to content

Commit

Permalink
move membership invoice generation into model
Browse files Browse the repository at this point in the history
  • Loading branch information
njaeggi committed Aug 21, 2024
1 parent 1bb0213 commit fd540fb
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 210 deletions.
20 changes: 17 additions & 3 deletions app/controllers/people/membership_invoices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,24 @@ def create_invoice
end

def enqueue_membership_invoice_job
membership_invoice_generator = Invoices::Abacus::MembershipInvoiceGenerator.new(person, date, @external_invoice, invoice_form.new_entry, invoice_form.discount)
membership_invoice = membership_invoice_generator.generate_membership_invoice
membership_invoice = @external_invoice.build_membership_invoice(invoice_form.discount, invoice_form.new_entry, invoice_form.reference_date)

CreateMembershipInvoiceJob.new(@external_invoice, date, membership_invoice, new_entry: invoice_form.new_entry, discount: invoice_form.discount).enqueue! if membership_invoice.present?
require 'pry'; binding.pry # rubocop:disable Style/Semicolon,Lint/Debugger
if membership_invoice.is_a?(Invoices::Abacus::MembershipInvoice)
CreateMembershipInvoiceJob.new(@external_invoice.id, invoice_form.discount, invoice_form.new_entry, invoice_form.reference_date).enqueue!
else
handle_invoice_generation_error(membership_invoice)
end
end

def handle_invoice_generation_error(membership_invoice)
external_invoice.update!(state: :error)
HitobitoLogEntry.create!(
message: membership_invoice,
level: :error,
category: "rechnungen",
subject: external_invoice
)
end

def invoice_form = @invoice_form ||= People::Membership::InvoiceForm.new({}, person)
Expand Down
94 changes: 0 additions & 94 deletions app/domain/invoices/abacus/membership_invoice_generator.rb

This file was deleted.

15 changes: 10 additions & 5 deletions app/jobs/create_membership_invoice_job.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class CreateMembershipInvoiceJob < BaseJob
attr_reader :external_invoice, :date, :membership_invoice
self.parameters = [:external_invoice_id, :date, :discount, :new_entry]

def initialize(external_invoice, date, membership_invoice)
@external_invoice = external_invoice
def initialize(external_invoice_id, date, discount, new_entry)
@external_invoice_id = external_invoice_id
@date = date
@membership_invoice = membership_invoice
@discount = discount
@new_entry = new_entry
end

def perform
Expand Down Expand Up @@ -38,6 +39,10 @@ def sales_order(membership_invoice)
)
end

def external_invoice = @external_invoice ||= ExternalInvoice.find(@external_invoice_id)

def membership_invoice = @membership_invoice ||= external_invoice.build_membership_invoice(@discount, @new_entry, @date)

def subject = @subject ||= Invoices::Abacus::Subject.new(person)

def sales_order_interface = @sales_order_interface ||= Invoices::Abacus::SalesOrderInterface.new(client)
Expand All @@ -46,5 +51,5 @@ def subject_interface = @subject_interface ||= Invoices::Abacus::SubjectInterfac

def client = @client ||= Invoices::Abacus::Client.new

def person = @person ||= Person.with_membership_years("people.*", date.beginning_of_year).find(external_invoice.person.id)
def person = @person ||= Person.with_membership_years("people.*", @date.beginning_of_year).find(external_invoice.person.id)
end
75 changes: 75 additions & 0 deletions app/models/external_invoice/sac_membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,79 @@ class ExternalInvoice::SacMembership < ExternalInvoice
def title
I18n.t("invoices.sac_memberships.title", year: year)
end

def build_membership_invoice(discount, new_entry, reference_date)
@date = reference_date
membership_invoice = membership_invoice(discount, new_entry, reference_date)

if !membership_invoice.invoice?
I18n.t(".people.membership_invoices.no_invoice_possible")
elsif memberships.blank?
I18n.t(".people.membership_invoices.no_invoice_possible")
else
membership_invoice
end
end

private

def membership_invoice(discount, new_entry, reference_date)
@membership_invoice ||= Invoices::Abacus::MembershipInvoice.new(
member,
memberships,
new_entry: new_entry,
discount: discount
)
end

def memberships
@memberships ||= if stammsektion?
active_memberships
elsif neuanmeldung_stammsektion?
neuanmeldung_stammsektion_memberships
elsif zusatzsektion?
zusatzsektion_memberships
else
[]
end
end

def active_memberships
[member.membership_from_role(sac_member.stammsektion_role)] +
sac_member.zusatzsektion_roles.map { |r| member.membership_from_role(r) }
end

def neuanmeldung_stammsektion_memberships
[member.membership_from_role(sac_member.neuanmeldung_stammsektion_role, main: true)]
end

def zusatzsektion_memberships
(sac_member.zusatzsektion_roles + sac_member.neuanmeldung_zusatzsektion_roles)
.select { |role| role.layer_group == link.layer_group }
.map { |r| member.membership_from_role(r) }
end

def handle_invoice_generation_error(message)
update!(state: :error)
HitobitoLogEntry.create!(
message: message,
level: :error,
category: "rechnungen",
subject: @invoice
)
end

def stammsektion? = link.layer_group == sac_member.stammsektion_role&.layer_group

def neuanmeldung_stammsektion? = link.layer_group == sac_member.neuanmeldung_stammsektion_role&.layer_group

def zusatzsektion? = (sac_member.zusatzsektion_roles + sac_member.neuanmeldung_zusatzsektion_roles).map(&:layer_group).include?(link.layer_group)

def sac_member = @sac_member ||= People::SacMembership.new(person_with_membership_years, date: @date)

def member = @member ||= Invoices::SacMemberships::Member.new(person_with_membership_years, context)

def context = @context ||= Invoices::SacMemberships::Context.new(@date)

def person_with_membership_years = @person ||= Person.with_membership_years("people.*", @date.beginning_of_year).find(person.id)
end
3 changes: 2 additions & 1 deletion spec/controllers/people/external_invoices_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ def check_presence_of_cancel_button_after
end

it "does not show the cancellation button" do
require 'pry'; binding.pry # rubocop:disable Style/Semicolon,Lint/Debugger
invoice
expect(ExternalInvoice.where(person_id: person.id).count).to eq(1)
expect(ExternalInvoice.where(person_id: person.id).count).to eq(2)

check_presence_of_cancel_button_after { invoice.update!(state: "cancelled") }
check_presence_of_cancel_button_after { invoice.update!(state: "error") }
Expand Down
103 changes: 0 additions & 103 deletions spec/domain/invoices/abacus/membership_invoice_generator_spec.rb

This file was deleted.

7 changes: 3 additions & 4 deletions spec/jobs/create_membership_invoice_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
let(:person) { people(:mitglied) }
let(:person_with_years) { context.people_with_membership_years.find(person.id) }
let(:abacus_client) { instance_double(Invoices::Abacus::Client) }
let(:membership_invoice) { Invoices::Abacus::MembershipInvoiceGenerator.new(Person.with_membership_years.find(person.id), date, external_invoices(:external_invoice_draft), false, 0).generate_membership_invoice }

before do
SacMembershipConfig.update_all(valid_from: 2020)
Expand All @@ -25,7 +24,7 @@
end

context "mitglied" do
subject(:job) { CreateMembershipInvoiceJob.new(external_invoices(:external_invoice_draft), date, membership_invoice) }
subject(:job) { CreateMembershipInvoiceJob.new(external_invoices(:external_invoice_draft).id, date, 0, false) }

it "creates an invoice for membership" do
allow_any_instance_of(CreateMembershipInvoiceJob).to receive(:client).and_return(abacus_client)
Expand Down Expand Up @@ -157,7 +156,7 @@
context "for main family person" do
let(:person) { people(:familienmitglied) }

subject(:job) { CreateMembershipInvoiceJob.new(external_invoices(:external_invoice_draft), date, membership_invoice) }
subject(:job) { CreateMembershipInvoiceJob.new(external_invoices(:external_invoice_draft).id, date, 0, false) }

it "creates an invoice for family membership" do
external_invoices(:external_invoice_draft).update!(person: person)
Expand Down Expand Up @@ -252,7 +251,7 @@
context "for secondary family person" do
let(:person) { people(:familienmitglied_kind) }

subject(:job) { CreateMembershipInvoiceJob.new(external_invoices(:external_invoice_draft), date, membership_invoice) }
subject(:job) { CreateMembershipInvoiceJob.new(external_invoices(:external_invoice_draft).id, date, 0, false) }

it "creates no invoice for family membership" do
external_invoices(:external_invoice_draft).update!(person: person)
Expand Down
Loading

0 comments on commit fd540fb

Please sign in to comment.