/
Giftvoucher.php
executable file
·186 lines (167 loc) · 6.27 KB
/
Giftvoucher.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
class Magestore_Giftvoucher_Model_Giftvoucher extends Mage_Core_Model_Abstract
{
public function _construct(){
parent::_construct();
$this->_init('giftvoucher/giftvoucher');
}
public function loadByCode($code){
return $this->load($code,'gift_code');
}
public function load($id, $field=null){
parent::load($id,$field);
if ($this->getIsDeleted()){
return Mage::getModel('giftvoucher/giftvoucher');
}
if ($this->getStatus() == Magestore_Giftvoucher_Model_Status::STATUS_ACTIVE
&& $this->getExpiredAt()
&& $this->getExpiredAt() < now())
$this->setStatus(Magestore_Giftvoucher_Model_Status::STATUS_EXPIRED);
return $this;
}
public function getIsDeleted(){
if (!$this->hasData('is_deleted')){
$this->setData('is_deleted',$this->getStatus() == Magestore_Giftvoucher_Model_Status::STATUS_DELETED);
}
return $this->getData('is_deleted');
}
public function getCollection(){
return parent::getCollection()->getAvailable();
}
public function getBaseBalance($storeId = null){
if (!$this->hasData('base_balance')){
$baseBalance = 0;
if ($rate = Mage::app()->getStore($storeId)->getBaseCurrency()->getRate($this->getData('currency')))
$baseBalance = $this->getBalance() / $rate;
$this->setData('base_balance',$baseBalance);
}
return $this->getData('base_balance');
}
protected function _beforeSave(){
if (!$this->getId()){
$this->setAction(Magestore_Giftvoucher_Model_Actions::ACTIONS_CREATE);
}
if ($this->getStatus() == Magestore_Giftvoucher_Model_Status::STATUS_ACTIVE
&& Mage::app()->getStore()->roundPrice($this->getBalance()) == 0)
$this->setStatus(Magestore_Giftvoucher_Model_Status::STATUS_USED);
if (!$this->getGiftCode())
$this->setGiftCode(Mage::helper('giftvoucher')->getGeneralConfig('pattern'));
if ($this->_codeIsExpression())
$this->setGiftCode($this->_getGiftCode());
return parent::_beforeSave();
}
protected function _codeIsExpression(){
return Mage::helper('giftvoucher')->isExpression($this->getGiftCode());
}
protected function _getGiftCode(){
$code = Mage::helper('giftvoucher')->calcCode($this->getGiftCode());
$times = 10;
while(Mage::getModel('giftvoucher/giftvoucher')->loadByCode($code)->getId() && $times){
$code = Mage::helper('giftvoucher')->calcCode($this->getGiftCode());
$times--;
if ($times == 0){
throw new Mage_Core_Exception('Exceeded maximum retries to find available random gift voucher code!');
}
}
return $code;
}
protected function _afterSave(){
if ($this->getIncludeHistory() && $this->getAction()){
$history = Mage::getModel('giftvoucher/history')
->setData($this->getData())
->setData('created_at',now());
try{
$history->save();
}catch(Exception $e){}
}
return parent::_afterSave();
}
public function delete(){
$this->setStatus(Magestore_Giftvoucher_Model_Status::STATUS_DELETED)
->save();
return $this;
}
public function getFormatedMessage()
{
return str_replace("\n","<br/>",$this->getMessage());
}
public function addToSession($session=null){
if (is_null($session)){
$session = Mage::getSingleton('checkout/session');
}
if ($codes = $session->getGiftCodes()){
$codesArray = explode(',',$codes);
$codesArray[] = $this->getGiftCode();
$codes = implode(',',array_unique($codesArray));
}else{
$codes = $this->getGiftCode();
}
$session->setGiftCodes($codes);
return $this;
}
public function sendEmail(){
$store = Mage::app()->getStore($this->getStoreId());
$translate = Mage::getSingleton('core/translate');
$translate->setTranslateInline(false);
$mailSent = 0;
if ($this->getCustomerEmail()){
$mailTemplate = Mage::getModel('core/email_template')
->setDesignConfig(array(
'area' => 'frontend',
'store' => $store->getStoreId()
))
->sendTransactional(
Mage::helper('giftvoucher')->getEmailConfig('self',$store->getStoreId()),
Mage::helper('giftvoucher')->getEmailConfig('sender',$store->getStoreId()),
$this->getCustomerEmail(),
$this->getCustomerName(),
array(
'store' => $store,
'sendername' => $this->getCustomerName(),
'name' => $this->getRecipientName(),
'code' => $this->getGiftCode(),
'balance' => $this->getBalanceFormated(),
'status' => $this->getStatusLabel(),
'expiredat' => $this->getExpiredAt() ? Mage::getModel('core/date')->date('M d, Y',$this->getExpiredAt()) : '',
'message' => $this->getFormatedMessage(),
)
);
$mailSent++;
}
if ($this->getRecipientName()){
$mailTemplate = Mage::getModel('core/email_template')
->setDesignConfig(array(
'area' => 'frontend',
'store' => $store->getStoreId()
))
->sendTransactional(
Mage::helper('giftvoucher')->getEmailConfig('template',$store->getStoreId()),
Mage::helper('giftvoucher')->getEmailConfig('sender',$store->getStoreId()),
$this->getRecipientEmail(),
$this->getRecipientName(),
array(
'store' => $store,
'sendername' => $this->getCustomerName(),
'name' => $this->getRecipientName(),
'code' => $this->getGiftCode(),
'balance' => $this->getBalanceFormated(),
'status' => $this->getStatusLabel(),
'expiredat' => $this->getExpiredAt() ? Mage::getModel('core/date')->date('M d, Y',$this->getExpiredAt()) : '' ,
'message' => $this->getFormatedMessage(),
)
);
$mailSent++;
}
$this->setEmailSent($mailSent);
$translate->setTranslateInline(true);
return $this;
}
public function getBalanceFormated(){
$currency = Mage::getModel('directory/currency')->load($this->getCurrency());
return $currency->format($this->getBalance());
}
public function getStatusLabel(){
$statusArray = Mage::getSingleton('giftvoucher/status')->getOptionArray();
return $statusArray[$this->getStatus()];
}
}