From d54431df808585ef470ba8bff8bbd9518c96c11e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Sun, 16 Jan 2022 21:08:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E7=AE=97=E7=AE=A1=E7=90=86=E8=80=85?= =?UTF-8?q?=E8=A1=A5=E8=B4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dealer/ManagerSubsidySettleCommand.php | 99 ++++++++++--------- app/Enums/DealerManagerSubsidyStatus.php | 7 ++ app/Models/DealerManagerSubsidy.php | 35 +++++++ app/Providers/AppServiceProvider.php | 1 + ..._create_dealer_manager_subsidies_table.php | 46 +++++++++ 5 files changed, 143 insertions(+), 45 deletions(-) create mode 100644 app/Enums/DealerManagerSubsidyStatus.php create mode 100644 app/Models/DealerManagerSubsidy.php create mode 100644 database/migrations/2022_01_16_200133_create_dealer_manager_subsidies_table.php diff --git a/app/Console/Commands/Dealer/ManagerSubsidySettleCommand.php b/app/Console/Commands/Dealer/ManagerSubsidySettleCommand.php index 1834a9d7..ba910d6b 100644 --- a/app/Console/Commands/Dealer/ManagerSubsidySettleCommand.php +++ b/app/Console/Commands/Dealer/ManagerSubsidySettleCommand.php @@ -2,10 +2,9 @@ namespace App\Console\Commands\Dealer; -use App\Enums\DealerEarningStatus; -use App\Enums\DealerEarningType; +use App\Enums\DealerManagerSubsidyStatus; use App\Models\Dealer; -use App\Models\DealerEarning; +use App\Models\DealerManagerSubsidy; use Illuminate\Console\Command; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; @@ -26,7 +25,7 @@ class ManagerSubsidySettleCommand extends Command * * @var string */ - protected $description = '结算管理者的管理津贴'; + protected $description = '结算上个月的管理者津贴'; /** * Execute the console command. @@ -38,7 +37,7 @@ class ManagerSubsidySettleCommand extends Command $tz = now(); $startAt = $tz->copy()->subMonthNoOverflow()->startOfMonth(); - $endAt = $startAt->copy()->endOfMonth(); + $endAt = $tz->copy()->subMonthNoOverflow()->endOfMonth(); $head = '【'.$startAt->format('Y/m/d').'-'.$endAt->format('Y/m/d').'】'; @@ -53,7 +52,7 @@ class ManagerSubsidySettleCommand extends Command if ($this->option('force')) { $this->info("{$head}正在删除旧数据..."); - DealerEarning::managerSubsidy()->where('start_at', $startAt)->where('end_at', $endAt)->delete(); + DealerManagerSubsidy::where('start_at', $startAt)->where('end_at', $endAt)->delete(); $this->info("{$head}数据删除成功"); } @@ -63,7 +62,7 @@ class ManagerSubsidySettleCommand extends Command $this->info("{$head}------------[结束]管理者津贴结算------------".PHP_EOL); - Cache::put($cacheKey, 1, $endAt->addMonthNoOverflow()); + Cache::put($cacheKey, 1, $tz->endOfMonth()); return 0; } @@ -89,45 +88,20 @@ class ManagerSubsidySettleCommand extends Command ->forPageAfterId($count, $lastId, 'id') ->get(); - $countDealers = $dealers->count(); + $dealersCount = $dealers->count(); - if ($countDealers == 0) { + if ($dealersCount == 0) { break; } + $tz = now()->toDateTimeString(); + $earnings = []; - $time = now()->toDateTimeString(); foreach ($dealers as $dealer) { - $salesLogs = $dealer->managerSalesLogs() - ->with(['product']) - ->select('product_id', DB::raw('sum(sales_volume) as sales_volume')) - ->whereBetween('order_completed_at', [$startAt, $endAt]) - ->groupBy('product_id') - ->get(); - - $description = ''; - $totalAmount = 0; - - foreach ($salesLogs as $log) { - $subsidy = bcmul($log->sales_volume, $log->product->manager_subsidy, 2); - - $totalAmount = bcadd($totalAmount, $subsidy, 2); - - if ($description !== '') { - $description .= "\n"; - } - - $description .= sprintf( - '【%s】销量: %s, 补贴金额: %s', - $log->product->name, - $log->sales_volume, - $subsidy - ); - } - - unset($salesLogs); + [$totalAmount, $remark] = $this->calculateTotalAmount($dealer, $startAt, $endAt); + // 如果 补贴总金额 > 0,则添加管理者补贴记录 if (bccomp($totalAmount, '0', 2) === 1) { // 计算手续费 $fee = bcmul($totalAmount, bcdiv($feeRate, '100', 10), 2); @@ -135,33 +109,68 @@ class ManagerSubsidySettleCommand extends Command $earnings[] = [ 'user_id' => $dealer->user_id, 'total_amount' => $totalAmount, + 'real_amount' => bcsub($totalAmount, $fee, 2), 'fee' => $fee, 'fee_rate' => $feeRate, - 'type' => DealerEarningType::ManagerSubsidy, 'start_at' => $startAt, 'end_at' => $endAt, 'lvl' => $dealer->lvl, 'is_manager' => $dealer->is_manager, - 'status' => DealerEarningStatus::Pending, - 'description' => $description, - 'created_at' => $time, - 'updated_at' => $time, + 'status' => DealerManagerSubsidyStatus::Pending, + 'remark' => $remark, + 'created_at' => $tz, + 'updated_at' => $tz, ]; } $lastId = $dealer->id; } - DealerEarning::insert($earnings); + DealerManagerSubsidy::insert($earnings); Cache::put($cacheKey, $lastId, $endAt->addMonthNoOverflow()); unset($dealers, $earnings); - } while ($countDealers == $count); + } while ($dealersCount == $count); Cache::forget($cacheKey); } + /** + * 计算补贴总金额 + * + * @param \App\Models\Dealer + * @param \Illuminate\Support\Carbon $startAt + * @param \Illuminate\Support\Carbon $endAt + * @return ARRAY + */ + protected function calculateTotalAmount(Dealer $dealer, Carbon $startAt, Carbon $endAt): array + { + $salesLogs = $dealer->managerSalesLogs() + ->with(['product']) + ->select('product_id', DB::raw('sum(sales_volume) as sales_volume')) + ->whereBetween('order_completed_at', [$startAt, $endAt]) + ->groupBy('product_id') + ->get(); + // 补贴总金额 + $totalAmount = 0; + // 备注信息 + $remark = ''; + + foreach ($salesLogs as $salesLog) { + $amount = bcmul($salesLog->sales_volume, $salesLog->product->manager_subsidy, 2); + $totalAmount = bcadd($totalAmount, $amount, 2); + + if ($remark !== '') { + $remark .= "\n"; + } + + $remark .= "【{$salesLog->product->name}】销量: {$salesLog->sales_volume}, 补贴金额: {$amount}"; + } + + return [$totalAmount, $remark]; + } + /** * 生成缓存的 key * diff --git a/app/Enums/DealerManagerSubsidyStatus.php b/app/Enums/DealerManagerSubsidyStatus.php new file mode 100644 index 00000000..36ce1864 --- /dev/null +++ b/app/Enums/DealerManagerSubsidyStatus.php @@ -0,0 +1,7 @@ + DealerLvl::None, + 'is_manager' => false, + ]; + + protected $casts = [ + 'lvl' => DealerLvl::class, + 'is_manager' => 'bool', + 'start_at' => 'datetime', + 'end_at' => 'datetime', + ]; + + protected $fillable = [ + 'user_id', + 'lvl', + 'is_manager', + 'total_amount', + 'real_amount', + 'fee', + 'fee_rate', + 'start_at', + 'end_at', + 'status', + 'remark', + ]; +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 6a84dfc0..9f18a523 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -45,6 +45,7 @@ class AppServiceProvider extends ServiceProvider 'distribution_pre_income' => \App\Models\DistributionPreIncome::class, 'admin_users' => \App\Models\Admin\Administrator::class, 'quota_v1_send_logs' => \App\Models\QuotaV1SendLog::class, + 'dealer_manager_subsidy' => \App\Models\DealerManagerSubsidy::class, ]); JsonResource::withoutWrapping(); diff --git a/database/migrations/2022_01_16_200133_create_dealer_manager_subsidies_table.php b/database/migrations/2022_01_16_200133_create_dealer_manager_subsidies_table.php new file mode 100644 index 00000000..02cdcc70 --- /dev/null +++ b/database/migrations/2022_01_16_200133_create_dealer_manager_subsidies_table.php @@ -0,0 +1,46 @@ +id(); + $table->unsignedBigInteger('user_id')->comment('经销商的用户ID'); + $table->tinyInteger('lvl')->comment('经销商等级'); + $table->boolean('is_manager')->default(false)->comment('是否是管理者'); + $table->unsignedDecimal('total_amount', 10, 2)->comment('总金额'); + $table->unsignedDecimal('real_amount', 10, 2)->comment('实际金额=总金额-手续费'); + $table->unsignedDecimal('fee', 10, 2)->default(0)->comment('手续费'); + $table->unsignedDecimal('fee_rate', 4, 2)->default(0)->comment('手续费率(百分比)'); + $table->timestamp('start_at')->comment('结算开始时间'); + $table->timestamp('end_at')->comment('结算结束时间'); + $table->tinyInteger('status')->default(0)->comment('状态: 0 待处理'); + $table->text('remark')->nullable()->comment('备注'); + $table->timestamps(); + + $table->unique(['user_id', 'start_at', 'end_at']); + $table->index(['start_at', 'end_at']); + $table->index('status'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('dealer_manager_subsidies'); + } +}