Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OrderShopSolver.php 15KB

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