RefundOrder.php 5.2 KB

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