Skip to content

Commit

Permalink
better readme
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Mar 14, 2024
1 parent ccaae37 commit 5d84d1d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 4 deletions.
113 changes: 109 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ return [
'auto_generate' => true,

/**
* Define the serial number format used for each invoice type
*
* P: Prefix
* S: Serie
* M: Month
Expand All @@ -70,6 +72,9 @@ return [
InvoiceType::Proforma->value => 'PPYYCCCC',
],

/**
* Define the default prefix used for each invoice type
*/
'prefix' => [
InvoiceType::Invoice->value => 'IN',
InvoiceType::Quote->value => 'QO',
Expand Down Expand Up @@ -116,7 +121,6 @@ return [
'orientation' => 'portrait',
],
];
;
```

## Usage
Expand All @@ -127,12 +131,42 @@ An invoice is just a model with InvoiceItem relationships, so you can create an

```php
use Finller\Invoice\Invoice;
$invoice = new Invoice();
use Finller\Invoice\InvoiceState;
use Finller\Invoice\InvoiceType;

// Define general invoice data
$invoice = new Invoice([
'type' => InvoiceType::Invoice,
'state' => InvoiceState::Draft,
'description' => 'A description for my invoice',
'seller_information' => config('invoices.default_seller'),
'buyer_information' => [
'name' => 'Client name',
'address' => [],
'tax_number' => "XYZ",
],
// ...
]);

// Set custom serial number values if you want
$invoice->setSerialNumberPrefix('CLI');
// For exemple, you could define a different serie for each of your customer
$invoice->setSerialNumberSerie($customer->id);

$invoice->buyer()->associate($customer);
$invoice->invoiceable()->associate($order); // optionnally associate the invoice to a model

$invoice->save();

$invoice->items()->saveMany([
new InvoiceItem(),
new InvoiceItem(),
new InvoiceItem([
'unit_price' => Money::of(100, 'USD'),
'unit_tax' => Money::of(20, 'USD'),
'currency' => 'USD',
'quantity' => 1,
'label' => 'A label for my item',
'description' => 'A description for my item',
]),
]);
```

Expand Down Expand Up @@ -165,6 +199,77 @@ $invoice->setSerialNumberSerie($buyer_id);
$invoice->save();
```

## Display your invoice as a PDF

The Invoice model has a `toPdfInvoice()` that return a `PdfInvoice` class.

### As a response in a controller

You can stream the `pdfInvoice` instance as a response, download it:

```php
namespace App\Http\Controllers;

use App\Models\Invoice;
use Illuminate\Http\Request;

class InvoiceController extends Controller
{
public function show(Request $request, string $serial)
{
/** @var Invoice $invoice */
$invoice = Invoice::where('serial_number', $serial)->firstOrFail();

$this->authorize('view', $invoice);

return $invoice->toPdfInvoice()->stream();
}

public function download(Request $request, string $serial)
{
/** @var Invoice $invoice */
$invoice = Invoice::where('serial_number', $serial)->firstOrFail();

$this->authorize('view', $invoice);

return $invoice->toPdfInvoice()->download();
}
}
```

### As a mail Attachment

The `Invoice` model provide a `toMailAttachment` method making it easy to use with `Mailable`

```php
namespace App\Mail;

use App\Models\Invoice;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class PaymentInvoice extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*/
public function __construct(
protected Invoice $invoice,
) {}


public function attachments(): array
{
return [
$this->invoice->toMailAttachment()
];
}
}
```

## Testing

```bash
Expand Down
5 changes: 5 additions & 0 deletions config/invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
'auto_generate' => true,

/**
* Define the serial number format used for each invoice type
*
* P: Prefix
* S: Serie
* M: Month
Expand All @@ -45,6 +47,9 @@
InvoiceType::Proforma->value => 'PPYYCCCC',
],

/**
* Define the default prefix used for each invoice type
*/
'prefix' => [
InvoiceType::Invoice->value => 'IN',
InvoiceType::Quote->value => 'QO',
Expand Down

0 comments on commit 5d84d1d

Please sign in to comment.