nfe-easy / nfe-easy
Easy package to manage NFe resources
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-12-05 10:59:11 UTC
README
Introduction
NFeEasy is a wrapper to manage NFe files.
Requirements
- PHP 7.0.13 or later
Instalation
Composer
You and install via composer:
$ composer require nfe-easy/nfe-easy
and use composer autoload:
require_once('vendor/autoload.php');
Manual Instalation
If you don't want to use Composer, download the latest version of NefEasy and include the init.php
file:
require_once('/path/to/nfeasy/init.php');
You will also need to dowload the NfeEasy php dependencies and autoload then
manually. See composer.json
to see NfeEasy dependencies.
How to use
To use the NfeEasy, just pass the xml nfe file content to the XmlInvoiceBuilder and it will returns the PHP objects representing the given NFe.
XmlInvoiceBuilder::::create( file_get_contents('path/to//my/invoice.xml') ); // returns an instance of NFeEasy\Invoice
Domain Objects
Each Domain from NFe file is represented by an NFeEasy object class. These classes are called Domain Object Classes.
The list of Domains Object Classes includes:
NFeEasy\Invoice
NFeEasy\Product
NFeEasy\Emitter
NFeEasy\Receiver
NFeEasy\Address
A full NFe
file is represented by a relationship between one or more NFeEasy Domain Objects Classes.
The NFeEasy Domain Object Classes contain the data extracted from the NFe file. The data is diviled between the NfeEasy Domain Object Classes.
For a full NfeEasy reprentation, use the
NFeEasy\Builder\XmlInvoiceBuilder::create
method.
It will return an NFeEasy\Invoice
instance.
Invoice
The NFeEasy\Invoice
represents the NFe file itself, and contains
data to indentity the NFe.
All other Domain Object Classes resides below Invoice
Object.
The table bellow describes all attributes you can access from
NFeEasy\Invoice
class:
Product
NFeEasy\Product
represents an Invoice`s product.
It contains informations like the name of the product, the NCM, the value, the quantity the taxes, and other product related things.
The table bellow describes all attributes you can access from
NFeEasy\Product
class:
Emitter
NFeEasy\Emitter
represents the NFe emitter
The table bellow describes all attributes you can access from
NFeEasy\Product
class:
Receiver
NFeEasy\Receiver
represents the NFe Receiver
The table bellow describes all attributes you can access from
NFeEasy\Product
class:
Address
NFeEasy\Address
represents an address object
The table bellow describes all attributes you can access from
NFeEasy\Address
class:
Creating Domain Objects
You can create Domain Object classes individually. Just use
the method created
from the respective object
passing the parameters you wnat to populate into the object:
$address = Address::create([ 'xLgr' => 'Some address street', 'nro' => '207' ]); // returns an NFeEasy\Address
If you dont't want to create the domain objects individually, you can
pass all data directy to the 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
Attributes
After a Domain Object is created, you can access the data by access attributes by its names:
$product = Product::create([ 'cProd' => '12' 'xProd' => 'Notebook' ]); echo $product->xProd; // prints 'Shampoo'
You can access child objects by the attributes names too:
$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é'
Collections
Array of Domain Objects are treated as Collections into NfeEasy. All Object Collections are represented by an instance of Illuminate\Support\Collection
.
Collections in NfeEasy are used for:
products
attribute intoInvoice
Object
For example, to return the collection of products from an Invoice, use:
$products $invoice->products; // Return an instance of `Illuminate\Support\Collection`.
To filter products with value greater than 20, use:
$products = $invoice->products->filter(function ($product, $key){ return $product->vProd > 20; });
For all Illuminate\Support\Collection
available methods, see Illuminate Collection Docs.
Serialization
All Domain objects implements Illuminate\Contracts\Support\Arrayable
and Illuminate\Contracts\Support\Arrayable\Jsonable
.
So you can transform the all NfeEasy Domain objects easily:
$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
Tests
NfeEasy uses PHPUnit unit tests for better reliablility and security.
To run all tests, justs go to the project folder and type:
$ phpunit
##License
NfeEasy is open-sourced software licensed under the MIT license.