From b2f3e4f1fdb6562609a579817d9ab4e55b38b97d Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 18 Jan 2025 21:17:04 +0100 Subject: [PATCH] Fix: Split subscriptions between members --- app/Invoice/Models/Invoice.php | 4 ++-- tests/Feature/Invoice/MassStoreActionTest.php | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/Invoice/Models/Invoice.php b/app/Invoice/Models/Invoice.php index fe0c0894..e409021b 100644 --- a/app/Invoice/Models/Invoice.php +++ b/app/Invoice/Models/Invoice.php @@ -49,7 +49,6 @@ class Invoice extends Model */ public static function createForMember(Member $member, Collection $members, int $year, Subscription $subscription = null): self { - $subscription = $subscription ?: $member->subscription; $invoice = new self([ 'to' => [ 'name' => 'Familie ' . $member->lastname, @@ -66,7 +65,8 @@ class Invoice extends Model $positions = collect([]); foreach ($members as $member) { - foreach ($subscription->children as $child) { + $memberSubscription = $subscription ?: $member->subscription; + foreach ($memberSubscription->children as $child) { $positions->push([ 'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $year), 'price' => $child->amount, diff --git a/tests/Feature/Invoice/MassStoreActionTest.php b/tests/Feature/Invoice/MassStoreActionTest.php index af31fc56..2f51f0b0 100644 --- a/tests/Feature/Invoice/MassStoreActionTest.php +++ b/tests/Feature/Invoice/MassStoreActionTest.php @@ -90,8 +90,8 @@ class MassStoreActionTest extends TestCase $this->assertDatabaseCount('invoices', 1); $this->assertDatabaseCount('invoice_positions', 2); - $this->assertDatabaseHas('invoice_positions', ['description' => 'beitrag Max Muster']); - $this->assertDatabaseHas('invoice_positions', ['description' => 'beitrag Jane Muster']); + $this->assertDatabaseHas('invoice_positions', ['description' => 'beitrag Max Muster', 'price' => 4466]); + $this->assertDatabaseHas('invoice_positions', ['description' => 'beitrag Jane Muster', 'price' => 4466]); } public function testItSeparatesBillKinds(): void @@ -105,4 +105,21 @@ class MassStoreActionTest extends TestCase $this->assertDatabaseCount('invoices', 2); $this->assertDatabaseCount('invoice_positions', 2); } + + public function testItSeparatesSubscriptions(): void + { + $member1 = Member::factory()->defaults()->emailBillKind() + ->for(Subscription::factory()->forFee()->children([new Child('beitrag1 {name}', 4466)])) + ->create(['firstname' => 'Member1', 'lastname' => 'ln']); + $member2 = Member::factory()->defaults()->sameFamilyAs($member1)->emailBillKind() + ->for(Subscription::factory()->forFee()->children([new Child('beitrag2 {name}', 4467)])) + ->create(['firstname' => 'Member2']); + + $this->postJson(route('invoice.mass-store'), ['year' => now()->addYear()->year])->assertOk(); + $invoice = Invoice::first(); + + $this->assertDatabaseCount('invoice_positions', 2); + $this->assertDatabaseHas('invoice_positions', ['invoice_id' => $invoice->id, 'member_id' => $member1->id, 'description' => 'beitrag1 Member1 ln', 'price' => 4466]); + $this->assertDatabaseHas('invoice_positions', ['invoice_id' => $invoice->id, 'member_id' => $member2->id, 'description' => 'beitrag2 Member2 ln', 'price' => 4467]); + } }