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.

459 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. $this->joinDistribution();
  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 joinUser(): self
  78. {
  79. if (!$this->isJoinUser) {
  80. $this->isJoinUser = true;
  81. return $this
  82. ->leftJoin('.user', 'user');
  83. }
  84. return $this;
  85. }
  86. public function filterByUser(UserInterface $user): self
  87. {
  88. return $this
  89. ->andWhere('.user = :user')
  90. ->setParameter('user', $user);
  91. }
  92. public function filterByUserEmail(string $email): self
  93. {
  94. $this->joinUser();
  95. return $this
  96. ->andWhere('user.email LIKE :email')
  97. ->setParameter('email', $email);
  98. }
  99. public function filterByUserLastname(string $lastname): self
  100. {
  101. $this->joinUser();
  102. return $this
  103. ->andWhere('user.lastname LIKE :lastname')
  104. ->setParameter('lastname', $lastname);
  105. }
  106. public function filterByUserFirstname(string $firstname): self
  107. {
  108. $this->joinUser();
  109. return $this
  110. ->andWhere('user.firstname LIKE :firstname')
  111. ->setParameter('firstname', $firstname);
  112. }
  113. public function filterByAlias(array $status): self
  114. {
  115. $this->joinOrderStatus();
  116. return $this
  117. ->andWhere('orderStatus.alias IN (:alias)')
  118. ->setParameter('alias', $status);
  119. }
  120. public function filterByDistributionData(string $cycleType, int $cycleNumber, int $year){
  121. $this->joinDistribution();
  122. return $this
  123. ->andWhere('distribution.cycleType LIKE :cycleType')
  124. ->andWhere('distribution.cycleNumber LIKE :cycleNumber')
  125. ->andWhere('distribution.year LIKE :year')
  126. ->setParameter('cycleType', $cycleType)
  127. ->setParameter('cycleNumber', $cycleNumber)
  128. ->setParameter('year', $year);
  129. }
  130. public function filterByDistributions(array $distributionArray): self
  131. {
  132. return $this
  133. ->andWhere('.distribution IN (:distributions)')
  134. ->setParameter('distributions', $distributionArray);
  135. }
  136. public function filterByProducts(array $products): self
  137. {
  138. $this->joinOrderProducts();
  139. return $this
  140. ->andWhere('product.id IN(:products)')
  141. ->setParameter('products', $products);
  142. }
  143. public function filterByProduct(ProductInterface $product): self
  144. {
  145. $this->joinProduct();
  146. return $this
  147. ->andWhere('orderProducts.product = :product')
  148. ->setParameter('product', $product);
  149. }
  150. public function filterIsMerchantOnline(): self
  151. {
  152. $this->joinMerchant();
  153. return $this
  154. ->andWhere('merchant.status = :status')
  155. ->setParameter(':status', 1);
  156. }
  157. public function filterByDateStart(string $dateField, DateTime $dateStart): self
  158. {
  159. return $this
  160. ->andWhere('.' . $dateField . ' >= :dateStart')
  161. ->setParameter('dateStart', $dateStart);
  162. }
  163. public function filterByDateEnd(string $dateField, DateTime $dateEnd): self
  164. {
  165. return $this
  166. ->andWhere('.' . $dateField . ' <= :dateEnd')
  167. ->setParameter('dateEnd', $dateEnd);
  168. }
  169. public function filterByVisitor(VisitorInterface $visitor): self
  170. {
  171. return $this->andWhereEqual('visitor', $visitor);
  172. }
  173. public function filterByAddress(AddressInterface $address): self
  174. {
  175. return $this
  176. ->andWhere('.deliveryAddress = :address OR .invoiceAddress = :address')
  177. ->setParameter('address', $address);
  178. }
  179. public function filterByStatus(array $statusArray): self
  180. {
  181. $this->joinOrderStatus();
  182. // TODO: Voir pour faire mieux
  183. // On fait qu'une seule fois le filtre
  184. if (!$this->isFilteredByStatus) {
  185. $this->isFilteredByStatus = true;
  186. return $this
  187. ->andWhere('orderStatus.alias IN (:alias)')
  188. ->setParameter('alias', $statusArray);
  189. }
  190. return $this;
  191. }
  192. public function filterByReductionCredit(ReductionCreditInterface $reductionCredit): self
  193. {
  194. $this->joinOrderReductionCredits();
  195. return $this
  196. ->andWhere('orderReductionCredits.reductionCredit = :reductionCredit')
  197. ->setParameter('reductionCredit', $reductionCredit);
  198. }
  199. public function filterByReductionCart(ReductionCartInterface $reductionCart): self
  200. {
  201. $this->joinOrderReductionCarts();
  202. return $this
  203. ->andWhere('orderReductionCarts.reductionCart = :reductionCart')
  204. ->setParameter('reductionCart', $reductionCart);
  205. }
  206. public function filterByDistribution(DistributionInterface $distribution): self
  207. {
  208. return $this
  209. ->andWhere('.distribution = :distribution')
  210. ->setParameter('distribution', $distribution);
  211. }
  212. public function filterByMeanPayment(string $meanPayment): self
  213. {
  214. $this->joinOrderPayment();
  215. return $this
  216. ->andWhere('orderPayments.meanPayment = :meanPayment')
  217. ->setParameter('meanPayment', $meanPayment);
  218. }
  219. public function filterByDeliveryType(string $deliveryType): self
  220. {
  221. return $this
  222. ->andWhere('.deliveryType = :deliveryType')
  223. ->setParameter('deliveryType', $deliveryType);
  224. }
  225. public function filterIsNotComplementaryOrderShop(): self
  226. {
  227. return $this->andWhere('.mainOrderShop = false OR .mainOrderShop IS NULL');
  228. }
  229. public function filterIsComplementaryOrderShop(): self
  230. {
  231. return $this->andWhere('.mainOrderShop = true OR .mainOrderShop IS NOT NULL');
  232. }
  233. public function filterSectionMainOrderShop(SectionInterface $section)
  234. {
  235. $this->joinMainOrderShop();
  236. return $this
  237. ->andWhere('mainOrderShop.section = :sectionMainOrderShop')
  238. ->setParameter('sectionMainOrderShop', $section);
  239. }
  240. public function filterIsNullMainOrderShop(): self
  241. {
  242. return $this
  243. ->andWhere('.mainOrderShop IS NULL');
  244. }
  245. public function filterMinimumTomorrowDelivery(): self
  246. {
  247. return $this->andWhere('.deliveryDate > :today')->setParameter('today', (new DateTime())->setTime(23, 59));
  248. }
  249. public function filterHasOrderProducts()
  250. {
  251. return $this->andWhere('orderShop.orderProducts IS NOT EMPTY');
  252. }
  253. public function selectOrderReductionCarts(): self
  254. {
  255. $this->joinOrderReductionCarts();
  256. return $this->addSelect('orderReductionCarts');
  257. }
  258. public function joinOrderProducts(bool $addSelect = false): self
  259. {
  260. if (!$this->isJoinOrderProducts) {
  261. $this->isJoinOrderProducts = true;
  262. $this->leftJoin('.orderProducts', 'orderProducts');
  263. if ($addSelect) {
  264. $this->addSelect('orderProducts');
  265. }
  266. }
  267. return $this;
  268. }
  269. public function joinProduct(bool $addSelect = false): self
  270. {
  271. $this->joinOrderProducts($addSelect);
  272. if (!$this->isJoinProduct) {
  273. $this->isJoinProduct = true;
  274. $this->leftJoin('orderProducts.product', 'product');
  275. if ($addSelect) {
  276. $this->addSelect('product');
  277. }
  278. }
  279. return $this;
  280. }
  281. public function joinDistribution(bool $addSelect = false): self
  282. {
  283. if (!$this->isJoinDistribution) {
  284. $this->isJoinDistribution = true;
  285. $this->leftJoin('.distribution', 'distribution');
  286. if ($addSelect) {
  287. $this->addSelect('distribution');
  288. }
  289. }
  290. return $this;
  291. }
  292. public function joinProductFamily(bool $addSelect = false): self
  293. {
  294. $this->joinProduct($addSelect);
  295. if (!$this->isJoinProductFamily) {
  296. $this->isJoinProductFamily = true;
  297. $this->leftJoin('product.productFamily', 'productFamily');
  298. if ($addSelect) {
  299. $this->addSelect('productFamily');
  300. }
  301. }
  302. return $this;
  303. }
  304. public function joinOrderReductionCredits(): self
  305. {
  306. if (!$this->isJoinOrderReductionCredits) {
  307. $this->isJoinOrderReductionCredits = true;
  308. return $this
  309. ->innerJoin('.orderReductionCredits', 'orderReductionCredits');
  310. }
  311. return $this;
  312. }
  313. public function joinOrderStatus(): self
  314. {
  315. if (!$this->isJoinOrderStatus) {
  316. $this->isJoinOrderStatus = true;
  317. return $this
  318. ->leftJoin('.orderStatus', 'orderStatus');
  319. }
  320. return $this;
  321. }
  322. public function joinOrderReductionCarts(): self
  323. {
  324. if (!$this->isJoinOrderReductionCarts) {
  325. $this->isJoinOrderReductionCarts = true;
  326. return $this
  327. ->leftJoin('.orderReductionCarts', 'orderReductionCarts');
  328. }
  329. return $this;
  330. }
  331. public function joinMerchant(): self
  332. {
  333. $this->joinSection();
  334. if (!$this->isJoinMerchant) {
  335. $this->isJoinMerchant = true;
  336. return $this
  337. ->leftJoin('s.merchant', 'merchant');
  338. }
  339. return $this;
  340. }
  341. public function joinComplementaryOrderShops(): self
  342. {
  343. if (!$this->isJoinComplementaryOrderShops) {
  344. $this->isJoinComplementaryOrderShops = true;
  345. return $this
  346. ->leftJoin('.complementaryOrderShops', 'complementaryOrderShops');
  347. }
  348. return $this;
  349. }
  350. public function joinDeliveryPointSale(): self
  351. {
  352. if (!$this->isJoinDeliveryPointSale) {
  353. $this->isJoinDeliveryPointSale = true;
  354. return $this
  355. ->leftJoin('.deliveryPointSale', 'deliveryPointSale');
  356. }
  357. return $this;
  358. }
  359. public function joinOrderPayment(): self
  360. {
  361. if (!$this->isJoinOrderPayment) {
  362. $this->isJoinOrderPayment = true;
  363. return $this
  364. ->leftJoin('.orderPayments', 'orderPayments');
  365. }
  366. return $this;
  367. }
  368. public function joinMainOrderShop(): self
  369. {
  370. if (!$this->isJoinMainOrderShop) {
  371. $this->isJoinMainOrderShop = true;
  372. return $this
  373. ->leftJoin('.mainOrderShop', 'mainOrderShop');
  374. }
  375. return $this;
  376. }
  377. }