审查视图

app/admin/controller/ZjOrderController.php 14.7 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 60 61
        $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,sum(whole_num) as whole_num')
            ->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);
77 78
        $this->assign('start_time', !empty($arr['start_time']) ? $arr['start_time'] : '');
        $this->assign('end_time', !empty($arr['end_time']) ? $arr['end_time'] : '');
lihan authored
79 80 81 82 83 84 85 86 87 88 89
        $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);
90 91 92 93 94 95
        return $this->fetch();
    }

    /**
     * 订单详情
     */
lihan authored
96 97 98 99 100 101 102 103 104
    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');
105
            }
lihan authored
106 107 108 109
            $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)
110
                ->field('g.name,og.num')->select();
lihan authored
111 112 113 114
            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);
115
            }
lihan authored
116 117 118 119
            $this->assign('kd', $kd);
            $this->assign('url', $url);
            $this->assign('one', $one);
            $this->assign('all', $all);
120 121 122
            return $this->fetch();
        }
    }
lihan authored
123
124 125 126
    /**
     * 订单详情修改提交
     */
lihan authored
127 128 129 130 131 132 133 134 135
    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 {
136
                    //引入快递订阅接口
lihan authored
137 138 139 140
                    $kd = new KdnApiController();
                    $kds = $kd->orderTracesSubByJson($param['id']);
                    $kds = json_decode($kds, true);
                    if ($kds['Success'] == true) {
141
                        $kdss = '快递状态订阅成功';
lihan authored
142
                    } else {
143
                        $kdss = '快递状态订阅失败';
144 145 146
                    }
                }
            }
lihan authored
147
            if (empty($edit)) {
148
                $this->error('修改失败,' . $kdss);
lihan authored
149
            } else {
150 151 152 153 154
                if ($kds['Success'] == true){
                    $this->success('修改成功,' . $kdss);
                }else{
                    $this->error('修改成功',$kdss);
                }
155 156 157 158 159 160 161 162
            }
        }
    }


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

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

}