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.

346 lines
9.6KB

  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\Reduction\ReductionCartInterface;
  7. use Lc\CaracoleBundle\Model\Reduction\ReductionCreditInterface;
  8. use Lc\CaracoleBundle\Model\User\VisitorInterface;
  9. use Lc\CaracoleBundle\Repository\SectionRepositoryQueryTrait;
  10. use Lc\SovBundle\Model\User\UserInterface;
  11. use Lc\SovBundle\Repository\AbstractRepositoryQuery;
  12. use DateTime;
  13. class OrderShopRepositoryQuery extends AbstractRepositoryQuery
  14. {
  15. use SectionRepositoryQueryTrait;
  16. protected bool $isJoinProduct = false;
  17. protected bool $isJoinDistribution = false;
  18. protected bool $isJoinProductFamily = false;
  19. protected bool $isJoinOrderProduct = false;
  20. protected bool $isJoinOrderReductionCredits = false;
  21. protected bool $isJoinOrderReductionCarts = false;
  22. protected bool $isJoinOrderStatus = false;
  23. protected bool $isJoinMerchant = false;
  24. protected bool $isJoinComplementaryOrderShops = false;
  25. protected bool $isJoinDeliveryPointSale = false;
  26. public function __construct(OrderShopRepository $repository, PaginatorInterface $paginator)
  27. {
  28. parent::__construct($repository, 'r', $paginator);
  29. }
  30. public function selectSumStatTotalWithTax(): self
  31. {
  32. return $this
  33. ->select(
  34. 'SUM(DISTINCT(r.statTotalWithTax)) as total'
  35. );
  36. }
  37. public function selectSumQuantityOrder(): self
  38. {
  39. $this->joinOrderProduct();
  40. return $this
  41. ->select(
  42. 'SUM(orderProduct.quantityOrder) as quantity'
  43. );
  44. }
  45. public function selectSum(): self
  46. {
  47. $this->joinProduct();
  48. $this->joinDistribution();
  49. return $this
  50. ->select(
  51. 'SUM(orderProduct.quantityOrder) as quantity, distribution.cycleNumber as cycleNumber, distribution.year as year , product.id as productId'
  52. );
  53. }
  54. public function selectCountUser(): self
  55. {
  56. return $this
  57. ->select('count(DISTINCT(r.user)) as total');
  58. }
  59. // @TODO : nécessaire ?
  60. public function selectParam($select): self
  61. {
  62. return $this
  63. ->addSelect($select);
  64. }
  65. public function selectCount(): self
  66. {
  67. return $this
  68. ->select('count(DISTINCT(r.id)) as total');
  69. }
  70. public function filterByUser(UserInterface $user): self
  71. {
  72. return $this
  73. ->andWhere('.user = :user')
  74. ->setParameter('user', $user);
  75. }
  76. public function filterByAlias(array $status): self
  77. {
  78. $this->joinOrderStatus();
  79. return $this
  80. ->andWhere('os.alias IN (:alias)')
  81. ->setParameter('alias', $status);
  82. }
  83. public function filterByDistributions(array $distributions): self
  84. {
  85. return $this
  86. ->andWhere('.distribution IN(:distributions)')
  87. ->setParameter('distributions', $distributions);
  88. }
  89. public function filterByProducts(array $products): self
  90. {
  91. $this->joinOrderProduct();
  92. return $this
  93. ->andWhere('product.id IN(:products)')
  94. ->setParameter('products', $products);
  95. }
  96. public function filterByProduct(int $productId): self
  97. {
  98. $this->joinProduct();
  99. return $this
  100. ->andWhere('orderProduct.product = :product')
  101. ->setParameter('product', $productId);
  102. }
  103. public function filterIsMerchantOnline(): self
  104. {
  105. $this->joinMerchant();
  106. return $this
  107. ->andWhere('merchant.status = :status')
  108. ->setParameter(':status', 1);
  109. }
  110. public function filterByDateStart(string $dateField, DateTime $dateStart): self
  111. {
  112. return $this
  113. ->andWhere('.' . $dateField . ' >= :dateStart')
  114. ->setParameter('dateStart', $dateStart);
  115. }
  116. public function filterByDateEnd(string $dateField, DateTime $dateEnd): self
  117. {
  118. return $this
  119. ->andWhere('.' . $dateField . ' <= :dateEnd')
  120. ->setParameter('dateEnd', $dateEnd);
  121. }
  122. public function filterByVisitor(VisitorInterface $visitor): self
  123. {
  124. return $this
  125. ->andWhere('.visitor = :visitor')
  126. ->setParameter('visitor', $visitor);
  127. }
  128. public function filterByAddress(AddressInterface $address): self
  129. {
  130. return $this
  131. ->andWhere('.deliveryAddress = :address OR .invoiceAddress = :address')
  132. ->setParameter('address', $address);
  133. }
  134. public function filterByStatus(array $statusArray): self
  135. {
  136. $this->joinOrderStatus();
  137. return $this
  138. ->andWhere('os.alias IN (:alias)')
  139. ->setParameter('alias', $statusArray);
  140. }
  141. public function filterByReductionCredit(ReductionCreditInterface $reductionCredit): self
  142. {
  143. $this->joinOrderReductionCredits();
  144. return $this
  145. ->andWhere('orc.reductionCredit = :reductionCredit')
  146. ->setParameter('reductionCredit', $reductionCredit);
  147. }
  148. public function filterByReductionCart(ReductionCartInterface $reductionCart): self
  149. {
  150. $this->joinOrderReductionCarts();
  151. return $this
  152. ->andWhere('orcart.reductionCart = :reductionCart')
  153. ->setParameter('reductionCart', $reductionCart);
  154. }
  155. public function filterByDistribution(DistributionInterface $distribution): self
  156. {
  157. return $this
  158. ->andWhere('.distribution = :distribution')
  159. ->setParameter('distribution', $distribution);
  160. }
  161. public function filterIsNotComplementaryOrderShop(): self
  162. {
  163. return $this
  164. ->andWhere('.mainOrderShop = false OR .mainOrderShop IS NULL');
  165. }
  166. public function filterIsNullMainOrderShop(): self
  167. {
  168. return $this
  169. ->andWhere('.mainOrderShop IS NULL');
  170. }
  171. public function filterMinimumTomorrowDelivery(): self
  172. {
  173. return $this->andWhere('.deliveryDate > :today')->setParameter('today', (new DateTime())->setTime(23, 59));
  174. }
  175. public function selectOrderReductionCarts(): self
  176. {
  177. $this->joinOrderReductionCarts();
  178. return $this->addSelect('orcart');
  179. }
  180. public function joinOrderProduct(bool $addSelect = false): self
  181. {
  182. if (!$this->isJoinOrderProduct) {
  183. $this->isJoinOrderProduct = true;
  184. $this->innerJoin('.orderProducts', 'orderProduct');
  185. if ($addSelect) {
  186. $this->addSelect('orderProduct');
  187. }
  188. }
  189. return $this;
  190. }
  191. public function joinProduct(bool $addSelect = false): self
  192. {
  193. $this->joinOrderProduct($addSelect);
  194. if (!$this->isJoinProduct) {
  195. $this->isJoinProduct = true;
  196. $this->leftJoin('orderProduct.product', 'product');
  197. if ($addSelect) {
  198. $this->addSelect('product');
  199. }
  200. }
  201. return $this;
  202. }
  203. public function joinDistribution(bool $addSelect = false): self
  204. {
  205. if (!$this->isJoinDistribution) {
  206. $this->isJoinDistribution = true;
  207. $this->leftJoin('.distribution', 'distribution');
  208. if ($addSelect) {
  209. $this->addSelect('distribution');
  210. }
  211. }
  212. return $this;
  213. }
  214. public function joinProductFamily(bool $addSelect = false): self
  215. {
  216. $this->joinProduct($addSelect);
  217. if (!$this->isJoinProductFamily) {
  218. $this->isJoinProductFamily = true;
  219. $this->leftJoin('product.productFamily', 'productFamily');
  220. if ($addSelect) {
  221. $this->addSelect('productFamily');
  222. }
  223. }
  224. return $this;
  225. }
  226. public function joinOrderReductionCredits(): self
  227. {
  228. if (!$this->isJoinOrderReductionCredits) {
  229. $this->isJoinOrderReductionCredits = true;
  230. return $this
  231. ->innerJoin('.orderReductionCredits', 'orc');
  232. }
  233. return $this;
  234. }
  235. public function joinOrderStatus(): self
  236. {
  237. if (!$this->isJoinOrderStatus) {
  238. $this->isJoinOrderStatus = true;
  239. return $this
  240. ->leftJoin('.orderStatus', 'os');
  241. }
  242. return $this;
  243. }
  244. public function joinOrderReductionCarts(): self
  245. {
  246. if (!$this->isJoinOrderReductionCarts) {
  247. $this->isJoinOrderReductionCarts = true;
  248. return $this
  249. ->leftJoin('.orderReductionCarts', 'orcart');
  250. }
  251. return $this;
  252. }
  253. public function joinMerchant(): self
  254. {
  255. $this->joinSection();
  256. if (!$this->isJoinMerchant) {
  257. $this->isJoinMerchant = true;
  258. return $this
  259. ->leftJoin('s.merchant', 'merchant');
  260. }
  261. return $this;
  262. }
  263. public function joinComplementaryOrderShops(): self
  264. {
  265. if (!$this->isJoinComplementaryOrderShops) {
  266. $this->isJoinComplementaryOrderShops = true;
  267. return $this
  268. ->leftJoin('.complementaryOrderShops', 'complementaryOrderShops');
  269. }
  270. return $this;
  271. }
  272. public function joinDeliveryPointSale(): self
  273. {
  274. if (!$this->isJoinDeliveryPointSale) {
  275. $this->isJoinDeliveryPointSale = true;
  276. return $this
  277. ->leftJoin('.deliveryPointSale', 'pointSale');
  278. }
  279. return $this;
  280. }
  281. }