Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

282 linhas
12KB

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