root = $this->request->root(true); $this->title = '订单记录管理'; $array = $this->statistical_order_info(); $this->assign('a', $array); $this->byWhere(1)->field('a.*,b.name,b.headimg')->order('a.id desc')->page(); } protected function statistical_order_info() { $array = array(); $array['all_order'] = $this->byWhere(2)->count(); $array['all_price'] = $this->byWhere(2)->where('a.status', '>',0)->sum('a.price_total'); $array['all_refund_price'] = $this->byWhere(2)->where('a.refund_price', '>',0)->sum('a.refund_price'); $array['all_price'] = $array['all_price'] - $array['all_refund_price']; $array['weixin_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '1')->sum('a.price_total'); $array['weixin_refund_all_price'] = $this->byWhere(2)->where('a.refund_price', '>',0)->where('a.pay_type', '1')->sum('a.refund_price'); $array['weixin_all_price'] = $array['weixin_all_price'] - $array['weixin_refund_all_price']; $array['zfb_all_price'] = $this->byWhere(2)->where('a.status', '>',0)->where('a.pay_type', '2')->sum('a.price_total'); $array['zfb_refund_all_price'] = $this->byWhere(2)->where('a.refund_price', '>',0)->where('a.pay_type', '2')->sum('a.refund_price'); $array['zfb_all_price'] = $array['zfb_all_price'] - $array['zfb_refund_all_price']; $array['engineer_id'] = session('user.engineer_id'); return $array; } /** * 搜索条件 * @return \library\helper\QueryHelper */ protected function byWhere($type) { if ($type == 1) { $query = $this->_query($this->table); } elseif ($type == 2) { $query = Db::name($this->table); } $query = $query->alias('a')->join('store_member b', 'a.user_id=b.id'); /*$engineer_id = session('user.engineer_id'); if($engineer_id > 0){ $query->where('a.worker_id = '.$engineer_id.' or a.status = 1'); }*/ $query->where('a.is_deleted', 0); if (isset($_GET['order_no']) && $_GET['order_no']) { $query->where('a.order_no', 'like','%'.$_GET['order_no'].'%'); } if (isset($_GET['status']) && $_GET['status'] != '') { $query->where('a.status', $_GET['status']); } if (isset($_GET['is_self_build']) && $_GET['is_self_build'] != '') { $query->where('a.is_self_build', $_GET['is_self_build']); } if (isset($_GET['pay_status']) && $_GET['pay_status'] != '') { $query->where('a.pay_status', $_GET['pay_status']); } if (isset($_GET['pay_type']) && $_GET['pay_type']) { $query->where('a.pay_type', $_GET['pay_type']); } if (isset($_GET['create_at']) && $_GET['create_at']) { $time = explode(' - ', $_GET['create_at']); $start_date_time = $time[0] . ' 00:00:00'; $end_date_time = $time[1] . ' 23:59:59'; $query->whereBetweenTime('a.create_at', $start_date_time, $end_date_time); } if (isset($_GET['pay_at']) && $_GET['pay_at']) { $time = explode(' - ', $_GET['pay_at']); $start_date_time = $time[0] . ' 00:00:00'; $end_date_time = $time[1] . ' 23:59:59'; $query->whereBetweenTime('a.pay_at', $start_date_time, $end_date_time); } if (isset($_GET['user_info']) && $_GET['user_info']) { $query->where('b.name|b.phone', '=', $_GET['user_info'] ); } if (isset($_GET['worker_info']) && $_GET['worker_info']) { $worker_id = Db::name('store_engineer')->where('name|phone', '=', $_GET['worker_info'] )->value('id'); $query->where('worker_id',$worker_id); } return $query; } /** * 订单列表处理 * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ protected function _index_page_filter(array &$data) { $mids = array_unique(array_merge(array_column($data, 'user_id'), array_column($data, 'user_id'))); $memberList = Db::name('StoreMember')->whereIn('id', $mids)->select(); $wids = array_unique(array_merge(array_column($data, 'worker_id'), array_column($data, 'worker_id'))); $workerList = Db::name('store_engineer')->whereIn('id', $wids)->select(); foreach ($data as &$vo) { list($vo['member'], $vo['worker']) = [[],[]]; foreach ($memberList as $member) if ($member['id'] === $vo['user_id']) { $vo['member'] = $member; } foreach ($workerList as $worker) if ($worker['id'] === $vo['worker_id']) { $vo['worker'] = $worker; } $vo['serve_title'] = Db::name('store_goods')->where('id',$vo['goods_id'])->value('title'); } } /** * 添加订单 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function add() { $this->title = '添加订单'; //服务类目 $this->cate_arr = Db::name('store_goods')->field('id,title')->where('status',1)->where('is_deleted',0)->order(['sort'=>'desc','id'=>'desc'])->select(); //服务类型 $serve_type_arr = array('1'=>' 技术咨询','2'=>'预约检测','3'=>'加急检测','4'=>'夜间检测'); $this->serve_type_arr = $serve_type_arr; $this->member_list = Db::name('store_member')->field('id,name,phone')->where('status',1)->where('is_deleted',0)->select(); $this->_form($this->table, 'form'); } /** * 表单数据处理 * @param array $data * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ protected function _form_filter(array &$data) { if ($this->request->isGet()) { //接单人员信息 if(isset($data['worker_id']) && $data['worker_id']){ $data['worker_info'] = Db::name('store_engineer')->field('name,phone')->where('id',$data['worker_id'])->find(); } $this->data = $data; }else{ $serve_type_arr = array('1'=>' 技术咨询','2'=>'预约检测','3'=>'加急检测','4'=>'夜间检测'); $data['worker_id'] = session('user.engineer_id'); $data['price_amount'] = $data['price_total']; $data['is_self_build'] = 1; $data['serve_type'] = $serve_type_arr[$data['serve_type_id']]; $data['order_no'] = get_order_sn(); $data['pay_no'] = get_order_sn(); } } /** * 表单结果处理 * @param boolean $result */ protected function _form_result($result) { if ($result && $this->request->isPost()) { $this->success('操作成功!', 'javascript:history.back()'); } } /** * 订单详情 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function order_detail() { $this->_form($this->table); } /** * 订单记录 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function order_status() { $id = $this->app->request->get('id'); $this->assign('id', $id); $post = $this->app->request->post(); if (isset($post['id']) && $post['id']) { Db::name($this->table)->where('id', $post['id'])->update(['remark' => $post['remark']]); $this->success('编辑成功!'); } else { $this->_form($this->table); } } /** * 接单 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function receive() { $this->_save($this->table, ['worker_id' => session('user.engineer_id'),'status'=>2,'start_at'=>date('Y-m-d H:i:s')]); } /** * 指派订单 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function send_order() { $id = $this->app->request->get('id'); $this->assign('id', $id); $post = $this->app->request->post(); if (isset($post['id']) && $post['id']) { $worker_id = $post['worker_id']; $order_info = Db::name('store_order')->where('id',$id)->find(); $order_data = array( 'status' => $post['status'], 'price_total' => $post['price_total'], 'price_amount' => $post['price_total'] + $order_info['coupon_amount'], 'remark' => $post['remark'] ); if($post['status'] == 1){ //未分配 $order_data['worker_id'] = 0; $order_data['start_at'] = null; }else{ $order_data['worker_id'] = $worker_id; } Db::name('store_order')->where('id',$id)->update($order_data); if($worker_id != $order_info['worker_id']){ //添加指派记录 $order_no = Db::name('store_order')->where('id',$id)->value('order_no'); $send_log_data = array( 'order_id' => $id, 'order_no' => $order_no, 'worker_id' => 0, 'send_worker_id' => $worker_id ); Db::name('store_send_log')->insert($send_log_data); //发送邮箱 $engineer_mail = Db::name('store_engineer')->where('mail is not null')->where('id',$worker_id)->where('is_deleted',0)->value('mail'); if($engineer_mail){ mail_push($engineer_mail,$id); } } $this->success('操作成功!'); } else { /*$engineer_id = session('user.engineer_id');*/ $store_worker = Db::name('store_engineer')->field('id,name,phone')->where('is_deleted',0)->select(); if($store_worker){ $this->store_worker = $store_worker; $this->_form($this->table, 'send_order'); }else{ $this->error('暂没有合适的工程师'); } } } /** * 修改金额 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function edit_order() { $id = $this->app->request->get('id'); $this->assign('id', $id); $post = $this->app->request->post(); if (isset($post['id']) && $post['id']) { $order_data = array( 'price_total' => $post['price_total'], 'price_amount' => $post['price_total'] ); // 用户支付成功处理 if($post['pay_status'] == 1){ $order_data['pay_at'] = date('Y-m-d H:i:s'); $order_data['pay_status'] = 1; $order_data['status'] = 1; } Db::name('store_order')->where('id',$id)->update($order_data); $this->success('操作成功!'); } else { $this->_form($this->table, 'edit_order'); } } /** * 立即退款 * @auth true * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function order_refund() { $id = $this->app->request->get('id'); $this->assign('id', $id); $post = $this->app->request->post(); if (isset($post['id']) && $post['id']) { $order_info = Db::name($this->table)->where('id', $post['id'])->find(); if($post['amount'] < 0 || $post['amount'] > $order_info['price_total']){ $this->error('退款金额错误'); } $refund_success = 0; if($order_info['pay_type'] == 1){ //微信退款 $app = Factory::payment(config('app.wx_pay')); $result = $app->refund->byOutTradeNumber($order_info['pay_no'], get_order_sn(),$order_info['price_total']*100, $post['amount']*100, [ // 可在此处传入其他参数,详细参数见微信支付文档 'refund_desc' => '', ]); if($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS'){ $refund_success = 1; } }elseif($order_info['pay_type'] == 2){ $result = Alipay::ali_refund($order_info['pay_no'],$post['amount']); if($result){ $refund_success = 1; } } if($refund_success == 1){ //更改退款状态 Db::name('store_order')->where('id',$post['id'])->update(array('refund_at'=>date('Y-m-d H:i:s'),'status'=>4,'refund_price'=>$post['amount'])); $this->success('退款成功'); }else{ $this->error('退款失败'); } } else { $this->_form($this->table, 'order_rufund'); } } /** * 删除订单 * @auth true * @throws \think\Exception * @throws \think\exception\PDOException */ public function remove() { $this->_save($this->table, ['is_deleted' => '1']); } }