Collection.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel\Order;
  3. /**
  4. * Class Collection
  5. *
  6. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  7. */
  8. class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
  9. {
  10. /**
  11. * @var \Dotdigitalgroup\Email\Model\Newsletter\SubscriberFilterer
  12. */
  13. private $subscriberFilterer;
  14. /**
  15. * @var string
  16. */
  17. protected $_idFieldName = 'email_order_id';
  18. /**
  19. * @var \Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory
  20. */
  21. private $orderCollection;
  22. /**
  23. * @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory
  24. */
  25. private $quoteCollection;
  26. /**
  27. * @var \Dotdigitalgroup\Email\Helper\Data
  28. */
  29. private $helper;
  30. /**
  31. * Initialize resource collection.
  32. *
  33. * @return null
  34. */
  35. public function _construct()
  36. {
  37. $this->_init(
  38. \Dotdigitalgroup\Email\Model\Order::class,
  39. \Dotdigitalgroup\Email\Model\ResourceModel\Order::class
  40. );
  41. }
  42. /**
  43. * Collection constructor.
  44. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  45. * @param \Psr\Log\LoggerInterface $logger
  46. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  47. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  48. * @param \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteCollection
  49. * @param \Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory $orderCollection
  50. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  51. * @param \Dotdigitalgroup\Email\Model\Newsletter\SubscriberFilterer $subscriberFilterer
  52. * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
  53. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
  54. */
  55. public function __construct(
  56. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  57. \Psr\Log\LoggerInterface $logger,
  58. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  59. \Magento\Framework\Event\ManagerInterface $eventManager,
  60. \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteCollection,
  61. \Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory $orderCollection,
  62. \Dotdigitalgroup\Email\Helper\Data $helper,
  63. \Dotdigitalgroup\Email\Model\Newsletter\SubscriberFilterer $subscriberFilterer,
  64. \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
  65. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
  66. ) {
  67. $this->helper = $helper;
  68. $this->quoteCollection = $quoteCollection;
  69. $this->orderCollection = $orderCollection;
  70. $this->subscriberFilterer = $subscriberFilterer;
  71. parent::__construct(
  72. $entityFactory,
  73. $logger,
  74. $fetchStrategy,
  75. $eventManager,
  76. $connection,
  77. $resource
  78. );
  79. }
  80. /**
  81. * Load the email order by quote id.
  82. *
  83. * @param int $orderId
  84. * @param int $quoteId
  85. *
  86. * @return boolean|\Dotdigitalgroup\Email\Model\Order
  87. */
  88. public function loadByOrderIdAndQuoteId($orderId, $quoteId)
  89. {
  90. $collection = $this->addFieldToFilter('order_id', $orderId)
  91. ->addFieldToFilter('quote_id', $quoteId)
  92. ->setPageSize(1);
  93. if ($collection->getSize()) {
  94. return $collection->getFirstItem();
  95. }
  96. return false;
  97. }
  98. /**
  99. * Get connector order.
  100. *
  101. * @param int $orderId
  102. * @param int $quoteId
  103. * @param int $storeId
  104. *
  105. * @return boolean|\Dotdigitalgroup\Email\Model\Order
  106. */
  107. public function getEmailOrderRow($orderId, $quoteId, $storeId)
  108. {
  109. $collection = $this->addFieldToFilter('order_id', $orderId)
  110. ->addFieldToFilter('quote_id', $quoteId)
  111. ->addFieldToFilter('store_id', $storeId)
  112. ->setPageSize(1);
  113. if ($collection->getSize()) {
  114. return $collection->getFirstItem();
  115. }
  116. return false;
  117. }
  118. /**
  119. * Get pending orders for import.
  120. *
  121. * @param array $storeIds
  122. * @param int $limit
  123. * @param array $orderStatuses
  124. *
  125. * @return $this
  126. */
  127. public function getOrdersToImport($storeIds, $limit, $orderStatuses)
  128. {
  129. $collection = $this->addFieldToFilter('store_id', ['in' => $storeIds])
  130. ->addFieldToFilter('order_status', ['in' => $orderStatuses])
  131. ->addFieldToFilter('email_imported', ['null' => true]);
  132. $collection->getSelect()->limit($limit);
  133. return $collection;
  134. }
  135. /**
  136. * Get pending modified orders to import.
  137. *
  138. * @param array $storeIds
  139. * @param int $limit
  140. * @param array $orderStatuses
  141. *
  142. * @return $this
  143. */
  144. public function getModifiedOrdersToImport($storeIds, $limit, $orderStatuses)
  145. {
  146. $collection = $this->addFieldToFilter('store_id', ['in' => $storeIds])
  147. ->addFieldToFilter('order_status', ['in' => $orderStatuses])
  148. ->addFieldToFilter('email_imported', '1')
  149. ->addFieldToFilter('modified', '1');
  150. $collection->getSelect()->limit($limit);
  151. return $collection;
  152. }
  153. /**
  154. * Get all sent orders.
  155. *
  156. * @param array $storeIds
  157. * @param int $limit
  158. *
  159. * @return $this
  160. */
  161. public function getAllSentOrders($storeIds, $limit)
  162. {
  163. $collection = $this->addFieldToFilter('email_imported', 1)
  164. ->addFieldToFilter('store_id', ['in' => $storeIds]);
  165. $collection->getSelect()->limit($limit);
  166. return $collection->load();
  167. }
  168. /**
  169. * Get sales collection for review.
  170. *
  171. * @param string $orderStatusFromConfig
  172. * @param array $created
  173. * @param \Magento\Store\Model\Website $website
  174. * @param array $campaignOrderIds
  175. *
  176. * @return \Magento\Sales\Model\ResourceModel\Order\Collection
  177. */
  178. public function getSalesCollectionForReviews(
  179. $orderStatusFromConfig,
  180. $created,
  181. $website,
  182. $campaignOrderIds = []
  183. ) {
  184. $storeIds = $website->getStoreIds();
  185. $collection = $this->orderCollection->create()
  186. ->addFieldToFilter(
  187. 'main_table.status',
  188. $orderStatusFromConfig
  189. )
  190. ->addFieldToFilter('main_table.created_at', $created)
  191. ->addFieldToFilter(
  192. 'main_table.store_id',
  193. ['in' => $storeIds]
  194. );
  195. if (!empty($campaignOrderIds)) {
  196. $collection->addFieldToFilter(
  197. 'main_table.increment_id',
  198. ['nin' => $campaignOrderIds]
  199. );
  200. }
  201. if ($this->helper->isOnlySubscribersForReview($website->getWebsiteId())) {
  202. $collection = $this->subscriberFilterer->filterBySubscribedStatus($collection);
  203. }
  204. return $collection;
  205. }
  206. /**
  207. * Get customer last order id.
  208. *
  209. * @param \Magento\Customer\Model\Customer $customer
  210. * @param array $storeIds
  211. *
  212. * @return boolean|\Magento\Sales\Model\Order
  213. */
  214. public function getCustomerLastOrderId(\Magento\Customer\Model\Customer $customer, $storeIds)
  215. {
  216. $collection = $this->orderCollection->create()
  217. ->addFieldToFilter('customer_id', $customer->getId())
  218. ->addFieldToFilter('store_id', ['in' => $storeIds])
  219. ->setPageSize(1)
  220. ->setOrder('entity_id');
  221. if ($collection->getSize()) {
  222. return $collection->getFirstItem();
  223. }
  224. return false;
  225. }
  226. /**
  227. * Get customer last quote id.
  228. *
  229. * @param \Magento\Customer\Model\Customer $customer
  230. * @param array $storeIds
  231. *
  232. * @return boolean|\Magento\Quote\Model\Quote
  233. */
  234. public function getCustomerLastQuoteId(\Magento\Customer\Model\Customer $customer, $storeIds)
  235. {
  236. $collection = $this->quoteCollection->create()
  237. ->addFieldToFilter('customer_id', $customer->getId())
  238. ->addFieldToFilter('store_id', ['in' => $storeIds])
  239. ->setPageSize(1)
  240. ->setOrder('entity_id');
  241. if ($collection->getSize()) {
  242. return $collection->getFirstItem();
  243. }
  244. return false;
  245. }
  246. /**
  247. * Get store quotes excluding inactive and empty.
  248. *
  249. * @param int $storeId
  250. * @param array $updated
  251. * @param bool $guest
  252. *
  253. * @return \Magento\Quote\Model\ResourceModel\Quote\Collection
  254. */
  255. public function getStoreQuotes($storeId, $updated, $guest = false)
  256. {
  257. $salesCollection = $this->quoteCollection->create();
  258. $salesCollection->addFieldToFilter('is_active', 1)
  259. ->addFieldToFilter('items_count', ['gt' => 0])
  260. ->addFieldToFilter('customer_email', ['neq' => ''])
  261. ->addFieldToFilter('main_table.store_id', $storeId)
  262. ->addFieldToFilter('main_table.updated_at', $updated);
  263. if ($this->helper->isOnlySubscribersForAC($storeId)) {
  264. $salesCollection = $this->subscriberFilterer->filterBySubscribedStatus($salesCollection);
  265. }
  266. //guests
  267. if ($guest) {
  268. $salesCollection->addFieldToFilter('main_table.customer_id', ['null' => true]);
  269. } else {
  270. //customers
  271. $salesCollection->addFieldToFilter('main_table.customer_id', ['notnull' => true]);
  272. }
  273. return $salesCollection;
  274. }
  275. /**
  276. * Check emails exist in sales order table.
  277. *
  278. * @param array $emails
  279. *
  280. * @return array
  281. */
  282. public function checkInSales($emails)
  283. {
  284. $collection = $this->orderCollection->create()
  285. ->addFieldToFilter('customer_email', ['in' => $emails]);
  286. return $collection->getColumnValues('customer_email');
  287. }
  288. /**
  289. * @param array $quoteIds
  290. * @param int $storeId
  291. *
  292. * @return \Magento\Quote\Model\ResourceModel\Quote\Collection|Object
  293. */
  294. public function getStoreQuotesFromQuoteIds($quoteIds, $storeId)
  295. {
  296. $salesCollection = $this->quoteCollection->create()
  297. ->addFieldToFilter('is_active', 1)
  298. ->addFieldToFilter('items_count', ['gt' => 0])
  299. ->addFieldToFilter('customer_email', ['neq' => ''])
  300. ->addFieldToFilter('entity_id', ['in' => $quoteIds]);
  301. if ($this->helper->isOnlySubscribersForAC($storeId)) {
  302. $salesCollection = $this->subscriberFilterer->filterBySubscribedStatus($salesCollection);
  303. }
  304. return $salesCollection;
  305. }
  306. }