6
0
Fork 0

结算管理者补贴

release
李静 2022-01-16 21:08:42 +08:00
parent 7e427008f5
commit d54431df80
5 changed files with 143 additions and 45 deletions

View File

@ -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
*

View File

@ -0,0 +1,7 @@
<?php
namespace App\Enums;
enum DealerManagerSubsidyStatus: int {
case Pending = 0;
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Models;
use App\Enums\DealerLvl;
use Illuminate\Database\Eloquent\Model;
class DealerManagerSubsidy extends Model
{
protected $attributes = [
'lvl' => 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',
];
}

View File

@ -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();

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDealerManagerSubsidiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('dealer_manager_subsidies', function (Blueprint $table) {
$table->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');
}
}