Commit e28818f3 authored by Thomas Bilk's avatar Thomas Bilk

renaming, cleanup and another test

parent ff244cf8
......@@ -346,6 +346,17 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
return $conditions;
}
/**
*
* @return Conditions
*/
public function getCartConditions() {
return (new Conditions($this->session->get($this->sessionKeyCartConditions)))
->filter(function (Condition $c) {
return $c->getTarget() == Condition::TARGET_CART;
});
}
/**
* get condition applied on the cart by its name
*
......@@ -634,7 +645,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
* get a collection of price conditions and their actual value
* @return Conditions
*/
public function priceConditions() {
public function getPriceConditions() {
$subtotal_with_conditions = $this->totalItemsWithoutConditions() + $this->totalOnlyConditions();
return $this->getConditions()
->filter(function (Condition $c) {
......
......@@ -35,12 +35,13 @@ class CurrencyCart extends Cart {
parent::__construct($session, $events, $instanceName, $session_key, $custom_item_rules);
}
/**
* get cart sub total - items WITH conditions WITHOUT cart conditions
*
* @param null|string $only_with_condition_type Only with this conditionType
* @return Money
*/
/**
* get cart sub total - items WITH conditions WITHOUT cart conditions
*
* @param null|string $only_with_condition_type Only with this conditionType
* @return Money
* @throws \Bnet\Money\MoneyException
*/
public function subTotal($only_with_condition_type = null) {
$sum = $this->items()->sum(function (CurrencyItem $item) use ($only_with_condition_type) {
return $item->priceSumWithConditions($only_with_condition_type)->amount();
......@@ -106,6 +107,7 @@ class CurrencyCart extends Cart {
*
* @param null|string $type Only with this conditionType
* @return Money
* @throws \Bnet\Money\MoneyException
*/
public function totalOnlyConditions($type = null) {
$subTotal = $this->subTotal();
......@@ -114,26 +116,25 @@ class CurrencyCart extends Cart {
? $this->getConditionsByType($type)
: $this->getConditions();
$itemConditionTotal = $this->totalItemsOnlyConditions($type);
if ($cond->isEmpty())
if ($cond->isEmpty()) {
return $itemConditionTotal;
}
/** @var Money $condTotal */
$condTotal = $cond->reduce(function ($carry, $cond) use ($subTotal) {
/** @var Condition $cond */
$price = $cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal->amount())
: 0;
// need a money obj to summarize
if (!$price instanceof Money)
$price = new Money((int)$price, $this->currency);
/** @var Money $carry */
if (!$carry) // set the $money at the first run
return $carry = $price;
else
$condTotal = $cond
->filter(function (Condition $cond) {
return $cond->getTarget() === Condition::TARGET_CART;
})
->reduce(function (Money $carry, Condition $cond) use ($subTotal) {
$price = $cond->applyCondition($subTotal->amount());
// need a money obj to summarize
if (!$price instanceof Money) {
$price = new Money((int)$price, $this->currency);
}
return $carry->add($price);
}, null);
}, new Money(0));
return $condTotal->add($itemConditionTotal);
}
......@@ -190,7 +191,7 @@ class CurrencyCart extends Cart {
* @return Conditions
* @throws \Bnet\Money\MoneyException
*/
public function priceConditions() {
public function getPriceConditions() {
$subtotal_with_conditions = $this->totalItemsWithoutConditions()->add($this->totalOnlyConditions());
return $this->getConditions()
->filter(function (Condition $c) {
......
......@@ -82,20 +82,22 @@ class CurrencyItem extends Item {
*
* @param null|string $type only sum with conditions for this $type
* @return Money
* @throws \Bnet\Money\MoneyException
*/
public function priceSumWithConditions($type = null) {
$originalPrice = $this->price->amount();
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */
$price = ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType()))
? $condition->applyConditionWithQuantity($originalPrice, $this->quantity)
: 0;
return $price instanceof Money
? $price->amount()
: $price;
});
$condition_price = $this->conditions
->filter(function (Condition $cond) use ($type) {
return $cond->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $cond->getType() == $type);
})
->sum(function (Condition $cond) use ($originalPrice) {
$price = $cond->applyConditionWithQuantity($originalPrice, $this->quantity);
return $price instanceof Money
? $price->amount()
: $price;
});
$newPrice = $this->returnPriceAboveZero($condition_price + ($this->quantity * $originalPrice));
return new Money((int)$newPrice, $this->currency);
}
......
......@@ -1151,20 +1151,20 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
]),
]);
$conditions = $this->cart->priceConditions();
$conditions = $this->cart->getPriceConditions();
$this->assertEquals(2, $conditions->count());
$this->assertEquals($this->cart->subTotal(), abs($conditions->sum('value')));
$this->assertEquals(0, $this->cart->total());
$this->cart->update(456, ['quantity' => '+1']);
$conditions = $this->cart->priceConditions();
$conditions = $this->cart->getPriceConditions();
$this->assertEquals(8000, abs($conditions->sum('value')));
$this->assertEquals(2000, $this->cart->total());
$this->cart->removeConditionsByType('coupon');
$conditions = $this->cart->priceConditions();
$conditions = $this->cart->getPriceConditions();
$this->assertEquals(0, $conditions->count());
$this->assertEquals(0, abs($conditions->sum('value')));
......
......@@ -1049,7 +1049,7 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(100),
'price' => new Money(10000),
'quantity' => 3,
]);
......@@ -1057,13 +1057,13 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
'name' => 'Coupon',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-5),
'value' => new Money(-500),
]);
$this->cart->condition($coupon);
$this->assertEquals(300, $this->cart->subTotal()->amount());
$this->assertEquals(295, $this->cart->total()->amount());
$this->assertEquals(30000, $this->cart->subTotal()->amount());
$this->assertEquals(29500, $this->cart->total()->amount());
}
/**
......@@ -1075,7 +1075,7 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(100),
'price' => new Money(10000),
'quantity' => 3,
]);
......@@ -1083,20 +1083,20 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
'name' => 'Coupon 1',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-5),
'value' => new Money(-500),
]);
$coupon2 = new CurrencyCondition([
'name' => 'Coupon 2',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-15),
'value' => new Money(-1500),
]);
$this->cart->condition([$coupon1, $coupon2]);
$this->assertEquals(300, $this->cart->subTotal()->amount());
$this->assertEquals(280, $this->cart->total()->amount());
$this->assertEquals(30000, $this->cart->subTotal()->amount());
$this->assertEquals(28000, $this->cart->total()->amount());
}
/**
......@@ -1108,7 +1108,7 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(25),
'price' => new Money(2500),
'quantity' => 3,
]);
......@@ -1116,19 +1116,19 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
'name' => 'Coupon 1',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-50),
'value' => new Money(-5000),
]);
$coupon2 = new CurrencyCondition([
'name' => 'Coupon 2',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-30),
'value' => new Money(-3000),
]);
$this->cart->condition([$coupon1, $coupon2]);
$this->assertEquals(75, $this->cart->subTotal()->amount());
$this->assertEquals(7500, $this->cart->subTotal()->amount());
$this->assertEquals(0, $this->cart->total()->amount());
}
......@@ -1141,7 +1141,7 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(25),
'price' => new Money(2500),
'quantity' => 3,
]);
......@@ -1149,32 +1149,32 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
'name' => 'Coupon 1',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-50),
'value' => new Money(-5000),
]);
$coupon2 = new CurrencyCondition([
'name' => 'Coupon 2',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-30),
'value' => new Money(-3000),
]);
$this->cart->condition([$coupon1, $coupon2]);
$this->assertEquals(75, $this->cart->subTotal()->amount());
$this->assertEquals(7500, $this->cart->subTotal()->amount());
$this->assertEquals(0, $this->cart->total()->amount());
/* remove an item from the cart */
$this->cart->update(456, ['quantity' => -1]);
$this->assertEquals(50, $this->cart->subTotal()->amount());
$this->assertEquals(5000, $this->cart->subTotal()->amount());
$this->assertEquals(0, $this->cart->total()->amount());
/* remove a coupon */
$this->cart->removeCartCondition('Coupon 1');
$this->assertEquals(50, $this->cart->subTotal()->amount());
$this->assertEquals(20, $this->cart->total()->amount());
$this->assertEquals(5000, $this->cart->subTotal()->amount());
$this->assertEquals(2000, $this->cart->total()->amount());
}
/**
......@@ -1186,7 +1186,7 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(25),
'price' => new Money(2500),
'quantity' => 3,
]);
......@@ -1194,7 +1194,7 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
'name' => 'Coupon 1',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-50),
'value' => new Money(-5000),
]);
$cartCondition1 = new CurrencyCondition([
......@@ -1211,8 +1211,8 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition([$coupon1, $cartCondition1]);
$this->assertEquals(75, $this->cart->subTotal()->amount());
$this->assertEquals(21, $this->cart->total()->amount());
$this->assertEquals(7500, $this->cart->subTotal()->amount());
$this->assertEquals(2125, $this->cart->total()->amount());
}
/**
......@@ -1224,7 +1224,7 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(25),
'price' => new Money(2500),
'quantity' => 3,
]);
$this->cart->condition([
......@@ -1232,17 +1232,17 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
'name' => 'Coupon 1',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-50),
'value' => new Money(-5000),
]),
new CurrencyCondition([
'name' => 'Coupon 2',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-30),
'value' => new Money(-3000),
]),
]);
$conditions = $this->cart->priceConditions();
$conditions = $this->cart->getPriceConditions();
$this->assertEquals(2, $conditions->count());
$this->assertEquals(
......@@ -1254,17 +1254,17 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(0, $this->cart->total()->amount());
$this->cart->update(456, ['quantity' => '+1']);
$conditions = $this->cart->priceConditions();
$conditions = $this->cart->getPriceConditions();
$this->assertEquals(80,
$this->assertEquals(8000,
abs($conditions->reduce(function (Money $a, CurrencyCondition $c) {
return $a->add($c->getValue());
}, new Money(0))->amount())
);
$this->assertEquals(20, $this->cart->total()->amount());
$this->assertEquals(2000, $this->cart->total()->amount());
$this->cart->removeConditionsByType('coupon');
$conditions = $this->cart->priceConditions();
$conditions = $this->cart->getPriceConditions();
$this->assertEquals(0, $conditions->count());
$this->assertEquals(0,
......@@ -1272,6 +1272,54 @@ class CurrencyCartConditionTest extends PHPUnit_Framework_TestCase {
return $a->add($c->getValue());
}, new Money(0))->amount())
);
$this->assertEquals(100, $this->cart->total()->amount());
$this->assertEquals(10000, $this->cart->total()->amount());
}
/**
* @throws InvalidConditionException
* @throws InvalidItemException
* @throws MoneyException
*/
public function test_coupon_and_cart_and_item_condition() {
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(2500),
'quantity' => 3,
'conditions' => [
new CurrencyCondition([
'name' => 'Test1 With Tax',
'tax' => 20,
'type' => 'sale',
'target' => Condition::TARGET_ITEM,
'quantity_independent' => true,
'value' => new Money(500)
])
]
]);
$cartCondition1 = new CurrencyCondition([
'name' => 'SALE 5%',
'type' => 'sale',
'target' => Condition::TARGET_CART,
'value' => '-5%',
'attributes' => [
'description' => 'october fest promo sale',
'sale_start_date' => '2015-01-20',
'sale_end_date' => '2015-01-30',
]
]);
$coupon1 = new CurrencyCondition([
'name' => 'Coupon 1',
'type' => 'coupon',
'target' => Condition::TARGET_PRICE,
'value' => new Money(-5000),
]);
$this->cart->condition([$coupon1, $cartCondition1]);
$this->assertEquals(8100, $this->cart->subTotal()->amount());
$this->assertEquals(2695, $this->cart->total()->amount());
}
}
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