131 lines
4.3 KiB
PHP
131 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace DigressivePrice\Loop;
|
|
|
|
use DigressivePrice\Model\DigressivePrice;
|
|
use Propel\Runtime\ActiveQuery\Criteria;
|
|
use Thelia\Core\Template\Element\BaseI18nLoop;
|
|
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
|
|
use Thelia\Core\Template\Element\LoopResult;
|
|
use Thelia\Core\Template\Element\LoopResultRow;
|
|
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
|
|
use Thelia\Core\Template\Loop\Argument\Argument;
|
|
use DigressivePrice\Model\DigressivePriceQuery;
|
|
use Thelia\Model\ProductQuery;
|
|
use Thelia\Model\ProductSaleElementsQuery;
|
|
|
|
/**
|
|
* Class DigressiveLoop
|
|
* Definition of the Digressive loop of DigressivePrice module
|
|
*
|
|
* @package DigressivePrice\Loop
|
|
* @author Etienne PERRIERE <eperriere@openstudio.fr> - Nexxpix - OpenStudio
|
|
* @method int getProductId()
|
|
* @method int getPseId()
|
|
* @method int getQuantity()
|
|
*/
|
|
class DigressiveLoop extends BaseI18nLoop implements PropelSearchLoopInterface
|
|
{
|
|
public $countable = true;
|
|
|
|
protected function getArgDefinitions()
|
|
{
|
|
return new ArgumentCollection(
|
|
Argument::createIntTypeArgument('product_id'),
|
|
Argument::createIntTypeArgument('pse_id'),
|
|
Argument::createIntTypeArgument('quantity')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return DigressivePriceQuery|\Propel\Runtime\ActiveQuery\ModelCriteria
|
|
*/
|
|
public function buildModelCriteria()
|
|
{
|
|
$search = DigressivePriceQuery::create();
|
|
|
|
if (null !== $productId = $this->getProductId()) {
|
|
$search->filterByProductId($productId);
|
|
} elseif (null !== $pseId = $this->getPseId()) {
|
|
$pse = ProductSaleElementsQuery::create()->findPk($pseId);
|
|
|
|
if (null !== $pse) {
|
|
$search->filterByProductId($pse->getProductId());
|
|
}
|
|
}
|
|
|
|
if (null !== $quantity = $this->getQuantity()) {
|
|
$search
|
|
->filterByQuantityFrom($quantity, Criteria::LESS_EQUAL)
|
|
->filterByQuantityTo($quantity, Criteria::GREATER_EQUAL)
|
|
;
|
|
|
|
}
|
|
|
|
$search->orderByQuantityFrom();
|
|
|
|
return $search;
|
|
}
|
|
|
|
/**
|
|
* @param LoopResult $loopResult
|
|
* @return LoopResult
|
|
* @throws \Propel\Runtime\Exception\PropelException
|
|
*/
|
|
public function parseResults(LoopResult $loopResult)
|
|
{
|
|
$currency = $this->requestStack->getCurrentRequest()->getSession()->getCurrency();
|
|
|
|
/** @var DigressivePrice $digressivePrice */
|
|
foreach ($loopResult->getResultDataCollection() as $digressivePrice) {
|
|
$loopResultRow = new LoopResultRow($digressivePrice);
|
|
|
|
if (null !== $pseId = $this->getPseId()) {
|
|
if (null !== $pse = ProductSaleElementsQuery::create()->findPk($pseId)) {
|
|
$product = $pse->getProduct();
|
|
$productId = $product->getId();
|
|
|
|
$prices = $pse->getPricesByCurrency($currency);
|
|
}
|
|
} else {
|
|
$productId = $digressivePrice->getProductId();
|
|
$product = ProductQuery::create()->findOneById($productId);
|
|
|
|
$prices = $product
|
|
->getDefaultSaleElements()
|
|
->getPricesByCurrency($currency);
|
|
}
|
|
|
|
$remise = 1 - $digressivePrice->getDiscount() / 100;
|
|
|
|
$price = $remise * $prices->getPrice();
|
|
$promo = $remise * $prices->getPromoPrice();
|
|
|
|
// Get country
|
|
$taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry();
|
|
|
|
// Get taxed prices
|
|
$taxedPrice = $product->getTaxedPrice($taxCountry, $price);
|
|
$taxedPromoPrice = $product->getTaxedPromoPrice($taxCountry, $promo);
|
|
|
|
$loopResultRow
|
|
->set("ID", $digressivePrice->getId())
|
|
->set("PRODUCT_ID", $productId)
|
|
->set("QUANTITY_FROM", $digressivePrice->getQuantityFrom())
|
|
->set("QUANTITY_TO", $digressivePrice->getQuantityTo())
|
|
|
|
->set("DISCOUNT", $digressivePrice->getDiscount())
|
|
|
|
->set("PRICE", $price)
|
|
->set("PROMO_PRICE", $promo)
|
|
|
|
->set("TAXED_PRICE", $taxedPrice)
|
|
->set("TAXED_PROMO_PRICE", $taxedPromoPrice);
|
|
|
|
$loopResult->addRow($loopResultRow);
|
|
}
|
|
|
|
return $loopResult;
|
|
}
|
|
}
|