WxOrders.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use EasyWeChat\Factory;
  6. use think\Exception;
  7. class WxOrders extends Base
  8. {
  9. public $status = [
  10. '未支付',
  11. '已支付',
  12. '已发货',
  13. '已完成',
  14. '已取消',
  15. '已评价',
  16. ];
  17. public $rStatus = [
  18. '退款处理中',
  19. '退款成功',
  20. '退款拒绝',
  21. ];
  22. public function add($cartId,$addressId,$userId,$orgId,$type,$goodsId,$isHasBarrel,$deliveryDate,$deliveryType,$orderRemark,$payType,$num){
  23. $addressInfo=Db::name('shop_address')->where('id',$addressId)->find();
  24. if(!$addressInfo){
  25. $this->error = "未选择地址";
  26. return false;
  27. }
  28. $ids = [];
  29. if($type==0){
  30. if(empty($goodsId) || empty($num)){
  31. $this->error ='参数错误';
  32. }
  33. $ids[] = ['id'=>$goodsId,'num'=>$num];
  34. }else if($type==1){
  35. if(empty($cartId)){
  36. $this->error='参数错误';
  37. }
  38. $gId = Db::name('wx_goods_cart')
  39. ->alias('a')
  40. ->join('wx_goods b','a.goods_id=b.id')
  41. ->where('a.id','in',explode(',',$cartId))
  42. ->where('a.user_id',$userId)
  43. ->where('b.del',0)
  44. ->where('b.org_id',$orgId)
  45. ->group('a.goods_id')
  46. ->select();
  47. foreach ($gId as $k=>$v){
  48. $ids[] = [
  49. 'id'=>$v['goods_id'],
  50. 'num'=>$v['nums'],
  51. ];
  52. }
  53. }
  54. $data =$this->formatOrder($ids);
  55. Db::startTrans();
  56. try {
  57. $params=[
  58. 'order_sn'=>get_unique_id(),
  59. 'org_id'=>$orgId,
  60. 'user_id'=>$userId,
  61. 'amount'=>$data['total'],
  62. 'status'=>0,
  63. 'create_time'=>date('Y-m-d H:i:s'),
  64. 'name'=>$addressInfo['name'],
  65. 'phone'=>$addressInfo['phone'],
  66. 'address'=>$addressInfo['content'],
  67. 'pay_type'=>$payType,
  68. 'order_remark'=>$orderRemark,
  69. 'delivery_date'=>$deliveryDate,
  70. 'delivery_type'=>$deliveryType,
  71. 'cash_price'=>$isHasBarrel==1?$data['cash_price']:0,
  72. 'is_has_barrel'=>$isHasBarrel,
  73. 'remark'=>$isHasBarrel==1?$data['remark']:"",
  74. 'order_type'=>$data['isWater'],
  75. 'create_yyyy'=>date('Y'),
  76. 'create_yyyymm'=>date('Ym'),
  77. 'create_yyyymmdd'=>date('Ymd'),
  78. 'create_yyyymmddhh'=>date('YmdH'),
  79. ];
  80. $addOrder=Db::name('wx_orders')->insertGetId($params);
  81. if(!$addOrder){
  82. exception('操作失败');
  83. }
  84. foreach ($data['list'] as $kay=>$val){
  85. $gdata=[
  86. 'order_id'=>$addOrder,
  87. 'goods_id'=>$val['goods_id'],
  88. 'nums'=>$val['num'],
  89. 'price'=>$val['price'],
  90. ];
  91. $addOrderGoods=Db::name('wx_orders_goods')->insert($gdata);
  92. if(!$addOrderGoods){
  93. exception('操作失败');
  94. }
  95. // //减少库存
  96. // $dec= Db::name('wx_goods')
  97. // ->where('id',$val['goods_id'])
  98. // ->dec('stock', $val['num'])
  99. // ->update();
  100. // if(!$dec){
  101. // exception('操作失败');
  102. // }
  103. }
  104. //删除用户购物车信息
  105. if($type==1){
  106. $delCart=Db::name('wx_goods_cart')
  107. ->where('user_id',$userId)
  108. ->whereIn('id',explode(',',$cartId))->delete();
  109. if(!$delCart){
  110. exception('操作失败');
  111. }
  112. }
  113. Db::commit();
  114. return $addOrder;
  115. } catch (\Exception $e) {
  116. // 回滚事务
  117. $this->error = $e->getMessage();
  118. Db::rollback();
  119. return false;
  120. }
  121. }
  122. public function lists($page,$size,$userId,$orgId,$status){
  123. if($status>0){
  124. if($status==1){
  125. $map[]=['status','=',0];
  126. }elseif ($status==2){
  127. $ids = Db::name('wx_orders_refund')
  128. ->where('org_id',$orgId)
  129. ->where('user_id',$userId)
  130. ->where('del',0)
  131. ->column('order_id');
  132. foreach ($ids as $k=>$v){
  133. $r = $this->checkOrderSend($v);
  134. if($r==2){//全部退款
  135. unset($ids[$k]);
  136. }
  137. }
  138. if(!empty($ids)){
  139. $map[]=['id','not in',$ids];
  140. }
  141. $map[]=['status','=',1];
  142. }elseif ($status==3){
  143. $map[]=['status','=',2];
  144. }elseif ($status==4){
  145. $ids = Db::name('wx_orders_refund')
  146. ->where('org_id',$orgId)
  147. ->where('user_id',$userId)
  148. ->where('del',0)
  149. ->column('order_id');
  150. if(empty($ids)){
  151. $map[]=['id','=',-1];
  152. }else{
  153. $map[]=['id','in',$ids];
  154. }
  155. }
  156. }
  157. $map[]=['user_id','=',$userId];
  158. $map[]=['org_id','=',$orgId];
  159. $map[]=['del','=',0];
  160. $ret=Db::name('wx_orders')
  161. ->field('id,order_sn,status,amount')
  162. ->where($map)
  163. ->order('id','desc')
  164. ->page($page,$size)
  165. ->select();
  166. foreach ($ret as $k=>$v){
  167. $ret[$k]['is_refund'] = $this->haveRefund($v['id']);
  168. $ret[$k]['is_can_refund'] = $this->checkOrderRefund($v['id']);
  169. $orderGoods=Db::name('wx_orders_goods')
  170. ->alias('og')
  171. ->field('og.nums,og.price,og.goods_id,g.title,g.img,g.label')
  172. ->join('wx_goods g','g.id=og.goods_id')
  173. ->where('og.order_id',$v['id'])
  174. ->select();
  175. $nums=0;
  176. foreach ($orderGoods as $kk=>$vv){
  177. $nums+=$vv['nums'];
  178. $orderGoods[$kk]['label_name'] = $vv['label']>0?Db::name('wx_goods_label')
  179. ->where('id',$vv['label'])
  180. ->value('title'):"";
  181. $r = $this->checkGoodsRefund1($v['id'],$vv['goods_id']);
  182. $orderGoods[$kk]['is_refund'] =$r['is'];
  183. $orderGoods[$kk]['refund_status_name'] =$r['refund_status_name'];
  184. }
  185. $ret[$k]['total_num']=$nums;
  186. $ret[$k]['goods_num']=count($orderGoods);
  187. $ret[$k]['order_goods']=$orderGoods;
  188. $r = $this->checkOrderSend($v['id']);
  189. $ret[$k]['refund_status']=$r;
  190. $ret[$k]['refund_name'] = '';
  191. if($r==2){
  192. $ret[$k]['refund_name'] = '(已退款)';
  193. }elseif ($r==1){
  194. $ret[$k]['refund_name'] = '(部分退款)';
  195. }elseif ($r==3){
  196. $ret[$k]['refund_name'] = '(退款中)';
  197. }
  198. }
  199. return $ret?$ret:[];
  200. }
  201. public function details($ordersId){
  202. $ret = Db::name('wx_orders')
  203. ->field('del,del_time,create_yyyy,create_yyyymm,create_yyyymmdd,create_yyyymmddhh',true)
  204. ->where('id',$ordersId)->find();
  205. $ret['pay_time'] = $ret['pay_time']? $ret['pay_time']:"";
  206. $ret['send_time'] = $ret['send_time']? $ret['send_time']:"";
  207. $ret['finish_time'] = $ret['finish_time']? $ret['finish_time']:"";
  208. $ret['cancel_time'] = $ret['cancel_time']? $ret['cancel_time']:"";
  209. $ret['delivery_date'] = $ret['delivery_date']? $ret['delivery_date']:"";
  210. $ret['status_name'] = isset($this->status[$ret['status']])?$this->status[$ret['status']]:'';
  211. $r = $this->checkOrderSend($ordersId);
  212. if($r==2){
  213. $ret['status_name'] = $ret['status_name'].'(已退款)';
  214. }elseif ($r==1){
  215. $ret['status_name'] = $ret['status_name'].'(部分退款)';
  216. }elseif ($r==3){
  217. $ret['status_name'] = $ret['status_name'].'(退款中)';
  218. }
  219. $ret['refund_status']=$r;
  220. $ret['refund_name'] = '';
  221. if($r==2){
  222. $ret['refund_name'] = '(已退款)';
  223. }elseif ($r==1){
  224. $ret['refund_name'] = '(部分退款)';
  225. }elseif ($r==3){
  226. $ret['refund_name'] = '(退款中)';
  227. }
  228. $ret['pay_type_name'] = $ret['pay_type']==1?'微信支付':'';
  229. $ret['delivery_type_name'] = $ret['delivery_type']==1?'商家配送':'';
  230. $ret['order_type_name'] = $ret['order_type']==1?'普通订单':'桶装水订单';
  231. $ret['goods'] = Db::name('wx_orders_goods')
  232. ->alias('og')
  233. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
  234. ->join('wx_goods g', 'g.id=og.goods_id')
  235. ->where('og.order_id', $ret['id'])
  236. ->select();
  237. $totalNum = 0;
  238. $ret['is_refund'] = $this->haveRefund($ordersId);;
  239. $ret['is_can_refund'] = $this->checkOrderRefund($ordersId);
  240. foreach ($ret['goods'] as $k=>$v){
  241. $ret['goods'][$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  242. ->where('id',$v['label'])
  243. ->value('title'):"";
  244. $r = $this->checkGoodsRefund($ordersId,$v['goods_id']);
  245. $ret['goods'][$k]['is_refund'] =$r['is'];
  246. $ret['goods'][$k]['refund_status_name'] =$r['refund_status_name'];
  247. $totalNum+=$v['num'];
  248. }
  249. $ret['comment'] = Db::name('wx_comment')
  250. ->field('create_yyyy,create_yyyymm,create_yyyymmdd',true)
  251. ->where('order_id',$ordersId)
  252. ->find();
  253. $ret['totalNum'] = $totalNum;
  254. $ret['goodsNum'] = count($ret['goods']);
  255. return $ret;
  256. }
  257. public function haveRefund($id){
  258. $refund = Db::name('wx_orders_refund')
  259. ->where('del',0)
  260. ->where('order_id',$id)
  261. ->find();
  262. return $refund?1:0;
  263. }
  264. public function checkOrderRefund($id){
  265. $goodsList = Db::name('wx_orders_goods')
  266. ->alias('og')
  267. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label,o.status')
  268. ->join('wx_goods g', 'g.id=og.goods_id')
  269. ->join('wx_orders o', 'og.order_id=o.id')
  270. ->where('og.order_id', $id)
  271. ->select();
  272. $is = 0;
  273. foreach ($goodsList as $k=>$v){
  274. $refund = Db::name('wx_orders_refund')
  275. ->where('order_id',$id)
  276. ->where('del',0)
  277. ->where('goods_id',$v['goods_id'])
  278. // ->where('status','in',[0,1])
  279. ->find();
  280. if(!$refund && in_array($v['status'],[1,2])){
  281. $is=1;
  282. }
  283. }
  284. return $is;
  285. }
  286. public function checkOrderSend($id){
  287. $goodsList = Db::name('wx_orders_goods')
  288. ->alias('og')
  289. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label,o.status')
  290. ->join('wx_goods g', 'g.id=og.goods_id')
  291. ->join('wx_orders o', 'og.order_id=o.id')
  292. ->where('og.order_id', $id)
  293. ->select();
  294. $is = 0;
  295. $iss= 0;
  296. foreach ($goodsList as $k=>$v){
  297. $refund = Db::name('wx_orders_refund')
  298. ->where('order_id',$id)
  299. ->where('del',0)
  300. ->where('goods_id',$v['goods_id'])
  301. ->where('status','in',[1])
  302. ->find();
  303. if($refund){
  304. $is++;
  305. }
  306. $refund = Db::name('wx_orders_refund')
  307. ->where('order_id',$id)
  308. ->where('del',0)
  309. ->where('goods_id',$v['goods_id'])
  310. ->where('status','in',[0])
  311. ->find();
  312. if($refund){
  313. $iss++;
  314. }
  315. }
  316. $r = 0;//未退款
  317. if($is >0 || $iss >0){
  318. if($is==count($goodsList)){
  319. $r = 2;//全部退款
  320. }elseif ($is < count($goodsList)){
  321. if ($iss >0){
  322. $r = 3;//退款中
  323. }else{
  324. $r = 1;//部分退款
  325. }
  326. }
  327. }
  328. return $r;
  329. }
  330. public function checkGoodsRefund1($orderId,$goodsId){
  331. $is = 0;//不显示
  332. $is_name = '';//不显示
  333. $refund = Db::name('wx_orders_refund')
  334. ->where('order_id',$orderId)
  335. ->where('goods_id',$goodsId)
  336. ->where('del',0)
  337. ->where('status','=',1)
  338. ->find();
  339. if($refund){
  340. $is=2;//显示退款成功
  341. $is_name = '退款成功';
  342. }
  343. $refund = Db::name('wx_orders_refund')
  344. ->where('order_id',$orderId)
  345. ->where('goods_id',$goodsId)
  346. ->where('del',0)
  347. ->where('status','=',2)
  348. ->find();
  349. if($refund){
  350. $is=3;//显示退款拒绝
  351. $is_name = '退款拒绝';
  352. }
  353. $refund = Db::name('wx_orders_refund')
  354. ->where('del',0)
  355. ->where('order_id',$orderId)
  356. ->where('goods_id',$goodsId)
  357. ->where('status','=',0)
  358. ->find();
  359. if($refund){
  360. $is=4;//显示审核处理中
  361. $is_name = '退款处理中';
  362. }
  363. return ['is'=>$is,'refund_status_name'=>$is_name];
  364. }
  365. public function checkGoodsRefund($orderId,$goodsId){
  366. $goodsList = Db::name('wx_orders_goods')
  367. ->alias('og')
  368. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label,o.status')
  369. ->join('wx_goods g', 'g.id=og.goods_id')
  370. ->join('wx_orders o', 'og.order_id=o.id')
  371. ->where('og.order_id', $orderId)
  372. ->where('g.id', $goodsId)
  373. ->find();
  374. $is = 0;//不显示
  375. $is_name = '';//不显示
  376. $refund = Db::name('wx_orders_refund')
  377. ->where('del',0)
  378. ->where('order_id',$orderId)
  379. ->where('goods_id',$goodsId)
  380. // ->where('status','in',[0,1])
  381. ->find();
  382. if(!$refund && in_array($goodsList['status'],[1,2])){
  383. $is=1;//显示申请退款
  384. $is_name = '申请退款';
  385. }
  386. $refund = Db::name('wx_orders_refund')
  387. ->where('del',0)
  388. ->where('order_id',$orderId)
  389. ->where('goods_id',$goodsId)
  390. ->where('status','=',1)
  391. ->find();
  392. if($refund){
  393. $is=2;//显示退款成功
  394. $is_name = '退款成功';
  395. }
  396. $refund = Db::name('wx_orders_refund')
  397. ->where('del',0)
  398. ->where('order_id',$orderId)
  399. ->where('goods_id',$goodsId)
  400. ->where('status','=',2)
  401. ->find();
  402. if($refund){
  403. $is=3;//显示退款拒绝
  404. $is_name = '退款拒绝';
  405. }
  406. $refund = Db::name('wx_orders_refund')
  407. ->where('del',0)
  408. ->where('order_id',$orderId)
  409. ->where('goods_id',$goodsId)
  410. ->where('status','=',0)
  411. ->find();
  412. if($refund){
  413. $is=4;//显示审核处理中
  414. $is_name = '退款处理中';
  415. }
  416. return ['is'=>$is,'refund_status_name'=>$is_name];
  417. }
  418. public function getRefundGoodsNum($goodsId,$orderId){
  419. $refund = Db::name('wx_orders_refund')
  420. ->where('del',0)
  421. ->where('order_id',$orderId)
  422. ->where('goods_id',$goodsId)
  423. // ->where('status','=',3)
  424. ->sum('num');
  425. return $refund?$refund:0;
  426. }
  427. public function accomplish($ordersId){
  428. $data=[
  429. 'status'=>2,
  430. 'finish_time'=>date('Y-m-d H:i:s'),
  431. ];
  432. $ret = Db::name('g_orders')->where('id', $ordersId)->update($data);
  433. return $ret;
  434. }
  435. public function refundOrder($userId){
  436. $id = input('id/d',0);
  437. $data = [
  438. 'money' => input('money/f',0),
  439. 'status' => input('status/d',0),
  440. 'remark' => input('remark','','trim'),
  441. 'deal_user_id'=>$userId,
  442. 'deal_time'=>getTime(),
  443. ];
  444. if($data['money'] <= 0){
  445. $this->error = '金额必须大于0';
  446. return false;
  447. }
  448. $pay = Db::name('wx_orders_refund')
  449. ->where('del',0)
  450. ->where('id',$id)->find();
  451. if(!$pay||$pay['status'] != 0){
  452. $this->error = '申请单不存在';
  453. return false;
  454. }
  455. if(!in_array($data['status'],[1,2])){
  456. $this->error = '参数错误';
  457. return false;
  458. }
  459. $sn = get_unique_id('TPH');
  460. if($data['money'] > $pay['amount']){
  461. $this->error = '退款金额过大';
  462. return false;
  463. }
  464. Db::startTrans();
  465. try{
  466. $order = Db::name('wx_orders_refund')
  467. ->where('del',0)
  468. ->where('id',$pay['id'])
  469. ->lock(true)
  470. ->find();
  471. if(!$order){
  472. \exception('订单不存在');
  473. }
  474. if($data['status']==2){
  475. $data['money'] = 0;
  476. }
  477. $ret = Db::name('wx_orders_refund')
  478. ->where('id',$pay['id'])->update($data);
  479. if(!$ret){
  480. \exception('操作失败');
  481. }
  482. if($data['status']==1){
  483. $orderInfo = Db::name('wx_orders')
  484. ->where('id',$pay['order_id'])
  485. ->find();
  486. $r = Db::name('wx_orders')
  487. ->where('id',$pay['order_id'])
  488. ->setInc('refund_money',$data['money']);
  489. if(!$r){
  490. \exception('修改退款金额失败');
  491. }
  492. $config = config('app.wx_mini_config');
  493. $app = \EasyWeChat\Factory::payment($config);
  494. $ret = $app->refund->byOutTradeNumber($orderInfo['order_sn'], $sn, $orderInfo['amount']*100, $data['money']*100);
  495. if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
  496. trace(json_encode($ret));
  497. \exception('退款申请提交失败');
  498. }
  499. }
  500. Db::commit();
  501. return true;
  502. }catch (\Exception $e){
  503. Db::rollback();
  504. trace($e->getMessage());
  505. $this->error = $e->getMessage();
  506. return false;
  507. }
  508. }
  509. public function formatOrder($ids){
  510. $list = [];
  511. $total = 0;
  512. $isWater = 0;
  513. $cash_price = 0;
  514. $remark = [];
  515. $totalNum = 0;
  516. foreach ($ids as $k=>$v){
  517. $ret=Db::name('wx_goods')
  518. ->field('price,id as goods_id,title,img,label,barrel_id')
  519. ->where('id','=',$v['id'])
  520. ->find();
  521. $ret['label_name'] = $ret['label']>0?Db::name('wx_goods_label')
  522. ->where('id',$ret['label'])
  523. ->value('title'):"";
  524. $ret['num'] = $v['num'];
  525. $total+=$v['num']*$ret['price'];
  526. $totalNum+=$v['num'];
  527. if($ret['barrel_id'] >0){
  528. $isWater = 1;
  529. $wx_barrel = Db::name('wx_barrel')
  530. ->where('id',$ret['barrel_id'])
  531. ->find();
  532. $cash_price+=$wx_barrel['cash_price']*$v['num'];
  533. $remark[] =$ret['title'].'押金:'.$wx_barrel['cash_price']*$v['num'];
  534. }
  535. $list[] = $ret;
  536. }
  537. $data = [
  538. 'list'=>$list,
  539. 'goodsNum'=>count($list),
  540. 'totalNum'=>$totalNum,
  541. 'isWater'=>$isWater,
  542. 'remark'=>$remark?implode(';',$remark):"",
  543. 'cash_price'=>sprintf("%01.2f", $cash_price),
  544. 'total'=>sprintf("%01.2f", $total),
  545. ];
  546. return $data;
  547. }
  548. public function buyBarrel($orgId,$userId,$id,$num){
  549. $info = Db::name('wx_barrel')
  550. ->where('id',$id)
  551. ->where('enable',1)
  552. ->where('del',0)
  553. ->find();
  554. if(empty($info)){
  555. HelpHander::error('空桶不存在');
  556. }
  557. $params = [
  558. 'sn'=>get_unique_id(),
  559. 'user_id'=>$userId,
  560. 'barrel_id'=>$id,
  561. 'org_id'=>$orgId,
  562. 'num'=>$num,
  563. 'amount'=>$num*$info['cash_price'],
  564. 'create_time'=>getTime()
  565. ];
  566. $res = Db::name('wx_cash_pay_log')
  567. ->insertGetId($params);
  568. return $res;
  569. }
  570. public function refundBarrel($orgId,$userId,$id,$num){
  571. $cash = Db::name('wx_cash')
  572. ->where('id',$id)
  573. ->find();
  574. if($cash['num'] <$num){
  575. HelpHander::error('超出退款数量');
  576. }
  577. Db::startTrans();
  578. try{
  579. $params = [
  580. 'sn'=>get_unique_id(),
  581. 'user_id'=>$userId,
  582. 'barrel_id'=>$cash['barrel_id'],
  583. 'org_id'=>$orgId,
  584. 'num'=>$num,
  585. 'amount'=>$num*$cash['price'],
  586. 'create_time'=>getTime(),
  587. 'cash_id'=>$id
  588. ];
  589. $res = Db::name('wx_cash')
  590. ->where('id',$cash['id'])
  591. ->setDec('num',$num);
  592. Db::name('wx_cash')
  593. ->where('id',$cash['id'])
  594. ->setDec('amount',$num*$cash['price']);
  595. if(!$res){
  596. \exception('操作失败1');
  597. }
  598. $res = Db::name('wx_cash_refund')
  599. ->insertGetId($params);
  600. Db::commit();
  601. return $res;
  602. }catch (Exception $e){
  603. Db::rollback();
  604. HelpHander::error($e->getMessage());
  605. }
  606. }
  607. public function checkoutApplyRefund($userId,$orgId,$type,$orderId,$goodsId){
  608. if(empty($orderId)){
  609. $this->error='参数错误';
  610. return false;
  611. }
  612. if($type==0 && empty($goodsId)){
  613. $this->error='参数错误';
  614. return false;
  615. }
  616. if($type==0){
  617. $goodsList = Db::name('wx_orders_goods')
  618. ->alias('og')
  619. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
  620. ->join('wx_goods g', 'g.id=og.goods_id')
  621. ->where('og.order_id', $orderId)
  622. ->where('og.goods_id', $goodsId)
  623. ->select();
  624. }else{
  625. $goodsList = Db::name('wx_orders_goods')
  626. ->alias('og')
  627. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
  628. ->join('wx_goods g', 'g.id=og.goods_id')
  629. ->where('og.order_id', $orderId)
  630. ->select();
  631. }
  632. foreach ($goodsList as $k=>$v){
  633. //$rNum = $this->getRefundGoodsNum($v['goods_id'],$orderId);
  634. //$goodsList[$k]['refund_num'] = $v['num']-$rNum;
  635. // $goodsList[$k]['refund_num'] = $v['num'];
  636. $goodsList[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  637. ->where('id',$v['label'])
  638. ->value('title'):"";
  639. $flag= Db::name('wx_orders_refund')
  640. ->where('del',0)
  641. ->where('goods_id',$v['goods_id'])
  642. ->where('order_id',$orderId)
  643. ->find();
  644. if($flag){
  645. unset($goodsList[$k]);
  646. }
  647. }
  648. return $goodsList;
  649. }
  650. public function checkoutApplyRefund1($orderId,$goodsId){
  651. if(empty($orderId)){
  652. $this->error='参数错误';
  653. return false;
  654. }
  655. if(empty($goodsId)){
  656. $this->error='参数错误';
  657. return false;
  658. }
  659. $goodsList = Db::name('wx_orders_goods')
  660. ->alias('og')
  661. ->field('og.nums as num,og.price,og.goods_id,g.title,g.img,g.label')
  662. ->join('wx_goods g', 'g.id=og.goods_id')
  663. ->where('og.order_id', $orderId)
  664. ->where('og.goods_id', 'in',explode(',',$goodsId))
  665. ->select();
  666. foreach ($goodsList as $k=>$v){
  667. //$rNum = $this->getRefundGoodsNum($v['goods_id'],$orderId);
  668. //$goodsList[$k]['refund_num'] = $v['num']-$rNum;
  669. // $goodsList[$k]['refund_num'] = $v['num'];
  670. $goodsList[$k]['label_name'] = $v['label']>0?Db::name('wx_goods_label')
  671. ->where('id',$v['label'])
  672. ->value('title'):"";
  673. $flag= Db::name('wx_orders_refund')
  674. ->where('del',0)
  675. ->where('goods_id',$v['goods_id'])
  676. ->where('order_id',$orderId)
  677. ->find();
  678. if($flag){
  679. unset($goodsList[$k]);
  680. }
  681. }
  682. return $goodsList;
  683. }
  684. public function addApplyRefund($userId,$orgId,$goodsJson,$orderId,$remark){
  685. if(empty($orderId)){
  686. $this->error='参数错误3';
  687. return false;
  688. }
  689. if(empty($goodsJson)){
  690. $this->error='参数错误2';
  691. return false;
  692. }
  693. $goodsList = json_decode($goodsJson,'true');
  694. if(empty($goodsList)){
  695. $this->error='参数错误1';
  696. return false;
  697. }
  698. if(!$this->checkOrderRefund($orderId)){
  699. $this->error='当前订单不能申请退款';
  700. return false;
  701. }
  702. foreach ($goodsList as $k=>$v){
  703. $flag= Db::name('wx_orders_refund')
  704. ->where('del',0)
  705. ->where('goods_id',$v['id'])
  706. ->where('order_id',$orderId)
  707. ->find();
  708. if(!$flag){
  709. $wx_orders_goods = Db::name('wx_orders_goods')
  710. ->where('order_id',$orderId)
  711. ->where('goods_id',$v['id'])
  712. ->find();
  713. $goodsInfo = Db::name('wx_goods')
  714. ->where('id',$v['id'])
  715. ->find();
  716. if(empty($goodsInfo)){
  717. $this->error='商品不存在';
  718. return false;
  719. }
  720. if($v['num'] >$wx_orders_goods['nums']){
  721. $this->error=$goodsInfo['title'].'退款数量超过购买数量';
  722. return false;
  723. }
  724. $a[] = [
  725. 'sn'=>get_unique_id(),
  726. 'order_id'=>$orderId,
  727. 'goods_id'=>$v['id'],
  728. 'amount'=>$v['num']*$wx_orders_goods['price'],
  729. 'num'=>$v['num'],
  730. 'reason'=>$remark,
  731. 'user_id'=>$userId,
  732. 'org_id'=>$orgId,
  733. 'create_time'=>getTime(),
  734. ];
  735. }
  736. }
  737. if(empty($a)){
  738. $this->error = '没有符合退款条件的商品';
  739. return false;
  740. }
  741. $res =Db::name('wx_orders_refund')
  742. ->insertAll($a);
  743. if(!$res){
  744. $this->error = '操作失败';
  745. return false;
  746. }
  747. $data = [
  748. 'orderId'=>$orderId,
  749. 'goodsId'=>implode(',',array_column($a,'goods_id')),
  750. ];
  751. return $data;
  752. }
  753. public function orderRefundDetail($orgId,$orderId,$goodsId){
  754. $map = [];
  755. if($goodsId!=''){
  756. $map[] = ['goods_id','in',explode(',',$goodsId)];
  757. }
  758. $list = Db::name('wx_orders_refund')
  759. ->where('del',0)
  760. ->field('order_id,goods_id,status,create_time,num')
  761. ->where('order_id',$orderId)
  762. ->where($map)
  763. ->where('org_id',$orgId)
  764. ->select();
  765. $sn = Db::name('wx_orders')
  766. ->where('id',$orderId)
  767. ->value('order_sn');
  768. foreach ($list as $k=>&$v){
  769. $goodsInfo = Db::name('wx_orders_goods')
  770. ->alias('og')
  771. ->field('og.price,g.title,g.img')
  772. ->join('wx_goods g', 'g.id=og.goods_id')
  773. ->where('og.order_id', $orderId)
  774. ->where('g.id', $v['goods_id'])
  775. ->find();
  776. $v['price'] = $goodsInfo['price'];
  777. $v['title'] = $goodsInfo['title'];
  778. $v['img'] = $goodsInfo['img'];
  779. $v['statusName'] = $this->rStatus[$v['status']];
  780. }
  781. return ['list'=>$list,'sn'=>$sn];
  782. }
  783. public function goodsRefundDetail($orgId,$orderId,$goodsId){
  784. $v = Db::name('wx_orders_refund')
  785. ->field('deal_user_id,user_id,order_id,goods_id,status,create_time,num,amount,reason,deal_time,money,remark')
  786. ->where('order_id',$orderId)
  787. ->where('goods_id',$goodsId)
  788. ->where('del',0)
  789. ->where('org_id',$orgId)
  790. ->find();
  791. $goodsInfo = Db::name('wx_orders_goods')
  792. ->alias('og')
  793. ->field('og.price,g.title,g.img')
  794. ->join('wx_goods g', 'g.id=og.goods_id')
  795. ->where('og.order_id', $orderId)
  796. ->where('g.id', $goodsId)
  797. ->find();
  798. $v['order_sn'] = Db::name('wx_orders')
  799. ->where('id',$orderId)
  800. ->value('order_sn');
  801. $v['user_name'] = Db::name('wxuser')
  802. ->where('id',$v['user_id'])
  803. ->value('nickname');
  804. $v['deal_user_name'] = Db::name('user')
  805. ->where('id',$v['deal_user_id'])
  806. ->value('real_name');
  807. $v['goods'] = [
  808. 'price'=>$goodsInfo['price'],
  809. 'title'=>$goodsInfo['title'],
  810. 'img'=>$goodsInfo['img'],
  811. 'num'=>$v['num'],
  812. ];
  813. $v['statusName'] = $this->rStatus[$v['status']];
  814. $v['deal_time'] = $v['deal_time']? $v['deal_time']:'';
  815. unset($v['num']);
  816. return $v;
  817. }
  818. public function orderRefund($userId,$orgId){
  819. $params = input('num',[]);
  820. $orderId = input('id/d',0);
  821. $reason = input('reason','');
  822. if(empty($orderId)){
  823. $this->error='参数错误3';
  824. return false;
  825. }
  826. if(empty($params)){
  827. $this->error='参数错误2';
  828. return false;
  829. }
  830. if(!$this->checkOrderRefund($orderId)){
  831. $this->error='当前订单不能申请退款';
  832. return false;
  833. }
  834. $goodsList = [];
  835. foreach ($params as $k=>$v){
  836. $goodsList[] = [
  837. 'id'=>$k,
  838. 'num'=>$v,
  839. ];
  840. }
  841. if(empty($goodsList)){
  842. $this->error='参数错误1';
  843. return false;
  844. }
  845. $total = 0;
  846. foreach ($goodsList as $k=>$v){
  847. $flag= Db::name('wx_orders_refund')
  848. ->where('del',0)
  849. ->where('goods_id',$v['id'])
  850. ->where('order_id',$orderId)
  851. ->find();
  852. if(!$flag){
  853. $wx_orders_goods = Db::name('wx_orders_goods')
  854. ->where('order_id',$orderId)
  855. ->where('goods_id',$v['id'])
  856. ->find();
  857. $goodsInfo = Db::name('wx_goods')
  858. ->where('id',$v['id'])
  859. ->find();
  860. if(empty($goodsInfo)){
  861. $this->error='商品不存在';
  862. return false;
  863. }
  864. if($v['num'] >$wx_orders_goods['nums']){
  865. $this->error=$goodsInfo['title'].'退款数量超过购买数量';
  866. return false;
  867. }
  868. $m = $v['num']*$wx_orders_goods['price'];
  869. $a[] = [
  870. 'sn'=>get_unique_id(),
  871. 'order_id'=>$orderId,
  872. 'goods_id'=>$v['id'],
  873. 'amount'=>$m,
  874. 'num'=>$v['num'],
  875. 'reason'=>$reason,
  876. 'user_id'=>$userId,
  877. 'org_id'=>$orgId,
  878. 'status'=>1,
  879. 'money'=>$m,
  880. 'create_time'=>getTime(),
  881. ];
  882. $total+=$m;
  883. }
  884. }
  885. if(empty($a)){
  886. $this->error = '没有符合退款条件的商品';
  887. return false;
  888. }
  889. Db::startTrans();
  890. try{
  891. $res =Db::name('wx_orders_refund')
  892. ->insertAll($a);
  893. if(!$res){
  894. \exception( '操作失败');
  895. }
  896. $orderInfo = Db::name('wx_orders')
  897. ->where('id',$orderId)
  898. ->find();
  899. $r = Db::name('wx_orders')
  900. ->where('id',$orderId)
  901. ->setInc('refund_money',$total);
  902. if(!$r){
  903. \exception('修改退款金额失败');
  904. }
  905. $sn = get_unique_id('TPH');
  906. $config = config('app.wx_mini_config');
  907. $app = \EasyWeChat\Factory::payment($config);
  908. $ret = $app->refund->byOutTradeNumber($orderInfo['order_sn'], $sn, $orderInfo['amount']*100, $total*100);
  909. if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
  910. trace(json_encode($ret));
  911. \exception('退款申请提交失败');
  912. }
  913. Db::commit();
  914. return true;
  915. }catch (Exception $e){
  916. Db::rollback();
  917. $this->error = $e->getMessage();
  918. return false;
  919. }
  920. }
  921. }