Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

439 lines
12KB

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