Refund.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use app\api\model\card\UserRiceCard;
  14. use app\api\model\card\UserRiceCardConsume;
  15. use app\api\model\fullsend\FullSendActivity;
  16. use app\api\model\member\GoldRice;
  17. use app\api\model\OrderRefund as OrderRefundModel;
  18. use app\api\model\user\CommissionsDetail;
  19. use app\common\enum\order\DeliveryStatus as DeliveryStatusEnum;
  20. use app\common\enum\order\ReceiptStatus as ReceiptStatusEnum;
  21. use app\common\exception\BaseException;
  22. use app\common\model\OrderGoods as OrderGoodsModel;
  23. use app\api\service\User as UserService;
  24. use app\common\model\Order as OrderModel;
  25. use app\common\service\Order as OrderService;
  26. use think\Db;
  27. use app\common\model\order\RefundHis;
  28. use app\common\service\order\Refund as RefundService;
  29. use app\common\library\helper;
  30. use app\common\model\Express as ExpressModel;
  31. use app\api\model\card\UserRiceCard as UserRiceCardModel;
  32. use app\common\service\goods\source\Factory as FactoryStock;
  33. use app\common\enum\order\OrderSource as OrderSourceEnum;
  34. use app\api\model\za\ZaActivityRelation;
  35. /**
  36. * 订单售后服务
  37. * Class service
  38. * @package app\api\controller\user\order
  39. */
  40. class Refund extends Controller
  41. {
  42. /**
  43. * 售后单列表
  44. * @param int $state
  45. * @return array|\think\response\Json
  46. * @throws BaseException
  47. * @throws \think\db\exception\DbException
  48. */
  49. public function list(int $state = -1)
  50. {
  51. $model = new OrderRefundModel;
  52. $list = $model->getList($state);
  53. return $this->renderSuccess(compact('list'));
  54. }
  55. /**
  56. * 订单商品详情
  57. * @param int $orderGoodsId 订单商品ID
  58. * @return array|\think\response\Json
  59. * @throws BaseException
  60. */
  61. public function goods(int $orderGoodsId)
  62. {
  63. $model = new OrderRefundModel;
  64. $goods = $model->getRefundGoods($orderGoodsId);
  65. return $this->renderSuccess(compact('goods'));
  66. }
  67. /**
  68. * 申请售后
  69. * @param $order_goods_id
  70. * @return array
  71. * @throws \think\exception\DbException
  72. * @throws \Exception
  73. */
  74. public function apply($order_goods_id)
  75. {
  76. // 订单商品详情
  77. $goods = OrderGoodsModel::get(['order_goods_id'=>$order_goods_id],['refund','orderM']);
  78. if (isset($goods['refund']) && !empty($goods['refund'])) {
  79. return $this->renderError('当前商品已申请售后');
  80. }
  81. unset($goods['image']);
  82. // unset($goods['order_m']);
  83. unset($goods['content']);
  84. $order['delivery_status'] = $goods['order_m']['delivery_status'];
  85. return $this->renderSuccess(compact('order'));
  86. }
  87. //判断是否需要添加运费
  88. private function isExpressPrice($order,$order_goods_id){
  89. if($order['express_price']<=0){//没有运费
  90. return false;
  91. }
  92. //以下订单有运费
  93. $order_goods_ids = OrderGoodsModel::where('order_id',$order['order_id'])->where('order_goods_id','<>',$order_goods_id)->column('order_goods_id');
  94. if(empty($order_goods_ids)){ //空数组,该订单只有一种商品,得加上运费
  95. return true;
  96. }else{
  97. $count = OrderRefundModel::where(['order_id'=>$order['order_id'],'is_freight'=>1,'order_goods_id'=>['in',$order_goods_ids]])->whereNotIn('status',[5,6,7])->count();//是否有添加了运费的进行中的售后单
  98. if($count<1) {
  99. $OrderRefund = OrderRefundModel::with('orderGoods')->where(['order_id' => $order['order_id'], 'order_goods_id' => ['in', $order_goods_ids]])->whereNotIn('status', [5, 6, 7])->select();//未撤销的商品
  100. if($OrderRefund){
  101. foreach ($OrderRefund as $refund){
  102. if($refund['type']['value']==10 && $refund['goods_num']<$refund['order_goods']['total_num']){
  103. return false;
  104. }
  105. }
  106. if (count($OrderRefund) == count($order_goods_ids)) { //其他商品都在申请中了,最后一款商品需要加上运费
  107. return true;
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. }
  114. //提交售后
  115. public function applySubmit($type)
  116. {
  117. if($type==10){ //退款退货
  118. //退货退款的逻辑
  119. return $this->refundMoneyAndGoods($type);
  120. }
  121. if($type==20){ //不退货退款的逻辑
  122. return $this->refundMoneyNotGoods($type);
  123. }
  124. if($type==30){ //退款
  125. return $this->refundMoney($type);
  126. }
  127. }
  128. //修改退货退款申请
  129. public function applyEdit($type){
  130. if($type==10){ //退款退货
  131. //退货退款的逻辑
  132. return $this->editRefundMoneyAndGoods($type);
  133. }
  134. if($type==30){
  135. return $this->editRefundMoney($type);
  136. }
  137. }
  138. //已付款未发货退款修改
  139. public function editRefundMoney($type){
  140. //退款的逻辑
  141. $user = UserService::getCurrentLoginUser(true);
  142. $params = $this->baseValidate([
  143. "order_refund_id"=>"require",//售后ID
  144. "refund_money"=>"require|float|gt:0",//退款金额
  145. "goods_type"=>"require|in:1,2",//货物状态 货物到货状态(1未收到货 2已收到货)
  146. "apply_desc"=>"require",//申请原因
  147. ],[
  148. 'refund_money.require'=>'退款金额不能为空',
  149. 'refund_money.float'=>'退款金额非法',
  150. 'refund_money.gt'=>'退款金额不能小于等于0',
  151. 'goods_type.require'=>'货物状态不能为空',
  152. 'goods_type.in'=>'货物状态非法',
  153. 'apply_desc.require'=>'申请原因不能为空',
  154. ]);
  155. $user_id = $user->user_id;
  156. $order_refund_id = $params['order_refund_id'];
  157. $refund_res = OrderRefundModel::where(['order_refund_id'=>$order_refund_id])->find();
  158. if($refund_res['user_id']!=$user->user_id){
  159. return $this->renderError('非法售后单');
  160. }
  161. $order_goods_id = $refund_res['order_goods_id'];
  162. if (empty($order_goods_id)) { // 整单退款
  163. return $this->renderError('整单退款不能修改退款申请');
  164. }
  165. $order_good = OrderGoodsModel::find($order_goods_id);
  166. if(!$order_good){
  167. return $this->renderError('非法商品');
  168. }
  169. // if($order_good['total_num']<$params['goods_num']){
  170. // return $this->renderError('当前商品数量不能超过'.$order_good['total_num'].'个');
  171. // }
  172. //判断价格
  173. $refundMoney = helper::bcsub($order_good['total_pay_price'] + $order_good['rice_card_money'], 0, 2); // 退款金额 = 实付金额 + 现金卡抵扣金额
  174. if($refundMoney < $params['refund_money']){ //比较价格
  175. return $this->renderError('退款金额不能超过'.$refundMoney.'元');
  176. }
  177. if($refund_res['audit_status']==10){
  178. return $this->renderError("专员同意不能修改");
  179. }
  180. if($refund_res['audit_status_zg']==10){
  181. return $this->renderError("主管同意不能修改");
  182. }
  183. if($refund_res['finance_refund']==10){
  184. return $this->renderError("财务已退款 不能修改");
  185. }
  186. if($refund_res['status']==20){
  187. return $this->renderError("售后已完成不能修改");
  188. }
  189. if($refund_res['status']==30){
  190. return $this->renderError("用户撤消申请不能修改");
  191. }
  192. if($refund_res['status']==40){
  193. return $this->renderError("用户超时未退货,系统自动关闭售后单");
  194. }
  195. $param['refund_money'] = $params['refund_money'];
  196. $param['pay_money'] = $order_good['total_pay_price'];
  197. $param['rice_card_money'] = $order_good['rice_card_money'];
  198. $param['rice_card_id'] = $order_good['rice_card_id'];
  199. $param['goods_type'] = $params['goods_type'];
  200. $param['apply_desc'] = $params['apply_desc'];
  201. $param['status'] = 0;
  202. $param['audit_status'] = 0;
  203. $param['audit_status_zg'] =0;
  204. $param['refuse_desc_zg'] = '';
  205. $param['refuse_desc'] = '';
  206. // $param['images'] = $params['images']??'';
  207. $his_type = 1;
  208. $name = $user->nick_name??'';
  209. $json_str = json_encode(["买家已申请退款"]);
  210. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,[],'买家','提交退款申请');
  211. $refund_res = OrderRefundModel::where('order_refund_id',$order_refund_id)->update($param);
  212. $refund['is_sys_refund'] = 1;
  213. $refund['type'] = $type;
  214. $refund['refund_money'] = $params['refund_money'];
  215. $refund['goods_type'] = $params['goods_type'];
  216. $refund['apply_desc'] = $params['apply_desc'];
  217. $refund['create_time'] = Date("Y-m-d H:i:s",time());
  218. $refund['order_refund_id'] = $order_refund_id;
  219. return $this->renderSuccess(compact('refund'));
  220. }
  221. //修改申请
  222. public function editRefundMoneyAndGoods($type){
  223. $user = UserService::getCurrentLoginUser(true);
  224. $params = $this->baseValidate([
  225. "order_refund_id"=>"require",//售后ID
  226. "goods_num"=>"require|gt:0",//商品数量
  227. "refund_money"=>"require|float|gt:0",//退款金额
  228. "apply_desc"=>"require",//申请原因
  229. "apply_detail_desc"=>"max:150", //详细原因
  230. // "images"=>"array",//用户上传凭证图片(多图)
  231. ],[
  232. 'order_refund_id.require'=>'没有售后ID',
  233. 'goods_num.require'=>'商品数量不能为空',
  234. 'goods_num.gt'=>'商品数量不能为空',
  235. 'refund_money.require'=>'退款金额不能为空',
  236. 'refund_money.float'=>'退款金额非法',
  237. 'refund_money.gt'=>'退款金额不能小于等于0',
  238. 'apply_desc.require'=>'申请原因不能为空',
  239. 'apply_detail_desc.max'=>'详细原因不能超过150字',
  240. // 'images.array'=>'用户上传的凭证格式不对',
  241. ]);
  242. $order_refund_id = $params['order_refund_id'];
  243. $refund_res = OrderRefundModel::where(['order_refund_id'=>$order_refund_id])->find();
  244. if($refund_res['user_id']!=$user->user_id){
  245. return $this->renderError('非法售后单');
  246. }
  247. if($refund_res['type']!=10){
  248. return $this->renderError('该售后单不允许修改');
  249. }
  250. $order_goods_id = $refund_res['order_goods_id'];
  251. $order_good = OrderGoodsModel::find($order_goods_id);
  252. if(!$order_good){
  253. return $this->renderError('非法商品');
  254. }
  255. if($order_good['total_num']<$params['goods_num']){
  256. return $this->renderError('当前商品数量不能超过'.$order_good['total_num'].'个');
  257. }
  258. //判断价格
  259. $refundMoney = helper::bcsub($order_good['total_pay_price'] + $order_good['rice_card_money'], 0, 2); // 退款金额 = 实付金额 + 现金卡抵扣金额
  260. if($refundMoney < $params['refund_money']){ //比较价格
  261. return $this->renderError('退款金额不能超过'.$refundMoney.'元');
  262. }
  263. if($refund_res['delivery_type']==10){
  264. if($refund_res['audit_status']==10&&$refund_res['audit_status_zg']==0){
  265. return $this->renderError("专员同意不能修改");
  266. }
  267. if($refund_res['audit_status_zg']==10){
  268. return $this->renderError("主管同意不能修改");
  269. }
  270. }
  271. if($refund_res['delivery_type']==20){
  272. if($refund_res['audit_status']==10){
  273. return $this->renderError("门店同意不能修改");
  274. }
  275. }
  276. if($refund_res['finance_refund']==10){
  277. return $this->renderError("财务已退款 不能修改");
  278. }
  279. if($refund_res['status']==20){
  280. return $this->renderError("售后已完成不能修改");
  281. }
  282. if($refund_res['status']==30){
  283. return $this->renderError("用户撤消申请不能修改");
  284. }
  285. if($refund_res['status']==40){
  286. return $this->renderError("用户超时未退货,系统自动关闭售后单");
  287. }
  288. //商品订单
  289. $total_pay_price = $order_good['total_pay_price'];
  290. $rice_card_money = $order_good['rice_card_money'];
  291. // 退的数量少于 订单的商品总数 即存在部份退
  292. if($params['goods_num']<$order_good['total_num']){
  293. // $one_pay_price = helper::bcsub($total_pay_price/$order_good['total_num'],0,2);
  294. // $one_rice_card_money = helper::bcsub(($rice_card_money/$order_good['total_num']),0,2);
  295. // $total_pay_price = helper::bcsub($one_pay_price*$params['goods_num'],0,2);
  296. // $rice_card_money = helper::bcsub($one_rice_card_money*$params['goods_num'],0,2);
  297. $price = helper::bcsub((($total_pay_price+$rice_card_money)/
  298. $order_good['total_num'])*$params['goods_num'],0,2);
  299. if($price<$params['refund_money']){
  300. return $this->renderError("退款金额不能超过商品实付价");
  301. }
  302. //申请的退款低于实际支付,按比例退
  303. if($total_pay_price+$rice_card_money>$params['refund_money']){
  304. $all_pay = $total_pay_price+$rice_card_money;
  305. $total_pay_price = helper::bcsub(($params['refund_money']/$all_pay)*$total_pay_price,0,2);
  306. $rice_card_money = helper::bcsub(($params['refund_money']/$all_pay)*$rice_card_money,0,2);
  307. }
  308. }else{
  309. //全部数量都退了,看是不是低于实际支付
  310. if($total_pay_price+$rice_card_money>$params['refund_money']){
  311. $all_pay = $total_pay_price+$rice_card_money;
  312. $total_pay_price = helper::bcsub(($params['refund_money']/$all_pay)*$total_pay_price,0,2);
  313. $rice_card_money = helper::bcsub(($params['refund_money']/$all_pay)*$rice_card_money,0,2);
  314. }
  315. }
  316. $user_id = $user->user_id;
  317. $param['type'] = $type;
  318. $param['refund_money'] = $params['refund_money'];
  319. $param['pay_money'] = $total_pay_price;
  320. $param['rice_card_money'] = $rice_card_money;
  321. $param['rice_card_id'] = $order_good['rice_card_id'];
  322. $param['goods_num'] = $params['goods_num'];
  323. $param['apply_desc'] = $params['apply_desc'];
  324. $param['apply_detail_desc'] = $params['apply_detail_desc']??'';
  325. // $param['images'] = implode(",",$params['images']??[]);
  326. $param['images'] = $params['images']??'';
  327. $param['user_id'] = $user_id;
  328. // $param['provider_id'] = $order['provider_ids'];
  329. // $param['provider_name'] = $order_good['provider_name']??'';
  330. // $param['goods_type'] = 2;//默认写收收到货
  331. // $param['goods_images'] = [$order_good['good_image']];
  332. $param['status'] = 0; //等待商家审核中
  333. $param['audit_status'] = 0;
  334. $param['audit_status_zg'] =0;
  335. $param['refuse_desc_zg'] = '';
  336. $param['refuse_desc'] = '';
  337. OrderRefundModel::where('order_refund_id',
  338. $order_refund_id)->update($param);
  339. //写入协商历史 不一定要写 先关了先
  340. if($refund_res['delivery_type']==20){
  341. $his_type = 1;
  342. $name = $user->nick_name??'';
  343. $json_str = json_encode(["退款类型:退货退款","退款金额:¥".$param['refund_money'],"申请原因:".$param['apply_desc'],"退款说明:".$param['apply_detail_desc']]);
  344. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,$param['images'],'买家','提交退款申请');
  345. }
  346. if($refund_res['delivery_type']==10){
  347. //写入协商历史
  348. $his_type = 1;
  349. $name = $user->nick_name??'';
  350. $json_str = json_encode(["买家已申请退款"]);
  351. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,[],'买家','提交退款申请');
  352. }
  353. $refund['type'] = $type;
  354. $refund['refund_money'] = $param['refund_money'];
  355. $refund['goods_type'] = $refund_res->goods_type;
  356. $refund['goods_num'] = $param['goods_num'];
  357. $refund['apply_desc'] = $param['apply_desc'];
  358. // $refund['refund_succ_time'] = Date("Y-m-d H:i:s",$param['refund_succ_time']);
  359. $refund['create_time'] = Date("Y-m-d H:i:s",time());
  360. $refund['order_refund_id'] = $order_refund_id;
  361. return $this->renderSuccess(compact('refund'));
  362. }
  363. /**
  364. * 已付款未发货单个或整单退款
  365. */
  366. public function refundMoney($type){
  367. //退款的逻辑
  368. $user = UserService::getCurrentLoginUser(true);
  369. $params = $this->baseValidate([
  370. "order_id"=>"require",//订单ID
  371. "refund_money"=>"require|float|gt:0",//退款金额(包含运费)
  372. // "goods_type"=>"require|in:1,2",//货物状态 货物到货状态(1未收到货 2已收到货)
  373. "apply_desc"=>"require",//申请原因
  374. ],[
  375. 'order_id.require'=>'没有选择订单',
  376. 'refund_money.require'=>'退款金额不能为空',
  377. 'refund_money.float'=>'退款金额非法',
  378. 'refund_money.gt'=>'退款金额不能小于等于0',
  379. // 'goods_type.require'=>'货物状态不能为空',
  380. // 'goods_type.in'=>'货物状态非法',
  381. 'apply_desc.require'=>'申请原因不能为空',
  382. ]);
  383. $user_id = $user->user_id;
  384. $order = OrderModel::get(['order_id'=>$params['order_id'],'user_id'=>$user_id],['goods']);
  385. if(!$order){
  386. return $this->renderError('非法订单');
  387. }
  388. $params['goods_type'] = $params['goods_type']??1;
  389. //只有普通订单检查货物状态
  390. if($order['delivery_type']==10){
  391. if(!in_array($params['goods_type'],[1,2])){
  392. return $this->renderError('货物状态非法');
  393. }
  394. }
  395. $params['order_goods_id'] = $params['order_goods_id'] ?? 0;
  396. if($order['delivery_type']==20){
  397. if($params['order_goods_id']>0){
  398. return $this->renderError("门店自提单未核消不允许退单个商品");
  399. }
  400. }
  401. if (empty($params['order_goods_id'])) {
  402. // 整单退款
  403. return $this->refundAll($type, $order, $params, $user);
  404. } else {
  405. // 单个退款
  406. return $this->refundSingle($type, $order, $params, $user);
  407. }
  408. }
  409. /**
  410. * 单个退款
  411. */
  412. private function refundSingle($type, $order, $params, $user)
  413. {
  414. $orderGoods = OrderGoodsModel::get(['order_goods_id'=>$params['order_goods_id']]);
  415. // return $order;
  416. if($order['pay_status']==10){//未付款
  417. return $this->renderError('未付款不能申请退款');
  418. }
  419. if($orderGoods['delivery_status'] == DeliveryStatusEnum::DELIVERED){ //已发货
  420. return $this->renderError('已发货不能申请退款');
  421. }
  422. $refundMoney = helper::bcsub($orderGoods['total_pay_price'] + $orderGoods['rice_card_money'], 0, 2); // 退款金额 = 实付金额 + 现金卡抵扣金额
  423. if($refundMoney != $params['refund_money']){ //比较价格
  424. return $this->renderError('退款金额必须等于'.$refundMoney.'元');
  425. }
  426. $refund_time = $orderGoods['refund_time'];
  427. if($refund_time>0&&time()>$refund_time){
  428. return $this->renderError('当前售后时间已结束');
  429. }
  430. $refund = OrderRefundModel::where(['order_goods_id'=>$params['order_goods_id']])->where('status','in',[0,10,20])->find();
  431. if($refund){
  432. return $this->renderError('当前商品已申请售后');
  433. }
  434. $goods_images = [];
  435. foreach ($order['goods'] as $arr){ //获取图片url
  436. $goods_images[] = $arr['good_image'];
  437. }
  438. $param['order_id'] = $params['order_id'];
  439. $param['refund_money'] = $params['refund_money'];
  440. $param['pay_money'] = $orderGoods['total_pay_price'];
  441. $param['gold_rice_amount'] = $orderGoods['gold_rice_amount'];
  442. $param['gold_rice_amount_output'] = $orderGoods['gold_rice_amount_input'];
  443. $param['rice_card_money'] = $orderGoods['rice_card_money'];
  444. $param['rice_card_id'] = $orderGoods['rice_card_id'];
  445. $param['goods_type'] = $params['goods_type'];
  446. $param['apply_desc'] = $params['apply_desc'];
  447. $param['type'] = $type;// 30 未发货退款
  448. $param['refund_number'] = OrderService::createOrderNoPrefix('TKSQ'); //申请单编号
  449. // $param['out_refund_no'] = OrderService::createOrderNoPrefix('TK');//生成退款编号
  450. $param['user_id'] = $user['user_id']??0;
  451. $param['goods_images'] = $goods_images;
  452. $param['order_goods_id'] = $params['order_goods_id'];
  453. $param['store_id'] = 10001;
  454. $param['is_sys_refund'] = 1; //系统退款
  455. $param['goods_num'] = $orderGoods['total_num'];
  456. $param['shop_id'] = $order['shop_id'];
  457. //订单商品是否已导出,未导出就直接退款
  458. if($orderGoods['is_export']==0){
  459. $param['finance_refund'] =10;// 10已退款
  460. $param['out_refund_time'] = time(); //退款时间
  461. $param['refund_succ_time'] = time(); //退款成功时间
  462. $param['refund_close_time'] = time(); //退款关闭时间
  463. $param['status'] = 20;//售后单直接完成
  464. if ($param['pay_money'] > 0) {
  465. //直接退款逻辑处理 直接退款
  466. $RefundService = new RefundService();
  467. $refund_ret = $RefundService->execute($order,$param['refund_number'], $param['pay_money']);
  468. if ($refund_ret['status'] == 200) {
  469. $param['transaction_id'] = $refund_ret['data']['transaction_id']; // 微信退款交易号
  470. }
  471. }
  472. //订单商品修改成全额退款状态
  473. OrderGoodsModel::updateBase(['has_refund_full' => 1], ['order_goods_id'=>$params['order_goods_id']]);
  474. //全部退款变更不结算zq11-12
  475. log_record('is refund::#########');
  476. //1.3.92后分佣无订单商品了
  477. //CommissionsDetail::updateDetails($params['order_goods_id']);
  478. $riceCard = UserRiceCardModel::getValidRiceCardDetail($order['rice_card_id']);
  479. if ($orderGoods['rice_card_id']) { // 订单商品有使用现金卡
  480. // 验证余额
  481. if ($orderGoods['rice_card_money'] + $riceCard['balance'] > $riceCard['face_value']) {
  482. return $this->renderError("现金卡退还金额异常");
  483. }
  484. // 写入现金卡流水明细
  485. UserRiceCardConsume::add($riceCard, $order['order_no'], $order['pay_price'], $orderGoods['rice_card_money'], 1, '商品退款');
  486. // // 退还现金卡金额
  487. UserRiceCard::setIncByField($orderGoods['rice_card_id'], 'balance', (float)$orderGoods['rice_card_money']);
  488. $riceCard->effect_state = 1; // 改为 生效
  489. $riceCard->save();
  490. }
  491. //售后单成功退了某商品的全部数量,把库存退回
  492. // if($order['order_source']==)
  493. FactoryStock::getFactory($order['order_source'])->backGoodsSkuStock($order,$orderGoods['goods_id'],$orderGoods['goods_sku_id'],$orderGoods['total_num']);
  494. //售后单成功退了某商品的全部数量,把库存退回给活动库存
  495. FactoryStock::getFactory($order['order_source'])->backActivityGoodsStock($orderGoods,$orderGoods['total_num']);
  496. }else{
  497. //非直接退款的订单,先给他冻结
  498. OrderGoodsModel::where('order_goods_id',$params['order_goods_id'])->update(['frozen_status'=>1]);
  499. }
  500. $refund_res = OrderRefundModel::create($param);
  501. //关闭订单状态
  502. $RefundService = new OrderService();
  503. $RefundService->closeOrder($param['order_id']);
  504. //写入金米粒记录
  505. GoldRice::refundGoldRiceRoad($order,$refund_res);
  506. //写入协商历史
  507. $his_type = 1;
  508. $name = $user->nick_name??'';
  509. $json_str = json_encode(["买家已申请退款"]);
  510. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,[],'买家','提交退款申请');
  511. //退款了就写退款记录
  512. if(isset($param['status'])&&$param['status']==20){
  513. $his_type = 2;
  514. $json_str = json_encode(["退款成功"]);
  515. $name = '公明腊肠官方体验商城';
  516. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,[]);
  517. }
  518. $refund['is_sys_refund'] = $refund_res->is_sys_refund;
  519. $refund['type'] = $type;
  520. $refund['refund_money'] = $refund_res->refund_money;
  521. $refund['goods_type'] = $refund_res->goods_type;
  522. $refund['apply_desc'] = $refund_res->apply_desc;
  523. $refund['create_time'] = Date("Y-m-d H:i:s",time());
  524. $refund['order_refund_id'] = $refund_res['order_refund_id'];
  525. return $this->renderSuccess(compact('refund'));
  526. }
  527. /**
  528. * 整单退款
  529. */
  530. private function refundAll($type, $order, $params, $user)
  531. {
  532. if($order['pay_status']==10){//未付款
  533. return $this->renderError('未付款不能申请退款');
  534. }
  535. // 普通配送单
  536. if($order['delivery_type']==10){
  537. // 存在已导出订单商品/存在已发货订单商品 不允许整单退款
  538. if (OrderGoodsModel::where('order_id', $params['order_id'])->where('is_export', 1)->count() > 0) {
  539. return $this->renderError('已发货不能申请退款');
  540. }
  541. if (OrderGoodsModel::where('order_id', $params['order_id'])->where('delivery_status', 20)->count() > 0) {
  542. return $this->renderError('已发货不能申请退款');
  543. }
  544. $orderGoods = OrderGoodsModel::where(['order_id' => $params['order_id'], 'is_export' => 0, 'delivery_status' => 10])->select();
  545. if (empty($orderGoods)) {
  546. return $this->renderError('该订单不满足整单退款条件');
  547. }
  548. }
  549. //门店自提单
  550. if($order['delivery_type']==20){
  551. if($order['hx_status']==20){
  552. return $this->renderError("已核销的订单不能申请退款");
  553. }
  554. $orderGoods = OrderGoodsModel::where(['order_id' => $params['order_id']])->select();
  555. if (empty($orderGoods)) {
  556. return $this->renderError('该订单不满足整单退款条件');
  557. }
  558. }
  559. // if(!empty($orderGoods['express_no'])){ //已发货
  560. // return $this->renderError('已发货不能申请退款');
  561. // }
  562. $refundMoney = helper::bcsub($order['pay_price'] + $order['rice_card_money'], 0, 2); // 退款金额 = 实付金额 + 现金卡抵扣金额
  563. if($refundMoney != $params['refund_money']){ //比较价格
  564. return $this->renderError('退款金额必须等于'.$refundMoney.'元');
  565. }
  566. $refund = OrderRefundModel::where(['order_id'=>$params['order_id']])->where('status','in',[0,10,20])->find();
  567. if($refund){
  568. if($refund['status']==20){
  569. return $this->renderError('订单退款完成');
  570. }
  571. return $this->renderError('当前订单商品已申请售后');
  572. }
  573. $goods_images = [];
  574. foreach ($order['goods'] as $arr){ //获取图片url
  575. $goods_images[] = $arr['good_image'];
  576. }
  577. $param['order_id'] = $params['order_id'];
  578. $param['refund_money'] = $params['refund_money'];
  579. $param['pay_money'] = $order['pay_price'];
  580. $param['gold_rice_amount'] = $order['gold_rice_amount'];
  581. $param['gold_rice_amount_output'] = $order['gold_rice_amount_input'];
  582. $param['rice_card_money'] = $order['rice_card_money'];
  583. $param['rice_card_id'] = $order['rice_card_id'];
  584. $param['express_price'] = $order['express_price'];
  585. $param['goods_type'] = $params['goods_type'];
  586. $param['apply_desc'] = $params['apply_desc'];
  587. $param['type'] = $type;// 30 未发货退款
  588. $param['refund_number'] = OrderService::createOrderNoPrefix('TKSQ'); //申请单编号
  589. // $param['out_refund_no'] = OrderService::createOrderNoPrefix('TK');//生成退款编号
  590. $param['user_id'] = $user['user_id']??0;
  591. $param['goods_images'] = $goods_images;
  592. $param['order_goods_id'] = $params['order_goods_id'];
  593. $param['store_id'] = 10001;
  594. $param['is_sys_refund'] = 1; //系统退款
  595. $param['goods_num'] = helper::getArrayColumnSum($orderGoods, 'total_num');
  596. $param['finance_refund'] =10;// 10已退款
  597. $param['out_refund_time'] = time(); //退款时间
  598. $param['refund_succ_time'] = time(); //退款成功时间
  599. $param['refund_close_time'] = time(); //退款关闭时间
  600. $param['status'] = 20;//售后单直接完成
  601. $param['delivery_type'] = $order['delivery_type'];
  602. $param['hx_status'] = $order['hx_status'];
  603. $param['shop_id'] = $order['shop_id'];
  604. if($order['delivery_type']==20){
  605. $param['audit_status'] = 10;//自提单直接门店审核通过
  606. }
  607. if ($param['pay_money'] > 0) {
  608. //直接退款逻辑处理 直接退款
  609. $RefundService = new RefundService();
  610. $refund_ret = $RefundService->execute($order,$param['refund_number'], $param['pay_money']);
  611. if ($refund_ret['status'] == 200) {
  612. $param['transaction_id'] = $refund_ret['data']['transaction_id']; // 微信退款交易号
  613. }
  614. }
  615. //订单商品修改成全额退款状态
  616. OrderGoodsModel::updateBase(['has_refund_full' => 1], ['order_id'=>$params['order_id']]);
  617. //全部退款变更不结算
  618. CommissionsDetail::updateAllDetails($params['order_id']);
  619. $riceCard = UserRiceCardModel::getValidRiceCardDetail($order['rice_card_id']);
  620. if ($order['rice_card_id']) { // 订单商品有使用现金卡
  621. // 验证余额
  622. if ($order['rice_card_money'] + $riceCard['balance'] > $riceCard['face_value']) {
  623. return $this->renderError("现金卡退还金额异常");
  624. }
  625. // 写入现金卡流水明细
  626. UserRiceCardConsume::add($riceCard, $order['order_no'], $order['pay_price'], $order['rice_card_money'], 1, '商品退款');
  627. // 退还现金卡金额
  628. UserRiceCard::setIncByField($order['rice_card_id'], 'balance', (float)$order['rice_card_money']);
  629. $riceCard->effect_state = 1; // 改为 生效
  630. $riceCard->save();
  631. }
  632. $refund_res = OrderRefundModel::create($param);
  633. //写入金米粒记录
  634. GoldRice::refundGoldRiceRoad($order,$refund_res);
  635. //关闭订单状态
  636. $RefundService = new OrderService();
  637. $RefundService->closeOrder($param['order_id']);
  638. //普通配送单
  639. if($order['delivery_type']==10){
  640. //写入协商历史
  641. $his_type = 1;
  642. $name = $user->nick_name??'';
  643. $json_str = json_encode(["买家已申请退款"]);
  644. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,[],'买家','提交退款申请');
  645. //退款了就写退款记录
  646. if(isset($param['status'])&&$param['status']==20){
  647. $his_type = 2;
  648. $json_str = json_encode(["退款成功"]);
  649. $name = '公明腊肠官方体验商城';
  650. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,[]);
  651. }
  652. }
  653. //门店自提
  654. if($order['delivery_type']==20){
  655. if(isset($param['status'])&&$param['status']==20){
  656. $his_type = 1;
  657. $name = $user->nick_name??'';
  658. $json_str = json_encode(["买家申请退款,退款成功"]);
  659. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,[],'买家','提交退款申请');
  660. }
  661. }
  662. $refund['is_sys_refund'] = $refund_res->is_sys_refund;
  663. $refund['type'] = $type;
  664. $refund['refund_money'] = $refund_res->refund_money;
  665. $refund['goods_type'] = $refund_res->goods_type;
  666. $refund['apply_desc'] = $refund_res->apply_desc;
  667. $refund['create_time'] = Date("Y-m-d H:i:s",time());
  668. $refund['order_refund_id'] = $refund_res['order_refund_id'];
  669. $refund['delivery_type'] = $refund_res['delivery_type'];
  670. //售后单完成之后,把库存退回,整单退款的
  671. FactoryStock::getFactory($order['order_source'])->backGoodsStock($order, true);
  672. //回退多个活动的活动库存
  673. FactoryStock::getFactory($order['order_source'])->backActivityStock($order, true);
  674. if($order['order_source']==OrderSourceEnum::ZA){
  675. $zarel = ZaActivityRelation::where('order_id',$order['order_id'])->find();
  676. if($zarel){
  677. $zarel->is_pay = 2;
  678. $zarel->expire_time = Date("Y-m-d H:i:s",time());
  679. if($zarel->child_order_id>0){
  680. OrderGoodsModel::updateBase(['has_refund_full' => 1], ['order_id'=>$zarel->child_order_id]);
  681. $RefundService->closeOrder($zarel->child_order_id);
  682. }
  683. $zarel->save();
  684. }
  685. }
  686. return $this->renderSuccess(compact('refund'));
  687. }
  688. //已付款已发货 -不退货退款 20
  689. public function refundMoneyNotGoods($type){
  690. $user = UserService::getCurrentLoginUser(true);
  691. $params = $this->baseValidate([
  692. "order_id"=>"require",//订单ID
  693. "order_goods_id"=>"require",//商品ID
  694. "refund_money"=>"require|float|gt:0",//退款金额
  695. "goods_type"=>"require|in:1,2",//货物状态 货物到货状态(1未收到货 2已收到货)
  696. "apply_desc"=>"require",//申请原因
  697. "apply_detail_desc"=>"max:150",//详细原因
  698. // "images"=>"array",//用户上传凭证图片(多图)
  699. ],[
  700. 'order_id.require'=>'没有选择订单',
  701. 'order_goods_id.require'=>'没有选择要退货的商品',
  702. 'refund_money.require'=>'退款金额不能为空',
  703. 'refund_money.float'=>'退款金额非法',
  704. 'refund_money.gt'=>'退款金额不能小于等于0',
  705. 'goods_type.require'=>'货物状态不能为空',
  706. 'goods_type.in'=>'货物状态非法',
  707. 'apply_desc.require'=>'退款原因不能为空',
  708. 'apply_detail_desc.max'=>'问题描述不能超过150字',
  709. // 'images.array'=>'用户上传的凭证格式不对',
  710. ]);
  711. $params['type'] = $type;
  712. $order = OrderModel::get(['user_id'=>$user->user_id,'order_id'=>$params['order_id']]);
  713. if(!$order){
  714. return $this->renderError('非法订单');
  715. }
  716. if($order['pay_status']==10){//未付款
  717. return $this->renderError('未付款不能申请不退货退款');
  718. }
  719. if($order['delivery_status']==10){ //未发货
  720. return $this->renderError('未发货不能申请不退货退款');
  721. }
  722. $order_good = OrderGoodsModel::find($params['order_goods_id']);
  723. if(!$order_good){
  724. return $this->renderError('非法商品');
  725. }
  726. $refund = OrderRefundModel::where(['order_goods_id'=>$params['order_goods_id']])->find();
  727. if($refund){
  728. if($refund['status']==20){
  729. return $this->renderError('订单退款完成');
  730. }
  731. return $this->renderError('当前订单商品已申请售后');
  732. }
  733. if($order_good['total_pay_price']<$params['refund_money']){ //比较价格
  734. return $this->renderError('退款金额不能超过'.$order_good['total_pay_price'].'元');
  735. }
  736. $param['type'] = $type;
  737. $param['order_id'] = $params['order_id'];
  738. $param['order_goods_id'] = $params['order_goods_id'];
  739. $param['refund_money'] = $params['refund_money'];
  740. $param['goods_type'] = $params['goods_type'];
  741. $param['apply_desc'] = $params['apply_desc'];
  742. $param['apply_detail_desc'] = $params['apply_detail_desc']??'';
  743. $param['images'] = implode(',',$params['images']);
  744. $param['user_id'] = $user->user_id;
  745. $param['status'] = 0; //等待商家审核中
  746. $param['refund_number'] = OrderService::createOrderNoPrefix('TKSQ'); //申请单编号
  747. // $param['out_refund_no'] = OrderService::createOrderNoPrefix('TK'); //退款编号
  748. // $param['provider_id'] = $order['provider_ids'];
  749. // $param['provider_name'] = $order_good['provider_name']??'';
  750. // $param['shop_take_time'] = time()+$delay_time; //等待商家处理的最后时间
  751. // $param['goods_images'] = [$order_good['good_image']??''];
  752. $refund_res = OrderRefundModel::create($param);
  753. //写入协商历史
  754. $his_type = 1;
  755. $name = $user->nick_name??'';
  756. $json_str = json_encode(["退款类型:不退货退款","退款金额:¥".$param['refund_money'],"申请原因:".$param['apply_desc'],"退款说明:".$param['apply_detail_desc']]);
  757. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,$param['images']);
  758. $refund['type'] = $type;
  759. $refund['refund_money'] = $refund_res->refund_money;
  760. $refund['goods_type'] = $refund_res->goods_type;
  761. $refund['apply_desc'] = $refund_res->apply_desc;
  762. $refund['create_time'] = Date("Y-m-d H:i:s",time());
  763. $refund['order_refund_id'] = $refund_res['order_refund_id'];
  764. return $this->renderSuccess(compact('refund'));
  765. }
  766. //添加检测赠品订单退货时是否需要退回赠送商品
  767. public function checkRefund(){
  768. $user = UserService::getCurrentLoginUser(true);
  769. $params = $this->baseValidate([
  770. "order_id"=>"require",//订单ID
  771. "order_goods_id"=>"require",//商品ID
  772. "refund_money"=>"require|float|gt:0",//退款金额
  773. "goods_num"=>"require|gt:0",//退款金额
  774. // "images"=>"array",//用户上传凭证图片(多图)
  775. ],[
  776. 'order_id.require'=>'没有选择订单',
  777. 'order_goods_id.require'=>'没有选择要退货的商品',
  778. 'refund_money.require'=>'退款金额不能为空',
  779. 'refund_money.float'=>'退款金额非法',
  780. 'refund_money.gt'=>'退款金额不能小于等于0',
  781. 'goods_num.require'=>'退款数量不能为空',
  782. 'goods_num.gt'=>'退款数量不能小于0',
  783. ]);
  784. // dd($params);
  785. $orderGoodsDetail = OrderGoodsModel::get(['order_goods_id'=>$params['order_goods_id']]);
  786. if(!$orderGoodsDetail){
  787. return $this->renderSuccess(['code'=>0]);
  788. }
  789. if($orderGoodsDetail['full_send_activity_id']>0){//如果参与了满就送活动
  790. $checkRefund = RefundService::checkRefund($params['order_id'],$params['order_goods_id'],$params['refund_money']);
  791. if($checkRefund){
  792. return $this->renderSuccess(['code'=>1,'count'=>1]);//需要退回满就送商品
  793. }
  794. }
  795. if($orderGoodsDetail['mj_send_activity_id']>0){//如果参与了满件送活动
  796. $checkRefund = RefundService::checkMjRefund($params['order_id'],$params['order_goods_id'],$params['goods_num']);
  797. if($checkRefund){
  798. return $this->renderSuccess(['code'=>2,'count'=>$checkRefund]);//需要退回满件送商品
  799. }
  800. }
  801. if($orderGoodsDetail['qc_send_activity_id']>0){//如果参与了全场满件送活动
  802. $checkRefund = RefundService::checkQcMjRefund($params['order_id'],$params['order_goods_id'],$params['goods_num']);
  803. if($checkRefund){
  804. return $this->renderSuccess(['code'=>3,'count'=>$checkRefund]);//需要退回满件送商品
  805. }
  806. }
  807. return $this->renderSuccess(['code'=>0]);
  808. }
  809. //已付款已发货 -退货退款(二期同一商品暂不做多笔申请)
  810. public function refundMoneyAndGoods($type){
  811. $user = UserService::getCurrentLoginUser(true);
  812. $params = $this->baseValidate([
  813. "order_id"=>"require",//订单ID
  814. "order_goods_id"=>"require",//商品ID
  815. "goods_num"=>"require|gt:0",//商品数量
  816. "refund_money"=>"require|float|gt:0",//退款金额
  817. "apply_desc"=>"require",//申请原因
  818. "apply_detail_desc"=>"max:150",//详细原因
  819. // "images"=>"array",//用户上传凭证图片(多图)
  820. ],[
  821. 'order_id.require'=>'没有选择订单',
  822. 'order_goods_id.require'=>'没有选择要退货的商品',
  823. 'goods_num.require'=>'商品数量不能为空',
  824. 'goods_num.gt'=>'商品数量不能为空',
  825. 'refund_money.require'=>'退款金额不能为空',
  826. 'refund_money.float'=>'退款金额非法',
  827. 'refund_money.gt'=>'退款金额不能小于等于0',
  828. 'apply_desc.require'=>'申请原因不能为空',
  829. 'apply_detail_desc.max'=>'详细原因不能超过150字',
  830. // 'images.array'=>'用户上传的凭证格式不对',
  831. ]);
  832. $order = OrderModel::get(['order_id'=>$params['order_id']]);
  833. $order_good = OrderGoodsModel::find($params['order_goods_id']);
  834. if(!$order){
  835. return $this->renderError('非法订单');
  836. }
  837. if($order['pay_status']==10){//未付款
  838. return $this->renderError('未付款不能申请退货退款');
  839. }
  840. if($order['delivery_type']==10){
  841. if($order_good['delivery_status'] == DeliveryStatusEnum::NOT_DELIVERED){ //未发货
  842. return $this->renderError('未发货不能申请退货退款');
  843. }
  844. }
  845. if($order['delivery_type']==20){
  846. if($order_good['receipt_status'] != ReceiptStatusEnum::RECEIVED){ //未收货
  847. return $this->renderError('门店自提未收货不能申请退货退款');
  848. }
  849. }
  850. if(!$order_good){
  851. return $this->renderError('非法商品');
  852. }
  853. if($order_good['total_num']<$params['goods_num']){
  854. return $this->renderError('当前商品数量不能超过'.$order_good['total_num'].'个');
  855. }
  856. if($order_good['is_activity_discount']==1){
  857. if($order_good['total_num']!=$params['goods_num']){
  858. return $this->renderError('参加折扣活动的商品必须退全部数量');
  859. }
  860. }
  861. $refund_time = $order_good['refund_time'];
  862. if($refund_time>0&&time()>$refund_time){
  863. return $this->renderError('当前售后时间已结束');
  864. }
  865. $count = OrderRefundModel::where(['order_goods_id'=>$params['order_goods_id']])->where('status','in',[0,10,20])->find();
  866. if($count){
  867. return $this->renderError('当前商品已申请售后');
  868. }
  869. //判断价格
  870. $refundMoney = helper::bcsub($order_good['total_pay_price'] + $order_good['rice_card_money'], 0, 2); // 退款金额 = 实付金额 + 现金卡抵扣金额
  871. if($refundMoney < $params['refund_money']){ //比较价格
  872. return $this->renderError('退款金额不能超过'.$refundMoney.'元');
  873. }
  874. //商品订单
  875. $total_pay_price = $order_good['total_pay_price'];
  876. $rice_card_money = $order_good['rice_card_money'];
  877. // 退的数量少于 订单的商品总数 即存在部份退
  878. if($params['goods_num']<$order_good['total_num']){
  879. // $one_pay_price = helper::bcsub($total_pay_price/$order_good['total_num'],0,2);
  880. // $one_rice_card_money = helper::bcsub(($rice_card_money/$order_good['total_num']),0,2);
  881. // $total_pay_price = helper::bcsub($one_pay_price*$params['goods_num'],0,2);
  882. // $rice_card_money = helper::bcsub($one_rice_card_money*$params['goods_num'],0,2);
  883. $price = helper::bcsub((($total_pay_price+$rice_card_money)/
  884. $order_good['total_num'])*$params['goods_num'],0,2);
  885. if($price<$params['refund_money']){
  886. return $this->renderError("退款金额不能超过商品实付价");
  887. }
  888. // if($total_pay_price+$rice_card_money<$params['refund_money']){
  889. // return $this->renderError("退款金额不能超过商品实付价");
  890. // }
  891. //申请的退款低于实际支付,按比例退
  892. if($total_pay_price+$rice_card_money>$params['refund_money']){
  893. $all_pay = $total_pay_price+$rice_card_money;
  894. $total_pay_price = helper::bcsub(($params['refund_money']/$all_pay)*$total_pay_price,0,2);
  895. $rice_card_money = helper::bcsub(($params['refund_money']/$all_pay)*$rice_card_money,0,2);
  896. }
  897. }else{
  898. //全部数量都退了,看是不是低于实际支付
  899. if($total_pay_price+$rice_card_money>$params['refund_money']){
  900. $all_pay = $total_pay_price+$rice_card_money;
  901. $total_pay_price = helper::bcsub(($params['refund_money']/$all_pay)*$total_pay_price,0,2);
  902. $rice_card_money = helper::bcsub(($params['refund_money']/$all_pay)*$rice_card_money,0,2);
  903. }
  904. }
  905. $user_id = $user->user_id;
  906. $param['type'] = $type;
  907. $param['store_id'] = 10001;
  908. $param['order_id'] = $params['order_id'];
  909. $param['order_goods_id'] = $params['order_goods_id'];
  910. $param['refund_money'] = $params['refund_money'];
  911. $param['pay_money'] = $total_pay_price;
  912. $param['gold_rice_amount'] = helper::bcdiv($params['goods_num']*$order_good['gold_rice_amount'],$order_good['total_num'],0);
  913. $param['gold_rice_amount_output'] = helper::bcdiv($params['goods_num']*$order_good['gold_rice_amount_input'],$order_good['total_num'],0);
  914. $param['rice_card_money'] = $rice_card_money;
  915. $param['rice_card_id'] = $order_good['rice_card_id'];
  916. $param['goods_num'] = $params['goods_num'];
  917. $param['apply_desc'] = $params['apply_desc'];
  918. $param['apply_detail_desc'] = $params['apply_detail_desc']??'';
  919. // $param['images'] = implode(",",$params['images']??[]);
  920. $param['images'] = $params['images']??'';
  921. $param['user_id'] = $user_id;
  922. $param['status'] = 0; //等待商家审核中
  923. $param['refund_number'] = OrderService::createOrderNoPrefix('TKSQ'); //申请单编号
  924. // $param['out_refund_no'] = OrderService::createOrderNoPrefix('TK'); //退款编号
  925. // $param['provider_id'] = $order['provider_ids'];
  926. // $param['provider_name'] = $order_good['provider_name']??'';
  927. $param['goods_type'] = 2;//默认写收收到货
  928. $param['goods_images'] = [$order_good['good_image']];
  929. $param['delivery_type'] = $order['delivery_type'];
  930. $param['hx_status'] = $order['hx_status'];
  931. $param['shop_id'] = $order['shop_id'];
  932. $refund_res = OrderRefundModel::create($param);
  933. //写入协商历史
  934. //非直接退款的订单,先给他冻结
  935. OrderGoodsModel::where('order_goods_id',$params['order_goods_id'])->update(['frozen_status'=>1]);
  936. if($param['delivery_type']==10){
  937. $his_type = 1;
  938. $name = $user->nick_name??'';
  939. $json_str = json_encode(["买家已申请退款"]);
  940. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,$param['images'],'买家','提交退款申请');
  941. }
  942. if($param['delivery_type']==20){
  943. $his_type = 1;
  944. $name = $user->nick_name??'';
  945. $json_str = json_encode(["退款类型:退货退款","退款原因:".$param['apply_desc'],"问题描述:".$param['apply_detail_desc']]);
  946. (new RefundHis())->add($refund_res['order_refund_id'],$name,$his_type,$json_str,$param['images'],'买家','提交退款申请');
  947. }
  948. $refund['type'] = $type;
  949. $refund['refund_money'] = $refund_res->refund_money;
  950. $refund['goods_type'] = $refund_res->goods_type;
  951. $refund['goods_num'] = $refund_res->goods_num;
  952. $refund['apply_desc'] = $refund_res->apply_desc;
  953. // $refund['refund_succ_time'] = Date("Y-m-d H:i:s",$param['refund_succ_time']);
  954. $refund['create_time'] = Date("Y-m-d H:i:s",time());
  955. $refund['order_refund_id'] = $refund_res['order_refund_id'];
  956. $refund['delivery_type'] = $refund_res->delivery_type;
  957. return $this->renderSuccess(compact('refund'));
  958. }
  959. // /**
  960. // * 申请售后
  961. // * @param int $orderGoodsId 订单商品ID
  962. // * @return array|\think\response\Json
  963. // * @throws BaseException
  964. // */
  965. // public function apply(int $orderGoodsId)
  966. // {
  967. // // 新增售后单记录
  968. // $model = new OrderRefundModel;
  969. // if ($model->apply($orderGoodsId, $this->postForm())) {
  970. // return $this->renderSuccess([], '提交成功');
  971. // }
  972. // return $this->renderError($model->getError() ?: '提交失败');
  973. // }
  974. public function auditstatus(int $orderRefundId){
  975. $audit_status = OrderRefundModel::where("order_refund_id",
  976. $orderRefundId)->value("audit_status");
  977. return $this->renderSuccess(compact('audit_status'));
  978. }
  979. /**
  980. * 售后单详情
  981. * @param int $orderRefundId 售后单ID
  982. * @return array|\think\response\Json
  983. * @throws BaseException
  984. */
  985. public function detail(int $orderRefundId)
  986. {
  987. $detail = OrderRefundModel::getDetail($orderRefundId, true);
  988. $hx_refund_code = $detail['hx_refund_code'];
  989. if(empty($hx_refund_code)){
  990. $i = 10000000;
  991. $refund_id = $detail['order_refund_id'];
  992. $hx_refund_code = "T".($i+$refund_id);
  993. OrderRefundModel::where("order_refund_id",$detail['order_refund_id'])->update(['hx_refund_code'=>$hx_refund_code]);
  994. $detail['hx_refund_code'] = $hx_refund_code;
  995. }
  996. $detail['hx_refund_url'] = url('/api/order/getHxQrcode',['hxCode'=>$hx_refund_code], true, true)->build();
  997. $pay_type_text ='微信';
  998. if($detail['pay_money']>0&&$detail['rice_card_id']>0){
  999. $pay_type_text = '微信+现金卡';
  1000. }
  1001. if($detail['pay_money']==0&&$detail['rice_card_id']>0){
  1002. $pay_type_text = '现金卡';
  1003. }
  1004. $detail['pay_type_text'] = $pay_type_text;
  1005. return $this->renderSuccess(compact('detail'));
  1006. }
  1007. /**
  1008. * 撤销申请
  1009. * @param int $orderRefundId 售后单ID
  1010. * @return array|\think\response\Json
  1011. * @throws BaseException
  1012. */
  1013. public function revoke(int $orderRefundId)
  1014. {
  1015. // 售后单详情
  1016. $model = OrderRefundModel::getDetail($orderRefundId, false);
  1017. if ($model->revoke()) {
  1018. return $this->renderSuccess([], '操作成功');
  1019. }
  1020. return $this->renderError($model->getError() ?: '提交失败');
  1021. }
  1022. /**
  1023. * 用户发货
  1024. * @param int $orderRefundId 售后单ID
  1025. * @return array|\think\response\Json
  1026. * @throws BaseException
  1027. */
  1028. public function delivery(int $orderRefundId)
  1029. {
  1030. // 售后单详情
  1031. $model = OrderRefundModel::getDetail($orderRefundId, false);
  1032. if ($model->delivery( $this->request->post())) {
  1033. return $this->renderSuccess([], '操作成功');
  1034. }
  1035. return $this->renderError($model->getError() ?: '提交失败');
  1036. }
  1037. /**
  1038. * 查看物流详情
  1039. * @param int $orderRefundId 售后单ID
  1040. * @return array|\think\response\Json
  1041. * @throws BaseException
  1042. * @author: zjwhust
  1043. * @Time: 2021/10/28 14:13
  1044. */
  1045. public function express(int $orderRefundId)
  1046. {
  1047. // 订单信息
  1048. $model = OrderRefundModel::getDetail($orderRefundId, false);
  1049. if (!$model || !$model['is_user_send']) {
  1050. return $this->renderError('没有物流信息');
  1051. }
  1052. $ExpressModel = new ExpressModel;
  1053. $res = $ExpressModel->deliverylist($model['express_no'], $model['send_time']);
  1054. return $this->renderSuccess($res);
  1055. }
  1056. //协商历史
  1057. public function hisList($order_refund_id){
  1058. $list = RefundHis::where(['order_refund_id'=>$order_refund_id])->where('type','>',0)->order(['id'=>'desc'])->select();
  1059. if($list){
  1060. foreach ($list as &$arr){
  1061. $arr['json'] = json_decode($arr['json'],true);
  1062. }
  1063. }
  1064. return $this->renderSuccess(compact('list'));
  1065. }
  1066. }