CaiWuXiTong/app/Console/Commands/SyncStoreReceiptPlantingDaily.php

114 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use App\Business\StoreReceiptPlantingDailySynchronizer;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Throwable;
class SyncStoreReceiptPlantingDaily extends Command
{
protected $signature = 'store-receipt:planting-daily
{--from=2026-10-01 : 标签明细覆盖核查起始日期 YYYY-MM-DD}
{--to= : 覆盖核查截止日期,默认昨日}
{--company=1 : 公司 ID}
{--month=* : 重算月份 YYYY-MM,可重复;默认当前月和上月}
{--dry-run : 只核查,不写库}
{--json : 输出 JSON}';
protected $description = '核查标签覆盖并刷新业绩明细的种菜金额和标签';
public function handle(StoreReceiptPlantingDailySynchronizer $synchronizer): int
{
$from = trim((string) $this->option('from'));
$to = trim((string) $this->option('to'));
$to = $to !== '' ? $to : Carbon::now('Asia/Shanghai')->subDay()->toDateString();
$companyId = (int) $this->option('company');
$months = array_values(array_filter(
array_map('strval', (array) $this->option('month')),
static fn (string $month): bool => trim($month) !== '',
));
try {
$result = $synchronizer->sync(
$from,
$to,
$companyId,
$months,
(bool) $this->option('dry-run'),
);
} catch (Throwable $exception) {
$this->outputResult([
'status' => 'failed',
'from' => $from,
'to' => $to,
'error' => $exception->getMessage(),
]);
return self::FAILURE;
}
if ((bool) $this->option('dry-run')) {
$this->outputResult($result);
return self::SUCCESS;
}
$results = [];
foreach ($result['months'] as $month) {
$exitCode = $this->call('store-receipt:order-match:sync', [
'month' => $month,
'--company' => $companyId,
'--planting-only' => true,
]);
$results[$month] = ['exit_code' => $exitCode];
if ($exitCode !== self::SUCCESS) {
$this->outputResult([
'status' => 'failed',
'from' => $from,
'to' => $to,
'months' => $result['months'],
'results' => $results,
'error' => $month . ' 种菜金额重算失败。',
]);
return self::FAILURE;
}
}
$result['status'] = 'ok';
$result['results'] = $results;
$this->outputResult($result);
return self::SUCCESS;
}
private function outputResult(array $result): void
{
if ((bool) $this->option('json')) {
$this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return;
}
if (($result['status'] ?? '') === 'failed') {
$this->error((string) ($result['error'] ?? '种菜金额同步失败。'));
return;
}
$this->info(sprintf(
'业绩明细种菜金额已刷新:核查 %s ~ %s,月份 %s。',
$result['from'],
$result['to'],
implode('、', $result['months']),
));
foreach ($result['planting_sync'] ?? [] as $month => $sync) {
$this->line(sprintf(
' %s 种菜报销明细:%d 单,写入 %d 行。',
$month,
(int) ($sync['orders'] ?? 0),
(int) ($sync['inserted'] ?? 0),
));
}
}
}