Skip to content

Commit

Permalink
Implement generate_invoice method for JoinZusatzsektion
Browse files Browse the repository at this point in the history
  • Loading branch information
njaeggi committed Nov 21, 2024
1 parent fd363e8 commit 5dd77c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
22 changes: 22 additions & 0 deletions app/models/memberships/join_zusatzsektion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@ def affected_people
sac_family_membership? ? super : [person]
end

def save!
generate_invoice(roles.first) unless confirmation_needed?

super
end

private

def confirmation_needed?
group_for_neuanmeldung.is_a?(Group::SektionsNeuanmeldungenSektion)
end

def generate_invoice(role)
invoice = ExternalInvoice::SacMembership.create!(
person: role.person,
state: :draft,
year: Date.current.year,
issued_at: Date.current,
sent_at: Date.current,
link: role.layer_group
)
Invoices::Abacus::CreateMembershipInvoiceJob.new(invoice, Date.current, new_entry: false).enqueue!
end

def prepare_roles(person)
group_for_neuanmeldung.roles.build(
person: person,
Expand Down
24 changes: 12 additions & 12 deletions spec/models/memberships/join_zusatzsektion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,24 @@ def create_role(key, role, owner: person, **attrs)
ActiveRecord::Base.transaction do
expect(join_sektion).to be_valid
expect { join_sektion.save! }.to change { person.reload.roles.count }.by(1)
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
end
end

it "prefers to create role in NeuanmeldungenSektion group" do
expect { join_sektion.save! }.to change { person.reload.roles.count }.by(1)
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
expect(role.group).to eq groups(:matterhorn_neuanmeldungen_sektion)
expect(role.type).to eq "Group::SektionsNeuanmeldungenSektion::NeuanmeldungZusatzsektion"
end

it "falls back to create role in NeuanmeldungenSektionNv group" do
groups(:matterhorn_neuanmeldungen_sektion).destroy
expect { join_sektion.save! }.to change { person.reload.roles.count }.by(1)
.and change { ExternalInvoice::SacMembership.count }.by(1)
.and change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }.by(1)
.and change { ExternalInvoice::SacMembership.count }.by(1)
.and change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }.by(1)
expect(role.group).to eq groups(:matterhorn_neuanmeldungen_nv)
expect(role.type).to eq "Group::SektionsNeuanmeldungenNv::NeuanmeldungZusatzsektion"
end
Expand All @@ -185,16 +185,16 @@ def create_role(key, role, owner: person, **attrs)
neuanmeldungen = Fabricate(Group::SektionsNeuanmeldungenSektion.sti_name,
parent: sektion)
expect { join_sektion.save! }.to change { person.reload.roles.count }.by(1)
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
expect(role.group).to eq neuanmeldungen
expect(role.type).to eq "Group::SektionsNeuanmeldungenSektion::NeuanmeldungZusatzsektion"
end

it "falls back to create role in NeuanmeldungenSektionNv group" do
expect { join_sektion.save! }.to change { person.reload.roles.count }.by(1)
.and change { ExternalInvoice::SacMembership.count }.by(1)
.and change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }.by(1)
.and change { ExternalInvoice::SacMembership.count }.by(1)
.and change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }.by(1)
expect(role.group).to eq groups(:bluemlisalp_ortsgruppe_ausserberg_neuanmeldungen_nv)
expect(role.type).to eq "Group::SektionsNeuanmeldungenNv::NeuanmeldungZusatzsektion"
end
Expand Down Expand Up @@ -241,8 +241,8 @@ def create_sac_family(person, *others)
expect do
expect(join_sektion.save!).to eq true
end.to change { Role.count }.by(2)
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
.and not_change { ExternalInvoice::SacMembership.count }
.and not_change { Delayed::Job.where("handler like '%CreateMembershipInvoiceJob%'").count }
expect(role.beitragskategorie).to eq "family"
expect(role(other).beitragskategorie).to eq "family"
end
Expand Down

0 comments on commit 5dd77c3

Please sign in to comment.