LaravelDaily / laravel-invoices

Laravel package to generate PDF invoices from various customizable parameters
GNU General Public License v3.0
1.39k stars 303 forks source link

Add optional Quantity formatting methods #164

Closed dragonfly4 closed 1 year ago

dragonfly4 commented 2 years ago

The value quantity is displayed as a float, examples are 1 and 1.25 In Europe we display values with comma so I have added a QuantityFormatter trait to add this option to the package.

People have 3 options:

  1. change nothing in the blade file {{ $item->quantity }} This will display 1 or 1.25

  2. use fixed number formatting {{ $invoice->formatQuantityFixed($item->quantity) }} This will display based on the config options 1.00 - 1,00 - 1.25 - 1,25

  3. use dynamic number formatting {{ $invoice->formatQuantityDynamic($item->quantity) }} This will display based on the config options 1 - 1.25 - 1,25 - 1.265 - 1.265,18

mc0de commented 1 year ago

These are excessive methods, please format quantities in your custom template.

You don't have to rely only on default template provided with package.

https://github.com/LaravelDaily/laravel-invoices#templates

dragonfly4 commented 1 year ago

Anything can be solved in the template but sometimes it's easier to configure it in the base.

I extended the class so here it is in case someone else wants to use my methods. Check the PR changes for full details.

Edit the config file to add new defaults [ config\invoices.php ]

    'quantity' => [
        /*
         * Example: 19.00
         */
        'decimals' => 2,
        /*
         * Example: 1.99
         */
        'decimal_point' => '.',
        /*
         * By default empty.
         * Example: 1,999.00
         */
        'thousands_separator' => '',
    ],

Download "QuantityFormatter.php" to "app\Vendor\LaravelDaily\Invoices\Traits\QuantityFormatter.php"

Create a new file in app\Vendor\LaravelDaily\Invoices\Invoice.php

<?php
namespace App\Vendor\LaravelDaily\Invoices;

use LaravelDaily\Invoices\Invoice as BaseInvoice;
use App\Vendor\LaravelDaily\Invoices\Traits\QuantityFormatter;

class Invoice extends BaseInvoice
{
    use QuantityFormatter;

    /**
     * Invoice constructor.
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function __construct()
    {

        parent::__construct();

        // Quantity
        $this->quantity_decimals            = config('invoices.quantity.decimals');
        $this->quantity_decimal_point       = config('invoices.quantity.decimal_point');
        $this->quantity_thousands_separator = config('invoices.quantity.thousands_separator');

    }
}

In your controller you need to reference the new file.

use App\Vendor\LaravelDaily\Invoices\Invoice;