diff --git a/app/models/memberships/join_zusatzsektion.rb b/app/models/memberships/join_zusatzsektion.rb index 7eb163c72..e40fe3910 100644 --- a/app/models/memberships/join_zusatzsektion.rb +++ b/app/models/memberships/join_zusatzsektion.rb @@ -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, diff --git a/spec/models/memberships/join_zusatzsektion_spec.rb b/spec/models/memberships/join_zusatzsektion_spec.rb index 8dce4c82f..d9885085c 100644 --- a/spec/models/memberships/join_zusatzsektion_spec.rb +++ b/spec/models/memberships/join_zusatzsektion_spec.rb @@ -154,15 +154,15 @@ 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 @@ -170,8 +170,8 @@ def create_role(key, role, owner: person, **attrs) 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 @@ -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 @@ -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