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 14KB

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