/**
  * Helper function which calculates a single price struct of a product.
  * The product can contains multiple price struct elements like the graduated prices
  * and the cheapest price struct.
  * All price structs will be calculated through this function.
  *
  * @param Struct\Product\PriceRule $rule
  * @param Struct\Tax $tax
  * @param Struct\ProductContextInterface $context
  * @return Struct\Product\Price
  */
 private function calculatePriceStruct(Struct\Product\PriceRule $rule, Struct\Tax $tax, Struct\ProductContextInterface $context)
 {
     $price = new Struct\Product\Price($rule);
     //calculates the normal price of the struct.
     $price->setCalculatedPrice($this->calculatePrice($rule->getPrice(), $tax, $context));
     //check if a pseudo price is defined and calculates it too.
     $price->setCalculatedPseudoPrice($this->calculatePrice($rule->getPseudoPrice(), $tax, $context));
     //check if the product has unit definitions and calculate the reference price for the unit.
     if ($price->getUnit() && $price->getUnit()->getPurchaseUnit()) {
         $price->setCalculatedReferencePrice($this->calculateReferencePrice($price));
     }
     return $price;
 }
 /**
  * Helper function which builds the graduated prices
  * of a product for the passed price group discount array.
  *
  * This function is used to override the normal graduated prices
  * with a definition of the product price group discounts.
  *
  * @param Struct\Product\PriceRule $reference
  * @param Struct\Customer\Group $customerGroup
  * @param Struct\Product\PriceDiscount[] $discounts
  * @return array
  */
 private function buildDiscountGraduations(Struct\Product\PriceRule $reference, Struct\Customer\Group $customerGroup, array $discounts)
 {
     $prices = [];
     $firstDiscount = $discounts[0];
     /**@var $previous Struct\Product\PriceRule*/
     $previous = null;
     if ($firstDiscount->getQuantity() > 1) {
         $firstGraduation = clone $reference;
         $previous = $firstGraduation;
         $prices[] = $firstGraduation;
     }
     foreach ($discounts as $discount) {
         $rule = clone $reference;
         $percent = (100 - $discount->getPercent()) / 100;
         $price = $reference->getPrice() * $percent;
         $pseudo = $reference->getPseudoPrice();
         $rule->setPrice($price);
         $rule->setPseudoPrice($pseudo);
         $rule->setFrom($discount->getQuantity());
         $rule->setCustomerGroup($customerGroup);
         $rule->setTo(null);
         if ($previous) {
             $previous->setTo($rule->getFrom() - 1);
         }
         $previous = $rule;
         $prices[] = $rule;
     }
     return $prices;
 }