Skip to content

Commit ac79537

Browse files
committed
add new free type and code for calculate percentage ypes
1 parent f1eb834 commit ac79537

File tree

11 files changed

+63
-19
lines changed

11 files changed

+63
-19
lines changed

.styleci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
preset: laravel
1+
preset: recommended
22

33
disabled:
44
- single_class_element_per_statement

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

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

5+
## 1.1.2 - 2018-04-02
6+
7+
- Change mode for getting percentage
8+
- Added free tax type for IVA and IEPS
9+
510
## 1.1.1 - 2018-03-19
611

712
- Fix error in migrations, removed index for missed columns

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ $ieps = new \FeiMx\Tax\Taxes\IEPS($retention = false);
100100
$taxManager->addTax($tax = 'iva', $retention = false);
101101
$taxManager->addTax($iva);
102102
```
103+
_Note:_ You can pass a string for a tax type of given config file instead the retention boolean param.
104+
105+
``` php
106+
$iva = new \FeiMx\Tax\Taxes\IVA('free');
107+
$taxManager->addTax($tax = 'iva', 'free');
108+
```
103109

104110
You can add multiple taxes at once:
105111

config/tax.php

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'iva' => [
1616
'default' => 0.16,
1717
'retention' => -0.106667,
18+
'free' => 0,
1819
],
1920
'isr' => [
2021
'default' => -0.106667,
@@ -24,6 +25,7 @@
2425
'retention' => -0.08,
2526
'primary' => 0.11,
2627
'secondary' => 0.13,
28+
'free' => 0,
2729
],
2830
],
2931
];

src/Models/Tax.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function info(): TaxContract
2020
{
2121
$className = '\\FeiMx\\Tax\\Taxes\\'.strtoupper($this->name);
2222

23-
return new $className($this->retention);
23+
return new $className($this->retention ? $this->retention : $this->type);
2424
}
2525

2626
public function getInfoAttribute(): TaxContract

src/TaxManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct($amount = 0)
2929

3030
/**
3131
* @param string|FeiMx\Tax\Contracts\TaxContract $tax
32-
* @param bool $retention
32+
* @param bool|string $retention
3333
*
3434
* @return mixed
3535
*/

src/Taxes/Tax.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Tax
99
/**
1010
* Create new instance of taxes.
1111
*
12-
* @param bool $retention
12+
* @param bool|string $retention
1313
*/
14-
public function __construct(bool $retention = false)
14+
public function __construct($retention = false)
1515
{
1616
$this->retention = $retention;
1717
}
@@ -23,11 +23,11 @@ public function __construct(bool $retention = false)
2323
*
2424
* @return int $percentage Amount of the tax
2525
*/
26-
public function percentage($type = 'default'): float
26+
public function percentage(): float
2727
{
2828
$taxName = $this->getTaxName();
2929

30-
$type = $this->parseTypePercentage($type);
30+
$type = $this->parseTypePercentage();
3131

3232
return $percentage = config(
3333
"tax.taxes.{$taxName}.{$type}",
@@ -56,9 +56,9 @@ protected function getTaxName(): string
5656
*
5757
* @return string Type of the tax amount
5858
*/
59-
protected function parseTypePercentage($type = 'default')
59+
protected function parseTypePercentage()
6060
{
61-
return $this->retention ? 'retention' : $type;
61+
return is_bool($this->retention) && $this->retention ? 'retention' : $this->retention;
6262
}
6363

6464
public function __get($property)

tests/TaxManagerTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,13 @@ public function testCombineTraslateAndRetentions()
243243
$taxManager->addTax('ieps', $retention);
244244
$this->assertEquals(86.6666, $taxManager->total());
245245
}
246+
247+
public function testCanGetFreeTaxes()
248+
{
249+
$retention = true;
250+
251+
$taxManager = new TaxManager(100);
252+
$taxManager->addTax('iva', 'free');
253+
$this->assertEquals(100.0000, $taxManager->total());
254+
}
246255
}

tests/TaxTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ public function testCanGetPercentageByType()
4747
{
4848
$retention = true;
4949

50-
$iva = new IVA();
51-
$this->assertEquals(0.16, $iva->percentage());
52-
$this->assertEquals(-0.106667, $iva->percentage('retention'));
53-
$this->assertEquals(0.16, $iva->percentage('unexist'));
50+
$this->assertEquals(0.16, (new IVA())->percentage());
51+
$this->assertEquals(-0.106667, (new IVA('retention'))->percentage());
52+
$this->assertEquals(0.16, (new IVA('unexist'))->percentage());
53+
$this->assertEquals(0, (new IVA('free'))->percentage());
5454

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

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

61-
$ieps = new IEPS();
62-
$this->assertEquals(0.08, $ieps->percentage());
63-
$this->assertEquals(0.08, $ieps->percentage('unexist'));
64-
$this->assertEquals(-0.08, $ieps->percentage('retention'));
65-
$this->assertEquals(0.11, $ieps->percentage('primary'));
66-
$this->assertEquals(0.13, $ieps->percentage('secondary'));
61+
$this->assertEquals(0.08, (new IEPS())->percentage());
62+
$this->assertEquals(0.08, (new IEPS('unexist'))->percentage());
63+
$this->assertEquals(-0.08, (new IEPS('retention'))->percentage());
64+
$this->assertEquals(0.11, (new IEPS('primary'))->percentage());
65+
$this->assertEquals(0.13, (new IEPS('secondary'))->percentage());
66+
$this->assertEquals(0, (new IEPS('free'))->percentage());
6767

6868
$ieps = new IEPS($retention);
6969
$this->assertEquals(-0.08, $ieps->percentage());

tests/TaxableTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FeiMx\Tax\Tests;
44

5+
use FeiMx\Tax\Models\Tax;
56
use FeiMx\Tax\Models\TaxGroup;
67
use FeiMx\Tax\Exceptions\TaxErrorException;
78
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -145,4 +146,23 @@ public function testCanGetAListOfCalculatedAmounts()
145146

146147
$this->assertSame($data, $this->product->getAmounts($this->taxGroup));
147148
}
149+
150+
public function testCanGetAListOfCalculatedAmountsOfFreeTaxes()
151+
{
152+
$taxGroup = TaxGroup::find(5);
153+
$taxGroup->addTax(Tax::find(6));
154+
$this->product->assignTaxGroup($taxGroup);
155+
$data = [
156+
'amount' => '2300.9',
157+
'total' => '2300.900000',
158+
'taxes' => [
159+
[
160+
'tax' => 'iva',
161+
'amount' => '0.000000',
162+
],
163+
],
164+
];
165+
166+
$this->assertSame($data, $this->product->getAmounts($taxGroup));
167+
}
148168
}

tests/TestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function createTaxGroups()
7575
TaxGroup::create(['name' => 'iva and ieps']);
7676
TaxGroup::create(['name' => 'ieps', 'active' => false]);
7777
TaxGroup::create(['name' => 'iva ret and isr']);
78+
TaxGroup::create(['name' => 'free iva']);
7879
}
7980

8081
public function createTaxes()
@@ -84,6 +85,7 @@ public function createTaxes()
8485
Tax::create(['name' => 'isr']);
8586
Tax::create(['name' => 'ieps']);
8687
Tax::create(['name' => 'ieps', 'retention' => true]);
88+
Tax::create(['name' => 'iva', 'type' => 'free']);
8789
}
8890

8991
public function createProducts()

0 commit comments

Comments
 (0)