RefundOrder.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author Qinii
  6. * @day 2020-06-15
  7. *
  8. *
  9. */
  10. namespace app\controller\admin\order;
  11. use app\common\repositories\user\UserRepository;
  12. use app\services\ThirdParty\AlibabaAgent\OrderService;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\store\order\MerchantReconciliationorderRepository;
  15. use app\common\repositories\store\order\MerchantReconciliationRepository;
  16. use app\common\repositories\system\merchant\MerchantRepository;
  17. use app\common\repositories\store\order\StoreRefundOrderRepository as repository;
  18. use think\App;
  19. use think\facade\Db;
  20. class RefundOrder extends BaseController
  21. {
  22. protected $repository;
  23. public function __construct(App $app,repository $repository)
  24. {
  25. parent::__construct($app);
  26. $this->repository = $repository;
  27. }
  28. public function lst($id)
  29. {
  30. [$page,$limit] = $this->getPage();
  31. // $where['reconciliation_type'] = $this->request->param('status',1);
  32. $where['status'] =$this->request->param('status','');
  33. $where['date'] = $this->request->param('date');
  34. $where['mer_id'] = $id;
  35. $where['status'] = 3;
  36. return app('json')->success($this->repository->getAdminList($where,$page,$limit));
  37. }
  38. public function markForm($id)
  39. {
  40. if(!$this->repository->getWhereCount([$this->repository->getPk() => $id]))
  41. return app('json')->fail('数据不存在');
  42. return app('json')->success(formToData($this->repository->adminMarkForm($id)));
  43. }
  44. public function mark($id)
  45. {
  46. if(!$this->repository->getWhereCount([$this->repository->getPk() => $id]))
  47. return app('json')->fail('数据不存在');
  48. $data = $this->request->params(['admin_mark']);
  49. $this->repository->update($id,$data);
  50. return app('json')->success('备注成功');
  51. }
  52. public function getAllList()
  53. {
  54. [$page,$limit] = $this->getPage();
  55. $where = $this->request->params(['refund_order_sn','status','refund_type','date','mer_id','order_sn']);
  56. return app('json')->success($this->repository->getAllList($where, $page, $limit));
  57. }
  58. /**
  59. * @return mixed
  60. * @author 史晨
  61. * @date 2026/5/8 17:27
  62. * 1688订单获取退款原因
  63. */
  64. public function refundReasonList()
  65. {
  66. $orderId = request()->param('alibaba_order_id');
  67. if (empty($orderId)) {
  68. return app('json')->fail('订单号不能为空');
  69. }
  70. $orderService = new OrderService();
  71. $params = ['orderId' => $orderId];
  72. $result = $orderService->getRefundReasonList($params);
  73. if ($result === false) {
  74. return app('json')->fail('获取退款原因失败');
  75. }
  76. return app('json')->success($result);
  77. }
  78. public function detail($id)
  79. {
  80. $data=$this->repository->getOne($id);
  81. return app('json')->success($data);
  82. }
  83. /**
  84. * @param $id
  85. * @return mixed
  86. * @author Qinii
  87. * @day 2020-06-12
  88. */
  89. public function switchStatus($id)
  90. {
  91. $status=-1;
  92. if($this->request->param('status') == 1){
  93. $status=1;
  94. }else if($this->request->param('status') == 2){
  95. $status=2;
  96. }
  97. // $status = ($this->request->param('status') == 1) ? 1 : -1;
  98. if($status == 1 || $status == 2){
  99. $refund_order=$this->repository->get($id);
  100. if($status==2){
  101. $part_refund_message = $this->request->param('part_refund_message','');
  102. $refund_price =(float) $this->request->param('part_refund_price',0);
  103. if($refund_price<0.01){
  104. return app('json')->fail('退款金额不能小于0.01');
  105. }
  106. if($refund_order && $refund_price >$refund_order['refund_price']){
  107. return app('json')->fail('退款金额不能大于用户申请金额');
  108. }
  109. if($part_refund_message==''){
  110. return app('json')->fail('请填写商家备注');
  111. }
  112. $this->repository->update($id,['part_refund_price'=>$refund_price,'part_refund_message' => $part_refund_message,'part_refund_time'=>date('Y-m-d H:i:s')]);
  113. }
  114. $douduo = $this -> repository -> douduofund($id,true);
  115. if($douduo['code'] < 0){
  116. return app('json')->fail($douduo['message']);
  117. }
  118. if($douduo['code'] == 2){
  119. return app('json')->success($douduo['message']);
  120. }
  121. $data = $this->request->params(['mer_delivery_user','mer_delivery_address','phone']);
  122. if($refund_order['refund_type']==2 && $status == 1 && ($data['mer_delivery_user']=='' || $data['mer_delivery_address']=='' || $data['phone']=='')){
  123. return app('json')->fail('请输入收货人信息');
  124. }
  125. $data['status'] = $status;
  126. // Db::name('log')->insert(['data'=>'这了'. 1]);
  127. $this->repository->agree($id,$data,$this->request->adminId(),1);
  128. // 获取订单金额
  129. $store_order_info = Db::name('store_order')
  130. ->where('order_id', $refund_order['order_id'])
  131. ->find();
  132. $group_order_map = Db::name('store_group_order')
  133. ->where('group_order_id', $store_order_info['group_order_id'])
  134. ->field('uid,pay_price,hf_pay_id,fzone_paytype')
  135. ->find();
  136. $make = '商贸区';
  137. if ($store_order_info['mer_id'] == 439) {
  138. $make = '精彩生活区';
  139. } else if ($store_order_info['mer_id'] == 496) {
  140. $make = '会员专区';
  141. } else if ($store_order_info['mer_id'] == 491) {
  142. $make = '复购区';
  143. }
  144. // 如果是组合支付 现金 + 积分 则需要把积分退回
  145. // 参考 app\controller\api\Notify.php -- 745 $array_fzone_paytype['type']=='b2'
  146. $array_fzone_paytype = json_decode($group_order_map['fzone_paytype'], true);
  147. if (!empty($array_fzone_paytype)) {
  148. if ($array_fzone_paytype['type'] == 'b1') {
  149. // VR+积分 {"checked":true,"note":"VR+积分","other":210.0,"price":970.0,"total":0.0,"type":"b1","vr":"47557.5"}
  150. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('vr');
  151. $vrpri += $array_fzone_paytype['price'];
  152. Db::name("user")->where("uid", $group_order_map['uid'])->update(['vr' => $vrpri]);
  153. Db::name("user_sign_vr")->insert([
  154. 'uid' => $store_order_info['uid'],
  155. 'number' => $array_fzone_paytype['price'],
  156. 'mark' => "{$make}商品申请退款退回VR",
  157. 'desc' => "VR",
  158. 'order_type' => 0,
  159. 'order_id' => $store_order_info['order_id'],
  160. 'surplus' => $vrpri,
  161. 'type' => 1,
  162. 'addtime' => time()
  163. ]);
  164. // {"checked":true,"note":"VR+积分","other":1.5,"price":8.5,"total":0.0,"type":"b2","vr":"48799.82"}
  165. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('score');
  166. $vrpri += $array_fzone_paytype['other'];
  167. Db::name("user")->where("uid", $group_order_map['uid'])->update(['score' => $vrpri]);
  168. $data = [
  169. "uid" => $store_order_info['uid'],
  170. "reward_socre" => $array_fzone_paytype['other'],
  171. "addtime" => time(),
  172. "status" => 1,
  173. "order_id" => $store_order_info['order_id'],
  174. "mark" => "{$make}商品申请退款退回积分",
  175. "surplus" => $vrpri,
  176. "pm" => 1,
  177. "source_type" => 3
  178. ];
  179. Db::name("user_sign_score")->insert($data);//添加积分记录
  180. } else if ($array_fzone_paytype['type'] == 'b2') {
  181. // {"checked":true,"note":"现金+积分","other":1.5,"price":8.5,"total":0.0,"type":"b2","vr":"48799.82"}
  182. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('score');
  183. $vrpri += $array_fzone_paytype['other'];
  184. Db::name("user")->where("uid", $group_order_map['uid'])->update(['score' => $vrpri]);
  185. $data = [
  186. "uid" => $store_order_info['uid'],
  187. "reward_socre" => $array_fzone_paytype['other'],
  188. "addtime" => time(),
  189. "status" => 1,
  190. "order_id" => $store_order_info['order_id'],
  191. "mark" => "{$make}商品申请退款退回积分",
  192. "surplus" => $vrpri,
  193. "pm" => 1,
  194. "source_type" => 3
  195. ];
  196. Db::name("user_sign_score")->insert($data);//添加积分记录
  197. } else if ($array_fzone_paytype['type'] == 'b3') {
  198. // VR+积分 {"checked":true,"note":"VR+积分","other":210.0,"price":970.0,"total":0.0,"type":"b1","vr":"47557.5"}
  199. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('vr');
  200. $vrpri += $array_fzone_paytype['price'];
  201. Db::name("user")->where("uid", $group_order_map['uid'])->update(['vr' => $vrpri]);
  202. Db::name("user_sign_vr")->insert([
  203. 'uid' => $store_order_info['uid'],
  204. 'number' => $array_fzone_paytype['price'],
  205. 'mark' => "{$make}商品申请退款退回VR",
  206. 'desc' => "VR",
  207. 'order_type' => 0,
  208. 'order_id' => $store_order_info['order_id'],
  209. 'surplus' => $vrpri,
  210. 'type' => 1,
  211. 'addtime' => time()
  212. ]);
  213. // {"checked":true,"note":"VR+积分","other":1.5,"price":8.5,"total":0.0,"type":"b2","vr":"48799.82"}
  214. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('jingdou');
  215. $vrpri += $array_fzone_paytype['other'];
  216. Db::name("user")->where("uid", $group_order_map['uid'])->update(['jingdou' => $vrpri]);
  217. Db::name('user_sign_jingdou')->insert([
  218. 'uid' => $store_order_info['uid'],
  219. 'number' => $array_fzone_paytype['other'],
  220. 'status' => 1,
  221. 'mark' => "{$make}商品申请退款退回京豆",
  222. 'addtime' => time(),
  223. 'surplus' => $vrpri,
  224. 'settlement_time' => date('Y-m-d H:i:s'),
  225. 'type' => 1,
  226. 'order_type' => 0,
  227. 'order_id' => $store_order_info['order_id']
  228. ]);
  229. } else if ($array_fzone_paytype['type'] == 'b4') {
  230. // {"checked":true,"note":"现金+积分","other":1.5,"price":8.5,"total":0.0,"type":"b2","vr":"48799.82"}
  231. $jingdoupri = Db::name("user")->where("uid", $group_order_map['uid'])->value('jingdou');
  232. $jingdoupri += $array_fzone_paytype['other'];
  233. Db::name("user")->where("uid", $group_order_map['uid'])->update(['jingdou' => $jingdoupri]);
  234. Db::name('user_sign_jingdou')->insert([
  235. 'uid' => $store_order_info['uid'],
  236. 'number' => $array_fzone_paytype['other'],
  237. 'status' => 1,
  238. 'mark' => "{$make}商品申请退款退回京豆",
  239. 'addtime' => time(),
  240. 'surplus' => $jingdoupri,
  241. 'settlement_time' => date('Y-m-d H:i:s'),
  242. 'type' => 1,
  243. 'order_type' => 0,
  244. 'order_id' => $store_order_info['order_id']
  245. ]);
  246. } else if ($array_fzone_paytype['type'] == 'b5') {
  247. // {"checked":true,"note":"VR","other":0.0,"price":1.0,"total":0.0,"type":"b5","vr":"10"}
  248. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('vr');
  249. $vrpri += $array_fzone_paytype['price'];
  250. Db::name("user")->where("uid", $group_order_map['uid'])->update(['vr' => $vrpri]);
  251. Db::name("user_sign_vr")->insert([
  252. 'uid' => $store_order_info['uid'],
  253. 'number' => $array_fzone_paytype['price'],
  254. 'mark' => "{$make}商品申请退款退回VR",
  255. 'desc' => "VR",
  256. 'order_type' => 0,
  257. 'order_id' => $store_order_info['order_id'],
  258. 'surplus' => $vrpri,
  259. 'type' => 1,
  260. 'addtime' => time()
  261. ]);
  262. } else if ($array_fzone_paytype['type'] == 'a3') {
  263. // {"checked":true,"note":"现金+VR","other":1.5,"price":8.5,"total":0.0,"type":"a3","vr":"48799.82"}
  264. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('vr');
  265. $vrpri += $array_fzone_paytype['other'];
  266. Db::name("user")->where("uid", $group_order_map['uid'])->update(['vr' => $vrpri]);
  267. //VR入账
  268. $insertdata = [
  269. 'uid'=> $store_order_info['uid'],
  270. 'number' => $array_fzone_paytype['other'],
  271. 'surplus' => $vrpri,
  272. 'mark' => "{$make}商品申请退款退回VR",
  273. 'order_id'=> $store_order_info['order_id'],
  274. 'type' => 2,
  275. 'addtime' => time(),
  276. 'settlement_time'=> date('Y-m-d H:i:s', time())
  277. ];
  278. Db::name('user_sign_vr')->insert($insertdata);
  279. } else if ($array_fzone_paytype['type'] == 'c1') {
  280. // {"checked":true,"note":"复购金","other":1.5,"price":8.5,"total":0.0,"type":"c1","vr":"48799.82"}
  281. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('fugou');
  282. $vrpri += $array_fzone_paytype['price'];
  283. Db::name("user")->where("uid", $group_order_map['uid'])->update(['fugou' => $vrpri]);
  284. //支出账号存到log日志
  285. $log_data = [
  286. 'uid' => $store_order_info['uid'],
  287. 'number' => $array_fzone_paytype['price'],
  288. 'mark' => "复购区商品申请退款退回复购金:" . $array_fzone_paytype['price'],
  289. 'desc' => "复购金",
  290. 'order_id' => $store_order_info['order_id'],
  291. 'surplus' => $vrpri,
  292. 'order_type' => 0,
  293. 'type' => 1,
  294. 'addtime' => time()
  295. ];
  296. Db::name("user_sign_fugou")->insert($log_data);
  297. }
  298. }
  299. // 取消 订单所发放的福利
  300. $this->repository->cancelOrderBonus($id);
  301. }else{
  302. $fail_message = $this->request->param('fail_message','');
  303. if($status == -1 && empty($fail_message)){
  304. return app('json')->fail('未通过必须填写');
  305. }
  306. if($fail_message==''){
  307. return app('json')->fail('未通过必须填写');
  308. }
  309. $data['status'] = $status;
  310. $data['fail_message'] = $fail_message;
  311. $this->repository->refuse($id,$data);
  312. $this->repository->set_consult(2,$id);//协商记录
  313. }
  314. return app('json')->success('审核成功');
  315. }
  316. /**
  317. * TODO 收货后确定退款
  318. * @param $id
  319. * @return mixed
  320. * @author Qinii
  321. * @day 2020-06-12
  322. */
  323. public function refundPrice($id)
  324. {
  325. if($this->request->param('status') == 2){
  326. $part_refund_message = $this->request->param('part_refund_message','');
  327. $refund_price =(float) $this->request->param('part_refund_price',0);
  328. if($refund_price<0.01){
  329. return app('json')->fail('退款金额不能小于0.01');
  330. }
  331. $refund_order=$this->repository->get($id);
  332. if($refund_order && $refund_price>$refund_order['refund_price']){
  333. return app('json')->fail('退款金额不能大于用户申请金额');
  334. }
  335. $this->repository->update($id,['part_refund_price'=>$refund_price,'part_refund_message' => $part_refund_message,'part_refund_time'=>date('Y-m-d H:i:s')]);
  336. }else if($this->request->param('status') == -1){
  337. $fail_message = $this->request->param('fail_message','');
  338. if(empty($fail_message))
  339. return app('json')->fail('未通过必须填写');
  340. $data['status'] = -1;
  341. $data['fail_message'] = $fail_message;
  342. $this->repository->refuse($id,$data);
  343. $this->repository->set_consult(7,$id);//协商记录
  344. return app('json')->success('拒绝成功');
  345. }
  346. $this->repository->set_consult(5,$id);//协商记录
  347. $this->repository->adminRefund($id,$this->request->adminId());
  348. return app('json')->success('退款成功');
  349. }
  350. /**
  351. * TODO 快递查询
  352. * @param $id
  353. * @return mixed
  354. * @author Qinii
  355. * @day 2020-06-25
  356. */
  357. public function express($id)
  358. {
  359. // if(!$this->repository->getWhereCount(['refund_order_id' => $id,'status' =>2]))
  360. // return app('json')->fail('订单信息或状态错误');
  361. return app('json')->success($this->repository->express($id));
  362. }
  363. public function reList($id)
  364. {
  365. [$page,$limit] = $this->getPage();
  366. $where = ['reconciliation_id' => $id,'type' => 1];
  367. return app('json')->success($this->repository->reconList($where,$page,$limit));
  368. }
  369. public function dhm_list(){
  370. [$page,$limit] = $this->getPage();
  371. $status = $this->request->param('status','');
  372. $order_sn = $this->request->param('order_sn');
  373. $refund_order_sn = $this->request->param('refund_order_sn');
  374. $query = Db::name('StoreRefundOrder')
  375. -> alias('sro')
  376. -> join('StoreOrder so','sro.order_id = so.order_id')
  377. -> where('so.supply_type','in',[1,4,5]);
  378. if($status != ''){
  379. $query -> where('so.douhuomall_status',$status);
  380. }
  381. if($order_sn != ''){
  382. $query -> where('so.order_sn',$order_sn);
  383. }
  384. if($refund_order_sn != ''){
  385. $query -> where('sro.refund_order_sn',$refund_order_sn);
  386. }
  387. $query -> order('sro.refund_order_id desc');
  388. $count = $query->count();
  389. $list = $query->page($page, $limit)->field("so.*,sro.*,sro.status as sro_status") -> select();
  390. if ($list){
  391. $list = $list -> toArray();
  392. foreach ($list as $key => &$value){
  393. $product = Db::name('store_refund_product') -> where('refund_order_id',$value['refund_order_id']) -> select()->each(function ($item){
  394. $item['product'] = Db::name('store_order_product') -> where('order_product_id',$item['order_product_id']) -> find();
  395. $item['product']['cart_info'] = json_decode($item['product']['cart_info']);
  396. return $item;
  397. });
  398. $value['refundProduct'] = $product;
  399. $value['user'] = Db::name('user') -> where('uid',$value['uid']) -> field('nickname,phone,uid') -> find();
  400. }
  401. }
  402. return app('json')->success(compact('count', 'list'));
  403. }
  404. public function dhm_status(){
  405. $status = $this->request->param('status',0);
  406. $id = $this->request->param('id',0);
  407. $c = Db::name('StoreRefundOrder') -> where(['refund_order_id' => $id]) -> field('status') -> find();
  408. if($c['status'] != 0 || !$c || $status == 0) return app('json')->fail('信息或状态错误');
  409. if ($status == -1){
  410. $fail_message = $this->request->param('fail_message','');
  411. if($status == -1 && empty($fail_message))
  412. return app('json')->fail('未通过必须填写');
  413. $data['status'] = $status;
  414. $data['fail_message'] = $fail_message;
  415. $this->repository->refuse($id,$data);
  416. }elseif ($status == 2){
  417. //处理中
  418. $dhm_msg = $this->request->param('dhm_msg','');
  419. Db::name('StoreRefundOrder') -> where(['refund_order_id' => $id]) -> update(['dhm_msg'=>$dhm_msg]);
  420. return app('json')->success('提交成功');
  421. }else{
  422. $data['status'] = $status;
  423. $this->repository->agree($id,$data,$this->request->adminId(),1);
  424. }
  425. return app('json')->success('审核成功');
  426. }
  427. }