|
- <?php
-
- namespace Lc\CaracoleBundle\Resolver;
-
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
- use Lc\CaracoleBundle\Solver\Order\OrderShopSolver;
- use Lc\CaracoleBundle\Solver\Price\PriceSolver;
- use Lc\CaracoleBundle\Solver\Product\ProductFamilySolver;
-
- class ProductFamilyResolver
- {
- protected PriceSolver $priceSolver;
- protected ProductFamilySolver $productFamilySolver;
- protected OrderShopSolver $orderShopSolver;
-
- public function __construct(PriceSolver $priceSolver, ProductFamilySolver $productFamilySolver, OrderShopSolver $orderShopSolver)
- {
- $this->priceSolver = $priceSolver;
- $this->productFamilySolver = $productFamilySolver;
- $this->orderShopSolver = $orderShopSolver;
- }
-
- 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, OrderShopInterface $orderShop = null)
- {
- $priceSolver = $this->priceSolver;
-
- return $this->getCheapestOrMostExpensiveProduct(
- $productFamily,
- function ($a, $b) use ($priceSolver) {
- return $priceSolver->getPriceWithTaxAndReduction(
- $a
- ) > $priceSolver->getPriceWithTaxAndReduction($b);
- },
- true,
- $orderShop
- );
- }
-
- public function getCheapestProductByRefUnit(ProductFamilyInterface $productFamily, OrderShopInterface $orderShop = null)
- {
- $priceSolver = $this->priceSolver;
-
- return $this->getCheapestOrMostExpensiveProduct(
- $productFamily,
- function ($a, $b) use ($priceSolver) {
- return $priceSolver->getPriceByRefUnitWithTaxAndReduction(
- $a
- ) > $priceSolver->getPriceByRefUnitWithTaxAndReduction($b);
- },
- false,
- $orderShop
- );
- }
-
- public function getMostExpensiveProductByRefUnit(ProductFamilyInterface $productFamily, OrderShopInterface $orderShop = null)
- {
- $priceSolver = $this->priceSolver;
-
- return $this->getCheapestOrMostExpensiveProduct(
- $productFamily,
- function ($a, $b) use ($priceSolver) {
- return $priceSolver->getPriceByRefUnitWithTaxAndReduction(
- $a
- ) < $priceSolver->getPriceByRefUnitWithTaxAndReduction($b);
- },
- false,
- $orderShop
- );
- }
-
- private function getCheapestOrMostExpensiveProduct(
- ProductFamilyInterface $productFamily,
- $comparisonFunction,
- $returnSelfIfNotActiveProducts,
- OrderShopInterface $orderShop = null
- )
- {
- if ($productFamily->getActiveProducts()) {
- $products = $this->productFamilySolver->getProductsOnline($productFamily)->getValues();
-
- if (count($products) > 0) {
- usort($products, $comparisonFunction);
-
- if($orderShop) {
- foreach ($products as $product) {
- if ($this->orderShopSolver->isProductAvailable($orderShop->getSection(), $orderShop, $product, 1, true)) {
- return $product;
- }
- }
- }
-
- return $products[0];
- }
- } else {
- return $this->productFamilySolver->getOriginProduct($productFamily);
- }
- if ($returnSelfIfNotActiveProducts) {
- return $productFamily;
- } else {
- return false;
- }
- }
- }
|