123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use EasyWeChat\Factory;
- use think\Exception;
- class WxOrders extends Base
- {
- public $status = [
- '未支付',
- '已支付',
- '已发货',
- '已完成',
- '已取消',
- '已评价',
- ];
- public $rStatus = [
- '退款处理中',
- '退款成功',
- '退款拒绝',
- ];
- public function add($cartId,$addressId,$userId,$orgId,$type,$goodsId,$isHasBarrel,$deliveryDate,$deliveryType,$orderRemark,$payType,$num){
- $addressInfo=Db::name('shop_address')->where('id',$addressId)->find();
- if(!$addressInfo){
- $this->error = "未选择地址";
- return false;
- }
- $ids = [];
- if($type==0){
- if(empty($goodsId) || empty($num)){
- $this->error ='参数错误';
- }
- $ids[] = ['id'=>$goodsId,'num'=>$num];
- }else if($type==1){
- if(empty($cartId)){
- $this->error='参数错误';
- }
- $gId = Db::name('wx_goods_cart')
- ->alias('a')
- ->join('wx_goods b','a.goods_id=b.id')
- ->where('a.id','in',explode(',',$cartId))
- ->where('a.user_id',$userId)
- ->where('b.del',0)
- ->where('b.org_id',$orgId)
- ->group('a.goods_id')
- ->select();
- foreach ($gId as $k=>$v){
- $ids[] = [
- 'id'=>$v['goods_id'],
- 'num'=>$v['nums'],
- ];
- }
- }
- $data =$this->formatOrder($ids);
- Db::startTrans();
- try {
- $params=[
- 'order_sn'=>get_unique_id(),
- 'org_id'=>$orgId,
- 'user_id'=>$userId,
- 'amount'=>$data['total'],
- 'status'=>0,
- 'create_time'=>date('Y-m-d H:i:s'),
- 'name'=>$addressInfo['name'],
- 'phone'=>$addressInfo['phone'],
- 'address'=>$addressInfo['content'],
- 'pay_type'=>$payType,
- 'order_remark'=>$orderRemark,
- 'delivery_date'=>$deliveryDate,
- 'delivery_type'=>$deliveryType,
- 'cash_price'=>$isHasBarrel==1?$data['cash_price']:0,
- 'is_has_barrel'=>$isHasBarrel,
- 'remark'=>$isHasBarrel==1?$data['remark']:"",
- 'order_type'=>$data['isWater'],
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- 'create_yyyymmddhh'=>date('YmdH'),
- ];
- $addOrder=Db::name('wx_orders')->insertGetId($params);
- if(!$addOrder){
- exception('操作失败');
- }
- foreach ($data['list'] as $kay=>$val){
- $gdata=[
- 'order_id'=>$addOrder,
- 'goods_id'=>$val['goods_id'],
- 'nums'=>$val['num'],
- 'price'=>$val['price'],
- ];
- $addOrderGoods=Db::name('wx_orders_goods')->insert($gdata);
- if(!$addOrderGoods){
- exception('操作失败');
- }
- // //减少库存
- // $dec= Db::name('wx_goods')
- // ->where('id',$val['goods_id'])
- // ->dec('stock', $val['num'])
- // ->update();
- // if(!$dec){
- // exception('操作失败');
- // }
- }
- //删除用户购物车信息
- if($type==1){
- $delCart=Db::name('wx_goods_cart')
- ->where('user_id',$userId)
- ->whereIn('id',explode(',',$cartId))->delete();
- if(!$delCart){
- exception('操作失败');
- }
- }
- Db::commit();
- return $addOrder;
- } catch (\Exception $e) {
- // 回滚事务
- $this->error = $e->getMessage();
- Db::rollback();
- return false;
- }
- }
- public function lists($page,$size,$userId,$orgId,$status){
- if($status>0){
- if($status==1){
- $map[]=['status','=',0];
- }elseif ($status==2){
- $ids = Db::name('wx_orders_refund')
- ->where('org_id',$orgId)
- ->where('user_id',$userId)
- ->where('del',0)
- ->column('order_id');
- foreach ($ids as $k=>$v){
- $r = $this->checkOrderSend($v);
- if($r==2){//全部退款
- unset($ids[$k]);
- }
- }
- if(!empty($ids)){
- $map[]=['id','not in',$ids];
- }
- $map[]=['status','=',1];
- }elseif ($status==3){
- $map[]=['status','=',2];
- }elseif ($status==4){
- $ids = Db::name('wx_orders_refund')
- ->where('org_id',$orgId)
- ->where('user_id',$userId)
- ->where('del',0)
- ->column('order_id');
- if(empty($ids)){
- $map[]=['id','=',-1];
- }else{
- $map[]=['id','in',$ids];
- }
- }
- }
- $map[]=['user_id','=',$userId];
- $map[]=['org_id','=',$orgId];
- $map[]=['del','=',0];
- $ret=Db::name('wx_orders')
- ->field('id,order_sn,status,amount')
- ->where($map)
- ->order('id','desc')
- ->page($page,$size)
- ->select();
- foreach ($ret as $k=>$v){
- $ret[$k]['is_refund'] = $this->haveRefund($v['id']);
- $ret[$k]['is_can_refund'] = $this->checkOrderRefund($v['id']);
- $orderGoods=Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums,og.price,og.goods_id,g.title,g.img,g.label')
- ->join('wx_goods g','g.id=og.goods_id')
- ->where('og.order_id',$v['id'])
- ->select();
- $nums=0;
- foreach ($orderGoods as $kk=>$vv){
- $nums+=$vv['nums'];
- $orderGoods[$kk]['label_name'] = $vv['label']>0?Db::name('wx_goods_label')
- ->where('id',$vv['label'])
- ->value('title'):"";
- $r = $this->checkGoodsRefund1($v['id'],$vv['goods_id']);
- $orderGoods[$kk]['is_refund'] =$r['is'];
- $orderGoods[$kk]['refund_status_name'] =$r['refund_status_name'];
- }
- $ret[$k]['total_num']=$nums;
- $ret[$k]['goods_num']=count($orderGoods);
- $ret[$k]['order_goods']=$orderGoods;
- $r = $this->checkOrderSend($v['id']);
- $ret[$k]['refund_status']=$r;
- $ret[$k]['refund_name'] = '';
- if($r==2){
- $ret[$k]['refund_name'] = '(已退款)';
- }elseif ($r==1){
- $ret[$k]['refund_name'] = '(部分退款)';
- }elseif ($r==3){
- $ret[$k]['refund_name'] = '(退款中)';
- }
- }
- return $ret?$ret:[];
- }
- public function details($ordersId){
- $ret = Db::name('wx_orders')
- ->field('del,del_time,create_yyyy,create_yyyymm,create_yyyymmdd,create_yyyymmddhh',true)
- ->where('id',$ordersId)->find();
- $ret['pay_time'] = $ret['pay_time']? $ret['pay_time']:"";
- $ret['send_time'] = $ret['send_time']? $ret['send_time']:"";
- $ret['finish_time'] = $ret['finish_time']? $ret['finish_time']:"";
- $ret['cancel_time'] = $ret['cancel_time']? $ret['cancel_time']:"";
- $ret['delivery_date'] = $ret['delivery_date']? $ret['delivery_date']:"";
- $ret['status_name'] = isset($this->status[$ret['status']])?$this->status[$ret['status']]:'';
- $r = $this->checkOrderSend($ordersId);
- if($r==2){
- $ret['status_name'] = $ret['status_name'].'(已退款)';
- }elseif ($r==1){
- $ret['status_name'] = $ret['status_name'].'(部分退款)';
- }elseif ($r==3){
- $ret['status_name'] = $ret['status_name'].'(退款中)';
- }
- $ret['refund_status']=$r;
- $ret['refund_name'] = '';
- if($r==2){
- $ret['refund_name'] = '(已退款)';
- }elseif ($r==1){
- $ret['refund_name'] = '(部分退款)';
- }elseif ($r==3){
- $ret['refund_name'] = '(退款中)';
- }
- $ret['pay_type_name'] = $ret['pay_type']==1?'微信支付':'';
- $ret['delivery_type_name'] = $ret['delivery_type']==1?'商家配送':'';
- $ret['order_type_name'] = $ret['order_type']==1?'普通订单':'桶装水订单';
- $ret['goods'] = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->where('og.order_id', $ret['id'])
- ->select();
- $totalNum = 0;
- $ret['is_refund'] = $this->haveRefund($ordersId);;
- $ret['is_can_refund'] = $this->checkOrderRefund($ordersId);
- foreach ($ret['goods'] as $k=>$v){
- $ret['goods'][$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
- ->where('id',$v['label'])
- ->value('title'):"";
- $r = $this->checkGoodsRefund($ordersId,$v['goods_id']);
- $ret['goods'][$k]['is_refund'] =$r['is'];
- $ret['goods'][$k]['refund_status_name'] =$r['refund_status_name'];
- $totalNum+=$v['num'];
- }
- $ret['comment'] = Db::name('wx_comment')
- ->field('create_yyyy,create_yyyymm,create_yyyymmdd',true)
- ->where('order_id',$ordersId)
- ->find();
- $ret['totalNum'] = $totalNum;
- $ret['goodsNum'] = count($ret['goods']);
- return $ret;
- }
- public function haveRefund($id){
- $refund = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('order_id',$id)
- ->find();
- return $refund?1:0;
- }
- public function checkOrderRefund($id){
- $goodsList = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label,o.status')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->join('wx_orders o', 'og.order_id=o.id')
- ->where('og.order_id', $id)
- ->select();
- $is = 0;
- foreach ($goodsList as $k=>$v){
- $refund = Db::name('wx_orders_refund')
- ->where('order_id',$id)
- ->where('del',0)
- ->where('goods_id',$v['goods_id'])
- // ->where('status','in',[0,1])
- ->find();
- if(!$refund && in_array($v['status'],[1,2])){
- $is=1;
- }
- }
- return $is;
- }
- public function checkOrderSend($id){
- $goodsList = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label,o.status')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->join('wx_orders o', 'og.order_id=o.id')
- ->where('og.order_id', $id)
- ->select();
- $is = 0;
- $iss= 0;
- foreach ($goodsList as $k=>$v){
- $refund = Db::name('wx_orders_refund')
- ->where('order_id',$id)
- ->where('del',0)
- ->where('goods_id',$v['goods_id'])
- ->where('status','in',[1])
- ->find();
- if($refund){
- $is++;
- }
- $refund = Db::name('wx_orders_refund')
- ->where('order_id',$id)
- ->where('del',0)
- ->where('goods_id',$v['goods_id'])
- ->where('status','in',[0])
- ->find();
- if($refund){
- $iss++;
- }
- }
- $r = 0;//未退款
- if($is >0 || $iss >0){
- if($is==count($goodsList)){
- $r = 2;//全部退款
- }elseif ($is < count($goodsList)){
- if ($iss >0){
- $r = 3;//退款中
- }else{
- $r = 1;//部分退款
- }
- }
- }
- return $r;
- }
- public function checkGoodsRefund1($orderId,$goodsId){
- $is = 0;//不显示
- $is_name = '';//不显示
- $refund = Db::name('wx_orders_refund')
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- ->where('del',0)
- ->where('status','=',1)
- ->find();
- if($refund){
- $is=2;//显示退款成功
- $is_name = '退款成功';
- }
- $refund = Db::name('wx_orders_refund')
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- ->where('del',0)
- ->where('status','=',2)
- ->find();
- if($refund){
- $is=3;//显示退款拒绝
- $is_name = '退款拒绝';
- }
- $refund = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- ->where('status','=',0)
- ->find();
- if($refund){
- $is=4;//显示审核处理中
- $is_name = '退款处理中';
- }
- return ['is'=>$is,'refund_status_name'=>$is_name];
- }
- public function checkGoodsRefund($orderId,$goodsId){
- $goodsList = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label,o.status')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->join('wx_orders o', 'og.order_id=o.id')
- ->where('og.order_id', $orderId)
- ->where('g.id', $goodsId)
- ->find();
- $is = 0;//不显示
- $is_name = '';//不显示
- $refund = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- // ->where('status','in',[0,1])
- ->find();
- if(!$refund && in_array($goodsList['status'],[1,2])){
- $is=1;//显示申请退款
- $is_name = '申请退款';
- }
- $refund = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- ->where('status','=',1)
- ->find();
- if($refund){
- $is=2;//显示退款成功
- $is_name = '退款成功';
- }
- $refund = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- ->where('status','=',2)
- ->find();
- if($refund){
- $is=3;//显示退款拒绝
- $is_name = '退款拒绝';
- }
- $refund = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- ->where('status','=',0)
- ->find();
- if($refund){
- $is=4;//显示审核处理中
- $is_name = '退款处理中';
- }
- return ['is'=>$is,'refund_status_name'=>$is_name];
- }
- public function getRefundGoodsNum($goodsId,$orderId){
- $refund = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- // ->where('status','=',3)
- ->sum('num');
- return $refund?$refund:0;
- }
- public function accomplish($ordersId){
- $data=[
- 'status'=>2,
- 'finish_time'=>date('Y-m-d H:i:s'),
- ];
- $ret = Db::name('g_orders')->where('id', $ordersId)->update($data);
- return $ret;
- }
- public function refundOrder($userId){
- $id = input('id/d',0);
- $data = [
- 'money' => input('money/f',0),
- 'status' => input('status/d',0),
- 'remark' => input('remark','','trim'),
- 'deal_user_id'=>$userId,
- 'deal_time'=>getTime(),
- ];
- if($data['money'] <= 0){
- $this->error = '金额必须大于0';
- return false;
- }
- $pay = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('id',$id)->find();
- if(!$pay||$pay['status'] != 0){
- $this->error = '申请单不存在';
- return false;
- }
- if(!in_array($data['status'],[1,2])){
- $this->error = '参数错误';
- return false;
- }
- $sn = get_unique_id('TPH');
- if($data['money'] > $pay['amount']){
- $this->error = '退款金额过大';
- return false;
- }
- Db::startTrans();
- try{
- $order = Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('id',$pay['id'])
- ->lock(true)
- ->find();
- if(!$order){
- \exception('订单不存在');
- }
- if($data['status']==2){
- $data['money'] = 0;
- }
- $ret = Db::name('wx_orders_refund')
- ->where('id',$pay['id'])->update($data);
- if(!$ret){
- \exception('操作失败');
- }
- if($data['status']==1){
- $orderInfo = Db::name('wx_orders')
- ->where('id',$pay['order_id'])
- ->find();
- $r = Db::name('wx_orders')
- ->where('id',$pay['order_id'])
- ->setInc('refund_money',$data['money']);
- if(!$r){
- \exception('修改退款金额失败');
- }
- $config = config('app.wx_mini_config');
- $app = \EasyWeChat\Factory::payment($config);
- $ret = $app->refund->byOutTradeNumber($orderInfo['order_sn'], $sn, $orderInfo['amount']*100, $data['money']*100);
- if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
- trace(json_encode($ret));
- \exception('退款申请提交失败');
- }
- }
- Db::commit();
- return true;
- }catch (\Exception $e){
- Db::rollback();
- trace($e->getMessage());
- $this->error = $e->getMessage();
- return false;
- }
- }
- public function formatOrder($ids){
- $list = [];
- $total = 0;
- $isWater = 0;
- $cash_price = 0;
- $remark = [];
- $totalNum = 0;
- foreach ($ids as $k=>$v){
- $ret=Db::name('wx_goods')
- ->field('price,id as goods_id,title,img,label,barrel_id')
- ->where('id','=',$v['id'])
- ->find();
- $ret['label_name'] = $ret['label']>0?Db::name('wx_goods_label')
- ->where('id',$ret['label'])
- ->value('title'):"";
- $ret['num'] = $v['num'];
- $total+=$v['num']*$ret['price'];
- $totalNum+=$v['num'];
- if($ret['barrel_id'] >0){
- $isWater = 1;
- $wx_barrel = Db::name('wx_barrel')
- ->where('id',$ret['barrel_id'])
- ->find();
- $cash_price+=$wx_barrel['cash_price']*$v['num'];
- $remark[] =$ret['title'].'押金:'.$wx_barrel['cash_price']*$v['num'];
- }
- $list[] = $ret;
- }
- $data = [
- 'list'=>$list,
- 'goodsNum'=>count($list),
- 'totalNum'=>$totalNum,
- 'isWater'=>$isWater,
- 'remark'=>$remark?implode(';',$remark):"",
- 'cash_price'=>sprintf("%01.2f", $cash_price),
- 'total'=>sprintf("%01.2f", $total),
- ];
- return $data;
- }
- public function buyBarrel($orgId,$userId,$id,$num){
- $info = Db::name('wx_barrel')
- ->where('id',$id)
- ->where('enable',1)
- ->where('del',0)
- ->find();
- if(empty($info)){
- HelpHander::error('空桶不存在');
- }
- $params = [
- 'sn'=>get_unique_id(),
- 'user_id'=>$userId,
- 'barrel_id'=>$id,
- 'org_id'=>$orgId,
- 'num'=>$num,
- 'amount'=>$num*$info['cash_price'],
- 'create_time'=>getTime()
- ];
- $res = Db::name('wx_cash_pay_log')
- ->insertGetId($params);
- return $res;
- }
- public function refundBarrel($orgId,$userId,$id,$num){
- $cash = Db::name('wx_cash')
- ->where('id',$id)
- ->find();
- if($cash['num'] <$num){
- HelpHander::error('超出退款数量');
- }
- Db::startTrans();
- try{
- $params = [
- 'sn'=>get_unique_id(),
- 'user_id'=>$userId,
- 'barrel_id'=>$cash['barrel_id'],
- 'org_id'=>$orgId,
- 'num'=>$num,
- 'amount'=>$num*$cash['price'],
- 'create_time'=>getTime(),
- 'cash_id'=>$id
- ];
- $res = Db::name('wx_cash')
- ->where('id',$cash['id'])
- ->setDec('num',$num);
- Db::name('wx_cash')
- ->where('id',$cash['id'])
- ->setDec('amount',$num*$cash['price']);
- if(!$res){
- \exception('操作失败1');
- }
- $res = Db::name('wx_cash_refund')
- ->insertGetId($params);
- Db::commit();
- return $res;
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- }
- public function checkoutApplyRefund($userId,$orgId,$type,$orderId,$goodsId){
- if(empty($orderId)){
- $this->error='参数错误';
- return false;
- }
- if($type==0 && empty($goodsId)){
- $this->error='参数错误';
- return false;
- }
- if($type==0){
- $goodsList = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->where('og.order_id', $orderId)
- ->where('og.goods_id', $goodsId)
- ->select();
- }else{
- $goodsList = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->where('og.order_id', $orderId)
- ->select();
- }
- foreach ($goodsList as $k=>$v){
- //$rNum = $this->getRefundGoodsNum($v['goods_id'],$orderId);
- //$goodsList[$k]['refund_num'] = $v['num']-$rNum;
- // $goodsList[$k]['refund_num'] = $v['num'];
- $goodsList[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
- ->where('id',$v['label'])
- ->value('title'):"";
- $flag= Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('goods_id',$v['goods_id'])
- ->where('order_id',$orderId)
- ->find();
- if($flag){
- unset($goodsList[$k]);
- }
- }
- return $goodsList;
- }
- public function checkoutApplyRefund1($orderId,$goodsId){
- if(empty($orderId)){
- $this->error='参数错误';
- return false;
- }
- if(empty($goodsId)){
- $this->error='参数错误';
- return false;
- }
- $goodsList = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->where('og.order_id', $orderId)
- ->where('og.goods_id', 'in',explode(',',$goodsId))
- ->select();
- foreach ($goodsList as $k=>$v){
- //$rNum = $this->getRefundGoodsNum($v['goods_id'],$orderId);
- //$goodsList[$k]['refund_num'] = $v['num']-$rNum;
- // $goodsList[$k]['refund_num'] = $v['num'];
- $goodsList[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
- ->where('id',$v['label'])
- ->value('title'):"";
- $flag= Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('goods_id',$v['goods_id'])
- ->where('order_id',$orderId)
- ->find();
- if($flag){
- unset($goodsList[$k]);
- }
- }
- return $goodsList;
- }
- public function addApplyRefund($userId,$orgId,$goodsJson,$orderId,$remark){
- if(empty($orderId)){
- $this->error='参数错误3';
- return false;
- }
- if(empty($goodsJson)){
- $this->error='参数错误2';
- return false;
- }
- $goodsList = json_decode($goodsJson,'true');
- if(empty($goodsList)){
- $this->error='参数错误1';
- return false;
- }
- if(!$this->checkOrderRefund($orderId)){
- $this->error='当前订单不能申请退款';
- return false;
- }
- foreach ($goodsList as $k=>$v){
- $flag= Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('goods_id',$v['id'])
- ->where('order_id',$orderId)
- ->find();
- if(!$flag){
- $wx_orders_goods = Db::name('wx_orders_goods')
- ->where('order_id',$orderId)
- ->where('goods_id',$v['id'])
- ->find();
- $goodsInfo = Db::name('wx_goods')
- ->where('id',$v['id'])
- ->find();
- if(empty($goodsInfo)){
- $this->error='商品不存在';
- return false;
- }
- if($v['num'] >$wx_orders_goods['nums']){
- $this->error=$goodsInfo['title'].'退款数量超过购买数量';
- return false;
- }
- $a[] = [
- 'sn'=>get_unique_id(),
- 'order_id'=>$orderId,
- 'goods_id'=>$v['id'],
- 'amount'=>$v['num']*$wx_orders_goods['price'],
- 'num'=>$v['num'],
- 'reason'=>$remark,
- 'user_id'=>$userId,
- 'org_id'=>$orgId,
- 'create_time'=>getTime(),
- ];
- }
- }
- if(empty($a)){
- $this->error = '没有符合退款条件的商品';
- return false;
- }
- $res =Db::name('wx_orders_refund')
- ->insertAll($a);
- if(!$res){
- $this->error = '操作失败';
- return false;
- }
- $data = [
- 'orderId'=>$orderId,
- 'goodsId'=>implode(',',array_column($a,'goods_id')),
- ];
- return $data;
- }
- public function orderRefundDetail($orgId,$orderId,$goodsId){
- $map = [];
- if($goodsId!=''){
- $map[] = ['goods_id','in',explode(',',$goodsId)];
- }
- $list = Db::name('wx_orders_refund')
- ->where('del',0)
- ->field('order_id,goods_id,status,create_time,num')
- ->where('order_id',$orderId)
- ->where($map)
- ->where('org_id',$orgId)
- ->select();
- $sn = Db::name('wx_orders')
- ->where('id',$orderId)
- ->value('order_sn');
- foreach ($list as $k=>&$v){
- $goodsInfo = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.price,g.title,g.img')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->where('og.order_id', $orderId)
- ->where('g.id', $v['goods_id'])
- ->find();
- $v['price'] = $goodsInfo['price'];
- $v['title'] = $goodsInfo['title'];
- $v['img'] = $goodsInfo['img'];
- $v['statusName'] = $this->rStatus[$v['status']];
- }
- return ['list'=>$list,'sn'=>$sn];
- }
- public function goodsRefundDetail($orgId,$orderId,$goodsId){
- $v = Db::name('wx_orders_refund')
- ->field('deal_user_id,user_id,order_id,goods_id,status,create_time,num,amount,reason,deal_time,money,remark')
- ->where('order_id',$orderId)
- ->where('goods_id',$goodsId)
- ->where('del',0)
- ->where('org_id',$orgId)
- ->find();
- $goodsInfo = Db::name('wx_orders_goods')
- ->alias('og')
- ->field('og.price,g.title,g.img')
- ->join('wx_goods g', 'g.id=og.goods_id')
- ->where('og.order_id', $orderId)
- ->where('g.id', $goodsId)
- ->find();
- $v['order_sn'] = Db::name('wx_orders')
- ->where('id',$orderId)
- ->value('order_sn');
- $v['user_name'] = Db::name('wxuser')
- ->where('id',$v['user_id'])
- ->value('nickname');
- $v['deal_user_name'] = Db::name('user')
- ->where('id',$v['deal_user_id'])
- ->value('real_name');
- $v['goods'] = [
- 'price'=>$goodsInfo['price'],
- 'title'=>$goodsInfo['title'],
- 'img'=>$goodsInfo['img'],
- 'num'=>$v['num'],
- ];
- $v['statusName'] = $this->rStatus[$v['status']];
- $v['deal_time'] = $v['deal_time']? $v['deal_time']:'';
- unset($v['num']);
- return $v;
- }
- public function orderRefund($userId,$orgId){
- $params = input('num',[]);
- $orderId = input('id/d',0);
- $reason = input('reason','');
- if(empty($orderId)){
- $this->error='参数错误3';
- return false;
- }
- if(empty($params)){
- $this->error='参数错误2';
- return false;
- }
- if(!$this->checkOrderRefund($orderId)){
- $this->error='当前订单不能申请退款';
- return false;
- }
- $goodsList = [];
- foreach ($params as $k=>$v){
- $goodsList[] = [
- 'id'=>$k,
- 'num'=>$v,
- ];
- }
- if(empty($goodsList)){
- $this->error='参数错误1';
- return false;
- }
- $total = 0;
- foreach ($goodsList as $k=>$v){
- $flag= Db::name('wx_orders_refund')
- ->where('del',0)
- ->where('goods_id',$v['id'])
- ->where('order_id',$orderId)
- ->find();
- if(!$flag){
- $wx_orders_goods = Db::name('wx_orders_goods')
- ->where('order_id',$orderId)
- ->where('goods_id',$v['id'])
- ->find();
- $goodsInfo = Db::name('wx_goods')
- ->where('id',$v['id'])
- ->find();
- if(empty($goodsInfo)){
- $this->error='商品不存在';
- return false;
- }
- if($v['num'] >$wx_orders_goods['nums']){
- $this->error=$goodsInfo['title'].'退款数量超过购买数量';
- return false;
- }
- $m = $v['num']*$wx_orders_goods['price'];
- $a[] = [
- 'sn'=>get_unique_id(),
- 'order_id'=>$orderId,
- 'goods_id'=>$v['id'],
- 'amount'=>$m,
- 'num'=>$v['num'],
- 'reason'=>$reason,
- 'user_id'=>$userId,
- 'org_id'=>$orgId,
- 'status'=>1,
- 'money'=>$m,
- 'create_time'=>getTime(),
- ];
- $total+=$m;
- }
- }
- if(empty($a)){
- $this->error = '没有符合退款条件的商品';
- return false;
- }
- Db::startTrans();
- try{
- $res =Db::name('wx_orders_refund')
- ->insertAll($a);
- if(!$res){
- \exception( '操作失败');
- }
- $orderInfo = Db::name('wx_orders')
- ->where('id',$orderId)
- ->find();
- $r = Db::name('wx_orders')
- ->where('id',$orderId)
- ->setInc('refund_money',$total);
- if(!$r){
- \exception('修改退款金额失败');
- }
- $sn = get_unique_id('TPH');
- $config = config('app.wx_mini_config');
- $app = \EasyWeChat\Factory::payment($config);
- $ret = $app->refund->byOutTradeNumber($orderInfo['order_sn'], $sn, $orderInfo['amount']*100, $total*100);
- if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
- trace(json_encode($ret));
- \exception('退款申请提交失败');
- }
- Db::commit();
- return true;
- }catch (Exception $e){
- Db::rollback();
- $this->error = $e->getMessage();
- return false;
- }
- }
- }
|