1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_SalesRule_Model_Resource_Rule_Collection extends Mage_Rule_Model_Resource_Rule_Collection_Abstract
36: {
37: 38: 39: 40: 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: 57:
58: protected function _construct()
59: {
60: $this->_init('salesrule/rule');
61: $this->_map['fields']['rule_id'] = 'main_table.rule_id';
62: }
63:
64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76:
77: public function setValidationFilter($websiteId, $customerGroupId, $couponCode = '', $now = null)
78: {
79: if (!$this->getFlag('validation_filter')) {
80:
81:
82: $this->getSelect()->reset();
83: parent::_initSelect();
84:
85: $this->addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now);
86: $select = $this->getSelect();
87:
88: if (strlen($couponCode)) {
89: $select->joinLeft(
90: array('rule_coupons' => $this->getTable('salesrule/coupon')),
91: 'main_table.rule_id = rule_coupons.rule_id ',
92: array('code')
93: );
94: $select->where('(main_table.coupon_type = ? ', Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON)
95: ->orWhere('(main_table.coupon_type = ? AND rule_coupons.type = 0',
96: Mage_SalesRule_Model_Rule::COUPON_TYPE_AUTO)
97: ->orWhere('main_table.coupon_type = ? AND main_table.use_auto_generation = 1 ' .
98: 'AND rule_coupons.type = 1', Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
99: ->orWhere('main_table.coupon_type = ? AND main_table.use_auto_generation = 0 ' .
100: 'AND rule_coupons.type = 0)', Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
101: ->where('rule_coupons.code = ?)', $couponCode);
102: } else {
103: $this->addFieldToFilter('main_table.coupon_type', Mage_SalesRule_Model_Rule::COUPON_TYPE_NO_COUPON);
104: }
105: $this->setOrder('sort_order', self::SORT_ORDER_ASC);
106: $this->setFlag('validation_filter', true);
107: }
108:
109: return $this;
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123:
124: public function addWebsiteGroupDateFilter($websiteId, $customerGroupId, $now = null)
125: {
126: if (!$this->getFlag('website_group_date_filter')) {
127: if (is_null($now)) {
128: $now = Mage::getModel('core/date')->date('Y-m-d');
129: }
130:
131: $this->addWebsiteFilter($websiteId);
132:
133: $entityInfo = $this->_getAssociatedEntityInfo('customer_group');
134: $connection = $this->getConnection();
135: $this->getSelect()
136: ->joinInner(
137: array('customer_group_ids' => $this->getTable($entityInfo['associations_table'])),
138: $connection->quoteInto(
139: 'main_table.' . $entityInfo['rule_id_field']
140: . ' = customer_group_ids.' . $entityInfo['rule_id_field']
141: . ' AND customer_group_ids.' . $entityInfo['entity_id_field'] . ' = ?',
142: (int)$customerGroupId
143: ),
144: array()
145: )
146: ->where('from_date is null or from_date <= ?', $now)
147: ->where('to_date is null or to_date >= ?', $now);
148:
149: $this->addIsActiveFilter();
150:
151: $this->setFlag('website_group_date_filter', true);
152: }
153:
154: return $this;
155: }
156:
157: 158: 159: 160: 161:
162: public function _initSelect()
163: {
164: parent::_initSelect();
165: $this->getSelect()
166: ->joinLeft(
167: array('rule_coupons' => $this->getTable('salesrule/coupon')),
168: 'main_table.rule_id = rule_coupons.rule_id AND rule_coupons.is_primary = 1',
169: array('code')
170: );
171: return $this;
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: public function addAttributeInConditionFilter($attributeCode)
182: {
183: $match = sprintf('%%%s%%', substr(serialize(array('attribute' => $attributeCode)), 5, -1));
184: $field = $this->_getMappedField('conditions_serialized');
185: $cCond = $this->_getConditionSql($field, array('like' => $match));
186: $field = $this->_getMappedField('actions_serialized');
187: $aCond = $this->_getConditionSql($field, array('like' => $match));
188:
189: $this->getSelect()->where(sprintf('(%s OR %s)', $cCond, $aCond), null, Varien_Db_Select::TYPE_CONDITION);
190:
191: return $this;
192: }
193:
194: 195: 196: 197: 198:
199: public function addAllowedSalesRulesFilter()
200: {
201: $this->addFieldToFilter(
202: 'main_table.use_auto_generation',
203: array('neq' => 1)
204: );
205:
206: return $this;
207: }
208: }
209: