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: * SalesRule Resource Coupon
30: *
31: * @category Mage
32: * @package Mage_SalesRule
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_SalesRule_Model_Resource_Coupon extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Constructor adds unique fields
39: */
40: protected function _construct()
41: {
42: $this->_init('salesrule/coupon', 'coupon_id');
43: $this->addUniqueField(array(
44: 'field' => 'code',
45: 'title' => Mage::helper('salesrule')->__('Coupon with the same code')
46: ));
47: }
48:
49: /**
50: * Perform actions before object save
51: *
52: * @param Mage_Core_Model_Abstract $object
53: * @return Mage_Core_Model_Resource_Db_Abstract
54: */
55: public function _beforeSave(Mage_Core_Model_Abstract $object)
56: {
57: if (!$object->getExpirationDate()) {
58: $object->setExpirationDate(null);
59: } else if ($object->getExpirationDate() instanceof Zend_Date) {
60: $object->setExpirationDate($object->getExpirationDate()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
61: }
62:
63: // maintain single primary coupon per rule
64: $object->setIsPrimary($object->getIsPrimary() ? 1 : null);
65:
66: return parent::_beforeSave($object);
67: }
68:
69: /**
70: * Load primary coupon (is_primary = 1) for specified rule
71: *
72: *
73: * @param Mage_SalesRule_Model_Coupon $object
74: * @param Mage_SalesRule_Model_Rule|int $rule
75: * @return unknown
76: */
77: public function loadPrimaryByRule(Mage_SalesRule_Model_Coupon $object, $rule)
78: {
79: $read = $this->_getReadAdapter();
80:
81: if ($rule instanceof Mage_SalesRule_Model_Rule) {
82: $ruleId = $rule->getId();
83: } else {
84: $ruleId = (int)$rule;
85: }
86:
87: $select = $read->select()->from($this->getMainTable())
88: ->where('rule_id = :rule_id')
89: ->where('is_primary = :is_primary');
90:
91: $data = $read->fetchRow($select, array(':rule_id' => $ruleId, ':is_primary' => 1));
92:
93: if (!$data) {
94: return false;
95: }
96:
97: $object->setData($data);
98:
99: $this->_afterLoad($object);
100: return true;
101: }
102:
103: /**
104: * Check if code exists
105: *
106: * @param string $code
107: * @return bool
108: */
109: public function exists($code)
110: {
111: $read = $this->_getReadAdapter();
112: $select = $read->select();
113: $select->from($this->getMainTable(), 'code');
114: $select->where('code = :code');
115:
116: if ($read->fetchOne($select, array('code' => $code)) === false) {
117: return false;
118: }
119: return true;
120: }
121:
122: /**
123: * Update auto generated Specific Coupon if it's rule changed
124: *
125: * @param Mage_SalesRule_Model_Rule $rule
126: * @return Mage_SalesRule_Model_Resource_Coupon
127: */
128: public function updateSpecificCoupons(Mage_SalesRule_Model_Rule $rule)
129: {
130: if (!$rule || !$rule->getId() || !$rule->hasDataChanges()) {
131: return $this;
132: }
133:
134: $updateArray = array();
135: if ($rule->dataHasChangedFor('uses_per_coupon')) {
136: $updateArray['usage_limit'] = $rule->getUsesPerCoupon();
137: }
138:
139: if ($rule->dataHasChangedFor('uses_per_customer')) {
140: $updateArray['usage_per_customer'] = $rule->getUsesPerCustomer();
141: }
142:
143: $ruleNewDate = new Zend_Date($rule->getToDate());
144: $ruleOldDate = new Zend_Date($rule->getOrigData('to_date'));
145:
146: if ($ruleNewDate->compare($ruleOldDate)) {
147: $updateArray['expiration_date'] = $rule->getToDate();
148: }
149:
150: if (!empty($updateArray)) {
151: $this->_getWriteAdapter()->update(
152: $this->getTable('salesrule/coupon'),
153: $updateArray,
154: array('rule_id = ?' => $rule->getId())
155: );
156: }
157:
158: return $this;
159: }
160: }
161: