RefundlistController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. <?php
  2. /**
  3. * 退款申请列表
  4. */
  5. namespace app\admin\controller;
  6. use cmf\controller\AdminBaseController;
  7. use think\facade\Db;
  8. class RefundlistController extends AdminbaseController {
  9. protected function getType($k=''){
  10. $type=array(
  11. '0'=>'仅退款',
  12. '1'=>'退货退款',
  13. );
  14. if($k===''){
  15. return $type;
  16. }
  17. return isset($type[$k])?$type[$k]:'';
  18. }
  19. protected function getShopResult($k=''){
  20. $result=array(
  21. '-1'=>'拒绝',
  22. '0'=>'处理中',
  23. '1'=>'同意',
  24. );
  25. if($k===''){
  26. return $result;
  27. }
  28. return isset($result[$k])?$result[$k]:'';
  29. }
  30. protected function getPlatformResult($k=''){
  31. $result=array(
  32. '-1'=>'拒绝',
  33. '0'=>'待处理',
  34. '1'=>'同意',
  35. );
  36. if($k===''){
  37. return $result;
  38. }
  39. return isset($result[$k])?$result[$k]:'';
  40. }
  41. protected function getStatus($k=''){
  42. $status=array(
  43. '-1'=>'买家已取消',
  44. '0'=>'处理中',
  45. '1'=>'已完成',
  46. );
  47. if($k===''){
  48. return $status;
  49. }
  50. return isset($status[$k])?$status[$k]:'';
  51. }
  52. /*退款申请列表*/
  53. function index(){
  54. $data = $this->request->param();
  55. $map=[];
  56. $start_time=isset($data['start_time']) ? $data['start_time']: '';
  57. $end_time=isset($data['end_time']) ? $data['end_time']: '';
  58. if($start_time!=""){
  59. $map[]=['addtime','>=',strtotime($start_time)];
  60. }
  61. if($end_time!=""){
  62. $map[]=['addtime','<=',strtotime($end_time) + 60*60*24];
  63. }
  64. $status=isset($data['status']) ? $data['status']: '';
  65. if($status!=''){
  66. $map[]=['status','=',$status];
  67. }
  68. $goods_type=isset($data['goods_type']) ? $data['goods_type']: '';
  69. if($goods_type!=''){
  70. if($goods_type==0){
  71. $map[]=['shop_uid','<>',1];
  72. }else{
  73. $map[]=['shop_uid','=',1];
  74. }
  75. }
  76. $buyer_uid=isset($data['buyer_uid']) ? $data['buyer_uid']: '';
  77. if($buyer_uid!=''){
  78. $map[]=['uid','=',$buyer_uid];
  79. }
  80. $seller_uid=isset($data['seller_uid']) ? $data['seller_uid']: '';
  81. if($seller_uid!=''){
  82. $map[]=['shop_uid','=',$seller_uid];
  83. }
  84. $lists = Db::name("shop_order_refund")
  85. ->where($map)
  86. ->order("addtime DESC")
  87. ->paginate(20);
  88. $lists->each(function($v,$k){
  89. $v['buyer_info']=getUserInfo($v['uid']);
  90. $v['seller_info']=getUserInfo($v['shop_uid']);
  91. $v['platform_interpose_thumb']=get_upload_path($v['platform_interpose_thumb']);
  92. $orderinfo=getShopOrderInfo(['id'=>$v['orderid']],'orderno');
  93. $v['orderno']=$orderinfo['orderno'];
  94. return $v;
  95. });
  96. $lists->appends($data);
  97. $page = $lists->render();
  98. $this->assign('lists', $lists);
  99. $this->assign("page", $page);
  100. $this->assign("status", $this->getStatus());
  101. $this->assign("type", $this->getType());
  102. $this->assign("shop_result", $this->getShopResult());
  103. $this->assign("platform_result", $this->getPlatformResult());
  104. return $this->fetch();
  105. }
  106. /*退款编辑*/
  107. function edit(){
  108. $id = $this->request->param('id', 0, 'intval');
  109. $data=Db::name('shop_order_refund')
  110. ->where("id={$id}")
  111. ->find();
  112. if(!$data){
  113. $this->error("信息错误");
  114. }
  115. $orderinfo=getShopOrderInfo(['id'=>$data['orderid']],'orderno,phone');
  116. $data['orderno']=$orderinfo['orderno'];
  117. $data['phone']=$orderinfo['phone'];
  118. $data['buyer_info']=getUserInfo($data['uid']);
  119. $data['seller_info']=getUserInfo($data['shop_uid']);
  120. $data['platform_interpose_thumb']=get_upload_path($data['platform_interpose_thumb']);
  121. //获取平台的处理意见
  122. $platform_info=Db::name("shop_order_refund_list")->where("orderid={$data['orderid']} and type=3")->find();
  123. $data['platform_handle_desc']=isset($platform_info['handle_desc'])?$platform_info['handle_desc']:'';
  124. $this->assign("status", $this->getStatus());
  125. $this->assign("type", $this->getType());
  126. $this->assign("shop_result", $this->getShopResult());
  127. $this->assign("platform_result", $this->getPlatformResult());
  128. $this->assign('data', $data);
  129. return $this->fetch();
  130. }
  131. /*退款编辑提交*/
  132. function edit_post(){
  133. if ($this->request->isPost()){
  134. $data = $this->request->param();
  135. $platform_result=$data['platform_result'];
  136. $id=$data['id'];
  137. $desc=trim($data['desc']);
  138. $orderid=$data['orderid'];
  139. //判断订单是否存在
  140. $order_info=getShopOrderInfo(['id'=>$orderid],"*");
  141. if(!$order_info){
  142. $this->error("订单不存在");
  143. }
  144. $status=$order_info['status'];
  145. /*if($status!=5){
  146. $this->error("订单未申请退款");
  147. }*/
  148. $refund_info=getShopOrderRefundInfo(['orderid'=>$orderid]);
  149. $refund_status=$refund_info['status'];
  150. if($refund_status==-1){
  151. $this->error("买家已取消退款申请");
  152. }
  153. if($refund_status==1){ //已经完成
  154. $this->error("退款已处理完成");
  155. }
  156. if($platform_result==""){
  157. $this->error("请选择处理结果");
  158. }
  159. if($desc){
  160. if(mb_strlen($desc)>300){
  161. $this->error("处理意见在300字以内");
  162. }
  163. }
  164. $now=time();
  165. if(!$platform_result){
  166. $platform_result=-1;
  167. }
  168. $data['platform_process_time']=$now;
  169. $data['platform_result']=$platform_result;
  170. $data['admin']=cmf_get_current_admin_id();
  171. $data['ip']= ip2long($_SERVER["REMOTE_ADDR"]) ;
  172. $data['status']=1;
  173. unset($data['orderid']);
  174. unset($data['desc']);
  175. $res = DB::name('shop_order_refund')->update($data);
  176. if($res===false){
  177. $this->error("修改失败!");
  178. }
  179. if($platform_result==1){ //平台同意退款
  180. //将钱退给买家
  181. setUserBalance($order_info['uid'],1,$order_info['total']);
  182. //添加余额操作记录
  183. $data1=array(
  184. 'uid'=>$order_info['uid'],
  185. 'touid'=>$order_info['shop_uid'],
  186. 'balance'=>$order_info['total'],
  187. 'type'=>1,
  188. 'action'=>6, //买家发起退款,平台介入后同意
  189. 'orderid'=>$orderid,
  190. 'addtime'=>$now
  191. );
  192. addBalanceRecord($data1);
  193. //处理订单状态
  194. $data2=array(
  195. 'refund_status'=>1,
  196. 'refund_endtime'=>$now,
  197. );
  198. changeShopOrderStatus($order_info['uid'],$orderid,$data2);
  199. //减去商品的销量
  200. changeShopGoodsSaleNums($order_info['goodsid'],0,$order_info['nums']);
  201. //减去店铺销量
  202. changeShopSaleNums($order_info['shop_uid'],0,$order_info['nums']);
  203. //给买家发送消息
  204. $title="你的商品“".$order_info['goods_name']."”发起的退款,平台已同意退款";
  205. //写入订单消息列表
  206. $data3=array(
  207. 'title'=>$title,
  208. 'orderid'=>$orderid,
  209. 'uid'=>$order_info['uid'],
  210. 'addtime'=>$now,
  211. 'type'=>'0'
  212. );
  213. addShopGoodsOrderMessage($data3);
  214. //发送腾讯云IM
  215. txMessageIM($title,$order_info['uid'],'goodsorder_admin');
  216. //给卖家发送消息
  217. $title1="买家商品“".$order_info['goods_name']."”发起的退款,平台已同意退款";
  218. $data4=array(
  219. 'title'=>$title1,
  220. 'orderid'=>$orderid,
  221. 'uid'=>$order_info['shop_uid'],
  222. 'addtime'=>$now,
  223. 'type'=>'1'
  224. );
  225. addShopGoodsOrderMessage($data4);
  226. //发送腾讯云IM
  227. txMessageIM($title,$order_info['shop_uid'],'goodsorder_admin');
  228. //退款协商记录
  229. $refund_history_data=array(
  230. 'orderid'=>$orderid,
  231. 'type'=>3, //平台
  232. 'addtime'=>$now,
  233. 'desc'=>'平台同意退款',
  234. 'handle_desc'=>$desc
  235. );
  236. $action='编辑退款列表ID: '.$orderid.'--平台同意';
  237. setAdminLog($action);
  238. }else{ //平台拒绝
  239. //修改订单状态
  240. $data1=array(
  241. 'refund_status'=>-1, //退款失败
  242. 'refund_endtime'=>$now,
  243. );
  244. if($order_info['receive_time']>0){
  245. $data1['status']=3; //待评价
  246. }else{
  247. if($order_info['shipment_time']>0){
  248. $data1['status']=2; //待收货
  249. }else{
  250. $data1['status']=1; //待发货
  251. }
  252. }
  253. changeShopOrderStatus($order_info['uid'],$orderid,$data1);
  254. //给买家发送消息
  255. $title="你的商品“".$order_info['goods_name']."”发起的退款,平台已拒绝";
  256. //写入订单消息列表
  257. $data2=array(
  258. 'title'=>$title,
  259. 'orderid'=>$orderid,
  260. 'uid'=>$order_info['uid'],
  261. 'addtime'=>$now,
  262. 'type'=>'0'
  263. );
  264. addShopGoodsOrderMessage($data2);
  265. //发送腾讯云IM
  266. txMessageIM($title,$order_info['uid'],'goodsorder_admin');
  267. //给卖家发送消息
  268. $title1="买家商品“".$order_info['goods_name']."”发起的退款,平台已拒绝";
  269. $data3=array(
  270. 'title'=>$title1,
  271. 'orderid'=>$orderid,
  272. 'uid'=>$order_info['shop_uid'],
  273. 'addtime'=>$now,
  274. 'type'=>'1'
  275. );
  276. addShopGoodsOrderMessage($data3);
  277. //发送腾讯云IM
  278. txMessageIM($title,$order_info['shop_uid'],'goodsorder_admin');
  279. //退款协商记录
  280. $refund_history_data=array(
  281. 'orderid'=>$orderid,
  282. 'type'=>3,
  283. 'addtime'=>$now,
  284. 'desc'=>'平台拒绝退款',
  285. 'handle_desc'=>$desc
  286. );
  287. $action='编辑退款列表ID: '.$orderid.'--平台拒绝';
  288. setAdminLog($action);
  289. }
  290. //写入退款协商记录
  291. setGoodsOrderRefundList($refund_history_data);
  292. $this->success("退款处理成功!");
  293. }
  294. }
  295. //平台自营处理订单退款
  296. public function platformedit(){
  297. $data=$this->request->param();
  298. $id=$data['id'];
  299. $data=Db::name('shop_order_refund')
  300. ->where("id={$id}")
  301. ->find();
  302. if(!$data){
  303. $this->error("信息错误");
  304. }
  305. //获取订单详情
  306. $orderinfo=getShopOrderInfo(['id'=>$data['orderid']],'orderno,phone');
  307. $data['orderno']=$orderinfo['orderno'];
  308. $data['phone']=$orderinfo['phone'];
  309. $data['buyer_info']=getUserInfo($data['uid']);
  310. //获取卖家拒绝退款原因
  311. $key='getRefundRefuseReason';
  312. $refuse_reason=getcaches($key);
  313. if(!$refuse_reason){
  314. $refuse_reason=DB::name('shop_refuse_reason')
  315. ->field("id,name")
  316. ->where('status=1')
  317. ->order("list_order asc,id desc")
  318. ->select();
  319. }
  320. $this->assign("status", $this->getStatus());
  321. $this->assign("type", $this->getType());
  322. $this->assign('data', $data);
  323. $this->assign('refuse_reason', $refuse_reason);
  324. return $this->fetch();
  325. }
  326. //平台自营处理退款
  327. public function platformedit_post(){
  328. $platform_info=Db::name("shop_apply")->where("uid=1")->find();
  329. if(!$platform_info){
  330. $this->error("请先填写自营平台店铺信息");
  331. }
  332. $data=$this->request->param();
  333. $id=$data['id'];
  334. $orderid=$data['orderid'];
  335. $type=$data['type'];
  336. $refuse_reason=$data['reason'];
  337. $refuse_desc=$data['refuse_desc'];
  338. //判断订单信息
  339. $where=array(
  340. 'id'=>$orderid
  341. );
  342. $order_info=getShopOrderInfo($where);
  343. if(!$order_info){
  344. $this->error("订单不存在");
  345. }
  346. $status=$order_info['status'];
  347. if($status!=5){
  348. $this->error("订单未申请退款");
  349. }
  350. //获取退款详情
  351. $where1=array(
  352. 'orderid'=>$orderid
  353. );
  354. $refund_info=getShopOrderRefundInfo($where1);
  355. $refund_status=$refund_info['status'];
  356. $is_platform_interpose=$refund_info['is_platform_interpose'];
  357. $platform_result=$refund_info['platform_result'];
  358. $shop_result=$refund_info['shop_result'];
  359. $shop_process_num=$refund_info['shop_process_num'];
  360. if($refund_status==-1){
  361. $this->error("买家已取消退款申请");
  362. }
  363. if($refund_status==1){ //已经完成
  364. if($is_platform_interpose==1){ //平台介入
  365. if($platform_result==1){ //平台同意
  366. $this->error("平台介入已同意退款");
  367. }elseif($is_platform_interpose==-1){ //平台拒绝
  368. $this->error("平台介入已拒绝退款");
  369. }
  370. }else{
  371. if($shop_result==1){ //卖家同意
  372. $this->error("平台已同意退款");
  373. }elseif($shop_result==-1){
  374. $this->error("平台已拒绝退款");
  375. }
  376. }
  377. }else{
  378. if($shop_result==-1){
  379. $this->error("平台已经拒绝,不能再操作");
  380. }elseif($shop_result==1){
  381. $this->error("平台已经同意,不能再操作");
  382. }
  383. if($shop_process_num>=3){
  384. $this->error('平台已经拒绝'.$shop_process_num.'次,不能再操作');
  385. }
  386. }
  387. if($type==0){ //拒绝
  388. if(!$refuse_reason){
  389. $this->error("请选择拒绝理由");
  390. }
  391. if(mb_strlen($refuse_desc)>300){
  392. $this->error("拒绝详细原因应在300字以内");
  393. }
  394. //更新退款信息
  395. $where=array(
  396. 'orderid'=>$orderid
  397. );
  398. $data=array(
  399. 'shop_result'=>-1,
  400. 'shop_process_time'=>time(),
  401. 'shop_process_num'=>$shop_process_num+1,
  402. );
  403. $res=changeGoodsOrderRefund($where,$data);
  404. if(!$res){
  405. $rs['code']=1001;
  406. $rs['msg']='退款处理失败,请重试';
  407. return $rs;
  408. }
  409. //修改订单信息
  410. $data1=array(
  411. 'refund_shop_result'=>-1 //卖家处理状态为拒绝
  412. );
  413. changeShopOrderStatus(1,$orderid,$data1);
  414. $title="你的商品“".$order_info['goods_name']."”卖家拒绝退款";
  415. //写入订单消息列表
  416. $data3=array(
  417. 'title'=>$title,
  418. 'orderid'=>$orderid,
  419. 'uid'=>$order_info['uid'],
  420. 'addtime'=>time(),
  421. 'type'=>'0'
  422. );
  423. addShopGoodsOrderMessage($data3);
  424. //发送腾讯云IM
  425. txMessageIM($title,$order_info['uid'],'goodsorder_admin');
  426. $refund_history_data=array(
  427. 'orderid'=>$orderid,
  428. 'type'=>2,
  429. 'addtime'=>time(),
  430. 'desc'=>'卖家拒绝退款',
  431. 'refuse_reason'=>$refuse_reason,
  432. 'handle_desc'=>$refuse_desc
  433. );
  434. }else{ //自营店铺同意退款
  435. //更改退款信息
  436. $where=array(
  437. 'orderid'=>$orderid
  438. );
  439. $data=array(
  440. 'shop_result'=>1,
  441. 'shop_process_time'=>time(),
  442. 'status'=>1
  443. );
  444. $res=changeGoodsOrderRefund($where,$data);
  445. if(!$res){
  446. $this->error("退款处理失败,请重试");
  447. }
  448. //更改订单状态
  449. $data1=array(
  450. 'refund_status'=>1,
  451. 'refund_shop_result'=>1,
  452. 'refund_endtime'=>time(),
  453. );
  454. changeShopOrderStatus(1,$orderid,$data1);
  455. //给买家退钱
  456. setUserBalance($order_info['uid'],1,$order_info['total']);
  457. //添加余额操作记录
  458. $data2=array(
  459. 'uid'=>$order_info['uid'],
  460. 'touid'=>$order_info['shop_uid'],
  461. 'balance'=>$order_info['total'],
  462. 'type'=>1,
  463. 'action'=>5, //买家发起退款,卖家同意
  464. 'orderid'=>$orderid,
  465. 'addtime'=>time()
  466. );
  467. addBalanceRecord($data2);
  468. //减去商品销量
  469. changeShopGoodsSaleNums($order_info['goodsid'],0,$order_info['nums']);
  470. //减去店铺销量
  471. changeShopSaleNums($order_info['shop_uid'],0,$order_info['nums']);
  472. //商品规格库存回增
  473. changeShopGoodsSpecNum($order_info['goodsid'],$order_info['spec_id'],$order_info['nums'],1);
  474. $title="你的商品“".$order_info['goods_name']."”卖家已经同意退款";
  475. //写入订单消息列表
  476. $data3=array(
  477. 'title'=>$title,
  478. 'orderid'=>$orderid,
  479. 'uid'=>$order_info['uid'],
  480. 'addtime'=>time(),
  481. 'type'=>'0'
  482. );
  483. addShopGoodsOrderMessage($data3);
  484. //发送腾讯云IM
  485. txMessageIM($title,$order_info['uid'],'goodsorder_admin');
  486. $refund_history_data=array(
  487. 'orderid'=>$orderid,
  488. 'type'=>2,
  489. 'addtime'=>time(),
  490. 'desc'=>'卖家同意退款'
  491. );
  492. }
  493. //添加退款处理历史记录
  494. setGoodsOrderRefundList($refund_history_data);
  495. $this->success("退款处理完成");
  496. }
  497. }