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