OrderUtilsStockTrait.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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(!$orderShop) {
  45. $orderShop = $this->getCartCurrent() ;
  46. }
  47. $productFamily = $product->getProductFamily() ;
  48. $quantityAsked = $quantityOrder;
  49. if($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  50. if(!$quantityOrder) {
  51. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true) ;
  52. }
  53. else {
  54. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  55. }
  56. if($checkCart) {
  57. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true) ;
  58. }
  59. }
  60. if(($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  61. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  62. if(!$quantityOrder) {
  63. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product) ;
  64. }
  65. if($checkCart) {
  66. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product) ;
  67. }
  68. }
  69. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  70. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  71. return true;
  72. }
  73. else {
  74. return false;
  75. }
  76. }
  77. public function isOneProductAvailableAddCart($products): bool
  78. {
  79. foreach($products as $product) {
  80. if($this->isProductAvailable($product, 1, true)) {
  81. return true ;
  82. }
  83. }
  84. return false ;
  85. }
  86. public function isOrderProductAvailable(OrderProductInterface $orderProduct)
  87. {
  88. return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder()) ;
  89. }
  90. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
  91. {
  92. $product = $orderProduct->getProduct() ;
  93. return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
  94. }
  95. public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
  96. {
  97. $quantity = 0 ;
  98. $productFamily = $product->getProductFamily() ;
  99. $behaviorCountStock = $productFamily->getBehaviorCountStock() ;
  100. if($orderShop) {
  101. foreach($orderShop->getOrderProducts() as $orderProduct) {
  102. if($orderProduct->getProduct()->getId() == $product->getId()
  103. || ( ($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  104. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  105. if($byWeight) {
  106. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $product->getUnitInherited()->getCoefficient()) ;
  107. }
  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. }