123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace app\admin\controller;
- use think\App;
- use think\Db;
- use think\Exception;
- class DinnerRefuseOrder extends Auth
- {
- public function __construct(App $app = null) {
- parent::__construct($app);
- $this->table='dinner_order_refuse';
- }
- public function index(){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','id','trim'); //排序列
- $sort = input('sord','desc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $title = input('sn','','trim');
- if($title){
- $map[] = ['b.sn','like','%'.$title.'%'];
- }
- $map[] = ['a.org_id','=',$this->orgId];
- $map[] = ['b.state','<>',0];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = db($this->table)
- ->alias('a')
- ->join('dinner_order b','a.dinner_order_id=b.id')
- ->field('b.id,b.sn,b.price,b.name,b.mobile,b.address,b.remark,b.pay_time,a.create_time,a.op_time,a.status,a.id as refuse_id')
- ->where($map)->limit($start,$length)->order(
- ['a.id'=>'desc']
- )->select();
- foreach ($lists as $k=>$v){
- $all = Db::name('dinner_order_item')
- ->alias('a')
- ->join('dinner b','a.dinner_id=b.id')
- ->join('dinner_type c','a.dinner_type_id=c.id')
- ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name')
- ->where('a.order_id',$v['id'])
- ->select();
- $item = '';
- foreach ($all as $k2=>$v2){
- $ss = $k2+1;
- if($ss<10){
- $ss= '0'.$ss;
- }
- $ur = url('dinner/detail').'?id='.$v2['dinner_id'];
- $item.=''.$ss.',<span class="label" style="border-radius:0rem !important;background-color: '.$v2['color'].';color: white">'.$v2['dinner_type_name'].'</span><a data-title="详情" href="javascript:;" url="'.$ur.'">'.$v2['dinner_name'].'*'.$v2['num'];
- if(count($all)>1 && $k2<count($all)){
- $item.='<br/>';
- }
- }
- $lists[$k]['item'] = $item;
- $refuse = Db::name('dinner_order_refuse_item')
- ->alias('a')
- ->join('dinner b','a.dinner_id=b.id')
- ->join('dinner_type c','a.dinner_type_id=c.id')
- ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name')
- ->where('a.order_refuse_id',$v['refuse_id'])
- ->select();
- $item_refuse = '';
- foreach ($refuse as $k3=>$v3){
- $ss1 = $k3+1;
- if($ss1<10){
- $ss1= '0'.$ss1;
- }
- $ur = url('dinner/detail').'?id='.$v3['dinner_id'];
- $item_refuse.=''.$ss1.',<span class="label" style="border-radius:0rem !important;background-color: '.$v3['color'].';color: white">'.$v3['dinner_type_name'].'</span><a onclick="layer_open(this,0)" data-title="详情" href="javascript:;" url="'.$ur.'">'.$v3['dinner_name'].'*'.$v3['num'];
- if(count($refuse)>1 && $k3<count($refuse)){
- $item_refuse.='<br/>';
- }
- }
- $lists[$k]['item_refuse'] = $item_refuse;
- $status_txt = '';
- if($v['status']==4){
- $status_txt ='<span class="label" style="border-radius:0rem !important;background-color: #d15b47;color: white">正在退款中</span><br/>'.$v['sn'].'';
- }elseif ($v['status']==5){
- $status_txt ='<span class="label" style="border-radius:0rem !important;background-color: #82af6f;color: white">已退款</span><br/>'.$v['sn'].'';
- }elseif ($v['status']==6){
- $status_txt ='<span class="label" style="border-radius:0rem !important;background-color: #3a87ad;color: white">申请退款被拒</span><br/>'.$v['sn'].'';
- }
- $lists[$k]['status_txt'] = $status_txt;
- $lists[$k]['op_time'] = $v['op_time']?$v['op_time']:'';
- }
- //数据返回
- $totalCount = db($this->table)
- ->alias('a')
- ->join('dinner_order b','a.dinner_order_id=b.id')
- ->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- return $this->fetch();
- }
- }
- public function option($id=0){
- if(request()->isPost()){
- $res = Db::name($this->table)
- ->where('org_id',$this->orgId)
- ->where('id',$id)
- ->find();
- if($res['status']!==4){
- $this->error('当前订单已操作');
- }
- $order = Db::name('dinner_order')->where('id',$res['dinner_order_id'])->find();
- if(!$order){
- $this->error('订单不存在');
- }
- $status = input('status','');
- $remark = input('refuse_remark','');
- if(!in_array($status,[5,6])){
- $this->error('参数错误');
- }
- Db::startTrans();
- try{
- if($order['cate'] == 0){
- $r = Db::name('dinner_order')
- ->where('id',$res['dinner_order_id'])
- ->update([
- 'state'=>$status,
- 'update_time'=>getTime()
- ]);
- if(!$r){
- \exception('操作失败');
- }
- }else{
- if($status==5){
- $checkAll = Db::name('dinner_order_item')
- ->where('order_id',$res['dinner_order_id'])
- ->where('is_refuse',0)
- ->find();
- if(!$checkAll){//已退款
- Db::name('dinner_order')
- ->where('id',$res['dinner_order_id'])
- ->update([
- 'state'=>5,
- 'update_time'=>getTime()
- ]);
- }
- }
- }
- $r = Db::name($this->table)
- ->where('id',$id)
- ->update([
- 'status'=>$status,
- 'op_user_id'=>$this->userId,
- 'op_time'=>getTime(),
- 'refuse_remark'=>$remark
- ]);
- if(!$r){
- \exception('操作失败');
- }
- if($status==5&&$res['order_type']==1&&$res['is_replace']==0) {
- $checkAll = Db::name('dinner_order_item')
- ->where('order_id',$res['dinner_order_id'])
- ->where('is_refuse',0)
- ->find();
- if($checkAll){//部分退款
- Db::name('dinner_order')
- ->where('id',$res['dinner_order_id'])
- ->update([
- 'state'=>7,
- 'update_time'=>getTime()
- ]);
- }
- if($res['refuse_price'] > 0){
- $orderInfo = Db::name('dinner_order')
- ->where('id',$res['dinner_order_id'])
- ->find();
- $config =get_pay_wechat($orderInfo['org_id']);
- if(empty($config['mch_id']) || empty($config['key'])){
- \exception('微信商户信息未配置,请联系管理员');
- }
- $app = \EasyWeChat\Factory::payment($config);
- $totalFee = intval(round($orderInfo['price']*100));
- $refundFee = intval(round($res['refuse_price']*100));
- $ret = $app->refund->byOutTradeNumber($orderInfo['sn'], 'T'.$orderInfo['sn'], $totalFee, $refundFee);
- if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
- trace(json_encode($ret),'error');
- \exception('退款申请提交失败');
- }
- }
- }
- if($status==6){ // 拒绝,返还
- $all = Db::name('dinner_order_refuse_item')->where('order_refuse_id',$id)->select();
- if($order['cate'] == 0){
- foreach ($all as $k=>$v){
- Db::name('dinner_order_item')
- ->where('dinner_id',$v['dinner_id'])
- ->where('dinner_type_id',$v['dinner_type_id'])
- ->where('is_refuse',1)
- ->setField('is_refuse',0);
- }
- }else{
- foreach ($all as $k=>$v){
- Db::name('dinner_order_item')
- ->where('dinner_id',$v['dinner_id'])
- ->where('dinner_type_id',$v['dinner_type_id'])
- ->where('order_id',$res['dinner_order_id'])
- ->where('day',$v['day'])
- ->where('is_refuse',1)
- ->update(['is_refuse'=>0]);
- }
- }
- }
- Db::commit();
- $this->success('操作成功');
- }catch (Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
- }else{
- $this->assign('id',$id);
- return $this->fetch();
- }
- }
- }
|