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.

OrderShopRepository.php 11KB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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['orderStatus'])) {
  121. $query->leftJoin('e.orderStatus', 'os');
  122. $query->andWhere('os.alias LIKE :alias');
  123. $query->setParameter('alias', $params['orderStatus']);
  124. }
  125. if (isset($params['user'])) {
  126. $query->andWhere('e.user = :user')->setParameter('user', $params['user']);
  127. }
  128. if(isset($params['address'])) {
  129. $query->andWhere('e.deliveryAddress = :address OR e.invoiceAddress = :address')->setParameter('address', $params['address']);
  130. }
  131. if (isset($params['mergeComplementaryOrderShops'])) {
  132. $query->andWhere('e.mainOrderShop IS NULL');
  133. $query->leftJoin('e.complementaryOrderShops', 'complementaryOrderShops');
  134. $query->leftJoin('complementaryOrderShops.orderStatus', 'complementaryOrderStatus');
  135. $query->andWhere('complementaryOrderShops IS NULL OR complementaryOrderStatus.alias IN (:statusAliasValid)');
  136. $query->setParameter('statusAliasValid', OrderStatus::$statusAliasAsValid);
  137. }
  138. if (isset($params['excludeComplementaryOrderShops'])) {
  139. $query->andWhere('e.mainOrderShop IS NULL');
  140. }
  141. if (isset($params['isOffCircuit'])) {
  142. $query = $this->filterIsOffCircuit($query, true);
  143. }
  144. if (isset($params['isCircuit'])) {
  145. $query = $this->filterIsOffCircuit($query, false);
  146. $query->leftJoin('e.deliveryPointSale', 'pointSale');
  147. $query->andWhere('e.deliveryPointSale IS NULL OR pointSale.isDepository = 0');
  148. }
  149. if (isset($params['isDepository'])) {
  150. $query = $this->filterIsOffCircuit($query, false);
  151. $query->innerJoin('e.deliveryPointSale', 'pointSale');
  152. $query->andWhere('pointSale.isDepository = 1');
  153. }
  154. if (isset($params['deliveryAvailability'])) {
  155. $deliveryAvailability = $params['deliveryAvailability'] ;
  156. $deliveryAvailabilityZone = ($deliveryAvailability instanceof DeliveryAvailabilityZone) ? $deliveryAvailability : false;
  157. $deliveryAvailabilityPointSale = ($deliveryAvailability instanceof DeliveryAvailabilityPointSale) ? $deliveryAvailability : false;
  158. if($deliveryAvailabilityZone){
  159. $query->andWhere('e.deliveryAvailabilityZone = :deliveryAvailabilityZone');
  160. $query->setParameter('deliveryAvailabilityZone', $deliveryAvailabilityZone);
  161. }
  162. if($deliveryAvailabilityPointSale) {
  163. $query->andWhere('e.deliveryAvailabilityPointSale = :deliveryAvailabilityPointSale');
  164. $query->setParameter('deliveryAvailabilityPointSale', $deliveryAvailabilityPointSale);
  165. }
  166. }
  167. else{
  168. $query->leftJoin('e.deliveryAvailabilityZone', 'deliveryAvailabilityZone');
  169. $query->leftJoin('deliveryAvailabilityZone.deliverySlot', 'deliverySlotZone');
  170. $query->leftJoin('e.deliveryAvailabilityPointSale', 'deliveryAvailabilityPointSale');
  171. $query->leftJoin('deliveryAvailabilityPointSale.deliverySlot', 'deliverySlotPointSale');
  172. }
  173. if (isset($params['orderBy'])) {
  174. $query->orderBy('e.' . $params['orderBy'], isset($params['orderByDirection']) ? $params['orderByDirection'] : 'DESC');
  175. } else {
  176. $query->orderBy('e.id', 'DESC');
  177. }
  178. if (isset($params['count']) && $params['count']) {
  179. return $query->getQuery()->getSingleScalarResult();
  180. } else {
  181. return $query->getQuery()->getResult();
  182. }
  183. }
  184. public function findLastOrderValidOfWeek($weekNumber)
  185. {
  186. $query = $this->findByMerchantQuery();
  187. $query = $this->filterOrderValid($query);
  188. $query->andWhere('e.weekNumber = :weekNumber');
  189. $query->setParameter('weekNumber', $weekNumber);
  190. $query->orderBy('e.validationDate', 'DESC');
  191. $query->setMaxResults(1);
  192. return $query->getQuery()->getOneOrNullResult();
  193. }
  194. public function countValidOrderProductsOfWeekByProduct($weekNumber, $productId)
  195. {
  196. $query = $this->findByMerchantQuery();
  197. $query = $this->filterOrderValid($query);
  198. $query->andWhere('e.weekNumber = :weekNumber');
  199. $query->setParameter('weekNumber', $weekNumber);
  200. $query->leftJoin('e.orderProducts', 'orderProduct');
  201. $query->andWhere('orderProduct.product = :product');
  202. $query->setParameter('product', $productId);
  203. $query->select('SUM(orderProduct.quantityOrder) as quantity');
  204. $result = $query->getQuery()->getOneOrNullResult();
  205. return $result['quantity'];
  206. }
  207. }