Skip to content

Commit

Permalink
✨ Add create an save method to draft invoice (removed non static create)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschadegg committed Feb 20, 2024
1 parent 75c52e4 commit a95dbab
Showing 1 changed file with 136 additions and 11 deletions.
147 changes: 136 additions & 11 deletions src/Resources/Invoice/DraftInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,154 @@

namespace Morningtrain\Economic\Resources\Invoice;

use DateTime;
use Illuminate\Support\Collection;
use Morningtrain\Economic\Attributes\Resources\Create;
use Morningtrain\Economic\Attributes\Resources\GetCollection;
use Morningtrain\Economic\Attributes\Resources\GetSingle;
use Morningtrain\Economic\Attributes\Resources\Properties\PrimaryKey;
use Morningtrain\Economic\Attributes\Resources\Update;
use Morningtrain\Economic\DTOs\Invoice\Note;
use Morningtrain\Economic\DTOs\Invoice\ProductLine;
use Morningtrain\Economic\DTOs\Invoice\Recipient;
use Morningtrain\Economic\DTOs\Invoice\Reference;
use Morningtrain\Economic\Resources\Currency;
use Morningtrain\Economic\Resources\Customer;
use Morningtrain\Economic\Resources\Layout;
use Morningtrain\Economic\Resources\PaymentTerm;
use Morningtrain\Economic\Traits\Resources\Updatable;

#[GetCollection('invoices/drafts')]
#[GetSingle('invoices/drafts/:draftInvoiceNumber', ':draftInvoiceNumber')]
#[Create('invoices/drafts')]
#[Update('invoices/drafts/:draftInvoiceNumber', ':draftInvoiceNumber')]
class DraftInvoice extends Invoice
{
use Updatable {
Updatable::save as protected saveRequest;
}

public ?float $costPriceInBaseCurrency;

#[PrimaryKey]
public ?int $draftInvoiceNumber = null;

public function create()
public static function create(
Currency|string $currency,
Customer|int $customer,
DateTime $date,
Layout|int $layout,
PaymentTerm|int $paymentTerms,
Recipient $recipient,
array|Collection $lines = [],
// ?Delivery $delivery = null, // TODO: Implement
?DateTime $dueDate = null,
?float $exchangeRate = null,
?Note $notes = null,
// ?Project|int $project = null, // TODO: Implement
?Reference $references = null
): ?static {
if(is_a($currency, Currency::class)) {
$currency = $currency->isoNumber;
}

if(is_int($customer)) {
$customer = new Customer(['customerNumber' => $customer]);
}

if(is_int($layout)) {
$layout = new Layout(['layoutNumber' => $layout]);
}

if(is_int($paymentTerms)) {
$paymentTerms = new PaymentTerm(['paymentTermsNumber' => $paymentTerms]);
}

return static::createRequest(compact(
'currency',
'customer',
'date',
'layout',
'paymentTerms',
'recipient',
'lines',
'dueDate',
'exchangeRate',
'notes',
'references',
));
}

public static function new(
Currency|string $currency,
Customer|int $customer,
DateTime $date,
Layout|int $layout,
PaymentTerm|int $paymentTerms,
Recipient $recipient,
array|Collection $lines = [],
// ?Delivery $delivery = null, // TODO: Implement
?DateTime $dueDate = null,
?float $exchangeRate = null,
?Note $notes = null,
// ?Project|int $project = null, // TODO: Implement
?Reference $references = null
): ?static
{
return static::createRequest([
'customer' => $this->customer,
'layout' => $this->layout,
'currency' => $this->currency->isoNumber,
'paymentTerms' => $this->paymentTerms,
'date' => $this->date->format('Y-m-d'),
'recipient' => $this->recipient,
'lines' => $this->lines ?? null,
'notes' => $this->notes ?? null,
]);
if(is_a($currency, Currency::class)) {
$currency = $currency->isoNumber;
}

if(is_int($customer)) {
$customer = new Customer(['customerNumber' => $customer]);
}

if(is_int($layout)) {
$layout = new Layout(['layoutNumber' => $layout]);
}

if(is_int($paymentTerms)) {
$paymentTerms = new PaymentTerm(['paymentTermsNumber' => $paymentTerms]);
}

return new static(array_filter(compact(
'currency',
'customer',
'date',
'layout',
'paymentTerms',
'recipient',
'lines',
'dueDate',
'exchangeRate',
'notes',
'references',
)));
}

public function save(): ?static
{
if(empty($this->draftInvoiceNumber)) {
$new = static::create(
currency: $this->currency,
customer: $this->customer,
date: $this->date,
layout: $this->layout,
paymentTerms: $this->paymentTerms,
recipient: $this->recipient,
lines: $this->lines,
dueDate: $this->dueDate,
exchangeRate: $this->exchangeRate,
notes: $this->notes,
references: $this->references,
);

$this->populate($new->toArray());

return $this;
}

return $this->saveRequest();
}

public function book(): ?BookedInvoice
Expand Down

0 comments on commit a95dbab

Please sign in to comment.