Download the PHP package elegantly/laravel-invoices without Composer
On this page you can find all versions of the php package elegantly/laravel-invoices. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download elegantly/laravel-invoices
More information about elegantly/laravel-invoices
Files in elegantly/laravel-invoices
Package laravel-invoices
Short Description Store invoices safely in your Laravel application
License MIT
Homepage https://github.com/ElegantEngineeringTech/laravel-invoices
Informations about the package laravel-invoices
Everything You Need to Manage Invoices in Laravel
This package provides a robust, easy-to-use system for managing invoices within a Laravel application, with options for database storage, serial numbering, and PDF generation.
Interactive Demo
Try out the interactive demo to explore package capabilities.
Table of Contents
- Requirements
- Installation
- The
PdfInvoiceClass- Full Example
- Rendering the Invoice as a PDF
- Storing the PDF in a file
- Downloading the Invoice as a PDF
- From a controller
- From a Livewire component
- Rendering the Invoice as a view
- Rendering the Invoice within a View
- Adding Taxes
- Tax by Percentage
- Tax as a Fixed Amount
- Adding Discounts
- Discount by Percentage
- Discount as a Fixed Amount
- Adding Payment Instructions
- QR Code Generation
- Customization
- Customizing Fonts
- Customizing the Invoice Template
- The
InvoiceEloquent Model- Complete Example
- Generating Unique Serial Numbers
- Using Multiple Prefixes and Series for Serial Numbers
- Customizing the Serial Number Format
- Storing the Logo
- Storing a Dynamic Logo
- Converting an
InvoiceModel to aPdfInvoice - Display, Download, and Store Invoices
- Attaching Invoices to Mailables
- Attaching Invoices to Notifications
- Customizing PDF Output from the Model
- Using a Custom PdfInvoice Class
- Casting
stateandtypeto Enums
- Testing
- Changelog
- Contributing
- Security Vulnerabilities
- Credits
- License
Requirements
- PHP 8.1+
- Laravel 11.0+
dompdf/dompdffor PDF renderingelegantly/laravel-moneyfor money computation which usebrick\moneyunder the hood
Installation
You can install the package via composer:
If you intent to store your invoices using the Eloquent Model, you must publish and run the migrations with:
You can publish the config file with:
This is the contents of the published config file:
The PdfInvoice Class
This package provides a powerful, standalone PdfInvoice class. Its main functionalities include the ability to:
- Display your invoice as a PDF document.
- Render your invoice within a Blade view.
The PdfInvoice class is also integrated with the Invoice Eloquent Model, allowing you to easily convert an Invoice model instance into its PDF representation.
You can even use this package exclusively for the PdfInvoice class if you don't require database storage for your invoices.
Full Example
Rendering the Invoice as a PDF
Storing the PDF in a file
Downloading the Invoice as a PDF
From a controller
To download the PDF, simply return the download method.
From a Livewire component
To download the PDF from a Livewire component, use the streamDownload method as shown below:
Rendering the Invoice as a view
Rendering the Invoice within a View
You can embed the invoice within a larger Blade view to create interfaces like an "invoice builder," similar to the interactive demo.
To do this, include the main invoice partial in your view as shown below:
This approach allows for seamless integration of the invoice into a dynamic and customizable user interface.
[!NOTE]
The default template uses Tailwind CSS for styling. This ensures seamless integration with websites already using Tailwind. If your project doesn't use Tailwind, the invoice styling may not appear as intended.
Adding Taxes
Taxes are applied to individual PdfInvoiceItem item. You can define them either as a percentage or a fixed amount.
Tax by Percentage
To add a tax as a percentage, set the tax_percentage property on the PdfInvoiceItem. This value should be a float between 0 and 100.
Tax as a Fixed Amount
To apply a tax as a specific monetary amount, set the unit_tax property on the PdfInvoiceItem.
Adding Discounts
Discounts are represented by the InvoiceDiscount class and are applied to the entire PdfInvoice. They cannot be attached to individual PdfInvoiceItems at this time.
- You can add multiple discounts to a single invoice.
- Discounts can be specified as a fixed amount (
amount_off) or a percentage (percent_off). If both are provided for the same discount, theamount_offvalue takes precedence.
Discount by Percentage
To apply a discount as a percentage, set the percent_off property.
Discount as a Fixed Amount
To apply a discount as a fixed amount, set the amount_off property.
Adding Payment Instructions
You can include detailed payment instructions directly within the generated PDF invoice. This can be helpful for providing bank transfer details, QR codes for quick payments, and custom payment links.
Here’s an example of how to add a payment instruction:
Note: You can include HTML tags (e.g., links) within the
fieldsarray for interactive content.
QR Code Generation
To dynamically generate QR codes, I recommend using the chillerlan/php-qrcode package. It provides a simple and flexible API for generating QR codes in various formats.
Customization
Customizing Fonts
See the Dompdf font guide.
Customizing the Invoice Template
To customize the invoice template, first publish the package's views:
After publishing, you can modify the Blade files in resources/views/vendor/invoices/ to suit your needs.
[!NOTE] If you introduce new CSS classes in your custom template, ensure you define their styles in the style.blade.php file.
Alternatively, to use a completely different custom template, you can specify its path in the configuration file:
[!WARNING] Your custom template file must be in
resources/views/vendor/invoices
Ensure that your custom template follows the same structure and conventions as the default one to maintain compatibility with various use cases.
The Invoice Eloquent Model
The design of the Invoice Eloquent Model closely mirrors that of the PdfInvoice class.
This model provides powerful features for:
- Generating unique and complex serial numbers.
- Attaching your invoice to any other Eloquent model.
- Easily including your invoice as an attachment in emails.
[!NOTE] Remember to publish and run the database migrations
Complete Example
The following example demonstrates how to create and store an invoice.
For this illustration, let's assume the following application structure:
Teammodels haveUsermodels.Teammodels can have multipleInvoicemodels.Invoicemodels can be attached toOrdermodels.
Generating Unique Serial Numbers
This package provides a simple and reliable way to generate serial numbers automatically, such as "INV240001".
You can configure the format of your serial numbers in the configuration file. The default format is PPYYCCCC, where each letter has a specific meaning (see the config file for details).
When invoices.serial_number.auto_generate is set to true, a unique serial number is assigned to each new invoice automatically.
Serial numbers are generated sequentially, with each new serial number based on the latest available one. To define what qualifies as the previous serial number, you can extend the Elegantly\Invoices\Models\Invoice class and override the getPreviousInvoice method.
By default, the previous invoice is determined based on criteria such as prefix, series, year, and month for accurate, scoped numbering.
Using Multiple Prefixes and Series for Serial Numbers
In more complex applications, you may need to use different prefixes and/or series for your invoices.
For instance, you might want to define a unique series for each user, creating serial numbers that look like: INV0001-2400X, where 0001 represents the user’s ID, 24 the year and X the index of the invoice.
[!NOTE] When using IDs for series, it's recommended to plan for future growth to avoid overflow. Even if you have a limited number of users now, ensure that the ID can accommodate the maximum number of digits allowed by the serial number format.
When creating an invoice, you can dynamically specify the prefix and series with configureSerialNumber method:
Customizing the Serial Number Format
In most cases, the format of your serial numbers should remain consistent, so it's recommended to set it in the configuration file.
The format you choose will determine the types of information you need to provide to configureSerialNumber.
Below is an example of the most complex serial number format you can create with this package:
Storing the Logo
By default, the PDF logo is loaded from the configuration defined in your config file.
If you want the logo to remain consistent over time, even if the config changes, you can store it directly in the database:
Storing a Dynamic Logo
If your application allows users to upload or select their own company logos, you can dynamically set the logo on each invoice by updating the logo attribute on the Invoice model.
You can do this in several ways:
Converting an Invoice Model to a PdfInvoice
You can obtained a PdfInvoice class from your Invoice model by calling the toPdfInvoice method:
Display, Download, and Store Invoices
You can then stream the PdfInvoice instance directly or initiate a download:
Attaching Invoices to Mailables
You can easily attach an invoice to your Mailable as follows:
Attaching Invoices to Notifications
You can easily attach an invoice to your Notification as follows:
Customizing PDF Output from the Model
To customize how your Invoice model is converted into a PdfInvoice object, follow these steps:
- Create a Custom Invoice Model:
Define your own App\Models\Invoice class and ensure it extends the base Elegantly\Invoices\Models\Invoice.
- Override the
toPdfInvoiceMethod:
In your custom Invoice model, override the toPdfInvoice method. This is where you'll implement your specific logic to construct and return the PdfInvoice object with your desired customizations.
- Update the Package Configuration:
First, if you haven't already, publish the package's configuration file:
Then, modify the config/invoices.php file to tell the package to use your custom model by updating the model_invoice key:
Using a Custom PdfInvoice Class
You can extend the default PdfInvoice class provided by the package to customize its behavior, such as changing the generated filename or adding additional logic.
- Create Your Custom PdfInvoice Class
In this example, we're overriding the getFilename method.
- Return Your Custom
PdfInvoicefrom the Invoice Model
Update your Invoice model to return an instance of your custom PdfInvoice class.
By overriding the toPdfInvoice method, you can inject your custom logic while preserving compatibility with the rest of the package.
Casting state and type to Enums
By default, the type and state properties on the Invoice model are stored as strings. This approach offers flexibility, as it doesn't restrict you to predefined values and they are not automatically cast to Enum objects.
However, you might prefer to cast these properties to Enum objects for better type safety and code clarity. You can use your own custom Enums or the ones provided by this package (e.g., Elegantly\Invoices\Enums\InvoiceState, Elegantly\Invoices\Enums\InvoiceType).
To enable Enum casting for these properties, follow these steps:
- Create custom
InvoiceStateandInvoiceTypeEnums (optional):
If you're working with commonly used invoice states and types, you can use the enums provided by this package:
Elegantly\Invoices\Enums\InvoiceStateElegantly\Invoices\Enums\InvoiceType
For custom states or types, you can define your own enums.
Make sure your custom enums implement the Elegantly\Invoices\Contracts\HasLabel contract, like so:
- Create a Custom
InvoiceModel:
Define your own App\Models\Invoice class that extends \Elegantly\Invoices\Models\Invoice.
In this custom model, override the casts() method to specify the Enum classes for the type and state attributes.
- Publish Package Configuration:
If you haven't already, publish the package's configuration file:
- Update Configuration to Use Your Custom Model:
Modify the config/invoices.php file and update the model_invoice key to point to your newly created custom Invoice model:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Quentin Gabriele
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-invoices with dependencies
dompdf/dompdf Version ^3.1
elegantly/laravel-money Version ^4.0.0
illuminate/contracts Version ^13.0
spatie/laravel-package-tools Version ^1.14