Commit a0885525 authored by Thorsten Buss's avatar Thorsten Buss

* add totalConditionList() for a list of of totals of the conditions

* FIX: handling with Money objects in CurrencyCart and CurrencyCondition
parent fe2d6778
......@@ -562,6 +562,22 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
return $this->subTotal($only_with_condition_type);
}
/**
* return a list with the total of the conditions
* @param bool $withItemConditions
* @return Collection list of conditions with amount
*/
public function totalConditionsList($withItemConditions = true) {
return $this->getConditions($withItemConditions)
->map(function (Condition $i) {
return $i->getType();
})
->unique()
->mapWithKeys(function ($type) {
return [$type => $this->totalOnlyConditions($type)];
});
}
/**
* get cart sub total - items WITHOUT conditions and WITHOUT cart conditions
*
......
......@@ -105,17 +105,24 @@ class CurrencyCart extends Cart {
if ($cond->isEmpty())
return $itemConditionTotal;
$condTotal = $cond->sum(function ($cond) use ($subTotal) {
/** @var Money $condTotal */
$condTotal = $cond->reduce(function (Money $money = null, $cond) use ($subTotal) {
/** @var Condition $cond */
$price = $cond->getTarget() === 'cart'
? $cond->applyCondition($subTotal->amount())
: 0;
return $price instanceof Money
? $price->amount()
: $price;
// need a money obj to summarize
if (!$price instanceof Money)
$price = new Money((int)$price, $this->currency);
if (!$money) // set the $money at the first run
return $money = $price;
else
return $money->add($price);
});
return new Money((int)($itemConditionTotal->value() + $condTotal), $this->currency);
return $condTotal->add($itemConditionTotal);
}
/**
......
......@@ -38,20 +38,6 @@ class CurrencyCondition extends Condition {
parent::__construct($args);
}
/**
* apply condition
*
* @param $totalOrSubTotalOrPrice
* @param $conditionValue
* @return int
*/
protected function apply($totalOrSubTotalOrPrice, $conditionValue) {
if ($conditionValue instanceof Money) {
$conditionValue = $conditionValue->amount();
}
return parent::apply($totalOrSubTotalOrPrice, $conditionValue);
}
/**
* apply condition with the given quantity
*
......@@ -68,10 +54,13 @@ class CurrencyCondition extends Condition {
/**
* apply condition to total or subtotal
*
* @param $totalOrSubTotalOrPrice
* @param Money|int $totalOrSubTotalOrPrice
* @return Money
*/
public function applyCondition($totalOrSubTotalOrPrice) {
$totalOrSubTotalOrPrice = $totalOrSubTotalOrPrice instanceof Money
? $totalOrSubTotalOrPrice->amount()
: $totalOrSubTotalOrPrice;
$price = (int)parent::applyCondition($totalOrSubTotalOrPrice);
return $this->tax
? new TaxedMoney($price, $this->currency, $this->tax)
......@@ -81,11 +70,14 @@ class CurrencyCondition extends Condition {
/**
* apply condition to total or subtotal
*
* @param $totalOrSubTotalOrPrice
* @param Money|int $totalOrSubTotalOrPrice
* @param int $quantity
* @return Money
*/
public function applyConditionWithQuantity($totalOrSubTotalOrPrice, $quantity) {
$totalOrSubTotalOrPrice = $totalOrSubTotalOrPrice instanceof Money
? $totalOrSubTotalOrPrice->amount()
: $totalOrSubTotalOrPrice;
$price = (int)parent::applyConditionWithQuantity($totalOrSubTotalOrPrice, $quantity);
return $this->tax
? new TaxedMoney($price, $this->currency, $this->tax)
......@@ -95,10 +87,13 @@ class CurrencyCondition extends Condition {
/**
* get the calculated value of this condition supplied by the subtotal|price
*
* @param $totalOrSubTotalOrPrice
* @param Money|int $totalOrSubTotalOrPrice
* @return mixed
*/
public function getCalculatedValue($totalOrSubTotalOrPrice) {
$totalOrSubTotalOrPrice = $totalOrSubTotalOrPrice instanceof Money
? $totalOrSubTotalOrPrice->amount()
: $totalOrSubTotalOrPrice;
$price = (int)parent::getCalculatedValue($totalOrSubTotalOrPrice);
return $this->tax
? new TaxedMoney($price, $this->currency, $this->tax)
......@@ -118,4 +113,16 @@ class CurrencyCondition extends Condition {
public function setTax($tax) {
$this->tax = $tax;
}
/**
* removes some arithmetic signs (%,+,-) only
*
* @param mixed|Money $value
* @return mixed
*/
protected function cleanValue($value) {
return parent::cleanValue($value instanceof Money ? $value->amount() : $value);
}
}
\ No newline at end of file
......@@ -180,7 +180,12 @@ class Item extends Collection {
$condition->setTarget('item'); // set item as default it not set
return $condition;
})->keyBy(function ($cond) use (&$uniqueKeys) {
})
->filter(function($condition) {
// remove conditions which are invalid or for cart instead of item
return $condition !== false;
})
->keyBy(function ($cond) use (&$uniqueKeys) {
// use name as key and verify that the key is unique
$key = @$cond->name ?: 'unknown';
$postfix = '';
......
......@@ -261,6 +261,8 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(95, $this->cart->get(456)->priceSumWithConditions());
$this->assertEquals(-5, $this->cart->get(456)->priceSumOnlyConditions());
$l = $this->cart->totalConditionsList();
$this->assertEquals(-5, $l['tax']);
$this->assertEquals(95, $this->cart->subTotal());
}
......@@ -307,6 +309,11 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(9500, $this->cart->get(456)->priceWithConditions('sale'));
$this->assertEquals(-500, $this->cart->get(456)->priceOnlyConditions('sale'));
$l = $this->cart->totalConditionsList();
$this->assertEquals(-1000, $l['sale']);
$this->assertEquals(-5000, $l['promo']);
$this->assertEquals(2000, $l['misc']);
$this->assertEquals(16000, $this->cart->subTotal(), 'Cart subtotal with 1 item should be 80');
}
......@@ -356,6 +363,11 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(8500, $this->cart->get(456)->priceSumWithConditions(), 'Cart subtotal with 1 item should be 85');
$this->assertEquals(-1500, $this->cart->get(456)->priceSumOnlyConditions(), 'Cart subtotal with 1 item should be 85');
$this->assertEquals(8500, $this->cart->subTotal(), 'Cart subtotal with 1 item should be 85');
$l = $this->cart->totalConditionsList();
$this->assertEquals(-2500, $l['promo']);
$this->assertEquals(1000, $l['misc']);
}
public function test_add_item_condition() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment