/**
  * Search for orders to review per website.
  */
 public function searchOrdersForReview()
 {
     $websites = $this->helper->getwebsites(true);
     foreach ($websites as $website) {
         $apiEnabled = $this->helper->isEnabled($website);
         if ($apiEnabled && $this->helper->getWebsiteConfig(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_SYNC_ORDER_ENABLED, $website) && $this->helper->getOrderStatus($website) && $this->helper->getDelay($website)) {
             $storeIds = $website->getStoreIds();
             if (empty($storeIds)) {
                 continue;
             }
             $orderStatusFromConfig = $this->helper->getOrderStatus($website);
             $delayInDays = $this->helper->getDelay($website);
             $campaignCollection = $this->campaignCollection->create()->addFieldToFilter('event_name', 'Order Review');
             $campaignOrderIds = $campaignCollection->getColumnValues('order_increment_id');
             $fromTime = $this->date;
             $fromTime->subDay($delayInDays);
             $toTime = clone $fromTime;
             $to = $toTime->toString('YYYY-MM-dd HH:mm:ss');
             $from = $fromTime->subHour(2)->toString('YYYY-MM-dd HH:mm:ss');
             $created = ['from' => $from, 'to' => $to, 'date' => true];
             $collection = $this->orderCollection->create()->addFieldToFilter('main_table.status', $orderStatusFromConfig)->addFieldToFilter('main_table.created_at', $created)->addFieldToFilter('main_table.store_id', ['in' => $storeIds]);
             if (!empty($campaignOrderIds)) {
                 $collection->addFieldToFilter('main_table.increment_id', ['nin' => $campaignOrderIds]);
             }
             //process rules on collection
             $collection = $this->rulesFactory->create()->process($collection, \Dotdigitalgroup\Email\Model\Rules::REVIEW, $website->getId());
             if ($collection->getSize()) {
                 $this->reviewCollection[$website->getId()] = $collection;
             }
         }
     }
 }
 /**
  * Get campaign collection
  *
  * @param $storeIds
  * @param $sendStatus
  * @param $sendIdCheck
  * @return mixed
  */
 public function _getEmailCampaigns($storeIds, $sendStatus = 0, $sendIdCheck = false)
 {
     $emailCollection = $this->campaignCollection->create()->addFieldToFilter('send_status', $sendStatus)->addFieldToFilter('campaign_id', ['notnull' => true])->addFieldToFilter('store_id', ['in' => $storeIds]);
     //check for send id
     if ($sendIdCheck) {
         $emailCollection->addFieldToFilter('send_id', ['notnull' => true])->getSelect()->group('send_id');
     } else {
         $emailCollection->getSelect()->order('campaign_id');
     }
     $emailCollection->getSelect()->limit(self::SEND_EMAIL_CONTACT_LIMIT);
     return $emailCollection;
 }
 /**
  * Check customer campaign that was sent by a limit from config.
  * Return false for any found for this period.
  *
  * @param $email
  * @param $storeId
  *
  * @return bool
  */
 protected function _checkCustomerCartLimit($email, $storeId)
 {
     $cartLimit = $this->scopeConfig->getValue(\Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_ABANDONED_CART_LIMIT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
     //no limit is set skip
     if (!$cartLimit) {
         return false;
     }
     $fromTime = new \DateTime('now', new \DateTimeZone('UTC'));
     $toTime = clone $fromTime;
     $interval = new \DateInterval('PT' . $cartLimit . 'H');
     $fromTime->sub($interval);
     $fromDate = $fromTime->getTimestamp();
     $toDate = $toTime->getTimestamp();
     $updated = ['from' => $fromDate, 'to' => $toDate, 'date' => true];
     //total campaigns sent for this interval of time
     $campaignLimit = $this->_campaignCollection->create()->getCollection()->addFieldToFilter('email', $email)->addFieldToFilter('event_name', 'Lost Basket')->addFieldToFilter('sent_at', $updated)->count();
     //no campigns found
     if ($campaignLimit) {
         return true;
     }
     return false;
 }