|
- <?php
-
- namespace Lc\CaracoleBundle\Solver\Product;
-
- use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
- use Lc\CaracoleBundle\Model\Product\ProductInterface;
-
- class ProductSolver
- {
-
- // isProductAvailable
- public function isAvailable(ProductInterface $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
- {
- if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1 || !$this->isProductSaleStatusOn($product)) {
- return false;
- }
-
- // @TODO : orderShop à définir où est appelé isProductAvailable
- if ($checkCart && !$orderShop) {
- throw new \Exception("Attention jeune padawan : définir le orderShop à l'endroit où est appelé isProductAvailable");
- }
-
- $productFamily = $product->getProductFamily();
- $quantityAsked = $quantityOrder;
-
- if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- if (!$quantityOrder) {
- $quantityAsked = $orderShop->getQuantityOrderByProduct($product, true);
- } else {
- $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
- }
-
- if ($checkCart) {
- $quantityAsked += $orderShop->getQuantityOrderByProduct($product, true);
- }
- }
-
- if (($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
- || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
-
- if (!$quantityOrder) {
- $quantityAsked = $orderShop->getQuantityOrderByProduct($product);
- }
-
- if ($checkCart) {
- $quantityAsked += $orderShop->getQuantityOrderByProduct($product);
- }
- }
-
- if ($product->getAvailableQuantityInherited() >= $quantityAsked
- || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isOneProductAvailableAddCart(array $products): bool
- {
- foreach ($products as $product) {
- if ($product->isAvailable(1, true)) {
- return true;
- }
- }
-
- return false;
- }
-
- public function isProductSaleStatusOn(ProductInterface $product)
- {
- if ($product->getProductFamily()->getSaleStatus() != 1) {
- return false;
- }
-
- $allCategoriesSalesOff = true;
- $unavailableSpecificDay = false;
-
- foreach ($product->getProductFamily()->getProductCategories() as $category) {
- if ($category->getParent()) {
- if ($category->getSaleStatus() && $category->getParent()->getSaleStatus()) {
- $allCategoriesSalesOff = false;
- }
- } else {
- if ($category->getSaleStatus()) {
- $allCategoriesSalesOff = false;
- }
- }
-
- // specific day
- // @TODO : spécifique pdl ?
- $displaySpecificDay = $category->getDisplaySpecificDay();
- if ($displaySpecificDay && $displaySpecificDay != date('N')) {
- $unavailableSpecificDay = true;
- }
- }
-
- if ($allCategoriesSalesOff) {
- return false;
- }
-
- if ($unavailableSpecificDay) {
- return false;
- }
-
- return true;
- }
-
-
-
- }
|