Skip to content

Commit

Permalink
Fixed FullCombination to prevent right hand execution if left part di…
Browse files Browse the repository at this point in the history
…d not return original charge

Done to prevent discounts over Leasing
  • Loading branch information
SilverFire committed Dec 20, 2018
1 parent c0f64fc commit 789b32d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/charge/modifiers/FullCombination.php
Expand Up @@ -47,14 +47,22 @@ public function modifyCharge(?ChargeInterface $charge, ActionInterface $action):

if ($this->left->isSuitable($charge, $action)) {
$leftCharges = $this->left->modifyCharge($charge, $action);

if ($charge && empty($leftCharges)) {
return []; // If there was at least one charge, but it disappeared – modifier does not want this charge to happen. Stop.
}

$originalChargeExists = array_reduce($leftCharges, function ($result, Charge $item) use ($charge) {
return $result || $charge === $item;
}, false);
if ($charge && !$originalChargeExists) {
return $leftCharges;
}
}

/** @var Charge $leftTotal */
/** @var Charge $charge */
$leftTotal = $this->chargesSum($charge, $leftCharges);
$leftTotal = $this->sumCharges($charge, $leftCharges);
if ($this->right->isSuitable($leftTotal, $action)) {
$dirtyRightCharges = $this->right->modifyCharge($leftTotal, $action);
if ($leftTotal && empty($dirtyRightCharges)) {
Expand Down Expand Up @@ -123,7 +131,7 @@ private function unique(array $charges): array
* @return ChargeInterface|null
* @throws \Exception
*/
private function chargesSum(?ChargeInterface $originalCharge, array $producedCharges): ?ChargeInterface
private function sumCharges(?ChargeInterface $originalCharge, array $producedCharges): ?ChargeInterface
{
if ($originalCharge === null) {
return null;
Expand Down

0 comments on commit 789b32d

Please sign in to comment.