diff --git a/README.md b/README.md index 7f8dc24..72fc7c8 100644 --- a/README.md +++ b/README.md @@ -127,24 +127,24 @@ $formats->get('decimal')->format(1000.1235); // 1 000,124 $formats->get('currency')->format(1000.1235); // 1 000,12 Kč ``` -## Tax +## Vat ```php use h4kuna\Format\Number; -$tax = new Number\Tax(20); -echo $tax->add(100); // 120 -echo $tax->deduct(120); // 100.0 +$tax = new Number\Vat(20); +echo $tax->with(100); // 120 +echo $tax->without(120); // 100.0 echo $tax->diff(120); // 20.0 ``` -## Percent +## Discount ```php use h4kuna\Format\Number; -$percent = new Number\Percent(20); -echo $percent->add(100); // 120.0 +$percent = new Number\Discount(20); +echo $percent->with(100); // 120.0 echo $percent->deduct(120); // 96.0 echo $percent->diff(120); // 24.0 ``` diff --git a/changelog.md b/changelog.md index 9777a79..fcf4878 100644 --- a/changelog.md +++ b/changelog.md @@ -12,9 +12,9 @@ back compatibility with v5 use h4kuna; class_alias(h4kuna\Format\Number\Formats::class, 'h4kuna\Number\Utils\Formats'); class_alias(h4kuna\Format\Number\NumberFormat::class, 'h4kuna\Number\Format'); -class_alias(h4kuna\Format\Number\Percent::class, 'h4kuna\Number\Percent'); +class_alias(h4kuna\Format\Number\Discount::class, 'h4kuna\Number\Percent'); class_alias(h4kuna\Format\Number\Round::class, 'h4kuna\Number\Utils\Round'); -class_alias(h4kuna\Format\Number\Tax::class, 'h4kuna\Number\Tax'); +class_alias(h4kuna\Format\Number\Vat::class, 'h4kuna\Number\Tax'); class_alias(h4kuna\Format\Number\UnitValue::class, 'h4kuna\Number\Utils\UnitValue'); // parameters diff --git a/src/Number/Discount.php b/src/Number/Discount.php new file mode 100644 index 0000000..6a411f6 --- /dev/null +++ b/src/Number/Discount.php @@ -0,0 +1,30 @@ +percentage, $number); + } + + + public function deduct(float $number): float + { + return Utils\Percentage::deduct($this->percentage, $number); + } + + + public function diffWith(float $number): float + { + return Utils\Percentage::diffWith($this->ratio, $number); + } + +} diff --git a/src/Number/Percent.php b/src/Number/Percent.php deleted file mode 100644 index 0fa2cb9..0000000 --- a/src/Number/Percent.php +++ /dev/null @@ -1,58 +0,0 @@ -ratio = ($percent / 100) + 1; - } - - - /** - * @example 19.5% = float 19.5 - */ - public function getPercent(): float - { - return $this->percent; - } - - - public function add(float $number): float - { - return $this->getRatio() * $number; - } - - - /** - * @example 19.5% = float 1.195 - */ - public function getRatio(): float - { - return $this->ratio; - } - - - public function deduct(float $number): float - { - return $number - $this->diff($number); - } - - - public function diff(float $number): float - { - return ($number / 100) * $this->percent; - } - - - public function __toString() - { - return (string) $this->percent; - } - -} diff --git a/src/Number/Percentage.php b/src/Number/Percentage.php new file mode 100644 index 0000000..3ef0990 --- /dev/null +++ b/src/Number/Percentage.php @@ -0,0 +1,35 @@ +smallRatio = Utils\Percentage::smallRatio($this->percentage); + $this->ratio = Utils\Percentage::ratio($this->smallRatio); + } + + + public function with(float $number): float + { + return Utils\Percentage::with($this->ratio, $number); + } + + + abstract public function diff(float $number): float; + + + public function __toString() + { + return (string) $this->percentage; + } + +} diff --git a/src/Number/Tax.php b/src/Number/Tax.php deleted file mode 100644 index 5aecdfb..0000000 --- a/src/Number/Tax.php +++ /dev/null @@ -1,48 +0,0 @@ -vat = $vat; - } - - - public function getVat(): float - { - return $this->vat->getPercent(); - } - - - public function add(float $number): float - { - return $this->vat->add($number); - } - - - public function diff(float $number): float - { - return $number - $this->deduct($number); - } - - - public function deduct(float $number): float - { - return $number / $this->vat->getRatio(); - } - - - public function __toString() - { - return (string) $this->vat; - } - -} diff --git a/src/Number/Vat.php b/src/Number/Vat.php new file mode 100644 index 0000000..a31b703 --- /dev/null +++ b/src/Number/Vat.php @@ -0,0 +1,24 @@ +ratio, $number); + } + + + public function without(float $number): float + { + return Utils\Percentage::without($this->ratio, $number); + } + +} diff --git a/src/Utils/Percentage.php b/src/Utils/Percentage.php new file mode 100644 index 0000000..8e56603 --- /dev/null +++ b/src/Utils/Percentage.php @@ -0,0 +1,81 @@ + 17.5 + */ +final class Percentage +{ + /** + * 17.5% -> 0.175 + */ + public static function smallRatio(float $percentage): float + { + return $percentage / 100; + } + + + /** + * 17.5% -> 1.175 + */ + public static function ratio(float $smallRatio): float + { + return $smallRatio + 1.0; + } + + + /** + * N + 17.5% -> N * 1.175 + */ + public static function with(float $ratio, float $number): float + { + return $number * $ratio; + } + + + /** + * N * 1.175 - N + */ + public static function diffWith(float $ratio, float $number): float + { + return self::with($ratio, $number) - $number; + } + + + /** + * If you have price with percentage, and you need price without percentage. + * N / 1.175 + */ + public static function without(float $ratio, float $number): float + { + return $number / $ratio; + } + + + /** + * N - (N / 1.175) + */ + public static function diffWithout(float $ratio, float $number): float + { + return $number - self::without($ratio, $number); + } + + + /** + * N - ((N * 17.5) / 100) + */ + public static function deduct(float $percentage, float $number): float + { + return $number - self::diffDeduct($percentage, $number); + } + + + /** + * (N * 17.5) / 100 + */ + public static function diffDeduct(float $percentage, float $number): float + { + return self::smallRatio($number * $percentage); + } +} diff --git a/tests/src/Number/DiscountTest.php b/tests/src/Number/DiscountTest.php new file mode 100644 index 0000000..2c4b277 --- /dev/null +++ b/tests/src/Number/DiscountTest.php @@ -0,0 +1,32 @@ +with(100)); + Assert::same(96.0, $discount->deduct(120)); + Assert::same(24.0, $discount->diff(120)); + Assert::same(20.0, $discount->diffWith(100)); + Assert::same(20.0, $discount->percentage); + Assert::same(1.20, $discount->ratio); + Assert::same(0.20, $discount->smallRatio); + Assert::same('20', (string) $discount); + } + +} + +(new DiscountTest)->run(); diff --git a/tests/src/Number/PercentTest.php b/tests/src/Number/PercentTest.php deleted file mode 100644 index faece25..0000000 --- a/tests/src/Number/PercentTest.php +++ /dev/null @@ -1,40 +0,0 @@ -add(100)); - } - - - public function testDeduct(): void - { - $percent = new Percent(20); - Assert::same(96.0, $percent->deduct(120)); - } - - - public function testDiff(): void - { - $percent = new Percent(20); - Assert::same(24.0, $percent->diff(120)); - Assert::same(20.0, $percent->getPercent()); - } - -} - -(new PercentTest)->run(); diff --git a/tests/src/Number/TaxTest.php b/tests/src/Number/TaxTest.php deleted file mode 100644 index 533ca3d..0000000 --- a/tests/src/Number/TaxTest.php +++ /dev/null @@ -1,31 +0,0 @@ -add(100)); - Assert::same(100.0, $tax->deduct(120)); - Assert::same(20.0, $tax->diff(120)); - Assert::same(20.0, $tax->getVat()); - - Assert::same('20', (string) new Tax(new Percent(20))); - } - -} - -(new TaxTest)->run(); diff --git a/tests/src/Number/VatTest.php b/tests/src/Number/VatTest.php new file mode 100644 index 0000000..d4e6708 --- /dev/null +++ b/tests/src/Number/VatTest.php @@ -0,0 +1,31 @@ +with(100)); + Assert::same(100.0, $vat->without(120)); + Assert::same(20.0, $vat->diff(120)); + Assert::same(20.0, $vat->percentage); + Assert::same(1.20, $vat->ratio); + Assert::same(0.20, $vat->smallRatio); + Assert::same('20', (string) $vat); + } + +} + +(new VatTest)->run();