RefundOrder.php 20 KB

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