You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrderUtilsStockTrait.php 10KB

4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 isProductSaleStatusOn(Product $product)
  45. {
  46. if($product->getProductFamily()->getSaleStatus()!=1) {
  47. return false ;
  48. }
  49. $allCategoriesSalesOff = true;
  50. $unavailableSpecificDay = false;
  51. foreach ($product->getProductFamily()->getProductCategories() as $category){
  52. if($category->getParent()) {
  53. if($category->getSaleStatus() && $category->getParent()->getSaleStatus())
  54. $allCategoriesSalesOff = false;
  55. }
  56. else {
  57. if($category->getSaleStatus()) {
  58. $allCategoriesSalesOff = false;
  59. }
  60. }
  61. // specific day
  62. $displaySpecificDay = $category->getDisplaySpecificDay() ;
  63. if($displaySpecificDay && $displaySpecificDay != date('N')) {
  64. $unavailableSpecificDay = true;
  65. }
  66. }
  67. if($allCategoriesSalesOff) {
  68. return false;
  69. }
  70. if($unavailableSpecificDay) {
  71. return false;
  72. }
  73. return true ;
  74. }
  75. public function isProductAvailable(Product $product, $quantityOrder = 0, $checkCart = false, $orderShop = null)
  76. {
  77. if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus() != 1 || !$this->isProductSaleStatusOn($product)) {
  78. return false;
  79. }
  80. if (!$orderShop) {
  81. $orderShop = $this->getCartCurrent();
  82. }
  83. $productFamily = $product->getProductFamily();
  84. $quantityAsked = $quantityOrder;
  85. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  86. if (!$quantityOrder) {
  87. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
  88. } else {
  89. $quantityAsked = ($product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient()) * $quantityOrder;
  90. }
  91. if ($checkCart) {
  92. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true);
  93. }
  94. }
  95. if (($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  96. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  97. if (!$quantityOrder) {
  98. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
  99. }
  100. if ($checkCart) {
  101. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product);
  102. }
  103. }
  104. if ($product->getAvailableQuantityInherited() >= $quantityAsked
  105. || $productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. public function isOneProductAvailableAddCart($products): bool
  112. {
  113. foreach ($products as $product) {
  114. if ($this->isProductAvailable($product, 1, true)) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. public function isOrderProductAvailable(OrderProductInterface $orderProduct)
  121. {
  122. return $this->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder());
  123. }
  124. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, $orderShop = null)
  125. {
  126. $product = $orderProduct->getProduct();
  127. return $this->isProductAvailable($product, $orderProduct->getQuantityOrder(), true, $orderShop);
  128. }
  129. public function getQuantityOrderByProduct($orderShop, $product, $byWeight = false)
  130. {
  131. $quantity = 0;
  132. $productFamily = $product->getProductFamily();
  133. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  134. if ($orderShop) {
  135. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  136. if ($orderProduct->getProduct()->getId() == $product->getId()
  137. || (($behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  138. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  139. if ($byWeight) {
  140. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct() / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  141. } else {
  142. $quantity += $orderProduct->getQuantityOrder();
  143. }
  144. }
  145. }
  146. }
  147. return $quantity;
  148. }
  149. public function getProductQuantityMaxAddCart($product)
  150. {
  151. $orderShop = $this->getCartCurrent();
  152. $productFamily = $product->getProductFamily();
  153. $byWeight = false;
  154. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  155. $byWeight = true;
  156. }
  157. return $product->getAvailableQuantityInherited() - $this->getQuantityOrderByProduct($orderShop, $product, $byWeight);
  158. }
  159. public function getProductQuantity($product)
  160. {
  161. $productFamily = $product->getProductFamily();
  162. if ($productFamily->getBehaviorCountStock() == ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  163. return $product->getQuantityInherited() / $product->getUnitInherited()->getCoefficient();
  164. } else {
  165. return 1;
  166. }
  167. }
  168. }