审查视图

app/admin/controller/ZjOrderController.php 15.0 KB
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * Created by PhpStorm.
 * User: wz
 * Date: 2018/9/25
 * Time: 11:29
 */

namespace app\admin\controller;

12
use app\kdnapi\controller\KdnApiController;
13 14 15 16 17 18 19 20
use cmf\controller\AdminBaseController;
use think\Db;

class ZjOrderController extends AdminBaseController
{
    /**
     * 订单列表
     */
lihan authored
21 22 23 24 25 26 27 28 29
    public function index()
    {
        $where['o.delete_time'] = 0;
        $arr = [];
        $where1 = [];
        if ($this->request->param()) {
            $arr = input('param.');
            if (!empty($arr['uid'])) {
                $where['o.uid'] = $arr['uid'];
30
            }
lihan authored
31 32
            if (!empty($arr['yi'])) {
                $where['o.cid'] = $arr['yi'];
33
            }
lihan authored
34 35
            if (!empty($arr['er'])) {
                $where['o.cids'] = ['like', '%-' . $arr['er'] . '-%'];
36
            }
lihan authored
37 38
            if (!empty($arr['step'])) {
                $where['o.step'] = $arr['step'];
39
            }
lihan authored
40 41
            if (!empty($arr['num'])) {
                $where['o.order_num'] = $arr['num'];
42
            }
lihan authored
43 44
            if (!empty($arr['name'])) {
                $where['o.name'] = $arr['name'];
45
            }
lihan authored
46 47
            if (!empty($arr['mobile'])) {
                $where['o.mobile'] = $arr['mobile'];
48
            }
lihan authored
49 50
            if (!empty($arr['start_time'])) {
                $where['o.create_time'] = ['egt', strtotime($arr['start_time'])];
51
            }
lihan authored
52 53
            if (!empty($arr['end_time'])) {
                $where1['o.create_time'] = ['elt', strtotime($arr['end_time'])];
54 55
            }
        }
lihan authored
56 57 58 59
        $all = Db::name('zj_order')->alias('o')
            ->join('user u', 'o.uid=u.id', 'left')
            ->where($where)->where($where1)
            ->order('create_time', 'desc')
lihan authored
60
            ->field('o.*,u.user_nickname')
lihan authored
61
            ->paginate(15);
62
        $all->appends($arr);
63 64

        //查询商品分类
lihan authored
65 66 67 68 69 70 71 72
        $cate = Db::name('zj_category')->where(['delete_time' => '0'])->field('name,id,cid,grade')->select()->toArray();
        $cate1 = [];
        $cate2 = [];
        foreach ($cate as $k => $v) {
            if ($cate[$k]['grade'] == 1) {
                $cate1[$k] = $cate[$k];
            } else {
                $cate2[$k] = $cate[$k];
73 74
            }
        }
lihan authored
75 76
        $cate1 = array_values($cate1);
        $cate2 = array_values($cate2);
lihan authored
77 78 79 80 81 82 83 84
        //计算总金额和总积分
        $whole = 0;
        $whole_num = 0;
        foreach ($all as $item) {
            $whole+=$item['whole'];
            $whole_num+=$item['whole_num'];
        }
        $total = $whole - $whole_num;
85 86
        $this->assign('start_time', !empty($arr['start_time']) ? $arr['start_time'] : '');
        $this->assign('end_time', !empty($arr['end_time']) ? $arr['end_time'] : '');
lihan authored
87 88 89 90 91 92 93 94 95 96 97
        $this->assign('uid', !empty($arr['uid']) ? $arr['uid'] : '');
        $this->assign('step', !empty($arr['step']) ? $arr['step'] : '');
        $this->assign('num', !empty($arr['num']) ? $arr['num'] : '');
        $this->assign('name', !empty($arr['name']) ? $arr['name'] : '');
        $this->assign('mobile', !empty($arr['mobile']) ? $arr['mobile'] : '');
        $this->assign('yi', !empty($arr['yi']) ? $arr['yi'] : '');
        $this->assign('er', !empty($arr['er']) ? $arr['er'] : '');
        $this->assign('page', $all->render());
        $this->assign('all', $all->items());
        $this->assign('cate1', $cate1);
        $this->assign('cate2', $cate2);
lihan authored
98 99
        $this->assign('total', $total);
        $this->assign('whole_num', $whole_num);
100 101 102 103 104 105
        return $this->fetch();
    }

    /**
     * 订单详情
     */
lihan authored
106 107 108 109 110 111 112 113 114
    public function detail()
    {
        if ($this->request->param()) {
            $id = input('param.id');
            $url = input('param.sta');
            if ($url == 1) {
                $url = url('index');
            } elseif ($url == 2) {
                $url = url('backlog');
115
            }
lihan authored
116 117 118 119
            $one = Db::name('zj_order')->alias('o')->join('user u', 'o.uid=u.id', 'left')->join('zj_kd k', 'o.kid=k.id', 'left')
                ->where('o.id', $id)->field('o.*,u.user_nickname,k.name as kname')->find();
            $kd = Db::name('zj_kd')->select();
            $all = Db::name('zj_order_goods')->alias('og')->join('zj_goods g', 'og.gid=g.id')->where('oid', $id)
120
                ->field('g.name,og.num')->select();
lihan authored
121 122 123 124
            if ($one['step'] > 2 && $one['kid'] != 0) {
                $kdgj = Db::name('zj_kd_order')->where(['kd_num' => $one['kd_num']])->value('text');
                $kdgj = json_decode($kdgj, true);
                $this->assign('kdgj', $kdgj);
125
            }
lihan authored
126 127 128 129
            $this->assign('kd', $kd);
            $this->assign('url', $url);
            $this->assign('one', $one);
            $this->assign('all', $all);
130 131 132
            return $this->fetch();
        }
    }
lihan authored
133
134 135 136
    /**
     * 订单详情修改提交
     */
lihan authored
137 138 139 140 141 142 143 144 145
    public function detailPost()
    {
        if ($this->request->param()) {
            $param = input('param.');
            $edit = Db::name('zj_order')->update($param);
            if ($param['step'] == '3') {
                if ($param['kid'] == '0') {
                    $kds = '自提订单';
                } else {
146
                    //引入快递订阅接口
lihan authored
147 148 149 150
                    $kd = new KdnApiController();
                    $kds = $kd->orderTracesSubByJson($param['id']);
                    $kds = json_decode($kds, true);
                    if ($kds['Success'] == true) {
151
                        $kdss = '快递状态订阅成功';
lihan authored
152
                    } else {
153
                        $kdss = '快递状态订阅失败';
154 155 156
                    }
                }
            }
lihan authored
157
            if (empty($edit)) {
158
                $this->error('修改失败,' . $kdss);
lihan authored
159
            } else {
160 161 162 163 164
                if ($kds['Success'] == true){
                    $this->success('修改成功,' . $kdss);
                }else{
                    $this->error('修改成功',$kdss);
                }
165 166 167 168 169 170 171 172
            }
        }
    }


    /**
     * 待处理订单列表
     */
lihan authored
173 174 175 176 177 178 179 180 181
    public function backlog()
    {
        $where['o.delete_time'] = 0;
        $arr = [];
        $where1['o.step'] = ['in', ['2', '6', '8']];
        if ($this->request->param()) {
            $arr = input('param.');
            if (!empty($arr['uid'])) {
                $where['o.uid'] = $arr['uid'];
182
            }
lihan authored
183 184
            if (!empty($arr['step'])) {
                $where['o.step'] = $arr['step'];
185
            }
lihan authored
186 187
            if (!empty($arr['num'])) {
                $where['o.order_num'] = $arr['num'];
188
            }
lihan authored
189 190
            if (!empty($arr['name'])) {
                $where['o.name'] = $arr['name'];
191
            }
lihan authored
192 193
            if (!empty($arr['mobile'])) {
                $where['o.mobile'] = $arr['mobile'];
194
            }
lihan authored
195 196
            if (!empty($arr['start_time'])) {
                $where['o.create_time'] = ['egt', strtotime($arr['start_time'])];
197
            }
lihan authored
198 199
            if (!empty($arr['end_time'])) {
                $where1['o.create_time'] = ['elt', strtotime($arr['end_time'])];
200 201
            }
        }
lihan authored
202 203
        $all = Db::name('zj_order')->alias('o')->join('user u', 'o.uid=u.id', 'left')->where($where)->where($where1)
            ->order('create_time', 'desc')->field('o.*,u.user_nickname')->paginate(15);
204 205 206
        $all->appends($arr);
        $this->assign('start_time', !empty($arr['start_time']) ? $arr['start_time'] : '');
        $this->assign('end_time', !empty($arr['end_time']) ? $arr['end_time'] : '');
lihan authored
207 208 209 210 211 212 213
        $this->assign('uid', !empty($arr['uid']) ? $arr['uid'] : '');
        $this->assign('step', !empty($arr['step']) ? $arr['step'] : '');
        $this->assign('num', !empty($arr['num']) ? $arr['num'] : '');
        $this->assign('name', !empty($arr['name']) ? $arr['name'] : '');
        $this->assign('mobile', !empty($arr['mobile']) ? $arr['mobile'] : '');
        $this->assign('page', $all->render());
        $this->assign('all', $all->items());
214 215
        return $this->fetch();
    }
lihan authored
216
217 218 219
    /**
     * 订单发货
     */
lihan authored
220 221 222 223
    public function fahuo()
    {
        if ($this->request->param()) {
            $param = input('param.id');
224
            //获取订单数据
lihan authored
225 226
            $one = Db::name('zj_order')->alias('o')->join('user u', 'o.uid=u.id', 'left')
                ->where('o.id', $param)->field('o.*,u.user_nickname')->find();
227
            //获取快递公司数据
lihan authored
228
            $kd = Db::name('zj_kd')->select();
229
            //获取订单商品
lihan authored
230
            $all = Db::name('zj_order_goods')->alias('og')->join('zj_goods g', 'og.gid=g.id')->where('oid', $param)
231
                ->field('g.name,og.num')->select();
lihan authored
232
            if ($one['step'] > 2) {
233
                echo "<div style='width: 100%;text-align: center;padding-top: 50px'><p style='font-size: 25px'>订单已发货</p></div>";
lihan authored
234
            } elseif ($one['step'] == 1) {
235
                echo "<div style='width: 100%;text-align: center;padding-top: 50px'><p style='font-size: 25px'>订单未支付</p></div>";
lihan authored
236 237 238 239
            } else {
                $this->assign('kd', $kd);
                $this->assign('one', $one);
                $this->assign('all', $all);
240 241 242 243
                return $this->fetch();
            }
        }
    }
lihan authored
244
245 246 247
    /**
     * 订单发货提交
     */
lihan authored
248 249 250 251 252
    public function fhPost()
    {
        if ($this->request->param()) {
            $param = input('param.');
            if (empty($param['kd_num'])) {
253 254
                $this->error('快递单号不能为空');
            }
255
lihan authored
256 257
            $param['step'] = 3;
            $edit = Db::name('zj_order')->update($param);
258
            //引入快递订阅接口
lihan authored
259 260 261 262 263 264 265
            $kd = new KdnApiController();
            $kds = $kd->orderTracesSubByJson($param['id']);
            $kds = json_decode($kds, true);
            if ($kds['Success'] == true) {
                $kds = '快递状态订阅成功';
            } else {
                $kds = '快递状态订阅失败';
266
            }
lihan authored
267 268 269 270
            if (empty($edit)) {
                $this->error('订单状态更新失败,' . $kds);
            } else {
                $this->success('订单状态更新成功,' . $kds);
271 272 273
            }
        }
    }
lihan authored
274
275 276 277
    /**
     * 订单退款审核
     */
lihan authored
278 279 280 281
    public function check()
    {
        if ($this->request->param()) {
            $param = input('param.id');
282
            //获取订单数据
lihan authored
283
            $one = Db::name('zj_order')->alias('o')->join('user u', 'o.uid=u.id', 'left')->where('o.id', $param)
284 285
                ->field('o.*,u.user_nickname')->find();
            //获取订单商品
lihan authored
286
            $all = Db::name('zj_order_goods')->alias('og')->join('zj_goods g', 'og.gid=g.id')->where('oid', $param)
287
                ->field('g.name,g.price,og.num')->select();
lihan authored
288
            if ($one['step'] < 6) {
289
                echo "<div style='width: 100%;text-align: center;padding-top: 50px'><p style='font-size: 25px'>订单未退货</p></div>";
lihan authored
290
            } elseif ($one['step'] > 6) {
291
                echo "<div style='width: 100%;text-align: center;padding-top: 50px'><p style='font-size: 25px'>退款审核通过</p></div>";
lihan authored
292 293 294
            } else {
                $this->assign('one', $one);
                $this->assign('all', $all);
295 296 297 298 299 300 301 302
                return $this->fetch();
            }
        }
    }

    /**
     * 订单退款审核提交
     */
lihan authored
303 304 305 306
    public function checkPost()
    {
        if ($this->request->param()) {
            $param = input('param.');
307
            //判断审核是否通过
lihan authored
308 309
            if (!empty($param['sta'])) {
                $param['step'] = 5;
310
                unset($param['sta']);
lihan authored
311 312 313 314
                $edit = Db::name('zj_order')->update($param);
            } else {
                $param['step'] = 7;
                $edit = Db::name('zj_order')->update($param);
315
            }
lihan authored
316
            if (empty($edit)) {
317
                $this->error('订单状态更新失败');
lihan authored
318
            } else {
319 320 321 322
                $this->success('订单状态更新成功');
            }
        }
    }
lihan authored
323
324 325 326
    /**
     * 订单退款
     */
lihan authored
327 328 329 330
    public function refund()
    {
        if ($this->request->param()) {
            $param = input('param.id');
331
            //获取订单数据
lihan authored
332
            $one = Db::name('zj_order')->alias('o')->join('user u', 'o.uid=u.id', 'left')->where('o.id', $param)
333 334
                ->field('o.*,u.user_nickname')->find();
            //获取订单商品
lihan authored
335
            $all = Db::name('zj_order_goods')->alias('og')->join('zj_goods g', 'og.gid=g.id')->where('oid', $param)
336
                ->field('g.name,g.price,og.num')->select();
lihan authored
337
            if ($one['step'] > 8) {
338
                echo "<div style='width: 100%;text-align: center;padding-top: 50px'><p style='font-size: 25px'>订单已退款</p></div>";
lihan authored
339
            } elseif ($one['step'] < 8) {
340
                echo "<div style='width: 100%;text-align: center;padding-top: 50px'><p style='font-size: 25px'>订单状态错误</p></div>";
lihan authored
341 342 343
            } else {
                $this->assign('one', $one);
                $this->assign('all', $all);
344 345 346 347
                return $this->fetch();
            }
        }
    }
lihan authored
348
349 350 351
    /**
     * 订单退款提交
     */
lihan authored
352 353 354 355 356
    public function refundPost()
    {
        if ($this->request->param()) {
            $param = input('param.');
            if (Db::name('zj_order')->where(['id' => $param['id']])->value('step') == 8) {
lihan authored
357
                if ($param['refund_fee'] <= 0) {
lihan authored
358 359
                    $this->error('退款金额需大于0');
                }
lihan authored
360
                $return = $this->wxRefund($param['id'], $param['refund_fee']);
lihan authored
361 362 363 364 365 366 367 368 369 370 371 372 373
                if ($return !== false) {
                    $out_refund_no = Db::name('zj_order')->where(['id'=>$param['id']])->value('out_refund_no');
                    if($out_refund_no == '' && $out_refund_no != $return) {
                        $param['step'] = 9;
                        $param['out_refund_no'] = $return;
                        $edit = Db::name('zj_order')->update($param);
                        if (empty($edit)) {
                            $this->error('订单退款失败1');
                        } else {
                            $this->success('订单退款成功');
                        }
                    }else {
                        $this->error('禁止重复退款');
lihan authored
374
                    }
lihan authored
375
                } else {
lihan authored
376
                    $this->error('订单退款失败2');
lihan authored
377 378 379
                }
            } else {
                $this->error('非法操作');
380
            }
lihan authored
381 382
        }
    }
383
lihan authored
384 385 386 387 388
    private function wxRefund($oid, $refundFee)
    {
        $info = Db::name('zj_order')->field('order_num,whole,whole_num,out_trade_no,step')->where(['id' => $oid])->find();
        if ($info['step'] == 8) {
            $totalFee = $info['whole'] - $info['whole_num'];
lihan authored
389
            if ($refundFee > $totalFee) {
lihan authored
390
                $this->error('退款金额不能大于实际支付金额');
lihan authored
391
            } else {
lihan authored
392
                require_once EXTEND_PATH . '/Refund.php';
lihan authored
393
                $refund = new \Refund($info['out_trade_no'], $totalFee * 100, $info['order_num'], $refundFee * 100);
lihan authored
394 395
                $return = $refund->refund();
                if ($return['return_code'] == 'SUCCESS' && $return['result_code'] == 'SUCCESS') {
lihan authored
396
                    return $return['out_refund_no'];
lihan authored
397 398 399
                } else {
                    return false;
                }
400
            }
lihan authored
401 402
        } else {
            $this->error('非法操作');
403 404 405 406
        }
    }

}