Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

177 lines
8.7KB

  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 isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
  45. {
  46. if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1) {
  47. return false;
  48. }
  49. if (!$orderShop) {
  50. $orderShop = $this->getCartCurrent();
  51. }
  52. $productFamily = $product->getProductFamily();
  53. $quantityAsked = $quantityOrder;
  54. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  55. if (!$quantityOrder) {
  56. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
  57. } else {
  58. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  59. }
  60. if ($checkCart) {
  61. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true);
  62. }
  63. }
  64. if (($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  65. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  66. if (!$quantityOrder) {
  67. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
  68. }
  69. if ($checkCart) {
  70. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product);
  71. }
  72. }
  73. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  74. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80. public function isOneProductAvailableAddCart($products): bool
  81. {
  82. foreach ($products as $product) {
  83. if ($this->isProductAvailable($product, 1, true)) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. public function isOrderProductAvailable(OrderProductInterface $orderProduct)
  90. {
  91. return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder());
  92. }
  93. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
  94. {
  95. $product = $orderProduct->getProduct();
  96. return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
  97. }
  98. public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
  99. {
  100. $quantity = 0;
  101. $productFamily = $product->getProductFamily();
  102. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  103. if ($orderShop) {
  104. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  105. if ($orderProduct->getProduct()->getId() == $product->getId()
  106. || (($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  107. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  108. if ($byWeight) {
  109. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  110. } else {
  111. $quantity += $orderProduct->getQuantityOrder();
  112. }
  113. }
  114. }
  115. }
  116. return $quantity;
  117. }
  118. public function getProductQuantityMaxAddCart($product)
  119. {
  120. $orderShop = $this->getCartCurrent();
  121. $productFamily = $product->getProductFamily();
  122. $byWeight = false;
  123. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  124. $byWeight = true;
  125. }
  126. return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight);
  127. }
  128. public function getProductQuantity($product)
  129. {
  130. $productFamily = $product->getProductFamily();
  131. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  132. return $product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient();
  133. } else {
  134. return 1;
  135. }
  136. }
  137. }