1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Currency\Currency; |
4
|
|
|
|
5
|
|
|
if (!defined('BASEPATH')) { |
6
|
|
|
exit('No direct script access allowed'); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class discount_model_admin for Mod_Discount module |
11
|
|
|
* @uses CI_Model |
12
|
|
|
* @author DevImageCms |
13
|
|
|
* @copyright (c) 2013, ImageCMS |
14
|
|
|
* @package ImageCMSModule |
15
|
|
|
*/ |
16
|
|
|
class Discount_model_admin extends CI_Model |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public function __construct() { |
20
|
|
|
parent::__construct(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get discounts List |
25
|
|
|
* @param string $discountType |
26
|
|
|
* @param integer $rowCount |
27
|
|
|
* @param integer $offset |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function getDiscountsList($discountType = null, $rowCount = null, $offset = null, $locale) { |
31
|
|
|
$locale = $locale ? $locale : \MY_Controller::getCurrentLocale(); |
32
|
|
|
$query = $this->db->select('*, mod_shop_discounts.id as id')->join('mod_shop_discounts_i18n', "mod_shop_discounts_i18n.id = mod_shop_discounts.id and mod_shop_discounts_i18n.locale = '" . $locale . "'", 'left') |
33
|
|
|
//->where("mod_shop_discounts_i18n.locale " , $locale ) |
34
|
|
|
->join('mod_discount_all_order', 'mod_discount_all_order.discount_id = mod_shop_discounts.id', 'left') |
35
|
|
|
->order_by('mod_shop_discounts.active', 'desc')->order_by('mod_shop_discounts.id', 'desc'); |
36
|
|
|
if ($discountType != null) { |
37
|
|
|
$query = $query->where('mod_shop_discounts.type_discount', $discountType); |
38
|
|
|
} |
39
|
|
|
$query = $query->get('mod_shop_discounts')->result_array(); |
40
|
|
|
|
41
|
|
|
return $query; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Change discount status active or not |
46
|
|
|
* @param integer $id |
47
|
|
|
* @return boolean |
48
|
|
|
*/ |
49
|
|
|
public function changeActive($id) { |
50
|
|
|
$discount = $this->db->where('id', $id)->get('mod_shop_discounts')->row(); |
51
|
|
|
|
52
|
|
|
// Check is discount with such id |
53
|
|
|
if ($discount == null) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$active = $discount->active; |
58
|
|
|
$active = $active == 1 ? 0 : 1; |
59
|
|
|
|
60
|
|
|
// If updated active succes then return TRUE |
61
|
|
|
if ($this->db->where('id', $id)->update('mod_shop_discounts', ['active' => $active])) { |
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get main currency symbol |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
|
|
public function getMainCurrencySymbol() { |
73
|
|
|
return Currency::create()->getMainCurrency()->getSymbol(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Check have any discoun with given key |
78
|
|
|
* @param string $key |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function checkDiscountCode($key) { |
82
|
|
|
$query = $this->db->where('key', $key)->get('mod_shop_discounts')->row_array(); |
83
|
|
|
|
84
|
|
|
return $query ? true : false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* get users by id name email |
89
|
|
|
* @param string $term |
90
|
|
|
* @param integer $limit |
91
|
|
|
* return boolean|array |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function getUsersByIdNameEmail($term, $limit = 7) { |
95
|
|
|
|
96
|
|
|
$query = $this->db |
97
|
|
|
->like('username', $term) |
98
|
|
|
->or_like('email', $term) |
99
|
|
|
->or_like('id', $term) |
100
|
|
|
->limit($limit) |
101
|
|
|
->get('users') |
102
|
|
|
->result_array(); |
103
|
|
|
|
104
|
|
|
return $query ?: false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get user groups |
109
|
|
|
* @param string $locale |
110
|
|
|
* @return boolean|array |
111
|
|
|
*/ |
112
|
|
|
public function getUserGroups($locale = 'ru') { |
113
|
|
|
|
114
|
|
|
$query = $this->db |
115
|
|
|
->select('shop_rbac_roles.id, shop_rbac_roles_i18n.alt_name') |
116
|
|
|
->from('shop_rbac_roles') |
117
|
|
|
->join('shop_rbac_roles_i18n', 'shop_rbac_roles.id=shop_rbac_roles_i18n.id') |
118
|
|
|
->where('locale', $locale) |
119
|
|
|
->get() |
120
|
|
|
->result_array(); |
121
|
|
|
|
122
|
|
|
if ($query) { |
123
|
|
|
return $query; |
124
|
|
|
} else { |
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* |
131
|
|
|
* @param string $term |
132
|
|
|
* @param integer $limit |
133
|
|
|
* @return boolean|array |
134
|
|
|
*/ |
135
|
|
|
public function getProductsByIdNameNumber($term, $limit = 7, $locale = NULL) { |
136
|
|
|
$locale = $locale ?: MY_Controller::getCurrentLocale(); |
137
|
|
|
$query = $this->db |
138
|
|
|
->select('shop_products_i18n.id, shop_products_i18n.name, number, shop_products_i18n.locale') |
139
|
|
|
->join('shop_product_variants', 'shop_product_variants.product_id=shop_products_i18n.id') |
140
|
|
|
->like('shop_products_i18n.id', $term) |
141
|
|
|
->or_like('shop_products_i18n.name', $term) |
142
|
|
|
->or_like('number', $term) |
143
|
|
|
->limit($limit) |
144
|
|
|
->get('shop_products_i18n') |
145
|
|
|
->result_array(); |
146
|
|
|
|
147
|
|
|
foreach ($query as $key => $product) { |
148
|
|
|
if ($product['locale'] != $locale) { |
149
|
|
|
unset($query[$key]); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($query) { |
154
|
|
|
return $query; |
155
|
|
|
} else { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Insert data, uses when create discount |
162
|
|
|
* @param string $tableName |
163
|
|
|
* @param array $data |
164
|
|
|
* @return boolean|int |
165
|
|
|
*/ |
166
|
|
|
public function insertDataToDB($tableName, $data) { |
167
|
|
|
try { |
168
|
|
|
$this->db->insert($tableName, $data); |
169
|
|
|
|
170
|
|
|
return $this->db->insert_id(); |
171
|
|
|
} catch (Exception $e) { |
172
|
|
|
return false; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Update discount by id. |
178
|
|
|
* @param integer $id |
179
|
|
|
* @param array $data |
180
|
|
|
* @return boolean |
181
|
|
|
*/ |
182
|
|
|
public function updateDiscountById($id, $data, $typeDiscountData, $locale) { |
183
|
|
|
$name = $data['name']; |
184
|
|
|
unset($data['name']); |
185
|
|
|
$discountType = $data['type_discount']; |
186
|
|
|
$previousDiscount = $this->getDiscountAllDataById($id); |
187
|
|
|
|
188
|
|
|
$discountTypeTableNamePrevious = 'mod_discount_' . $previousDiscount['type_discount']; |
189
|
|
|
$discountTypeTableNameNew = 'mod_discount_' . $discountType; |
190
|
|
|
|
191
|
|
|
try { |
192
|
|
|
$this->db->where('id', $id)->update('mod_shop_discounts', $data); |
193
|
|
|
if ($this->db->query("select * from mod_shop_discounts_i18n where id = '$id' and locale = '$locale'")->num_rows()) { |
194
|
|
|
$this->db->query("update mod_shop_discounts_i18n set name = '$name' where id = '$id' and locale = '$locale'"); |
195
|
|
|
} else { |
196
|
|
|
$this->db->query("insert into mod_shop_discounts_i18n(id,name,locale) values('$id','$name','$locale')"); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->db->where('discount_id', $id)->delete($discountTypeTableNamePrevious); |
200
|
|
|
$typeDiscountData['discount_id'] = $id; |
201
|
|
|
|
202
|
|
|
$this->db->insert($discountTypeTableNameNew, $typeDiscountData); |
203
|
|
|
|
204
|
|
|
return true; |
205
|
|
|
} catch (Exception $e) { |
206
|
|
|
return false; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Check have any comulativ discount max endValue. |
212
|
|
|
* |
213
|
|
|
* @param integer $editDiscountId uses in order to not counting edited discount |
214
|
|
|
* @return boolean |
215
|
|
|
*/ |
216
|
|
|
public function checkHaveAnyComulativDiscountMaxEndValue($editDiscountId = null) { |
217
|
|
|
|
218
|
|
|
$query = $this->db; |
219
|
|
|
if ($editDiscountId) { |
220
|
|
|
$query = $query->where('discount_id !=', $editDiscountId); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$query = $query->where('end_value', null)->or_where('end_value', 0)->get('mod_discount_comulativ')->result_array(); |
224
|
|
|
|
225
|
|
|
if (count($query)) { |
|
|
|
|
226
|
|
|
return true; |
227
|
|
|
} else { |
228
|
|
|
return false; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get discount all data by id |
234
|
|
|
* @param integer $id |
235
|
|
|
* @return boolean|array |
|
|
|
|
236
|
|
|
*/ |
237
|
|
|
public function getDiscountAllDataById($id, $locale = null) { |
238
|
|
|
if (null === $locale) { |
239
|
|
|
$locale = MY_Controller::getCurrentLocale(); |
240
|
|
|
} |
241
|
|
|
$query = $this->db->from('mod_shop_discounts')->where('id', $id)->get()->row_array(); |
242
|
|
|
$query_locale = $this->db->from('mod_shop_discounts_i18n')->where('id', $id)->where('locale', $locale)->get()->row(); |
243
|
|
|
$query['name'] = $query_locale->name; |
244
|
|
|
$discountType = $query['type_discount']; |
245
|
|
|
|
246
|
|
|
if ($discountType) { |
247
|
|
|
$discountType = $discountType == 'certificate' ? 'all_order' : $discountType; |
248
|
|
|
$queryDiscountType = $this->db->from('mod_discount_' . $discountType)->where('discount_id', $id)->get()->row_array(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if ($queryDiscountType) { |
252
|
|
|
$query[$discountType] = $queryDiscountType; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if ($query) { |
256
|
|
|
return $query; |
257
|
|
|
} else { |
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get username and email by id |
264
|
|
|
* @param integer $id |
265
|
|
|
* @return string|false |
266
|
|
|
*/ |
267
|
|
|
public function getUserNameAndEmailById($id) { |
268
|
|
|
|
269
|
|
|
$query = $this->db->select('username, email')->from('users')->where('id', $id)->get()->row_array(); |
270
|
|
|
if ($query) { |
271
|
|
|
$userInfo = $query['username'] . ' - ' . $query['email']; |
272
|
|
|
return $userInfo; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return false; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get product name by id |
280
|
|
|
* @param integer $id |
281
|
|
|
* @return string|boolean |
282
|
|
|
*/ |
283
|
|
View Code Duplication |
public function getProductById($id) { |
284
|
|
|
$locale = MY_Controller::getCurrentLocale(); |
285
|
|
|
$query = $this->db |
286
|
|
|
->select('name') |
287
|
|
|
->from('shop_products_i18n') |
288
|
|
|
->where('id', $id) |
289
|
|
|
->where('locale', $locale) |
290
|
|
|
->get() |
291
|
|
|
->row_array(); |
292
|
|
|
|
293
|
|
|
if ($query) { |
294
|
|
|
return $query['name']; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Delete discount by id |
302
|
|
|
* @param int $id |
303
|
|
|
* @return boolean |
304
|
|
|
*/ |
305
|
|
|
public function deleteDiscountById($id) { |
306
|
|
|
$query = $this->db->from('mod_shop_discounts')->where('id', $id)->get()->row_array(); |
307
|
|
|
$discountType = $query['type_discount']; |
308
|
|
|
if (!$query) { |
309
|
|
|
return false; |
310
|
|
|
} |
311
|
|
|
try { |
312
|
|
|
$this->db->where('id', $id)->delete('mod_shop_discounts'); |
313
|
|
|
$this->db->where('id', $id)->delete('mod_shop_discounts_i18n'); |
314
|
|
|
$this->db->where('discount_id', $id)->delete('mod_discount_' . $discountType); |
315
|
|
|
return true; |
316
|
|
|
} catch (Exception $e) { |
317
|
|
|
return false; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Delete discount by id |
323
|
|
|
* @param (int) $id |
324
|
|
|
* @param (string) $entity |
325
|
|
|
* @return boolean |
326
|
|
|
*/ |
327
|
|
|
public function checkEntityExists($entity, $id) { |
328
|
|
|
|
329
|
|
|
switch ($entity) { |
330
|
|
|
case 'product': |
331
|
|
|
return $this->db->where('id', $id)->get('shop_products')->num_rows(); |
332
|
|
|
break; |
|
|
|
|
333
|
|
|
case 'category': |
334
|
|
|
return $this->db->where('id', $id)->get('shop_category')->num_rows(); |
335
|
|
|
break; |
|
|
|
|
336
|
|
|
case 'brand': |
337
|
|
|
return $this->db->where('id', $id)->get('shop_brands')->num_rows(); |
338
|
|
|
break; |
|
|
|
|
339
|
|
|
case 'user': |
340
|
|
|
return $this->db->where('id', $id)->get('users')->num_rows(); |
341
|
|
|
break; |
|
|
|
|
342
|
|
|
case 'group_user': |
343
|
|
|
return $this->db->where('id', $id)->get('shop_rbac_roles')->num_rows(); |
344
|
|
|
break; |
|
|
|
|
345
|
|
|
|
346
|
|
|
default: |
347
|
|
|
break; |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Install module |
353
|
|
|
*/ |
354
|
|
|
public function moduleInstall() { |
355
|
|
|
|
356
|
|
|
$column = $this->db->query("SHOW COLUMNS FROM `shop_orders` where `Field` = 'discount'")->num_rows(); |
357
|
|
|
if (!$column) { |
358
|
|
|
$sql = 'ALTER TABLE shop_orders ADD discount float(10,2);'; |
359
|
|
|
$this->db->query($sql); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
$column = $this->db->query("SHOW COLUMNS FROM `shop_orders` where `Field` = 'discount_info'")->num_rows(); |
363
|
|
|
if (!$column) { |
364
|
|
|
$sql = 'ALTER TABLE shop_orders ADD discount_info TEXT;'; |
365
|
|
|
$this->db->query($sql); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
$column = $this->db->query("SHOW COLUMNS FROM `shop_orders` where `Field` = 'origin_price'")->num_rows(); |
369
|
|
|
if (!$column) { |
370
|
|
|
$sql = 'ALTER TABLE shop_orders ADD origin_price float(10,2);'; |
371
|
|
|
$this->db->query($sql); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_shop_discounts` ( |
375
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
376
|
|
|
`key` VARCHAR(25) NULL , |
377
|
|
|
`active` TINYINT NULL , |
378
|
|
|
`max_apply` INT NULL , |
379
|
|
|
`count_apply` INT NULL , |
380
|
|
|
`date_begin` INT(11) NULL , |
381
|
|
|
`date_end` INT(11) NULL , |
382
|
|
|
`type_value` TINYINT NULL , |
383
|
|
|
`value` INT NULL , |
384
|
|
|
`type_discount` VARCHAR(15) NULL , |
385
|
|
|
PRIMARY KEY (`id`) , |
386
|
|
|
UNIQUE INDEX `key_UNIQUE` (`key` ASC) ) |
387
|
|
|
ENGINE = MyISAM |
388
|
|
|
DEFAULT CHARACTER SET = utf8 |
389
|
|
|
COLLATE = utf8_general_ci;'; |
390
|
|
|
$this->db->query($sql); |
391
|
|
|
|
392
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_shop_discounts_i18n` ( |
393
|
|
|
`id` INT NOT NULL , |
394
|
|
|
`locale` VARCHAR(5) NOT NULL , |
395
|
|
|
`name` VARCHAR(150) NULL , |
396
|
|
|
PRIMARY KEY (`id`,`locale`) ) |
397
|
|
|
ENGINE = MyISAM |
398
|
|
|
DEFAULT CHARACTER SET = utf8 |
399
|
|
|
COLLATE = utf8_general_ci;'; |
400
|
|
|
|
401
|
|
|
$this->db->query($sql); |
402
|
|
|
|
403
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_discount_product` ( |
404
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
405
|
|
|
`product_id` INT NULL , |
406
|
|
|
`discount_id` INT NULL , |
407
|
|
|
PRIMARY KEY (`id`), |
408
|
|
|
INDEX(`discount_id`), |
409
|
|
|
INDEX(`product_id`)) |
410
|
|
|
ENGINE = MyISAM |
411
|
|
|
DEFAULT CHARACTER SET = utf8 |
412
|
|
|
COLLATE = utf8_general_ci;'; |
413
|
|
|
$this->db->query($sql); |
414
|
|
|
|
415
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_discount_category` ( |
416
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
417
|
|
|
`category_id` INT NULL , |
418
|
|
|
`discount_id` INT NULL , |
419
|
|
|
PRIMARY KEY (`id`), |
420
|
|
|
INDEX(`discount_id`), |
421
|
|
|
INDEX(`category_id`)) |
422
|
|
|
ENGINE = MyISAM |
423
|
|
|
DEFAULT CHARACTER SET = utf8 |
424
|
|
|
COLLATE = utf8_general_ci;'; |
425
|
|
|
$this->db->query($sql); |
426
|
|
|
|
427
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_discount_user` ( |
428
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
429
|
|
|
`user_id` INT NULL , |
430
|
|
|
`discount_id` INT NULL , |
431
|
|
|
PRIMARY KEY (`id`), |
432
|
|
|
INDEX(`discount_id`), |
433
|
|
|
INDEX(`user_id`)) |
434
|
|
|
ENGINE = MyISAM |
435
|
|
|
DEFAULT CHARACTER SET = utf8 |
436
|
|
|
COLLATE = utf8_general_ci;'; |
437
|
|
|
$this->db->query($sql); |
438
|
|
|
|
439
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_discount_group_user` ( |
440
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
441
|
|
|
`group_id` INT NULL , |
442
|
|
|
`discount_id` INT NULL , |
443
|
|
|
PRIMARY KEY (`id`), |
444
|
|
|
INDEX(`discount_id`), |
445
|
|
|
INDEX(`group_id`)) |
446
|
|
|
ENGINE = MyISAM |
447
|
|
|
DEFAULT CHARACTER SET = utf8 |
448
|
|
|
COLLATE = utf8_general_ci;'; |
449
|
|
|
$this->db->query($sql); |
450
|
|
|
|
451
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_discount_comulativ` ( |
452
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
453
|
|
|
`discount_id` INT NULL , |
454
|
|
|
`begin_value` INT NULL , |
455
|
|
|
`end_value` INT NULL , |
456
|
|
|
PRIMARY KEY (`id`), |
457
|
|
|
INDEX(`discount_id`)) |
458
|
|
|
ENGINE = MyISAM |
459
|
|
|
DEFAULT CHARACTER SET = utf8 |
460
|
|
|
COLLATE = utf8_general_ci;'; |
461
|
|
|
$this->db->query($sql); |
462
|
|
|
|
463
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_discount_all_order` ( |
464
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
465
|
|
|
`for_autorized` TINYINT NULL , |
466
|
|
|
`discount_id` INT NULL , |
467
|
|
|
`is_gift` TINYINT NULL , |
468
|
|
|
`begin_value` FLOAT NULL , |
469
|
|
|
PRIMARY KEY (`id`), |
470
|
|
|
INDEX(`discount_id`)) |
471
|
|
|
ENGINE = MyISAM |
472
|
|
|
DEFAULT CHARACTER SET = utf8 |
473
|
|
|
COLLATE = utf8_general_ci;'; |
474
|
|
|
$this->db->query($sql); |
475
|
|
|
|
476
|
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `mod_discount_brand` ( |
477
|
|
|
`id` INT NOT NULL AUTO_INCREMENT , |
478
|
|
|
`brand_id` INT NULL , |
479
|
|
|
`discount_id` INT NULL , |
480
|
|
|
PRIMARY KEY (`id`), |
481
|
|
|
INDEX(`discount_id`), |
482
|
|
|
INDEX(`brand_id`)) |
483
|
|
|
ENGINE = MyISAM |
484
|
|
|
DEFAULT CHARACTER SET = utf8 |
485
|
|
|
COLLATE = utf8_general_ci;'; |
486
|
|
|
$this->db->query($sql); |
487
|
|
|
|
488
|
|
|
$this->db->where('name', 'mod_discount'); |
489
|
|
|
$this->db->update('components', ['enabled' => 1, 'autoload' => 1]); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Delete module |
494
|
|
|
*/ |
495
|
|
|
public function moduleDelete() { |
496
|
|
|
|
497
|
|
|
$this->load->dbforge(); |
498
|
|
|
$this->dbforge->drop_table('mod_shop_discounts'); |
499
|
|
|
$this->dbforge->drop_table('mod_shop_discounts_i18n'); |
500
|
|
|
$this->dbforge->drop_table('mod_discount_brand'); |
501
|
|
|
$this->dbforge->drop_table('mod_discount_all_order'); |
502
|
|
|
$this->dbforge->drop_table('mod_discount_comulativ'); |
503
|
|
|
$this->dbforge->drop_table('mod_discount_group_user'); |
504
|
|
|
$this->dbforge->drop_table('mod_discount_user'); |
505
|
|
|
$this->dbforge->drop_table('mod_discount_category'); |
506
|
|
|
$this->dbforge->drop_table('mod_discount_product'); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Validation atribute lables |
511
|
|
|
* @return array |
512
|
|
|
*/ |
513
|
|
|
public function attributeLabels() { |
514
|
|
|
return [ |
515
|
|
|
'value' => ShopCore::t(lang('Value', 'mod_discount')), |
516
|
|
|
]; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Validation attribute rules |
521
|
|
|
* @return array |
|
|
|
|
522
|
|
|
*/ |
523
|
|
|
public function rules() { |
524
|
|
|
return [ |
525
|
|
|
[ |
526
|
|
|
'field' => 'value', |
527
|
|
|
'label' => lang('Value', 'mod_discount'), |
528
|
|
|
'rules' => 'required|integer', |
529
|
|
|
], |
530
|
|
|
]; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* Check range for cumulative discount |
535
|
|
|
* @param array $data |
|
|
|
|
536
|
|
|
* @return boolean |
537
|
|
|
*/ |
538
|
|
|
public function checkRangeForCumulativeDiscount($data = FALSE, $id = null) { |
539
|
|
|
if (!$data) { |
540
|
|
|
return FALSE; |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
$sql = 'SELECT * FROM `mod_discount_comulativ` '; |
544
|
|
|
if ($data['end_value'] != NULL) { |
545
|
|
|
if ($id != NULL) { |
546
|
|
|
$sql .= 'WHERE `discount_id` <> ' . $id . ' AND ((`begin_value` BETWEEN ' . $data['begin_value'] . ' AND ' . $data['end_value'] . ') |
547
|
|
|
OR (`end_value` BETWEEN ' . $data['begin_value'] . ' AND ' . $data['end_value'] . ') |
548
|
|
|
OR (`begin_value` <= ' . $data['begin_value'] . ' AND `end_value` >= ' . $data['end_value'] . '))'; |
549
|
|
|
} else { |
550
|
|
|
$sql .= 'WHERE (`begin_value` BETWEEN ' . $data['begin_value'] . ' AND ' . $data['end_value'] . ') |
551
|
|
|
OR (`end_value` BETWEEN ' . $data['begin_value'] . ' AND ' . $data['end_value'] . ') |
552
|
|
|
OR (`begin_value` <= ' . $data['begin_value'] . ' AND `end_value` >= ' . $data['end_value'] . ')'; |
553
|
|
|
} |
554
|
|
|
} else { |
555
|
|
|
if ($id != NULL) { |
556
|
|
|
$sql .= 'WHERE `discount_id` <> ' . $id . ' AND ' . $data['begin_value'] . ' < `begin_value`'; |
557
|
|
|
} else { |
558
|
|
|
$sql .= 'WHERE ' . $data['begin_value'] . ' < `begin_value`'; |
559
|
|
|
} |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
$query = $this->db->query($sql)->row_array(); |
563
|
|
|
|
564
|
|
|
if ($query) { |
|
|
|
|
565
|
|
|
return TRUE; |
566
|
|
|
} |
567
|
|
|
return FALSE; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
} |