Пример #1
0
 protected function _checkSaleAmount($params, $source)
 {
     $parts = explode(AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_SEPARATOR, $this->getSaleAmount(), 2);
     if (count($parts) < 2) {
         return true;
     }
     list($condition, $value) = $parts;
     if (!$condition || !$value) {
         return true;
     }
     if (isset($params[$source])) {
         $saleAmount = $params[$source]->getGrandTotal();
         $currencyModel = Mage::getModel('directory/currency');
         $_baseCurrency = $currencyModel->getConfigBaseCurrencies();
         if ($_baseCurrency && is_array($_baseCurrency)) {
             $_baseCurrency = $_baseCurrency[0];
             $_currencyTo = $params[$source]->getData('order_currency_code');
             if (is_null($_currencyTo)) {
                 $_currencyTo = $params[$source]->getData('store_currency_code');
             }
             $saleAmount = Mage::helper('followupemail')->convertPrice($saleAmount, $_currencyTo, $_baseCurrency);
         }
     } else {
         return 'No source for sale amount condition';
     }
     if ($saleAmount === false) {
         return 'Order currency convertation error';
     }
     switch (array_search($condition, AW_Followupemail_Model_Source_Rule_Saleamount::getConditions())) {
         case AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_EQ:
             $result = $saleAmount == $value;
             break;
         case AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_GT:
             $result = $saleAmount > $value;
             break;
         case AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_EGT:
             $result = $saleAmount >= $value;
             break;
         case AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_LT:
             $result = $saleAmount < $value;
             break;
         case AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_ELT:
             $result = $saleAmount <= $value;
             break;
         case AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_NE:
             $result = $saleAmount != $value;
             break;
         default:
             return 'Unknown condition';
     }
     return true === $result ? true : 'Sale amount is not ' . $condition . ' ' . $value . ' (' . $saleAmount . ')';
 }
 public function saveAction($sendTest = false)
 {
     $session = Mage::getSingleton('adminhtml/session');
     if ($data = $this->getRequest()->getPost()) {
         if (!isset($data['cancel_events'])) {
             $data['cancel_events'] = '';
         } else {
             $data['cancel_events'] = implode(',', $data['cancel_events']);
         }
         if (isset($data['category_ids'])) {
             $data['category_ids'] = implode(',', array_unique(explode(' ', trim(str_replace(',', ' ', $data['category_ids'])))));
         }
         if (!isset($data['product_type_ids']) || in_array('all', $data['product_type_ids'])) {
             $data['product_type_ids'] = AW_Followupemail_Model_Source_Product_Types::PRODUCT_TYPE_ALL;
         } else {
             $data['product_type_ids'] = implode(',', $data['product_type_ids']);
         }
         if (!isset($data['store_ids'])) {
             $data['store_ids'] = '';
         } elseif (is_array($data['store_ids'])) {
             $data['store_ids'] = implode(',', $data['store_ids']);
         }
         // sku
         if (!isset($data['sku'])) {
             $data['sku'] = array();
         } else {
             $data['sku'] = explode(',', $data['sku']);
         }
         foreach ($data['sku'] as $k => $v) {
             if (!($v = trim($v))) {
                 unset($data['sku'][$k]);
             } else {
                 $data['sku'][$k] = $v;
             }
         }
         $data['sku'] = implode(',', $data['sku']);
         $formatDate = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
         //active from
         if ($data['active_from']) {
             try {
                 $dateFrom = Mage::app()->getLocale()->utcDate(null, $data['active_from'], true, $formatDate)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
             } catch (Exception $e) {
                 unset($data['active_from']);
                 throw $e;
             }
             $data['active_from'] = $dateFrom;
         } else {
             $data['active_from'] = null;
         }
         //active to
         if ($data['active_to']) {
             try {
                 $date = Mage::app()->getLocale()->utcDate(null, $data['active_to'], true, $formatDate)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
             } catch (Exception $e) {
                 unset($data['active_to']);
                 throw $e;
             }
             $data['active_to'] = $date;
         } else {
             $data['active_to'] = null;
         }
         // customer groups
         if (!isset($data['customer_groups'])) {
             $data['customer_groups'] = '';
         }
         if (!is_array($data['customer_groups'])) {
             $data['customer_groups'] = explode(',', $data['customer_groups']);
         }
         if (!count($data['customer_groups']) || in_array(AW_Followupemail_Model_Source_Customer_Group::CUSTOMER_GROUP_ALL, $data['customer_groups']) && count($data['customer_groups']) > 1) {
             $data['customer_groups'] = array(AW_Followupemail_Model_Source_Customer_Group::CUSTOMER_GROUP_ALL);
         }
         if (is_array($data['customer_groups'])) {
             $data['customer_groups'] = implode(',', $data['customer_groups']);
         }
         // Advanced Newsletters segments
         if (!isset($data['anl_segments'])) {
             $data['anl_segments'] = '';
         }
         if (Mage::helper('followupemail')->canUseAN()) {
             if (!is_array($data['anl_segments'])) {
                 $data['anl_segments'] = explode(',', $data['anl_segments']);
             }
             if (!count($data['anl_segments']) || in_array(AW_Followupemail_Model_Mysql4_Rule::ADVANCED_NEWSLETTER_SEGMENTS_ALL, $data['anl_segments']) && count($data['anl_segments']) > 1) {
                 $data['anl_segments'] = array(AW_Followupemail_Model_Mysql4_Rule::ADVANCED_NEWSLETTER_SEGMENTS_ALL);
             }
         }
         if (is_array($data['anl_segments'])) {
             $data['anl_segments'] = implode(',', $data['anl_segments']);
         }
         // sale amount
         if (!isset($data['sale_amount_condition']) || !$data['sale_amount_condition']) {
             $data['sale_amount_condition'] = '';
         }
         $data['sale_amount_value'] = isset($data['sale_amount_value']) ? trim($data['sale_amount_value']) : '';
         if ($data['sale_amount_value'] || $data['sale_amount_condition']) {
             $data['sale_amount'] = AW_Followupemail_Model_Source_Rule_Saleamount::getCondition($data['sale_amount_condition']) . AW_Followupemail_Model_Source_Rule_Saleamount::CONDITION_SEPARATOR . $data['sale_amount_value'];
         } else {
             $data['sale_amount'] = '';
         }
         $data['test_objects'] = serialize($data['test']);
         // chain processing
         if (!isset($data['chain'])) {
             $data['chain'] = array();
         } else {
             foreach ($data['chain'] as $key => $value) {
                 if (isset($value['delete'])) {
                     if ($value['delete']) {
                         unset($data['chain'][$key]);
                     } else {
                         unset($data['chain'][$key]['delete']);
                     }
                 }
             }
             foreach ($data['chain'] as $key => $value) {
                 if (false === strpos($value['TEMPLATE_ID'], AW_Followupemail_Model_Source_Rule_Template::TEMPLATE_SOURCE_SEPARATOR)) {
                     $session->addError($this->__('Please select template'));
                     $session->setFollowupemailData($data);
                     $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'), 'tab' => 'details'));
                     return;
                 }
             }
             foreach ($data['chain'] as $k => $v) {
                 $data['chain'][$k]['DAYS'] = trim($data['chain'][$k]['DAYS']);
                 if ($data['chain'][$k]['DAYS'] && !is_numeric($data['chain'][$k]['DAYS'])) {
                     $session->addError($this->__('The quantity of days in the chain is not a number'));
                     $session->setFollowupemailData($data);
                     $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'), 'tab' => 'details'));
                     return;
                 }
             }
             if (count($data['chain']) > 1) {
                 // sorting
                 $chainSorted = array();
                 foreach ($data['chain'] as $k => $v) {
                     $chainSorted[$v['BEFORE'] * ($v['DAYS'] * 1440 + $v['HOURS'] * 60 + $v['MINUTES']) * 10000 + mt_rand(0, 9999)] = $k;
                 }
                 ksort($chainSorted, SORT_NUMERIC);
                 $chain = array();
                 foreach ($chainSorted as $k => $v) {
                     $chain[] = $data['chain'][$v];
                 }
                 $data['chain'] = $chain;
             }
         }
         if ($this->getRequest()->getParam('coupon_enabled')) {
             $couponExpireDays = $this->getRequest()->getParam('coupon_expire_days');
             if (!$couponExpireDays || !intval($couponExpireDays)) {
                 $session->addError($this->__('Coupon expire days value must be integer and equals or greater than 1'));
                 $session->setFollowupemailData($data);
                 return $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'), 'tab' => 'followupemail_tabs_coupons'));
             }
             if ($couponExpireDays < 1) {
                 $session->addError($this->__('Coupon expire days value can\'t be less than 1'));
                 $session->setFollowupemailData($data);
                 return $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'), 'tab' => 'followupemail_tabs_coupons'));
             }
             $couponCode = $this->getRequest()->getParam('coupon_prefix');
             if ($couponCode && !preg_match('/^[a-zA-Z0-9]*$/', $couponCode)) {
                 $session->addError($this->__('The following symbols are allowed to be used in the \'Coupon Code Prefix\' field: a-z 0-9'));
                 $session->setFollowupemailData($data);
                 return $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'), 'tab' => 'followupemail_tabs_coupons'));
             }
             $this->getRequest()->setParam('coupon_expire_days', intval($couponExpireDays));
         }
         $data['chain'] = serialize($data['chain']);
         $data['id'] = $this->getRequest()->getParam('id');
         /** @var $model AW_Followupemail_Model_Rule */
         $model = Mage::getModel('followupemail/rule');
         $model->setData($data);
         try {
             $model->save();
             $session->setFollowupemailData(false);
             $session->addSuccess($this->__('Item was successfully saved'));
             if ($this->getRequest()->getParam('sendTest')) {
                 $email = $data['test_recipient'];
                 $validator = new Zend_Validate_EmailAddress();
                 if (!$validator->isValid($email)) {
                     $session->addError($this->__("Incorrect e-mail for 'Test recipient' field"));
                     $this->_redirect('*/*/edit', array('id' => $model->getId(), 'tab' => 'followupemail_tabs_sendtest'));
                     return;
                 }
                 if (!$data['test_recipient']) {
                     $session->addError($this->__('To send a test message you have to fill up the \'Test recipient\' field'));
                     $this->_redirect('*/*/edit', array('id' => $model->getId(), 'tab' => 'followupemail_tabs_sendtest'));
                     return;
                 }
                 $testResult = $model->sendTestEmail($data['test']);
                 if ($testResult) {
                     $mailLogUrl = Mage::getModel('adminhtml/url')->getUrl('followupemail_admin/adminhtml_queue');
                     $session->addSuccess($this->__('Test email is scheduled for automatic sending. ' . 'Go to <a href="%s">Mail Log</a> to send it manually.', $mailLogUrl));
                 } else {
                     $session->addError($this->__('Error sending test message'));
                 }
             }
             if ($tab = $this->getRequest()->getParam('tab')) {
                 $this->_redirect('*/*/edit', array('id' => $model->getId(), 'tab' => $sendTest ? $sendTest : $tab));
                 return;
             }
             $this->_redirect('*/*/');
             return;
         } catch (Exception $e) {
             Mage::logException($e);
             $session->addError($e->getMessage());
             $session->setFollowupemailData($data);
             $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
             return;
         }
     }
     $session->addError($this->__('Cannot find data to save'));
     $this->_redirect('*/*/');
 }