diff --git a/src/Resources/Invoice/BookedInvoice.php b/src/Resources/Invoice/BookedInvoice.php index 2d8e56b..95f6387 100644 --- a/src/Resources/Invoice/BookedInvoice.php +++ b/src/Resources/Invoice/BookedInvoice.php @@ -9,8 +9,10 @@ #[GetCollection('invoices/booked')] #[GetSingle('invoices/booked/:bookedInvoiceNumber', ':bookedInvoiceNumber')] #[Create('invoices/booked')] -class BookedInvoice extends DraftInvoice +class BookedInvoice extends Invoice { + public ?int $bookedInvoiceNumber = null; + public static function createFromDraft(int|DraftInvoice $draft) { return static::createRequest([ diff --git a/src/Resources/Invoice/DraftInvoice.php b/src/Resources/Invoice/DraftInvoice.php index 0d0d43d..ab067bb 100644 --- a/src/Resources/Invoice/DraftInvoice.php +++ b/src/Resources/Invoice/DraftInvoice.php @@ -20,43 +20,10 @@ #[GetCollection('invoices/drafts')] #[GetSingle('invoices/drafts/:draftInvoiceNumber', ':draftInvoiceNumber')] #[Create('invoices/drafts')] -class DraftInvoice extends Resource +class DraftInvoice extends Invoice { - use Creatable, GetCollectionable, GetSingleable; - use HasLines; - - public Customer $customer; - - public Layout $layout; - - public Currency $currency; - - public PaymentTerm $paymentTerms; - - public DateTime $date; - - public Recipient $recipient; - public ?int $draftInvoiceNumber = null; - public static function new( - Customer|int $customer, - Layout|int $layout, - Currency|string $currency, - PaymentTerm|int $paymentTerms, - DateTime $date, - Recipient $recipient, - ): static { - return new static([ - 'customer' => $customer, - 'layout' => $layout, - 'currency' => $currency, - 'paymentTerms' => $paymentTerms, - 'date' => $date, - 'recipient' => $recipient, - ]); - } - public function create() { return static::createRequest([ diff --git a/src/Resources/Invoice/Invoice.php b/src/Resources/Invoice/Invoice.php index c981cf6..6de5196 100644 --- a/src/Resources/Invoice/Invoice.php +++ b/src/Resources/Invoice/Invoice.php @@ -2,10 +2,12 @@ namespace MorningTrain\Economic\Resources\Invoice; +use DateTime; use MorningTrain\Economic\Abstracts\Resource; use MorningTrain\Economic\Attributes\Resources\Create; use MorningTrain\Economic\Attributes\Resources\GetCollection; use MorningTrain\Economic\Attributes\Resources\GetSingle; +use MorningTrain\Economic\DTOs\Recipient; use MorningTrain\Economic\Resources\Currency; use MorningTrain\Economic\Resources\Customer; use MorningTrain\Economic\Resources\Layout; @@ -13,32 +15,42 @@ use MorningTrain\Economic\Traits\Resources\Creatable; use MorningTrain\Economic\Traits\Resources\GetCollectionable; use MorningTrain\Economic\Traits\Resources\GetSingleable; +use MorningTrain\Economic\Traits\Resources\HasLines; -#[GetCollection('products')] -#[GetSingle('invoices/:product', ':product')] -#[Create('invoices/drafts')] class Invoice extends Resource { - use Creatable, GetCollectionable, GetSingleable; + use Creatable; + use GetCollectionable; + use GetSingleable; + use HasLines; + + public Customer $customer; + + public Layout $layout; + + public Currency $currency; + + public PaymentTerm $paymentTerms; + + public DateTime $date; + + public Recipient $recipient; public static function new( Customer|int $customer, Layout|int $layout, Currency|string $currency, PaymentTerm|int $paymentTerms, - ) { + DateTime $date, + Recipient $recipient, + ): static { return new static([ 'customer' => $customer, 'layout' => $layout, 'currency' => $currency, 'paymentTerms' => $paymentTerms, + 'date' => $date, + 'recipient' => $recipient, ]); } - - public function addLine(ProductLine $line): static - { - $this->lines[] = $line; - - return $this; - } } diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 561de4b..4cead0d 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -3,6 +3,7 @@ use MorningTrain\Economic\Classes\EconomicResponse; use MorningTrain\Economic\DTOs\Recipient; use MorningTrain\Economic\Enums\PaymentTermsType; +use MorningTrain\Economic\Resources\Invoice\BookedInvoice; use MorningTrain\Economic\Resources\Invoice\DraftInvoice; use MorningTrain\Economic\Resources\Invoice\ProductLine; use MorningTrain\Economic\Resources\PaymentTerm; @@ -163,3 +164,25 @@ ->product->toBeInstanceOf(Product::class) ->quantity->toBe(5.0); }); + +it('returns expected booked invoice data', function () { + $this->driver->expects()->post() + ->withArgs(function (string $url, array $body) { + return $url === 'https://restapi.e-conomic.com/invoices/booked' + && $body === [ + 'draftInvoice' => [ + 'draftInvoiceNumber' => 1, + ], + ]; + }) + ->once() + ->andReturn(new EconomicResponse(201, [ + 'bookedInvoiceNumber' => 1, + ])); + + $bookedInvoice = BookedInvoice::createFromDraft(1); + + expect($bookedInvoice) + ->bookedInvoiceNumber->toBe(1); +}); +