Coupon.php
22.6 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/**
* tpshop
* ============================================================================
* 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
* 网站地址: http://www.tp-shop.cn
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* Date: 2015-12-11
*/
namespace app\admin\controller;
use think\AjaxPage;
use think\Page;
use think\Db;
class Coupon extends Base {
/**----------------------------------------------*/
/* 优惠券控制器 */
/**----------------------------------------------*/
/*
* 优惠券类型列表
*/
public function index(){
//获取优惠券列表
$pagenum=10;//每页显示多少条
if ((int)I('pagenum/s')>0)
{
$pagenum=I('pagenum/s');
}
$couponname = I('couponname');
$coupontype = I('coupontype');
$condition['store_id']=getAdmStoId();
if(!empty($couponname)){
$condition['name'] = array('like',"%$couponname%");
}
if(!empty($coupontype)){
$condition['type'] = array('like',"%$coupontype%");
}
$count = M('coupon')->where($condition)->count();
$Page = new Page($count,$pagenum);
$show = $Page->show();
$lists = M('coupon')->where($condition)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('lists',$lists);
$this->assign('store_id',getAdmStoId());
$this->assign('pager',$Page);// 赋值分页输出
$this->assign('page',$show);// 赋值分页输出
$this->assign('coupons',C('COUPON_TYPE'));
$this->assign('pagenum',$pagenum);
// upload_ylp_log('优惠券');
return $this->fetch('',getAdmStoId());
}
/*
* 添加编辑一个优惠券类型
*/
public function coupon_info(){
if(IS_POST){
ClearALLCache();
delFile(TEMP_PATH."/".getAdmStoId());
$data = I('post.');
$data['send_start_time'] = strtotime($data['send_start_time']);
$data['send_end_time'] = strtotime($data['send_end_time']);
$data['use_start_time'] = strtotime($data['use_start_time']);
if($data['send_start_time'] > $data['send_end_time']){
$this->error('发放日期结束时间必须大于开始时间');
}
//当有效时间为0,表示无限制
if($data['endtype']==1){
if(empty($data['days'])){
$data['use_end_time']="";
}
}
if ($data['use_end_time']!="") {
$data['use_end_time'] = strtotime($data['use_end_time']);
if($data['send_start_time'] > $data['use_end_time'] && $data['endtype']==0){
$this->error('使用日期结束时间必须大于开始时间');
}
}
if($data['money'] <= 0){
$this->error('优惠券面额必须大于0');
}
if($data['money'] >= $data['condition']){
$this->error('优惠券面额不能大于等于消费金额');
}
if (empty($data['useobjecttype']))
{
$data['useobjectname']="";
$data['useobjectno']="";
$data['useobjectid']="";
}
if(empty($data['id'])){
$data['store_id']=getAdmStoId();
$data['add_time'] = time();
// $finddata = M('coupon')->where(array('store_id'=>getAdmStoId(),'type'=>$data['type']))->find();
// if ($finddata)
// {
// $this->error('当前类型已添加,请返回列表进行编辑!');
// }
// else {
// $row = M('coupon')->add($data);
// }
// upload_ylp_log('添加优惠券');
$row = M('coupon')->add($data);
}else{
// upload_ylp_log('修改优惠券');
$row = M('coupon')->where(array('store_id'=>getAdmStoId(),'id'=>$data['id']))->save($data);
}
if(!$row)
$this->error('编辑失败或者没有做修改');
$this->success('编辑代金券成功',U('Admin/Coupon/index'));
exit;
}
$cid = I('get.id/d');
$type = I('type/d',0);
if($cid){
$coupon = M('coupon')->where(array('store_id'=>getAdmStoId(),'id'=>$cid))->find();
$this->assign('coupon',$coupon);
}else{
if (empty($type)){
$def['type']=1;//默认优惠券类型
}else{
$def['type']=5;
}
$def['send_start_time'] = strtotime("+0 day");
$def['send_end_time'] = strtotime("+1 month");
$def['use_start_time'] = strtotime("+0 day");
$def['use_end_time'] = strtotime("+1 year");
$this->assign('coupon',$def);
}
$feemail=M('area_feemail')->where('store_id='.getAdmStoId())->order('id desc')->select();
$this->assign('feemail',$feemail);
return $this->fetch('',getAdmStoId());
}
/*
* 优惠券发放
*/
public function make_coupon(){
//获取优惠券ID
$cid = I('get.id/d');
$type = I('get.type');
//查询是否存在优惠券
$data = M('coupon')->where(array('store_id'=>getAdmStoId(),'id'=>$cid))->find();
$remain = $data['createnum'] - $data['send_num'];//剩余派发量
if($remain<=0) $this->error($data['name'].'已经发放完了');
if(!$data) $this->error("优惠券类型不存在");
if($type != 4) $this->error("该优惠券类型不支持发放");
if(IS_POST){
ClearALLCache();
delFile(TEMP_PATH."/".getAdmStoId());
$num = I('post.num/d');
if($num>$remain) $this->error($data['name'].'发放量不够了');
if(!$num > 0) $this->error("发放数量不能小于0");
$add['cid'] = $cid;
$add['type'] = $type;
$add['send_time'] = time();
$add['store_id']=getAdmStoId();
for($i=0;$i<$num; $i++){
do{
$code = get_rand_str(8,0,1);//获取随机8位字符串
$check_exist = M('coupon_list')->where(array('store_id'=>getAdmStoId(),'code'=>$code))->find();
}while($check_exist);
$add['code'] = $code;
M('coupon_list')->add($add);
}
M('coupon')->where("id",$cid)->setInc('send_num',$num);
// upload_ylp_log('发放优惠券');
adminLog("发放".$num.'张'.$data['name']);
$this->success("发放成功",U('Admin/Coupon/index'));
exit;
}
$this->assign('coupon',$data);
return $this->fetch('',getAdmStoId());
}
public function ajax_get_user(){
//搜索条件
$condition = array();
I('mobile') ? $condition['mobile'] = I('mobile') : false;
I('email') ? $condition['email'] = I('email') : false;
$nickname = I('nickname');
if(!empty($nickname)){
$condition['nickname'] = array('like',"%$nickname%");
}
$condition['store_id']=getAdmStoId();
$model = M('users');
$count = $model->where($condition)->count();
$Page = new AjaxPage($count,10);
foreach($condition as $key=>$val) {
$Page->parameter[$key] = urlencode($val);
}
$show = $Page->show();
$userList = $model->where($condition)->order("user_id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
$user_level = M('user_level')->where(" store_id=".getAdmStoId()."")->getField('level_id,level_name',true);
$this->assign('user_level',$user_level);
$this->assign('userList',$userList);
$this->assign('page',$show);
$this->assign('pager',$Page);
return $this->fetch('',getAdmStoId());
}
public function send_coupon(){
$cid = I('cid/d');
if(IS_POST){
ClearALLCache();
delFile(TEMP_PATH."/".getAdmStoId());
$level_id = I('level_id');
$user_id = I('user_id/a');
$insert = '';
$coupon = M('coupon')->where("id",$cid)->find();
if($coupon['createnum']>0){
$remain = $coupon['createnum'] - $coupon['send_num'];//剩余派发量
if($remain<=0) $this->error($coupon['name'].'已经发放完了');
}
if(empty($user_id) && $level_id>=0){
if($level_id==0){
$user = M('users')->where(" store_id=".getAdmStoId()." and is_lock=0")->select();
}else{
$user = M('users')->where(" and store_id=".getAdmStoId()." and is_lock=0 and level_id=".$level_id."")->select();
}
if($user){
$able = count($user);//本次发送量
if($coupon['createnum']>0 && $remain<$able){
$this->error($coupon['name'].'派发量只剩'.$remain.'张');
}
foreach ($user as $k=>$val){
$time = time();
$insert[] = ['cid' => $cid, 'type' => 1, 'uid' => $val['user_id'], 'send_time' => $time,'store_id'=>getAdmStoId()];
}
}else{
$this->error("未找到该等级的用户");
exit;
}
}else{
$able = count($user_id);//本次发送量
if($coupon['createnum']>0 && $remain<$able){
$this->error($coupon['name'].'派发量只剩'.$remain.'张');
}
foreach ($user_id as $k=>$v){
$time = time();
$insert[] = ['cid' => $cid, 'type' => 1, 'uid' => $v, 'send_time' => $time,'store_id'=>getAdmStoId()];
}
if(empty($insert)){
$this->error("未找到用户");
exit;
}
}
DB::name('coupon_list')->insertAll($insert);
M('coupon')->where("id",$cid)->setInc('send_num',$able);
adminLog("发放".$able.'张'.$coupon['name']);
// upload_ylp_log('发放优惠券');
$this->success("发放成功");
exit;
}
$level = M('user_level')->where(" store_id=".getAdmStoId()."")->select();
$this->assign('level',$level);
$this->assign('cid',$cid);
return $this->fetch('',getAdmStoId());
}
public function send_cancel(){
}
/*
* 删除优惠券类型
*/
public function del_coupon(){
ClearALLCache();
delFile(TEMP_PATH."/".getAdmStoId());
//获取优惠券ID
$cid = I('get.id/d');
//查询是否存在优惠券
$row = M('coupon')->where(array('store_id'=>getAdmStoId(),'id'=>$cid))->delete();
if($row){
// upload_ylp_log('删除优惠券类型');
//删除此类型下的优惠券
M('coupon_list')->where(array('cid'=>$cid))->delete();
$this->success("删除成功");
}else{
$this->error("删除失败");
}
}
/*
* 优惠券详细查看
*/
public function coupon_list(){
//获取优惠券ID
$cid = I('get.id/d');
//查询是否存在优惠券
$check_coupon = M('coupon')->field('id,type')->where(array('store_id'=>getAdmStoId(),'id'=>$cid))->find();
if(!$check_coupon['id'] > 0)
$this->error('不存在该类型优惠券');
//查询该优惠券的列表的数量
$sql = "SELECT count(1) as c FROM __PREFIX__coupon_list l ".
"LEFT JOIN __PREFIX__coupon c ON c.id = l.cid ". //联合优惠券表查询名称
"LEFT JOIN __PREFIX__order o ON o.order_id = l.order_id ". //联合订单表查询订单编号
"LEFT JOIN __PREFIX__users u ON u.user_id = l.uid WHERE l.cid = :cid and l.store_id=:store_id"; //联合用户表去查询用户名
$count = DB::query($sql,['cid' => $cid,'store_id'=>getAdmStoId()]);
$count = $count[0]['c'];
$Page = new Page($count,10);
$show = $Page->show();
//查询该优惠券的列表
$sql = "SELECT l.*,c.name,o.order_sn,u.nickname FROM __PREFIX__coupon_list l ".
"LEFT JOIN __PREFIX__coupon c ON c.id = l.cid ". //联合优惠券表查询名称
"LEFT JOIN __PREFIX__order o ON o.order_id = l.order_id ". //联合订单表查询订单编号
"LEFT JOIN __PREFIX__users u ON u.user_id = l.uid WHERE l.cid = :cid and l.store_id=:store_id". //联合用户表去查询用户名
" limit {$Page->firstRow} , {$Page->listRows}";
$coupon_list = DB::query($sql,['cid' => $cid,'store_id'=>getAdmStoId()]);
$this->assign('coupon_type',C('COUPON_TYPE'));
$this->assign('type',$check_coupon['type']);
$this->assign('lists',$coupon_list);
$this->assign('page',$show);
$this->assign('pager',$Page);
return $this->fetch('',getAdmStoId());
}
/*
* 删除一张优惠券
*/
public function coupon_list_del(){
//获取优惠券ID
$cid = I('get.id');
if(!$cid)
$this->error("缺少参数值");
//查询是否存在优惠券
$row = M('coupon_list')->where(array('id'=>$cid,'store_id'=>getAdmStoId()))->delete();
if(!$row)
$this->error('删除失败');
// upload_ylp_log('删除一张优惠券');
$this->success('删除成功');
}
//统计
public function statistics()
{
$pagenum = 10;//每页显示多少条
if ((int)I('pagenum/s') > 0) {
$pagenum = I('pagenum/s');
}
$p = I('p/d', 1);
$start = urldecode(I('start', date('Y-m-d H:i:s', strtotime('-1 month'))));
$end = urldecode(I('end', date('Y-m-d H:i:s')));
$key = trim(urldecode(urldecode(I('key/s'))));
$no = I('no/s');
$is = I('is/d');
$sum = I('sum/d');
$type = trim(urldecode(urldecode(I('type/s'))));
//有账套
if (getERPId()) {
$where['SendMan'] = ['in', '免费发放,注册发放,邀请发放,优惠活动'];
if ($key) {
$where['VIPName|MobileTel'] = ['like', '%' . $key . '%'];
}
if ($no) {
$where['CashRepNo'] = ['like', '%' . $no . '%'];
}
if ($is) {
$where['IsUse'] = $is - 1;
}
if ($type) {
$where['SendMan'] = $type;
}
if ($sum) {
$where['Sum'] = $sum;
}
$where['Billdate'] = ['between', [$start, $end]];
$tk['api_token']=tpCache("shop_info.api_token",getAdmStoId());
$res = getApiData("wxd.vip.cash.list.get", $tk['api_token'], null, $where, $p, $pagenum);
$data = json_decode($res, true);
$count = (int)$data['count'];
$list = $data['data'];
$Page = new Page($count, $pagenum);
$show = $Page->show();
if (!empty($list)) {
foreach ($list as $k => $v) {
if ($v['BeginDate'])
$list[$k]['BeginDate'] = substr($v['BeginDate'], 0, -4);
if ($v['ValidDate'])
$list[$k]['ValidDate'] = date('Y-m-d 23:59:59',strtotime($v['ValidDate']));
}
session('excelstatistics_'.getAdmStoId(),$list);
}
}else
{
if ($key) {
$where['b.nickname|b.vipname|b.mobile'] = ['like', '%' . $key . '%'];
}
if ($no) {
$where['a.code'] = ['like', '%' . $no . '%'];
}
if ($is) {
$where['a.is_user'] = $is - 1;
}
if ($type) {
$where['a.remark'] = $type;
}
if ($sum) {
$where['a.sum'] = $sum;
}
$where['a.send_time'] = ['between', [strtotime($start), strtotime($end)]];
$filed="a.code as CashRepNo,a.sum as Sum,a.buysum as BuySum,a.send_time as BillDate,a.remark as SendMan,a.is_user as IsUse,b.mobile as MobileTel,b.nickname,b.vipname as VIPName,a.begintime,a.validtime";
$model=M('coupon_list');
$count = $model->alias('a')
->join('users b','a.uid=b.user_id','left')
->where($where)->count();
$Page = $pager = new AjaxPage($count,$pagenum);
$show = $Page->show();
$list = $model->alias('a')
->join('users b','a.uid=b.user_id','left')
->where($where)->order('send_time DESC')
->field($filed)
->limit($Page->firstRow.','.$Page->listRows)
->select();
if($list)
{
foreach ($list as $k => $v) {
if ($v['begintime'])
$list[$k]['BeginDate'] =date('Y-m-d',$v['begintime']);
if ($v['validtime'])
$list[$k]['ValidDate'] =date('Y-m-d 23:59:59',$v['validtime']);
if ($v['BillDate'])
$list[$k]['BillDate'] =date('Y-m-d h:i:s',$v['BillDate']);
}
session('excelstatistics_'.getAdmStoId(),$list);
}
}
$this->assign([
'start_time' => $start,
'pagenum' => $pagenum,
'end_time' => $end,
'key' => $key,
'no' => $no,
'is' => $is,
'sum' => $sum,
'type' => $type,
'pager' => $Page,
'page' => $show,
'list' => $list
]);
return $this->fetch('', getAdmStoId());
}
//会员管理导出
public function exportreport_statistics()
{
$res = session('excelstatistics_' . getAdmStoId());
$page=I('page/d',1);
if (empty($res)) {
return json(array('code' => -1, 'msg' => '无数据'));
}
$strTable = '<table border="1">';
$strTable .= '<tr style="font-weight: bold;">';
$strTable .= '<td style="text-align:center;width:150px;" >会员姓名</td>';
$strTable .= '<td style="text-align:center;" width="100" >手机</td>';
$strTable .= '<td style="text-align:center;" width="100" >券号</td>';
$strTable .= '<td style="text-align:center;" width="100" >券面值(元)</td>';
$strTable .= '<td style="text-align:center;" width="100" >需购买额(元)</td>';
$strTable .= '<td style="text-align:center;" width="140">开始时间</td>';
$strTable .= '<td style="text-align:center;" width="140">结束时间</td>';
$strTable .= '<td style="text-align:center;" width="140">领取时间</td>';
$strTable .= '<td style="text-align:center;" width="100">发放类型</td>';
$strTable .= '<td style="text-align:center;" width="100">状态</td>';
$strTable .= '</tr>';
if (is_array($res)) {
foreach ($res as $k => $val) {
$strTable .= '<tr>';
$strTable .= '<td style="text-align:left;" > ' . $val['VIPName'] . '</td>';
$strTable .= '<td style="text-align:left;" > ' . $val['MobileTel'] . ' </td>';
$strTable .= '<td style="text-align:center;" > ' . $val['CashRepNo'] . ' </td>';
$strTable .= '<td style="text-align:center;" >' . $val['Sum'] . '</td>';
$strTable .= '<td style="text-align:center;" >' . $val['BuySum'] . '</td>';
$strTable .= '<td style="text-align:center;" >' . ($val['BeginDate']==""?'无限制':$val['BeginDate']) . '</td>';
$strTable .= '<td style="text-align:center;" >' . ($val['ValidDate']==""?'无限制':$val['ValidDate']) . '</td>';
$strTable .= '<td style="text-align:center;" >' . $val['BillDate'] . '</td>';
$strTable .= '<td style="text-align:center;" >' . $val['SendMan'] . '</td>';
$strTable .= '<td style="text-align:center;" >' . ($val['IsUse']==0?'未使用':'已使用') . '</td>';
}
}
$strTable .= '</table>';
echo $strTable;
unset($res);
downloadExcel($strTable, '优惠券领取记录');
exit();
}
//season 20181026 选择性导入
public function useobjecttype()
{
$pagenum = 10;
$type = I('type/d', 1);
$parentclassno = I('parentclassno');
$this->assign('type', $type);
$this->assign('pagenum', $pagenum);
$this->assign('parentclassno', $parentclassno);
return $this->fetch('', getAdmStoId());
}
public function ajax_useobjecttype()
{
$pagenum = I('pagenum/d', 10);
$type = I('type/d', 1);
$page = I('p/d', 1);
$parentclassno = I('parentclassno');
if (empty($parentclassno))
{
$parentclassno="-1";
}
$stoid = getAdmStoId();
$erpid = getERPId();
// $tk = tpCache('shop_info.api_token', $stoid);
$keywords = I('keywords');
// $where=" ParentClassNo='".$parentclassno."'";
//
// if ($keywords) {
// $where.=" and cname like '%$keywords%'";
// }
switch ($type)
{
case 1:
$api_name='/api/erp/msg/brand/page';
break;
case 2:
$api_name='/api/erp/msg/class/page';
break;
case 11:
$api_name='/api/erp/msg/use/page';
break;
case 12:
$api_name='/api/erp/msg/otherWare/page';
break;
}
$wdata['ParentClassNo'] = urlencode($parentclassno);
$wdata['CName'] = urlencode($keywords);
$accdb = tpCache("shop_info.ERPId", $stoid);
$res = getApiData_java($api_name, $accdb, $wdata, $page, $pagenum);
// $res = getApiData($api_name, $tk, null, $where, $page, $pagenum);
$data = json_decode($res, true);
$brandlist=$data['data']['pageData'];
$count=0;
if ($data['data']['total']) {
$count = $data['data']['total'];
}
$Page = new AjaxPage($count, $pagenum);
$show = $Page->show();//分页显示输出
$this->assign('page', $show);//赋值分页输出
$this->assign('brandlist', $brandlist);
$this->assign('pager', $Page);//赋值分页输出
$this->assign('type', $type);
return $this->fetch('', getAdmStoId());
}
}