Skip to content

Commit

Permalink
Merge branch 'feature/invoice' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonJnsson committed Dec 7, 2023
2 parents fca1f80 + 87bb9c6 commit 97f16a7
Show file tree
Hide file tree
Showing 20 changed files with 457 additions and 68 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage",
"test-coverage": "vendor/bin/pest --coverage",
"format": "vendor/bin/pint"
},
"config": {
Expand Down
28 changes: 25 additions & 3 deletions src/Abstracts/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Resource
{
public string $self;

public function __construct(array|string|int|float $properties = null)
public function __construct(array|string|int|float|null $properties = null)
{
if (is_array($properties)) {
$this->populate($properties);
Expand Down Expand Up @@ -43,8 +43,15 @@ protected function resolvePropertyValue(string $property, mixed $value): mixed

$propertyReflection = $reflection->getProperty($property);

$reflectionTypeName = $propertyReflection->getType()->getName();

// If is already the expected type
if ($value instanceof $reflectionTypeName) {
return $value;
}

// If is EconomicCollection
if (is_a($propertyReflection->getType()->getName(), EconomicCollection::class, true)) {
if (is_a($reflectionTypeName, EconomicCollection::class, true)) {
$attribute = $propertyReflection->getAttributes(ResourceType::class);

if (! empty($attribute[0])) {
Expand All @@ -55,7 +62,7 @@ protected function resolvePropertyValue(string $property, mixed $value): mixed
}

// If is a class
if (class_exists($propertyReflection->getType()->getName())) {
if (class_exists($reflectionTypeName)) {
return new ($propertyReflection->getType()->getName())($value);
}

Expand Down Expand Up @@ -151,4 +158,19 @@ public function __toString(): string
{
return json_encode($this->toArray());
}

protected static function filterEmpty(array $values): array
{
foreach ($values as $key => $value) {
if (is_array($value)) {
$values[$key] = static::filterEmpty($value);
}

if (empty($values[$key])) {
unset($values[$key]);
}
}

return $values;
}
}
4 changes: 2 additions & 2 deletions src/Classes/EconomicQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function getEndpoint(): string
return $this->endpoint;
}

public function where(int|string|Closure $propertyName, string $operatorOrValue = null, mixed $value = null): static
public function where(int|string|Closure $propertyName, ?string $operatorOrValue = null, mixed $value = null): static
{
if (! isset($this->filter)) {
$this->filter = new EconomicQueryFilterBuilder(EconomicQueryFilterBuilder::FILTER_RELATION_AND);
Expand All @@ -53,7 +53,7 @@ public function where(int|string|Closure $propertyName, string $operatorOrValue
return $this;
}

public function orWhere(int|string|Closure $propertyName, string $operatorOrValue = null, mixed $value = null): static
public function orWhere(int|string|Closure $propertyName, ?string $operatorOrValue = null, mixed $value = null): static
{
if (! isset($this->filter)) {
$this->filter = new EconomicQueryFilterBuilder(EconomicQueryFilterBuilder::FILTER_RELATION_OR);
Expand Down
4 changes: 2 additions & 2 deletions src/Classes/EconomicQueryFilterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function whereNested(Closure $closure)
return $this;
}

public function where(int|string|Closure $propertyName, string $operatorOrValue = null, mixed $value = null): static
public function where(int|string|Closure $propertyName, ?string $operatorOrValue = null, mixed $value = null): static
{
if ($propertyName instanceof Closure) {
return $this->whereNested($propertyName);
Expand All @@ -91,7 +91,7 @@ public function where(int|string|Closure $propertyName, string $operatorOrValue
return $this;
}

public function orWhere(int|string|Closure $propertyName, string $operatorOrValue = null, mixed $value = null): static
public function orWhere(int|string|Closure $propertyName, ?string $operatorOrValue = null, mixed $value = null): static
{
$instance = new static(static::FILTER_RELATION_OR);

Expand Down
11 changes: 11 additions & 0 deletions src/DTOs/Attention.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace MorningTrain\Economic\DTOs;

class Attention
{
public function __construct(
public ?int $customerContactNumber = null,
) {
}
}
21 changes: 21 additions & 0 deletions src/DTOs/Recipient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MorningTrain\Economic\DTOs;

use MorningTrain\Economic\Resources\VatZone;

class Recipient
{
public function __construct(
public readonly string $name,
public readonly VatZone $vatZone,
public ?string $address = null,
public ?string $zip = null,
public ?string $city = null,
public ?string $country = null,
public ?string $ean = null,
public ?string $publicEntryNumber = null,
//public ?Attention $attention = null,
) {
}
}
38 changes: 19 additions & 19 deletions src/Resources/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,25 @@ public static function create(
Currency|string $currency,
VatZone|int $vatZone,
PaymentTerm|int $paymentTerms,
string $email = null,
string $address = null,
string $zip = null,
string $city = null,
string $country = null,
string $corporateIdentificationNumber = null,
string $pNumber = null,
string $vatNumber = null,
string $ean = null,
string $publicEntryNumber = null,
string $website = null,
string $mobilePhone = null,
string $telephoneAndFaxNumber = null,
bool $barred = null,
bool $eInvoicingDisabledByDefault = null,
float $creditLimit = null,
int $customerNumber = null,
Layout|int $layout = null,
Employee|int $salesPerson = null,
?string $email = null,
?string $address = null,
?string $zip = null,
?string $city = null,
?string $country = null,
?string $corporateIdentificationNumber = null,
?string $pNumber = null,
?string $vatNumber = null,
?string $ean = null,
?string $publicEntryNumber = null,
?string $website = null,
?string $mobilePhone = null,
?string $telephoneAndFaxNumber = null,
?bool $barred = null,
?bool $eInvoicingDisabledByDefault = null,
?float $creditLimit = null,
?int $customerNumber = null,
Layout|int|null $layout = null,
Employee|int|null $salesPerson = null,
): static {
return static::createRequest(array_filter(compact(
'name',
Expand Down
10 changes: 5 additions & 5 deletions src/Resources/Customer/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public static function fromCustomer(Customer|int $customer)
public static function create(
Customer $customer,
string $name,
string $email = null,
string $phone = null,
string $notes = null,
string $eInvoiceId = null,
array $emailNotifications = null
?string $email = null,
?string $phone = null,
?string $notes = null,
?string $eInvoiceId = null,
?array $emailNotifications = null
) {
return static::createRequest(array_filter(compact(
'customer',
Expand Down
27 changes: 0 additions & 27 deletions src/Resources/Invoice.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/Resources/Invoice/BookedInvoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace MorningTrain\Economic\Resources\Invoice;

use MorningTrain\Economic\Attributes\Resources\Create;
use MorningTrain\Economic\Attributes\Resources\GetCollection;
use MorningTrain\Economic\Attributes\Resources\GetSingle;

#[GetCollection('invoices/booked')]
#[GetSingle('invoices/booked/:bookedInvoiceNumber', ':bookedInvoiceNumber')]
#[Create('invoices/booked')]
class BookedInvoice extends DraftInvoice
{
public static function createFromDraft(int|DraftInvoice $draft)
{
return static::createRequest([
'draftInvoice' => [
'draftInvoiceNumber' => is_int($draft) ? $draft : $draft->draftInvoiceNumber,
],
]);
}
}
77 changes: 77 additions & 0 deletions src/Resources/Invoice/DraftInvoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

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('invoices/drafts')]
#[GetSingle('invoices/drafts/:draftInvoiceNumber', ':draftInvoiceNumber')]
#[Create('invoices/drafts')]
class DraftInvoice extends Resource
{
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([
'customer' => $this->customer,
'layout' => $this->layout,
'currency' => $this->currency,
'paymentTerms' => $this->paymentTerms,
'date' => $this->date->format('Y-m-d'),
'recipient' => $this->recipient,
'lines' => $this->lines ?? null,
]);
}

public function book(): ?BookedInvoice
{
return BookedInvoice::createFromDraft($this);
}
}
44 changes: 44 additions & 0 deletions src/Resources/Invoice/Invoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace MorningTrain\Economic\Resources\Invoice;

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\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;

#[GetCollection('products')]
#[GetSingle('invoices/:product', ':product')]
#[Create('invoices/drafts')]
class Invoice extends Resource
{
use Creatable, GetCollectionable, GetSingleable;

public static function new(
Customer|int $customer,
Layout|int $layout,
Currency|string $currency,
PaymentTerm|int $paymentTerms,
) {
return new static([
'customer' => $customer,
'layout' => $layout,
'currency' => $currency,
'paymentTerms' => $paymentTerms,
]);
}

public function addLine(ProductLine $line): static
{
$this->lines[] = $line;

return $this;
}
}
Loading

0 comments on commit 97f16a7

Please sign in to comment.