Activerefund.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 Activerefund extends Backend
  12. {
  13. /**
  14. * Activerefund模型对象
  15. * @var \app\admin\model\Activerefund
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Activerefund;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. $this->view->assign("refundstatusList", $this->model->getRefundstatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['active','activeorder','user'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. $row->getRelation('active')->visible(['title','desc']);
  52. $row->getRelation('activeorder')->visible(['order_sn']);
  53. $row->getRelation('user')->visible(['nickname','mobile']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 编辑
  62. */
  63. public function edit($ids = null) {
  64. $row = $this->model->get($ids);
  65. if (!$row) {
  66. $this->error(__('No Results were found'));
  67. }
  68. $adminIds = $this->getDataLimitAdminIds();
  69. if (is_array($adminIds)) {
  70. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  71. $this->error(__('You have no permission'));
  72. }
  73. }
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. if ($params) {
  77. $params = $this->preExcludeFields($params);
  78. if ($row['status'] != 0) {
  79. $this->error('该数据已经操作过');
  80. }
  81. if (isset($params['status']) && $params['status'] == 1) {
  82. //同意退款
  83. //查询活动状态
  84. $active_info = Db::name('active')->find($row['active_id']);
  85. if ($active_info['status'] == 2) {
  86. $this->error('活动已经结束,不能通过');
  87. }
  88. if ($active_info['status'] == 3) {
  89. $this->error('活动已经取消,不能通过');
  90. }
  91. //查询报名信息
  92. $active_order_info = Db::name('active_order')->find($row['order_id']);
  93. if ($active_order_info['status'] == 2) {
  94. $this->error('活动已经结束,不能通过');
  95. }
  96. if ($active_order_info['status'] == 3) {
  97. $this->error('活动已经取消,不能通过');
  98. }
  99. $params['refundstatus'] = 1;
  100. $params['updatetime'] = time();
  101. }
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. //修改信息
  106. if (isset($params['status'])) {
  107. if ($params['status'] == 1) {
  108. //同意退款
  109. //修改活动订单人数
  110. $active_rs = Db::name('active')->where(['id' => $row['active_id'], 'status' => $active_info['status'], 'currentperson' => $active_info['currentperson']])->setField('currentperson', $active_info['currentperson'] - $row['number']);
  111. if (!$active_rs) {
  112. Db::rollback();
  113. return ['code' => 0, 'msg' => '操作失败'];
  114. }
  115. //修改活动人员状态
  116. $people_rs = Db::name('active_people')->where(['order_id' => $row['order_id']])->setField(['status' => 3, 'modifystatus' => 0, 'updatetime' => time()]);
  117. if ($people_rs === false) {
  118. Db::rollback();
  119. return ['code' => 0, 'msg' => '操作失败'];
  120. }
  121. //构建活动订单数据
  122. $order_data = [
  123. 'status' => 3,
  124. 'updatetime' => time(),
  125. ];
  126. if ($row['refundprice'] > 0) {
  127. $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败
  128. $order_data['refundprice'] = $row['refundprice'];//退款金额
  129. $order_data['refundtime'] = time();
  130. if ($row['paytype'] == 0) {
  131. //余额支付, 退回余额
  132. $rs = create_log($row['refundprice'], '申请退款返还', $row['user_id'], 3, $row['order_id']);
  133. if ($rs != 1) {
  134. $order_data['refundstatus'] = 2;
  135. $params['refundstatus'] = 2;
  136. }
  137. } elseif ($row['paytype'] == 1) {
  138. //微信支付, 退回微信
  139. //扣除用户成长值
  140. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  141. $balance = floor($paygrowth * $row['price']);
  142. if ($balance > 0) {
  143. $paygrowth_rs = create_growth_log(-$balance, '申请退款扣除', $row['user_id'], 4, $row['order_id']);
  144. }
  145. //退款单号
  146. $order_data['refund_no'] = $row['refund_no'];
  147. //调用微信退款
  148. $wxData['transaction_id'] = $row['transaction_id'];
  149. $wxData['out_refund_no'] = $order_data['refund_no'];
  150. $wxData['total_fee'] = (int)($row['price'] * 100);//1 微信支付 单位为分
  151. $wxData['refund_fee'] = (int)($row['refundprice'] * 100);//1 微信支付 单位为分
  152. $wxData['refund_desc'] = '申请退款返还';
  153. // require_once("Plugins/WxPay/WxPay.php");
  154. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  155. $back = $wxPay->WxPayRefund($wxData);
  156. if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
  157. $order_data['refundstatus'] = 2;
  158. $params['refundstatus'] = 2;
  159. }
  160. }
  161. }
  162. //修改活动订单
  163. $order_rs = Db::name('active_order')->where(['id' => $row['order_id'], 'status' => $active_order_info['status']])->setField($order_data);
  164. if (!$order_rs) {
  165. Db::rollback();
  166. return ['code' => 0, 'msg' => '操作失败'];
  167. }
  168. //上级优惠券设为过期
  169. Db::name('user_coupon')->where(['order_id' => $row['order_id'], 'status' => 0])->setField('endtime', time() - 1);
  170. //发送消息
  171. $data = [
  172. 'user_id' => $row['user_id'],
  173. 'type' => 1,
  174. 'title' => '活动通知',
  175. 'content' => '您报名的' . $active_info['title'] . '活动申请退款已通过',
  176. 'createtime' => time()
  177. ];
  178. Db::name('sys_msg')->insertGetId($data);
  179. } elseif ($params['status'] == 2) {
  180. //拒绝, 发送消息
  181. $active_title = Db::name('active')->where(['id' => $row['active_id']])->value('title');
  182. $data = [
  183. 'user_id' => $row['user_id'],
  184. 'type' => 1,
  185. 'title' => '活动通知',
  186. 'content' => '您报名的' . $active_title . '活动申请退款已被拒绝',
  187. 'createtime' => time()
  188. ];
  189. Db::name('sys_msg')->insertGetId($data);
  190. }
  191. }
  192. //是否采用模型验证
  193. if ($this->modelValidate) {
  194. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  195. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  196. $row->validateFailException(true)->validate($validate);
  197. }
  198. $result = $row->allowField(true)->save($params);
  199. Db::commit();
  200. } catch (ValidateException $e) {
  201. Db::rollback();
  202. $this->error($e->getMessage());
  203. } catch (PDOException $e) {
  204. Db::rollback();
  205. $this->error($e->getMessage());
  206. } catch (Exception $e) {
  207. Db::rollback();
  208. $this->error($e->getMessage());
  209. }
  210. if ($result !== false) {
  211. $this->success();
  212. } else {
  213. $this->error(__('No rows were updated'));
  214. }
  215. }
  216. $this->error(__('Parameter %s can not be empty', ''));
  217. }
  218. $this->view->assign("row", $row);
  219. return $this->view->fetch();
  220. }
  221. }