Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

383 lines
11KB

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