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 13KB

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