/**
  * used only for Magento >= 1.7
  *
  * Filter collection by website(s), customer group(s) and date.
  * Filter collection to only active rules.
  * Sorting is not involved
  *
  * @param int $websiteId
  * @param int $customerGroupId
  * @param string|null $now
  * @use $this->addWebsiteFilter()
  *
  * @return Mage_SalesRule_Model_Mysql4_Rule_Collection
  */
 public function addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now = null)
 {
     $oResource = Mage::getSingleton('core/resource');
     $sTable = $oResource->getTableName('aitoc_salesrule_assign_cutomer');
     if (!$sTable) {
         return parent::addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now);
     }
     if (!$this->getFlag('website_group_date_filter')) {
         if (is_null($now)) {
             $now = Mage::getModel('core/date')->date('Y-m-d');
         }
         $this->addWebsiteFilter($websiteId);
         $entityInfo = $this->_getAssociatedEntityInfo('customer_group');
         $connection = $this->getConnection();
         $iCustomerId = Mage::getSingleton('customer/session')->getCustomerId();
         if (!$iCustomerId) {
             $iCustomerId = 0;
             // fix for create order by admin
             if ($_SESSION and isset($_SESSION['adminhtml_quote']) and isset($_SESSION['adminhtml_quote']['customer_id']) and $_SESSION['adminhtml_quote']['customer_id']) {
                 $iCustomerId = $_SESSION['adminhtml_quote']['customer_id'];
             }
         }
         $this->getSelect()->joinLeft(array('rc' => $sTable), 'main_table.rule_id = rc.entity_id AND rc.customer_id = ' . $iCustomerId);
         /* coupon code date is checked here to skip the promotion expiry date in case the coupon is set which has a different expiry */
         $_coupon_code = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();
         $use_promotion_expiry = true;
         if (isset($_coupon_code)) {
             $coupon_object = Mage::getModel("salesrule/coupon")->loadByCode($_coupon_code);
             $coupon_data = $coupon_object->getData();
             if (!empty($coupon_data)) {
                 $coupon_expiry = $coupon_object->getExpirationDate();
                 $current_date = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
                 if ($coupon_expiry >= $current_date) {
                     $use_promotion_expiry = false;
                 }
             }
         }
         /* coupon code date is checked here to skip the promotion expiry date in case the coupon is set which has a different expiry */
         if ($use_promotion_expiry) {
             $this->getSelect()->joinLeft(array('customer_group_ids' => $this->getTable($entityInfo['associations_table'])), $connection->quoteInto('main_table.' . $entityInfo['rule_id_field'] . ' = customer_group_ids.' . $entityInfo['rule_id_field'] . ' AND customer_group_ids.' . $entityInfo['entity_id_field'] . ' = ?', (int) $customerGroupId), array())->where('from_date is null or from_date <= ?', $now)->where('to_date is null or to_date >= ?', $now)->where('(rc.entity_id IS NOT NULL) OR (customer_group_ids.customer_group_id = ?)', $customerGroupId);
         } else {
             $this->getSelect()->joinLeft(array('customer_group_ids' => $this->getTable($entityInfo['associations_table'])), $connection->quoteInto('main_table.' . $entityInfo['rule_id_field'] . ' = customer_group_ids.' . $entityInfo['rule_id_field'] . ' AND customer_group_ids.' . $entityInfo['entity_id_field'] . ' = ?', (int) $customerGroupId), array())->where('from_date is null or from_date <= ?', $now)->where("((to_date is null or to_date >= ?) AND (coupon_type=1)) OR (coupon_type!=1)", $now)->where('(rc.entity_id IS NOT NULL) OR (customer_group_ids.customer_group_id = ?)', $customerGroupId);
         }
         $this->addIsActiveFilter();
         $this->setFlag('website_group_date_filter', true);
     }
     return $this;
 }