elitexp/laravel-invoices

Laravel缺少发票

1.3.17 2020-08-01 03:44 UTC

README

Latest Stable Version Total Downloads Latest Unstable Version License

这个Laravel包提供了一个易于使用的界面,用于使用您提供的数据生成发票PDF文件

发票文件可以存储、下载、在您配置的任何文件系统中流式传输。支持不同的模板和地区。

原始包是在PHP 7.3.11和Laravel 6.2上开发的,但也应该适用于较低版本。

特性

  • 税费 - 固定税率或比率 - 对项目或发票
  • 折扣 - 固定金额或百分比 - 对项目或发票
  • 运费 - 将运费添加到您的发票中
  • 自动计算 - 提供最小信息集,或自行计算并提供打印内容
  • 到期日
  • 易于自定义货币格式
  • 您喜欢的序列号
  • 模板
  • 翻译
  • 全局设置和即时覆盖

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

安装

通过Composer

$ composer require laraveldaily/laravel-invoices

安装Laravel Invoices后,使用invoices:install Artisan命令发布其资产、视图、翻译和配置

$ php artisan invoices:install

更新

由于它发展迅速,您可能希望在更新后使用Artisan命令获取最新的模板

$ php artisan invoices:update

如果确实想覆盖默认资源,它会给出警告

或者,也可以单独完成。

$ php artisan vendor:publish --tag=invoices.views --force
$ php artisan vendor:publish --tag=invoices.translations --force

对于Laravel版本 < 5.5

如果您不使用自动发现,请将ServiceProvider添加到config/app.php中的providers数组

LaravelDaily\Invoices\InvoiceServiceProvider::class,

如果您想使用外观来生成发票,请将其添加到config/app.php中的外观

'Invoice' => LaravelDaily\Invoices\Facades\Invoice::class

基本用法

RandomController.php

use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;

<...>

        $customer = new Buyer([
            'name'          => 'John Doe',
            'custom_fields' => [
                'email' => 'test@example.com',
            ],
        ]);

        $item = (new InvoiceItem())->title('Service 1')->pricePerUnit(2);

        $invoice = Invoice::make()
            ->buyer($customer)
            ->discountByPercent(10)
            ->taxRate(15)
            ->shipping(1.99)
            ->addItem($item);

        return $invoice->stream();

查看结果Invoice_AA_00001.pdf

高级用法

use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Party;
use LaravelDaily\Invoices\Classes\InvoiceItem;

<...>

        $client = new Party([
            'name'          => 'Roosevelt Lloyd',
            'phone'         => '(520) 318-9486',
            'custom_fields' => [
                'note'        => 'IDDQD',
                'business id' => '365#GG',
            ],
        ]);

        $customer = new Party([
            'name'          => 'Ashley Medina',
            'address'       => 'The Green Street 12',
            'code'          => '#22663214',
            'custom_fields' => [
                'order number' => '> 654321 <',
            ],
        ]);

        $items = [
            (new InvoiceItem())->title('Service 1')->pricePerUnit(47.79)->quantity(2)->discount(10),
            (new InvoiceItem())->title('Service 2')->pricePerUnit(71.96)->quantity(2),
            (new InvoiceItem())->title('Service 3')->pricePerUnit(4.56),
            (new InvoiceItem())->title('Service 4')->pricePerUnit(87.51)->quantity(7)->discount(4)->units('kg'),
            (new InvoiceItem())->title('Service 5')->pricePerUnit(71.09)->quantity(7)->discountByPercent(9),
            (new InvoiceItem())->title('Service 6')->pricePerUnit(76.32)->quantity(9),
            (new InvoiceItem())->title('Service 7')->pricePerUnit(58.18)->quantity(3)->discount(3),
            (new InvoiceItem())->title('Service 8')->pricePerUnit(42.99)->quantity(4)->discountByPercent(3),
            (new InvoiceItem())->title('Service 9')->pricePerUnit(33.24)->quantity(6)->units('m2'),
            (new InvoiceItem())->title('Service 11')->pricePerUnit(97.45)->quantity(2),
            (new InvoiceItem())->title('Service 12')->pricePerUnit(92.82),
            (new InvoiceItem())->title('Service 13')->pricePerUnit(12.98),
            (new InvoiceItem())->title('Service 14')->pricePerUnit(160)->units('hours'),
            (new InvoiceItem())->title('Service 15')->pricePerUnit(62.21)->discountByPercent(5),
            (new InvoiceItem())->title('Service 16')->pricePerUnit(2.80),
            (new InvoiceItem())->title('Service 17')->pricePerUnit(56.21),
            (new InvoiceItem())->title('Service 18')->pricePerUnit(66.81)->discountByPercent(8),
            (new InvoiceItem())->title('Service 19')->pricePerUnit(76.37),
            (new InvoiceItem())->title('Service 20')->pricePerUnit(55.80),
        ];

        $notes = [
            'your multiline',
            'additional notes',
            'in regards of delivery or something else',
        ];
        $notes = implode("<br>", $notes);

        $invoice = Invoice::make('receipt')
            ->series('BIG')
            ->sequence(667)
            ->serialNumberFormat('{SEQUENCE}/{SERIES}')
            ->seller($client)
            ->buyer($customer)
            ->date(now()->subWeeks(3))
            ->dateFormat('m/d/Y')
            ->payUntilDays(14)
            ->currencySymbol('$')
            ->currencyCode('USD')
            ->currencyFormat('{SYMBOL}{VALUE}')
            ->currencyThousandsSeparator('.')
            ->currencyDecimalPoint(',')
            ->filename($client->name . ' ' . $customer->name)
            ->addItems($items)
            ->notes($notes)
            ->logo(public_path('vendor/invoices/sample-logo.png'))
            // You can additionally save generated invoice to configured disk
            ->save('public');
            
        $link = $invoice->url();
        // Then send email to party with link

        // And return invoice itself to browser or have a different view
        return $invoice->stream();

查看结果Roosevelt Lloyd Ashley Medina.pdf

使用外观的替代方案

可选地,您可以使用外观来创建新的当事人或项目

use Invoice;

$customer = Invoice::makeParty([
    'name' => 'John Doe',
]);

$item = Invoice::makeItem('Your service or product title')->pricePerUnit(9.99);

return Invoice::make()->buyer($customer)->addItem($item)->stream();

配置

return [
    'date' => [
        /**
         * Carbon date format
         */
        'format'         => 'Y-m-d',
        /**
         * Due date for payment since invoice's date.
         */
        'pay_until_days' => 7,
    ],

    'serial_number' => [
        'series'           => 'AA',
        'sequence'         => 1,
        /**
         * Sequence will be padded accordingly, for ex. 00001
         */
        'sequence_padding' => 5,
        'delimiter'        => '.',
        /**
         * Supported tags {SERIES}, {DELIMITER}, {SEQUENCE}
         * Example: AA.00001
         */
        'format'           => '{SERIES}{DELIMITER}{SEQUENCE}',
    ],

    'currency' => [
        'code'                => 'eur',
        /**
         * Usually cents
         * Used when spelling out the amount and if your currency has decimals.
         *
         * Example: Amount in words: Eight hundred fifty thousand sixty-eight EUR and fifteen ct.
         */
        'fraction'            => 'ct.',
        'symbol'              => '',
        /**
         * Example: 19.00
         */
        'decimals'            => 2,
        /**
         * Example: 1.99
         */
        'decimal_point'       => '.',
        /**
         * By default empty.
         * Example: 1,999.00
         */
        'thousands_separator' => '',
        /**
         * Supported tags {VALUE}, {SYMBOL}, {CODE}
         * Example: 1.99 €
         */
        'format'              => '{VALUE} {SYMBOL}',
    ],

    'paper' => [
        // A4 = 210 mm x 297 mm = 595 pt x 842 pt
        'size'        => 'a4',
        'orientation' => 'portrait',
    ],

    'disk' => 'local',

    'seller' => [
        /**
         * Class used in templates via $invoice->seller
         *
         * Must implement LaravelDaily\Invoices\Contracts\PartyContract
         *      or extend LaravelDaily\Invoices\Classes\Party
         */
        'class' => \LaravelDaily\Invoices\Classes\Seller::class,

        /**
         * Default attributes for Seller::class
         */
        'attributes' => [
            'name'          => 'Towne, Smith and Ebert',
            'address'       => '89982 Pfeffer Falls Damianstad, CO 66972-8160',
            'code'          => '41-1985581',
            'vat'           => '123456789',
            'phone'         => '760-355-3930',
            'custom_fields' => [
                /**
                 * Custom attributes for Seller::class
                 *
                 * Used to display additional info on Seller section in invoice
                 * attribute => value
                 */
                'SWIFT' => 'BANK101',
            ],
        ],
    ],
];

可用方法

几乎每个配置值都可以通过方法动态覆盖。

发票

常规

  • addItem(InvoiceItem $item)
  • addItems(Iterable)
  • name(string)
  • seller(PartyContract)
  • buyer(PartyContract)
  • template(string)
  • logo(string) - logo路径
  • getLogo() - 返回base64编码的图像,在模板中使用以避免路径问题
  • filename(string) - 覆盖自动文件名
  • taxRate(float)
  • shipping(float) - 运费金额
  • totalDiscount(float) - 如果未提供,则自行计算
  • totalTaxes(float) - 如果未提供,则自行计算
  • totalAmount(float) - 如果未提供,则自行计算
  • taxableAmount(float) - 如果未提供,则自行计算

序列号

  • series(string)
  • sequence(int)
  • delimiter(string)
  • sequencePadding(int)
  • serialNumberFormat(string)
  • getSerialNumber() - 返回格式化的序列号

日期

  • date(Carbon)
  • dateFormat(string) - 日期的Carbon格式
  • payUntilDays(int) - 自发票发出起付款天数
  • getDate() - 返回格式化的日期
  • getPayUntilDate() - 返回格式化的到期日期

货币

  • currencyCode(string) - EUR, USD等
  • currencyFraction(string) - 分,分,便士等
  • currencySymbol(string)
  • currencyDecimals(int)
  • currencyDecimalPoint(string)
  • currencyThousandsSeparator(string)
  • currencyFormat(string)
  • getAmountInWords(float) - 将浮点数转换为文字(仅限英文)
  • getTotalAmountInWords() - 将total_amount转换为文字
  • formatCurrency(float) - 返回带有货币设置的格式化值 '$ 1,99'

文件

  • stream() - 在浏览器中打开发票
  • download() - 提供下载发票的选项
  • save($disk) - 将发票保存到存储,使用 ->filename() 设置文件名
  • url() - 返回已保存发票的URL

InvoiceItem

  • title(string) - 产品或服务名称
  • units(string) - 项目测量单位(如果设置,则添加单位列)
  • quantity(float) - 项目的单位数量
  • pricePerUnit(float)
  • discount(float) - 货币形式的折扣
  • discountByPercent(float) - 百分比折扣,discountByPercent(15) 表示 15%
  • tax(float)
  • taxByPercent(float)
  • subTotalPrice(float) - 如果未提供,将自动计算

测试

$ composer test

安全性

如果您发现任何安全相关的问题,请发送电子邮件至 mysticcode@gmail.com,而不是使用问题跟踪器。

作者

许可证

GPL-3.0-only。请参阅 许可证文件 了解更多信息。