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.

256 lines
11KB

  1. <?php
  2. namespace Lc\ShopBundle\Repository;
  3. use App\Entity\DeliveryAvailabilityPointSale;
  4. use App\Entity\DeliveryAvailabilityZone;
  5. use Doctrine\ORM\QueryBuilder;
  6. use Lc\ShopBundle\Context\DefaultRepositoryInterface;
  7. use Lc\ShopBundle\Context\OrderShopInterface;
  8. use Lc\ShopBundle\Model\OrderStatus;
  9. /**
  10. * @method OrderShopInterface|null find($id, $lockMode = null, $lockVersion = null)
  11. * @method OrderShopInterface|null findOneBy(array $criteria, array $orderBy = null)
  12. * @method OrderShopInterface[] findAll()
  13. * @method OrderShopInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14. */
  15. class OrderShopRepository extends BaseRepository implements DefaultRepositoryInterface
  16. {
  17. public function getInterfaceClass()
  18. {
  19. return OrderShopInterface::class;
  20. }
  21. public function countValidOrderWithReductionCredit($reductionCredit, $user)
  22. {
  23. $query = $this->findByMerchantQuery();
  24. $query = $this->filterOrderValid($query);
  25. $query->select('count(e.id)');
  26. $query->andWhere('e.user = :user');
  27. $query->innerJoin('e.orderReductionCredits', 'orc');
  28. $query->andWhere('orc.reductionCredit = :reductionCredit');
  29. $query->setParameter('reductionCredit', $reductionCredit);
  30. $query->setParameter('user', $user);
  31. return $query->getQuery()->getSingleScalarResult();
  32. }
  33. public function countValidOrderWithReductionCart($reductionCart)
  34. {
  35. $query = $this->findByMerchantQuery();
  36. $query = $this->filterOrderValid($query);
  37. $query->select('count(e.id)');
  38. $query->join('e.orderReductionCarts', 'orc');
  39. $query->andWhere('orc.reductionCart = :reductionCart');
  40. $query->setParameter('reductionCart', $reductionCart);
  41. return $query->getQuery()->getSingleScalarResult();
  42. }
  43. public function countValidOrderWithReductionCartPerUser($reductionCart, $user)
  44. {
  45. $query = $this->findByMerchantQuery();
  46. $query = $this->filterOrderValid($query);
  47. $query->select('count(e.id)');
  48. $query->andWhere('e.user = :user');
  49. $query->join('e.orderReductionCarts', 'orc');
  50. $query->andWhere('orc.reductionCart = :reductionCart');
  51. $query->setParameter('reductionCart', $reductionCart);
  52. $query->setParameter('user', $user);
  53. return $query->getQuery()->getSingleScalarResult();
  54. }
  55. public function filterOrderValid(?QueryBuilder $query): QueryBuilder
  56. {
  57. $query->leftJoin('e.orderStatus', 'os');
  58. $query->andWhere('os.alias IN (:alias)');
  59. $query->setParameter('alias', OrderStatus::$statusAliasAsValid);
  60. return $query;
  61. }
  62. public function filterOrderCart($query)
  63. {
  64. $query->leftJoin('e.orderStatus', 'os');
  65. $query->andWhere('os.alias IN (:alias)');
  66. $query->setParameter('alias', OrderStatus::$statusAliasAsCart);
  67. return $query;
  68. }
  69. public function filterIsOffCircuit($query, $isOffCircuit)
  70. {
  71. if ($isOffCircuit) {
  72. $query->andWhere('e.isOffCircuit = 1');
  73. } else {
  74. $query->andWhere('e.isOffCircuit IS NULL OR e.isOffCircuit = 0');
  75. }
  76. return $query;
  77. }
  78. public function findCartCurrent($params)
  79. {
  80. $query = $this->findByMerchantQuery();
  81. if (isset($params['user'])) {
  82. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  83. }
  84. if (isset($params['visitor'])) {
  85. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']);
  86. }
  87. $query = $this->filterOrderCart($query);
  88. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  89. ->addSelect('orderReductionCarts');
  90. $results = $query->getQuery()->getResult();
  91. if ($results) {
  92. return $results[0];
  93. }
  94. return null;
  95. }
  96. public function findAllBy($params = [])
  97. {
  98. $query = $this->findByMerchantQuery();
  99. if (isset($params['count']) && $params['count']) {
  100. $query->select('count(e.id)');
  101. }
  102. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  103. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  104. }
  105. if (isset($params['dateStart'])) {
  106. $query->andWhere('e.' . $params['dateField'] . ' >= :dateStart')->setParameter('dateStart', $params['dateStart']);
  107. }
  108. if (isset($params['dateEnd'])) {
  109. $query->andWhere('e.' . $params['dateField'] . ' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']);
  110. }
  111. if (isset($params['weekNumber'])) {
  112. $query->andWhere('e.weekNumber = :weekNumber')->setParameter('weekNumber', $params['weekNumber']);
  113. }
  114. if (isset($params['isCart'])) {
  115. $query = $this->filterOrderCart($query);
  116. }
  117. if (isset($params['isValid'])) {
  118. $query = $this->filterOrderValid($query);
  119. }
  120. if (isset($params['user'])) {
  121. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  122. }
  123. if(isset($params['address'])) {
  124. $query->andWhere('e.deliveryAddress = :address OR e.invoiceAddress = :address')->setParameter('address', $params['address']);
  125. }
  126. if (isset($params['mergeComplementaryOrderShops'])) {
  127. $query->andWhere('e.mainOrderShop IS NULL');
  128. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  129. $query->leftJoin('complementaryOrderShops.orderStatus', 'complementaryOrderStatus');
  130. $query->andWhere('complementaryOrderShops IS NULL OR complementaryOrderStatus.alias IN (:statusAliasValid)');
  131. $query->setParameter('statusAliasValid', OrderStatus::$statusAliasAsValid);
  132. }
  133. if (isset($params['excludeComplementaryOrderShops'])) {
  134. $query->andWhere('e.mainOrderShop IS NULL');
  135. }
  136. if (isset($params['isOffCircuit'])) {
  137. $query = $this->filterIsOffCircuit($query, true);
  138. }
  139. if (isset($params['isCircuit'])) {
  140. $query = $this->filterIsOffCircuit($query, false);
  141. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  142. $query->andWhere('e.deliveryPointSale IS NULL OR pointSale.isDepository = 0');
  143. }
  144. if (isset($params['isDepository'])) {
  145. $query = $this->filterIsOffCircuit($query, false);
  146. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  147. $query->andWhere('pointSale.isDepository = 1');
  148. }
  149. if (isset($params['deliveryAvailability'])) {
  150. $deliveryAvailability = $params['deliveryAvailability'] ;
  151. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  152. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  153. if($deliveryAvailabilityZone){
  154. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  155. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  156. }
  157. if($deliveryAvailabilityPointSale) {
  158. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  159. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  160. }
  161. }
  162. else{
  163. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  164. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  165. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  166. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  167. }
  168. if (isset($params['orderBy'])) {
  169. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  170. } else {
  171. $query->orderBy('e.id', 'DESC');
  172. }
  173. if (isset($params['count']) && $params['count']) {
  174. return $query->getQuery()->getSingleScalarResult();
  175. } else {
  176. return $query->getQuery()->getResult();
  177. }
  178. }
  179. public function findLastOrderValidOfWeek($weekNumber)
  180. {
  181. $query = $this->findByMerchantQuery();
  182. $query = $this->filterOrderValid($query);
  183. $query->andWhere('e.weekNumber = :weekNumber');
  184. $query->setParameter('weekNumber', $weekNumber);
  185. $query->orderBy('e.validationDate', 'DESC');
  186. $query->setMaxResults(1);
  187. return $query->getQuery()->getOneOrNullResult();
  188. }
  189. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  190. {
  191. $query = $this->findByMerchantQuery();
  192. $query = $this->filterOrderValid($query);
  193. $query->andWhere('e.weekNumber = :weekNumber');
  194. $query->setParameter('weekNumber', $weekNumber);
  195. $query->leftJoin('e.orderProducts', 'orderProduct');
  196. $query->andWhere('orderProduct.product = :product');
  197. $query->setParameter('product', $productId);
  198. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  199. return $query->getQuery()->getOneOrNullResult();
  200. }
  201. }