RefundOrder.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\order\controller;
  3. use app\common\model\GoodsOrder;
  4. use app\common\model\GoodsOrderItem;
  5. use app\common\model\GoodsOrderRefund;
  6. use app\common\model\OrderRefundCase;
  7. use app\common\model\StoreOrderRefund;
  8. use app\common\model\UserMessage;
  9. use app\common\service\OrderCallback;
  10. use EasyWeChat\Factory;
  11. use library\Controller;
  12. use think\Db;
  13. /**
  14. * 商品订单
  15. * Class CustomOrder
  16. * @package app\order\controller
  17. */
  18. class RefundOrder extends Controller
  19. {
  20. protected $table = 'StoreOrderRefund';
  21. /**
  22. * 订单列表
  23. * @auth true
  24. * @menu true
  25. * @throws \think\Exception
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. * @throws \think\exception\PDOException
  30. */
  31. public function index()
  32. {
  33. $this->title = '订单管理';
  34. $this->order_status = ['待支付','已支付'];
  35. $this->all_pay_type = all_pay_type();
  36. $query = $this->_query($this->table);
  37. $where = [];
  38. $where[] = ['o.is_deleted','=',0];
  39. $where[] = ['r.order_type','=',2];
  40. $where[] = ['r.status','<>',5];
  41. if($this->request->request('tel'))$where[]= ['u.phone','like','%'.$this->request->request('tel').'%'];
  42. if($this->request->request('order_no'))$where[]= ['o.order_no','like','%'.$this->request->request('order_no').'%'];
  43. if($this->request->request('user_name'))$where[]= ['u.name','like','%'.$this->request->request('user_name').'%'];
  44. $query->alias('r')
  45. ->field('r.express_time,r.consignee_add,r.consignee_name,r.consignee_tel,r.express_company,r.express_no,r.id as rid,refund_type,r.create_at as rtime,r.status as rstatus,r.express_info,r.order_id,r.refund_money, u.name ,u.phone as user_phone,u.headimg, o.*')
  46. ->join('StoreMember u','r.user_id = u.id ','LEFT')
  47. ->join('StoreOrder o',' o.id = r.order_id ','LEFT');
  48. if(!empty($where)) $query->where($where);
  49. $query ->order('o.id desc')->page();
  50. }
  51. /**
  52. * 订单列表处理
  53. * @param array $data
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. * @throws \think\exception\DbException
  57. */
  58. protected function _index_page_filter(array &$data)
  59. {
  60. foreach ($data as &$vo) {
  61. $vo['item_list'] = GoodsOrderItem::field('i.num,i.sell_price,g.name,i.goods_spec')
  62. ->alias('i')
  63. ->where(['i.order_id'=>$vo['id']])
  64. ->leftJoin('StoreGoods g','g.id = i.goods_id')
  65. ->select();
  66. }
  67. }
  68. /**
  69. * 订单审核
  70. * @auth true
  71. * @menu true
  72. * @throws \think\Exception
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. * @throws \think\exception\PDOException
  77. */
  78. public function audit()
  79. {
  80. $this->title = '审核';
  81. $this->_form($this->table);
  82. }
  83. /**
  84. * 表单数据处理
  85. * @auth true
  86. * @menu true
  87. * @param array $data
  88. */
  89. protected function _form_filter(&$data)
  90. {
  91. if($this->request->isGet() && $this->request->action() == 'audit'){
  92. $this->order_info = GoodsOrder::where('id',$data['order_id'])->find()->toArray();
  93. $data['apply_case_title'] = OrderRefundCase::where('id',$data['apply_case'])->value('title');
  94. }
  95. if($this->request->isPost() && $this->request->action() == 'audit'){
  96. if($data['price_total'] < $data['refund_money']) $this->error('退款金额不能超过支付金额');
  97. }
  98. }
  99. protected function _form_result($id)
  100. {
  101. if($this->request->isPost() && $this->request->action() == 'audit'){
  102. $refund_info = StoreOrderRefund::where('id',$id)->find()->toArray();
  103. $status = input('post.status');
  104. $sh_status = 1;
  105. switch ($status){
  106. case 1:
  107. $sh_status = 3;
  108. break;
  109. case 2:
  110. $sh_status = 2;
  111. break;
  112. case 3:
  113. $sh_status = 3;
  114. break;
  115. }
  116. if($sh_status > 1){
  117. GoodsOrder::where('id',$refund_info['order_id'])->update(['refund_state'=>$sh_status]);
  118. UserMessage::sendUserMessage($refund_info['user_id'],'mall',1,0,0,$refund_info['order_id'],'您的退款申请已通过审核',$refund_info['order_id']);
  119. }
  120. if($status == 3) {
  121. $res = OrderCallback::refundMoney($refund_info,[],$refund_info['order_id']);
  122. if($res['code'] != 200) StoreOrderRefund::where('id',$id)->update(['status'=>4]);
  123. $res['code'] == 200 ? $this->success($res['msg']):$this->error($res['msg']);
  124. }
  125. }
  126. }
  127. public function refund()
  128. {
  129. if($this->request->isPost()){
  130. $id = input('post.id');
  131. $refund_info = GoodsOrderRefund::where('id',$id)->find()->toArray();
  132. $order_info = GoodsOrder::where('id',$refund_info['order_id'])->find()->toArray();
  133. if($refund_info['status'] == 3) $this->error('已退款');
  134. if($refund_info['status'] == 5) $this->error('申请已取消');
  135. if ($order_info['price_total'] < $refund_info['refund_money'])return $this->error('退款金额错误');
  136. $res = OrderCallback::refundMoney($refund_info,$order_info,$refund_info['order_id']);
  137. $res['code'] == 200 ? $this->success($res['msg']):$this->error($res['msg']);
  138. }
  139. }
  140. }