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.
191 lines
8.3 KiB
191 lines
8.3 KiB
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Common\WxPay;
|
|
use App\Admin\Repositories\LanzuCsWithdraw;
|
|
use App\Models\FinancialRecord;
|
|
use App\Models\FinancialRecord0;
|
|
use App\Models\LanzuCsInfo;
|
|
use App\Models\LanzuUserBalance;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Controllers\AdminController;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Models\LanzuCsWithdraw as modelCsWithdraw;
|
|
|
|
class LanzuCsWithdrawController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new LanzuCsWithdraw('csInfo'), function (Grid $grid) {
|
|
$user = Admin::user();
|
|
$grid->model()->orderBy('id', 'desc');
|
|
if (!$user->isRole('lanzu_cs')) {//如果不是社区站点的角色登陆,则隐藏提现入口
|
|
$grid->disableCreateButton();
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) use ($grid) {
|
|
if ($actions->row->status != 0) {//状态一但改变,就不能编辑
|
|
$actions->disableEdit();
|
|
}
|
|
});
|
|
} else {
|
|
$cid = LanzuCsInfo::where('admin_user_id', $user->id)->pluck('id')->first();
|
|
$grid->model()->where('cs_id', $cid);
|
|
$grid->disableEditButton();
|
|
}
|
|
$grid->disableViewButton();
|
|
$grid->disableDeleteButton();
|
|
$grid->id->sortable();
|
|
$grid->column('csInfo.name', '提现用户');
|
|
$grid->money;
|
|
$grid->status('状态')->using([1 => '已同意', -1 => '已拒绝', 0 => '待审核', -2 => '提现失败'])->label([1 => 'success', -1 => 'danger', 0 => 'default']);
|
|
$grid->is_pay('是否到账')->using(['否', '是']);
|
|
$grid->created_at;
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$filter->like('csInfo.name', '提现用户');
|
|
});
|
|
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new LanzuCsWithdraw(), function (Show $show) {
|
|
$show->id;
|
|
$show->cs_id('提现用户');
|
|
$show->money;
|
|
$show->status('状态');
|
|
$show->is_pay('是否到帐');
|
|
$show->created_at;
|
|
$show->updated_at;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new LanzuCsWithdraw('csInfo'), function (Form $form) {
|
|
$form->display('id');
|
|
$user = Admin::user();
|
|
|
|
if ($form->isCreating()) {//如果是添加操作
|
|
if ($user->isRole('lanzu_cs')) {//如果是社区站点角色
|
|
$cs = LanzuCsInfo::where('admin_user_id', $user->id)->first();
|
|
if ($cs) {
|
|
$form->text('amount', '可提现金额')->value(LanzuUserBalance::getBalance($user->id, 3))->disable();
|
|
$form->hidden('cs_id', '提现用户id')->value($cs->id);
|
|
$form->text('csInfo.name', '提现用户')->value($cs->name)->disable();
|
|
$form->number('money')->min(0)->max(env('MAX_MONEY'));
|
|
$form->saving(function (Form $form) use ($user) {
|
|
//保存前校验提现金额是否符合申请条件
|
|
if ($form->money < env('MIN_MONEY') || $form->money > env('MAX_MONEY')) {
|
|
return $form->error('申请提现金额不得小于 ' . env('MIN_MONEY') . ' 元 或 不得大于 ' . env('MAX_MONEY') . ' 元.');
|
|
}
|
|
$res = LanzuUserBalance::checkBalance($user->id, $form->money);
|
|
if (!$res) {
|
|
return $form->error('您可提现金额不足!');
|
|
}
|
|
});
|
|
}
|
|
$form->disableEditingCheck();
|
|
$form->disableViewCheck();
|
|
}
|
|
|
|
$form->saved(function () use ($cs, $form) {//扣减提现金额
|
|
LanzuUserBalance::reduceBalance($cs->admin_user_id, 3, $form->money);
|
|
});
|
|
|
|
} else {//编辑操作
|
|
$aduid = LanzuCsInfo::where('id', $form->model()->cs_id)->pluck('admin_user_id')->first();
|
|
|
|
$form->display('amount', '可提现金额')->value(LanzuUserBalance::getBalance($aduid, 3));
|
|
$form->display('cs_id', '提现用户')->value($form->model()->name);
|
|
$form->display('money');
|
|
|
|
if ($form->model()->status != 0) {//提现审核后 就能再编辑
|
|
$form->radio('status', '状态')->options([1 => '同意', -1 => '拒绝'])->disable();
|
|
} else {
|
|
$form->radio('status', '状态')->options([1 => '同意', -1 => '拒绝'])->default(-1);
|
|
}
|
|
|
|
$form->saved(function (Form $form) use ($aduid) {
|
|
if ($form->status == -1) {//如何审核被拒绝,退回提现金额
|
|
LanzuUserBalance::returnBalance($aduid, 3, $form->model()->money);
|
|
} elseif ($form->status == 1) {//调用微信企业付
|
|
//获取站点信息
|
|
$csInfo = LanzuCsInfo::find($form->model()->cs_id);
|
|
if (!$csInfo){
|
|
return $form->error('社区站点不存在或已删除');
|
|
}
|
|
$res = WxPay::pay($csInfo, $form);
|
|
if ($res['result_code'] == "SUCCESS") {//更新到账状态
|
|
DB::transaction(function () use ($csInfo,$form) {
|
|
$csw = modelCsWithdraw::find($form->model()->id);
|
|
$csw->is_pay = 1;
|
|
$csw->save();
|
|
|
|
//添加流水记录
|
|
$model = FinancialRecord::getFinancialRecordModel($csInfo->admin_user_id);
|
|
$model->user_id = $csInfo->admin_user_id;
|
|
$model->user_type = 4;
|
|
$model->money = $form->model()->money;
|
|
$model->money_type = 104;
|
|
$model->desc = '社区站点提现';
|
|
$model->comment = '社区站点提现';
|
|
$model->save();
|
|
|
|
//添加公司流水
|
|
$model = new \App\Models\LanzuFinancialRecord();
|
|
$model->user_id = -1;
|
|
$model->user_type = -1;
|
|
$model->money = $form->model()->money;
|
|
$model->money_type = 104;
|
|
$model->desc = '社区站点提现';
|
|
$model->comment = '社区站点提现';
|
|
$model->save();
|
|
});
|
|
|
|
|
|
} else {
|
|
DB::transaction(function () use ($aduid,$form) {
|
|
//提现失败,退回提现金额
|
|
LanzuUserBalance::returnBalance($aduid, 3, $form->model()->money);
|
|
|
|
//更改提现状态为-2 提现失败
|
|
\App\Models\LanzuCsWithdraw::updateStatus($form->model()->id, -2);
|
|
});
|
|
|
|
//记录失败日志
|
|
Log::error('提现失败.', $res);
|
|
return $form->error($res['err_code_des']);
|
|
}
|
|
}
|
|
});
|
|
$form->disableViewButton();
|
|
$form->disableDeleteButton();
|
|
$form->disableResetButton();
|
|
$form->disableViewCheck();
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|