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.

OrderShopRepositoryQuery.php 12KB

3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace Lc\CaracoleBundle\Repository\Order;
  3. use Knp\Component\Pager\PaginatorInterface;
  4. use Lc\CaracoleBundle\Model\Address\AddressInterface;
  5. use Lc\CaracoleBundle\Model\Distribution\DistributionInterface;
  6. use Lc\CaracoleBundle\Model\Order\OrderPaymentInterface;
  7. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  8. use Lc\CaracoleBundle\Model\Reduction\ReductionCartInterface;
  9. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  10. use Lc\CaracoleBundle\Model\User\VisitorInterface;
  11. use Lc\CaracoleBundle\Repository\SectionRepositoryQueryTrait;
  12. use Lc\SovBundle\Model\User\UserInterface;
  13. use Lc\SovBundle\Repository\AbstractRepositoryQuery;
  14. use DateTime;
  15. class OrderShopRepositoryQuery extends AbstractRepositoryQuery
  16. {
  17. use SectionRepositoryQueryTrait;
  18. protected bool $isJoinProduct = false;
  19. protected bool $isJoinDistribution = false;
  20. protected bool $isJoinProductFamily = false;
  21. protected bool $isJoinOrderProducts = false;
  22. protected bool $isJoinOrderReductionCredits = false;
  23. protected bool $isJoinOrderReductionCarts = false;
  24. protected bool $isJoinOrderStatus = false;
  25. protected bool $isJoinMerchant = false;
  26. protected bool $isJoinUser = false;
  27. protected bool $isJoinComplementaryOrderShops = false;
  28. protected bool $isJoinDeliveryPointSale = false;
  29. protected bool $isJoinOrderPayment = false;
  30. protected bool $isFilteredByStatus = false;
  31. public function __construct(OrderShopRepository $repository, PaginatorInterface $paginator)
  32. {
  33. parent::__construct($repository, 'orderShop', $paginator);
  34. }
  35. public function selectSumStatTotalWithTax(): self
  36. {
  37. return $this
  38. ->select(
  39. 'SUM(DISTINCT(orderShop.statTotalWithTax)) as total'
  40. );
  41. }
  42. public function selectSumQuantityOrder(): self
  43. {
  44. $this->joinOrderProducts();
  45. return $this
  46. ->select(
  47. 'SUM(orderProducts.quantityOrder) as quantity'
  48. );
  49. }
  50. public function selectSum(): self
  51. {
  52. $this->joinProduct();
  53. $this->joinDistribution();
  54. return $this
  55. ->select(
  56. 'SUM(orderProducts.quantityOrder) as quantity, distribution.cycleNumber as cycleNumber, distribution.year as year , product.id as productId'
  57. );
  58. }
  59. public function selectCountUser(): self
  60. {
  61. return $this
  62. ->select('count(DISTINCT(orderShop.user)) as total');
  63. }
  64. // @TODO : nécessaire ?
  65. public function selectParam($select): self
  66. {
  67. return $this
  68. ->addSelect($select);
  69. }
  70. public function selectCount(): self
  71. {
  72. return $this
  73. ->select('count(DISTINCT(orderShop.id)) as total');
  74. }
  75. public function joinUser(): self
  76. {
  77. if (!$this->isJoinUser) {
  78. $this->isJoinUser = true;
  79. return $this
  80. ->leftJoin('.user', 'user');
  81. }
  82. return $this;
  83. }
  84. public function filterByUser(UserInterface $user): self
  85. {
  86. return $this
  87. ->andWhere('.user = :user')
  88. ->setParameter('user', $user);
  89. }
  90. public function filterByUserEmail(string $email): self
  91. {
  92. $this->joinUser();
  93. return $this
  94. ->andWhere('user.email LIKE :email')
  95. ->setParameter('email', $email);
  96. }
  97. public function filterByUserLastname(string $lastname): self
  98. {
  99. $this->joinUser();
  100. return $this
  101. ->andWhere('user.lastname LIKE :lastname')
  102. ->setParameter('lastname', $lastname);
  103. }
  104. public function filterByUserFirstname(string $firstname): self
  105. {
  106. $this->joinUser();
  107. return $this
  108. ->andWhere('user.firstname LIKE :firstname')
  109. ->setParameter('firstname', $firstname);
  110. }
  111. public function filterByAlias(array $status): self
  112. {
  113. $this->joinOrderStatus();
  114. return $this
  115. ->andWhere('orderStatus.alias IN (:alias)')
  116. ->setParameter('alias', $status);
  117. }
  118. public function filterByDistributions(array $distributions): self
  119. {
  120. return $this
  121. ->andWhere('.distribution IN(:distributions)')
  122. ->setParameter('distributions', $distributions);
  123. }
  124. public function filterByProducts(array $products): self
  125. {
  126. $this->joinOrderProducts();
  127. return $this
  128. ->andWhere('product.id IN(:products)')
  129. ->setParameter('products', $products);
  130. }
  131. public function filterByProduct(ProductInterface $product): self
  132. {
  133. $this->joinProduct();
  134. return $this
  135. ->andWhere('orderProducts.product = :product')
  136. ->setParameter('product', $product);
  137. }
  138. public function filterIsMerchantOnline(): self
  139. {
  140. $this->joinMerchant();
  141. return $this
  142. ->andWhere('merchant.status = :status')
  143. ->setParameter(':status', 1);
  144. }
  145. public function filterByDateStart(string $dateField, DateTime $dateStart): self
  146. {
  147. return $this
  148. ->andWhere('.' . $dateField . ' >= :dateStart')
  149. ->setParameter('dateStart', $dateStart);
  150. }
  151. public function filterByDateEnd(string $dateField, DateTime $dateEnd): self
  152. {
  153. return $this
  154. ->andWhere('.' . $dateField . ' <= :dateEnd')
  155. ->setParameter('dateEnd', $dateEnd);
  156. }
  157. public function filterByVisitor(VisitorInterface $visitor): self
  158. {
  159. return $this->andWhereEqual('visitor', $visitor);
  160. }
  161. public function filterByAddress(AddressInterface $address): self
  162. {
  163. return $this
  164. ->andWhere('.deliveryAddress = :address OR .invoiceAddress = :address')
  165. ->setParameter('address', $address);
  166. }
  167. public function filterByStatus(array $statusArray): self
  168. {
  169. $this->joinOrderStatus();
  170. // TODO: Voir pour faire mieux
  171. // On fait qu'une seule fois le filtre
  172. if (!$this->isFilteredByStatus) {
  173. $this->isFilteredByStatus = true;
  174. return $this
  175. ->andWhere('orderStatus.alias IN (:alias)')
  176. ->setParameter('alias', $statusArray);
  177. }
  178. return $this;
  179. }
  180. public function filterByReductionCredit(ReductionCreditInterface $reductionCredit): self
  181. {
  182. $this->joinOrderReductionCredits();
  183. return $this
  184. ->andWhere('orderReductionCredits.reductionCredit = :reductionCredit')
  185. ->setParameter('reductionCredit', $reductionCredit);
  186. }
  187. public function filterByReductionCart(ReductionCartInterface $reductionCart): self
  188. {
  189. $this->joinOrderReductionCarts();
  190. return $this
  191. ->andWhere('orderReductionCarts.reductionCart = :reductionCart')
  192. ->setParameter('reductionCart', $reductionCart);
  193. }
  194. public function filterByDistribution(DistributionInterface $distribution): self
  195. {
  196. return $this
  197. ->andWhere('.distribution = :distribution')
  198. ->setParameter('distribution', $distribution);
  199. }
  200. public function filterByMeanPayment(string $meanPayment): self
  201. {
  202. $this->joinOrderPayment();
  203. return $this
  204. ->andWhere('orderPayments.meanPayment = :meanPayment')
  205. ->setParameter('meanPayment', $meanPayment);
  206. }
  207. public function filterByDeliveryType(string $deliveryType): self
  208. {
  209. return $this
  210. ->andWhere('.deliveryType = :deliveryType')
  211. ->setParameter('deliveryType', $deliveryType);
  212. }
  213. public function filterIsNotComplementaryOrderShop(): self
  214. {
  215. return $this
  216. ->andWhere('.mainOrderShop = false OR .mainOrderShop IS NULL');
  217. }
  218. public function filterIsComplementaryOrderShop(): self
  219. {
  220. return $this
  221. ->andWhere('.mainOrderShop = true OR .mainOrderShop IS NOT NULL');
  222. }
  223. public function filterIsNullMainOrderShop(): self
  224. {
  225. return $this
  226. ->andWhere('.mainOrderShop IS NULL');
  227. }
  228. public function filterMinimumTomorrowDelivery(): self
  229. {
  230. return $this->andWhere('.deliveryDate > :today')->setParameter('today', (new DateTime())->setTime(23, 59));
  231. }
  232. public function selectOrderReductionCarts(): self
  233. {
  234. $this->joinOrderReductionCarts();
  235. return $this->addSelect('orderReductionCarts');
  236. }
  237. public function joinOrderProducts(bool $addSelect = false): self
  238. {
  239. if (!$this->isJoinOrderProducts) {
  240. $this->isJoinOrderProducts = true;
  241. $this->leftJoin('.orderProducts', 'orderProducts');
  242. if ($addSelect) {
  243. $this->addSelect('orderProducts');
  244. }
  245. }
  246. return $this;
  247. }
  248. public function joinProduct(bool $addSelect = false): self
  249. {
  250. $this->joinOrderProducts($addSelect);
  251. if (!$this->isJoinProduct) {
  252. $this->isJoinProduct = true;
  253. $this->leftJoin('orderProducts.product', 'product');
  254. if ($addSelect) {
  255. $this->addSelect('product');
  256. }
  257. }
  258. return $this;
  259. }
  260. public function joinDistribution(bool $addSelect = false): self
  261. {
  262. if (!$this->isJoinDistribution) {
  263. $this->isJoinDistribution = true;
  264. $this->leftJoin('.distribution', 'distribution');
  265. if ($addSelect) {
  266. $this->addSelect('distribution');
  267. }
  268. }
  269. return $this;
  270. }
  271. public function joinProductFamily(bool $addSelect = false): self
  272. {
  273. $this->joinProduct($addSelect);
  274. if (!$this->isJoinProductFamily) {
  275. $this->isJoinProductFamily = true;
  276. $this->leftJoin('product.productFamily', 'productFamily');
  277. if ($addSelect) {
  278. $this->addSelect('productFamily');
  279. }
  280. }
  281. return $this;
  282. }
  283. public function joinOrderReductionCredits(): self
  284. {
  285. if (!$this->isJoinOrderReductionCredits) {
  286. $this->isJoinOrderReductionCredits = true;
  287. return $this
  288. ->innerJoin('.orderReductionCredits', 'orderReductionCredits');
  289. }
  290. return $this;
  291. }
  292. public function joinOrderStatus(): self
  293. {
  294. if (!$this->isJoinOrderStatus) {
  295. $this->isJoinOrderStatus = true;
  296. return $this
  297. ->leftJoin('.orderStatus', 'orderStatus');
  298. }
  299. return $this;
  300. }
  301. public function joinOrderReductionCarts(): self
  302. {
  303. if (!$this->isJoinOrderReductionCarts) {
  304. $this->isJoinOrderReductionCarts = true;
  305. return $this
  306. ->leftJoin('.orderReductionCarts', 'orderReductionCarts');
  307. }
  308. return $this;
  309. }
  310. public function joinMerchant(): self
  311. {
  312. $this->joinSection();
  313. if (!$this->isJoinMerchant) {
  314. $this->isJoinMerchant = true;
  315. return $this
  316. ->leftJoin('s.merchant', 'merchant');
  317. }
  318. return $this;
  319. }
  320. public function joinComplementaryOrderShops(): self
  321. {
  322. if (!$this->isJoinComplementaryOrderShops) {
  323. $this->isJoinComplementaryOrderShops = true;
  324. return $this
  325. ->leftJoin('.complementaryOrderShops', 'complementaryOrderShops');
  326. }
  327. return $this;
  328. }
  329. public function joinDeliveryPointSale(): self
  330. {
  331. if (!$this->isJoinDeliveryPointSale) {
  332. $this->isJoinDeliveryPointSale = true;
  333. return $this
  334. ->leftJoin('.deliveryPointSale', 'deliveryPointSale');
  335. }
  336. return $this;
  337. }
  338. public function joinOrderPayment(): self
  339. {
  340. if (!$this->isJoinOrderPayment) {
  341. $this->isJoinOrderPayment = true;
  342. return $this
  343. ->leftJoin('.orderPayments', 'orderPayments');
  344. }
  345. return $this;
  346. }
  347. }