Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

OrderUtilsStockTrait.php 7.3KB

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