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.

437 lines
12KB

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