From a70f4280adec3f2a7372e1a00e5bb4c8b347cd89 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Tue, 25 Jan 2022 15:23:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=8F=E9=94=80=E5=95=86?= =?UTF-8?q?=E6=8F=90=E7=8E=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Forms/Settings/Dealer.php | 9 +- .../Controllers/Dealer/UserController.php | 1 + .../Controllers/Dealer/WalletController.php | 121 ++++++++++++++++++ .../Http/Resources/Dealer/DealerResource.php | 3 + .../Resources/Dealer/WalletLogResource.php | 23 ++++ .../Dealer/WalletToBankLogResource.php | 27 ++++ .../Dealer/WalletToBankLogSimpleResource.php | 26 ++++ app/Endpoint/Api/routes.php | 5 + app/Enums/DealerWalletAction.php | 1 + app/Models/DealerWalletToBankLog.php | 64 +++++++++ app/Models/User.php | 9 ++ app/Providers/AppServiceProvider.php | 1 + ...reate_dealer_wallet_to_bank_logs_table.php | 40 ++++++ database/seeders/AppSettingSeeder.php | 2 + 14 files changed, 330 insertions(+), 2 deletions(-) create mode 100644 app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php create mode 100644 app/Endpoint/Api/Http/Resources/Dealer/WalletLogResource.php create mode 100644 app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogResource.php create mode 100644 app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogSimpleResource.php create mode 100644 app/Models/DealerWalletToBankLog.php create mode 100644 database/migrations/2022_01_25_135106_create_dealer_wallet_to_bank_logs_table.php diff --git a/app/Admin/Forms/Settings/Dealer.php b/app/Admin/Forms/Settings/Dealer.php index 0a33f5f7..9fdb755a 100644 --- a/app/Admin/Forms/Settings/Dealer.php +++ b/app/Admin/Forms/Settings/Dealer.php @@ -39,6 +39,9 @@ class Dealer extends Form public function form() { $this->currency('fee_rate', '手续费比例(百分比)')->symbol('%'); + $this->number('withdraw_threshold_amount', '起提金额(元)'); + $this->currency('withdraw_fee_rate', '提现费率')->symbol('%'); + $this->currency('upgrade_amount_'.DealerLvl::Contracted->value, '签约门槛')->symbol('¥'); $this->currency('upgrade_amount_'.DealerLvl::Special->value, '特邀门槛')->symbol('¥'); $this->currency('upgrade_amount_'.DealerLvl::Gold->value, '金牌门槛')->symbol('¥'); @@ -127,8 +130,10 @@ class Dealer extends Form { $dealerSettings = (array) Setting::where('key', 'dealer')->value('value'); return [ - 'fee_rate'=>$dealerSettings['fee_rate'] ?? '', - 'order_auto_allocate_times'=>$dealerSettings['order_auto_allocate_times'] ?? 1, + 'fee_rate'=> $dealerSettings['fee_rate'] ?? '', + 'withdraw_threshold_amount'=> $dealerSettings['withdraw_threshold_amount'] ?? 0, + 'withdraw_fee_rate'=> $dealerSettings['withdraw_fee_rate'] ?? 0, + 'order_auto_allocate_times'=> $dealerSettings['order_auto_allocate_times'] ?? 1, 'upgrade_amount_'.DealerLvl::Contracted->value => $dealerSettings['upgrade_amount_'.DealerLvl::Contracted->value] ?? '', 'upgrade_amount_'.DealerLvl::Special->value => $dealerSettings['upgrade_amount_'.DealerLvl::Special->value] ?? '', 'upgrade_amount_'.DealerLvl::Gold->value => $dealerSettings['upgrade_amount_'.DealerLvl::Gold->value] ?? '', diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/UserController.php b/app/Endpoint/Api/Http/Controllers/Dealer/UserController.php index f1a8f378..cb46a6b8 100644 --- a/app/Endpoint/Api/Http/Controllers/Dealer/UserController.php +++ b/app/Endpoint/Api/Http/Controllers/Dealer/UserController.php @@ -22,6 +22,7 @@ class UserController extends Controller return response()->json([ 'phone' => $user->phone, 'dealer'=> $user->dealer ? DealerResource::make($user->dealer) : [], + 'dealer_wallet' => $user->dealerWallet?->balance, 'user_info' => UserInfoResource::make($user->userInfo), ]); } diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php b/app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php new file mode 100644 index 00000000..690267e9 --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/Dealer/WalletController.php @@ -0,0 +1,121 @@ +user()->dealerWalletLogs() + ->latest('id') + ->simplePaginate($perPage)); + } + + /** + * 提现到银行 + * + * @return void + */ + public function walletToBank(Request $request, WalletService $walletService) + { + $input = $request->validate([ + 'amount' => ['bail', 'required', 'int', 'min:1'], + ]); + + $user = $request->user(); + $amount = Arr::get($input, 'amount', 0); + + if (is_null($user->dealer->pay_info)) { + throw new BizException('请先绑定设置收款信息'); + } + //校验是否关闭提现 + if (!$user->dealerWallet?->withdrawable) { + throw new BizException('可提账户已被限制提现'); + } + + // 校验提现门槛 + if (bcdiv($amount, 100, 2) < app_settings('dealer.withdraw_threshold_amount', 0)) { + throw new BizException('提现金额需大于'.app_settings('dealer.withdraw_threshold_amount', 0).'元'); + } + + try { + DB::beginTransaction(); + + $rate = app_settings('dealer.withdraw_fee_rate', '0.00');//手续费率 + // $rate = 0; + //计算手续费(四舍五入) + $serviceAmount = round(bcdiv($rate, 100, 4)*$amount); + + //生成提现记录 + $log = DealerWalletToBankLog::create([ + 'user_id' =>$user->id, + 'amount'=> $amount, + 'rate' => $rate, + 'service_amount' => $serviceAmount, + 'account_amount' => $amount-$serviceAmount, + ]); + + //减去用户可提金额 + $walletService->changeBalance($user, -$amount, DealerWalletAction::WithdrawBank, '提现', $log); + DB::commit(); + } catch (BizException $th) { + DB::rollBack(); + throw new BizException($th->getMessage()); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + throw new BizException('系统繁忙,请稍后再试'); + } + return response()->noContent(); + } + + /** + * 提现记录 + * + * @return void + */ + public function walletToBankLogs(Request $request) + { + $perPage = PaginatorHelper::resolvePerPage('per_page', 20, 50); + + $logs = $request->user()->dealerWalletToBankLogs() + ->latest('id') + ->simplePaginate($perPage); + + return WalletToBankLogSimpleResource::collection($logs); + } + + /** + * 提现记录详情 + * + * @param [type] $id + * @param Request $request + * @return void + */ + public function walletToBankLogShow($id, Request $request) + { + $log = $request->user()->dealerWalletToBankLogs()->findOrFail($id); + return WalletToBankLogResource::make($log); + } +} diff --git a/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php b/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php index 651ec61c..8c0fbcfe 100644 --- a/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php +++ b/app/Endpoint/Api/Http/Resources/Dealer/DealerResource.php @@ -2,6 +2,7 @@ namespace App\Endpoint\Api\Http\Resources\Dealer; +use App\Models\DealerWalletToBankLog; use Illuminate\Http\Resources\Json\JsonResource; class DealerResource extends JsonResource @@ -19,7 +20,9 @@ class DealerResource extends JsonResource 'lvl_name'=> $this->lvl_text, 'sale_values'=> $this->sale_values, //团队销售业绩基数 'guanli_values'=> $this->calculate_total_amount, //预计管理津贴 + 'team_sales_value' => $this->team_sales_value, 'pay_info'=>$this->pay_info ?: null, + 'can_withdraw'=> DealerWalletToBankLog::where('user_id', $this->user_id)->where('created_at', '>', now()->subDays(7))->doesntExist(), ]; } } diff --git a/app/Endpoint/Api/Http/Resources/Dealer/WalletLogResource.php b/app/Endpoint/Api/Http/Resources/Dealer/WalletLogResource.php new file mode 100644 index 00000000..1679192c --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/Dealer/WalletLogResource.php @@ -0,0 +1,23 @@ + $this->remarks, + 'created_at' => $this->created_at->format('y-m-d H:i'), + 'change_balance' => $this->change_balance, + ]; + } +} diff --git a/app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogResource.php b/app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogResource.php new file mode 100644 index 00000000..0a73d002 --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogResource.php @@ -0,0 +1,27 @@ + $this->amount, + 'service_amount' => $this->service_amount, + 'created_at' => $this->created_at->toDateTimeString(), + 'status' => $this->status, + 'remarks' => $this->remarks, + 'pay_info' => $this->getPayInfo(), + 'pay_image' => $this->pay_image, + ]; + } +} diff --git a/app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogSimpleResource.php b/app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogSimpleResource.php new file mode 100644 index 00000000..a55c2a31 --- /dev/null +++ b/app/Endpoint/Api/Http/Resources/Dealer/WalletToBankLogSimpleResource.php @@ -0,0 +1,26 @@ + $this->id, + 'amount' => $this->amount, + 'service_amount' => $this->service_amount, + 'created_at' => $this->created_at->toDateTimeString(), + 'status' => $this->status, + 'remarks' => $this->remarks, + ]; + } +} diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index e34bd2b1..b8754df0 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -260,5 +260,10 @@ Route::group([ Route::get('earnings/{earning}', [Dealer\EarningController::class, 'show']); Route::post('earnings/{earning}/pay', [Dealer\EarningController::class, 'payEarning']); Route::post('earnings/{earning}/confirm', [Dealer\EarningController::class, 'confirmEarning']); + //余额提现 + Route::get('wallet', [Dealer\WalletController::class, 'index']); + Route::post('wallet/withdraw', [Dealer\WalletController::class, 'walletToBank']); + Route::get('wallet/withdraw-logs', [Dealer\WalletController::class, 'walletToBankLogs']); + Route::get('wallet/withdraw-logs/{withdraw_log}', [Dealer\WalletController::class, 'walletToBankLogShow']); }); }); diff --git a/app/Enums/DealerWalletAction.php b/app/Enums/DealerWalletAction.php index a166ae6b..2aedb7d3 100644 --- a/app/Enums/DealerWalletAction.php +++ b/app/Enums/DealerWalletAction.php @@ -3,4 +3,5 @@ namespace App\Enums; enum DealerWalletAction: int { + case WithdrawBank = 4; } diff --git a/app/Models/DealerWalletToBankLog.php b/app/Models/DealerWalletToBankLog.php new file mode 100644 index 00000000..d2c8e8a7 --- /dev/null +++ b/app/Models/DealerWalletToBankLog.php @@ -0,0 +1,64 @@ +belongsTo(User::class); + } + + /** + * 待打款状态 + * + * @return boolean + */ + public function isPending() + { + return $this->status == self::STATUS_PENDING; + } + + /** + * 获取用户的打款信息 + * + * @return void + */ + public function getPayInfo() + { + if ($this->isPending()) {//待打款订单显示发货人收款信息 + $payInfo = $this->user->dealer->pay_info; + } else { + $payInfo = $this->pay_info; + } + return $payInfo ?: null; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 544a0205..a1f2734d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -170,6 +170,15 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac return $this->hasMany(DealerWalletLog::class, 'user_id'); } + /** + * + * 属于此用户的经销商余额提现日志 + */ + public function dealerWalletToBankLogs() + { + return $this->hasMany(DealerWalletToBankLog::class, 'user_id'); + } + /** * 经销商订购订单 * diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 22548093..56e48311 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -52,6 +52,7 @@ class AppServiceProvider extends ServiceProvider 'dealer_manage_subsidy' => \App\Models\DealerManageSubsidy::class, 'dealer_channel_subsidy_log' => \App\Models\DealerChannelSubsidyLog::class, 'dealer_purchase_subsidy' => \App\Models\DealerPurchaseSubsidy::class, + 'dealer_wallet_to_bank_log' => \App\Models\DealerWalletToBankLog::class, ]); JsonResource::withoutWrapping(); diff --git a/database/migrations/2022_01_25_135106_create_dealer_wallet_to_bank_logs_table.php b/database/migrations/2022_01_25_135106_create_dealer_wallet_to_bank_logs_table.php new file mode 100644 index 00000000..41913975 --- /dev/null +++ b/database/migrations/2022_01_25_135106_create_dealer_wallet_to_bank_logs_table.php @@ -0,0 +1,40 @@ +id(); + $table->unsignedBigInteger('user_id')->comment('用户ID'); + $table->unsignedDecimal('amount')->comment('提现金额'); + $table->unsignedDecimal('rate', 18, 2)->default(0)->comment('费率'); + $table->unsignedDecimal('service_amount')->default(0)->comment('手续费'); + $table->unsignedDecimal('account_amount')->comment('到账金额'); + $table->unsignedTinyInteger('status')->default(0)->comment('状态:0未处理,1成功,2失败'); + $table->text('pay_info')->nullable()->comment('收款信息'); + $table->string('pay_image')->nullable()->comment('打款凭证'); + $table->string('remarks')->nullable()->comment('备注'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('dealer_wallet_to_bank_logs'); + } +} diff --git a/database/seeders/AppSettingSeeder.php b/database/seeders/AppSettingSeeder.php index 0a6f873c..7da2863a 100644 --- a/database/seeders/AppSettingSeeder.php +++ b/database/seeders/AppSettingSeeder.php @@ -177,6 +177,8 @@ class AppSettingSeeder extends Seeder 'dealer' => [ 'value' => [ 'fee_rate' => '6', // 手续费比例(百分比) + 'withdraw_threshold_amount'=>'0', //提现门槛金额 + 'withdraw_fee_rate'=>'0.00', //提现手续费率 // 金牌经销商升级金额 'upgrade_amount_'.DealerLvl::Gold->value => '2520',