diff --git a/README.md b/README.md index f18b3cc..8bf42db 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # Very short description of the package -[![Latest Version on Packagist](https://img.shields.io/packagist/v/feimx/Tax.svg?style=flat-square)](https://packagist.org/packages/feimx/Tax) -[![Build Status](https://img.shields.io/travis/feimx/Tax/master.svg?style=flat-square)](https://travis-ci.org/feimx/Tax) -[![Quality Score](https://img.shields.io/scrutinizer/g/feimx/Tax.svg?style=flat-square)](https://scrutinizer-ci.com/g/feimx/Tax) -[![Total Downloads](https://img.shields.io/packagist/dt/feimx/Tax.svg?style=flat-square)](https://packagist.org/packages/feimx/Tax) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/feimx/tax.svg?style=flat-square)](https://packagist.org/packages/feimx/tax) +[![Build Status](https://img.shields.io/travis/feimx/tax/master.svg?style=flat-square)](https://travis-ci.org/feimx/tax) +[![Quality Score](https://img.shields.io/scrutinizer/g/feimx/tax.svg?style=flat-square)](https://scrutinizer-ci.com/g/feimx/tax) +[![Total Downloads](https://img.shields.io/packagist/dt/feimx/tax.svg?style=flat-square)](https://packagist.org/packages/feimx/tax) This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors. @@ -12,7 +12,7 @@ This is where your description should go. Try and limit it to a paragraph or two You can install the package via composer: ```bash -composer require feimx/Tax +composer require feimx/tax ``` ## Usage diff --git a/src/Contracts/TaxContract.php b/src/Contracts/TaxContract.php index 6228853..29f840e 100644 --- a/src/Contracts/TaxContract.php +++ b/src/Contracts/TaxContract.php @@ -11,5 +11,5 @@ interface TaxContract * * @return float Percetage */ - public function calculate($amount): float; + public function calculate($amount); } diff --git a/src/TaxManager.php b/src/TaxManager.php index d170297..637f9e8 100755 --- a/src/TaxManager.php +++ b/src/TaxManager.php @@ -40,7 +40,7 @@ public function addTax($tax, $retention = false) $tax = new $className($retention); } - if (! in_array($tax, $this->taxes)) { + if (!in_array($tax, $this->taxes)) { $this->taxes[] = $tax; } @@ -68,7 +68,7 @@ public function addTaxes(...$taxes) * * @return float Total amount */ - public function get() + public function total() { $total = 0; foreach ($this->taxes as $tax) { @@ -78,6 +78,28 @@ public function get() return number_format($this->amount + $total, 6, '.', ''); } + /** + * Get a list of taxes with amount calculated. + * + * @return float Total amount + */ + public function get() + { + $taxes = array_map(function ($tax) { + return [ + 'tax' => $tax->name, + 'amount' => $tax->calculate($this->amount), + ]; + }, $this->taxes); + + + return array_merge([ + 'amount' => $this->amount, + 'total' => $this->total, + 'taxes' => $taxes, + ]); + } + /** * @param string $tax * @@ -86,10 +108,23 @@ public function get() public function stringToClassName($tax) { $className = 'FeiMx\\Tax\\Taxes\\'.strtoupper($tax); - if (! class_exists($className)) { + if (!class_exists($className)) { throw new TaxErrorException("The tax '{$tax}' is not valid"); } return $className; } + + public function __get($property) + { + if ('total' == $property) { + return $this->total(); + } + + if (property_exists($this, $property)) { + return $this->{$property}; + } + + throw new \Exception('Property not exists'); + } } diff --git a/src/Taxes/IEPS.php b/src/Taxes/IEPS.php index 09b8739..9704a05 100644 --- a/src/Taxes/IEPS.php +++ b/src/Taxes/IEPS.php @@ -13,8 +13,8 @@ class IEPS extends Tax implements TaxContract * * @return float Percetage */ - public function calculate($amount): float + public function calculate($amount) { - return $amount * $this->percentage(); + return number_format($amount * $this->percentage(), 6, '.', ''); } } diff --git a/src/Taxes/ISR.php b/src/Taxes/ISR.php index 27cc328..7ab2e4a 100644 --- a/src/Taxes/ISR.php +++ b/src/Taxes/ISR.php @@ -13,9 +13,9 @@ class ISR extends Tax implements TaxContract * * @return float Percetage */ - public function calculate($amount): float + public function calculate($amount) { // return -(($amount * 0.16) / 3) * 2; - return $amount * $this->percentage(); + return number_format($amount * $this->percentage(), 6, '.', ''); } } diff --git a/src/Taxes/IVA.php b/src/Taxes/IVA.php index 9eab587..057308c 100644 --- a/src/Taxes/IVA.php +++ b/src/Taxes/IVA.php @@ -9,12 +9,12 @@ class IVA extends Tax implements TaxContract /** * Calculate tax percentage of a given amount. * - * @param int $amount Amount for aclculate + * @param int $amount Amount for calculated * * @return float Percetage */ - public function calculate($amount): float + public function calculate($amount) { - return $amount * $this->percentage(); + return number_format($amount * $this->percentage(), 6, '.', ''); } } diff --git a/src/Taxes/Tax.php b/src/Taxes/Tax.php index cb5c5e8..c75fe6b 100644 --- a/src/Taxes/Tax.php +++ b/src/Taxes/Tax.php @@ -60,4 +60,13 @@ protected function parseTypePercentage($type = 'default') { return $this->retention ? 'retention' : $type; } + + public function __get($property) + { + if ('name' == $property) { + return $this->getTaxName(); + } + + throw new \Exception('Property not exists'); + } } diff --git a/tests/TaxManagerTest.php b/tests/TaxManagerTest.php index edc061c..dbe1442 100644 --- a/tests/TaxManagerTest.php +++ b/tests/TaxManagerTest.php @@ -92,15 +92,15 @@ public function testCanCalculateIVA() { $taxManager = new TaxManager(100); $taxManager->addTax('iva'); - $this->assertEquals(116.000000, $taxManager->get()); + $this->assertEquals(116.000000, $taxManager->total()); $taxManager = new TaxManager(5470); $taxManager->addTax('iva'); - $this->assertEquals(6345.200000, $taxManager->get()); + $this->assertEquals(6345.200000, $taxManager->total()); $taxManager = new TaxManager(2300.90); $taxManager->addTax('iva'); - $this->assertEquals(2669.044000, $taxManager->get()); + $this->assertEquals(2669.044000, $taxManager->total()); } public function testCanCalculateIVARetention() @@ -109,30 +109,30 @@ public function testCanCalculateIVARetention() $taxManager = new TaxManager(100); $taxManager->addTax('iva', $retention); - $this->assertEquals(84.000000, $taxManager->get()); + $this->assertEquals(84.000000, $taxManager->total()); $taxManager = new TaxManager(5470); $taxManager->addTax('iva', $retention); - $this->assertEquals(4594.800000, $taxManager->get()); + $this->assertEquals(4594.800000, $taxManager->total()); $taxManager = new TaxManager(2300.90); $taxManager->addTax('iva', $retention); - $this->assertEquals(1932.756000, $taxManager->get()); + $this->assertEquals(1932.756000, $taxManager->total()); } public function testCanCalculateIEPS() { $taxManager = new TaxManager(100); $taxManager->addTax('ieps'); - $this->assertEquals(108.000000, $taxManager->get()); + $this->assertEquals(108.000000, $taxManager->total()); $taxManager = new TaxManager(5470); $taxManager->addTax('ieps'); - $this->assertEquals(5907.600000, $taxManager->get()); + $this->assertEquals(5907.600000, $taxManager->total()); $taxManager = new TaxManager(2300.90); $taxManager->addTax('ieps'); - $this->assertEquals(2484.972000, $taxManager->get()); + $this->assertEquals(2484.972000, $taxManager->total()); } public function testCanCalculateIEPSRetention() @@ -141,30 +141,30 @@ public function testCanCalculateIEPSRetention() $taxManager = new TaxManager(100); $taxManager->addTax('ieps', $retention); - $this->assertEquals(92.000000, $taxManager->get()); + $this->assertEquals(92.000000, $taxManager->total()); $taxManager = new TaxManager(5470); $taxManager->addTax('ieps', $retention); - $this->assertEquals(5032.400000, $taxManager->get()); + $this->assertEquals(5032.400000, $taxManager->total()); $taxManager = new TaxManager(2300.90); $taxManager->addTax('ieps', $retention); - $this->assertEquals(2116.828000, $taxManager->get()); + $this->assertEquals(2116.828000, $taxManager->total()); } public function testCanCalculateISR() { $taxManager = new TaxManager(100); $taxManager->addTax('isr'); - $this->assertEquals(89.333300, $taxManager->get()); + $this->assertEquals(89.333300, $taxManager->total()); $taxManager = new TaxManager(5470); $taxManager->addTax('isr'); - $this->assertEquals(4886.531510, $taxManager->get()); + $this->assertEquals(4886.531510, $taxManager->total()); $taxManager = new TaxManager(2300.90); $taxManager->addTax('isr'); - $this->assertEquals(2055.469900, $taxManager->get()); + $this->assertEquals(2055.469900, $taxManager->total()); } public function testCanCalculateMultipleTaxes() @@ -172,34 +172,75 @@ public function testCanCalculateMultipleTaxes() $taxManager = new TaxManager(100); $taxManager->addTax('iva'); $taxManager->addTax('isr'); - $this->assertEquals(105.333300, $taxManager->get()); + $this->assertEquals(105.333300, $taxManager->total()); $taxManager = new TaxManager(5470); $taxManager->addTax('iva'); $taxManager->addTax('isr'); - $this->assertEquals(5761.731510, $taxManager->get()); + $this->assertEquals(5761.731510, $taxManager->total()); $taxManager = new TaxManager(2300.90); $taxManager->addTax('iva'); $taxManager->addTax('isr'); - $this->assertEquals(2423.613900, $taxManager->get()); + $this->assertEquals(2423.613900, $taxManager->total()); $taxManager = new TaxManager(100); $taxManager->addTax('iva'); $taxManager->addTax('isr'); $taxManager->addTax('ieps'); - $this->assertEquals(113.333300, $taxManager->get()); + $this->assertEquals(113.333300, $taxManager->total()); $taxManager = new TaxManager(5470); $taxManager->addTax('iva'); $taxManager->addTax('ieps'); $taxManager->addTax('isr'); - $this->assertEquals(6199.331510, $taxManager->get()); + $this->assertEquals(6199.331510, $taxManager->total()); $taxManager = new TaxManager(2300.90); $taxManager->addTax('iva'); $taxManager->addTax('isr'); $taxManager->addTax('ieps'); - $this->assertEquals(2607.685900, $taxManager->get()); + $this->assertEquals(2607.685900, $taxManager->total()); + } + + public function testGetTotalByProperty() + { + $taxManager = new TaxManager(100); + $taxManager->addTax('iva'); + $this->assertEquals(116.000000, $taxManager->total); + } + + public function testGetAListOfTaxesCalculated() + { + $taxManager = new TaxManager(100); + $taxManager->addTax('iva'); + $taxManager->addTax('isr'); + $data = [ + 'amount' => 100, + 'total' => '105.333300', + 'taxes' => [ + [ + 'tax' => 'iva', + 'amount' => '16.000000', + ], + [ + 'tax' => 'isr', + 'amount' => '-10.666700', + ], + ], + ]; + $this->assertSame($data, $taxManager->get()); + } + + public function testCombineTraslateAndRetentions() + { + $retention = true; + + $taxManager = new TaxManager(100); + $taxManager->addTax('iva'); + $taxManager->addTax('isr'); + $taxManager->addTax('iva', $retention); + $taxManager->addTax('ieps', $retention); + $this->assertEquals(81.333300, $taxManager->total()); } }