77 lines
2.6 KiB
PHP
77 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Business\InventoryWarehouseMappingAlerts;
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Carbon;
|
||
use Throwable;
|
||
|
||
class SyncInventoryWarehouseMappingAlerts extends Command
|
||
{
|
||
protected $signature = 'inventory:sync-warehouse-mapping-alert
|
||
{--month= : 指定月份(YYYY-MM),默认检查最新已激活的进销存月报}
|
||
{--company-id=1 : 提醒任务归属公司}';
|
||
|
||
protected $description = '检查进销存月报中未映射的仓库简称,并同步月度提醒任务';
|
||
|
||
public function handle(InventoryWarehouseMappingAlerts $alerts): int
|
||
{
|
||
$monthOption = trim((string) $this->option('month'));
|
||
$companyId = max(1, (int) $this->option('company-id'));
|
||
|
||
if ($monthOption !== '' && preg_match('/^\d{4}-\d{2}(?:-\d{2})?$/', $monthOption) !== 1) {
|
||
$this->error('月份格式应为 YYYY-MM。');
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
try {
|
||
$month = $monthOption !== ''
|
||
? Carbon::parse($monthOption)->startOfMonth()->toDateString()
|
||
: $alerts->latestMonth();
|
||
if ($month === null) {
|
||
$this->line('暂无已激活的进销存权威月报,未生成提醒任务。');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$result = $alerts->syncMonth($month, $companyId);
|
||
} catch (Throwable $exception) {
|
||
report($exception);
|
||
$this->error('进销存仓库简称提醒任务同步失败:' . $exception->getMessage());
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
if ($result['status'] === 'skipped' && $result['action'] === 'deleted') {
|
||
$this->warn(sprintf(
|
||
'%s 的仓库简称提醒任务已被人工删除,本次不重新创建。',
|
||
substr($result['month'], 0, 7),
|
||
));
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
if ($result['missing_pair_count'] > 0) {
|
||
$this->warn(sprintf(
|
||
'%s 有 %d 个仓储方/仓库组合未映射仓库简称,涉及 %d 行;提醒任务 #%d 已%s。',
|
||
substr($result['month'], 0, 7),
|
||
$result['missing_pair_count'],
|
||
$result['missing_row_count'],
|
||
$result['task_id'],
|
||
match ($result['action']) {
|
||
'created' => '创建',
|
||
'reopened' => '重新打开',
|
||
default => '更新',
|
||
},
|
||
));
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$this->info(substr($result['month'], 0, 7) . ' 的进销存仓库简称映射完整。');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|