Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

197 lines
9.6KB

  1. <?php
  2. namespace Lc\ShopBundle\Services\Order;
  3. use Lc\ShopBundle\Context\OrderProductInterface;
  4. use Lc\ShopBundle\Model\Product;
  5. use Lc\ShopBundle\Model\ProductFamily;
  6. trait OrderUtilsStockTrait
  7. {
  8. public function deductAvailabilityProduct(\Lc\ShopBundle\Model\OrderShop $orderShop)
  9. {
  10. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  11. //Si ce n'esrt pas une relivraison OU si c'est une relivraison + relivraison + ce n'est pas une erruer producteur
  12. if (!$orderProduct->isRedelivery() || ($orderProduct->isRedelivery() && $orderProduct->isRedeliverySupplierOrder() && !$orderProduct->isRedeliverySupplierMistake())) {
  13. switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
  14. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  15. //Disponibilité par unité de référence
  16. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  17. $newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()));
  18. $productFamily = $orderProduct->getProduct()->getProductFamily();
  19. $productFamily->setAvailableQuantity($newAvailability);
  20. $productFamily->setUpdatedBy($orderShop->getUser());
  21. $this->em->persist($productFamily);
  22. break;
  23. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  24. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  25. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  26. $productFamily = $orderProduct->getProduct()->getProductFamily();
  27. $productFamily->setAvailableQuantity($newAvailability);
  28. $productFamily->setUpdatedBy($orderShop->getUser());
  29. $this->em->persist($productFamily);
  30. break;
  31. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  32. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  33. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  34. $product = $orderProduct->getProduct();
  35. $product->setAvailableQuantity($newAvailability);
  36. $product->setUpdatedBy($orderShop->getUser());
  37. $this->em->persist($product);
  38. break;
  39. }
  40. $this->em->flush();
  41. }
  42. }
  43. }
  44. public function isProductSaleStatusOn(Product $product)
  45. {
  46. if($product->getProductFamily()->getSaleStatus()!=1) {
  47. return false ;
  48. }
  49. $allCategoriesSalesOff = true;
  50. foreach ($product->getProductFamily()->getProductCategories() as $category){
  51. if($category->getParent()) {
  52. if($category->getSaleStatus() && $category->getParent()->getSaleStatus())
  53. $allCategoriesSalesOff = false;
  54. }
  55. else {
  56. if($category->getSaleStatus()) $allCategoriesSalesOff = false;
  57. }
  58. }
  59. if($allCategoriesSalesOff) return false;
  60. return true ;
  61. }
  62. public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
  63. {
  64. if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1 || !$this->isProductSaleStatusOn($product)) {
  65. return false;
  66. }
  67. if (!$orderShop) {
  68. $orderShop = $this->getCartCurrent();
  69. }
  70. $productFamily = $product->getProductFamily();
  71. $quantityAsked = $quantityOrder;
  72. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  73. if (!$quantityOrder) {
  74. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
  75. } else {
  76. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  77. }
  78. if ($checkCart) {
  79. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true);
  80. }
  81. }
  82. if (($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  83. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  84. if (!$quantityOrder) {
  85. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
  86. }
  87. if ($checkCart) {
  88. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product);
  89. }
  90. }
  91. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  92. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. public function isOneProductAvailableAddCart($products): bool
  99. {
  100. foreach ($products as $product) {
  101. if ($this->isProductAvailable($product, 1, true)) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. public function isOrderProductAvailable(OrderProductInterface $orderProduct)
  108. {
  109. return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder());
  110. }
  111. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
  112. {
  113. $product = $orderProduct->getProduct();
  114. return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
  115. }
  116. public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
  117. {
  118. $quantity = 0;
  119. $productFamily = $product->getProductFamily();
  120. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  121. if ($orderShop) {
  122. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  123. if ($orderProduct->getProduct()->getId() == $product->getId()
  124. || (($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  125. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  126. if ($byWeight) {
  127. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  128. } else {
  129. $quantity += $orderProduct->getQuantityOrder();
  130. }
  131. }
  132. }
  133. }
  134. return $quantity;
  135. }
  136. public function getProductQuantityMaxAddCart($product)
  137. {
  138. $orderShop = $this->getCartCurrent();
  139. $productFamily = $product->getProductFamily();
  140. $byWeight = false;
  141. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  142. $byWeight = true;
  143. }
  144. return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight);
  145. }
  146. public function getProductQuantity($product)
  147. {
  148. $productFamily = $product->getProductFamily();
  149. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  150. return $product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient();
  151. } else {
  152. return 1;
  153. }
  154. }
  155. }