RefundOrder.php 20 KB

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