Fix: Delete invoice positions when deleting member
This commit is contained in:
parent
451680bd70
commit
a7b25e9b5f
|
@ -14,10 +14,27 @@ class InvoicePosition extends Model
|
|||
public $guarded = [];
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Member>
|
||||
* @return BelongsTo<Member, self>
|
||||
*/
|
||||
public function member(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Member::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Invoice, self>
|
||||
*/
|
||||
public function invoice(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Invoice::class);
|
||||
}
|
||||
|
||||
public static function booted(): void
|
||||
{
|
||||
static::deleted(function ($model) {
|
||||
if ($model->invoice->positions()->get()->count() === 0) {
|
||||
$model->invoice->delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ use App\Course\Models\CourseMember;
|
|||
use App\Gender;
|
||||
use App\Group;
|
||||
use App\Invoice\BillKind;
|
||||
use App\Invoice\Models\InvoicePosition;
|
||||
use App\Nami\HasNamiField;
|
||||
use App\Nationality;
|
||||
use App\Payment\Payment;
|
||||
|
@ -213,6 +214,14 @@ class Member extends Model implements Geolocatable
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<InvoicePosition>
|
||||
*/
|
||||
public function invoicePositions(): HasMany
|
||||
{
|
||||
return $this->hasMany(InvoicePosition::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Confession, self>
|
||||
*/
|
||||
|
@ -291,6 +300,9 @@ class Member extends Model implements Geolocatable
|
|||
$model->payments->each->delete();
|
||||
$model->memberships->each->delete();
|
||||
$model->courses->each->delete();
|
||||
$model->invoicePositions->each(function ($position) {
|
||||
$position->delete();
|
||||
});
|
||||
});
|
||||
|
||||
static::saving(fn ($model) => $model->updateSearch());
|
||||
|
|
|
@ -5,6 +5,8 @@ namespace Tests\Feature\Member;
|
|||
use Illuminate\Support\Str;
|
||||
use App\Course\Models\Course;
|
||||
use App\Course\Models\CourseMember;
|
||||
use App\Invoice\Models\Invoice;
|
||||
use App\Invoice\Models\InvoicePosition;
|
||||
use App\Lib\Events\JobFailed;
|
||||
use App\Lib\Events\JobFinished;
|
||||
use App\Lib\Events\JobStarted;
|
||||
|
@ -75,6 +77,37 @@ class DeleteTest extends TestCase
|
|||
$this->assertDatabaseMissing('members', ['id' => $member->id]);
|
||||
}
|
||||
|
||||
public function testItDeletesInvoicePositions(): void
|
||||
{
|
||||
$this->withoutExceptionHandling()->login()->loginNami();
|
||||
$member = Member::factory()->defaults()->create();
|
||||
$member2 = Member::factory()->defaults()->create();
|
||||
Invoice::factory()
|
||||
->has(InvoicePosition::factory()->for($member), 'positions')
|
||||
->has(InvoicePosition::factory()->for($member2), 'positions')
|
||||
->create();
|
||||
|
||||
MemberDeleteAction::run($member->id);
|
||||
|
||||
$this->assertDatabaseCount('invoices', 1);
|
||||
$this->assertDatabaseCount('invoice_positions', 1);
|
||||
}
|
||||
|
||||
public function testItDeletesInvoicePositionsAndInvoices(): void
|
||||
{
|
||||
$this->withoutExceptionHandling()->login()->loginNami();
|
||||
$member = Member::factory()->defaults()->create();
|
||||
Invoice::factory()
|
||||
->has(InvoicePosition::factory()->for($member), 'positions')
|
||||
->has(InvoicePosition::factory()->for($member), 'positions')
|
||||
->create();
|
||||
|
||||
MemberDeleteAction::run($member->id);
|
||||
|
||||
$this->assertDatabaseCount('invoices', 0);
|
||||
$this->assertDatabaseCount('invoice_positions', 0);
|
||||
}
|
||||
|
||||
public function testItFiresEventWhenFinished(): void
|
||||
{
|
||||
Event::fake([JobStarted::class, JobFinished::class]);
|
||||
|
|
Loading…
Reference in New Issue