Skip to content

Commit

Permalink
Update Invoice structure
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonJnsson committed Dec 13, 2023
1 parent 64ba523 commit 1ba3be9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 47 deletions.
4 changes: 3 additions & 1 deletion src/Resources/Invoice/BookedInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
35 changes: 1 addition & 34 deletions src/Resources/Invoice/DraftInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
36 changes: 24 additions & 12 deletions src/Resources/Invoice/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,55 @@

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;
use MorningTrain\Economic\Resources\PaymentTerm;
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;
}
}
23 changes: 23 additions & 0 deletions tests/Unit/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

0 comments on commit 1ba3be9

Please sign in to comment.