Activerefund.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_data['currentperson'] = $active_info['currentperson'] - $row['number'];
  111. if ($active_info['currentperson'] - $row['number'] < $active_info['minperson'] && $active_info['status'] == 1) {
  112. $active_data['status'] = 0;
  113. }
  114. $active_rs = Db::name('active')->where(['id' => $row['active_id'], 'status' => $active_info['status'], 'currentperson' => $active_info['currentperson']])->setField($active_data);
  115. if (!$active_rs) {
  116. Db::rollback();
  117. return ['code' => 0, 'msg' => '操作失败'];
  118. }
  119. //修改活动人员状态
  120. $people_rs = Db::name('active_people')->where(['order_id' => $row['order_id']])->setField(['status' => 3, 'modifystatus' => 0, 'updatetime' => time()]);
  121. if ($people_rs === false) {
  122. Db::rollback();
  123. return ['code' => 0, 'msg' => '操作失败'];
  124. }
  125. //构建活动订单数据
  126. $order_data = [
  127. 'status' => 3,
  128. 'updatetime' => time(),
  129. ];
  130. if ($row['refundprice'] > 0) {
  131. $order_data['refundstatus'] = 1; //退款状态:0=无需退款,1=退款成功,2=退款失败
  132. $order_data['refundprice'] = $row['refundprice'];//退款金额
  133. $order_data['refundtime'] = time();
  134. if ($row['paytype'] == 0) {
  135. //余额支付, 退回余额
  136. $rs = create_log($row['refundprice'], '申请退款返还', $row['user_id'], 3, $row['order_id']);
  137. if ($rs != 1) {
  138. $order_data['refundstatus'] = 2;
  139. $params['refundstatus'] = 2;
  140. }
  141. } elseif ($row['paytype'] == 1) {
  142. //微信支付, 退回微信
  143. //扣除用户成长值
  144. $paygrowth = (int)config('site.paygrowth'); //每消费1元赠送成长值数量, 1元=?成长值
  145. $balance = floor($paygrowth * $row['price']);
  146. if ($balance > 0) {
  147. $paygrowth_rs = create_growth_log(-$balance, '申请退款扣除', $row['user_id'], 4, $row['order_id']);
  148. }
  149. //退款单号
  150. $order_data['refund_no'] = $row['refund_no'];
  151. //调用微信退款
  152. $wxData['transaction_id'] = $row['transaction_id'];
  153. $wxData['out_refund_no'] = $order_data['refund_no'];
  154. $wxData['total_fee'] = (int)($row['price'] * 100);//1 微信支付 单位为分
  155. $wxData['refund_fee'] = (int)($row['refundprice'] * 100);//1 微信支付 单位为分
  156. $wxData['refund_desc'] = '申请退款返还';
  157. // require_once("Plugins/WxPay/WxPay.php");
  158. $wxPay = new wxpay\WxPay(config('wxchatpay'));
  159. $back = $wxPay->WxPayRefund($wxData);
  160. if($back['return_code'] != 'SUCCESS' || $back['result_code'] != 'SUCCESS') {
  161. $order_data['refundstatus'] = 2;
  162. $params['refundstatus'] = 2;
  163. }
  164. }
  165. }
  166. //修改活动订单
  167. $order_rs = Db::name('active_order')->where(['id' => $row['order_id'], 'status' => $active_order_info['status']])->setField($order_data);
  168. if (!$order_rs) {
  169. Db::rollback();
  170. return ['code' => 0, 'msg' => '操作失败'];
  171. }
  172. //上级优惠券设为过期
  173. Db::name('user_coupon')->where(['order_id' => $row['order_id'], 'status' => 0])->setField('endtime', time() - 1);
  174. //发送消息
  175. $data = [
  176. 'user_id' => $row['user_id'],
  177. 'type' => 1,
  178. 'title' => '活动通知',
  179. 'content' => '您报名的' . $active_info['title'] . '活动申请退款已通过',
  180. 'createtime' => time()
  181. ];
  182. Db::name('sys_msg')->insertGetId($data);
  183. } elseif ($params['status'] == 2) {
  184. //拒绝, 发送消息
  185. $active_title = Db::name('active')->where(['id' => $row['active_id']])->value('title');
  186. $data = [
  187. 'user_id' => $row['user_id'],
  188. 'type' => 1,
  189. 'title' => '活动通知',
  190. 'content' => '您报名的' . $active_title . '活动申请退款已被拒绝',
  191. 'createtime' => time()
  192. ];
  193. Db::name('sys_msg')->insertGetId($data);
  194. }
  195. }
  196. //是否采用模型验证
  197. if ($this->modelValidate) {
  198. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  199. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  200. $row->validateFailException(true)->validate($validate);
  201. }
  202. $result = $row->allowField(true)->save($params);
  203. Db::commit();
  204. } catch (ValidateException $e) {
  205. Db::rollback();
  206. $this->error($e->getMessage());
  207. } catch (PDOException $e) {
  208. Db::rollback();
  209. $this->error($e->getMessage());
  210. } catch (Exception $e) {
  211. Db::rollback();
  212. $this->error($e->getMessage());
  213. }
  214. if ($result !== false) {
  215. $this->success();
  216. } else {
  217. $this->error(__('No rows were updated'));
  218. }
  219. }
  220. $this->error(__('Parameter %s can not be empty', ''));
  221. }
  222. $this->view->assign("row", $row);
  223. return $this->view->fetch();
  224. }
  225. }