netcore/module-invoice

生成发票的模块。

v1.2.4 2018-07-11 23:57 UTC

README

此模块旨在简化发票创建过程。

特性

  • 可翻译的发票项
  • 发票可以附加到用户、订单或其他相关内容
  • 自定义PDF模板

安装前准备

此模块是Netcore CMS生态系统的一部分,仅在已安装以下包的项目中有效

  1. https://github.com/netcore/netcore
  2. https://github.com/netcore/module-admin
  3. https://github.com/netcore/module-translate

安装

  1. 使用composer安装此包
    composer require netcore/module-invoice
  1. 发布 assets/configuration/migrations
    php artisan module:publish Invoice
    php artisan module:publish-config Invoice
    php artisan module:publish-migration Invoice
  1. 重要 - 在迁移之前配置关系
    edit config/netcore/module-invoice.php file to enable/disable used relations
  1. 运行迁移和种子文件
    php artisan migrate
    php artisan module:seed Invoice

配置

  • 配置文件位于 config/netcore/module-invoice.php

关系

  • 关系将从配置中加载
    return [
        'relations' => [
            [
                'name'       => 'user', // relation name
                'type'       => 'belongsTo', // relation type
                'foreignKey' => 'user_id', // foreign key (user_id in invoices table in this case)
                'ownerKey'   => 'id', // owner key (id in related table in this case)
                'enabled'    => false, // is relation enabled? (it should be enable when migrating)
                'class'      => \App\User::class, // related model class
    
                // Datatable colum config
                'table' => [
                    'show' => true, // Show this column?
                    'name' => 'User', // Column name?
    
                    'searchable' => true, // Is column searchable?
                    'sortable'   => true, // Is column sortable?
                    'd_data'     => 'user', // Datatables data param
                    'd_name'     => 'user.first_name', // Datatables SQL field param
                    'modifier'   => 'fullName', // Accessor in model to format display format
                ],
            ],
        ...
    ];

用户关系

  • 要使用 ->forUser() 方法,您应该在您的用户模型中实现 getInvoiceReceiverData() 方法
    /**
     * Get user data for invoices.
     *
     * @return array
     */
    public function getInvoiceReceiverData(): array
    {
        return [
            'first_name' => $this->first_name,
            'last_name'  => $this->last_name,
            'email'      => $this->email,
            'phone'      => $this->phone,
        ];
    }

创建发票

  • 要创建发票,您可以使用 invoice() 辅助方法
    $user = auth()->user();
    $items = [
        [
            'price' => 10.99,
            'name'  => 'Test item #1' // Name is equal for all languages 
        ],
        [
            'price' => 25.65,
            // Name is different for each language
            'translations' => [
                'en' => ['name' => 'First product.'],                    
                'ru' => ['name' => 'Первый товар..'],         
                'lv' => ['name' => 'Pirmā prece.'],
            ],
        ]
    ];

    $invoice = invoice()
        ->setItems($items)
        ->setPaymentDetails('VISA ending XXXX')
        
        ->forUser($user) // optional - set associated user (user relation should be enabled and configured)
        ->setInvoiceNr('MY123') // optional - set custom invoice nr.
        ->setVat(21) // optional - overrides vat specified in config
        ->setSender([ 'name' => 'My awesome company', ... ]) // optional - overrides sender data specified in config
        ->setReceiver([ 'first_name' => ..., 'last_name' => ... ]) // optional - overrides receiver data
        ->mergeReceiver([ 'some_additional_field' => ... ]) // optional - use if you need to add some extra receiver data

        ->make(); // build eveything up and returns Invoice instance