Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

367 rindas
13KB

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