RefundOrder.php 14 KB

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