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.
114 lines
3.7 KiB
114 lines
3.7 KiB
<?php
|
|
|
|
namespace App\AdminSupplier\Controllers;
|
|
|
|
use App\AdminSupplier\Repositories\WithdrawalBank;
|
|
use App\Common\StatementType;
|
|
use App\Models\Agent;
|
|
use App\Models\Supplier;
|
|
use App\Models\SystemSetting;
|
|
use App\Models\Withdrawal;
|
|
use App\Service\WithdrawalService;
|
|
use App\Traits\DemandTraits;
|
|
use App\Traits\WithdrawalTraits;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Repositories\EloquentRepository;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class WithdrawalBankController extends AdminController
|
|
{
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new WithdrawalBank(), function (Form $form) {
|
|
|
|
$auto = Withdrawal::query()
|
|
->with('pay')
|
|
->where([
|
|
'user_id' => Admin::user()->id,
|
|
'user_type' => DemandTraits::$col[1],
|
|
'status' => WithdrawalTraits::$state[3],
|
|
'pay_type' => WithdrawalTraits::$userType[1],
|
|
])
|
|
->orderByDesc('updated_at')
|
|
->first();
|
|
$form->display('id');
|
|
$min = SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[3]) ?? 0;
|
|
$form->decimal('price','提现金额')->rules('required|numeric|min:'.$min.'|not_in:0',[
|
|
'*' => '提现金额为必填字段且必须大于'.$min,
|
|
])
|
|
->default($auto->price ?? 0)
|
|
->help('含手续费' . SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[1]) . ' %,最小提现金额'.$min);
|
|
$form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
|
|
$form->text('card_number')->required()->maxLength(50)->type('number')->default($auto->pay->card_number ?? '');
|
|
$form->text('account_name')->required()->maxLength(50)->default($auto->pay->account_name ?? '');
|
|
$form->text('branch')->required()->maxLength(100)->default($auto->pay->branch ?? '');
|
|
$form->hidden('withdrawal_id');
|
|
|
|
$form->saving(function (Form $form) {
|
|
DB::beginTransaction();
|
|
try {
|
|
$user = Supplier::query()->where('id', Admin::user()->id)->lockForUpdate()->first();
|
|
//手续费
|
|
$cutPrice = bcmul($form->price, SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[1]), 6);
|
|
$cutPrice = $cutPrice > 0 ? bcdiv($cutPrice, 100, 6) : 0;
|
|
//总花费
|
|
$total = bcadd($form->price, $cutPrice,6);
|
|
if ($total > $user->balance) {
|
|
return $form->response()->error('余额不足,本次提现需花费'.bcadd($total,0,2).'(含手续费),当前可用余额为' . $user->balance);
|
|
}
|
|
//提现扣钱流水
|
|
$service = new WithdrawalService();
|
|
$service->create(
|
|
bcmul($form->price, -1, 6),
|
|
StatementType::WITHDRAWAL,
|
|
Admin::user()->id,
|
|
DemandTraits::$col[1]
|
|
);
|
|
//提现手续费流水
|
|
$service->create(
|
|
bcmul($cutPrice, -1, 6),
|
|
StatementType::WITHDRAWAL_CAT,
|
|
Admin::user()->id,
|
|
DemandTraits::$col[1]
|
|
);
|
|
|
|
$user->balance = bcsub($user->balance, $total, 6);
|
|
$user->save();
|
|
|
|
$withdrawal = new Withdrawal();
|
|
$withdrawal->user_id = Admin::user()->id;
|
|
$withdrawal->user_type = DemandTraits::$col[1];
|
|
$withdrawal->price = request('price', 0);
|
|
$withdrawal->cut_price = $cutPrice;
|
|
//$withdrawal->pay_id = $form->getKey();
|
|
$withdrawal->pay_type = WithdrawalTraits::$userType[1];
|
|
$withdrawal->save();
|
|
|
|
$form->withdrawal_id = $withdrawal->id;
|
|
$form->deleteInput('price');
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $form->response()->error($e->getMessage());
|
|
}
|
|
});
|
|
|
|
$form->saved(function (Form $form) {
|
|
$withdrawal = Withdrawal::find($form->withdrawal_id);
|
|
$withdrawal->pay_id = $form->getKey();
|
|
$withdrawal->save();
|
|
});
|
|
});
|
|
}
|
|
}
|