Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

352 lines
9.9KB

  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 filterMinimumTomorrowDelivery(): self
  178. {
  179. return $this->andWhere('.deliveryDate > :today')->setParameter('today', (new DateTime())->setTime(23, 59));
  180. }
  181. public function selectOrderReductionCarts(): self
  182. {
  183. $this->joinOrderReductionCarts();
  184. return $this->addSelect('orcart');
  185. }
  186. public function joinOrderProduct(bool $addSelect = false): self
  187. {
  188. if (!$this->isJoinOrderProduct) {
  189. $this->isJoinOrderProduct = true;
  190. $this->innerJoin('.orderProducts', 'orderProduct');
  191. if ($addSelect) {
  192. $this->addSelect('orderProduct');
  193. }
  194. }
  195. return $this;
  196. }
  197. public function joinProduct(bool $addSelect = false): self
  198. {
  199. $this->joinOrderProduct($addSelect);
  200. if (!$this->isJoinProduct) {
  201. $this->isJoinProduct = true;
  202. $this->leftJoin('orderProduct.product', 'product');
  203. if ($addSelect) {
  204. $this->addSelect('product');
  205. }
  206. }
  207. return $this;
  208. }
  209. public function joinDistribution(bool $addSelect = false): self
  210. {
  211. if (!$this->isJoinDistribution) {
  212. $this->isJoinDistribution = true;
  213. $this->leftJoin('.distribution', 'distribution');
  214. if ($addSelect) {
  215. $this->addSelect('distribution');
  216. }
  217. }
  218. return $this;
  219. }
  220. public function joinProductFamily(bool $addSelect = false): self
  221. {
  222. $this->joinProduct($addSelect);
  223. if (!$this->isJoinProductFamily) {
  224. $this->isJoinProductFamily = true;
  225. $this->leftJoin('product.productFamily', 'productFamily');
  226. if ($addSelect) {
  227. $this->addSelect('productFamily');
  228. }
  229. }
  230. return $this;
  231. }
  232. public function joinOrderReductionCredits(): self
  233. {
  234. if (!$this->isJoinOrderReductionCredits) {
  235. $this->isJoinOrderReductionCredits = true;
  236. return $this
  237. ->innerJoin('.orderReductionCredits', 'orc');
  238. }
  239. return $this;
  240. }
  241. public function joinOrderStatus(): self
  242. {
  243. if (!$this->isJoinOrderStatus) {
  244. $this->isJoinOrderStatus = true;
  245. return $this
  246. ->leftJoin('.orderStatus', 'os');
  247. }
  248. return $this;
  249. }
  250. public function joinOrderReductionCarts(): self
  251. {
  252. if (!$this->isJoinOrderReductionCarts) {
  253. $this->isJoinOrderReductionCarts = true;
  254. return $this
  255. ->leftJoin('.orderReductionCarts', 'orcart');
  256. }
  257. return $this;
  258. }
  259. public function joinMerchant(): self
  260. {
  261. $this->joinSection();
  262. if (!$this->isJoinMerchant) {
  263. $this->isJoinMerchant = true;
  264. return $this
  265. ->leftJoin('s.merchant', 'merchant');
  266. }
  267. return $this;
  268. }
  269. public function joinComplementaryOrderShops(): self
  270. {
  271. if (!$this->isJoinComplementaryOrderShops) {
  272. $this->isJoinComplementaryOrderShops = true;
  273. return $this
  274. ->leftJoin('.complementaryOrderShops', 'complementaryOrderShops');
  275. }
  276. return $this;
  277. }
  278. public function joinDeliveryPointSale(): self
  279. {
  280. if (!$this->isJoinDeliveryPointSale) {
  281. $this->isJoinDeliveryPointSale = true;
  282. return $this
  283. ->leftJoin('.deliveryPointSale', 'pointSale');
  284. }
  285. return $this;
  286. }
  287. }