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

175 lines
8.2KB

  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. //TODO ne pas déduire des stocks les orderProduct marqué en relivraison
  11. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  12. switch ($orderProduct->getProduct()->getProductFamily()->getBehaviorCountStock()) {
  13. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE :
  14. //Disponibilité par unité de référence
  15. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  16. $newAvailability = $oldAvailability - ($orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getUnit()->getCoefficient()));
  17. $productFamily = $orderProduct->getProduct()->getProductFamily();
  18. $productFamily->setAvailableQuantity($newAvailability);
  19. $productFamily->setUpdatedBy($orderShop->getUser());
  20. $this->em->persist($productFamily);
  21. break;
  22. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY :
  23. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  24. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  25. $productFamily = $orderProduct->getProduct()->getProductFamily();
  26. $productFamily->setAvailableQuantity($newAvailability);
  27. $productFamily->setUpdatedBy($orderShop->getUser());
  28. $this->em->persist($productFamily);
  29. break;
  30. case ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT :
  31. $oldAvailability = $orderProduct->getProduct()->getAvailableQuantityInherited();
  32. $newAvailability = $oldAvailability - $orderProduct->getQuantityOrder();
  33. $product = $orderProduct->getProduct();
  34. $product->setAvailableQuantity($newAvailability);
  35. $product->setUpdatedBy($orderShop->getUser());
  36. $this->em->persist($product);
  37. break;
  38. }
  39. $this->em->flush();
  40. }
  41. }
  42. public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
  43. {
  44. if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1) {
  45. return false;
  46. }
  47. if (!$orderShop) {
  48. $orderShop = $this->getCartCurrent();
  49. }
  50. $productFamily = $product->getProductFamily();
  51. $quantityAsked = $quantityOrder;
  52. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  53. if (!$quantityOrder) {
  54. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
  55. } else {
  56. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  57. }
  58. if ($checkCart) {
  59. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true);
  60. }
  61. }
  62. if (($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  63. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  64. if (!$quantityOrder) {
  65. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
  66. }
  67. if ($checkCart) {
  68. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product);
  69. }
  70. }
  71. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  72. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }
  78. public function isOneProductAvailableAddCart($products): bool
  79. {
  80. foreach ($products as $product) {
  81. if ($this->isProductAvailable($product, 1, true)) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. public function isOrderProductAvailable(OrderProductInterface $orderProduct)
  88. {
  89. return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder());
  90. }
  91. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
  92. {
  93. $product = $orderProduct->getProduct();
  94. return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
  95. }
  96. public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
  97. {
  98. $quantity = 0;
  99. $productFamily = $product->getProductFamily();
  100. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  101. if ($orderShop) {
  102. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  103. if ($orderProduct->getProduct()->getId() == $product->getId()
  104. || (($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  105. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  106. if ($byWeight) {
  107. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  108. } else {
  109. $quantity += $orderProduct->getQuantityOrder();
  110. }
  111. }
  112. }
  113. }
  114. return $quantity;
  115. }
  116. public function getProductQuantityMaxAddCart($product)
  117. {
  118. $orderShop = $this->getCartCurrent();
  119. $productFamily = $product->getProductFamily();
  120. $byWeight = false;
  121. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  122. $byWeight = true;
  123. }
  124. return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight);
  125. }
  126. public function getProductQuantity($product)
  127. {
  128. $productFamily = $product->getProductFamily();
  129. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  130. return $product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient();
  131. } else {
  132. return 1;
  133. }
  134. }
  135. }