|
- <?php
-
- namespace Lc\CaracoleBundle\Solver\Product;
-
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
- use Lc\CaracoleBundle\Solver\Price\PriceSolver;
-
- class ProductFamilySolver
- {
- protected PriceSolver $priceSolver;
-
- public function __construct(PriceSolver $priceSolver)
- {
- $this->priceSolver = $priceSolver;
- }
-
- public function getMultiplyingFactor(ProductFamilyInterface $productFamily)
- {
- if ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
- if ($productFamily->getBuyingPrice() > 0) {
- return number_format(
- $this->priceSolver->getPriceWithTax($productFamily) / $productFamily->getBuyingPrice(),
- 3
- );
- }
- } elseif ($productFamily->getBehaviorPrice() == ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
- if ($productFamily->getBuyingPriceByRefUnit() > 0) {
- return number_format(
- $this->priceSolver->getPriceByRefUnitWithTax(
- $productFamily
- ) / $productFamily->getBuyingPriceByRefUnit(),
- 3
- );
- }
- }
- }
-
- public function getCheapestProduct(ProductFamilyInterface $productFamily)
- {
- $priceSolver = $this->priceSolver;
- return $this->getCheapestOrMostExpensiveProduct(
- $productFamily,
- function ($a, $b) use ($priceSolver) {
- return $priceSolver->getPriceWithTaxAndReduction(
- $a
- ) > $priceSolver->getPriceWithTaxAndReduction($b);
- },
- true
- );
- }
-
- public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily)
- {
- $priceSolver = $this->priceSolver;
- return $this->getCheapestOrMostExpensiveProduct(
- $productFamily,
- function ($a, $b) use ($priceSolver) {
- return $priceSolver->getPriceByRefUnitWithTaxAndReduction(
- $a
- ) > $priceSolver->getPriceByRefUnitWithTaxAndReduction($b);
- },
- false
- );
- }
-
- public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily)
- {
- $priceSolver = $this->priceSolver;
- return $this->getCheapestOrMostExpensiveProduct(
- $productFamily,
- function ($a, $b) use ($priceSolver) {
- return $priceSolver->getPriceByRefUnitWithTaxAndReduction(
- $a
- ) < $priceSolver->getPriceByRefUnitWithTaxAndReduction($b);
- },
- false
- );
- }
-
- private function getCheapestOrMostExpensiveProduct(
- ProductFamilyInterface $productFamily,
- $comparisonFunction,
- $returnSelfIfNotActiveProducts
- ) {
- if ($productFamily->getActiveProducts()) {
- $products = $productFamily->getProductsOnline()->getValues();
- if (count($products) > 0) {
- usort($products, $comparisonFunction);
- return $products[0];
- }
- } else {
- return $productFamily->getOriginProduct();
- }
- if ($returnSelfIfNotActiveProducts) {
- return $productFamily;
- } else {
- return false;
- }
- }
- }
-
|