...
 
Commits (2)
This diff is collapsed.
......@@ -317,8 +317,8 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
throw new InvalidConditionException('Argument 1 must be an instance of \'Bnet\Cart\CartCondition\'');
// set target to cart if not set
$condition->setTarget($condition->getTarget() ?: 'cart');
if ($condition->getTarget() !== 'cart')
$condition->setTarget($condition->getTarget() ?: Condition::TARGET_CART);
if ($condition->getTarget() !== Condition::TARGET_CART)
throw new InvalidConditionException('target have to be cart for cart conditions');
$conditions = $this->getConditions();
......@@ -502,7 +502,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
$condTotal = $this->getConditions($only_with_condition_type)->sum(function ($cond) use ($subTotal) {
/** @var Condition $cond */
return $cond->getTarget() === 'cart'
return $cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal)
: 0;
});
......@@ -544,7 +544,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
$condTotal = $cond->sum(function ($cond) use ($subTotal) {
/** @var Condition $cond */
return $cond->getTarget() === 'cart'
return $cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal)
: 0;
});
......
......@@ -12,6 +12,10 @@ use Illuminate\Support\Collection;
*/
class Condition extends Collection {
const TARGET_CART = 'cart';
const TARGET_ITEM = 'item';
const TARGET_price = 'price';
/**
* List of validation rules for the condition
* @var array
......
......@@ -64,7 +64,7 @@ class CurrencyCart extends Cart {
$condTotal = $this->getConditions($only_with_condition_type)->sum(function ($cond) use ($subTotal) {
/** @var Condition $cond */
$price =$cond->getTarget() === 'cart'
$price =$cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal)
: 0;
return $price instanceof Money
......@@ -106,9 +106,9 @@ class CurrencyCart extends Cart {
return $itemConditionTotal;
/** @var Money $condTotal */
$condTotal = $cond->reduce(function (Money $money = null, $cond) use ($subTotal) {
$condTotal = $cond->reduce(function ($carry, $cond) use ($subTotal) {
/** @var Condition $cond */
$price = $cond->getTarget() === 'cart'
$price = $cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal->amount())
: 0;
......@@ -116,11 +116,12 @@ class CurrencyCart extends Cart {
if (!$price instanceof Money)
$price = new Money((int)$price, $this->currency);
if (!$money) // set the $money at the first run
return $money = $price;
/** @var Money $carry */
if (!$carry) // set the $money at the first run
return $carry = $price;
else
return $money->add($price);
});
return $carry->add($price);
}, null);
return $condTotal->add($itemConditionTotal);
}
......
......@@ -65,7 +65,7 @@ class CurrencyItem extends Item {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */
$price = ($condition && $condition->getTarget() === 'item'
$price = ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType()))
? $condition->applyCondition($originalPrice)
: 0;
......@@ -88,7 +88,7 @@ class CurrencyItem extends Item {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */
$price = ($condition && $condition->getTarget() === 'item'
$price = ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType()))
? $condition->applyConditionWithQuantity($originalPrice, $this->quantity)
: 0;
......
......@@ -84,7 +84,7 @@ class Item extends Collection {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */
return ($condition && $condition->getTarget() === 'item'
return ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType()))
? $condition->applyCondition($originalPrice)
: 0;
......@@ -121,7 +121,7 @@ class Item extends Collection {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */
return ($condition && $condition->getTarget() === 'item'
return ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType()))
? $condition->applyConditionWithQuantity($originalPrice, $this->quantity)
: 0;
......@@ -175,9 +175,9 @@ class Item extends Collection {
$condition = new Condition($condition);
// ignore target==cart conditions
if ($condition->getTarget() == 'cart')
if ($condition->getTarget() == Condition::TARGET_CART)
return false;
$condition->setTarget('item'); // set item as default it not set
$condition->setTarget(Condition::TARGET_ITEM); // set item as default it not set
return $condition;
})
......
This diff is collapsed.
......@@ -536,7 +536,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
$itemCondition = new Condition(array(
'name' => 'SALE 5%',
'type' => 'tax',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => '-5%',
));
$items = [
......@@ -564,7 +564,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
$condition = new Condition(array(
'name' => 'VAT 12.5%',
'type' => 'tax',
'target' => 'cart',
'target' => Condition::TARGET_CART,
'value' => '12.5%',
));
......@@ -574,7 +574,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
'SALE 5%' => [
'name' => 'SALE 5%',
'type' => 'tax',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => '-5%',
]
];
......@@ -603,7 +603,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
'VAT 12.5%' => [
'name' => 'VAT 12.5%',
'type' => 'tax',
'target' => 'cart',
'target' => Condition::TARGET_CART,
'value' => '12.5%',
]
]
......
This diff is collapsed.
......@@ -6,6 +6,7 @@
* Time: 9:59 PM
*/
use Bnet\Cart\Condition;
use Bnet\Cart\CurrencyCart as Cart;
use Bnet\Money\Money;
use Mockery as m;
......@@ -503,7 +504,7 @@ class CurrencyCartTest extends PHPUnit_Framework_TestCase {
new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Tax Plus',
'type' => 'misc2',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\TaxedMoney(500, 'EUR', 20),
])
]
......
<?php
use Bnet\Cart\Condition;
/**
* User: thorsten
* Date: 22.07.16
......@@ -70,37 +72,37 @@ class CurrencyItemTest extends PHPUnit_Framework_TestCase {
$condition_percent_plus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Percent Plus',
'type' => 'misc2',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => '+10%',
]);
$condition_percent_minus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Percent Minus',
'type' => 'misc2',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => '-10%',
]);
$condition_with_tax_plus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Tax Plus',
'type' => 'misc2',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\TaxedMoney(500, 'EUR', 20),
]);
$condition_with_tax_minus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Tax Minus',
'type' => 'misc2',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\TaxedMoney(-500, 'EUR', 20),
]);
$condition_no_tax_plus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Plus',
'type' => 'misc2',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\Money(500, 'EUR'),
]);
$condition_no_tax_minus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Minus',
'type' => 'misc2',
'target' => 'item',
'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\Money(-500, 'EUR'),
]);
......