Skip to content

Commit

Permalink
PriceCalculator: Added separated discount implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
foxycode committed Dec 10, 2015
1 parent ed545f7 commit 9d0fbb7
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 25 deletions.
64 changes: 64 additions & 0 deletions src/Discount/AmountDiscount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Sunfox\PriceCalculator\Discount;

use Nette;


class AmountDiscount extends Nette\Object implements \Sunfox\PriceCalculator\IDiscount
{
/** @var float */
protected $value = 0.0;


/**
* @param int|float
*/
public function __construct($value)
{
$this->setValue($value);
}

/**
* Get discount value.
*
* @return float
*/
public function getValue()
{
return $this->value;
}

/**
* Set discount value.
*
* @param int|float
* @return IPriceCalculator
*/
public function setValue($value)
{
$this->value = (float) $value;
return $this;
}

/**
* Returns price after discount.
* @param float
* @return float
*/
public function addDiscount($price)
{
return $price - $this->value;
}

/**
* Returns price before discount.
* @param float
* @return float
*/
public function removeDiscount($price)
{
return $price + $this->value;
}

}
64 changes: 64 additions & 0 deletions src/Discount/PercentDiscount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Sunfox\PriceCalculator\Discount;

use Nette;


class PercentDiscount extends Nette\Object implements \Sunfox\PriceCalculator\IDiscount
{
/** @var float */
protected $value = 0.0;


/**
* @param int|float
*/
public function __construct($value)
{
$this->setValue($value);
}

/**
* Get discount value.
*
* @return float
*/
public function getValue()
{
return $this->value;
}

/**
* Set discount value.
*
* @param int|float
* @return IPriceCalculator
*/
public function setValue($value)
{
$this->value = (float) $value;
return $this;
}

/**
* Returns price after discount.
* @param float
* @return float
*/
public function addDiscount($price)
{
return $price * (1 - $this->value / 100);
}

/**
* Returns price before discount.
* @param float
* @return float
*/
public function removeDiscount($price)
{
return $price / (1 - $this->value / 100);
}

}
40 changes: 40 additions & 0 deletions src/IDiscount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Sunfox\PriceCalculator;

use Nette;


interface IDiscount
{

/**
* Get discount value.
*
* @return float
*/
public function getValue();

/**
* Set discount value.
*
* @param int|float
* @return IDiscount
*/
public function setValue($value);

/**
* Returns price after discount.
* @param float
* @return float
*/
public function addDiscount($price);

/**
* Returns price before discount.
* @param float
* @return float
*/
public function removeDiscount($price);

}
4 changes: 2 additions & 2 deletions src/IPriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function getDiscount();
/**
* Set discount in percent without VAT.
*
* @param int|float
* @param IDiscount
* @return IPriceCalculator
*/
public function setDiscount($value);
public function setDiscount(IDiscount $discount = NULL);

/**
* Get price after discount without VAT.
Expand Down
44 changes: 34 additions & 10 deletions src/PriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class PriceCalculator extends Nette\Object implements IPriceCalculator
/** @var float */
protected $basePrice = 0.0;

/** @var float */
protected $discount = 0.0;
/** @var IDiscount|NULL */
protected $discount = NULL;

/** @var float */
protected $price = 0.0;
Expand All @@ -34,7 +34,7 @@ class PriceCalculator extends Nette\Object implements IPriceCalculator
* @param int|float Price without VAT and discount
* @param int|float Discount without VAT
*/
public function __construct($vatRate = 0.0, $basePrice = 0.0, $discount = 0.0)
public function __construct($vatRate = 0.0, $basePrice = 0.0, IDiscount $discount = NULL)
{
$this->setVatRate($vatRate);
$this->setBasePrice($basePrice);
Expand Down Expand Up @@ -67,22 +67,46 @@ public function setBasePrice($value)
/**
* Get discount in percent without VAT.
*
* @return float
* @return IDiscount
*/
public function getDiscount()
{
return $this->discount;
}

/**
* Set discount in percent without VAT.
* Set discount instance.
*
* @param IDiscount
* @return IPriceCalculator
*/
public function setDiscount(IDiscount $discount = NULL)
{
$this->discount = $discount;
return $this;
}

/**
* Set amount discount.
*
* @param int|float
* @return IPriceCalculator
*/
public function setAmountDiscount($value)
{
$this->discount = new Discount\AmountDiscount($value);
return $this;
}

/**
* Set percent discount.
*
* @param int|float
* @return IPriceCalculator
*/
public function setDiscount($value)
public function setPercentDiscount($value)
{
$this->discount = (float) $value;
$this->discount = new Discount\PercentDiscount($value);
return $this;
}

Expand Down Expand Up @@ -188,14 +212,14 @@ public function calculate()
$priceVat = round($this->priceVat, $this->decimalPoints);

if ($this->calculateFrom === self::FROM_BASEPRICE) {
$price = round($basePrice * (1 - $this->discount / 100), $this->decimalPoints);
$price = round($this->discount ? $this->discount->addDiscount($basePrice) : $basePrice, $this->decimalPoints);
$priceVat = round($price * ($this->vatRate / 100 + 1), $this->decimalPoints);
} elseif ($this->calculateFrom === self::FROM_PRICE) {
$basePrice = $this->discount ? round($price / (1 - $this->discount / 100), $this->decimalPoints) : $price;
$basePrice = $this->discount ? round($this->discount->removeDiscount($price), $this->decimalPoints) : $price;
$priceVat = round($price * ($this->vatRate / 100 + 1), $this->decimalPoints);
} elseif ($this->calculateFrom === self::FROM_PRICEVAT) {
$price = round($priceVat / ($this->vatRate / 100 + 1), $this->decimalPoints);
$basePrice = $this->discount ? round($price / (1 - $this->discount / 100), $this->decimalPoints) : $price;
$basePrice = $this->discount ? round($this->discount->removeDiscount($price), $this->decimalPoints) : $price;
}

$vat = $priceVat - $price;
Expand Down
4 changes: 2 additions & 2 deletions src/PriceCalculatorResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PriceCalculatorResult extends Nette\Object
* @param float
* @param float
*/
public function __construct(IPriceCalculator $calculator, $basePrice, $discount,
public function __construct(IPriceCalculator $calculator, $basePrice, IDiscount $discount = NULL,
$price, $vatRate, $vat, $priceVat)
{
$this->calculator = $calculator;
Expand Down Expand Up @@ -129,7 +129,7 @@ public function toArray()
{
return [
'basePrice' => $this->basePrice,
'discount' => $this->discount,
'discount' => $this->discount ? $this->discount->value : 0.0,
'price' => $this->price,
'vatRate' => $this->vatRate,
'vat' => $this->vat,
Expand Down
Loading

0 comments on commit 9d0fbb7

Please sign in to comment.