RefundOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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','status','refund_type','date','order_sn']);
  42. $where['mer_id'] = $this->request->merId();
  43. return app('json')->success($this->repository->getList($where,$page,$limit));
  44. }
  45. /**
  46. * @param $id
  47. * @return mixed
  48. * @author Qinii
  49. * @day 2020-06-12
  50. */
  51. public function detail($id)
  52. {
  53. if(!$this->repository->getExistsById($this->request->merId(),$id))
  54. return app('json')->fail('数据不存在');
  55. $data=$this->repository->getOne($id);
  56. return app('json')->success($data);
  57. }
  58. /**
  59. * @param $id
  60. * @return mixed
  61. * @author Qinii
  62. * @day 2020-06-12
  63. */
  64. public function switchStatus($id)
  65. {
  66. if(!$this->repository->getStatusExists($this->request->merId(),$id))
  67. return app('json')->fail('信息或状态错误');
  68. $status=-1;
  69. if($this->request->param('status') == 1){
  70. $status=1;
  71. }else if($this->request->param('status') == 2){
  72. $status=2;
  73. }
  74. // $status = ($this->request->param('status') == 1) ? 1 : -1;
  75. if($status == 1 || $status == 2){
  76. $refund_order=$this->repository->get($id);
  77. if($status==2){
  78. $part_refund_message = $this->request->param('part_refund_message','');
  79. $refund_price =(float) $this->request->param('part_refund_price',0);
  80. if($refund_price<0.01){
  81. return app('json')->fail('退款金额不能小于0.01');
  82. }
  83. $pay_price=Db::name('store_order')->where('order_id',$refund_order['order_id'])->value('pay_price');
  84. if($refund_order && $refund_price > $pay_price){
  85. return app('json')->fail('退款金额不能大于用户支付金额');
  86. }
  87. if($part_refund_message==''){
  88. return app('json')->fail('请填写商家备注');
  89. }
  90. $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')]);
  91. }
  92. $douduo = $this -> repository -> douduofund($id,true);
  93. if($douduo['code'] < 0){
  94. return app('json')->fail($douduo['message']);
  95. }
  96. if($douduo['code'] == 2){
  97. return app('json')->success($douduo['message']);
  98. }
  99. // $data = $this->request->params(['mer_delivery_user','mer_delivery_address','phone']);
  100. $data['mer_delivery_user']=$this->request->param('mer_refund_user','');
  101. $data['mer_delivery_address']=$this->request->param('mer_refund_address','');
  102. $data['phone']=$this->request->param('set_phone','');
  103. if($refund_order['refund_type']==2 && $status == 1 && ($data['mer_delivery_user']=='' || $data['mer_delivery_address']=='' || $data['phone']=='')){
  104. return app('json')->fail('请输入收货人信息');
  105. }
  106. $data['status'] = $status;
  107. $this->repository->agree($id,$data,$this->request->adminId(),1);
  108. // 获取订单金额
  109. $store_order_info = Db::name('store_order')
  110. ->where('order_id', $refund_order['order_id'])
  111. ->find();
  112. $group_order_map = Db::name('store_group_order')
  113. ->where('group_order_id', $store_order_info['group_order_id'])
  114. ->field('uid,pay_price,hf_pay_id,fzone_paytype')
  115. ->find();
  116. $make = '商贸区';
  117. if ($store_order_info['mer_id'] == 439) {
  118. $make = '精彩生活';
  119. } else if ($store_order_info['mer_id'] == 496) {
  120. $make = '会员专区';
  121. }
  122. // 如果是组合支付 现金 + 积分 则需要把积分退回
  123. // 参考 app\controller\api\Notify.php -- 745 $array_fzone_paytype['type']=='b2'
  124. $array_fzone_paytype = json_decode($group_order_map['fzone_paytype'], true);
  125. if (!empty($array_fzone_paytype)) {
  126. if ($array_fzone_paytype['type'] == 'b2') {
  127. // {"checked":true,"note":"现金+积分","other":1.5,"price":8.5,"total":0.0,"type":"b2","vr":"48799.82"}
  128. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('score');
  129. $vrpri += $array_fzone_paytype['other'];
  130. Db::name("user")->where("uid", $group_order_map['uid'])->update(['score' => $vrpri]);
  131. $data = [
  132. "uid" => $store_order_info['uid'],
  133. "reward_socre" => $array_fzone_paytype['other'],
  134. "addtime" => time(),
  135. "status" => 1,
  136. "order_id" => $store_order_info['order_id'],
  137. "mark" => "{$make}商品申请退款退回积分",
  138. "surplus" => $vrpri,
  139. "pm" => 1,
  140. "source_type" => 3
  141. ];
  142. Db::name("user_sign_score")->insert($data);//添加积分记录
  143. } else if ($array_fzone_paytype['type'] == 'a3') {
  144. // {"checked":true,"note":"现金+VR","other":1.5,"price":8.5,"total":0.0,"type":"a3","vr":"48799.82"}
  145. $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('vr');
  146. $vrpri += $array_fzone_paytype['other'];
  147. Db::name("user")->where("uid", $group_order_map['uid'])->update(['vr' => $vrpri]);
  148. //VR入账
  149. $insertdata = [
  150. 'uid'=> $store_order_info['uid'],
  151. 'number' => $array_fzone_paytype['other'],
  152. 'surplus' => $vrpri,
  153. 'mark' => "{$make}商品申请退款退回VR",
  154. 'order_id'=> $store_order_info['order_id'],
  155. 'type' => 2,
  156. 'addtime' => time(),
  157. 'settlement_time'=> date('Y-m-d H:i:s', time())
  158. ];
  159. Db::name('user_sign_vr')->insert($insertdata);
  160. }
  161. // else if ($array_fzone_paytype['type'] == 'c1') {
  162. // // {"checked":true,"note":"复购金","other":1.5,"price":8.5,"total":0.0,"type":"c1","vr":"48799.82"}
  163. // $vrpri = Db::name("user")->where("uid", $group_order_map['uid'])->value('fugou');
  164. // $vrpri += $array_fzone_paytype['other'];
  165. // Db::name("user")->where("uid", $group_order_map['uid'])->update(['fugou' => $vrpri]);
  166. // //支出账号存到log日志
  167. // $log_data = [
  168. // 'uid' => $store_order_info['uid'],
  169. // 'number' => $array_fzone_paytype['price'],
  170. // 'mark' => "复购区商品申请退款退回复购金:" . $array_fzone_paytype['price'],
  171. // 'desc' => "复购金",
  172. // 'order_id' => $store_order_info['order_id'],
  173. // 'surplus' => $vrpri,
  174. // 'order_type' => 0,
  175. // 'type' => 2,
  176. // 'addtime' => time()
  177. // ];
  178. // $user_res_log = Db::name("user_sign_fugou")->insert($log_data);
  179. // }
  180. }
  181. // 取消 订单所发放的福利
  182. $this->repository->cancelOrderBonus($id);
  183. }else{
  184. $fail_message = $this->request->param('fail_message','');
  185. if($status == -1 && empty($fail_message)){
  186. return app('json')->fail('未通过必须填写');
  187. }
  188. if($fail_message==''){
  189. return app('json')->fail('未通过必须填写');
  190. }
  191. $data['status'] = $status;
  192. $data['fail_message'] = $fail_message;
  193. $this->repository->refuse($id,$data);
  194. $this->repository->set_consult(2,$id);//协商记录
  195. }
  196. return app('json')->success('审核成功');
  197. }
  198. public function switchStatusjhm($id)
  199. {
  200. $this->repository->agreejhm($id);
  201. return app('json')->success('审核成功');
  202. }
  203. public function switchStatusnf($id)
  204. {
  205. $res = $this->repository->agreenf($id);
  206. return app('json')->success($res);
  207. }
  208. public function switchStatusqds($id)
  209. {
  210. $res = $this->repository->agreeqds($id);
  211. return app('json')->success($res);
  212. }
  213. /**
  214. * TODO 收货后确定退款
  215. * @param $id
  216. * @return mixed
  217. * @author Qinii
  218. * @day 2020-06-12
  219. */
  220. public function refundPrice($id)
  221. {
  222. if(!$this->repository->getRefundPriceExists($this->request->merId(),$id))
  223. return app('json')->fail('信息或状态错误');
  224. if($this->request->param('status') == 2){
  225. $part_refund_message = $this->request->param('part_refund_message','');
  226. $refund_price =(float) $this->request->param('part_refund_price',0);
  227. if($refund_price<0.01){
  228. return app('json')->fail('退款金额不能小于0.01');
  229. }
  230. $refund_order=$this->repository->get($id);
  231. $pay_price=Db::name('store_order')->where('order_id',$refund_order['order_id'])->value('pay_price');
  232. if($refund_price > $pay_price){
  233. return app('json')->fail('退款金额不能大于用户支付金额');
  234. }
  235. if($part_refund_message==''){
  236. return app('json')->fail('请填写商家备注');
  237. }
  238. $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')]);
  239. }else if($this->request->param('status') == -1){
  240. $fail_message = $this->request->param('fail_message','');
  241. if(empty($fail_message))
  242. return app('json')->fail('请输入拒绝原因');
  243. $data['status'] = -1;
  244. $data['fail_message'] = $fail_message;
  245. $this->repository->refuse($id,$data);
  246. $this->repository->set_consult(7,$id);//协商记录
  247. return app('json')->success('拒绝成功');
  248. }
  249. $this->repository->set_consult(5,$id);//协商记录
  250. $this->repository->adminRefund($id,$this->request->adminId());
  251. return app('json')->success('退款成功');
  252. }
  253. /**
  254. * TODO
  255. * @param $id
  256. * @return mixed
  257. * @author Qinii
  258. * @day 2020-06-12
  259. */
  260. public function switchStatusForm($id)
  261. {
  262. if(!$this->repository->getStatusExists($this->request->merId(),$id))
  263. return app('json')->fail('信息或状态错误');
  264. // $douduo = $this -> repository -> douduofund($id,false);
  265. // if($douduo['code'] < 0){
  266. // return app('json')->fail($douduo['message']);
  267. // }
  268. // if($douduo['code'] == 2){
  269. // return app('json')->success($douduo['message']);
  270. // }
  271. return app('json')->success(formToData($this->repository->statusForm($id)));
  272. }
  273. /**
  274. * TODO
  275. * @param $id
  276. * @return mixed
  277. * @author Qinii
  278. * @day 2020-06-18
  279. */
  280. public function delete($id)
  281. {
  282. if(!$this->repository->getUserDelExists($this->request->merId(),$id))
  283. return app('json')->fail('信息或状态错误');
  284. $this->repository->update($id,['is_system_del' => 1]);
  285. return app('json')->success('删除成功');
  286. }
  287. /**
  288. * TODO
  289. * @param $id
  290. * @return mixed
  291. * @author Qinii
  292. * @day 2020-06-18
  293. */
  294. public function markForm($id)
  295. {
  296. if(!$this->repository->getExistsById($this->request->merId(),$id))
  297. return app('json')->fail('数据不存在');
  298. return app('json')->success(formToData($this->repository->markForm($id)));
  299. }
  300. /**
  301. * TODO
  302. * @param $id
  303. * @return mixed
  304. * @author Qinii
  305. * @day 2020-06-18
  306. */
  307. public function mark($id)
  308. {
  309. if(!$this->repository->getExistsById($this->request->merId(),$id))
  310. return app('json')->fail('数据不存在');
  311. $this->repository->update($id,['mer_mark' => $this->request->param('mer_mark','')]);
  312. return app('json')->success('备注成功');
  313. }
  314. public function log($id)
  315. {
  316. list($page,$limit) = $this->getPage();
  317. $make = app()->make(StoreRefundStatusRepository::class);
  318. return app('json')->success($make->search($id,$page,$limit));
  319. }
  320. public function reList($id)
  321. {
  322. [$page,$limit] = $this->getPage();
  323. $make = app()->make(MerchantReconciliationRepository::class);
  324. if(!$make->getWhereCount(['mer_id' => $this->request->merId(),'reconciliation_id' => $id]))
  325. return app('json')->fail('数据不存在');
  326. $where = ['reconciliation_id' => $id,'type' => 1];
  327. return app('json')->success($this->repository->reconList($where,$page,$limit));
  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. }