6
0
Fork 0

经销商个人业绩和团队业绩

release
李静 2022-01-19 19:59:28 +08:00
parent 481a220fe1
commit 1367a22fec
8 changed files with 225 additions and 22 deletions

View File

@ -4,12 +4,15 @@ namespace App\Console\Commands\Dealer;
use App\Enums\DealerOrderSettleState;
use App\Enums\DealerOrderStatus;
use App\Enums\DealerSalesValueLogType;
use App\Models\Dealer;
use App\Models\DealerChannelSubsidyLog;
use App\Models\DealerEarning;
use App\Models\DealerManagerSalesLog;
use App\Models\DealerManageSubsidyLog;
use App\Models\DealerOrder;
use App\Models\DealerPurchaseLog;
use App\Models\DealerSalesValueLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
@ -41,6 +44,8 @@ class OrderSettleCommand extends Command
'status' => DealerOrderStatus::Completed,
'settle_state' => DealerOrderSettleState::Processed,
])->whereNotNull('shippinged_time')->chunkById(200, function ($orders) {
$orders->load('dealer.userInfo');
foreach ($orders as $order) {
try {
DB::beginTransaction();
@ -62,45 +67,42 @@ class OrderSettleCommand extends Command
/**
* 处理经销商订单
*
* @param \App\Models\DealerOrder $dealerOrder
* @param \App\Models\DealerOrder $order
* @return void
*/
protected function handleDealerOrder(DealerOrder $dealerOrder)
protected function handleDealerOrder(DealerOrder $order)
{
$this->handleManagerSalesLogs($dealerOrder);
$this->handleManagerSalesLogs($order);
$this->handleManageSubsidyLogs($dealerOrder);
$this->handleManageSubsidyLogs($order);
$this->handleChannelSubsidyLogs($dealerOrder);
$this->handleChannelSubsidyLogs($order);
$this->handlePurchaseLogs($dealerOrder);
$this->handlePurchaseLogs($order);
// 将订单标记为已完成
$dealerOrder->forceFill([
'settle_state' => DealerOrderSettleState::Completed,
])->save();
$this->handleOrder($order);
}
protected function handleManagerSalesLogs(DealerOrder $dealerOrder)
protected function handleManagerSalesLogs(DealerOrder $order)
{
DealerManagerSalesLog::where('order_id', $dealerOrder->id)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
DealerManagerSalesLog::where('order_id', $order->id)->update([
'order_completed_at' => $order->shippinged_time,
]);
}
protected function handleManageSubsidyLogs(DealerOrder $dealerOrder)
protected function handleManageSubsidyLogs(DealerOrder $order)
{
DealerManageSubsidyLog::where('order_id', $dealerOrder->id)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
DealerManageSubsidyLog::where('order_id', $order->id)->update([
'order_completed_at' => $order->shippinged_time,
]);
}
protected function handleChannelSubsidyLogs(DealerOrder $dealerOrder)
protected function handleChannelSubsidyLogs(DealerOrder $order)
{
$channelSubsidyLogIds = DealerChannelSubsidyLog::where('order_id', $dealerOrder->id)->get('id')->toArray();
$channelSubsidyLogIds = DealerChannelSubsidyLog::where('order_id', $order->id)->get('id')->toArray();
DealerChannelSubsidyLog::whereIn('id', $channelSubsidyLogIds)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
'order_completed_at' => $order->shippinged_time,
]);
DealerEarning::where('earningable_type', 'dealer_channel_subsidy_log')->whereIn('earningable_id', $channelSubsidyLogIds)->update([
@ -108,10 +110,68 @@ class OrderSettleCommand extends Command
]);
}
protected function handlePurchaseLogs(DealerOrder $dealerOrder)
protected function handlePurchaseLogs(DealerOrder $order)
{
DealerPurchaseLog::where('order_id', $dealerOrder->id)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
DealerPurchaseLog::where('order_id', $order->id)->update([
'order_completed_at' => $order->shippinged_time,
]);
}
protected function handleOrder(DealerOrder $order)
{
$salesValue = $order->total_amount;
if (bccomp($salesValue, '0', 2) < 1) {
return;
}
$salesValueLogs = [];
if (bccomp($salesValue, '0', 2) === 1) {
$ts = now()->toDateTimeString();
$order->dealer->update([
'self_sales_value' => DB::raw("self_sales_value+{$salesValue}"),
]);
$salesValueLogs[] = [
'user_id' => $order->user_id,
'loggable_type' => $order->getMorphClass(),
'loggable_id' => $order->id,
'type' => DealerSalesValueLogType::Personal,
'change_sales_value' => $salesValue,
'remarks' => '个人进货',
'created_at' => $ts,
'updated_at' => $ts,
];
if (count($pids = $order->dealer->userInfo->parent_ids) > 0) {
foreach ($order->dealer->userInfo->parent_ids as $pid) {
$salesValueLogs[] = [
'user_id' => $pid,
'loggable_type' => $order->getMorphClass(),
'loggable_id' => $order->id,
'type' => DealerSalesValueLogType::Team,
'change_sales_value' => $salesValue,
'remarks' => '团队成员进货',
'created_at' => $ts,
'updated_at' => $ts,
];
}
// 更新上级的团队团队业绩
Dealer::whereIn('user_id', $pids)->update([
'team_sales_value' => DB::raw("team_sales_value + {$salesValue}"),
]);
}
}
// 保存销售值日志
DealerSalesValueLog::insert($salesValueLogs);
// 将订单标记未已结算
$order->forceFill([
'settle_state' => DealerOrderSettleState::Completed,
])->save();
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace App\Enums;
enum DealerSalesValueLogType: int {
case Personal = 1;
case Team = 2;
}

View File

@ -15,6 +15,8 @@ class Dealer extends Model
'lvl' => DealerLvl::None,
'is_sale' => false,
'is_manager' => false,
'self_sales_value' => 0,
'team_sales_value' => 0,
];
protected $casts = [
@ -33,6 +35,8 @@ class Dealer extends Model
'pay_info',
'contracted_lvl_at',
'bonds',
'self_sales_value',
'team_sales_value',
];
public function user()

View File

@ -0,0 +1,26 @@
<?php
namespace App\Models;
use App\Enums\DealerSalesValueLogType;
use Illuminate\Database\Eloquent\Model;
class DealerSalesValueLog extends Model
{
protected $attributes = [
'type' => DealerSalesValueLogType::Personal,
];
protected $casts = [
'type' => DealerSalesValueLogType::class,
];
protected $fillable = [
'user_id',
'loggable_type',
'loggable_id',
'type',
'change_sales_value',
'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_order' => \App\Models\DealerOrder::class,
'dealer_manager_subsidy' => \App\Models\DealerManagerSubsidy::class,
'dealer_manage_subsidy' => \App\Models\DealerManageSubsidy::class,
'dealer_channel_subsidy_log' => \App\Models\DealerChannelSubsidyLog::class,

View File

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDealerSalesValueLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('dealer_sales_value_logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id')->comment('用户ID');
$table->string('loggable_type')->nullable()->comment('日志关联对象的类型');
$table->unsignedBigInteger('loggable_id')->nullable()->comment('日志关联对象的ID');
$table->tinyInteger('type')->default(1)->comment('类型: 1 个人, 2 团队');
$table->decimal('change_sales_value', 18, 2)->default(0.00)->comment('变更销售值');
$table->string('remark')->nullable()->comment('备注');
$table->timestamps();
$table->index(['user_id', 'type']);
$table->index(['loggable_type', 'loggable_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dealer_sales_value_logs');
}
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddSelfSalesValueToDealersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dealers', function (Blueprint $table) {
$table->unsignedDecimal('self_sales_value', 18, 2)->default(0)->comment('个人销售值');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dealers', function (Blueprint $table) {
$table->dropColumn('self_sales_value');
});
}
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddTeamSalesValueToDealersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dealers', function (Blueprint $table) {
$table->unsignedDecimal('team_sales_value', 18, 2)->default(0)->comment('个人销售值');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dealers', function (Blueprint $table) {
$table->dropColumn('team_sales_value');
});
}
}