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.

OrderShopSolver.php 12KB

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