|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
-
- namespace Lc\ShopBundle\Services\Order;
-
-
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Model\Product;
- use Lc\ShopBundle\Model\ProductFamily;
-
- trait OrderUtilsStockTrait
- {
- public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
- {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- //Si ce n'esrt pas une relivraison OU si c'est une relivraison + relivraison + ce n'est pas une erruer producteur
- if (!$orderProduct->isRedelivery() || ($orderProduct->isRedelivery() && $orderProduct->isRedeliverySupplierOrder() && !$orderProduct->isRedeliverySupplierMistake())) {
- switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
- case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
-
- //Disponibilité par unité de référence
- $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
- $newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()));
-
- $productFamily = $orderProduct->getProduct()->getProductFamily();
- $productFamily->setAvailableQuantity($newAvailability);
- $productFamily->setUpdatedBy($orderShop->getUser());
-
- $this->em->persist($productFamily);
-
- break;
- case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
-
- $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
- $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
-
- $productFamily = $orderProduct->getProduct()->getProductFamily();
- $productFamily->setAvailableQuantity($newAvailability);
- $productFamily->setUpdatedBy($orderShop->getUser());
-
- $this->em->persist($productFamily);
-
- break;
- case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
- $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
- $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
-
- $product = $orderProduct->getProduct();
- $product->setAvailableQuantity($newAvailability);
- $product->setUpdatedBy($orderShop->getUser());
-
- $this->em->persist($product);
-
- break;
- }
-
- $this->em->flush();
- }
- }
- }
-
-
- public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
- {
- if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1) {
- return false;
- }
-
- if (!$orderShop) {
- $orderShop = $this->getCartCurrent();
- }
- $productFamily = $product->getProductFamily();
- $quantityAsked = $quantityOrder;
-
- if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- if (!$quantityOrder) {
- $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
- } else {
- $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
- }
-
- if ($checkCart) {
- $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true);
- }
- }
-
- if (($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
- || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
-
- if (!$quantityOrder) {
- $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
- }
-
- if ($checkCart) {
- $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product);
- }
- }
-
- if ($product->getAvailableQuantityInherited() >= $quantityAsked
- || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
- return true;
- } else {
- return false;
- }
- }
-
- public function isOneProductAvailableAddCart($products): bool
- {
- foreach ($products as $product) {
- if ($this->isProductAvailable($product, 1, true)) {
- return true;
- }
- }
-
- return false;
- }
-
- public function isOrderProductAvailable(OrderProductInterface $orderProduct)
- {
- return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder());
- }
-
- public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
- {
- $product = $orderProduct->getProduct();
- return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
- }
-
- public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
- {
- $quantity = 0;
- $productFamily = $product->getProductFamily();
- $behaviorCountStock = $productFamily->getBehaviorCountStock();
-
- if ($orderShop) {
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- if ($orderProduct->getProduct()->getId() == $product->getId()
- || (($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
- && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
-
- if ($byWeight) {
- $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
- } else {
- $quantity += $orderProduct->getQuantityOrder();
- }
- }
- }
- }
-
- return $quantity;
- }
-
- public function getProductQuantityMaxAddCart($product)
- {
- $orderShop = $this->getCartCurrent();
- $productFamily = $product->getProductFamily();
-
- $byWeight = false;
- if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- $byWeight = true;
- }
-
- return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight);
- }
-
- public function getProductQuantity($product)
- {
-
- $productFamily = $product->getProductFamily();
-
- if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
- return $product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient();
- } else {
- return 1;
- }
- }
- }
|