forked from cunaedy/Cart-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoupon.php
More file actions
59 lines (46 loc) · 2.29 KB
/
coupon.php
File metadata and controls
59 lines (46 loc) · 2.29 KB
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
<?php
require './includes/user_init.php';
$code = get_param('code');
$view = get_param('view');
if ($view) {
$mode = 'view';
} else {
$mode = 'enter';
}
switch ($mode) {
case 'enter':
// get coupon info
$row = sql_qquery("SELECT * FROM ".$db_prefix."gift WHERE gift_code='$code' LIMIT 1");
// single use
if (($row['coupon_type'] == 'once') && (($row['redeem_user_id'] != '') || ($row['redeem_order_id'] != ''))) {
msg_die($lang['msg']['coupon_err']);
}
// update coupon -> change ownership to $current_user_id
if ($sql_today > $row['valid_date']) {
msg_die($lang['msg']['coupon_err']);
}
// any existing coupon? reset user id to empty
sql_query("UPDATE ".$db_prefix."gift SET redeem_user_id='' WHERE redeem_user_id='$current_user_id' AND redeem_order_id=''");
// single use -> add user_id to coupon
if ($row['coupon_type'] == 'once') {
sql_query("UPDATE ".$db_prefix."gift SET redeem_user_id='$current_user_id' WHERE gift_code='$code' LIMIT 1");
}
// multiuse -> create new coupon
if ($row['coupon_type'] == 'multi') {
$code = $code.'.'.random_str(6);
sql_query("INSERT INTO ".$db_prefix."gift SET gift_code = '$code', master_code = '$row[gift_code]', gift_value = '$row[gift_value]',
gift_pct='$row[gift_pct]', min_purchase = '$row[min_purchase]', valid_date = '$row[valid_date]', coupon_type = 'once', redeem_user_id = '$current_user_id'");
}
$msg = sprintf($lang['l_coupon_info'], $row['gift_pct'] ? round($row['gift_value']).'%' : num_format($row['gift_value'], 0, 1), num_format($row['min_purchase'], 0, 1), convert_date($row['valid_date']));
msg_die(sprintf($lang['msg']['coupon_ok'], $msg));
break;
case 'view':
// get coupon info
$row = sql_qquery("SELECT * FROM ".$db_prefix."gift WHERE gift_code='$view' LIMIT 1");
if (empty($row)) {
msg_die($lang['msg']['coupon_err']);
}
$msg = sprintf($lang['l_coupon_info'], $row['gift_pct'] ? round($row['gift_value']).'%' : num_format($row['gift_value'], 0, 1), num_format($row['min_purchase'], 0, 1), convert_date($row['valid_date']));
msg_die(sprintf($lang['msg']['coupon_ok'], $msg));
break;
}