Files
kupshop/bundles/KupShop/MarginsBundle/Compute/Compute.php
2025-08-02 16:30:27 +02:00

84 lines
2.4 KiB
PHP

<?php
namespace KupShop\MarginsBundle\Compute;
use Doctrine\ORM\EntityManagerInterface;
class Compute
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* Compute constructor.
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function computeAll()
{
$this->computeProducts();
$this->computeVariations();
}
protected function computeProducts()
{
sqlQueryBuilder()
->update('products', 'p')
->set('p.price', $this->getMarginPattern('p'))
->where('p.price_buy IS NOT NULL AND !FIND_IN_SET(\'MM\', p.campaign)')
->execute();
}
protected function computeVariations()
{
sqlQuery("
UPDATE products_variations pv
JOIN margins m ON m.range_from < pv.price_buy AND m.range_to >= pv.price_buy
JOIN products p ON p.id = pv.id_product
SET pv.price = pv.price_buy * (100 + m.margin) / 100
WHERE pv.price_buy IS NOT NULL AND !FIND_IN_SET('MM', p.campaign)
");
}
/**
* @param $id_product int id of product
*
* @return bool
*/
public function computeProductPrice($id_product)
{
return sqlQueryBuilder()
->update('products', 'p')
->set('p.price', $this->getMarginPattern('p'))
->where('p.price_buy IS NOT NULL AND p.id = :id AND !FIND_IN_SET(\'MM\', p.campaign)')
->setParameter('id', $id_product)
->execute();
}
/**
* @param $id_variation int id of variation
*
* @return bool
*/
public function computeVariationPrice($id_variation = null)
{
return sqlQuery("
UPDATE products_variations pv
JOIN margins m ON m.range_from < pv.price_buy AND m.range_to >= pv.price_buy
JOIN products p ON p.id = pv.id_product
SET pv.price = pv.price_buy * (100 + m.margin) / 100
WHERE pv.price_buy IS NOT NULL AND pv.id = :id AND !FIND_IN_SET('MM', p.campaign)
", ['id' => $id_variation]);
}
private function getMarginPattern($alias)
{
return "{$alias}.price_buy * (100 + COALESCE((SELECT margin FROM margins m WHERE m.range_from < {$alias}.price_buy AND m.range_to >= {$alias}.price_buy), 0)) / 100";
}
}