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.

483 lines
17KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Order;
  3. use App\Entity\Order\OrderStatus;
  4. use Lc\CaracoleBundle\Builder\File\DocumentBuilder;
  5. use Lc\CaracoleBundle\Factory\File\DocumentFactory;
  6. use Lc\CaracoleBundle\Model\File\DocumentModel;
  7. use Lc\CaracoleBundle\Model\Order\OrderReductionCartInterface;
  8. use Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface;
  9. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  10. use Lc\CaracoleBundle\Repository\SectionStoreTrait;
  11. use Lc\CaracoleBundle\Resolver\Price\PriceResolver;
  12. use Lc\CaracoleBundle\Resolver\Reference\DocumentReferenceResolver;
  13. use Lc\SovBundle\Model\User\UserInterface;
  14. use Lc\SovBundle\Repository\AbstractStore;
  15. class OrderShopStore extends AbstractStore
  16. {
  17. use SectionStoreTrait;
  18. protected OrderShopRepositoryQuery $query;
  19. protected PriceResolver $priceResolver;
  20. protected DocumentReferenceResolver $documentReferenceResolver;
  21. protected DocumentBuilder $documentBuilder;
  22. public function __construct(
  23. OrderShopRepositoryQuery $query,
  24. PriceResolver $priceResolver,
  25. DocumentReferenceResolver $documentReferenceResolver,
  26. DocumentBuilder $documentBuilder
  27. ) {
  28. $this->query = $query;
  29. $this->priceResolver = $priceResolver;
  30. $this->documentReferenceResolver = $documentReferenceResolver;
  31. $this->documentBuilder = $documentBuilder;
  32. }
  33. public function getDatas(OrderShopInterface $orderShop = null): array
  34. {
  35. $data = [];
  36. if (is_null($orderShop)) {
  37. $orderShop = $this->getCartCurrent();
  38. }
  39. $data['order'] = $orderShop;
  40. if ($orderShop) {
  41. $data['count'] = $orderShop->countQuantities();
  42. $data['total_with_tax'] = $this->priceResolver->getTotalWithTax($orderShop);
  43. $data['order_products_by_category'] = $orderShop->getOrderProductsByParentCategory();
  44. $data['total_remaining_to_be_paid'] = $this->getTotalRemainingToBePaid($orderShop);
  45. }
  46. return $data;
  47. }
  48. public function getAsJsonObject(OrderShopInterface $orderShop): array
  49. {
  50. $data['id'] = $orderShop->getId();
  51. $data['user'] = $orderShop->getUser()->getSummary();
  52. $data['orderStatus'] = $orderShop->getOrderStatus()->__toString();
  53. $data['deliveryAddress'] = $orderShop->getDeliveryAddress()->getSummary();
  54. $data['invoiceAddress'] = $orderShop->getInvoiceAddress()->getSummary();
  55. $data['total'] = $this->priceResolver->getTotal($orderShop);
  56. $data['totalWithTax'] = $this->priceResolver->getTotalWithTax($orderShop);
  57. $data['totalWithTaxAndReduction'] = $this->priceResolver->getTotalWithTax($orderShop);
  58. $i = 0;
  59. foreach ($orderShop->getOrderProductsByParentCategory() as $labelCategory => $orderProducts) {
  60. foreach ($orderProducts as $orderProduct) {
  61. $data['orderProducts'][$i]['id'] = $orderProduct->getId();
  62. $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId();
  63. $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder();
  64. $data['orderProducts'][$i]['labelCategory'] = $labelCategory;
  65. $data['orderProducts'][$i]['title'] = $orderProduct->getTitle();
  66. $data['orderProducts'][$i]['price'] = $this->priceResolver->getPrice($orderProduct);
  67. $data['orderProducts'][$i]['priceWithTax'] = $this->priceResolver->getPriceWithTax($orderProduct);
  68. $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceResolver->getPriceWithTaxAndReduction(
  69. $orderProduct
  70. );
  71. $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder();
  72. $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceResolver->getTotalOrderProductsWithTaxAndReduction(
  73. array($orderProduct)
  74. );
  75. $i++;
  76. }
  77. }
  78. return $data;
  79. }
  80. public function groupOrderProductsByProductFamily(array $orderProducts): array
  81. {
  82. $orderProductsByProductFamily = [];
  83. foreach ($orderProducts as $orderProduct) {
  84. if ($orderProduct->getProduct() && $orderProduct->getProduct()->getProductFamily()) {
  85. $productFamily = $orderProduct->getProduct()->getProductFamily();
  86. if (!isset($orderProductsByProductFamily[$productFamily->getId()])) {
  87. $orderProductsByProductFamily[$productFamily->getId()] = [
  88. 'order_products' => [],
  89. 'total_quantity_weight' => 0,
  90. ];
  91. }
  92. $orderProductsByProductFamily[$productFamily->getId()]['order_products'][] = $orderProduct;
  93. $orderProductsByProductFamily[$productFamily->getId(
  94. )]['total_quantity_weight'] += ($orderProduct->getQuantityProduct() / $orderProduct->getUnit(
  95. )->getCoefficient()) * $orderProduct->getQuantityOrder();
  96. }
  97. }
  98. return $orderProductsByProductFamily;
  99. }
  100. // isOrderShopPositiveAmount
  101. public function isPositiveAmount(OrderShopInterface $orderShop)
  102. {
  103. return $this->priceResolver->getTotalWithTax($orderShop) >= 0;
  104. }
  105. public function isPaid(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false)
  106. {
  107. $totalOrderPayments = $this->getTotalOrderPayments($orderShop, $mergeComplementaryOrderShop);
  108. $totalOrder = $this->priceResolver->getTotalWithTax($orderShop);
  109. if ((abs($totalOrderPayments - $totalOrder) < 0.00001
  110. || $totalOrderPayments >= $totalOrder)
  111. && $totalOrder > 0) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. public function getTotalOrderPayments(OrderShopInterface $orderShop, $mergeComplementaryOrderShop = false): float
  118. {
  119. $totalAmount = floatval(0);
  120. foreach ($orderShop->getOrderPayments() as $orderPayment) {
  121. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  122. }
  123. if ($mergeComplementaryOrderShop) {
  124. foreach ($orderShop->getComplementaryOrderShops() as $complementaryOrderShop) {
  125. foreach ($complementaryOrderShop->getOrderPayments() as $orderPayment) {
  126. $totalAmount = $orderPayment->getAmount() + $totalAmount;
  127. }
  128. }
  129. }
  130. return $totalAmount;
  131. }
  132. public function getTotalRemainingToBePaid(OrderShopInterface $orderShop): float
  133. {
  134. return $this->priceResolver->getTotalWithTax($orderShop) - $this->getTotalOrderPayments($orderShop);
  135. }
  136. // isOrderShopPositiveAmountRemainingToBePaid
  137. public function isPositiveAmountRemainingToBePaid(OrderShopInterface $orderShop): bool
  138. {
  139. return $this->getTotalRemainingToBePaid($orderShop) > 0;
  140. }
  141. public function getCartByUserOrCreateIt($user)
  142. {
  143. $newOrderShop = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
  144. if ($newOrderShop === null) {
  145. $newOrderShop = $this->createOrderShop(
  146. array(
  147. 'user' => $user,
  148. 'merchant' => $this->merchantUtils->getMerchantUser()
  149. )
  150. );
  151. }
  152. return $newOrderShop;
  153. }
  154. public function isCartAllowToBeOrder(OrderShopInterface $orderShop)
  155. {
  156. return true;
  157. }
  158. /*
  159. public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null)
  160. {
  161. $paramsSearchOrderShop = [];
  162. $user = $this->security->getUser();
  163. $visitor = $this->userUtils->getVisitorCurrent();
  164. $orderShop = null;
  165. $orderShopUser = null;
  166. $orderShopVisitor = null;
  167. if ($user) {
  168. $orderShopUser = $this->orderShopRepo->findCartCurrent(
  169. [
  170. 'user' => $user
  171. ]
  172. );
  173. }
  174. if ($visitor) {
  175. $orderShopVisitor = $this->orderShopRepo->findCartCurrent(
  176. [
  177. 'visitor' => $visitor
  178. ]
  179. );
  180. }
  181. if ($orderShopUser || $orderShopVisitor) {
  182. // merge
  183. if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor
  184. && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())
  185. && $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) {
  186. $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor);
  187. $this->utils->addFlash(
  188. 'success',
  189. "Votre panier visiteur vient d'être fusionné avec votre panier client."
  190. );
  191. } else {
  192. $orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor;
  193. }
  194. // set user
  195. if ($orderShop && $user && !$orderShop->getUser()) {
  196. $orderShop->setUser($user);
  197. $orderShop->setVisitor(null);
  198. $this->em->persist($orderShop);
  199. $this->em->flush();
  200. }
  201. }
  202. return $orderShop;
  203. }*/
  204. public function countValidOrderWithReductionCredit(
  205. OrderReductionCreditInterface $reductionCredit,
  206. UserInterface $user = null
  207. ): string {
  208. $query = $this->query->create();
  209. if ($user) {
  210. $query->filterByUser($user);
  211. }
  212. $query
  213. ->selectCount()
  214. ->filterByReductionCredit($reductionCredit)
  215. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  216. ->filterBySection($this->section);
  217. return $query->count();
  218. }
  219. public function countValidOrderWithReductionCart(OrderReductionCartInterface $reductionCart): string
  220. {
  221. $query = $this->query->create();
  222. $query
  223. ->selectCount()
  224. ->filterByReductionCart($reductionCart)
  225. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  226. ->filterBySection($this->section);
  227. return $query->count();
  228. }
  229. public function countValidOrderWithReductionCartPerUser(
  230. OrderReductionCartInterface $reductionCart,
  231. UserInterface $user
  232. ): string {
  233. $query = $this->query->create();
  234. $query
  235. ->selectCount()
  236. ->filterByUser($user)
  237. ->filterByReductionCart($reductionCart)
  238. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  239. ->filterBySection($this->section);
  240. return $query->count();
  241. }
  242. //findCartCurrent
  243. public function getCartCurrent(array $params): ?OrderShopInterface
  244. {
  245. $query = $this->query->create();
  246. if (isset($params['user'])) {
  247. $query
  248. ->filterByUser($params['user']);
  249. }
  250. if (isset($params['visitor'])) {
  251. $query
  252. ->filterByVisitor($params['visitor']);
  253. }
  254. $query
  255. ->selectOrderReductionCarts()
  256. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  257. ->filterBySection($this->section);
  258. $results = $query->find();
  259. if ($results) {
  260. return $results[0];
  261. }
  262. return null;
  263. }
  264. //findLastOrderValidOfWeek
  265. public function getOneLastOrderValidOfWeek(int $weekNumber): ?OrderShopInterface
  266. {
  267. $query = $this->query->create();
  268. $query
  269. ->filterByWeekNumber($weekNumber)
  270. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  271. ->filterIsNotMainOrderShop()
  272. ->orderBy('.weekId', 'DESC')
  273. ->filterBySection($this->section);
  274. return $query->findOne();
  275. }
  276. //findLastOrderValid
  277. public function getOneLastOrderValid(): ?OrderShopInterface
  278. {
  279. $query = $this->query->create();
  280. $query
  281. ->filterByStatus(OrderStatus::$statusAliasAsValid)
  282. ->filterIsNotMainOrderShop()
  283. ->orderBy('.idValidOrder', 'DESC')
  284. ->filterBySection($this->section);
  285. return $query->findOne();
  286. }
  287. //TODO Fonction à tester
  288. // findAllBy
  289. public function getAllBy(array $params = [])
  290. {
  291. $query = $this->query->create();
  292. if (isset($params['section'])) {
  293. $query->filterBySection($params['section']);
  294. } else {
  295. $query->filterBySection($this->section);
  296. }
  297. if (isset($params['count']) && $params['count']) {
  298. $query->selectCount();
  299. }
  300. if (isset($params['select'])) {
  301. $query->selectParam($params['select']);
  302. }
  303. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  304. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  305. }
  306. if (isset($params['dateStart'])) {
  307. $query->filterByDateStart($params['dateField'], $params['dateStart']);
  308. }
  309. if (isset($params['dateEnd'])) {
  310. $query->filterByDateEnd($params['dateField'], $params['dateEnd']);
  311. }
  312. if (isset($params['weekNumber'])) {
  313. $query->filterByWeekNumber($params['weekNumber']);
  314. }
  315. if (isset($params['isCart'])) {
  316. $query->filterByStatus(OrderStatus::$statusAliasAsCart);
  317. }
  318. if (isset($params['isValid'])) {
  319. $query->filterByStatus(OrderStatus::$statusAliasAsValid);
  320. }
  321. if (isset($params['isWaitingDelivery'])) {
  322. $query->filterByStatus(OrderStatus::$statusAliasWaitingDelivery);
  323. }
  324. if (isset($params['orderStatus'])) {
  325. $query->filterByStatus($params['orderStatus']);
  326. }
  327. if (isset($params['user'])) {
  328. $query->filterByUser($params['user']);
  329. }
  330. if (isset($params['address'])) {
  331. $query->filterByAddress($params['address']);
  332. }
  333. if (isset($params['weekDeliveryTrucks'])) {
  334. $query->filterByWeekDeliveryTruck($params['weekDeliveryTrucks']);
  335. }
  336. if (isset($params['estimatedDeliveryDateTime'])) {
  337. $date = clone $params['estimatedDeliveryDateTime'];
  338. $query
  339. ->filterByEstimatedDeliveryDateStart($date->format('Y-m-d 00:00:00'))
  340. ->filterByEstimatedDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
  341. }
  342. if (isset($params['deliveryDate'])) {
  343. $date = clone $params['deliveryDate'];
  344. $query
  345. ->filterByDeliveryDateStart($date->format('Y-m-d 00:00:00'))
  346. ->filterByDeliveryDateEnd($date->modify('+1 day')->format('Y-m-d 00:00:00'));
  347. }
  348. if (isset($params['mergeComplementaryOrderShops'])) {
  349. //TODO jointure peut être pas utile
  350. $query
  351. ->joinComplementaryOrderShops();
  352. }
  353. if (isset($params['excludeComplementaryOrderShops']) || isset($params['mergeComplementaryOrderShops'])) {
  354. $query->filterIsNullMainOrderShop();
  355. }
  356. if (isset($params['isCircuit'])) {
  357. $query->filterIsNullDeliveryPointSale();
  358. }
  359. if (isset($params['isDepository'])) {
  360. $query->filterIsNotNullDeliveryPointSale();
  361. }
  362. if (isset($params['isOffCircuit'])) {
  363. $query->filterIsPointSale('devAliasHorsTournee');
  364. }
  365. if (isset($params['isGiftVoucher'])) {
  366. $query->filterIsPointSale('devAliasGiftVoucher');
  367. }
  368. if (isset($params['deliveryAvailability'])) {
  369. $deliveryAvailability = $params['deliveryAvailability'];
  370. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  371. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  372. if ($deliveryAvailabilityZone) {
  373. $query->filterByAvailabilityPointZone($deliveryAvailabilityZone);
  374. }
  375. if ($deliveryAvailabilityPointSale) {
  376. $query->filterByAvailabilityPointZone($deliveryAvailabilityPointSale);
  377. }
  378. } else {
  379. $query->joinDeliverySlotZone();
  380. $query->joinDeliverySlotPointSale();
  381. }
  382. if (isset($params['orderBy'])) {
  383. $query->orderBy(
  384. $params['orderBy'],
  385. isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC'
  386. );
  387. } else {
  388. $query->orderBy('.id', 'DESC');
  389. }
  390. if (isset($params['groupBy'])) {
  391. $query->groupBy($params['groupBy']);
  392. }
  393. if (isset($params['count']) && $params['count']) {
  394. return $query->count();
  395. }
  396. return $query->find();
  397. }
  398. }