Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_SalesRule_Exception
  • Mage_SalesRule_Helper_Coupon
  • Mage_SalesRule_Helper_Data
  • Mage_SalesRule_Model_Coupon
  • Mage_SalesRule_Model_Coupon_Codegenerator
  • Mage_SalesRule_Model_Coupon_Massgenerator
  • Mage_SalesRule_Model_Mysql4_Coupon
  • Mage_SalesRule_Model_Mysql4_Coupon_Collection
  • Mage_SalesRule_Model_Mysql4_Coupon_Usage
  • Mage_SalesRule_Model_Mysql4_Report_Collection
  • Mage_SalesRule_Model_Mysql4_Report_Rule
  • Mage_SalesRule_Model_Mysql4_Report_Updatedat_Collection
  • Mage_SalesRule_Model_Mysql4_Rule
  • Mage_SalesRule_Model_Mysql4_Rule_Collection
  • Mage_SalesRule_Model_Mysql4_Rule_Customer
  • Mage_SalesRule_Model_Mysql4_Rule_Customer_Collection
  • Mage_SalesRule_Model_Mysql4_Rule_Product
  • Mage_SalesRule_Model_Mysql4_Rule_Product_Collection
  • Mage_SalesRule_Model_Observer
  • Mage_SalesRule_Model_Quote_Discount
  • Mage_SalesRule_Model_Quote_Freeshipping
  • Mage_SalesRule_Model_Quote_Nominal_Discount
  • Mage_SalesRule_Model_Resource_Coupon
  • Mage_SalesRule_Model_Resource_Coupon_Collection
  • Mage_SalesRule_Model_Resource_Coupon_Usage
  • Mage_SalesRule_Model_Resource_Report_Collection
  • Mage_SalesRule_Model_Resource_Report_Rule
  • Mage_SalesRule_Model_Resource_Report_Rule_Createdat
  • Mage_SalesRule_Model_Resource_Report_Rule_Updatedat
  • Mage_SalesRule_Model_Resource_Report_Updatedat_Collection
  • Mage_SalesRule_Model_Resource_Rule
  • Mage_SalesRule_Model_Resource_Rule_Collection
  • Mage_SalesRule_Model_Resource_Rule_Customer
  • Mage_SalesRule_Model_Resource_Rule_Customer_Collection
  • Mage_SalesRule_Model_Resource_Rule_Product
  • Mage_SalesRule_Model_Resource_Rule_Product_Collection
  • Mage_SalesRule_Model_Rule
  • Mage_SalesRule_Model_Rule_Action_Collection
  • Mage_SalesRule_Model_Rule_Action_Product
  • Mage_SalesRule_Model_Rule_Condition_Address
  • Mage_SalesRule_Model_Rule_Condition_Combine
  • Mage_SalesRule_Model_Rule_Condition_Product
  • Mage_SalesRule_Model_Rule_Condition_Product_Combine
  • Mage_SalesRule_Model_Rule_Condition_Product_Found
  • Mage_SalesRule_Model_Rule_Condition_Product_Subselect
  • Mage_SalesRule_Model_Rule_Customer
  • Mage_SalesRule_Model_Rule_Product
  • Mage_SalesRule_Model_System_Config_Source_Coupon_Format
  • Mage_SalesRule_Model_Validator

Interfaces

  • Mage_SalesRule_Model_Coupon_CodegeneratorInterface
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Magento
  4:  *
  5:  * NOTICE OF LICENSE
  6:  *
  7:  * This source file is subject to the Open Software License (OSL 3.0)
  8:  * that is bundled with this package in the file LICENSE.txt.
  9:  * It is also available through the world-wide-web at this URL:
 10:  * http://opensource.org/licenses/osl-3.0.php
 11:  * If you did not receive a copy of the license and are unable to
 12:  * obtain it through the world-wide-web, please send an email
 13:  * to license@magentocommerce.com so we can send you a copy immediately.
 14:  *
 15:  * DISCLAIMER
 16:  *
 17:  * Do not edit or add to this file if you wish to upgrade Magento to newer
 18:  * versions in the future. If you wish to customize Magento for your
 19:  * needs please refer to http://www.magentocommerce.com for more information.
 20:  *
 21:  * @category    Mage
 22:  * @package     Mage_SalesRule
 23:  * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 24:  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25:  */
 26: 
 27: 
 28: /**
 29:  * Sales Rule resource model
 30:  *
 31:  * @category Mage
 32:  * @package Mage_SalesRule
 33:  * @author Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_SalesRule_Model_Resource_Rule extends Mage_Rule_Model_Resource_Abstract
 36: {
 37:     /**
 38:      * Store associated with rule entities information map
 39:      *
 40:      * @var array
 41:      */
 42:     protected $_associatedEntitiesMap = array(
 43:         'website' => array(
 44:             'associations_table' => 'salesrule/website',
 45:             'rule_id_field'      => 'rule_id',
 46:             'entity_id_field'    => 'website_id'
 47:         ),
 48:         'customer_group' => array(
 49:             'associations_table' => 'salesrule/customer_group',
 50:             'rule_id_field'      => 'rule_id',
 51:             'entity_id_field'    => 'customer_group_id'
 52:         )
 53:     );
 54: 
 55:     /**
 56:      * Initialize main table and table id field
 57:      */
 58:     protected function _construct()
 59:     {
 60:         $this->_init('salesrule/rule', 'rule_id');
 61:     }
 62: 
 63:     /**
 64:      * Add customer group ids and website ids to rule data after load
 65:      *
 66:      * @param Mage_Core_Model_Abstract $object
 67:      *
 68:      * @return Mage_SalesRule_Model_Resource_Rule
 69:      */
 70:     protected function _afterLoad(Mage_Core_Model_Abstract $object)
 71:     {
 72:         $object->setData('customer_group_ids', (array)$this->getCustomerGroupIds($object->getId()));
 73:         $object->setData('website_ids', (array)$this->getWebsiteIds($object->getId()));
 74: 
 75:         parent::_afterLoad($object);
 76:         return $this;
 77:     }
 78: 
 79:     /**
 80:      * Prepare sales rule's discount quantity
 81:      *
 82:      * @param Mage_Core_Model_Abstract $object
 83:      *
 84:      * @return Mage_SalesRule_Model_Resource_Rule
 85:      */
 86:     public function _beforeSave(Mage_Core_Model_Abstract $object)
 87:     {
 88:         if (!$object->getDiscountQty()) {
 89:             $object->setDiscountQty(new Zend_Db_Expr('NULL'));
 90:         }
 91: 
 92:         parent::_beforeSave($object);
 93:         return $this;
 94:     }
 95: 
 96:     /**
 97:      * Bind sales rule to customer group(s) and website(s).
 98:      * Save rule's associated store labels.
 99:      * Save product attributes used in rule.
100:      *
101:      * @param Mage_Core_Model_Abstract $object
102:      *
103:      * @return Mage_SalesRule_Model_Resource_Rule
104:      */
105:     protected function _afterSave(Mage_Core_Model_Abstract $object)
106:     {
107:         if ($object->hasStoreLabels()) {
108:             $this->saveStoreLabels($object->getId(), $object->getStoreLabels());
109:         }
110: 
111:         if ($object->hasWebsiteIds()) {
112:             $websiteIds = $object->getWebsiteIds();
113:             if (!is_array($websiteIds)) {
114:                 $websiteIds = explode(',', (string)$websiteIds);
115:             }
116:             $this->bindRuleToEntity($object->getId(), $websiteIds, 'website');
117:         }
118: 
119:         if ($object->hasCustomerGroupIds()) {
120:             $customerGroupIds = $object->getCustomerGroupIds();
121:             if (!is_array($customerGroupIds)) {
122:                 $customerGroupIds = explode(',', (string)$customerGroupIds);
123:             }
124:             $this->bindRuleToEntity($object->getId(), $customerGroupIds, 'customer_group');
125:         }
126: 
127:         // Save product attributes used in rule
128:         $ruleProductAttributes = array_merge(
129:             $this->getProductAttributes(serialize($object->getConditions()->asArray())),
130:             $this->getProductAttributes(serialize($object->getActions()->asArray()))
131:         );
132:         if (count($ruleProductAttributes)) {
133:             $this->setActualProductAttributes($object, $ruleProductAttributes);
134:         }
135: 
136:         // Update auto geterated specific coupons if exists
137:         if ($object->getUseAutoGeneration() && $object->hasDataChanges()) {
138:             Mage::getResourceModel('salesrule/coupon')->updateSpecificCoupons($object);
139:         }
140:         return parent::_afterSave($object);
141:     }
142: 
143:     /**
144:      * Retrieve coupon/rule uses for specified customer
145:      *
146:      * @param Mage_SalesRule_Model_Rule $rule
147:      * @param int $customerId
148:      *
149:      * @return string
150:      */
151:     public function getCustomerUses($rule, $customerId)
152:     {
153:         $read = $this->_getReadAdapter();
154:         $select = $read->select()->from($this->getTable('rule_customer'), array('cnt'=>'count(*)'))
155:             ->where('rule_id = :rule_id')
156:             ->where('customer_id = :customer_id');
157:         return $read->fetchOne($select, array(':rule_id' => $rule->getRuleId(), ':customer_id' => $customerId));
158:     }
159: 
160:     /**
161:      * Save rule labels for different store views
162:      *
163:      * @param int $ruleId
164:      * @param array $labels
165:      *
166:      * @return Mage_SalesRule_Model_Resource_Rule
167:      */
168:     public function saveStoreLabels($ruleId, $labels)
169:     {
170:         $deleteByStoreIds = array();
171:         $table   = $this->getTable('salesrule/label');
172:         $adapter = $this->_getWriteAdapter();
173: 
174:         $data    = array();
175:         foreach ($labels as $storeId => $label) {
176:             if (Mage::helper('core/string')->strlen($label)) {
177:                 $data[] = array('rule_id' => $ruleId, 'store_id' => $storeId, 'label' => $label);
178:             } else {
179:                 $deleteByStoreIds[] = $storeId;
180:             }
181:         }
182: 
183:         $adapter->beginTransaction();
184:         try {
185:             if (!empty($data)) {
186:                 $adapter->insertOnDuplicate(
187:                     $table,
188:                     $data,
189:                     array('label')
190:                 );
191:             }
192: 
193:             if (!empty($deleteByStoreIds)) {
194:                 $adapter->delete($table, array(
195:                     'rule_id=?'       => $ruleId,
196:                     'store_id IN (?)' => $deleteByStoreIds
197:                 ));
198:             }
199:         } catch (Exception $e) {
200:             $adapter->rollback();
201:             throw $e;
202: 
203:         }
204:         $adapter->commit();
205: 
206:         return $this;
207:     }
208: 
209:     /**
210:      * Get all existing rule labels
211:      *
212:      * @param int $ruleId
213:      * @return array
214:      */
215:     public function getStoreLabels($ruleId)
216:     {
217:         $select = $this->_getReadAdapter()->select()
218:             ->from($this->getTable('salesrule/label'), array('store_id', 'label'))
219:             ->where('rule_id = :rule_id');
220:         return $this->_getReadAdapter()->fetchPairs($select, array(':rule_id' => $ruleId));
221:     }
222: 
223:     /**
224:      * Get rule label by specific store id
225:      *
226:      * @param int $ruleId
227:      * @param int $storeId
228:      * @return string
229:      */
230:     public function getStoreLabel($ruleId, $storeId)
231:     {
232:         $select = $this->_getReadAdapter()->select()
233:             ->from($this->getTable('salesrule/label'), 'label')
234:             ->where('rule_id = :rule_id')
235:             ->where('store_id IN(0, :store_id)')
236:             ->order('store_id DESC');
237:         return $this->_getReadAdapter()->fetchOne($select, array(':rule_id' => $ruleId, ':store_id' => $storeId));
238:     }
239: 
240:     /**
241:      * Return codes of all product attributes currently used in promo rules for specified customer group and website
242:      *
243:      * @param unknown_type $websiteId
244:      * @param unknown_type $customerGroupId
245:      * @return mixed
246:      */
247:     public function getActiveAttributes($websiteId, $customerGroupId)
248:     {
249:         $read = $this->_getReadAdapter();
250:         $select = $read->select()
251:             ->from(array('a' => $this->getTable('salesrule/product_attribute')),
252:                 new Zend_Db_Expr('DISTINCT ea.attribute_code'))
253:             ->joinInner(array('ea' => $this->getTable('eav/attribute')), 'ea.attribute_id = a.attribute_id', array());
254:         return $read->fetchAll($select);
255:     }
256: 
257:     /**
258:      * Save product attributes currently used in conditions and actions of rule
259:      *
260:      * @param Mage_SalesRule_Model_Rule $rule
261:      * @param mixed $attributes
262:      * @return Mage_SalesRule_Model_Resource_Rule
263:      */
264:     public function setActualProductAttributes($rule, $attributes)
265:     {
266:         $write = $this->_getWriteAdapter();
267:         $write->delete($this->getTable('salesrule/product_attribute'), array('rule_id=?' => $rule->getId()));
268: 
269:         //Getting attribute IDs for attribute codes
270:         $attributeIds = array();
271:         $select = $this->_getReadAdapter()->select()
272:             ->from(array('a' => $this->getTable('eav/attribute')), array('a.attribute_id'))
273:             ->where('a.attribute_code IN (?)', array($attributes));
274:         $attributesFound = $this->_getReadAdapter()->fetchAll($select);
275:         if ($attributesFound) {
276:             foreach ($attributesFound as $attribute) {
277:                 $attributeIds[] = $attribute['attribute_id'];
278:             }
279: 
280:             $data = array();
281:             foreach ($rule->getCustomerGroupIds() as $customerGroupId) {
282:                 foreach ($rule->getWebsiteIds() as $websiteId) {
283:                     foreach ($attributeIds as $attribute) {
284:                         $data[] = array (
285:                             'rule_id'           => $rule->getId(),
286:                             'website_id'        => $websiteId,
287:                             'customer_group_id' => $customerGroupId,
288:                             'attribute_id'      => $attribute
289:                         );
290:                     }
291:                 }
292:             }
293:             $write->insertMultiple($this->getTable('salesrule/product_attribute'), $data);
294:         }
295: 
296:         return $this;
297:     }
298: 
299:     /**
300:      * Collect all product attributes used in serialized rule's action or condition
301:      *
302:      * @param string $serializedString
303:      *
304:      * @return array
305:      */
306:     public function getProductAttributes($serializedString)
307:     {
308:         $result = array();
309:         if (preg_match_all('~s:32:"salesrule/rule_condition_product";s:9:"attribute";s:\d+:"(.*?)"~s',
310:             $serializedString, $matches)){
311:             foreach ($matches[1] as $offset => $attributeCode) {
312:                 $result[] = $attributeCode;
313:             }
314:         }
315: 
316:         return $result;
317:     }
318: }
319: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0