1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Cart\BaseCart; |
4
|
|
|
use CMSFactory\assetManager; |
5
|
|
|
use CMSFactory\Events; |
6
|
|
|
use Currency\Currency; |
7
|
|
|
use mod_discount\classes\BaseDiscount; |
8
|
|
|
use mod_discount\Discount_product; |
9
|
|
|
use Propel\Runtime\Exception\PropelException; |
10
|
|
|
|
11
|
|
|
if (!defined('BASEPATH')) { |
12
|
|
|
exit('No direct script access allowed'); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class for Mod_Discount module |
17
|
|
|
* @uses \MY_Controller |
18
|
|
|
* @author DevImageCms |
19
|
|
|
* @copyright (c) 2013, ImageCMS |
20
|
|
|
* @package ImageCMSModule |
21
|
|
|
* @property discount_model_admin $discount_model_admin |
22
|
|
|
* @property BaseDiscount baseDiscount |
23
|
|
|
*/ |
24
|
|
|
class Mod_discount extends \MY_Controller |
25
|
|
|
{ |
|
|
|
|
26
|
|
|
|
27
|
|
|
public static $orderPassOverloadControl = false; |
28
|
|
|
|
29
|
|
|
public static $cnt = 0; |
30
|
|
|
|
31
|
|
|
public $no_install = true; |
32
|
|
|
|
33
|
|
|
protected $result_discount = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Discount counts apply - products quantity in |
37
|
|
|
* the cart with discount than it is in max apply |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $appliesControl = []; |
41
|
|
|
|
42
|
|
|
public $overload = 0.0; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* __construct base object loaded |
46
|
|
|
* @access public |
47
|
|
|
* @author DevImageCms |
48
|
|
|
* @copyright (c) 2013, ImageCMS |
49
|
|
|
*/ |
50
|
|
|
public function __construct() { |
51
|
|
|
|
52
|
|
|
parent::__construct(); |
53
|
|
|
$lang = new MY_Lang(); |
54
|
|
|
$lang->load('mod_discount'); |
55
|
|
|
$this->load->model('discount_model_admin'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* autoload execute when get product variant |
60
|
|
|
* @access public |
61
|
|
|
* @author DevImageCms |
62
|
|
|
* @copyright (c) 2013, ImageCMS |
63
|
|
|
* @throws \Exception |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function autoload() { |
66
|
|
|
if (BaseDiscount::checkModuleInstall()) { |
67
|
|
|
|
68
|
|
|
$this->applyDiscountCartItems(); |
69
|
|
|
|
70
|
|
|
$this->applyResultDiscount(); |
71
|
|
|
|
72
|
|
|
/** apply Gift */ |
73
|
|
|
if ($this->input->post('gift')) { |
74
|
|
|
$this->applyGift(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$giftKey = \CI::$APP->session->flashdata('makeOrderGiftKey'); |
78
|
|
|
if (!empty($giftKey)) { |
79
|
|
|
BaseDiscount::create()->updateDiskApply($giftKey, 'gift'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
Events::create()->setListener([$this, 'updateDiscountsApplies'], 'Cart:OrderValidated'); |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* apply discount to Cart Items |
89
|
|
|
* @access private |
90
|
|
|
* @author DevImageCms |
91
|
|
|
* @copyright (c) 2013, ImageCMS |
92
|
|
|
* @throws PropelException |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
private function applyDiscountCartItems() { |
95
|
|
|
$cart = BaseCart::getInstance(); |
96
|
|
|
$cartItems = $cart->getItems('SProducts'); |
97
|
|
|
|
98
|
|
|
if (count($cartItems['data']) == 0) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** @var SProducts|ShopKit $item */ |
103
|
|
|
foreach ($cartItems['data'] as $item) { |
104
|
|
|
|
105
|
|
|
$usedDiscountVariantPrice = true; |
106
|
|
|
|
107
|
|
|
if (class_exists('\VariantPriceType\BaseVariantPriceType') && self::isPremiumCMS()) { |
108
|
|
|
|
109
|
|
|
$usedDiscountVariantPrice = \VariantPriceType\BaseVariantPriceType::checkUsedDiscount($this->dx_auth->get_role_id()?: '-1', $item); |
|
|
|
|
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($item->originPrice > $item->price ) { |
|
|
|
|
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($usedDiscountVariantPrice === false) { |
118
|
|
|
|
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$arr_for_discount = [ |
123
|
|
|
'product_id' => $item->getSProducts()->getId(), |
124
|
|
|
'category_id' => $item->getSProducts()->getCategoryId(), |
125
|
|
|
'brand_id' => $item->getSProducts()->getBrandId(), |
126
|
|
|
'vid' => $item->id, |
127
|
|
|
'id' => $item->getSProducts()->getId(), |
128
|
|
|
]; |
129
|
|
|
assetManager::create()->discount = 0; |
|
|
|
|
130
|
|
|
|
131
|
|
|
if (BaseDiscount::checkModuleInstall()) { |
132
|
|
|
Discount_product::create()->getProductDiscount($arr_for_discount); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($discount = assetManager::create()->discount) { |
136
|
|
|
|
137
|
|
|
$priceNew = ((float) $item->originPrice - (float) $discount['discount_value'] < 0) ? 1 : (float) $item->originPrice - (float) $discount['discount_value']; |
138
|
|
|
$productData = [ |
139
|
|
|
'instance' => 'SProducts', |
140
|
|
|
'id' => $item->id, |
141
|
|
|
]; |
142
|
|
|
$cartItem = $cart->getItem($productData); |
143
|
|
|
$dkey = $discount['discount_max']['key']; |
144
|
|
|
if ($cartItem['success'] === TRUE) { |
145
|
|
|
$cartItem['data']->discountKey = $dkey; |
146
|
|
|
} |
147
|
|
|
$cart->setItemPrice($productData, round($priceNew, ShopCore::app()->SSettings->getPricePrecision())); |
148
|
|
|
|
149
|
|
|
if (!isset($this->appliesControl[$dkey])) { |
150
|
|
|
$appliesLeft = BaseDiscount::create()->getAppliesLeft($item->discountKey); |
151
|
|
|
if ($appliesLeft === null) { |
152
|
|
|
continue; |
153
|
|
|
} |
154
|
|
|
$this->appliesControl[$dkey] = [ |
155
|
|
|
'appliesLeft' => $appliesLeft, |
156
|
|
|
'assumedApplies' => 0, |
157
|
|
|
'overloadPrice' => 0.0, |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// gradually gathering overload (if will be) |
162
|
|
|
for ($i = 0; $i < $item->quantity; $i++) { |
163
|
|
|
if (++$this->appliesControl[$dkey]['assumedApplies'] > $this->appliesControl[$dkey]['appliesLeft']) { |
164
|
|
|
$this->appliesControl[$dkey]['overloadPrice'] += $discount['discount_value']; |
165
|
|
|
$this->overload += $discount['discount_value']; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* apply result discount |
174
|
|
|
* @access public |
175
|
|
|
* @author DevImageCms |
176
|
|
|
* @copyright (c) 2013, ImageCMS |
177
|
|
|
*/ |
178
|
|
|
private function applyResultDiscount() { |
179
|
|
|
BaseDiscount::prepareOption(['reBuild' => 1]); |
180
|
|
|
$this->baseDiscount = BaseDiscount::create(); |
181
|
|
|
|
182
|
|
|
if (BaseDiscount::checkModuleInstall()) { |
183
|
|
|
$discount['max_discount'] = $this->baseDiscount->discountMax; |
|
|
|
|
184
|
|
|
$discount['sum_discount_product'] = $this->baseDiscount->discountProductVal; |
185
|
|
|
$discount['sum_discount_no_product'] = $this->baseDiscount->discountNoProductVal; |
|
|
|
|
186
|
|
View Code Duplication |
if ($this->baseDiscount->discountProductVal > $this->baseDiscount->discountNoProductVal) { |
|
|
|
|
187
|
|
|
$discount['result_sum_discount'] = $this->baseDiscount->discountProductVal; |
188
|
|
|
$discount['type'] = 'product'; |
189
|
|
|
} else { |
190
|
|
|
$discount['result_sum_discount'] = $this->baseDiscount->discountNoProductVal; |
|
|
|
|
191
|
|
|
$discount['type'] = 'user'; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($discount['result_sum_discount'] > 0) { |
195
|
|
|
$cartTotalPrice = $this->baseDiscount->cart->getOriginTotalPrice() - $discount['result_sum_discount']; |
196
|
|
|
|
197
|
|
|
$this->baseDiscount->cart->setTotalPrice($cartTotalPrice > 0 ? $cartTotalPrice : BaseCart::MIN_ORDER_PRICE); |
198
|
|
|
$this->baseDiscount->cart->discount_info = $discount; |
199
|
|
|
$this->baseDiscount->cart->discount_type = $discount['type']; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function updateDiscountsApplies() { |
205
|
|
|
BaseDiscount::prepareOption(['reBuild' => 1]); |
206
|
|
|
$baseDiscount = BaseDiscount::create(); |
207
|
|
|
|
208
|
|
|
if (BaseDiscount::checkModuleInstall()) { |
209
|
|
|
if ($baseDiscount->discountProductVal > $baseDiscount->discountNoProductVal) { |
|
|
|
|
210
|
|
|
$discount['result_sum_discount'] = $baseDiscount->discountProductVal; |
|
|
|
|
211
|
|
|
$discount['type'] = 'product'; |
212
|
|
|
} else { |
213
|
|
|
$discount['result_sum_discount'] = $baseDiscount->discountNoProductVal; |
|
|
|
|
214
|
|
|
$discount['type'] = 'user'; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if ($discount['result_sum_discount'] > 0) { |
218
|
|
|
|
219
|
|
|
if ($discount['type'] != 'product') { |
220
|
|
|
$baseDiscount->updateDiskApply($baseDiscount->discountMax['key']); |
221
|
|
|
} else { |
222
|
|
|
$cartItems = BaseCart::getInstance()->getItems(); |
223
|
|
|
$diff = 0; |
224
|
|
|
foreach ($cartItems['data'] as $item) { |
225
|
|
|
if ($item->discountKey == null) { |
226
|
|
|
continue; |
227
|
|
|
} |
228
|
|
|
$appliesLeft = BaseDiscount::create()->getAppliesLeft($item->discountKey); |
229
|
|
|
if ($appliesLeft === null) { |
230
|
|
|
continue; |
231
|
|
|
} |
232
|
|
|
for ($i = 0; $i < $item->quantity; $i++) { |
233
|
|
|
if ($appliesLeft-- > 0) { |
234
|
|
|
BaseDiscount::create()->updateDiskApply($item->discountKey); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
if ($appliesLeft < 0) { |
238
|
|
|
$appliesLeft = abs($appliesLeft); |
239
|
|
|
$diff += ($item->originPrice - $item->price) * $appliesLeft; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
if ($diff > 0) { |
243
|
|
|
Events::create()->setListener( |
244
|
|
|
function($params) use ($diff) { |
245
|
|
|
$order = $params['order']; |
246
|
|
|
$price = $params['price']; |
|
|
|
|
247
|
|
|
if (!Mod_discount::$orderPassOverloadControl) { |
248
|
|
|
$price = $order->getTotalPrice() + $diff; |
249
|
|
|
$discount = $order->getDiscount() - $diff; |
|
|
|
|
250
|
|
|
$order |
251
|
|
|
//->setDiscount($discount) |
252
|
|
|
->setTotalPrice($price) |
253
|
|
|
->save(); |
254
|
|
|
Mod_discount::$orderPassOverloadControl = true; |
255
|
|
|
} |
256
|
|
|
}, |
257
|
|
|
'Cart:MakeOrder' |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* apply user gift |
267
|
|
|
* @access private |
268
|
|
|
* @author DevImageCms |
269
|
|
|
* @copyright (c) 2013, ImageCMS |
270
|
|
|
*/ |
271
|
|
|
private function applyGift() { |
272
|
|
|
|
273
|
|
|
$key = $this->input->post('gift'); |
274
|
|
|
$aplyGift = false; |
275
|
|
|
foreach ($this->baseDiscount->discountType['certificate'] as $disc) { |
276
|
|
|
if ($disc['key'] == $key and $disc['is_gift']) { |
277
|
|
|
$value = $this->baseDiscount->getDiscountValue($disc, $this->baseDiscount->cart->getTotalPrice()); |
278
|
|
|
|
279
|
|
|
$this->baseDiscount->cart->gift_info = $disc['key']; |
280
|
|
|
$this->baseDiscount->cart->gift_value = Currency::create()->convertFloor($value); |
281
|
|
|
if (\ShopCore::app()->SSettings->getPricePrecision() == 0) { |
282
|
|
|
$this->baseDiscount->cart->gift_value = floor($value); |
283
|
|
|
} |
284
|
|
|
$cartTotalPrice = $this->baseDiscount->cart->getTotalPrice() - $value; |
285
|
|
|
$this->baseDiscount->cart->setTotalPrice($cartTotalPrice > 0 ? $cartTotalPrice : BaseCart::MIN_ORDER_PRICE); |
286
|
|
|
$aplyGift = true; |
287
|
|
|
break; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if (!$aplyGift) { |
292
|
|
|
$this->baseDiscount->cart->gift_error = TRUE; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* install module and create table |
298
|
|
|
* @access public |
299
|
|
|
* @author DevImageCms |
300
|
|
|
* @copyright (c) 2013, ImageCMS |
301
|
|
|
*/ |
302
|
|
|
public function _install() { |
303
|
|
|
|
304
|
|
|
if (SHOP_INSTALLED) { |
305
|
|
|
$this->discount_model_admin->moduleInstall(); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* deinstall module and drop tables |
311
|
|
|
* @access public |
312
|
|
|
* @author DevImageCms |
313
|
|
|
* @copyright (c) 2013, ImageCMS |
314
|
|
|
*/ |
315
|
|
|
public function _deinstall() { |
316
|
|
|
|
317
|
|
|
if ($this->dx_auth->is_admin() == FALSE) { |
318
|
|
|
exit; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$this->discount_model_admin->moduleDelete(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/* End of file mod_discount.php */ |