RefundOrder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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'] == 'b2') {
  130. // {"checked":true,"note":"现金+积分","other":1.5,"price":8.5,"total":0.0,"type":"b2","vr":"48799.82"}
  131. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('score');
  132. $vrpri += $array_fzone_paytype['other'];
  133. Db::name("user")->where("uid", $group_order_map['uid'])->update(['score' => $vrpri]);
  134. $data = [
  135. "uid" => $store_order_info['uid'],
  136. "reward_socre" => $array_fzone_paytype['other'],
  137. "addtime" => time(),
  138. "status" => 1,
  139. "order_id" => $store_order_info['order_id'],
  140. "mark" => "{$make}商品申请退款退回积分",
  141. "surplus" => $vrpri,
  142. "pm" => 1,
  143. "source_type" => 3
  144. ];
  145. Db::name("user_sign_score")->insert($data);//添加积分记录
  146. } else if ($array_fzone_paytype['type'] == 'b4') {
  147. // {"checked":true,"note":"现金+积分","other":1.5,"price":8.5,"total":0.0,"type":"b2","vr":"48799.82"}
  148. $jingdoupri = Db::name("user")->where("uid", $group_order_map['uid'])->value('jingdou');
  149. $jingdoupri += $array_fzone_paytype['other'];
  150. Db::name("user")->where("uid", $group_order_map['uid'])->update(['jingdou' => $jingdoupri]);
  151. Db::name('user_sign_jingdou')->insert([
  152. 'uid' => $store_order_info['uid'],
  153. 'number' => $array_fzone_paytype['other'],
  154. 'status' => 1,
  155. 'mark' => "{$make}商品申请退款退回京豆",
  156. 'addtime' => time(),
  157. 'surplus' => $jingdoupri,
  158. 'settlement_time' => date('Y-m-d H:i:s'),
  159. 'type' => 1,
  160. 'order_type' => 0,
  161. 'order_id' => $store_order_info['order_id']
  162. ]);
  163. } else if ($array_fzone_paytype['type'] == 'a3') {
  164. // {"checked":true,"note":"现金+VR","other":1.5,"price":8.5,"total":0.0,"type":"a3","vr":"48799.82"}
  165. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('vr');
  166. $vrpri += $array_fzone_paytype['other'];
  167. Db::name("user")->where("uid", $group_order_map['uid'])->update(['vr' => $vrpri]);
  168. //VR入账
  169. $insertdata = [
  170. 'uid'=> $store_order_info['uid'],
  171. 'number' => $array_fzone_paytype['other'],
  172. 'surplus' => $vrpri,
  173. 'mark' => "{$make}商品申请退款退回VR",
  174. 'order_id'=> $store_order_info['order_id'],
  175. 'type' => 2,
  176. 'addtime' => time(),
  177. 'settlement_time'=> date('Y-m-d H:i:s', time())
  178. ];
  179. Db::name('user_sign_vr')->insert($insertdata);
  180. }
  181. // else if ($array_fzone_paytype['type'] == 'c1') {
  182. // // {"checked":true,"note":"复购金","other":1.5,"price":8.5,"total":0.0,"type":"c1","vr":"48799.82"}
  183. // $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('fugou');
  184. // $vrpri += $array_fzone_paytype['other'];
  185. // Db::name("user")->where("uid", $group_order_map['uid'])->update(['fugou' => $vrpri]);
  186. // //支出账号存到log日志
  187. // $log_data = [
  188. // 'uid' => $store_order_info['uid'],
  189. // 'number' => $array_fzone_paytype['price'],
  190. // 'mark' => "复购区商品申请退款退回复购金:" . $array_fzone_paytype['price'],
  191. // 'desc' => "复购金",
  192. // 'order_id' => $store_order_info['order_id'],
  193. // 'surplus' => $vrpri,
  194. // 'order_type' => 0,
  195. // 'type' => 2,
  196. // 'addtime' => time()
  197. // ];
  198. // $user_res_log = Db::name("user_sign_fugou")->insert($log_data);
  199. // }
  200. }
  201. // 取消 订单所发放的福利
  202. $this->repository->cancelOrderBonus($id);
  203. }else{
  204. $fail_message = $this->request->param('fail_message','');
  205. if($status == -1 && empty($fail_message)){
  206. return app('json')->fail('未通过必须填写');
  207. }
  208. if($fail_message==''){
  209. return app('json')->fail('未通过必须填写');
  210. }
  211. $data['status'] = $status;
  212. $data['fail_message'] = $fail_message;
  213. $this->repository->refuse($id,$data);
  214. $this->repository->set_consult(2,$id);//协商记录
  215. }
  216. return app('json')->success('审核成功');
  217. }
  218. public function switchStatusjhm($id)
  219. {
  220. $this->repository->agreejhm($id);
  221. return app('json')->success('审核成功');
  222. }
  223. public function switchStatusnf($id)
  224. {
  225. $res = $this->repository->agreenf($id);
  226. return app('json')->success($res);
  227. }
  228. public function switchStatusqds($id)
  229. {
  230. $res = $this->repository->agreeqds($id);
  231. return app('json')->success($res);
  232. }
  233. /**
  234. * TODO 收货后确定退款
  235. * @param $id
  236. * @return mixed
  237. * @author Qinii
  238. * @day 2020-06-12
  239. */
  240. public function refundPrice($id)
  241. {
  242. if(!$this->repository->getRefundPriceExists($this->request->merId(),$id))
  243. return app('json')->fail('信息或状态错误');
  244. if($this->request->param('status') == 2){
  245. $part_refund_message = $this->request->param('part_refund_message','');
  246. $refund_price =(float) $this->request->param('part_refund_price',0);
  247. if($refund_price<0.01){
  248. return app('json')->fail('退款金额不能小于0.01');
  249. }
  250. $refund_order=$this->repository->get($id);
  251. $pay_price=Db::name('store_order')->where('order_id',$refund_order['order_id'])->value('pay_price');
  252. if($refund_price > $pay_price){
  253. return app('json')->fail('退款金额不能大于用户支付金额');
  254. }
  255. if($part_refund_message==''){
  256. return app('json')->fail('请填写商家备注');
  257. }
  258. $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')]);
  259. }else if($this->request->param('status') == -1){
  260. $fail_message = $this->request->param('fail_message','');
  261. if(empty($fail_message))
  262. return app('json')->fail('请输入拒绝原因');
  263. $data['status'] = -1;
  264. $data['fail_message'] = $fail_message;
  265. $this->repository->refuse($id,$data);
  266. $this->repository->set_consult(7,$id);//协商记录
  267. return app('json')->success('拒绝成功');
  268. }
  269. $this->repository->set_consult(5,$id);//协商记录
  270. $this->repository->adminRefund($id,$this->request->adminId());
  271. return app('json')->success('退款成功');
  272. }
  273. /**
  274. * TODO
  275. * @param $id
  276. * @return mixed
  277. * @author Qinii
  278. * @day 2020-06-12
  279. */
  280. public function switchStatusForm($id)
  281. {
  282. if(!$this->repository->getStatusExists($this->request->merId(),$id))
  283. return app('json')->fail('信息或状态错误');
  284. // $douduo = $this -> repository -> douduofund($id,false);
  285. // if($douduo['code'] < 0){
  286. // return app('json')->fail($douduo['message']);
  287. // }
  288. // if($douduo['code'] == 2){
  289. // return app('json')->success($douduo['message']);
  290. // }
  291. return app('json')->success(formToData($this->repository->statusForm($id)));
  292. }
  293. /**
  294. * TODO
  295. * @param $id
  296. * @return mixed
  297. * @author Qinii
  298. * @day 2020-06-18
  299. */
  300. public function delete($id)
  301. {
  302. if(!$this->repository->getUserDelExists($this->request->merId(),$id))
  303. return app('json')->fail('信息或状态错误');
  304. $this->repository->update($id,['is_system_del' => 1]);
  305. return app('json')->success('删除成功');
  306. }
  307. /**
  308. * TODO
  309. * @param $id
  310. * @return mixed
  311. * @author Qinii
  312. * @day 2020-06-18
  313. */
  314. public function markForm($id)
  315. {
  316. if(!$this->repository->getExistsById($this->request->merId(),$id))
  317. return app('json')->fail('数据不存在');
  318. return app('json')->success(formToData($this->repository->markForm($id)));
  319. }
  320. /**
  321. * TODO
  322. * @param $id
  323. * @return mixed
  324. * @author Qinii
  325. * @day 2020-06-18
  326. */
  327. public function mark($id)
  328. {
  329. if(!$this->repository->getExistsById($this->request->merId(),$id))
  330. return app('json')->fail('数据不存在');
  331. $this->repository->update($id,['mer_mark' => $this->request->param('mer_mark','')]);
  332. return app('json')->success('备注成功');
  333. }
  334. public function log($id)
  335. {
  336. list($page,$limit) = $this->getPage();
  337. $make = app()->make(StoreRefundStatusRepository::class);
  338. return app('json')->success($make->search($id,$page,$limit));
  339. }
  340. public function reList($id)
  341. {
  342. [$page,$limit] = $this->getPage();
  343. $make = app()->make(MerchantReconciliationRepository::class);
  344. if(!$make->getWhereCount(['mer_id' => $this->request->merId(),'reconciliation_id' => $id]))
  345. return app('json')->fail('数据不存在');
  346. $where = ['reconciliation_id' => $id,'type' => 1];
  347. return app('json')->success($this->repository->reconList($where,$page,$limit));
  348. }
  349. /**
  350. * TODO 快递查询
  351. * @param $id
  352. * @return mixed
  353. * @author Qinii
  354. * @day 2020-06-25
  355. */
  356. public function express($id)
  357. {
  358. // if(!$this->repository->getWhereCount(['refund_order_id' => $id,'status' =>2]))
  359. // return app('json')->fail('订单信息或状态错误');
  360. return app('json')->success($this->repository->express($id));
  361. }
  362. }