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.7KB

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