Activeorder.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. use wxpay;
  6. /**
  7. * 活动报名管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Activeorder extends Backend
  12. {
  13. /**
  14. * Activeorder模型对象
  15. * @var \app\admin\model\Activeorder
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Activeorder;
  22. $this->view->assign("paytypeList", $this->model->getPaytypeList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("refundstatusList", $this->model->getRefundstatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. $active_id = input('active_id', 0, 'intval'); //活动id
  37. if (!$active_id) {
  38. $active_id = input('ids', 0, 'intval'); //从活动列表第一次跳转过来
  39. }
  40. $this->assignconfig('active_id', $active_id);
  41. //查询活动状态
  42. $active_status = Db::name('active')->where(['id' => $active_id])->value('status'); //状态:0=报名中,1=已成行,2=已结束,3=已取消
  43. $this->assignconfig('active_status', $active_status);
  44. //当前是否为关联查询
  45. $this->relationSearch = true;
  46. //设置过滤方法
  47. $this->request->filter(['strip_tags', 'trim']);
  48. if ($this->request->isAjax()) {
  49. //如果发送的来源是Selectpage,则转发到Selectpage
  50. if ($this->request->request('keyField')) {
  51. return $this->selectpage();
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $list = $this->model
  55. ->with(['active','user'])
  56. ->where(['active_id' => $active_id])
  57. ->where($where)
  58. ->order($sort, $order)
  59. ->paginate($limit);
  60. foreach ($list as $row) {
  61. $row->getRelation('active')->visible(['title','desc']);
  62. $row->getRelation('user')->visible(['nickname','mobile']);
  63. }
  64. $result = array("total" => $list->total(), "rows" => $list->items());
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 退款
  71. */
  72. public function refund() {
  73. $ids = input('ids', 0, 'intval'); //订单id
  74. if (!$ids) {
  75. $this->error('参数缺失');
  76. }
  77. $info = Db::name('active_order')->find($ids);
  78. if (!$info) {
  79. $this->error('订单不存在');
  80. }
  81. //查询活动
  82. $active_info = Db::name('active')->find($info['active_id']);
  83. if (!$active_info) {
  84. $this->error('活动不存在');
  85. }
  86. if ($active_info['status'] != 3) {
  87. $this->error('当前活动状态不能手动退款');
  88. }
  89. if ($info['status'] != 1 && $info['refundstatus'] != 2) {
  90. $this->error('当前订单状态不能手动退款');
  91. }
  92. //开启事务
  93. Db::startTrans();
  94. //修改活动人员状态
  95. $people_rs = Db::name('active_people')->where(['order_id' => $ids, 'status' => 1])->setField(['status' => 3, 'modifystatus' => 0, 'updatetime' => time()]);
  96. if ($people_rs === false) {
  97. Db::rollback();
  98. $this->error('操作失败');
  99. }
  100. //修改活动人员信息修改表
  101. $people_modify_rs = Db::name('active_people_modify')->where(['order_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  102. if ($people_modify_rs === false) {
  103. Db::rollback();
  104. $this->error('操作失败');
  105. }
  106. //修改活动申请取消表
  107. $refund_rs = Db::name('active_refund')->where(['order_id' => $ids, 'status' => 0])->setField(['status' => 2, 'updatetime' => time()]);
  108. if ($refund_rs === false) {
  109. Db::rollback();
  110. $this->error('操作失败');
  111. }
  112. //查询活动订单
  113. $active_order = Db::name('active_order'); //活动订单表
  114. $user_coupon = Db::name('user_coupon'); //用户优惠券表
  115. $sys_msg = Db::name('sys_msg'); //消息通知
  116. //活动订单修改信息
  117. $order_data['status'] = 3; //状态:0=待付款,1=待出行,2=已完成,3=已取消
  118. $order_data['updatetime'] = time();
  119. //退还支付金额
  120. if ($info['price'] > 0) {
  121. $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败
  122. $order_data['refundprice'] = $info['price'];//退款金额
  123. $order_data['refundtime'] = time();
  124. if ($info['paytype'] == 0) {
  125. //余额支付, 退回余额
  126. $rs = create_log($info['price'], '活动取消返还', $info['user_id'], 3, $info['id']);
  127. if ($rs != 1) {
  128. $order_data['refundstatus'] = 2;
  129. }
  130. } elseif ($info['paytype'] == 1) {
  131. //微信支付, 退回微信
  132. //扣除用户成长值
  133. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  134. $balance = floor($paygrowth * $info['price']);
  135. if ($balance > 0) {
  136. $paygrowth_rs = create_growth_log(-$balance, '活动取消扣除', $info['user_id'], 4);
  137. }
  138. //退款单号
  139. $order_data['refund_no'] = $info['order_sn'];
  140. //调用微信退款
  141. $wxData['transaction_id'] = $info['transaction_id'];
  142. $wxData['out_refund_no'] = $order_data['refund_no'];
  143. $wxData['total_fee'] = (int)($info['price'] * 100);//1 微信支付 单位为分
  144. $wxData['refund_fee'] = (int)($info['price'] * 100);//1 微信支付 单位为分
  145. $wxData['refund_desc'] = '活动取消返还';
  146. // require_once("Plugins/WxPay/WxPay.php");
  147. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  148. $back = $wxPay->WxPayRefund($wxData);
  149. if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
  150. $order_data['refundstatus'] = 2;
  151. }
  152. }
  153. }
  154. //修改活动订单状态
  155. $order_rs = $active_order->where(['id' => $info['id'], 'status' => $info['status'], 'refundstatus' => $info['refundstatus']])->setField($order_data);
  156. if ($order_rs === false) {
  157. Db::rollback();
  158. $this->error('操作失败');
  159. }
  160. //上级优惠券设为过期
  161. $user_coupon->where(['order_id' => $ids, 'status' => 0])->setField('endtime', time() - 1);
  162. //发送消息
  163. $data = [
  164. 'user_id' => $info['user_id'],
  165. 'type' => 1,
  166. 'title' => '活动通知',
  167. 'content' => '您报名的' . $active_info['title'] . '活动已取消',
  168. 'createtime' => time()
  169. ];
  170. $sys_msg->insertGetId($data);
  171. Db::commit();
  172. $this->success('退款成功');
  173. }
  174. }