OrderReturn.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\store\controller;
  3. use EasyWeChat\Factory;
  4. use library\Controller;
  5. use think\Db;
  6. /**
  7. * 商城订单退款
  8. * Class Order
  9. * @package app\store\controller
  10. */
  11. class OrderReturn extends Controller
  12. {
  13. protected $table = 'OrderRefund';
  14. /**
  15. * 订单列表
  16. * @auth true
  17. * @menu true
  18. */
  19. public function index()
  20. {
  21. $query = $this->_query($this->table);
  22. $this->sh_status_desc = ['待审核','审核通过','审核拒绝'];
  23. $this->refund_state_desc = ['未退款','待退款','已退款','退款失败'];
  24. $where = [];
  25. if($this->request->request('tel'))$where[]= ['u.phone','like','%'.$this->request->request('tel').'%'];
  26. if($this->request->request('user_name'))$where[]= ['u.name','like','%'.$this->request->request('user_name').'%'];
  27. if($this->request->request('order_no')) $where[]= ['o.order_no','like','%'.$this->request->request('order_no').'%'];
  28. $this->sel_ck_status = $this->request->request('sel_ck_status',-1);
  29. $this->refund_state = $this->request->request('refund_state',-1);
  30. if($this->sel_ck_status > -1 ) $where[]= ['r.ck_status','=',$this->request->request('sel_ck_status')];
  31. if($this->refund_state > -1 ) $where[]= ['r.refund_state','=',$this->request->request('refund_state')];
  32. $query->alias('r')
  33. ->field('r.* , u.name as user_name ,u.phone,o.price_total,o.price_express,o.order_no')
  34. ->join('store_member u',' r.uid = u.id ','LEFT')
  35. ->join('store_order o',' o.id = r.order_id ','LEFT');
  36. if(!empty($where)) $query->where($where);
  37. $query ->order('r.id desc')->page();
  38. }
  39. /**
  40. * 订单详情
  41. * @auth true
  42. * @menu true
  43. */
  44. public function detail()
  45. {
  46. $this->title = '订单详情';
  47. $order_id = input('id');
  48. $refund_reason = Db::table('order_refund_cause')
  49. ->field('id,title')
  50. ->where(['status'=>1])
  51. ->order('sort desc , id asc')
  52. ->select();
  53. $detail = Db::table('order_refund')
  54. ->alias('r')
  55. ->field('r.* , u.name as user_name ,u.phone,o.price_total,o.price_express,o.order_no,o.pro_info')
  56. ->join('store_member u',' r.uid = u.id ','LEFT')
  57. ->join('store_order o',' o.id = r.order_id ','LEFT')
  58. ->where('r.id',$order_id)
  59. ->find();
  60. $this->assign('detail',$detail);
  61. $this->assign('refund_reason',array_column($refund_reason,null,'id'));
  62. $this->fetch('detail');
  63. }
  64. /**
  65. * 订单发货
  66. * @auth true
  67. * @menu true
  68. */
  69. public function deliver()
  70. {
  71. $this->title = '发货';
  72. $this->express_company = Db::table('store_express_company')->field('id,express_title')->select();
  73. $this->_form($this->table,'deliver');
  74. }
  75. /**
  76. * 表单数据处理
  77. * @auth true
  78. * @menu true
  79. * @param array $data
  80. */
  81. protected function _form_filter(&$data)
  82. {
  83. if ($this->request->isPost() && $this->request->action() == 'deliver') {
  84. $express_company = Db::table('store_express_company')->field('id,express_title')->find($data['express_company_id']);
  85. $data['express_company_title'] = $express_company['express_title'] ? $express_company['express_title'] : '';
  86. $data['express_send_at'] = date("Y-m-d H:i:s");
  87. $data['express_state'] = 1;
  88. }
  89. }
  90. public function aduit()
  91. {
  92. $id = input('post.id');
  93. $ck_status = input('post.ck_status',0);
  94. $refund_money = input('post.refund_money');
  95. $ck_remark = input('post.ck_remark');
  96. if ($ck_status == 1 && (!is_numeric($refund_money) || $refund_money < 0)) {
  97. echo 0;die();
  98. }
  99. Db::table('order_refund')->where(['id' => $id])->update(['ck_status' => $ck_status, 'refund_money' => $refund_money, 'ck_remark' => $ck_remark, 'ck_time' => date('Y-m-d H:i:s')]);
  100. echo 1;
  101. die();
  102. }
  103. public function refund()
  104. {
  105. $id = input('post.id');
  106. $refund_info = Db::table('order_refund')->find($id);
  107. $order_info = Db::table('store_order')->find($refund_info['order_id']);
  108. if ($order_info['price_total'] < $refund_info['apply_money']) $this->error('退款金额错误');
  109. $rufund_success = 0;
  110. Db::startTrans();
  111. try {
  112. $app = Factory::payment(config('app.wx_pay'));
  113. $result = $app->refund->byOutTradeNumber($order_info['pay_no'], $order_info['refund_no'], $order_info['price_total']*100, $refund_info['amount']*100, [
  114. 'refund_desc' => '订单退款',
  115. ]);
  116. if($result['return_result']=='SUCCESS' && $result['result_code']=='SUCCESS') $rufund_success = 1;
  117. $return_json=json_encode($result);
  118. if ($rufund_success == 1) {
  119. Db::commit();
  120. $this->success('退款成功!');
  121. } else {
  122. Db::rollback();// 回滚事务
  123. $this->error('退款失败1');
  124. }
  125. }catch (\UnexpectedValueException $e) {
  126. Db::rollback();// 回滚事务
  127. $this->error('退款失败');
  128. }
  129. }
  130. }