generated from liutk/owl-admin-base
102 lines
2.8 KiB
PHP
102 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\PlanStatus;
|
|
use App\Enums\TaskStatus;
|
|
use App\Models\Ledger;
|
|
use App\Models\PlanLedger;
|
|
use App\Models\Store;
|
|
use App\Models\TaskLedger;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class TaskLedgerGenerateCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'app:task-ledger-generate {date?}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '生成门店每天的总账录入任务';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$date = $this->argument('date');
|
|
|
|
$this->generateTasks(
|
|
$date ? Carbon::parse($date) : today()
|
|
);
|
|
}
|
|
|
|
protected function generateTasks(Carbon $datetime): void
|
|
{
|
|
/** @var \App\Models\PlanLedger */
|
|
$planable = DB::transaction(function () use ($datetime) {
|
|
/** @var \App\Models\PlanLedger */
|
|
$planable = PlanLedger::firstOrNew([
|
|
'date' => $datetime->format('Y-m-d'),
|
|
]);
|
|
|
|
if ($planable->exists) {
|
|
return $planable;
|
|
}
|
|
|
|
$planable->save();
|
|
|
|
$plan = $planable->plan()->create([
|
|
'name' => "【{$planable->date}】总账录入",
|
|
'plan_status' => PlanStatus::Published,
|
|
]);
|
|
|
|
return $planable->setRelation('plan', $plan);
|
|
});
|
|
|
|
$stores = Store::with(['master'])->get();
|
|
|
|
/** @var \App\Models\Store */
|
|
foreach ($stores as $store) {
|
|
DB::transaction(function () use ($store, $planable) {
|
|
$taskable = TaskLedger::firstOrNew([
|
|
'store_id' => $store->id,
|
|
'date' => $planable->date,
|
|
], [
|
|
'store_master_id' => $store->master?->id,
|
|
]);
|
|
|
|
if ($taskable->exists) {
|
|
return;
|
|
}
|
|
|
|
$taskable->save();
|
|
|
|
$ledger = Ledger::where('store_id', $store->id)
|
|
->where('date', $planable->date)
|
|
->first();
|
|
|
|
$date = Carbon::parse($planable->date);
|
|
|
|
$taskable->task()->create([
|
|
'plan_id' => $planable->plan->id,
|
|
'name' => '总账录入',
|
|
'start_at' => $date->copy()->startOfDay(),
|
|
'end_at' => $date->copy()->endOfDay(),
|
|
'task_status' => $ledger ? TaskStatus::Success : TaskStatus::Pending,
|
|
'completed_at' => $ledger?->created_at,
|
|
]);
|
|
});
|
|
}
|
|
}
|
|
}
|