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.

471 lines
13KB

  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. }