123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace app\common\model;
- use think\Db;
- use EasyWeChat\Factory;
- class WxCash extends Base
- {
- public $status = [
- 0=> '退款处理中',
- 1=> '退款成功',
- 2=> '退款拒绝',
- ];
- public function lists($page,$size,$userId,$orgId,$status){
- if($status>0){
- if($status==1){
- $map[]=['status','=',0];
- }elseif ($status==2){
- $map[]=['status','=',1];
- }elseif ($status==3){
- $map[]=['status','=',2];
- }elseif ($status==4){
- $map[]=['status','in',[5,6,7]];
- }
- }
- $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){
- $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[$k]['label_name'] = $vv['label']>0?Db::name('wx_goods_label')
- ->where('id',$vv['label'])
- ->value('title'):"";
- }
- $ret[$k]['total_num']=$nums;
- $ret[$k]['goods_num']=count($orderGoods);
- $ret[$k]['order_goods']=$orderGoods;
- }
- return $ret?$ret:[];
- }
- public function details($ordersId){
- $ret = Db::name('g_orders')->where('id', $ordersId)->find();
- $ret['goods'] = Db::name('g_order_goods')
- ->alias('og')
- ->field('og.nums,og.price,og.goods_id,g.title,g.img')
- ->join('g_goods g', 'g.id=og.goods_id')
- ->where('og.order_id', $ret['id'])
- ->select();
- $rd = Db::name('g_order_refund')
- ->where('order_id', $ret['id'])
- ->find();
- if(empty($rd)){
- $rd = [
- 'sn'=>'',
- 'order_id'=>'',
- 'money'=>'',
- 'remark'=>'',
- 'refund_money'=>'',
- 'refund_remark'=>'',
- ];
- }
- $ret['refund'] = $rd;
- $nums=0;
- foreach ($ret['goods'] as $k=>$v){
- $nums+=$v['nums'];
- }
- $ret['sum']=$nums;
- if($ret['status']==1 && time() < (strtotime($ret['create_time'])+(30*24*3600))){
- $ret['tk'] = 1;
- }else{
- $ret['tk'] = 0;
- }
- $s = [
- '未支付',
- '已支付',
- '已完成',
- '已取消',
- '已退款',
- '退款拒绝',
- '退款中',
- ];
- $ret['statusTxt']=isset($s[$ret['status']])?$s[$ret['status']]:'';
- return $ret;
- }
- 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){
- $data = [
- 'money' => input('money/f',0),
- 'order_id' => input('id/d',0),
- 'user_id' => $userId,
- 'remark' => input('remark','','trim'),
- 'create_time' => date('Y-m-d H:i:s'),
- ];
- if($data['money'] <= 0){
- $this->error = '金额必须大于0';
- return false;
- }
- $pay = Db::name('g_orders')
- ->where('id',$data['order_id'])->find();
- if(!$pay||$pay['status'] != 1){
- $this->error = '订单不存在';
- return false;
- }
- $data['sn'] = get_unique_id('TPH');
- $data['org_id'] = $pay['org_id'];
- if($data['money'] > $pay['amount']){
- $this->error = '退款金额过大';
- return false;
- }
- Db::startTrans();
- try{
- $ret = Db::name('g_order_refund')->insert($data);
- if(!$ret){
- \exception('操作失败');
- }
- $order = Db::name('g_orders')
- ->where('id',$pay['id'])
- ->lock(true)
- ->find();
- if(!$order){
- \exception('订单不存在');
- }
- $ret = Db::name('g_orders')
- ->where('id',$pay['id'])->update([
- 'refund_money' => $data['money'],
- 'status' => 4
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- $config = config('app.wx_mini_config');
- $app = \EasyWeChat\Factory::payment($config);
- $ret = $app->refund->byOutTradeNumber($pay['order_sn'], $data['sn'], $pay['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 = [];
- 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'];
- 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,
- 'isWater'=>$isWater,
- 'remark'=>$remark?implode(';',$remark):"",
- 'cash_price'=>sprintf("%01.2f", $cash_price),
- 'total'=>sprintf("%01.2f", $total),
- ];
- return $data;
- }
- }
|