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.

330 line
9.1KB

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