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.

355 lines
11KB

  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\Order\OrderReductionCartInterface;
  9. use Lc\CaracoleBundle\Model\Order\OrderReductionCreditInterface;
  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 $isJoinOrderReductionCredits = false;
  19. protected $isJoinOrderReductionCarts = false;
  20. protected $isJoinOrderStatus = false;
  21. protected $isJoinComplementaryOrderShops = false;
  22. protected $isJoinDeliveryPointSale = false;
  23. protected $isJoinDeliveryAvailabilityZone = false;
  24. protected $isJoinDeliverySlotZone = false;
  25. protected $isJoinDeliveryAvailabilityPointSale = false;
  26. protected $isJoinDeliverySlotPointSale = false;
  27. protected $isHorsTournee = false;
  28. protected $isGiftVoucher = false;
  29. public function __construct(OrderShopRepository $repository, PaginatorInterface $paginator)
  30. {
  31. parent::__construct($repository, 'r', $paginator);
  32. }
  33. public function selectParam($select): self
  34. {
  35. return $this
  36. ->addSelect($select);
  37. }
  38. public function selectCount(): self
  39. {
  40. return $this
  41. ->select('count(r.id) as total');
  42. }
  43. public function filterByUser(UserInterface $user): self
  44. {
  45. return $this
  46. ->andWhere('.user = :user')
  47. ->setParameter('user', $user);
  48. }
  49. public function filterByDateStart(string $dateField, DateTime $dateStart): self
  50. {
  51. return $this
  52. ->andWhere('.' . $dateField . ' >= :dateStart')
  53. ->setParameter('dateStart', $dateStart);
  54. }
  55. public function filterByDateEnd(string $dateField, DateTime $dateEnd): self
  56. {
  57. return $this
  58. ->andWhere('.' . $dateField . ' <= :dateEnd')
  59. ->setParameter('dateEnd', $dateEnd);
  60. }
  61. public function filterByEstimatedDeliveryDateStart(string $dateStart): self
  62. {
  63. return $this
  64. ->andWhere('.estimatedDeliveryDateTime >= :deliveryDateStart')
  65. ->setParameter('deliveryDateStart', $dateStart);
  66. }
  67. public function filterByEstimatedDeliveryDateEnd(string $dateEnd): self
  68. {
  69. return $this
  70. ->andWhere('.estimatedDeliveryDateTime < :deliveryDateEnd')
  71. ->setParameter('deliveryDateEnd', $dateEnd);
  72. }
  73. public function filterByDeliveryDateStart(string $dateStart): self
  74. {
  75. return $this
  76. ->andWhere('.deliveryDate >= :deliveryDateStart')
  77. ->setParameter('deliveryDateStart', $dateStart);
  78. }
  79. public function filterByDeliveryDateEnd(string $dateEnd): self
  80. {
  81. return $this
  82. ->andWhere('.deliveryDate < :deliveryDateEnd')
  83. ->setParameter('deliveryDateEnd', $dateEnd);
  84. }
  85. public function filterByVisitor(VisitorInterface $visitor): self
  86. {
  87. return $this
  88. ->andWhere('.visitor = :visitor')
  89. ->setParameter('visitor', $visitor);
  90. }
  91. public function filterByAddress(AddressInterface $address): self
  92. {
  93. return $this
  94. ->andWhere('.deliveryAddress = :address OR .invoiceAddress = :address')
  95. ->setParameter('address', $address);
  96. }
  97. public function filterByWeekDeliveryTruck(string $weekDeliveryTrucks): self
  98. {
  99. return $this
  100. ->andWhere('.weekDeliveryTruck IN (:weekDeliveryTrucks)')
  101. ->setParameter('weekDeliveryTrucks', $weekDeliveryTrucks);
  102. }
  103. public function filterByStatus(array $statusArray): self
  104. {
  105. $this->joinOrderStatus();
  106. return $this
  107. ->andWhere('os.alias IN (:alias)')
  108. ->setParameter('alias', $statusArray);
  109. }
  110. public function filterByReductionCredit(OrderReductionCreditInterface $reductionCredit): self
  111. {
  112. $this->joinOrderReductionCredits();
  113. return $this
  114. ->andWhere('orc.reductionCredit = :reductionCredit')
  115. ->setParameter('reductionCredit', $reductionCredit);
  116. }
  117. public function filterByReductionCart(OrderReductionCartInterface $reductionCart): self
  118. {
  119. $this->joinOrderReductionCarts();
  120. return $this
  121. ->andWhere('orcart.reductionCart = :reductionCart')
  122. ->setParameter('reductionCart', $reductionCart);
  123. }
  124. public function filterByAvailabilityPointZone(DeliveryAvailabilityPointSale $deliveryAvailabilityPointSale): self
  125. {
  126. return $this
  127. ->andWhere('.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale')
  128. ->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  129. }
  130. public function filterByAvailabilityPointSale(DeliveryAvailabilityPointSale $deliveryAvailabilityPointSale): self
  131. {
  132. return $this
  133. ->andWhere('.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale')
  134. ->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  135. }
  136. public function filterByWeekNumber(int $weekNumber): self
  137. {
  138. return $this
  139. ->andWhere('.weekNumber = :weekNumber')
  140. ->setParameter('weekNumber', $weekNumber);
  141. }
  142. public function filterIsNotMainOrderShop(): self
  143. {
  144. return $this
  145. ->andWhere('.mainOrderShop = false OR .mainOrderShop IS NULL');
  146. }
  147. public function filterIsNullMainOrderShop(): self
  148. {
  149. return $this
  150. ->andWhere('.mainOrderShop IS NULL');
  151. }
  152. public function filterIsNullDeliveryPointSale(): self
  153. {
  154. $this
  155. ->joinDeliveryPointSale()
  156. ->andWhere(
  157. '.deliveryPointSale IS NULL OR (pointSale.isDepository = 0 AND (pointSale.devAlias IS NULL OR (pointSale.devAlias != :devAliasHorsTournee AND pointSale.devAlias != :devAliasGiftVoucher)))'
  158. )
  159. ->setParameterGiftVoucher()
  160. ->setParameterHorsTournee();
  161. return $this;
  162. }
  163. public function filterIsNotNullDeliveryPointSale(): self
  164. {
  165. $this
  166. ->joinDeliveryPointSale()
  167. ->andWhere(
  168. 'pointSale IS NOT NULL AND pointSale.isDepository = 1 AND (pointSale.devAlias IS NULL OR (pointSale.devAlias != :devAliasHorsTournee AND pointSale.devAlias != :devAliasGiftVoucher))'
  169. )
  170. ->setParameterGiftVoucher()
  171. ->setParameterHorsTournee();
  172. return $this;
  173. }
  174. public function filterIsPointSale(string $devAlias = 'devAliasHorsTournee'): self
  175. {
  176. $this
  177. ->joinDeliveryPointSale()
  178. ->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :' . $devAlias);
  179. if($devAlias == 'devAliasHorsTournee'){
  180. $this->setParameterHorsTournee();
  181. } elseif($devAlias == 'devAliasGiftVoucher') {
  182. $this->setParameterGiftVoucher();
  183. }
  184. return $this;
  185. }
  186. public function selectOrderReductionCarts(): self
  187. {
  188. $this->joinOrderReductionCarts();
  189. return $this->addSelect('orcart');
  190. }
  191. public function setParameterGiftVoucher(): self
  192. {
  193. if (!$this->isGiftVoucher) {
  194. $this->isGiftVoucher = true;
  195. return $this
  196. ->setParameter('devAliasGiftVoucher', PointSale::DEV_ALIAS_GIFT_VOUCHER);
  197. }
  198. return $this;
  199. }
  200. public function setParameterHorsTournee(): self
  201. {
  202. if (!$this->isHorsTournee) {
  203. $this->isHorsTournee = true;
  204. return $this
  205. ->setParameter('devAliasHorsTournee', PointSale::DEV_ALIAS_OFF_CIRCUIT);
  206. }
  207. return $this;
  208. }
  209. public function joinOrderReductionCredits(): self
  210. {
  211. if (!$this->isJoinOrderReductionCredits) {
  212. $this->isJoinOrderReductionCredits = true;
  213. return $this
  214. ->innerJoin('.orderReductionCredits', 'orc');
  215. }
  216. return $this;
  217. }
  218. public function joinDeliveryAvailabilityZone(): self
  219. {
  220. if (!$this->isJoinDeliveryAvailabilityZone) {
  221. $this->isJoinDeliveryAvailabilityZone = true;
  222. return $this
  223. ->leftJoin('.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  224. }
  225. return $this;
  226. }
  227. public function joinDeliverySlotZone(): self
  228. {
  229. $this->joinDeliveryAvailabilityZone();
  230. if (!$this->isJoinDeliverySlotZone) {
  231. $this->isJoinDeliverySlotZone = true;
  232. return $this
  233. ->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  234. }
  235. return $this;
  236. }
  237. public function joinDeliveryAvailabilityPointSale(): self
  238. {
  239. if (!$this->isJoinDeliveryAvailabilityPointSale) {
  240. $this->isJoinDeliveryAvailabilityPointSale = true;
  241. return $this
  242. ->leftJoin('.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  243. }
  244. return $this;
  245. }
  246. public function joinDeliverySlotPointSale(): self
  247. {
  248. $this->joinDeliveryAvailabilityPointSale();
  249. if (!$this->isJoinDeliverySlotPointSale) {
  250. $this->isJoinDeliverySlotPointSale = true;
  251. return $this
  252. ->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  253. }
  254. return $this;
  255. }
  256. public function joinOrderStatus(): self
  257. {
  258. if (!$this->isJoinOrderStatus) {
  259. $this->isJoinOrderStatus = true;
  260. return $this
  261. ->leftJoin('.orderStatus', 'os');
  262. }
  263. return $this;
  264. }
  265. public function joinOrderReductionCarts(): self
  266. {
  267. if (!$this->isJoinOrderReductionCarts) {
  268. $this->isJoinOrderReductionCarts = true;
  269. return $this
  270. ->leftJoin('.orderReductionCarts', 'orcart');
  271. }
  272. return $this;
  273. }
  274. public function joinComplementaryOrderShops(): self
  275. {
  276. if (!$this->isJoinComplementaryOrderShops) {
  277. $this->isJoinComplementaryOrderShops = true;
  278. return $this
  279. ->leftJoin('.complementaryOrderShops', 'complementaryOrderShops');
  280. }
  281. return $this;
  282. }
  283. public function joinDeliveryPointSale(): self
  284. {
  285. if (!$this->isJoinDeliveryPointSale) {
  286. $this->isJoinDeliveryPointSale = true;
  287. return $this
  288. ->leftJoin('.deliveryPointSale', 'pointSale');
  289. }
  290. return $this;
  291. }
  292. }