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.

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