|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- <?php
-
- namespace Lc\CaracoleBundle\Solver\Product;
-
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Collections\Collection;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
- use Lc\CaracoleBundle\Model\Product\ProductInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
- 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 = $this->getProductsOnline($productFamily)->getValues();
- if (count($products) > 0) {
- usort($products, $comparisonFunction);
- return $products[0];
- }
- } else {
- return $this->getOriginProduct($productFamily);
- }
- if ($returnSelfIfNotActiveProducts) {
- return $productFamily;
- } else {
- return false;
- }
- }
-
-
- public function getAvailableQuantityInherited(ProductFamilyInterface $productFamily)
- {
- $availableQuantity = 0;
-
- switch ($productFamily->getBehaviorCountStock()) {
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
-
- $availableQuantity = $productFamily->getAvailableQuantity();
- break;
-
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
-
- foreach ($this->getProductsOnline($productFamily) as $product) {
- $availableQuantity += $product->getAvailableQuantityInherited();
- }
- break;
-
- case ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED :
- $availableQuantity = false;
- break;
- }
-
- return $availableQuantity;
- }
-
-
- public function getTaxRateInherited(ProductFamilyInterface $productFamily)
- {
- if ($productFamily->getTaxRate()) {
- return $productFamily->getTaxRate();
- } else {
- return $productFamily->getSection()->getMerchant()->getTaxRate();
- }
- }
-
-
- public function getProductsOnline(ProductFamilyInterface $productFamily): Collection
- {
- $products = $productFamily->getProducts();
- $productsOnlineArray = new ArrayCollection();
-
- foreach ($products as $product) {
- if ($product->getStatus() == 1 && $product->getOriginProduct() != true) {
- $productsOnlineArray[] = $product;
- }
- }
-
- return $productsOnlineArray;
- }
-
-
- public function getReductionCatalogInherited(ProductFamilyInterface $productFamily): ?ReductionCatalogInterface
- {
- return $productFamily->getReductionCatalog();
- }
-
-
- public function getProductCategoryParent(ProductFamilyInterface $productFamily)
- {
- $productCategories = $productFamily->getProductCategories();
-
- if (count($productCategories) > 0) {
- return $productCategories[0]->getParent();
- }
-
- return false;
- }
-
-
- public function getProductCategoryChild(ProductFamilyInterface $productFamily)
- {
- $productCategories = $productFamily->getProductCategories();
-
- foreach ($productCategories as $productCategory) {
- if ($productCategory->getParent()) {
- return $productCategory;
- }
- }
-
- return false;
- }
-
-
- public function isPropertyNoveltyOnline(ProductFamilyInterface $productFamily): ?bool
- {
- if ($productFamily->getPropertyNoveltyExpirationDate()) {
- $now = new \DateTime();
- if ($now <= $productFamily->getPropertyNoveltyExpirationDate()) {
- return true;
- }
- }
-
- return false;
- }
-
-
- public function countProperties(ProductFamilyInterface $productFamily): bool
- {
- $count = 0;
-
- $count += (int)strlen($productFamily->getPropertyOrganicLabel()) > 0;
- $count += (int)strlen($productFamily->getPropertyWeight()) > 0;
- $count += (int)strlen($productFamily->getPropertyFragrances()) > 0;
- $count += (int)strlen($productFamily->getPropertyComposition()) > 0;
- $count += (int)strlen($productFamily->getPropertyAllergens()) > 0;
- $count += (int)strlen($productFamily->getPropertyAlcoholLevel()) > 0;
- $count += (int)strlen($productFamily->getPropertyCharacteristics()) > 0;
- $count += (int)strlen($productFamily->getPropertyFeature()) > 0;
- $count += (int)strlen($productFamily->getPropertyPackaging()) > 0;
- $count += (int)strlen($productFamily->getPropertyQuantity()) > 0;
- $count += (int)strlen($productFamily->getPropertyVariety()) > 0;
- $count += (int)($productFamily->getPropertyExpirationDate() != null);
-
- return $count;
- }
-
-
- public function hasProductsWithVariousWeight(ProductFamilyInterface $productFamily)
- {
- if ($productFamily->getActiveProducts()) {
- $arrayCountProducts = [];
- $products = $this->getProductsOnline($productFamily);
-
- foreach ($products as $product) {
- $titleProduct = $product->getTitleInherited();
- if (!isset($arrayCountProducts[$titleProduct])) {
- $arrayCountProducts[$titleProduct] = [];
- }
-
- if (!in_array($product->getQuantityLabelInherited(), $arrayCountProducts[$titleProduct])) {
- $arrayCountProducts[$titleProduct][] = $product->getQuantityLabelInherited();
- }
-
- if (count($arrayCountProducts[$titleProduct]) > 1) {
- return true;
- }
- }
- }
-
- return false;
- }
-
-
- public function getProductsGroupByTitle(ProductFamilyInterface $productFamily): array
- {
- $arrayProductsGroupByTitle = [];
- $products = $this->getProductsOnline($productFamily);
-
- foreach ($products as $product) {
- if ($product->getStatus() == 1) {
- $titleProduct = $product->getTitleInherited();
- if (!isset($arrayProductsGroupByTitle[$titleProduct])) {
- $arrayProductsGroupByTitle[$titleProduct] = [];
- }
- $arrayProductsGroupByTitle[$titleProduct][] = $product;
- }
- }
-
- return $arrayProductsGroupByTitle;
- }
-
-
- public function getOriginProduct(ProductFamilyInterface $productFamily): ?ProductInterface
- {
- $products = $productFamily->getProducts();
-
- foreach ($products as $product) {
- if ($product->getOriginProduct()) {
- return $product;
- }
- }
-
- return null;
- }
-
-
- public function getOriginProductOnline(ProductFamilyInterface $productFamily): ?ProductInterface
- {
- $originProduct = $this->getOriginProduct($productFamily);
-
- if ($originProduct->getStatus() == 1) {
- return $originProduct;
- } else {
- return null;
- }
- }
-
-
- public function hasOneProductOnline(ProductFamilyInterface $productFamily)
- {
- if (($productFamily->getActiveProducts() && count($this->getProductsOnline($productFamily)) > 0)
- || (!$productFamily->getActiveProducts() && $this->getOriginProduct($productFamily))) {
- return true;
- }
-
- return false;
- }
-
-
- public function getFieldBuyingPrice(ProductFamilyInterface $productFamily): string
- {
- if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
- return 'buyingPrice';
- } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
- return 'buyingPriceByRefUnit';
- }
- }
-
-
- public function getFieldPrice(ProductFamilyInterface $productFamily): string
- {
- if ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE) {
- return 'price';
- } elseif ($productFamily->getBehaviorPrice() === ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT) {
- return 'priceByRefUnit';
- }
- }
-
- public function getBehaviorPriceInherited(ProductFamilyInterface $productFamily) :string
- {
- return $productFamily->getBehaviorPrice();
- }
-
- public function getBehaviorCountStockChoices(): array
- {
- return [
- ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE,
- ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT,
- ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
- ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED,
- ];
- }
-
- public function getBehaviorDisplaySaleChoices(): array
- {
- return [
- ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_MEASURE,
- ProductFamilyModel::BEHAVIOR_DISPLAY_SALE_BY_QUANTITY,
- ];
- }
-
- public function getBehaviorStockCycleChoices(): array
- {
- return [
- ProductFamilyModel::BEHAVIOR_STOCK_CYCLE_NON_RENEWABLE,
- ProductFamilyModel::BEHAVIOR_STOCK_CYCLE_RENEWABLE,
- ProductFamilyModel::BEHAVIOR_STOCK_CYCLE_RENEWABLE_VALIDATION,
- ];
- }
-
- public function getWaringMessageTypeChoices(): array
- {
- return [
- ProductFamilyModel::WARNING_MESSAGE_TYPE_ERROR,
- ProductFamilyModel::WARNING_MESSAGE_TYPE_INFO,
- ProductFamilyModel::WARNING_MESSAGE_TYPE_SUCCESS,
- ProductFamilyModel::WARNING_MESSAGE_TYPE_WARNING,
- ];
- }
-
- public function getBehaviorAddToCartChoices(): array
- {
- return [
- ProductFamilyModel::BEHAVIOR_ADD_TO_CART_MULTIPLE,
- ProductFamilyModel::BEHAVIOR_ADD_TO_CART_SIMPLE,
- ];
- }
-
- public function getBehaviorPriceChoices(): array
- {
- return [
- ProductFamilyModel::BEHAVIOR_PRICE_BY_PIECE,
- ProductFamilyModel::BEHAVIOR_PRICE_BY_REFERENCE_UNIT,
- ];
- }
-
-
- public function getPropertyOrganicLabelChoices(): array
- {
- return [
- ProductFamilyModel::PROPERTY_ORGANIC_LABEL_AB,
- ProductFamilyModel::PROPERTY_ORGANIC_LABEL_NP,
- ProductFamilyModel::PROPERTY_ORGANIC_LABEL_HVE,
- ProductFamilyModel::PROPERTY_ORGANIC_LABEL_TVVR,
- ];
- }
-
- public function getTypeExpirationDateChoices(): array
- {
- return [
- ProductFamilyModel::TYPE_EXPIRATION_DATE_DLC,
- ProductFamilyModel::TYPE_EXPIRATION_DATE_DDM,
- ProductFamilyModel::TYPE_EXPIRATION_DATE_DLUO,
- ];
- }
-
-
- public function getBehaviorExpirationDateChoices(): array
- {
- return [
- ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY,
- ProductFamilyModel::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT,
- ];
- }
-
- }
-
|