/**
  * Main function that determines what the result does
  * @param \Heystack\Deals\Interfaces\DealHandlerInterface $dealHandler
  * @return \SebastianBergmann\Money\Money
  */
 public function process(DealHandlerInterface $dealHandler)
 {
     $discount = $this->getCurrencyService()->getZeroMoney();
     $dealIdentifier = $dealHandler->getIdentifier();
     // Reset the free count for this deal of all the purchasables. We need to do this because
     // a new 'cheaper' product may have been added to the purchasable holder in the meantime
     foreach ($this->purchasableHolder->getPurchasables() as $purchasable) {
         if ($purchasable instanceof DealPurchasableInterface) {
             $purchasable->setFreeQuantity($dealIdentifier, 0);
             $purchasable->setDealDiscount($dealIdentifier, $discount);
         }
     }
     $remainingFreeDiscounts = $dealHandler->getConditionsMetCount();
     $purchasables = $this->getPurchsablesSortedByUnitPrice();
     $purchasable = current($purchasables);
     while ($purchasable && $remainingFreeDiscounts > 0) {
         // Skip if it isn't a deal purchasable
         if (!$purchasable instanceof DealPurchasableInterface) {
             $purchasable = next($purchasable);
             continue;
         }
         // Get the smaller number, either the quantity of the current purchasable
         // or the remaining free discounts
         $freeQuantity = min($purchasable->getQuantity(), $remainingFreeDiscounts);
         // Get the current discount
         $currentPurchasableDiscount = $purchasable->getDealDiscountWithExclusions([$dealIdentifier->getFull()]);
         $purchasableTotal = $purchasable->getTotal();
         $purchasableDiscount = $purchasable->getUnitPrice()->multiply($freeQuantity);
         // When the deduction exceeds the remaining money just remove the remaining money
         if ($currentPurchasableDiscount->add($purchasableDiscount)->greaterThan($purchasableTotal)) {
             $purchasableDiscount = $purchasableTotal->subtract($currentPurchasableDiscount);
         }
         $discount = $discount->add($purchasableDiscount);
         $purchasable->setFreeQuantity($dealIdentifier, $freeQuantity);
         $purchasable->setDealDiscount($dealHandler->getIdentifier(), $purchasableDiscount);
         $remainingFreeDiscounts -= $freeQuantity;
         // Advance to the next purchasable
         $purchasable = next($purchasables);
     }
     $this->purchasableHolder->updateTotal();
     $this->eventService->dispatch(Events::RESULT_PROCESSED, new ResultEvent($this));
     return $this->totalDiscount = $discount;
 }
Ejemplo n.º 2
0
 /**
  * @param \SS_HTTPRequest $request
  * @param \SS_HTTPResponse $response
  * @param \DataModel $model
  * @return void
  */
 public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model)
 {
     if ($this->eventDispatcher instanceof EventDispatcher) {
         $this->eventDispatcher->dispatch(Events::POST_REQUEST);
     }
 }