Skip to content

Commit

Permalink
Merge pull request #11 from Dropelikeit/WorkoverPricecalculatorV2-1
Browse files Browse the repository at this point in the history
Workover pricecalculator v2 1
  • Loading branch information
Dropelikeit authored Apr 28, 2018
2 parents d8354fe + dbcfdcf commit 61fbf0a
Show file tree
Hide file tree
Showing 20 changed files with 397 additions and 61 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": ">=7.1",
"php": ">=7.2",
"ext-bcmath": "*"
},
"require-dev": {
Expand Down
136 changes: 135 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,141 @@ You are also welcome to use the Issue Tracker to set bugs, improvements or upgra

``` composer require marcel-strahl/price-calculator ```

### Example
### How to use

If you want to use our price calculator, you can instantiate an instance of the price calculator via the price calculator facade from version 2.1.

```php
<?php
use MarcelStrahl\PriceCalculator\Facade\PriceCalculator;

$vat = 19;
$priceCalculator = PriceCalculator::getPriceCalculator($vat);
```

In versions older than 2.1, you must instantiate the price calculator itself and add the dependencies.

```php
<?php

use MarcelStrahl\PriceCalculator\PriceCalculator;
use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;

$vat = new Vat();
$vat->setVat(19);
$priceCalculator = new PriceCalculator($vat);
```

The price calculator can add prices

```php
<?php

use MarcelStrahl\PriceCalculator\PriceCalculator;
use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;

$total = 0;
$add = 100;

$vat = new Vat();
$vat->setVat(19);
$priceCalculator = new PriceCalculator($vat);

// Total: 100
$total = $priceCalculator->addPrice($total, $add);
```

Subtracting prices is made possible by the following method:

```php
<?php

use MarcelStrahl\PriceCalculator\PriceCalculator;
use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;

$total = 200;
$sub = 100;

$vat = new Vat();
$vat->setVat(19);
$priceCalculator = new PriceCalculator($vat);

// Total: 100
$total = $priceCalculator->subPrice($total, $sub);
```

You can also multiply prices by a number (integer).

```php
<?php

use MarcelStrahl\PriceCalculator\PriceCalculator;
use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;

$price = 200;
$amount = 2;

$vat = new Vat();
$vat->setVat(19);
$priceCalculator = new PriceCalculator($vat);

// Total: 100
$total = $priceCalculator->mulPrice($amount, 200);
```

With the following code you can calculate the VAT:

```php
<?php

use MarcelStrahl\PriceCalculator\PriceCalculator;
use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;

$total = 200.50;

$vat = new Vat();
$vat->setVat(19);
$priceCalculator = new PriceCalculator($vat);

$vatPrice = $priceCalculator->calculateSalesTaxFromTotalPrice($total);
```

The following example shows how to calculate the net price using the gross price.

```php
<?php

use MarcelStrahl\PriceCalculator\PriceCalculator;
use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;

$total = 200.50;

$vat = new Vat();
$vat->setVat(19);
$priceCalculator = new PriceCalculator($vat);

$vatPrice = $priceCalculator->calculateNetPriceFromGrossPrice($total);
```

Or the gross price is determined using the net price

```php
<?php

use MarcelStrahl\PriceCalculator\PriceCalculator;
use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;

$netPrice = 15.50;

$vat = new Vat();
$vat->setVat(19);
$priceCalculator = new PriceCalculator($vat);

$vatPrice = $priceCalculator->calculatePriceWithSalesTax($netPrice);
```
In another example you can see the functions of the price calculator.
A price is calculated, converted from cents to euros and the total sum is formatted.

```php
<?php
use MarcelStrahl\PriceCalculator\PriceCalculator;
Expand Down
24 changes: 24 additions & 0 deletions src/Exceptions/ConverterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MarcelStrahl\PriceCalculator\Exceptions;

use Exception;

/**
* Class ConverterException
* @author Marcel Strahl <[email protected]>
* @package MarcelStrahl\PriceCalculator\Exceptions
*/
class ConverterException extends Exception
{
/**
* @param float $amout
* @return ConverterException
*/
public static function fromZeroAmount(float $amout): self
{
return new self(
sprintf('Division by zero is not allowed. Please check your injection. Given amount: %f', $amout)
);
}
}
13 changes: 0 additions & 13 deletions src/Exceptions/PriceException.php

This file was deleted.

43 changes: 43 additions & 0 deletions src/Facade/PriceCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MarcelStrahl\PriceCalculator\Facade;

use MarcelStrahl\PriceCalculator\Helpers\Entity\Vat;
use MarcelStrahl\PriceCalculator\PriceCalculator as PriceCalculatorService;

/**
* Class PriceCalculator
* @author Marcel Strahl <[email protected]>
* @package MarcelStrahl\PriceCalculator\Facade
*/
class PriceCalculator
{
/**
* @var Vat
*/
private $vat;

public function __construct()
{
$this->vat = new Vat();
}

/**
* @param int $vat
* @return PriceCalculatorService
*/
public static function getPriceCalculator(int $vat)
{
return (new self())->createPriceCalculator($vat);
}

/**
* @param int $vat
* @return PriceCalculatorService
*/
private function createPriceCalculator(int $vat): PriceCalculatorService
{
$this->vat->setVat($vat);
return new PriceCalculatorService($this->vat);
}
}
38 changes: 38 additions & 0 deletions src/Facade/UnitConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace MarcelStrahl\PriceCalculator\Facade;

use MarcelStrahl\PriceCalculator\UnitConverter as UnitConverterService;
use MarcelStrahl\PriceCalculator\Factory\Converter as ConverterFactory;

/**
* Class UnitConverter
* @author Marcel Strahl <[email protected]>
* @package MarcelStrahl\PriceCalculator\Facade
*/
class UnitConverter
{
/**
* @return UnitConverterService
*/
public static function getConverter(): UnitConverterService
{
return (new self)->createUnitConverter();
}

/**
* @return UnitConverterService
*/
private function createUnitConverter(): UnitConverterService
{
return new UnitConverterService($this->createFactory());
}

/**
* @return ConverterFactory
*/
private function createFactory(): ConverterFactory
{
return new ConverterFactory();
}
}
10 changes: 10 additions & 0 deletions src/Helpers/Converter/ConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MarcelStrahl\PriceCalculator\Helpers\Converter;

use MarcelStrahl\PriceCalculator\Exceptions\ConverterException;

/**
* Interface ConverterInterface
* @author Marcel Strahl <[email protected]>
Expand All @@ -12,6 +14,14 @@ interface ConverterInterface
/**
* @param float $amount
* @return float
* @throws ConverterException
*/
public function convert(float $amount): float;

/**
* @param float $amount
* @return void
* @throws ConverterException
*/
public function isZeroAmount(float $amount): void;
}
13 changes: 13 additions & 0 deletions src/Helpers/Converter/Currencies/CentToEuro.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MarcelStrahl\PriceCalculator\Helpers\Converter;

use MarcelStrahl\PriceCalculator\Exceptions\ConverterException;

/**
* Class CentToEuro
* @author Marcel Strahl <[email protected]>
Expand All @@ -14,6 +16,17 @@ class CentToEuro implements ConverterInterface
*/
public function convert(float $amount): float
{
$this->isZeroAmount($amount);
return (float)bcdiv($amount, 100, 2);
}

/**
* @inheritDoc
*/
public function isZeroAmount(float $amount): void
{
if (empty($amount)) {
throw ConverterException::fromZeroAmount($amount);
}
}
}
13 changes: 13 additions & 0 deletions src/Helpers/Converter/Currencies/EuroToCent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MarcelStrahl\PriceCalculator\Helpers\Converter;

use MarcelStrahl\PriceCalculator\Exceptions\ConverterException;

/**
* Class EuroToCent
* @author Marcel Strahl <[email protected]>
Expand All @@ -14,6 +16,17 @@ class EuroToCent implements ConverterInterface
*/
public function convert(float $amount): float
{
$this->isZeroAmount($amount);
return (float)bcmul($amount, 100);
}

/**
* @inheritDoc
*/
public function isZeroAmount(float $amount): void
{
if (empty($amount)) {
throw ConverterException::fromZeroAmount($amount);
}
}
}
7 changes: 3 additions & 4 deletions src/Helpers/Entity/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

namespace MarcelStrahl\PriceCalculator\Helpers\Entity;

use MarcelStrahl\PriceCalculator\Helpers\Types\PercentInterface;
use MarcelStrahl\PriceCalculator\Helpers\Types\DiscountInterface;
use MarcelStrahl\PriceCalculator\Helpers\Types\NumberInterface;
use MarcelStrahl\PriceCalculator\Helpers\Types\PercentInterface;

/**
* Class Discount
* @author Marcel Strahl <[email protected]>
* @package MarcelStrahl\PriceCalculator\Helpers\Entity
*/
class Discount implements DiscountInterface, PercentInterface, NumberInterface
class Discount implements DiscountInterface, PercentInterface
{
/**
* @var float
*/
private $percent = 0.0;
private $percent = .0;

/**
* @param float $percent
Expand Down
Loading

0 comments on commit 61fbf0a

Please sign in to comment.