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.

261 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 findCartCurrent($params)
  70. {
  71. $query = $this->findByMerchantQuery();
  72. if (isset($params['user'])) {
  73. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  74. }
  75. if (isset($params['visitor'])) {
  76. $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']);
  77. }
  78. $query = $this->filterOrderCart($query);
  79. $query->leftJoin('e.orderReductionCarts', 'orderReductionCarts')
  80. ->addSelect('orderReductionCarts');
  81. $results = $query->getQuery()->getResult();
  82. if ($results) {
  83. return $results[0];
  84. }
  85. return null;
  86. }
  87. public function findAllBy($params = [])
  88. {
  89. $query = $this->findByMerchantQuery();
  90. if (isset($params['count']) && $params['count']) {
  91. $query->select('count(e.id)');
  92. }
  93. if (isset($params['dateStart']) || isset($params['dateEnd'])) {
  94. $params['dateField'] = isset($params['dateField']) ? $params['dateField'] : 'validationDate';
  95. }
  96. if (isset($params['dateStart'])) {
  97. $query->andWhere('e.' . $params['dateField'] . ' >= :dateStart')->setParameter('dateStart', $params['dateStart']);
  98. }
  99. if (isset($params['dateEnd'])) {
  100. $query->andWhere('e.' . $params['dateField'] . ' <= :dateEnd')->setParameter('dateEnd', $params['dateEnd']);
  101. }
  102. if (isset($params['weekNumber'])) {
  103. $query->andWhere('e.weekNumber = :weekNumber')->setParameter('weekNumber', $params['weekNumber']);
  104. }
  105. if (isset($params['isCart'])) {
  106. $query = $this->filterOrderCart($query);
  107. }
  108. if (isset($params['isValid'])) {
  109. $query = $this->filterOrderValid($query);
  110. }
  111. if (isset($params['orderStatus'])) {
  112. $query->leftJoin('e.orderStatus', 'os');
  113. $query->andWhere('os.alias LIKE :alias');
  114. $query->setParameter('alias', $params['orderStatus']);
  115. }
  116. if (isset($params['user'])) {
  117. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  118. }
  119. if(isset($params['address'])) {
  120. $query->andWhere('e.deliveryAddress = :address OR e.invoiceAddress = :address')->setParameter('address', $params['address']);
  121. }
  122. if (isset($params['mergeComplementaryOrderShops'])) {
  123. $query->andWhere('e.mainOrderShop IS NULL');
  124. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  125. $query->leftJoin('complementaryOrderShops.orderStatus', 'complementaryOrderStatus');
  126. $query->andWhere('complementaryOrderShops IS NULL OR complementaryOrderStatus.alias IN (:statusAliasValid)');
  127. $query->setParameter('statusAliasValid', OrderStatus::$statusAliasAsValid);
  128. }
  129. if (isset($params['excludeComplementaryOrderShops'])) {
  130. $query->andWhere('e.mainOrderShop IS NULL');
  131. }
  132. $setParameterHorsTournee = false ;
  133. if (isset($params['isCircuit'])) {
  134. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  135. $query->andWhere('e.deliveryPointSale IS NULL OR (pointSale.isDepository = 0 AND (pointSale.devAlias IS NULL OR pointSale.devAlias != :devAliasHorsTournee))');
  136. $setParameterHorsTournee = true ;
  137. }
  138. if (isset($params['isDepository'])) {
  139. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  140. $query->andWhere('pointSale IS NOT NULL AND pointSale.isDepository = 1 AND (pointSale.devAlias IS NULL OR pointSale.devAlias != :devAliasHorsTournee)');
  141. $setParameterHorsTournee = true ;
  142. }
  143. if (isset($params['isOffCircuit'])) {
  144. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  145. $query->andWhere('pointSale IS NOT NULL AND pointSale.devAlias = :devAliasHorsTournee');
  146. $setParameterHorsTournee = true ;
  147. }
  148. if($setParameterHorsTournee) {
  149. $query->setParameter('devAliasHorsTournee', 'horstournee') ;
  150. }
  151. if (isset($params['deliveryAvailability'])) {
  152. $deliveryAvailability = $params['deliveryAvailability'] ;
  153. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  154. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  155. if($deliveryAvailabilityZone){
  156. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  157. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  158. }
  159. if($deliveryAvailabilityPointSale) {
  160. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  161. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  162. }
  163. }
  164. else{
  165. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  166. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  167. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  168. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  169. }
  170. if (isset($params['orderBy'])) {
  171. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  172. } else {
  173. $query->orderBy('e.id', 'DESC');
  174. }
  175. if (isset($params['count']) && $params['count']) {
  176. return $query->getQuery()->getSingleScalarResult();
  177. } else {
  178. return $query->getQuery()->getResult();
  179. }
  180. }
  181. public function findLastOrderValidOfWeek($weekNumber)
  182. {
  183. $query = $this->findByMerchantQuery();
  184. $query = $this->filterOrderValid($query);
  185. $query->andWhere('e.weekNumber = :weekNumber');
  186. $query->andWhere('e.mainOrderShop = false OR e.mainOrderShop IS NULL');
  187. $query->setParameter('weekNumber', $weekNumber);
  188. $query->orderBy('e.validationDate', 'DESC');
  189. $query->setMaxResults(1);
  190. return $query->getQuery()->getOneOrNullResult();
  191. }
  192. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  193. {
  194. $query = $this->findByMerchantQuery();
  195. $query = $this->filterOrderValid($query);
  196. $query->andWhere('e.weekNumber = :weekNumber');
  197. $query->setParameter('weekNumber', $weekNumber);
  198. $query->leftJoin('e.orderProducts', 'orderProduct');
  199. $query->andWhere('orderProduct.product = :product');
  200. $query->setParameter('product', $productId);
  201. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  202. $result = $query->getQuery()->getOneOrNullResult();
  203. return $result['quantity'];
  204. }
  205. }