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.

482 lines
14KB

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