laraveldaily/laravel-invoices

Laravel缺失发票

4.0.0 2024-03-14 08:31 UTC

README

![Banner]

Laravel Invoices

Latest Stable Version Total Downloads Latest Unstable Version License

version 2 version 1

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

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

特性

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

安装

通过Composer

$ composer require laraveldaily/laravel-invoices:^4.0

旧版本

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

$ 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

基本用法

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 = InvoiceItem::make('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 = [
    InvoiceItem::make('Service 1')
        ->description('Your product or service description')
        ->pricePerUnit(47.79)
        ->quantity(2)
        ->discount(10),
    InvoiceItem::make('Service 2')->pricePerUnit(71.96)->quantity(2),
    InvoiceItem::make('Service 3')->pricePerUnit(4.56),
    InvoiceItem::make('Service 4')->pricePerUnit(87.51)->quantity(7)->discount(4)->units('kg'),
    InvoiceItem::make('Service 5')->pricePerUnit(71.09)->quantity(7)->discountByPercent(9),
    InvoiceItem::make('Service 6')->pricePerUnit(76.32)->quantity(9),
    InvoiceItem::make('Service 7')->pricePerUnit(58.18)->quantity(3)->discount(3),
    InvoiceItem::make('Service 8')->pricePerUnit(42.99)->quantity(4)->discountByPercent(3),
    InvoiceItem::make('Service 9')->pricePerUnit(33.24)->quantity(6)->units('m2'),
    InvoiceItem::make('Service 11')->pricePerUnit(97.45)->quantity(2),
    InvoiceItem::make('Service 12')->pricePerUnit(92.82),
    InvoiceItem::make('Service 13')->pricePerUnit(12.98),
    InvoiceItem::make('Service 14')->pricePerUnit(160)->units('hours'),
    InvoiceItem::make('Service 15')->pricePerUnit(62.21)->discountByPercent(5),
    InvoiceItem::make('Service 16')->pricePerUnit(2.80),
    InvoiceItem::make('Service 17')->pricePerUnit(56.21),
    InvoiceItem::make('Service 18')->pricePerUnit(66.81)->discountByPercent(8),
    InvoiceItem::make('Service 19')->pricePerUnit(76.37),
    InvoiceItem::make('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')
    // ability to include translated invoice status
    // in case it was paid
    ->status(__('invoices::invoice.paid'))
    ->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();

模板

发布资产后,您可以修改或创建自己的发票模板。

模板存储在resources/views/vendor/invoices/templates目录中。在那里您将找到默认使用的default.blade.php模板。

您可以通过在Invoice对象上调用template方法来指定要使用的模板。

例如,如果您有resources/views/vendor/invoices/templates/my_company.blade.php,它应该看起来像这样

Invoice::make('receipt')->template('my_company');

要查看模板中事物是如何工作的,您可以查看default.blade.php作为示例。

配置

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)
  • status(string) - 如果需要,发票状态 [已支付/到期]
  • seller(PartyContract)
  • buyer(PartyContract)
  • setCustomData(mixed) - 允许用户将附加数据附加到发票
  • getCustomData() - 检索用于模板的附加数据
  • 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(CarbonInterface)
  • 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, ?string $locale) - 将浮点数转换为文字,第二个参数是区域设置
  • getTotalAmountInWords() - 将total_amount转换为文字
  • formatCurrency(float) - 返回带有货币设置的格式化值,例如 '$ 1,99'

文件

  • stream() - 在浏览器中打开发票
  • download() - 提供下载发票
  • save($disk) - 将发票保存到存储,使用 ->filename() 设置文件名
  • url() - 返回保存的发票URL
  • toHtml() - 渲染HTML视图而不是PDF

发票项

  • make(string) - [静态函数] 与 (new InvoiceItem())->title(string) 相同
  • title(string) - 产品或服务名称
  • description(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。请参阅许可证文件获取更多信息。