Calculate taxes for a given amount
, (*1)
The feimx/tax
package provide a simple way for calculate taxes of an amount., (*2)
Basic Usage
``` php
$taxManager = new FeiMx\Tax\TaxManager($amount = 100);
$taxManager->addTax('iva');
echo $taxManager->total(); // 116.000000, (*3)
## Installation
You can install the package via composer:
```bash
composer require feimx/tax
// config/app.php
'providers' => [
FeiMx\Tax\TaxServiceProvider::class,
];
Note: If your Laravel versions is >=5.5
you don't need register providers., (*4)
Note: This is not necesary if you are not using Laravel, (*5)
You can optionally publish the config file with:, (*6)
php artisan vendor:publish --provider="FeiMx\Tax\TaxServiceProvider" --tag="config"
This is the contents of the published config file:, (*7)
return [
/**
* Used The fallback type determines the type to use when the current one
* is not available. You may change the value to correspond to any of
* provided types
*/
'fallback' => 'default',
/**
* List of taxes with their types ans percentages
* You can add more types and percentages.
*/
'taxes' => [
'iva' => [
'default' => 0.16,
'retention' => -0.106667,
],
'isr' => [
'default' => -0.106667,
],
'ieps' => [
'default' => 0.08,
'retention' => -0.08,
'primary' => 0.11,
'secondary' => 0.13,
],
],
];
Note: This is not necesary if you are not using Laravel, (*8)
You can optionally publish the migrations file with:, (*9)
php artisan vendor:publish --provider="FeiMx\Tax\TaxServiceProvider" --tag="migrations"
Note: This is not necesary if you are not using Laravel, (*10)
Usage
Firt need create a new instance of TaxManager
:, (*11)
``` php
$taxManager = new FeiMx\Tax\TaxManager($amount = 100);, (*12)
Second you need to add the taxes for calculate the final amount:
The first parameter could be a tax name `['iva', 'ieps', 'isr']` or an instance of `FeiMx\Tax\Contracts\TaxContract`.
Exist 3 Tax Objects:
``` php
$iva = new \FeiMx\Tax\Taxes\IVA($retention = false);
$isr = new \FeiMx\Tax\Taxes\ISR($retention = false);
$ieps = new \FeiMx\Tax\Taxes\IEPS($retention = false);
$taxManager->addTax($tax = 'iva', $retention = false);
$taxManager->addTax($iva);
Note: You can pass a string for a tax type of given config file instead the retention boolean param., (*13)
``` php
$iva = new \FeiMx\Tax\Taxes\IVA('free');
$taxManager->addTax($tax = 'iva', 'free');, (*14)
You can add multiple taxes at once:
``` php
$taxManager->addTaxes([
'iva', $isr, $ieps,
]);
Now you can get the final amount:, (*15)
``` php
$taxManager->total();
// or
$taxManager->total;, (*16)
You can get a list for given data:
``` php
$taxManager->get();
This is the contents of get method:, (*17)
``` php
[
'amount' => 100,
'total' => '105.333300',
'taxes' => [
[
'tax' => 'iva',
'amount' => '16.000000',
],
[
'tax' => 'isr',
'amount' => '-10.666700',
],
],
];, (*18)
## Models
You can assign Taxable trait to your models.
``` php
use FeiMx\Tax\Traits\Taxable;
class Product extends Model
{
use Taxable;
protected $fillable = ['price'];
}
You can assign tax groups to your model:, (*19)
``` php
$product = Product::first();
$taxGroup = \FeiMx\Tax\Models\TaxGroup::first();, (*20)
$product->assignTaxGroup($taxGroup);, (*21)
You can pass a name and multiples tax groups:
``` php
$product->assignTaxGroup('iva');
$product->assignTaxGroup('iva', 'isr');
$product->assignTaxGroup(['iva', 'isr']);
$product->assignTaxGroup(collect(['iva', $taxGroup]));
You can sync too:, (*22)
``` php
$product->syncTaxGroups('iva');
$product->syncTaxGroups('iva', 'isr');
$product->syncTaxGroups(['iva', 'isr']);, (*23)
And you can remove:
``` php
$product->removeTaxGroup('iva');
$product->removeTaxGroup($taxGroup);
You can verify if a model has a TaxGroup:, (*24)
``` php
$product->hasTaxGroup('iva');
$product->hasTaxGroup($taxGroup);
$product->hasTaxGroup([$taxGroup, 'iva']);, (*25)
For get the total amount after taxes, must need to know what column use, for defaul we use `price` column,
but you can use another one:
``` php
class Product extends Model
{
use Taxable;
protected $fillable = ['price'];
public static function priceColumn()
{
return 'price';
}
}
Now you can get the total of a given TaxGroup:, (*26)
``` php
$product->total($taxGroup);, (*27)
And you can obtain the content of the get method of the TaxManager:
``` php
$product->getAmounts($taxGroup);
Testing
bash
composer test
, (*28)
Changelog
Please see CHANGELOG for more information what has changed recently., (*29)
Contributing
Please see CONTRIBUTING for details., (*30)
Security
If you discover any security related issues, please email yorch@fei.com.mx instead of using the issue tracker., (*31)
Credits
Support us
FEI is a Digital Invoicing startup based in Yucatán, México. You'll find an overview of all our open source projects on our website., (*32)
Does your business depend on our contributions? Reach out and support us on Patreon.
All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff., (*33)
License
The MIT License (MIT). Please see License File for more information., (*34)