nfe-easy / nfe-easy
管理NFe资源的简易包
Requires
- php: >=7.0.13
- illuminate/support: ^5.3
- lightools/xml: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is auto-updated.
Last update: 2024-09-05 10:25:55 UTC
README
介绍
NFeEasy 是一个管理NFe文件的包装器。
要求
- PHP 7.0.13 或更高版本
安装
Composer
您可以通过Composer安装
$ composer require nfe-easy/nfe-easy
并使用Composer自动加载
require_once('vendor/autoload.php');
手动安装
如果您不想使用Composer,请下载最新版本的 NefEasy 并包含 init.php 文件
require_once('/path/to/nfeasy/init.php');
您还需要下载 NfeEasy 的PHP依赖项并手动自动加载。请参阅 composer.json 了解NfeEasy依赖项。
如何使用
要使用NfeEasy,只需将nfe xml文件内容传递给XmlInvoiceBuilder,它将返回表示给定NFe的PHP对象。
XmlInvoiceBuilder::::create( file_get_contents('path/to//my/invoice.xml') ); // returns an instance of NFeEasy\Invoice
域对象
每个来自NFe文件的域都由一个NFeEasy对象类表示。这些类称为 域对象类。
域对象类列表包括
NFeEasy\InvoiceNFeEasy\ProductNFeEasy\EmitterNFeEasy\ReceiverNFeEasy\Address
完整的 NFe 文件由一个或多个 NFeEasy 域对象类 之间的关系表示。
NFeEasy 域对象类包含从NFe文件中提取的数据。数据在NfeEasy域对象类之间划分。
完整表示
使用 NFeEasy\Builder\XmlInvoiceBuilder::create 方法可以获得完整的NfeEasy表示。它将返回一个 NFeEasy\Invoice 实例。
发票
NFeEasy\Invoice 表示NFe文件本身,并包含用于标识NFe的数据。
下表描述了您可以从 NFeEasy\Invoice 类访问的所有属性
产品
NFeEasy\Product 表示发票的产品。它包含有关产品名称、NCM、价值、数量、税项和其他与产品相关的信息。
下表描述了您可以从 NFeEasy\Product 类访问的所有属性
发射器
NFeEasy\Emitter 表示NFe发射器
下表描述了您可以从 NFeEasy\Product 类访问的所有属性
接收者
NFeEasy\Receiver 表示NFe接收者
下表描述了您可以从 NFeEasy\Product 类访问的所有属性
地址
NFeEasy\Address 表示地址对象
下表描述了您可以从 NFeEasy\Address 类访问的所有属性
创建域对象
您可以单独创建域对象类。只需使用相应对象的 created 方法,并传递您想填充到对象中的参数即可
$address = Address::create([ 'xLgr' => 'Some address street', 'nro' => '207' ]); // returns an NFeEasy\Address
如果您不想单独创建域对象,可以直接将所有数据传递给 NfeEasy\Invoice
$invoice = Invoice::create([ 'cUF' => '32', 'natOp' => 'Venda Sub / Venda Mer', 'addicionalInfo' => 'Some Info', // ... 'emitter' => [ 'xNome' => 'Emitter Name', 'CNPJ' => '23740049120232' // ... ], 'receiver' => [ 'xNome' => 'Receiver Name', 'CNPJ' => '40193549120112' // ... ], 'products' => [ [ 'cProd' => '13' 'xProd' => 'Shampoo' // ... ], [ 'cProd' => '345' 'xProd' => 'Soap' // ... ], // ... ] ]); // returns an NFeEasy\Invoice
属性
创建域对象后,您可以通过访问属性名称来访问数据
$product = Product::create([ 'cProd' => '12' 'xProd' => 'Notebook' ]); echo $product->xProd; // prints 'Shampoo'
您也可以通过属性名称访问子对象
$emitter = Emitter::create([ 'xNome' => 'Andrew' 'CNPJ' => '23740049120232', 'address' => [ 'xLgr' => 'Rua Ivaí', 'nro' => 207, 'xBairro' => 'Tatuapé' ], ]); $emitter->address; // returns an instance of NfeEasy\Address; echo $emitter->address->xBairro; prints 'Tatuapé'
集合
NfeEasy 中的域对象数组被视为集合。所有对象集合都由 Illuminate\Support\Collection 的实例表示。
NfeEasy 中的集合用于
- Invoice 对象中的
products属性
例如,要从发票中返回产品集合,请使用
$products $invoice->products; // Return an instance of `Illuminate\Support\Collection`.
要过滤价值大于20的产品,请使用
$products = $invoice->products->filter(function ($product, $key){ return $product->vProd > 20; });
有关所有可用的 Illuminate\Support\Collection 方法,请参阅 Illuminate 集合文档。
序列化
所有域对象都实现了 Illuminate\Contracts\Support\Arrayable 和 Illuminate\Contracts\Support\Arrayable\Jsonable。
因此,您可以轻松地转换所有NfeEasy域对象
$invoice => Invoice::create([ // attributes data ]); $invoice->toArray(); // converts the domains objects into arrays $invoice->toJson(); // converts the domains objects into a json string (string)$invoice; // converts the domains objects into a json string
测试
NfeEasy使用PHPUnit单元测试,以实现更高的可靠性和安全性。
要运行所有测试,只需进入项目文件夹并输入
$ phpunit
##许可证
NfeEasy是开源软件,受MIT许可证许可。