链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
9.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Common\WxPay;
  4. use App\Admin\Repositories\LanzuCsWithdraw;
  5. use App\Models\FinancialRecord;
  6. use App\Models\ImsCjdcUser;
  7. use App\Models\LanzuCsInfo;
  8. use App\Models\LanzuUserBalance;
  9. use Dcat\Admin\Admin;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use App\Admin\Common\Type;
  14. use Dcat\Admin\Controllers\AdminController;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. use App\Models\LanzuCsWithdraw as modelCsWithdraw;
  18. class LanzuCsWithdrawController extends AdminController
  19. {
  20. /**
  21. * Make a grid builder.
  22. *
  23. * @return Grid
  24. */
  25. protected function grid()
  26. {
  27. return Grid::make(new LanzuCsWithdraw('csInfo'), function (Grid $grid) {
  28. $user = Admin::user();
  29. $grid->model()->orderBy('id', 'desc');
  30. if (!$user->isRole('lanzu_cs')) {//如果不是社区站点的角色登陆,则隐藏提现入口
  31. $grid->disableCreateButton();
  32. $grid->actions(function (Grid\Displayers\Actions $actions) use ($grid) {
  33. if ($actions->row->status != 0) {//状态一但改变,就不能编辑
  34. $actions->disableEdit();
  35. }
  36. });
  37. } else {
  38. $cid = LanzuCsInfo::where('admin_user_id', $user->id)->pluck('id')->first();
  39. $grid->model()->where('cs_id', $cid);
  40. $grid->disableEditButton();
  41. }
  42. $grid->disableViewButton();
  43. $grid->disableDeleteButton();
  44. $grid->id->sortable();
  45. $grid->column('csInfo.name', '提现用户');
  46. $grid->money;
  47. $grid->status('状态')->using([1 => '已同意', -1 => '已拒绝', 0 => '待审核', -2 => '提现失败'])->label([1 => 'success', -1 => 'danger', 0 => 'default']);
  48. $grid->is_pay('是否到账')->using(['否', '是'])->label(['danger', 'success',]);
  49. $grid->created_at;
  50. $grid->filter(function (Grid\Filter $filter) {
  51. $filter->like('csInfo.name', '提现用户');
  52. });
  53. });
  54. }
  55. /**
  56. * Make a show builder.
  57. *
  58. * @param mixed $id
  59. *
  60. * @return Show
  61. */
  62. protected function detail($id)
  63. {
  64. return Show::make($id, new LanzuCsWithdraw(), function (Show $show) {
  65. $show->id;
  66. $show->cs_id('提现用户');
  67. $show->money;
  68. $show->status('状态');
  69. $show->is_pay('是否到帐');
  70. $show->created_at;
  71. $show->updated_at;
  72. });
  73. }
  74. /**
  75. * Make a form builder.
  76. *
  77. * @return Form
  78. */
  79. protected function form()
  80. {
  81. return Form::make(new LanzuCsWithdraw('csInfo'), function (Form $form) {
  82. $form->display('id');
  83. $user = Admin::user();
  84. if ($form->isCreating()) {//如果是添加操作
  85. if ($user->isRole('lanzu_cs')) {//如果是社区站点角色
  86. $cs = LanzuCsInfo::where('admin_user_id', $user->id)->first();
  87. if ($cs) {
  88. $form->text('amount', '可提现金额')->value(LanzuUserBalance::getBalance($user->id, ))->disable();
  89. $form->hidden('cs_id', '提现用户id')->value($cs->id);
  90. $form->text('csInfo.name', '提现用户')->value($cs->name)->disable();
  91. $form->number('money')->min(env('MIN_MONEY'))->max(env('MAX_MONEY'));
  92. $form->saving(function (Form $form) use ($user) {
  93. //保存前校验提现金额是否符合申请条件
  94. if ($form->money < env('MIN_MONEY') || $form->money > env('MAX_MONEY')) {
  95. return $form->error('申请提现金额不得小于 ' . env('MIN_MONEY') . ' 元 或 不得大于 ' . env('MAX_MONEY') . ' 元.');
  96. }
  97. $res = LanzuUserBalance::checkBalance($user->id, $form->money);
  98. if (!$res) {
  99. return $form->error('您可提现金额不足!');
  100. }
  101. });
  102. }
  103. $form->disableEditingCheck();
  104. $form->disableViewCheck();
  105. }
  106. $form->saved(function () use ($cs, $form) {//扣减提现金额
  107. LanzuUserBalance::reduceBalance($cs->admin_user_id, Type::BALANCE_USER_TYPE_CS, $form->money);
  108. });
  109. } else {//编辑操作
  110. $aduid = LanzuCsInfo::where('id', $form->model()->cs_id)->pluck('admin_user_id')->first();
  111. $form->display('amount', '可提现金额')->value(LanzuUserBalance::getBalance($aduid, Type::BALANCE_USER_TYPE_CS));
  112. $form->display('cs_id', '提现用户')->value($form->model()->name);
  113. $form->display('money');
  114. if ($form->model()->status != 0) {//提现审核后 就能再编辑
  115. $form->radio('status', '状态')->options([1 => '同意', -1 => '拒绝'])->disable();
  116. } else {
  117. $form->radio('status', '状态')->options([1 => '同意', -1 => '拒绝']);
  118. }
  119. $form->saving(function (Form $form){
  120. if ($form->model()->status == -2){
  121. return $form->error('提现失败,请重新提交申请!');
  122. }elseif (!\Request::get('status')){
  123. return $form->error('请选择状态按钮!');
  124. }
  125. });
  126. $form->saved(function (Form $form) use ($aduid) {
  127. if ($form->status == -1) {//如何审核被拒绝,退回提现金额
  128. LanzuUserBalance::returnBalance($aduid, Type::BALANCE_USER_TYPE_CS, $form->model()->money);
  129. } elseif ($form->status == 1) {//调用微信企业付
  130. //获取站点信息
  131. $csInfo = LanzuCsInfo::find($form->model()->cs_id);
  132. if (!$csInfo) {
  133. return $form->error('社区站点不存在或已删除');
  134. }
  135. $data = [];
  136. $user = ImsCjdcUser::find($csInfo->user_id);
  137. $data['openid'] = $user->openid;
  138. $data['amount'] = $form->model()->money * 100;
  139. $data['partner_trade_no'] = uniqid();
  140. $data['re_user_name'] = $csInfo->name;
  141. $data['desc'] = $csInfo->name;
  142. $res = WxPay::pay($data);
  143. if ($res['result_code'] == "SUCCESS") {//更新到账状态
  144. DB::transaction(function () use ($csInfo, $form) {
  145. $csw = modelCsWithdraw::find($form->model()->id);
  146. $csw->is_pay = 1;
  147. $csw->save();
  148. //添加流水记录
  149. $model = FinancialRecord::getFinancialRecordModel($csInfo->admin_user_id);
  150. $model->user_id = $csInfo->admin_user_id;
  151. $model->user_type = Type::BALANCE_USER_TYPE_CS;
  152. $model->money = $form->model()->money;
  153. $model->money_type = Type::MONEY_TYPE_CS;
  154. $model->desc = '社区站点提现';
  155. $model->comment = '社区站点提现';
  156. $model->save();
  157. //添加公司流水
  158. $model = new \App\Models\LanzuFinancialRecord();
  159. $model->user_id = -1;
  160. $model->user_type = -1;
  161. $model->money = $form->model()->money;
  162. $model->money_type = Type::MONEY_TYPE_CS;
  163. $model->desc = '社区站点提现';
  164. $model->comment = '社区站点提现';
  165. $model->save();
  166. });
  167. } else {
  168. DB::transaction(function () use ($aduid, $form) {
  169. //提现失败,退回提现金额
  170. LanzuUserBalance::returnBalance($aduid, Type::BALANCE_USER_TYPE_CS, $form->model()->money);
  171. //更改提现状态为-2 提现失败
  172. \App\Models\LanzuCsWithdraw::updateStatus($form->model()->id, -2);
  173. });
  174. //记录失败日志
  175. $res['w_id'] = $form->model()->id;
  176. $res['time'] = date('Y-m-d H:i:s',time());
  177. $res['data'] = $data;
  178. Log::error('提现失败.', $res);
  179. return $form->error($res['err_code_des']);
  180. }
  181. }
  182. });
  183. $form->disableViewButton();
  184. $form->disableDeleteButton();
  185. $form->disableResetButton();
  186. $form->disableViewCheck();
  187. }
  188. });
  189. }
  190. }