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.

OrderShopSolver.php 13KB

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace Lc\CaracoleBundle\Solver\Order;
  3. use App\Entity\Order\OrderShop;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\CaracoleBundle\Model\File\DocumentInterface;
  6. use Lc\CaracoleBundle\Model\File\DocumentModel;
  7. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  8. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  9. use Lc\CaracoleBundle\Model\Order\OrderShopModel;
  10. use Lc\CaracoleBundle\Model\Order\OrderStatusInterface;
  11. use Lc\CaracoleBundle\Model\Order\OrderStatusModel;
  12. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  13. use Lc\CaracoleBundle\Model\Product\ProductFamilyModel;
  14. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  15. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  16. use Lc\CaracoleBundle\Solver\Product\ProductSolver;
  17. class OrderShopSolver
  18. {
  19. protected EntityManagerInterface $entityManager;
  20. protected ProductSolver $productSolver;
  21. public function __construct(
  22. EntityManagerInterface $entityManager,
  23. ProductSolver $productSolver
  24. ) {
  25. $this->entityManager = $entityManager;
  26. $this->productSolver = $productSolver;
  27. }
  28. public function countQuantities(OrderShopInterface $orderShop): int
  29. {
  30. return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts());
  31. }
  32. public function countQuantitiesByOrderProducts($orderProducts = []): int
  33. {
  34. $count = 0;
  35. foreach ($orderProducts as $orderProduct) {
  36. $count += $orderProduct->getQuantityOrder();
  37. }
  38. return $count;
  39. }
  40. public function getOrderProductsByParentCategory(OrderShopInterface $orderShop): array
  41. {
  42. $categoriesArray = [];
  43. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  44. $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories();
  45. $category = $productCategories[0]->getParentCategory();
  46. $labelCategory = $category->getTitle();
  47. if (!isset($categoriesArray[$labelCategory])) {
  48. $categoriesArray[$labelCategory] = [];
  49. }
  50. $categoriesArray[$labelCategory][] = $orderProduct;
  51. }
  52. return $categoriesArray;
  53. }
  54. // getOrderProductsByProductFamily
  55. public function getOrderProductsByProductFamily(
  56. OrderShopInterface $orderShop,
  57. ProductFamilyInterface $productFamily
  58. ): array {
  59. $arrayOrderProducts = [];
  60. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  61. if ($orderProduct->getProduct()->getProductFamily() == $productFamily) {
  62. $arrayOrderProducts[] = $orderProduct;
  63. }
  64. }
  65. return $arrayOrderProducts;
  66. }
  67. public function getQuantityOrderByProduct(
  68. OrderShopInterface $orderShop,
  69. ProductInterface $product,
  70. $byWeight = false
  71. ): int {
  72. $quantity = 0;
  73. $productFamily = $product->getProductFamily();
  74. $behaviorCountStock = $productFamily->getBehaviorCountStock();
  75. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  76. if ($orderProduct->getProduct()->getId() == $product->getId()
  77. || (($behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY || $behaviorCountStock == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE)
  78. && $orderProduct->getProduct()->getProductFamily()->getId() == $productFamily->getId())) {
  79. if ($byWeight) {
  80. $quantity += $orderProduct->getQuantityOrder() * ($orderProduct->getQuantityProduct(
  81. ) / $orderProduct->getProduct()->getUnitInherited()->getCoefficient());
  82. } else {
  83. $quantity += $orderProduct->getQuantityOrder();
  84. }
  85. }
  86. }
  87. return $quantity;
  88. }
  89. // isProductAvailable
  90. public function isProductAvailable(
  91. ProductInterface $product,
  92. $quantityOrder = 0,
  93. $checkCart = false,
  94. $orderShop = null
  95. ) {
  96. if ($product->getStatus() != 1 || $product->getProductFamily()->getStatus(
  97. ) != 1 || !$this->productSolver->isProductSaleStatusOn($product)) {
  98. return false;
  99. }
  100. if ($checkCart && !$orderShop) {
  101. throw new \Exception("Attention : définir le orderShop à l'endroit où est appelé isAvailable");
  102. }
  103. $productFamily = $product->getProductFamily();
  104. $quantityAsked = $quantityOrder;
  105. if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  106. if (!$quantityOrder) {
  107. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product, true);
  108. } else {
  109. $quantityAsked = ($this->productSolver->getQuantityInherited(
  110. $product
  111. ) / $this->productSolver->getUnitInherited($product)->getCoefficient(
  112. )) * $quantityOrder;
  113. }
  114. if ($checkCart) {
  115. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product, true);
  116. }
  117. }
  118. if (($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY
  119. || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_PRODUCT)) {
  120. if (!$quantityOrder) {
  121. $quantityAsked = $this->getQuantityOrderByProduct($orderShop, $product);
  122. }
  123. if ($checkCart) {
  124. $quantityAsked += $this->getQuantityOrderByProduct($orderShop, $product);
  125. }
  126. }
  127. if ($this->productSolver->getAvailableQuantityInherited($product) >= $quantityAsked
  128. || $productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_UNLIMITED) {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. }
  134. public function isOneProductAvailableAddCart(OrderShopInterface $orderShop, $products): bool
  135. {
  136. foreach ($products as $product) {
  137. if ($this->isProductAvailable($product, 1, true, $orderShop)) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. public function isOrderProductAvailableAddCart(OrderProductInterface $orderProduct, OrderShopInterface $orderShop)
  144. {
  145. return $this->isProductAvailable(
  146. $orderProduct->getProduct(),
  147. $orderProduct->getQuantityOrder(),
  148. true,
  149. $orderShop
  150. );
  151. }
  152. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  153. {
  154. $totalAmount = floatval(0);
  155. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  156. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  157. }
  158. if ($mergeComplementaryOrderShop) {
  159. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  160. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  161. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  162. }
  163. }
  164. }
  165. return $totalAmount;
  166. }
  167. public function getOrderStatusHistory(OrderShopInterface $orderShop, OrderStatusInterface $status)
  168. {
  169. $orderStatusHistories = $orderShop->getOrderStatusHistories();
  170. if (count($orderStatusHistories) > 0) {
  171. foreach ($orderStatusHistories as $orderStatusHistory) {
  172. if ($orderStatusHistory->getOrderStatus() === $status) {
  173. return $orderStatusHistory;
  174. }
  175. }
  176. }
  177. return null;
  178. }
  179. public function getDocumentInvoice(OrderShopInterface $orderShop): ?DocumentInterface
  180. {
  181. foreach ($orderShop->getDocuments() as $document) {
  182. if ($document->getType() == DocumentModel::TYPE_INVOICE) {
  183. return $document;
  184. }
  185. }
  186. return null;
  187. }
  188. public function isDeliveryHome(OrderShopInterface $orderShop): bool
  189. {
  190. return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_HOME;
  191. }
  192. public function isDeliveryPointSale(OrderShopInterface $orderShop): bool
  193. {
  194. return $orderShop->getDeliveryType() == OrderShopModel::DELIVERY_TYPE_POINTSALE;
  195. }
  196. public function isComplementaryOrderShop(OrderShopInterface $orderShop): bool
  197. {
  198. return (bool)$orderShop->getMainOrderShop();
  199. }
  200. public function mergeComplentaryOrderShops(
  201. OrderShopInterface $orderShop,
  202. bool $combineProducts = true
  203. ): OrderShopInterface {
  204. $this->entityManager->refresh($orderShop);
  205. if ($orderShop->getComplementaryOrderShops()) {
  206. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  207. foreach ($complementaryOrderShop->getOrderProducts() as $orderProductAdd) {
  208. $updated = false;
  209. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  210. if ($combineProducts && $orderProduct->getProduct()->getId() == $orderProductAdd->getProduct(
  211. )->getId()
  212. && (string)$orderProduct->getPrice() == (string)$orderProductAdd->getPrice()
  213. ) {
  214. $orderProduct->setUpdatedOnMergeComplementaryOrderShop(true);
  215. $orderProduct->setQuantityOrder(
  216. $orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()
  217. );
  218. $updated = true;
  219. }
  220. }
  221. if (!$updated) {
  222. $orderProductAdd->setOnMergeComplementaryOrderShop($complementaryOrderShop);
  223. $orderProductAdd->setCreatedOnMergeComplementaryOrderShop(true);
  224. $orderShop->addOrderProduct($orderProductAdd);
  225. }
  226. }
  227. }
  228. }
  229. return $orderShop;
  230. }
  231. public function isReductionCreditAddedToOrder(
  232. OrderShopInterface $orderShop,
  233. ReductionCreditInterface $reductionCredit
  234. ) {
  235. foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
  236. if ($orderReductionCredit->getReductionCredit() == $reductionCredit) {
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. public function hasOrderProductAlreadyInCart(
  243. OrderShopInterface $orderShop,
  244. OrderProductInterface $orderProductTest
  245. ): ?OrderProductInterface {
  246. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  247. if ($orderProduct->getProduct() == $orderProductTest->getProduct()) {
  248. return $orderProduct;
  249. }
  250. }
  251. return null;
  252. }
  253. public function isValid(OrderShopInterface $orderShop): bool
  254. {
  255. if ($orderShop->getOrderStatus() && in_array(
  256. $orderShop->getOrderStatus()->getAlias(),
  257. OrderStatusModel::$statusAliasAsValid
  258. ) > 0) {
  259. return true;
  260. }
  261. return false;
  262. }
  263. public function isCart(OrderShopInterface $orderShop): bool
  264. {
  265. if ($orderShop->getOrderStatus() && in_array(
  266. $orderShop->getOrderStatus()->getAlias(),
  267. OrderStatusModel::$statusAliasAsCart
  268. ) > 0) {
  269. return true;
  270. }
  271. return false;
  272. }
  273. public function isCartAllowToBeOrder(OrderShopInterface $orderShop): bool
  274. {
  275. return true;
  276. }
  277. // getProductQuantityMaxAddCart
  278. public function getProductQuantityMaxAddCart(ProductInterface $product, OrderShopInterface $orderShop)
  279. {
  280. $productFamily = $product->getProductFamily();
  281. $byWeight = false;
  282. if ($productFamily->getBehaviorCountStock() == ProductFamilyModel::BEHAVIOR_COUNT_STOCK_BY_MEASURE) {
  283. $byWeight = true;
  284. }
  285. return $this->productSolver->getAvailableQuantityInherited($product) - $this->getQuantityOrderByProduct(
  286. $orderShop,
  287. $product,
  288. $byWeight
  289. );
  290. }
  291. public function hasMakeAChoiceAboutComplementaryOrder(OrderShop $orderShop): bool
  292. {
  293. return $orderShop->getMainOrderShop() || $orderShop->getDeclineComplementaryOrderShop();
  294. }
  295. }