Prom.php
4.3 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
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
<?php
//促销处理类
class Prom{
private $total_amount = 0.0;
//活动类型: 0满额打折 1满额优惠金额 2满额送倍数积分 3满额送优惠券 4满额免运费
private $prom_order = array(0,1,2,3,4);
//活动类型: 0直接打折 1减价优惠 2固定金额 3买就赠优惠券 4买M件送N件
private $prom_goods = array(0,1,2,3,4);
public function __construct($amount=0){
if(is_numeric($amount)){
$this->total_amount = $amount;
}else{
//显示错误
}
}
public function setAmount($amount=0){
if(is_numeric($amount)){
$this->total_amount = $amount;
}else{
$this->total_amount = 0;
}
}
//取得满足条件的规则
public function meetProms($group_id=0){
$model = new Model("prom_order");
$time = date('Y-m-d H:i:s');
$where = " `money` <= ".$this->total_amount." and is_close=0 and start_time < '".$time."' and end_time > '".$time."'";
$safebox = Safebox::getInstance();
$user = $safebox->get('user');
$group_id = ',0,';
if(isset($user['group_id'])) $group_id = ','.$user['group_id'].',';
if($group_id){
$where.=" and CONCAT(',',`group`,',') like '%".$group_id."%'";
}
$proms = $model->where($where)->order("type,cast(expression as decimal) desc")->findAll();
return $proms;
}
public function parsePorm($prom){
$type = $prom['type'];
$expr = $prom['expression'];
$result = array('total_amount'=>$this->total_amount,'value'=>0.00);
switch ($type) {
case '0':
$result['total_amount'] = $this->total_amount*($expr)/100;
$result['note'] = "省".($this->total_amount*(100-$expr)/100)."(".($expr/10)."折)";
$result['value'] = floatval($this->total_amount*(100-$expr)/100);
break;
case '1':
$result['total_amount'] = $this->total_amount-$expr;
$result['note'] = "省".$expr."(立减".$expr.")";
$result['value'] = floatval($expr);
break;
case '2':
$result['value'] = intval($expr);
$result['note'] = "送".$expr."倍积分";
break;
case '3':
$model = new Model("voucher_template");
$voucher = $model->where("id=".Filter::int($expr))->find();
if($voucher){
$result['note'] = "面值".$voucher['value']."优惠券";
}else{
$result['note'] = "所送的优惠券已不存在";
}
break;
case '4':
$result['note'] = "免运费";
break;
}
return $result;
}
public function prom_goods($goods){
$price = $goods['sell_price'];
$nums = $goods['goods_nums'];
$result = array('real_price'=>$price,'note'=>'','minus'=>"-0");
$prom_id = $goods['prom_id'];
if($prom_id>0){
$model = new Model('prom_goods');
$time = date('Y-m-d H:i:s');
$prom = $model->where("id=$prom_id and is_close=0 and start_time < '".$time."' and end_time > '".$time."'")->find();
$safebox = Safebox::getInstance();
$user = $safebox->get('user');
$group_id = ',0,';
if(isset($user['group_id'])) $group_id = ','.$user['group_id'].',';
if($prom && stripos(','.$prom['group'].',',$group_id)!==false){
$type = $prom['type'];
$expr = $prom['expression'];
$result['prom'] = $prom;
//0直接打折 1减价优惠 2固定金额 3买就赠优惠券 4买M件送N件
switch ($type) {
case '0':
$result['real_price'] = $price*$expr/100;
$result['minus'] = "*".($expr/100);
$result['note'] = '直接'.($expr/10)."折";
break;
case '1':
$result['real_price'] = ($price-$expr)>0?($price-$expr):0;
$result['real_price'] = sprintf("%01.2f",$result['real_price']);
$result['minus'] = "-".$expr;
$result['note'] = "立减".$expr;
break;
case '2':
$result['real_price'] = sprintf("%01.2f",$expr);
$result['minus'] = "*0+".$expr;
$result['note'] = "促销价".$expr;
break;
case '3':
$model = new Model("voucher_template");
$voucher = $model->where("id=".$expr)->find();
if($voucher){
$result['note'] = "送".$voucher['name']."优惠券,面值".$voucher['value'];
}else{
$result['note'] = "所送的优惠券已不存在";
}
break;
case '4':
$expr = explode('/', $expr);
if($nums>=$expr[0]) $result['note'] = "满".$expr[0]."件送".$expr[1];
break;
}
}
}
return $result;
}
}