Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

OrderUtilsStockTrait.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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) {
  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. }
  56. else {
  57. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  58. }
  59. if($checkCart) {
  60. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true) ;
  61. }
  62. }
  63. if(($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  64. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  65. if(!$quantityOrder) {
  66. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product) ;
  67. }
  68. if($checkCart) {
  69. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product) ;
  70. }
  71. }
  72. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  73. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  74. return true;
  75. }
  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. }
  111. else {
  112. $quantity += $orderProduct->getQuantityOrder() ;
  113. }
  114. }
  115. }
  116. }
  117. return $quantity ;
  118. }
  119. public function getProductQuantityMaxAddCart($product)
  120. {
  121. $orderShop = $this->getCartCurrent() ;
  122. $productFamily = $product->getProductFamily() ;
  123. $byWeight = false ;
  124. if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  125. $byWeight = true ;
  126. }
  127. return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight) ;
  128. }
  129. public function getProductQuantity($product) {
  130. $productFamily = $product->getProductFamily() ;
  131. if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  132. return $product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient() ;
  133. }
  134. else {
  135. return 1 ;
  136. }
  137. }
  138. }