Libao.php
27.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
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018-05-09
* Time: 17:12
*/
namespace app\mobile\controller;
use app\erpuser\controller\Integral;
use think\Db;
use think\Page;
use think\Cookie;
use think\Cache;
use think\AjaxPage;
class Libao extends MobileBase
{
public $user_id = 0;
public $user = array();
/**
* 析构流函数
*/
public function _initialize()
{
parent::_initialize();
$sid = I('stoid');
if (session('?user')) {
$user = session('user');
unset($_SESSION['user']);
$user0 = M('users')->where("user_id", $user['user_id'])->where("store_id", $sid)->find();
session('user', $user0); //覆盖session 中的 user
$this->user = $user0;
$this->user_id = $user0['user_id'];
$this->assign('user', $user0); //存储用户信息
} else {
$usrid = Cookie::get('user_id');
if ($usrid) {
$user0 = M('users')->where("user_id", $usrid)->where("store_id", $sid)->find();
session('user', $user0); //覆盖session 中的 user
$this->user = $user0;
$this->user_id = $user0['user_id'];
$this->assign('user', $user0); //存储用户信息
}
}
$nologin = ['index','lbdetails', 'lbquan', 'libaoorder'];
if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
if (!in_array(ACTION_NAME, $nologin)) {
if (($this->pm_erpid && (!$user || !$user['erpvipid'])) || (empty($this->pm_erpid) && !$user || !$user['mobile'])) {
$this->redirect(U('mobile/User/login', array('stoid' => getMobileStoId())));
exit;
}
else if(ACTION_NAME!='vipcard') //已绑定会员
{
$storeconfig=M('store_config')->where("store_id",getMobileStoId())->field('switch_list')->find();
$rank_switch=json_decode($storeconfig['switch_list'],true)['rank_switch'];
if($rank_switch==1 && empty($this->user['card_field']))//要求商城会员必须开通等级卡(商家开启等级卡)
{
$this->redirect(U('mobile/User/vipcard', array('stoid' => getMobileStoId())));
exit;
}
}
}
$order_status_coment = array(
'WAITPAY' => '待付款 ', //订单查询状态 待支付
'WAITSEND' => '待发货', //订单查询状态 待发货
'WAITRECEIVE' => '待收货', //订单查询状态 待收货
'WAITCCOMMENT' => '待评价', //订单查询状态 待评价
);
$this->assign('order_status_coment', $order_status_coment);
}
public function index()
{
$p = I('get.p/d', 1);
$pagenum = 10;
$where = array('store_id' => getMobileStoId(), 'isdel' => 0, 'startime' => array('elt', time()), ' endtime' => array('egt', time()));
$count = M('libao_form')->alias('a')->where($where)->count(1); //总用户量
$Page = $pager = new AjaxPage($count, $pagenum);
$show = $Page->show();
$list = M('libao_form')
->where($where)
->order('sort_order asc,id desc')
->limit($Page->firstRow . ',' . $Page->listRows)
->select();
if ($list) {
foreach ($list as $k => $vo) {
$list[$k]['curnum'] = $vo['num'] - $vo['salenum'];
}
$this->assign('list', $list);
}
if ($count > $p * $pagenum) {
$this->assign('mshow', 1);
} else {
$this->assign('mshow', 0);
}
if (I('is_ajax')) {
return $this->fetch('ajax_index', getMobileStoId());
}
return $this->fetch('', getMobileStoId());
}
//礼包礼品详情
public function lbdetails()
{
$id=I('id/d');
$myid=I('mid');
$storeid=getMobileStoId();
$data=M('libao_formvip')->where(array('user_id'=>$this->user_id,'id'=>$myid,'fbillstate'=>1))->select();
if($data[0])
{
$id=$data[0]['lbid'];
$this->assign('mid',$myid);
$this->assign('isget',$data[0]['isget']);
$this->assign('expdate',$data[0]['expdate']);
}
if($id)
{
$list=M('libao_form')->where(array('store_id'=>$storeid,'id'=>$id,'lbtype'=>1))->find();
if ($list) {
$isstart = 1;
if ($list['startime'] > time()) {
$isstart = 0;
}
if ($list['endtime'] < time()) {
$isstart = 2;
}
$list1 = M('libao_list')->where('lbid=' . $id)->select();
if ($myid) {
unset($list1);
$list1 = M('libao_listvip')->where('lbvipid=' . $myid)->select();
foreach ($list1 as $k => $voo) {
$list1[$k]['alsonum'] = $voo['goods_num'] - $voo['usenum'];//剩余次数
}
}
$this->assign('isstart', $isstart);
$this->assign('list', $list);
$this->assign('list1', $list1);
}
}
return $this->fetch('', getMobileStoId());
}
//我的礼包
public function mylibao()
{
$p = I('get.p/d', 1);
$pagenum = 10;
$where =array('a.user_id'=>$this->user_id,'a.fbillstate'=>1);
$count = M('libao_formvip')->alias('a')->where($where)->count(1); //总用户量
$Page = $pager = new AjaxPage($count, $pagenum);
$show = $Page->show();
$list=M('libao_formvip')
->alias('a')
->join('libao_form b','a.lbid=b.id','left')
->where($where)
->field(' b.*,a.id as lbvipid,a.expdate')
->order('a.id desc')
->limit($Page->firstRow . ',' . $Page->listRows)
->select();
if ($list) {
foreach ($list as $k => $vo)
{
$list[$k]['curnum']=$vo['num']-$vo['salenum'];
}
$this->assign('list', $list);
}
if ($count > $p * $pagenum) {
$this->assign('mshow', 1);
} else {
$this->assign('mshow', 0);
}
if (I('is_ajax')) {
return $this->fetch('ajax_mylibao', getMobileStoId());
}
return $this->fetch('', getMobileStoId());
}
//礼包礼券详情
public function lbquan()
{
$id=I('id/d');
$myid=I('mid/d');
$storeid=getMobileStoId();
$data=M('libao_formvip')->where(array('user_id'=>$this->user_id,'id'=>$myid))->select();
if($data[0])
{
$id=$data[0]['lbid'];
$this->assign('mid',$myid);
$this->assign('expdate',$data[0]['expdate']);
}
if($id)
{
$list=M('libao_form')->where(array('store_id'=>$storeid,'id'=>$id,'lbtype'=>2))->select();
mlog("list:".json_encode($list),"lbquan/".$storeid);
if ($list[0]) {
$isstart = 1;
if ($list[0]['startime'] > time()) {
$isstart = 0;
}
if ($list[0]['endtime'] < time()) {
$isstart = 2;
}
$list1 = db::name('libao_list')
->alias('a')
->join('coupon b', 'a.cpid=b.id', 'left')
->where('a.lbid=' . $id)
->field('a.goods_num,b.*')
->order('a.id desc')
->select();
$this->assign('isstart', $isstart);
mlog("isstart:".$isstart,"lbquan/".$storeid);
$this->assign('list', $list[0]);
$this->assign('list1', $list1);
}
}
return $this->fetch('', getMobileStoId());
}
//生成礼包订单
public function libaoorder()
{
$lbid=I('lbid/s');//礼包id
$paytype=I('paytype');//支付方式
$userid=$this->user_id;
if (empty($userid))
{
$res=array(
'code'=>-1,
'msg'=>'请先登录'
);
return json($res);
}
$storeid=getMobileStoId();
//验证是否有此礼包
$libao=M('libao_form')->where('store_id='.$storeid.' and startime<='.time().' and endtime>='.time().' and id='.$lbid)->select();
if(empty($libao[0]))
{
$res=array(
'code'=>-1,
'msg'=>'找不到礼包或礼包已过期'
);
return json($res);
}
if($libao[0]['num'] && $libao[0]['num']<=$libao[0]['salenum'])//礼包已售完
{
$res=array(
'code'=>-1,
'msg'=>'礼包已售罄'
);
return json($res);
}
if($libao[0]['limitnum'])//有限制购买
{
$user_lb=M('libao_formvip')->where(array('store_id'=>$storeid,'user_id'=>$userid,'lbid'=>$lbid,'fbillstate'=>1))->select();
if(count($user_lb)>=$libao[0]['limitnum'])
{
$res=array(
'code'=>-1,
'msg'=>'已达每个会员购买限制'
);
return json($res);
}
}
if($libao[0]['usertype']) //限制新会员购买
{
$orderlist=M('order')->where(array('user_id'=>$userid,'order_status'=>4))->count();
if($orderlist)
{
$res=array(
'code'=>-1,
'msg'=>'该礼包只允许新会员购买'
);
return json($res);
}
}
if($paytype==1)//积分兑换
{
$integ=0;
if($this->pm_erpid) {
$tk = tpCache("shop_info", getMobileStoId());
$userd = M("users")->where('user_id', $userid)->find();
//$map['Id'] = array('=', $userd['erpvipid']);
//$rs = getApiData(' wxd.vip.vipinfo.list.get', $tk['api_token'], null, $map);
//$d = json_decode($rs, true);
if($userd['erpvipid']){
$d=get_erpvipinfo($userd['erpvipid'],$this->pm_erpid);
if($d) $integ = $d['Integral'];
}
}else{
$integ=$this->user['pay_points'];
}
if ($libao[0]['lbintegral'] > $integ) {
$res=array(
'code'=>-1,
'msg'=>'您的积分不足'
);
return json($res);
}
else
{
//启动事务
db::startTrans();
try {
$lbnumber='lb'. date("YmdHis") . rand(1000, 9999);
//扣积分
$sc=new \app\mobile\controller\Integral();
$res=$sc->updateoffintegral($userid,'-'.$libao[0]['lbintegral'],'积分兑换礼包',$lbnumber);
if($res==1)
{
$data['user_id']=$userid;
$data['store_id']=$storeid;
$data['lbid']=$lbid;
$data['expdate']=strtotime('+'.$libao[0]['expday'].' day',time());
$data['number']=$lbnumber;
$data['paytype']=1;
$data['fbillstate']=1;
$data['add_time']=time();
$uselbid=M('libao_formvip')->add($data);
$list=M('libao_list')->where(array('store_id'=>$storeid,'lbid'=>$lbid))->select();
if($list)
{
for ($i=0;$i<count($list);$i++)
{
if ($libao[0]['lbtype']==2)//如果是礼券的话,一个数量生成一条记录
{
for ($k=0;$k<$list[$i]['goods_num'];$k++) {
unset($data);
$data['user_id'] = $userid;
$data['store_id'] = $storeid;
$data['lbvipid'] = $uselbid;
$data['goods_num'] =1;
$data['goods_name'] = $list[$i]['cpid'];
$data['goods_sn'] = $list[$i]['goods_sn'];
$data['lb_state'] = $list[$i]['state'];
M('libao_listvip')->add($data);
}
}
else {
unset($data);
$data['user_id'] = $userid;
$data['store_id'] = $storeid;
$data['lbvipid'] = $uselbid;
$data['goods_num'] = $list[$i]['goods_num'];
$data['goods_name'] = $libao[0]['lbtype'] == 1 ? $list[$i]['goods_name'] : $list[$i]['cpid'];
$data['goods_sn'] = $list[$i]['goods_sn'];
$data['lb_state'] = $list[$i]['state'];
M('libao_listvip')->add($data);
}
}
}
DB::execute("update wxd_libao_form set salenum=salenum+1 where store_id=".$storeid." and id=".$lbid."");
$res=array(
'code'=>1,
'msg'=>'成功'
);
}
else
{
$res=array(
'code'=>-1,
'msg'=>'积分扣减失败'
);
}
Db::commit();
}catch (\Exception $e) {
// 回滚事务
db::rollback();
$res=array(
'code'=>-1,
'msg'=>'订单生成失败,请重试'
);
// M('libao_form')->where('id='.$result)->delete();
}
return json($res);
}
}
else if($paytype==2)//微信购买
{
$res=array(
'code'=>1,
'msg'=>'允许购买礼包'
);
return json($res);
}
}
//会员礼包礼券详情
public function lbshow()
{
$myid=I('mid/d');
$storeid=getMobileStoId();
if($myid)
{
$data=M('libao_formvip')->where(array('user_id'=>$this->user_id,'id'=>$myid))->select();
if($data[0])
{
$id=$data[0]['lbid'];
$this->assign('mid',$myid);
$this->assign('isget',$data[0]['isget']);
}
if($id)
{
$list=M('libao_form')->where(array('store_id'=>$storeid,'id'=>$id,'lbtype'=>2))->select();
$list1=db::name('libao_list')
->alias('a')
->join('coupon b','a.cpid=b.id','left')
->where('a.lbid='.$id)
->field('a.goods_num,b.*')
->order('a.id desc')
->select();
$this->assign('list',$list[0]);
$this->assign('list1',$list1);
}
}
return $this->fetch('', getMobileStoId());
}
//获取礼包礼券
public function getlibaoquan()
{
$lbvipid=I('lvd');
$code=-1;
$msg='找不到礼包或礼包已过期';
$list=M('libao_formvip')->where('id='.$lbvipid)->find();
$isget=$list['isget'];
if($isget){
$res=array('code'=>$code,'msg'=>"您已经领取过了", );
return json($res);
}
if($list)
{
$userid=$list['user_id'];
$list=M('libao_form')->where(array('store_id'=>getMobileStoId(),'id'=>$list['lbid'],'lbtype'=>2))->find();
if($list)
{
$cplist=Db::query('SELECT a.id as lid,a.goods_num,a.no,b.* from wxd_libao_listvip a LEFT JOIN wxd_coupon b on a.goods_name=b.id where a.lbvipid='.$lbvipid);
if($cplist)
{
$tk = tpCache("shop_info",getMobileStoId());
$userd = M("users")->where('user_id', $userid)->find();
$is_success=1;//默认全部成功线下优惠券
for($i=0;$i<count($cplist);$i++)
{
if($cplist[$i]['type']!=6)
{
//如果是erp用户
if($this->pm_erpid) {
for ($k = 0; $k < $cplist[$i]['goods_num']; $k++) {
//优惠券到帐户
if ($cplist[$i]['no'])
{
$CashRepNo=$cplist[$i]['no'];
}
else
{
$CashRepNo = "9006" . date("ymdHis") . get_total_millisecond();
$updatelistvip = M('libao_listvip')->where('id=' . $cplist[$i]["lid"])->save(array('no' => $CashRepNo));
}
unset($data);
$data = array(
'CashRepNo' => $CashRepNo,//券号
'VIPId' => $userd["erpvipid"],//会员id
'Sum' => $cplist[$i]["money"],//金额
'SendMan' => '礼包发放',//发放类型
'Operator' => '微信商城',//来源
'BuySum' => $cplist[$i]["condition"],//满多少才能使用
'Remark' => '购买礼包得优惠券【消费满' . $cplist[$i]["condition"] . '元可用】',
//'BeginDate' => date('Y-m-d H:i:s', $cplist[$i]["use_start_time"]),
//'ValidDate' => date('Y-m-d H:i:s', $cplist[$i]["use_end_time"]),
);
if ($cplist[$i]['useobjecttype'])
{
$data['UseObjectType']=$cplist[$i]['useobjecttype'];
$data['UseObjectID']=$cplist[$i]['useobjectid'];
$data['UseObjectNo']=$cplist[$i]['useobjectno'];
$data['UseObjectName']=$cplist[$i]['useobjectname'];
}
$coupon=$cplist[$i];
//如果有设置有效期
if($coupon['endtype']==1){
$data['BeginDate'] = date('Y-m-d');//使用开始日期
if($coupon['days']>0){
$ed = strtotime("+" . $coupon['days'] . " day");
$data['ValidDate'] = date('Y-m-d', $ed);//截止期限
}
}else {
if($coupon["use_start_time"] != "") {
$data['BeginDate'] = date('Y-m-d', $coupon["use_start_time"]);//使用开始日期
}
if ($coupon["use_end_time"] != "") {
$data['ValidDate'] = date('Y-m-d', $coupon["use_end_time"]);//截止期限
}
}
$erp=tpCache("shop_info.ERPId",getMobileStoId());
$add_rs = getApiData_java_p("/api/erp/vip/cash/insert", $erp, $data,null, null, null, "POST");
if (empty($add_rs))
{
$is_success=0;
}
}
//全部成功的话, 再对应修改1
if (empty($is_success)) {
$res=array(
'code'=>-1,
'msg'=>'领取失败,请重新点击一键领取优惠券!',
);
return json($res);
}
M('libao_formvip')->where('id=' . $lbvipid)->save(array('isget' => 1));
}else{//纯线上功能
for ($k = 0; $k < $cplist[$i]['goods_num']; $k++) {
$coupon=$cplist[$i];
//优惠券到帐户
$CashRepNo = "9006" . date("ymdHis") . get_total_millisecond();
//插入线上优惠券领取表
$coupon_list['cid'] = 0;
$coupon_list['type'] = 2;
$coupon_list['uid'] = $this->user['user_id'];
$coupon_list['send_time'] = time();
$coupon_list['code'] = $CashRepNo;
$coupon_list['store_id'] = getMobileStoId();
if($coupon['endtype']==1){
$coupon_list['begintime']=time();
if($coupon['days']>0) {
$coupon_list['validtime'] = strtotime("+" . $coupon['days'] . " day");
}
else
$coupon_list['validtime']=0;
}else {
if($coupon["use_start_time"] != "")
$coupon_list['begintime'] = $coupon["use_start_time"];
if($coupon["use_end_time"] != "")
$coupon_list['validtime'] = $coupon["use_end_time"];
}
$coupon_list['sum'] = $cplist[$i]["money"];
$coupon_list['buysum'] = $cplist[$i]["condition"];//满多少才能使用
$coupon_list['remark'] = '购买礼包得优惠券【消费满' . $cplist[$i]["condition"] . '元可用】';
$add_couponinfo=add_coupon($coupon_list,$this->user,$this->user['store_id']);
M('libao_listvip')->where('id=' . $cplist[$i]["lid"])->save(array('no' => $CashRepNo));
M('libao_formvip')->where('id=' . $lbvipid)->save(array('isget' => 1));
}
}
}
else{
unset($data);
$coupon=$cplist[$i];
$CashRepNo="9006" . date("ymdHis") . get_total_millisecond();
$data=array(
'store_id'=>getMobileStoId(),
'user_id'=>$userid,
'no'=>$CashRepNo,
'billdate'=>time(),
'fmid'=>$cplist[$i]['id'],
'condition'=>$coupon['condition'],
'exparea'=>$coupon['exparea'],
);
if($coupon['endtype']==1){
$data['use_start_time']=time();
if($coupon['days']==0){
$data['use_end_time']=0;
}else {
$data['use_end_time'] = strtotime("+" . $coupon['days'] . " day");
}
}else {
if($coupon["use_start_time"] != "")
$data['use_start_time'] = $coupon["use_start_time"];
if($coupon["use_end_time"] != "")
$data['use_end_time'] = $coupon["use_end_time"];
}
$result1=M('user_feemail')->add($data);
if($result1)
{
M('libao_listvip')->where('id='.$cplist[$i]["lid"])->save(array('no'=>$CashRepNo));
M('libao_formvip')->where('id='.$lbvipid)->save(array('isget'=>1));
}
}
}
$code=1;
}
}
}
$res=array(
'code'=>$code,
'msg'=>$msg,
);
return json($res);
}
//获取核销码信息
public function getlibaohxm()
{
$id=I('id');
$type=I('type');
$userid=$this->user_id;
if($type==1)
{
$list=M('libao_formvip')->where(' user_id='.$userid.' and id='.$id.' and isget=0 and fbillstate=1')->find();
$no='90072'. date("YmdHis") . rand(1000, 9999); //新核销码(全部商品核销)
}
else{
$list=M('libao_listvip')->where(' user_id='.$userid.' and id='.$id.' and goods_num>usenum')->find();
$no='90071'. date("YmdHis") . rand(1000, 9999); //新核销码(单个商品/服务核销)
}
if($list)
{
if($type==1)
{
$result=M('libao_listvip')->where('lbvipid='.$id)->save(array('freebh'=>$no));
}else{
$result=M('libao_listvip')->where('id='.$id)->save(array('freebh'=>$no));
}
if($result)
{
$res=array(
'code'=>1,
'msg'=>$no,
);
}
else{
$res=array(
'code'=>-1,
'msg'=>'请重试',
);
}
}
else{
$res=array(
'code'=>-1,
'msg'=>'当前商品已核销完'
);
}
return json($res);
}
}