Skip to content

Commit

Permalink
Add Smartbill API exception handling and invoice functionality
Browse files Browse the repository at this point in the history
Expanded Smartbill API exception to include status code handling and introduced creation of new API responses for invoices. Changes were also made in Invoice, InvoiceEndpoint, ProformaEndpoint and SmartBillCloudRestClient classes to improve exception handling and data return types. A new file, ProformaInvoice, was also introduced. These enhancements improve code reliability and functionality.
  • Loading branch information
ag84ark committed Jan 27, 2024
1 parent 2e76c07 commit 413b7d1
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ coverage
.php_cs.cache
.phpunit.result.cache
.php-cs-fixer.cache

/.phpactor.json
65 changes: 65 additions & 0 deletions src/ApiResponse/CreateInvoiceResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Ag84ark\SmartBill\ApiResponse;

use Illuminate\Contracts\Support\Arrayable;

class CreateInvoiceResponse implements Arrayable
{
private string $errorText = '';
private string $message = '';

private string $number = '';

private string $series = '';

private string $url = '';

public static function fromArray(array $data): CreateInvoiceResponse
{
$invoiceResponse = new CreateInvoiceResponse();
$invoiceResponse->errorText = $data['errorText'];
$invoiceResponse->message = $data['message'];
$invoiceResponse->number = $data['number'];
$invoiceResponse->series = $data['series'];
$invoiceResponse->url = $data['url'];

return $invoiceResponse;
}

public function getErrorText(): string
{
return $this->errorText;
}

public function getMessage(): string
{
return $this->message;
}

public function getNumber(): string
{
return $this->number;
}

public function getSeries(): string
{
return $this->series;
}

public function getUrl(): string
{
return $this->url;
}

public function toArray(): array
{
return [
'errorText' => $this->errorText,
'message' => $this->message,
'number' => $this->number,
'series' => $this->series,
'url' => $this->url,
];
}
}
7 changes: 4 additions & 3 deletions src/Endpoints/InvoiceEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ag84ark\SmartBill\Endpoints;

use Ag84ark\SmartBill\ApiResponse\CreateInvoiceResponse;
use Ag84ark\SmartBill\Resources\Invoice;
use Ag84ark\SmartBill\Resources\ReverseInvoices;
use Ag84ark\SmartBill\SmartBillCloudRestClient;
Expand All @@ -26,7 +27,7 @@ public function getPDFInvoice($number)
return $this->rest_read($url, 'Accept: application/octet-stream');
}

public function createInvoice(Invoice $invoice)
public function createInvoice(Invoice $invoice): CreateInvoiceResponse
{
return $this->createInvoiceFromArray($invoice->toArray());
}
Expand Down Expand Up @@ -64,9 +65,9 @@ public function getStatusInvoicePayments($number)
return $this->rest_read($url);
}

public function createInvoiceFromArray(array $data)
public function createInvoiceFromArray(array $data): CreateInvoiceResponse
{
return $this->rest_create(self::INVOICE_URL, $data);
return CreateInvoiceResponse::fromArray($this->rest_create(self::INVOICE_URL, $data));
}

public function createReverseInvoiceFromArray(array $data)
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoints/ProformaEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Ag84ark\SmartBill\Endpoints;

use Ag84ark\SmartBill\Resources\Invoice;
use Ag84ark\SmartBill\Resources\ProformaInvoice;
use Ag84ark\SmartBill\SmartBillCloudRestClient;

class ProformaEndpoint extends BaseEndpoint
Expand All @@ -25,7 +25,7 @@ public function getPDFProforma($number)
return $this->rest_read($url, 'Accept: application/octet-stream');
}

public function createProforma(Invoice $invoice)
public function createProforma(ProformaInvoice $invoice)
{
return $this->createProformaFromArray($invoice->toArray());
}
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/SmartbillApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,17 @@

class SmartbillApiException extends \Exception
{
private int $statusCode;

public function __construct($message = "", $statusCode = 0, \Throwable $previous = null)
{
$message = $message ?: 'Smartbill API Exception';
$this->statusCode = $statusCode;
parent::__construct($message, $statusCode, $previous);
}

public function getStatusCode(): int
{
return $this->statusCode;
}
}
50 changes: 29 additions & 21 deletions src/Resources/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Invoice
*/
private bool $useEstimateDetails = false;

private ?array $estimate = null;

/**
* true - se foloseste tva la incasare;
* false - nu se foloseste tva la incasare
Expand All @@ -79,9 +81,6 @@ class Invoice
/** se completeaza doar pentru afisarea linkului de plata pe pdf */
private ?string $paymentUrl = null;

/** numarul proformei */
private ?string $number = null;

/** Trimiterea facturii clientului la emitere utilizand $email data de mai jos */
private bool $sendEmail = false;

Expand All @@ -108,11 +107,15 @@ public static function make(): self
return new self();
}

public static function makeProforma(string $number): self
public static function makeFromProforma(string $proformaSeries, string $number): self
{
$invoice = new self();
$invoice->seriesName = config('smartbill.proformaSeries');
$invoice->number = $number;
$invoice->useEstimateDetails = true;
$invoice->estimate = [
'number' => $number,
'seriesName' => $proformaSeries,
];

return $invoice;
}
Expand Down Expand Up @@ -161,12 +164,14 @@ public function toArray(): array
'colectedTax' => $this->colectedTax,
'paymentTotal' => $this->paymentTotal,
'paymentUrl' => $this->paymentUrl,
'number' => $this->number,
'sendEmail' => $this->sendEmail,
'email' => $this->email ? $this->email->toArray() : null,
'products' => $this->products->map(function (InvoiceProduct $product) {
return $product->toArray();
})->toArray(),
'estimate' => $this->estimate,
'products' => $this->products->count()
? $this->products->map(function (InvoiceProduct $product) {
return $product->toArray();
})->toArray()
: null,
])->filter(function ($value) {
return ! is_null($value);
})->toArray();
Expand Down Expand Up @@ -524,18 +529,6 @@ public function setPaymentUrl(?string $paymentUrl): Invoice
return $this;
}

public function getNumber(): ?string
{
return $this->number;
}

public function setNumber(?string $number): Invoice
{
$this->number = $number;

return $this;
}

public function isSendEmail(): bool
{
return $this->sendEmail;
Expand Down Expand Up @@ -571,4 +564,19 @@ public function setProducts(Collection $products): Invoice

return $this;
}

public function getEstimate(): ?array
{
return $this->estimate;
}

public function setEstimate(array $estimate): Invoice
{
$this->estimate = [
'number' => $estimate['number'],
'seriesName' => $estimate['seriesName'],
];

return $this;
}
}
Loading

0 comments on commit 413b7d1

Please sign in to comment.