PHP code example of finller / laravel-invoices

1. Go to this page and download the library: Download finller/laravel-invoices library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

finller / laravel-invoices example snippets


use Brick\Money\Money;
use Elegantly\Invoices\Collections\InvoiceDiscountCollection;
use Elegantly\Invoices\Collections\InvoiceTaxCollection;
use Elegantly\Invoices\Collections\PdfInvoiceItemCollection;
use Elegantly\Invoices\Enums\InvoiceState;
use Elegantly\Invoices\Enums\InvoiceType;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
use Elegantly\Invoices\Pdf\PdfInvoice;
use Elegantly\Invoices\Pdf\PdfInvoiceItem;
use Elegantly\Invoices\Support\Address;
use Elegantly\Invoices\Support\Identity;
use Elegantly\Invoices\Support\Party;
use Elegantly\Invoices\Support\PaymentInstruction;
use Elegantly\Invoices\Support\TaxId;

$pdfInvoice = new PdfInvoice(
    type: InvoiceType::Invoice,
    state: InvoiceState::Pending,
    serial_number: 'INV-260001',
    created_at: now(),
    due_at: now()->addDays(30),
    fields: ['Order' => 'PO0234'],
    seller: new Party(
        company: 'Acme Studio',
        name: 'Jane Doe',
        address: new Address(
            street: "Place de l'Opéra",
            city: 'Paris',
            postal_code: '75009',
            country: 'FR',
        ),
        tax_id: new TaxId(country: 'FR', code: '123456789'),
        email: '[email protected]',
        phone: '+33 1 23 45 67 89',
        identities: [new Identity(type: 'SIREN', code: '732829320')],
        fields: ['Website' => 'https://example.com'],
    ),
    buyer: new Party(
        company: 'Doe Corporation',
        name: 'John Doe',
        address: new Address(
            street: '8405 Old James St',
            city: 'New York',
            postal_code: '14609',
            state: 'NY',
            country: 'US',
        ),
        shipping_address: new Address(
            street: ['8405 Old James St', 'Apartment 1'],
            city: 'New York',
            postal_code: '14609',
            state: 'NY',
            country: 'US',
        ),
        email: '[email protected]',
    ),
    items: new PdfInvoiceItemCollection([
        new PdfInvoiceItem(
            label: 'Consulting',
            unit_price: Money::of('100.00', 'EUR'),
            quantity: 2,
            quantity_unit: 'hours',
            description: 'Application development',
            discounts: new InvoiceDiscountCollection([
                new InvoiceDiscount(
                    code: 'WELCOME',
                    label: 'Welcome offer',
                    percentage: 10,
                ),
            ]),
            taxes: new InvoiceTaxCollection([
                new InvoiceTax(
                    type: 'vat',
                    country: 'FR',
                    percentage: 20,
                    label: 'VAT France (20%)',
                ),
            ]),
        ),
    ]),
    description: 'Thank you for your business.',
    paymentInstructions: [
        new PaymentInstruction(
            name: 'Bank transfer',
            description: 'Use the invoice number as the payment reference.',
            fields: [
                'IBAN' => 'GB12ACME12345678123456',
                'SWIFT/BIC' => 'ACMEGB2L',
                'Reference' => 'INV-260001',
            ],
        ),
    ],
    // Optional: logo: public_path('images/logo.png'),
    template: 'default.layout',
    templateData: ['font' => 'DejaVu Sans'],
);

$pdfInvoice->denormalize();

// Subtotal: EUR 200.00; discount: EUR 20.00;
// tax: EUR 36.00; total: EUR 216.00.

return $pdfInvoice->stream();

use Elegantly\Invoices\Pdf\PdfInvoice;

$pdfInvoice = PdfInvoice::make([
    'type' => 'invoice',
    'state' => 'pending',
    'serial_number' => 'INV-260002',
    'currency' => 'EUR',
    'created_at' => now(),
    'seller' => config('invoices.default_seller'),
    'buyer' => [
        'company' => 'Doe Corporation',
        'email' => '[email protected]',
    ],
    'items' => [
        [
            'label' => 'Consulting',
            'unit_price' => 100.00,
            'quantity' => 2,
            'discounts' => [['code' => 'WELCOME', 'percentage' => 10]],
            'taxes' => [['type' => 'vat', 'country' => 'FR', 'percentage' => 20]],
        ],
    ],
])->denormalize();

$pdfInvoice->denormalize();

use Brick\Money\Money;
use Elegantly\Invoices\Collections\PdfInvoiceItemCollection;
use Elegantly\Invoices\Pdf\PdfInvoice;
use Elegantly\Invoices\Pdf\PdfInvoiceItem;

$pdfInvoice = new PdfInvoice(
    serial_number: 'INV-260003',
    items: new PdfInvoiceItemCollection([
        new PdfInvoiceItem(
            label: 'Consulting',
            quantity: 2,
            unit_price: Money::of('100.00', 'EUR'),
            price_subtotal: Money::of('200.00', 'EUR'),
            price_discount: Money::of('20.00', 'EUR'),
            price_tax: Money::of('36.00', 'EUR'),
            price: Money::of('216.00', 'EUR'),
        ),
    ]),
    subtotal_amount: Money::of('200.00', 'EUR'),
    discount_amount: Money::of('20.00', 'EUR'),
    tax_amount: Money::of('36.00', 'EUR'),
    total_amount: Money::of('216.00', 'EUR'),
);

$pdfInvoice->items->first()->quantity = 3;
$pdfInvoice->denormalize(force: true);

use Brick\Money\Money;
use Elegantly\Invoices\Collections\InvoiceDiscountCollection;
use Elegantly\Invoices\Collections\InvoiceTaxCollection;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
use Elegantly\Invoices\Pdf\PdfInvoiceItem;

$item = new PdfInvoiceItem(
    label: 'Consulting',
    unit_price: Money::of('100.00', 'EUR'),
    quantity: 2,
    discounts: new InvoiceDiscountCollection([
        new InvoiceDiscount(code: 'SUMMER', label: 'Summer offer', percentage: 10),
        new InvoiceDiscount(label: 'Loyalty credit', amount: Money::of('5.00', 'EUR')),
    ]),
    taxes: new InvoiceTaxCollection([
        new InvoiceTax(type: 'vat', country: 'FR', percentage: 20),
        new InvoiceTax(label: 'Fixed levy', amount: Money::of('2.00', 'EUR')),
    ]),
);

$item->denormalize();
// Subtotal: 200.00; discounts: 25.00; taxes: 37.00; final amount: 212.00.

$pdfInvoice->fields = [
    'Order' => 'PO0234',
    'Customer reference' => 'ACME-42',
];

use Elegantly\Invoices\Support\PaymentInstruction;

$pdfInvoice->paymentInstructions = [
    new PaymentInstruction(
        name: 'Bank transfer',
        description: 'Pay using the bank details below.',
        qrcode: 'data:image/png;base64,'.base64_encode(
            file_get_contents(public_path('images/payment-qr.png'))
        ),
        fields: [
            'Bank' => 'Acme Bank',
            'IBAN' => 'GB12ACME12345678123456',
            'SWIFT/BIC' => 'ACMEGB2L',
            '<a href="https://example.com/pay">Pay online</a>',
        ],
    ),
];

namespace App\Http\Controllers;

use Elegantly\Invoices\Pdf\PdfInvoice;
use Illuminate\Support\Facades\Storage;

class PdfInvoiceController extends Controller
{
    public function show()
    {
        return $this->makeInvoice()->stream();
    }

    public function download()
    {
        return $this->makeInvoice()->download(filename: 'invoice.pdf');
    }

    public function store()
    {
        $pdfInvoice = $this->makeInvoice();

        Storage::disk('local')->put(
            'invoices/'.$pdfInvoice->getFilename(),
            $pdfInvoice->getPdfOutput(),
        );

        return response()->noContent();
    }

    private function makeInvoice(): PdfInvoice
    {
        return PdfInvoice::make([
            'serial_number' => 'INV-260001',
            'currency' => 'EUR',
            'created_at' => now(),
            'seller' => config('invoices.default_seller'),
            'buyer' => ['company' => 'Doe Corporation'],
            'items' => [
                ['label' => 'Consulting', 'unit_price' => 100, 'quantity' => 2],
            ],
        ])->denormalize();
    }
}

namespace App\Livewire;

use Elegantly\Invoices\Pdf\PdfInvoice;
use Livewire\Component;

class InvoicePreview extends Component
{
    public function download()
    {
        $pdfInvoice = PdfInvoice::make([
            'serial_number' => 'INV-260001',
            'currency' => 'EUR',
            'seller' => config('invoices.default_seller'),
            'buyer' => ['company' => 'Doe Corporation'],
            'items' => [
                ['label' => 'Consulting', 'unit_price' => 100, 'quantity' => 2],
            ],
        ])->denormalize();

        return response()->streamDownload(function () use ($pdfInvoice) {
            echo $pdfInvoice->getPdfOutput();
        }, $pdfInvoice->getFilename(), ['Content-Type' => 'application/pdf']);
    }

    public function render()
    {
        return view('livewire.invoice-preview');
    }
}

$dompdf = $pdfInvoice->pdf(
    options: ['defaultFont' => 'DejaVu Sans'],
    paper: ['size' => 'a4', 'orientation' => 'landscape'],
    data: ['footerText' => 'Thank you for your business'],
);

$dompdf->render();
$output = $dompdf->output();

namespace App\Http\Controllers;

use Elegantly\Invoices\Pdf\PdfInvoice;

class InvoicePreviewController extends Controller
{
    public function show()
    {
        $pdfInvoice = PdfInvoice::make([
            'serial_number' => 'INV-260001',
            'currency' => 'EUR',
            'seller' => config('invoices.default_seller'),
            'buyer' => ['company' => 'Doe Corporation'],
            'items' => [
                ['label' => 'Consulting', 'unit_price' => 100, 'quantity' => 2],
            ],
        ])->denormalize();

        return $pdfInvoice->view();
    }
}

namespace App\Mail;

use Elegantly\Invoices\Pdf\PdfInvoice;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;

class PdfInvoiceMail extends Mailable
{
    public function __construct(public PdfInvoice $pdfInvoice) {}

    public function content(): Content
    {
        return new Content(htmlString: '<p>Your invoice is attached.</p>');
    }

    public function attachments(): array
    {
        return [
            $this->pdfInvoice->toMailAttachment(filename: 'invoice.pdf'),
        ];
    }
}

use App\Mail\PdfInvoiceMail;
use Illuminate\Support\Facades\Mail;

Mail::to('[email protected]')->send(new PdfInvoiceMail($pdfInvoice));

namespace App\Notifications;

use Elegantly\Invoices\Pdf\PdfInvoice;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PdfInvoiceNotification extends Notification
{
    public function __construct(public PdfInvoice $pdfInvoice) {}

    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject('Your invoice')
            ->line('Your invoice is attached.')
            ->attach($this->pdfInvoice);
    }
}

use App\Notifications\PdfInvoiceNotification;
use Illuminate\Support\Facades\Notification;

Notification::route('mail', '[email protected]')
    ->notify(new PdfInvoiceNotification($pdfInvoice));

$pdfInvoice->templateData = [
    'font' => 'Arimo',
    'fonts' => [
        'https://fonts.googleapis.com/css2?family=Arimo:ital,wght@0,400..700;1,400..700&display=swap',
    ],
];

use Brick\Money\CurrencyDisplay;
use Brick\Money\Money;

use function Elegantly\Invoices\format_money;

$formatted = format_money(
    Money::of('100.00', 'EUR'),
    locale: 'fr',
    currencyDisplay: CurrencyDisplay::Symbol,
    hideFractionIfWhole: true,
);

namespace App\ValueObjects;

class PdfInvoice extends \Elegantly\Invoices\Pdf\PdfInvoice
{
    public function getFilename(): string
    {
        return 'acme-'.parent::getFilename();
    }
}

use Brick\Money\Money;
use Elegantly\Invoices\Collections\InvoiceDiscountCollection;
use Elegantly\Invoices\Collections\InvoiceTaxCollection;
use Elegantly\Invoices\Enums\InvoiceState;
use Elegantly\Invoices\Enums\InvoiceType;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
use Elegantly\Invoices\Models\Invoice;
use Elegantly\Invoices\Models\InvoiceItem;
use Elegantly\Invoices\Support\PaymentInstruction;
use Illuminate\Support\Facades\DB;

$invoice = new Invoice([
    'type' => InvoiceType::Invoice->value,
    'state' => InvoiceState::Pending->value,
    'state_set_at' => now(),
    'seller_information' => config('invoices.default_seller'),
    'buyer_information' => [
        'company' => 'Doe Corporation',
        'name' => 'John Doe',
        'address' => [
            'street' => ['8405 Old James St', 'Apartment 1'],
            'city' => 'New York',
            'postal_code' => '14609',
            'state' => 'NY',
            'country' => 'US',
        ],
        'email' => '[email protected]',
    ],
    'description' => 'Application development',
    'due_at' => now()->addDays(30),
    'fields' => ['Order' => 'PO0234'],
    'metadata' => ['source' => 'customer-portal'],
    'payment_instructions' => [
        new PaymentInstruction(
            name: 'Bank transfer',
            fields: ['IBAN' => 'GB12ACME12345678123456'],
        ),
    ],
]);

$invoice->configureSerialNumber(
    format: 'PPP-YYCCCC',
    prefix: 'INV',
    year: now()->year,
);

$invoice->setItems([
    new InvoiceItem([
        'label' => 'Consulting',
        'unit_price' => Money::of('100.00', 'EUR'),
        'quantity' => 2,
        'quantity_unit' => 'hours',
        'description' => 'Application development',
        'discounts' => new InvoiceDiscountCollection([
            new InvoiceDiscount(code: 'WELCOME', percentage: 10),
        ]),
        'taxes' => new InvoiceTaxCollection([
            new InvoiceTax(type: 'vat', country: 'FR', percentage: 20),
        ]),
        'metadata' => ['product_id' => 'consulting'],
    ]),
]);

DB::transaction(fn () => $invoice->denormalize()->saveWithItems());

// The invoice and its item are stored, with total_amount = EUR 216.00.

$invoice->denormalize()->saveWithItems();

use Brick\Money\Money;
use Elegantly\Invoices\Collections\InvoiceDiscountCollection;
use Elegantly\Invoices\Collections\InvoiceTaxCollection;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;
use Elegantly\Invoices\Models\Invoice;
use Elegantly\Invoices\Models\InvoiceItem;

$invoice = new Invoice([
    'subtotal_amount' => Money::of('200.00', 'EUR'),
    'discount_amount' => Money::of('20.00', 'EUR'),
    'tax_amount' => Money::of('36.00', 'EUR'),
    'total_amount' => Money::of('216.00', 'EUR'),
]);

$invoice->setItems([
    new InvoiceItem([
        'label' => 'Consulting',
        'quantity' => 2,
        'unit_price' => Money::of('100.00', 'EUR'),
        'price_subtotal' => Money::of('200.00', 'EUR'),
        'price_discount' => Money::of('20.00', 'EUR'),
        'price_tax' => Money::of('36.00', 'EUR'),
        'price' => Money::of('216.00', 'EUR'),
        'discounts' => new InvoiceDiscountCollection([
            new InvoiceDiscount(code: 'WELCOME', amount: Money::of('20.00', 'EUR')),
        ]),
        'taxes' => new InvoiceTaxCollection([
            new InvoiceTax(type: 'vat', country: 'FR', amount: Money::of('36.00', 'EUR')),
        ]),
    ]),
]);

$invoice->saveWithItems();

$invoice->items->first()->quantity = 3;
$invoice->denormalize(force: true)->saveWithItems();

use Brick\Money\Money;
use Elegantly\Invoices\Models\InvoiceItem;

// Set the items.
$invoice->setItems([
    new InvoiceItem(['label' => 'Support', 'unit_price' => Money::of('50.00', 'EUR')]),
]);

// Add more items.
$invoice->addItems([
    new InvoiceItem(['label' => 'Hosting', 'unit_price' => Money::of('10.00', 'EUR')]),
]);

$invoice->denormalize(force: true)->saveWithItems();

use Brick\Money\Money;
use Elegantly\Invoices\InvoiceDiscount;
use Elegantly\Invoices\InvoiceTax;

$item = $invoice->items->first();

$item->addDiscounts(new InvoiceDiscount(
    code: 'SUMMER',
    label: 'Summer offer',
    percentage: 10,
));

$item->addTaxes([
    new InvoiceTax(type: 'vat', country: 'FR', percentage: 20),
    new InvoiceTax(label: 'Fixed levy', amount: Money::of('2.00', 'EUR')),
]);

$invoice->denormalize(force: true)->saveWithItems();

use Elegantly\Invoices\Support\Party;
use Elegantly\Invoices\Support\PaymentInstruction;

$invoice->seller_information = Party::make(config('invoices.default_seller'));

$invoice->buyer_information = [
    'company' => 'Doe Corporation',
    'tax_id' => ['country' => 'FR', 'code' => '123456789'],
    'identities' => [['type' => 'SIREN', 'code' => '732829320']],
    'fields' => ['Customer number' => 'ACME-42'],
];

$invoice->mergeFields(['Order' => 'PO0234']);
$invoice->metadata = ['external_id' => 'order_123'];

$invoice->payment_instructions = [
    new PaymentInstruction(
        name: 'Bank transfer',
        description: 'Use the invoice number as the reference.',
        fields: ['IBAN' => 'GB12ACME12345678123456'],
    ),
];

$invoice->save();

$invoice->buyer()->associate($customer);
$invoice->seller()->associate($team);
$invoice->invoiceable()->associate($order);
$invoice->save();

namespace App\Models;

use Elegantly\Invoices\Models\Invoice;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Customer extends Model
{
    public function invoices(): MorphMany
    {
        return $this->morphMany(Invoice::class, 'buyer');
    }
}

use Elegantly\Invoices\Models\Invoice;

$credit = new Invoice(['type' => 'credit']);
$credit->parent()->associate($invoice);
// Add credit items and amounts, then denormalize and save.

use Elegantly\Invoices\Models\Invoice;

$paidInvoices = Invoice::invoice()->paid()->with('items')->get();
$draftQuotes = Invoice::quote()->draft()->get();
$pendingCredits = Invoice::credit()->pending()->get();
$refundedInvoices = Invoice::refunded()->get();
$proformas = Invoice::where('type', 'proforma')->get();

use Elegantly\Invoices\Models\Invoice;

$invoice = new Invoice;
$invoice->configureSerialNumber(
    format: 'PPP-SSSS-YYMMCCCC',
    prefix: 'INV',
    serie: 42,
    year: 2026,
    month: 10,
);
$invoice->save();

// First invoice in this scope: INV-0042-26100001.

// In config/invoices.php, alongside the other settings:
return [
    'serial_number' => [
        'auto_generate' => true,
        'format' => [
            'invoice' => 'PPPYYCCCC',
            'quote' => 'PPPYYCCCC',
            'credit' => 'PPPYYCCCC',
            'proforma' => 'PPPYYCCCC',
        ],
        'prefix' => [
            'invoice' => 'INV',
            'quote' => 'QUO',
            'credit' => 'CRE',
            'proforma' => 'PRO',
        ],
    ],
];

$invoice->setSerialNumber(
    value: 'INV-0042-26100001',
    format: 'PPP-SSSS-YYMMCCCC',
)->save();

use Elegantly\Invoices\SerialNumberGenerator;

$generator = new SerialNumberGenerator('PPP-YYCCCC');
$serial = $generator->generate(prefix: 'INV', year: 26, count: 1);
$parts = $generator->parse($serial); // prefix, serie, year, month, count

// Capture the current configured logo.
$invoice->setLogoFromConfig();

// Or capture a local file / uploaded file.
$invoice->setLogoFromPath(public_path('images/logo.png'));
$invoice->setLogoFromFile($uploadedFile);

// Or assign raw image bytes directly.
$invoice->logo = $rawImageBytes;

$invoice->save();

namespace App\Http\Controllers;

use Elegantly\Invoices\Models\Invoice;
use Illuminate\Support\Facades\Storage;

class InvoiceController extends Controller
{
    public function show(Invoice $invoice)
    {
        return $invoice->loadMissing('items')->toPdfInvoice()->stream();
    }

    public function download(Invoice $invoice)
    {
        return $invoice->loadMissing('items')->toPdfInvoice()->download();
    }

    public function preview(Invoice $invoice)
    {
        return $invoice->loadMissing('items')->toPdfInvoice()->view();
    }

    public function store(Invoice $invoice)
    {
        $pdfInvoice = $invoice->loadMissing('items')->toPdfInvoice();

        Storage::put(
            'invoices/'.$pdfInvoice->getFilename(),
            $pdfInvoice->getPdfOutput(),
        );

        return response()->noContent();
    }
}

use App\Http\Controllers\InvoiceController;
use Illuminate\Support\Facades\Route;

Route::get('/invoices/{invoice}', [InvoiceController::class, 'show']);
Route::get('/invoices/{invoice}/download', [InvoiceController::class, 'download']);
Route::get('/invoices/{invoice}/preview', [InvoiceController::class, 'preview']);
Route::post('/invoices/{invoice}/pdf', [InvoiceController::class, 'store']);

namespace App\Livewire;

use Elegantly\Invoices\Models\Invoice;
use Livewire\Component;

class InvoiceDownload extends Component
{
    public Invoice $invoice;

    public function mount(Invoice $invoice): void
    {
        $this->invoice = $invoice;
    }

    public function download()
    {
        $pdfInvoice = $this->invoice->loadMissing('items')->toPdfInvoice();

        return response()->streamDownload(function () use ($pdfInvoice) {
            echo $pdfInvoice->getPdfOutput();
        }, $pdfInvoice->getFilename(), ['Content-Type' => 'application/pdf']);
    }

    public function render()
    {
        return view('livewire.invoice-download');
    }
}

namespace App\Models;

use Elegantly\Invoices\Pdf\PdfInvoice;

class Invoice extends \Elegantly\Invoices\Models\Invoice
{
    public function toPdfInvoice(): PdfInvoice
    {
        $pdfInvoice = parent::toPdfInvoice();
        $pdfInvoice->template = 'invoices::my-custom.layout';
        $pdfInvoice->templateData = ['font' => 'DejaVu Sans'];
        $pdfInvoice->fields['Support'] = '[email protected]';

        return $pdfInvoice;
    }
}

namespace App\Mail;

use Elegantly\Invoices\Models\Invoice;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;

class InvoiceMail extends Mailable
{
    public function __construct(public Invoice $invoice) {}

    public function content(): Content
    {
        return new Content(htmlString: '<p>Your invoice is attached.</p>');
    }

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

use App\Mail\InvoiceMail;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Support\Facades\Mail;

$invoice = Invoice::with('items')->findOrFail($invoiceId);

Mail::to($invoice->buyer_information->email)->send(new InvoiceMail($invoice));

namespace App\Notifications;

use Elegantly\Invoices\Models\Invoice;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class InvoiceNotification extends Notification
{
    public function __construct(public Invoice $invoice) {}

    public function via(object $notifiable): array
    {
        return ['mail'];
    }

    public function toMail(object $notifiable): MailMessage
    {
        return (new MailMessage)
            ->subject('Your invoice')
            ->line('Your invoice is attached.')
            ->attach($this->invoice);
    }
}

use App\Notifications\InvoiceNotification;
use Elegantly\Invoices\Models\Invoice;
use Illuminate\Support\Facades\Notification;

$invoice = Invoice::with('items')->findOrFail($invoiceId);

Notification::route('mail', $invoice->buyer_information->email)
    ->notify(new InvoiceNotification($invoice));

use Elegantly\Invoices\Enums\InvoiceType;

$credit = $invoice->replicate();
$credit->type = InvoiceType::Credit->value;
$credit->configureSerialNumber(format: 'PPP-YYCCCC', prefix: 'CRE');
$credit->parent()->associate($invoice);
$credit->setItems($invoice->items->replicate());
$credit->multiplyBy(-1)->saveWithItems();

use Brick\Money\AllocationMode;
use Brick\Money\Money;

$invoice->items->allocate(
    subtotal: Money::of('100.00', 'EUR'),
    tax: Money::of('18.00', 'EUR'),
    discount: Money::of('10.00', 'EUR'),
    total: Money::of('108.00', 'EUR'),
    mode: AllocationMode::FloorToFirst,
);

$invoice->subtotal_amount = $invoice->items->sumMoney('price_subtotal');
$invoice->discount_amount = $invoice->items->sumMoney('price_discount');
$invoice->tax_amount = $invoice->items->sumMoney('price_tax');
$invoice->total_amount = $invoice->items->sumMoney('price');
$invoice->saveWithItems();

use Elegantly\Invoices\Models\Invoice;

$invoices = Invoice::paid()->where('currency', 'EUR')->with('items')->get();
$total = $invoices->sumMoney('total_amount');
$pdfInvoices = $invoices->toPdfInvoices();

$subtotal = $invoice->items->sumMoney('price_subtotal');
$pdfItems = $invoice->items->toPdfItems();

use Elegantly\Invoices\Integrations\StripeCheckoutIntegration;
use Elegantly\Invoices\Models\Invoice;
use Stripe\StripeClient;

$stripe = new StripeClient(config('services.stripe.secret'));
$lineItems = $stripe->checkout->sessions->allLineItems($checkoutSessionId, [
    'expand' => [
        'data.price',
        'data.discounts.discount.promotion_code',
        'data.taxes',
    ],
]);

$integration = new StripeCheckoutIntegration;
$invoice = new Invoice([
    'state' => 'paid',
    'state_set_at' => now(),
    'seller_information' => config('invoices.default_seller'),
    'buyer_information' => $buyerSnapshot,
]);

$invoice->setItems($integration->toInvoiceItemCollection($lineItems));
$invoice->denormalize()->saveWithItems();

use Elegantly\Invoices\Models\Invoice;
use Illuminate\Support\Facades\Storage;

$invoice = Invoice::with(['items', 'parent'])->firstOrFail();
$gobl = $invoice->toGOBL();

Storage::put(
    'invoices/'.$invoice->serial_number.'.json',
    json_encode($gobl, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR),
);

$gobl = $invoice->toGOBL(['regime' => 'FR']);
$line = $invoice->items->first()->toGOBL([
    'item' => ['ref' => 'SKU-001'],
]);

// In config/invoices.php, alongside the other settings:
return [
    'model_invoice' => \App\Models\Invoice::class,
    'model_invoice_item' => \App\Models\InvoiceItem::class,
    'discount_class' => \App\ValueObjects\InvoiceDiscount::class,
    'tax_class' => \App\ValueObjects\InvoiceTax::class,
    'party_class' => \App\ValueObjects\Party::class,
    'identity_class' => \App\ValueObjects\Identity::class,
    'address_class' => \App\ValueObjects\Address::class,
    'tax_id_class' => \App\ValueObjects\TaxId::class,
    'payment_instructions_class' => \App\ValueObjects\PaymentInstruction::class,
];

namespace App\Models;

use Elegantly\Invoices\Enums\InvoiceState;
use Elegantly\Invoices\Enums\InvoiceType;

class Invoice extends \Elegantly\Invoices\Models\Invoice
{
    protected function casts(): array
    {
        return [
            ...parent::casts(),
            'type' => InvoiceType::class,
            'state' => InvoiceState::class,
        ];
    }

    public function getType(): InvoiceType
    {
        return $this->type;
    }

    public function getState(): InvoiceState
    {
        return $this->state;
    }
}
bash
php artisan vendor:publish --tag="invoices-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="invoices-config"
blade
<div class="aspect-[210/297] bg-white shadow-md">
    @
bash
php artisan vendor:publish --tag="invoices-views"
bash
php artisan vendor:publish --tag="invoices-translations"
bash
composer