Add factory for creating invoices
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
2640622685
commit
78629989e4
|
|
@ -37,10 +37,11 @@ class MassStoreAction
|
|||
->groupBy(fn($member) => "{$member->bill_kind->value}{$member->lastname}{$member->address}{$member->zip}{$member->location}");
|
||||
|
||||
foreach ($memberGroup as $members) {
|
||||
$invoice = $members->first()->getInvoiceFactory()
|
||||
$factory = $members->first()->getInvoiceFactory()
|
||||
->withFamilyMembers($members)
|
||||
->year($year)
|
||||
->getInvoice();
|
||||
->year($year);
|
||||
|
||||
$invoice = Invoice::createFromFactory($factory);
|
||||
$invoice->save();
|
||||
$invoice->positions()->createMany($invoice->positions->toArray());
|
||||
$invoices->push($invoice->fresh('positions'));
|
||||
|
|
|
|||
|
|
@ -30,11 +30,12 @@ class MemberNewInvoiceAction
|
|||
*/
|
||||
public function handle(Member $member, Subscription $subscription, int $year): array
|
||||
{
|
||||
$invoice = $member->first()->getInvoiceFactory()
|
||||
$factory = $member->first()->getInvoiceFactory()
|
||||
->forSingleMember()
|
||||
->year($year)
|
||||
->withSubscription($subscription)
|
||||
->getInvoice();
|
||||
->withSubscription($subscription);
|
||||
|
||||
$invoice = Invoice::createFromFactory($factory);
|
||||
|
||||
return [
|
||||
...$invoice->getAttributes(),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App\Invoice\Data;
|
||||
|
||||
use Spatie\LaravelData\Data;
|
||||
|
||||
class PositionData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public string $description,
|
||||
public string $price,
|
||||
public int $memberId,
|
||||
) {}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ use App\Invoice\Enums\InvoiceStatus;
|
|||
use App\Invoice\InvoiceDocument;
|
||||
use App\Invoice\InvoiceSettings;
|
||||
use App\Invoice\RememberDocument;
|
||||
use App\Member\Factories\MemberInvoiceFactory;
|
||||
use Database\Factories\Invoice\Models\InvoiceFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
|
@ -40,6 +41,23 @@ class Invoice extends Model
|
|||
return $this->hasMany(InvoicePosition::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Member> $members
|
||||
*/
|
||||
public static function createFromFactory(MemberInvoiceFactory $factory): self
|
||||
{
|
||||
$invoice = new self([
|
||||
'to' => $factory->getReceiver(),
|
||||
'status' => InvoiceStatus::NEW,
|
||||
'via' => $factory->getVia(),
|
||||
'usage' => $factory->getUsage(),
|
||||
]);
|
||||
|
||||
$invoice->setRelation('positions', $factory->getPositions()->toArray());
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
public static function booted(): void
|
||||
{
|
||||
static::deleting(function ($model) {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
namespace App\Member\Factories;
|
||||
|
||||
use App\Invoice\Enums\InvoiceStatus;
|
||||
use App\Invoice\Models\Invoice;
|
||||
use App\Invoice\BillKind;
|
||||
use App\Invoice\Data\PositionData;
|
||||
use App\Invoice\Data\ReceiverData;
|
||||
use App\Member\Member;
|
||||
use App\Payment\Subscription;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -51,36 +52,41 @@ class MemberInvoiceFactory
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function getInvoice(): Invoice
|
||||
{
|
||||
$invoice = new Invoice([
|
||||
'to' => [
|
||||
'name' => 'Familie ' . $this->member->lastname,
|
||||
'address' => $this->member->address,
|
||||
'zip' => $this->member->zip,
|
||||
'location' => $this->member->location,
|
||||
'greeting' => 'Liebe Familie ' . $this->member->lastname,
|
||||
'email' => $this->member->email_parents ?: $this->member->email,
|
||||
],
|
||||
'status' => InvoiceStatus::NEW,
|
||||
'via' => $this->member->bill_kind,
|
||||
'usage' => 'Mitgliedsbeitrag für ' . $this->member->lastname,
|
||||
public function getReceiver(): ReceiverData {
|
||||
return ReceiverData::from([
|
||||
'name' => 'Familie ' . $this->member->lastname,
|
||||
'address' => $this->member->address,
|
||||
'zip' => $this->member->zip,
|
||||
'location' => $this->member->location,
|
||||
'greeting' => 'Liebe Familie ' . $this->member->lastname,
|
||||
'email' => $this->member->email_parents ?: $this->member->email,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getVia(): BillKind {
|
||||
return $this->member->bill_kind;
|
||||
}
|
||||
|
||||
public function getUsage(): string {
|
||||
return 'Mitgliedsbeitrag für ' . $this->member->lastname;
|
||||
}
|
||||
|
||||
/** @return Collection<int, PositionData> */
|
||||
public function getPositions() {
|
||||
/** @var Collection<int, PositionData> */
|
||||
$positions = collect([]);
|
||||
foreach ($this->positions as $member) {
|
||||
$memberSubscription = $this->subscription ?: $member->subscription;
|
||||
foreach ($memberSubscription->children as $child) {
|
||||
$positions->push([
|
||||
$positions->push(PositionData::from([
|
||||
'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $this->year),
|
||||
'price' => $child->amount,
|
||||
'member_id' => $member->id,
|
||||
'id' => null,
|
||||
]);
|
||||
]));
|
||||
}
|
||||
}
|
||||
$invoice->setRelation('positions', $positions);
|
||||
|
||||
return $invoice;
|
||||
return $positions;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue