Skip to content

Commit

Permalink
add new free type and code for calculate percentage ypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorchi committed Apr 2, 2018
1 parent f1eb834 commit ac79537
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
preset: laravel
preset: recommended

disabled:
- single_class_element_per_statement
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `Tax` will be documented in this file

## 1.1.2 - 2018-04-02

- Change mode for getting percentage
- Added free tax type for IVA and IEPS

## 1.1.1 - 2018-03-19

- Fix error in migrations, removed index for missed columns
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ $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.

``` php
$iva = new \FeiMx\Tax\Taxes\IVA('free');
$taxManager->addTax($tax = 'iva', 'free');
```

You can add multiple taxes at once:

Expand Down
2 changes: 2 additions & 0 deletions config/tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'iva' => [
'default' => 0.16,
'retention' => -0.106667,
'free' => 0,
],
'isr' => [
'default' => -0.106667,
Expand All @@ -24,6 +25,7 @@
'retention' => -0.08,
'primary' => 0.11,
'secondary' => 0.13,
'free' => 0,
],
],
];
2 changes: 1 addition & 1 deletion src/Models/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function info(): TaxContract
{
$className = '\\FeiMx\\Tax\\Taxes\\'.strtoupper($this->name);

return new $className($this->retention);
return new $className($this->retention ? $this->retention : $this->type);
}

public function getInfoAttribute(): TaxContract
Expand Down
2 changes: 1 addition & 1 deletion src/TaxManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct($amount = 0)

/**
* @param string|FeiMx\Tax\Contracts\TaxContract $tax
* @param bool $retention
* @param bool|string $retention
*
* @return mixed
*/
Expand Down
12 changes: 6 additions & 6 deletions src/Taxes/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Tax
/**
* Create new instance of taxes.
*
* @param bool $retention
* @param bool|string $retention
*/
public function __construct(bool $retention = false)
public function __construct($retention = false)
{
$this->retention = $retention;
}
Expand All @@ -23,11 +23,11 @@ public function __construct(bool $retention = false)
*
* @return int $percentage Amount of the tax
*/
public function percentage($type = 'default'): float
public function percentage(): float
{
$taxName = $this->getTaxName();

$type = $this->parseTypePercentage($type);
$type = $this->parseTypePercentage();

return $percentage = config(
"tax.taxes.{$taxName}.{$type}",
Expand Down Expand Up @@ -56,9 +56,9 @@ protected function getTaxName(): string
*
* @return string Type of the tax amount
*/
protected function parseTypePercentage($type = 'default')
protected function parseTypePercentage()
{
return $this->retention ? 'retention' : $type;
return is_bool($this->retention) && $this->retention ? 'retention' : $this->retention;
}

public function __get($property)
Expand Down
9 changes: 9 additions & 0 deletions tests/TaxManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,13 @@ public function testCombineTraslateAndRetentions()
$taxManager->addTax('ieps', $retention);
$this->assertEquals(86.6666, $taxManager->total());
}

public function testCanGetFreeTaxes()
{
$retention = true;

$taxManager = new TaxManager(100);
$taxManager->addTax('iva', 'free');
$this->assertEquals(100.0000, $taxManager->total());
}
}
20 changes: 10 additions & 10 deletions tests/TaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ public function testCanGetPercentageByType()
{
$retention = true;

$iva = new IVA();
$this->assertEquals(0.16, $iva->percentage());
$this->assertEquals(-0.106667, $iva->percentage('retention'));
$this->assertEquals(0.16, $iva->percentage('unexist'));
$this->assertEquals(0.16, (new IVA())->percentage());
$this->assertEquals(-0.106667, (new IVA('retention'))->percentage());
$this->assertEquals(0.16, (new IVA('unexist'))->percentage());
$this->assertEquals(0, (new IVA('free'))->percentage());

$iva = new IVA($retention);
$this->assertEquals(-0.106667, $iva->percentage());

$isr = new ISR();
$this->assertEquals(-0.106667, $isr->percentage());

$ieps = new IEPS();
$this->assertEquals(0.08, $ieps->percentage());
$this->assertEquals(0.08, $ieps->percentage('unexist'));
$this->assertEquals(-0.08, $ieps->percentage('retention'));
$this->assertEquals(0.11, $ieps->percentage('primary'));
$this->assertEquals(0.13, $ieps->percentage('secondary'));
$this->assertEquals(0.08, (new IEPS())->percentage());
$this->assertEquals(0.08, (new IEPS('unexist'))->percentage());
$this->assertEquals(-0.08, (new IEPS('retention'))->percentage());
$this->assertEquals(0.11, (new IEPS('primary'))->percentage());
$this->assertEquals(0.13, (new IEPS('secondary'))->percentage());
$this->assertEquals(0, (new IEPS('free'))->percentage());

$ieps = new IEPS($retention);
$this->assertEquals(-0.08, $ieps->percentage());
Expand Down
20 changes: 20 additions & 0 deletions tests/TaxableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FeiMx\Tax\Tests;

use FeiMx\Tax\Models\Tax;
use FeiMx\Tax\Models\TaxGroup;
use FeiMx\Tax\Exceptions\TaxErrorException;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -145,4 +146,23 @@ public function testCanGetAListOfCalculatedAmounts()

$this->assertSame($data, $this->product->getAmounts($this->taxGroup));
}

public function testCanGetAListOfCalculatedAmountsOfFreeTaxes()
{
$taxGroup = TaxGroup::find(5);
$taxGroup->addTax(Tax::find(6));
$this->product->assignTaxGroup($taxGroup);
$data = [
'amount' => '2300.9',
'total' => '2300.900000',
'taxes' => [
[
'tax' => 'iva',
'amount' => '0.000000',
],
],
];

$this->assertSame($data, $this->product->getAmounts($taxGroup));
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function createTaxGroups()
TaxGroup::create(['name' => 'iva and ieps']);
TaxGroup::create(['name' => 'ieps', 'active' => false]);
TaxGroup::create(['name' => 'iva ret and isr']);
TaxGroup::create(['name' => 'free iva']);
}

public function createTaxes()
Expand All @@ -84,6 +85,7 @@ public function createTaxes()
Tax::create(['name' => 'isr']);
Tax::create(['name' => 'ieps']);
Tax::create(['name' => 'ieps', 'retention' => true]);
Tax::create(['name' => 'iva', 'type' => 'free']);
}

public function createProducts()
Expand Down

0 comments on commit ac79537

Please sign in to comment.