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}");
|
->groupBy(fn($member) => "{$member->bill_kind->value}{$member->lastname}{$member->address}{$member->zip}{$member->location}");
|
||||||
|
|
||||||
foreach ($memberGroup as $members) {
|
foreach ($memberGroup as $members) {
|
||||||
$invoice = $members->first()->getInvoiceFactory()
|
$factory = $members->first()->getInvoiceFactory()
|
||||||
->withFamilyMembers($members)
|
->withFamilyMembers($members)
|
||||||
->year($year)
|
->year($year);
|
||||||
->getInvoice();
|
|
||||||
|
$invoice = Invoice::createFromFactory($factory);
|
||||||
$invoice->save();
|
$invoice->save();
|
||||||
$invoice->positions()->createMany($invoice->positions->toArray());
|
$invoice->positions()->createMany($invoice->positions->toArray());
|
||||||
$invoices->push($invoice->fresh('positions'));
|
$invoices->push($invoice->fresh('positions'));
|
||||||
|
|
|
||||||
|
|
@ -30,11 +30,12 @@ class MemberNewInvoiceAction
|
||||||
*/
|
*/
|
||||||
public function handle(Member $member, Subscription $subscription, int $year): array
|
public function handle(Member $member, Subscription $subscription, int $year): array
|
||||||
{
|
{
|
||||||
$invoice = $member->first()->getInvoiceFactory()
|
$factory = $member->first()->getInvoiceFactory()
|
||||||
->forSingleMember()
|
->forSingleMember()
|
||||||
->year($year)
|
->year($year)
|
||||||
->withSubscription($subscription)
|
->withSubscription($subscription);
|
||||||
->getInvoice();
|
|
||||||
|
$invoice = Invoice::createFromFactory($factory);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
...$invoice->getAttributes(),
|
...$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\InvoiceDocument;
|
||||||
use App\Invoice\InvoiceSettings;
|
use App\Invoice\InvoiceSettings;
|
||||||
use App\Invoice\RememberDocument;
|
use App\Invoice\RememberDocument;
|
||||||
|
use App\Member\Factories\MemberInvoiceFactory;
|
||||||
use Database\Factories\Invoice\Models\InvoiceFactory;
|
use Database\Factories\Invoice\Models\InvoiceFactory;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
@ -40,6 +41,23 @@ class Invoice extends Model
|
||||||
return $this->hasMany(InvoicePosition::class);
|
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
|
public static function booted(): void
|
||||||
{
|
{
|
||||||
static::deleting(function ($model) {
|
static::deleting(function ($model) {
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,9 @@
|
||||||
|
|
||||||
namespace App\Member\Factories;
|
namespace App\Member\Factories;
|
||||||
|
|
||||||
use App\Invoice\Enums\InvoiceStatus;
|
use App\Invoice\BillKind;
|
||||||
use App\Invoice\Models\Invoice;
|
use App\Invoice\Data\PositionData;
|
||||||
|
use App\Invoice\Data\ReceiverData;
|
||||||
use App\Member\Member;
|
use App\Member\Member;
|
||||||
use App\Payment\Subscription;
|
use App\Payment\Subscription;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
|
@ -51,36 +52,41 @@ class MemberInvoiceFactory
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInvoice(): Invoice
|
public function getReceiver(): ReceiverData {
|
||||||
{
|
return ReceiverData::from([
|
||||||
$invoice = new Invoice([
|
'name' => 'Familie ' . $this->member->lastname,
|
||||||
'to' => [
|
'address' => $this->member->address,
|
||||||
'name' => 'Familie ' . $this->member->lastname,
|
'zip' => $this->member->zip,
|
||||||
'address' => $this->member->address,
|
'location' => $this->member->location,
|
||||||
'zip' => $this->member->zip,
|
'greeting' => 'Liebe Familie ' . $this->member->lastname,
|
||||||
'location' => $this->member->location,
|
'email' => $this->member->email_parents ?: $this->member->email,
|
||||||
'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 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([]);
|
$positions = collect([]);
|
||||||
foreach ($this->positions as $member) {
|
foreach ($this->positions as $member) {
|
||||||
$memberSubscription = $this->subscription ?: $member->subscription;
|
$memberSubscription = $this->subscription ?: $member->subscription;
|
||||||
foreach ($memberSubscription->children as $child) {
|
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),
|
'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $this->year),
|
||||||
'price' => $child->amount,
|
'price' => $child->amount,
|
||||||
'member_id' => $member->id,
|
'member_id' => $member->id,
|
||||||
'id' => null,
|
'id' => null,
|
||||||
]);
|
]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$invoice->setRelation('positions', $positions);
|
|
||||||
|
|
||||||
return $invoice;
|
return $positions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue