96 lines
2.8 KiB
PHP
96 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Forms;
|
|
|
|
use App\Enums\DealerEarningStatus;
|
|
use App\Enums\DealerWalletAction;
|
|
use App\Models\DealerChannelSubsidyLog;
|
|
use App\Models\DealerEarning;
|
|
use App\Models\DealerManagerSubsidy;
|
|
use App\Models\DealerManageSubsidy;
|
|
use App\Services\Dealer\WalletService;
|
|
use Carbon\Carbon;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class DealerEarningPay extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
/**
|
|
* @param Model|Authenticatable|HasPermissions|null $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
protected function authorize($user): bool
|
|
{
|
|
return $user->can('dcat.admin.dealer_earnings.pay');
|
|
}
|
|
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
$id = $this->payload['id'] ?? 0;
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
$earning = DealerEarning::findOrFail($id);
|
|
$earning->update([
|
|
'pay_way' => DealerEarning::PAY_WAY_WALLET,
|
|
'pay_info' => $earning->getPayInfo(),
|
|
'pay_image' => $input['pay_image'] ?? null,
|
|
'pay_at' => now(),
|
|
'status' => DealerEarningStatus::Completed,
|
|
]);
|
|
//打款到余额;
|
|
$walletService = new WalletService();
|
|
switch ($earning->earningable_type) {
|
|
case (new DealerManagerSubsidy())->getMorphClass():
|
|
$action = DealerWalletAction::ManagerSubsidyIn;
|
|
break;
|
|
case (new DealerManageSubsidy())->getMorphClass():
|
|
$action = DealerWalletAction::ManageSubsidyIn;
|
|
break;
|
|
case (new DealerChannelSubsidyLog())->getMorphClass():
|
|
$action = DealerWalletAction::ChannelSubsidyIn;
|
|
break;
|
|
default:
|
|
$action = DealerWalletAction::PurchaseSubsidyIn;
|
|
break;
|
|
}
|
|
|
|
$walletService->changeBalance($earning->user, $earning->total_earnings, $action, '收入-'.$earning->type_name, $earning);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
return $this->response()->error('操作失败:'.$th->getMessage());
|
|
}
|
|
|
|
return $this->response()
|
|
->success(__('admin.update_succeeded'))
|
|
->refresh();
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$this->image('pay_image')
|
|
->move('dealer-pay/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->autoUpload();
|
|
}
|
|
}
|