diff --git a/app/Business/SupplierSummaryInboundImporter.php b/app/Business/SupplierSummaryInboundImporter.php new file mode 100644 index 00000000..b7fba151 --- /dev/null +++ b/app/Business/SupplierSummaryInboundImporter.php @@ -0,0 +1,934 @@ +> */ + private const REQUIRED_HEADERS = [ + 'source_no' => ['来源单号'], + 'document_no' => ['单据编号(采购单号)', '单据编号(采购单号)', '单据编号'], + 'supplier' => ['供应商'], + 'item_no' => ['货号'], + 'barcode' => ['条码'], + 'spec_name' => ['规格名'], + 'quantity' => ['库存变动量'], + 'unit_price' => ['单价'], + 'amount' => ['金额'], + 'estimated_unit_price' => ['预估单价'], + 'estimated_amount' => ['预估金额'], + 'supplier_barcode' => ['供应商条码'], + 'remark' => ['备注'], + 'warehouse' => ['仓库'], + ]; + + /** @var array> */ + private const OPTIONAL_HEADERS = [ + 'express_name' => ['快递名称'], + 'express_no' => ['快递单号'], + 'not_sent' => ['未发出'], + 'owner_name' => ['负责人'], + ]; + + /** + * @return array{ + * status:string, + * report_month:string, + * source_filename:string, + * success:int, + * duplicates:int, + * failed:int, + * mapped:int, + * total_in_table:int, + * failure_reason_count:int, + * failure_reasons:list + * } + */ + public function import(string $file, string $reportMonth): array + { + $path = realpath($file); + if ($path === false || ! is_file($path) || ! is_readable($path)) { + throw new RuntimeException('供应商汇总文件不存在或不可读取。'); + } + + $month = $this->normalizeMonth($reportMonth); + $connection = (string) config('upstream_mirrors.connection', 'business_mirror'); + $maps = $this->shortNameMap(); + + [$reader, $sharedStrings] = $this->streamDetailSheet($path); + + try { + $success = 0; + $duplicates = 0; + $mapped = 0; + $failed = 0; + $reasons = []; + $seen = []; + $batch = []; + + DB::connection($connection)->transaction(function () use ( + $connection, + $reader, + $sharedStrings, + $maps, + $month, + &$success, + &$duplicates, + &$mapped, + &$failed, + &$reasons, + &$seen, + &$batch, + ): void { + $flush = function () use ( + $connection, + $month, + &$success, + &$duplicates, + &$batch, + ): void { + if ($batch === []) { + return; + } + + $keys = array_column($batch, 'row_key'); + $existing = DB::connection($connection) + ->table(self::TABLE) + ->where('report_month', $month) + ->whereIn('row_key', $keys) + ->pluck('row_key') + ->map(static fn (mixed $key): string => (string) $key) + ->all(); + $existing = array_fill_keys($existing, true); + + $insertable = []; + foreach ($batch as $record) { + if (isset($existing[$record['row_key']])) { + $duplicates++; + continue; + } + $insertable[] = $record; + } + + if ($insertable !== []) { + $inserted = DB::connection($connection) + ->table(self::TABLE) + ->insertOrIgnore($insertable); + $success += (int) $inserted; + $duplicates += count($insertable) - (int) $inserted; + } + $batch = []; + }; + + $headerRow = null; + $columns = []; + $headerCandidates = []; + + while ($reader->read()) { + if ( + $reader->nodeType !== XMLReader::ELEMENT + || $reader->name !== 'row' + ) { + continue; + } + + $rowNumber = (int) $reader->getAttribute('r'); + $values = $this->readRow($reader, $sharedStrings); + + if ($headerRow === null) { + $headerCandidates[$rowNumber] = $values; + $header = $this->headerColumns($headerCandidates); + if ($header !== null) { + [$headerRow, $columns] = $header; + $headerCandidates = []; + continue; + } + if ($rowNumber >= 20) { + throw new RuntimeException( + '「明细」子表前 20 行未找到供应商汇总标准表头。', + ); + } + continue; + } + + if ($rowNumber <= $headerRow) { + continue; + } + + if ($this->emptyRow($values)) { + continue; + } + + try { + $record = $this->record( + $values, + $columns, + $maps, + $month, + $rowNumber, + ); + } catch (Throwable $exception) { + $failed++; + $reasons[] = [ + 'row' => $rowNumber, + 'reason' => $exception->getMessage(), + ]; + continue; + } + + if (isset($seen[$record['row_key']])) { + $duplicates++; + continue; + } + $seen[$record['row_key']] = true; + $mapped++; + $batch[] = $record; + + if (count($batch) >= 500) { + $flush(); + } + } + + if ($headerRow === null) { + throw new RuntimeException( + '「明细」子表前 20 行未找到供应商汇总标准表头。', + ); + } + + $flush(); + }); + + $status = $failed === 0 + ? 'success' + : ($success > 0 ? 'partial' : 'failed'); + + return [ + 'status' => $status, + 'report_month' => $month, + 'source_filename' => basename($path), + 'success' => $success, + 'duplicates' => $duplicates, + 'failed' => $failed, + 'mapped' => $mapped, + 'total_in_table' => DB::connection($connection) + ->table(self::TABLE) + ->where('report_month', $month) + ->count(), + 'failure_reason_count' => count($reasons), + 'failure_reasons' => $reasons, + ]; + } finally { + $reader->close(); + } + } + + private function normalizeMonth(string $month): string + { + if (preg_match('/^\d{4}-\d{2}$/', $month) !== 1) { + throw new RuntimeException('月份格式应为 YYYY-MM。'); + } + + return $month . '-01'; + } + + /** + * @param array> $rows + * @return array{0:int,1:array}|null + */ + private function headerColumns(array $rows): ?array + { + ksort($rows); + foreach ($rows as $row => $values) { + $positions = []; + foreach ($values as $index => $value) { + $heading = $this->normalizeHeader( + (string) $this->plainValue($value), + ); + if ($heading !== '') { + $positions[$heading][] = $index; + } + } + + $columns = []; + foreach (self::REQUIRED_HEADERS as $field => $aliases) { + $position = $this->firstHeaderPosition($positions, $aliases); + if ($position !== null) { + $columns[$field] = $position; + } + } + + $createdPositions = $positions[$this->normalizeHeader('创建时间')] ?? []; + if (count($createdPositions) >= 2) { + $columns['created_serial'] = $createdPositions[0]; + $columns['created_at_raw'] = $createdPositions[1]; + } + + if (count($columns) < count(self::REQUIRED_HEADERS) + 2) { + continue; + } + + foreach (self::OPTIONAL_HEADERS as $field => $aliases) { + $position = $this->firstHeaderPosition($positions, $aliases); + if ($position !== null) { + $columns[$field] = $position; + } + } + + foreach (['created_serial', 'created_at_raw'] as $field) { + if (! isset($columns[$field])) { + throw new RuntimeException('「明细」表头缺少第二个「创建时间」列。'); + } + } + + return [$row, $columns]; + } + + return null; + } + + /** @param array> $positions @param list $aliases */ + private function firstHeaderPosition(array $positions, array $aliases): ?int + { + foreach ($aliases as $alias) { + $heading = $this->normalizeHeader($alias); + if (isset($positions[$heading][0])) { + return $positions[$heading][0]; + } + } + + return null; + } + + private function normalizeHeader(string $heading): string + { + return preg_replace('/[\s\x{3000}]+/u', '', trim($heading)) ?? trim($heading); + } + + /** + * @param array $values + * @param array $columns + * @param array{warehouse:array,provider:array} $maps + * @return array + */ + private function record( + array $values, + array $columns, + array $maps, + string $month, + int $rowNumber, + ): array { + $text = fn (string $field): ?string => $this->text( + $values[$columns[$field] ?? -1] ?? null, + ); + $number = function (string $label, string $field) use ( + $values, + $columns, + $rowNumber, + ): ?float { + $value = $values[$columns[$field] ?? -1] ?? null; + if (is_array($value)) { + $formula = trim((string) ($value['__formula'] ?? '')); + if ($formula !== '') { + return $this->formulaValue( + $label, + $field, + $formula, + $rowNumber, + $values, + $columns, + ); + } + $value = $value['__cached'] ?? null; + } + + $text = str_replace( + [',', ','], + '', + trim((string) $this->plainValue($value)), + ); + if ($text === '') { + return null; + } + if (! is_numeric($text)) { + throw new RuntimeException("字段「{$label}」不是有效数字:" . $text); + } + + return (float) $text; + }; + + $barcode = $text('barcode'); + $warehouse = $text('warehouse'); + if ($barcode === null) { + throw new RuntimeException('商品编码(条码)为空'); + } + if ($warehouse === null) { + throw new RuntimeException('仓库为空'); + } + + $shortName = $maps['warehouse'][$warehouse] + ?? $maps['provider'][$warehouse] + ?? null; + if ($shortName === null || trim($shortName) === '') { + throw new RuntimeException('仓库未匹配到唯一仓库简称:' . $warehouse); + } + + $createdRaw = $this->moment($values[$columns['created_at_raw']] ?? null); + if ($createdRaw === null) { + throw new RuntimeException('第二个「创建时间」为空或格式无效'); + } + + $record = [ + 'report_month' => $month, + 'source_no' => $text('source_no'), + 'document_no' => $text('document_no'), + 'created_serial' => $text('created_serial'), + 'supplier' => $text('supplier'), + 'item_no' => $text('item_no'), + 'barcode' => mb_strtoupper($barcode), + 'spec_name' => $text('spec_name'), + 'quantity' => $number('库存变动量', 'quantity'), + 'unit_price' => $number('单价', 'unit_price'), + 'amount' => $number('金额', 'amount'), + 'estimated_unit_price' => $number('预估单价', 'estimated_unit_price'), + 'estimated_amount' => $number('预估金额', 'estimated_amount'), + 'supplier_barcode' => $text('supplier_barcode'), + 'remark' => $text('remark'), + 'warehouse' => $warehouse, + 'warehouse_short_name' => $shortName, + 'created_at_raw' => $createdRaw, + 'express_name' => $text('express_name'), + 'express_no' => $text('express_no'), + 'not_sent' => $text('not_sent'), + 'owner_name' => $text('owner_name'), + ]; + + unset($record['warehouse_short_name']); + $record['row_key'] = hash( + 'sha256', + json_encode($record, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), + ); + $record['warehouse_short_name'] = $shortName; + $record['imported_at'] = now(); + + return $record; + } + + /** + * 仅接受金额类字段当前行内的乘积公式,避免执行来源文件中的任意公式。 + * + * @param array $values + * @param array $columns + */ + private function formulaValue( + string $label, + string $field, + string $formula, + int $rowNumber, + array $values, + array $columns, + ): float { + $expectedFields = match ($field) { + 'amount' => ['quantity', 'unit_price'], + 'estimated_amount' => ['estimated_unit_price', 'quantity'], + default => [], + }; + if ($expectedFields === []) { + throw new RuntimeException( + "字段「{$label}」不允许使用公式:" . $formula, + ); + } + + if ( + preg_match( + '/^=?\s*\$?([A-Z]{1,3})\$?(\d+)\s*\*\s*\$?([A-Z]{1,3})\$?(\d+)\s*$/i', + $formula, + $matches, + ) !== 1 + ) { + throw new RuntimeException( + "字段「{$label}」公式无法安全识别:" . $formula, + ); + } + + if ((int) $matches[2] !== $rowNumber || (int) $matches[4] !== $rowNumber) { + throw new RuntimeException( + "字段「{$label}」公式引用了其他行:" . $formula, + ); + } + + $actualColumns = [ + $this->columnIndex($matches[1]), + $this->columnIndex($matches[3]), + ]; + $expectedColumns = [ + $columns[$expectedFields[0]] + 1, + $columns[$expectedFields[1]] + 1, + ]; + sort($actualColumns); + sort($expectedColumns); + if ($actualColumns !== $expectedColumns) { + throw new RuntimeException( + "字段「{$label}」公式与取数口径不一致:" . $formula, + ); + } + + $left = $this->numericReference( + $values[$actualColumns[0] - 1] ?? null, + $label, + ); + $right = $this->numericReference( + $values[$actualColumns[1] - 1] ?? null, + $label, + ); + + return $left * $right; + } + + private function numericReference(mixed $value, string $label): float + { + if (is_array($value)) { + if (($value['__formula'] ?? '') !== '') { + throw new RuntimeException( + "字段「{$label}」的公式引用了另一个公式单元格", + ); + } + $value = $value['__cached'] ?? null; + } + + $text = str_replace( + [',', ','], + '', + trim((string) $this->plainValue($value)), + ); + if (! is_numeric($text)) { + throw new RuntimeException( + "字段「{$label}」的公式引用值不是有效数字:" . $text, + ); + } + + return (float) $text; + } + + /** @param array $values */ + private function emptyRow(array $values): bool + { + foreach ($values as $value) { + if (trim((string) $this->plainValue($value)) !== '') { + return false; + } + } + + return true; + } + + private function text(mixed $value): ?string + { + $text = trim((string) $this->plainValue($value)); + + return $text === '' ? null : $text; + } + + private function plainValue(mixed $value): mixed + { + if (is_array($value)) { + return $value['__cached'] ?? null; + } + + return $value; + } + + private function moment(mixed $value): ?string + { + $value = $this->plainValue($value); + if ($value === null || trim((string) $value) === '') { + return null; + } + if (is_numeric($value)) { + try { + return ExcelDate::excelToDateTimeObject((float) $value)->format('Y-m-d H:i:s'); + } catch (Throwable) { + return null; + } + } + + $value = trim((string) $value); + foreach (['Y/m/d H:i:s', 'Y-m-d H:i:s', 'Y/m/d', 'Y-m-d'] as $format) { + $date = DateTimeImmutable::createFromFormat($format, $value); + if ($date instanceof DateTimeImmutable) { + return $date->format('Y-m-d H:i:s'); + } + } + $timestamp = strtotime($value); + + return $timestamp === false ? null : date('Y-m-d H:i:s', $timestamp); + } + + /** @return array{warehouse:array,provider:array} */ + private function shortNameMap(): array + { + $rows = InventoryWarehouseNameMapping::withoutGlobalScopes() + ->get(['warehouse_provider', 'warehouse', 'warehouse_short_name']); + $grouped = []; + foreach ($rows as $row) { + $warehouse = trim((string) $row->warehouse); + if ($warehouse !== '') { + $grouped[$warehouse][] = trim((string) $row->warehouse_short_name); + } + } + + $byWarehouse = []; + foreach ($grouped as $warehouse => $names) { + $unique = array_values(array_unique($names)); + if (count($unique) === 1 && $unique[0] !== '') { + $byWarehouse[$warehouse] = $unique[0]; + } + } + + $providerGrouped = []; + foreach ($rows as $row) { + $provider = trim((string) $row->warehouse_provider); + if ($provider !== '') { + $providerGrouped[$provider][] = [ + 'warehouse' => trim((string) $row->warehouse), + 'short_name' => trim((string) $row->warehouse_short_name), + ]; + } + } + $byProvider = []; + foreach ($providerGrouped as $provider => $providerRows) { + $unique = array_values(array_unique(array_column( + $providerRows, + 'short_name', + ))); + if (count($unique) === 1 && $unique[0] !== '') { + $byProvider[$provider] = $unique[0]; + continue; + } + + $mainShortNames = []; + foreach ($providerRows as $providerRow) { + if (str_contains($providerRow['warehouse'], '主仓')) { + $mainShortNames[] = $providerRow['short_name']; + } + } + $mainShortNames = array_values(array_unique($mainShortNames)); + if (count($mainShortNames) === 1 && $mainShortNames[0] !== '') { + $byProvider[$provider] = $mainShortNames[0]; + } + } + + return ['warehouse' => $byWarehouse, 'provider' => $byProvider]; + } + + /** @return array{0:XMLReader,1:list} */ + private function streamDetailSheet(string $path): array + { + $zip = new ZipArchive(); + if ($zip->open($path) !== true) { + throw new RuntimeException('供应商汇总文件不是有效的 XLSX 工作簿。'); + } + + try { + $sheetPath = $this->detailSheetPath($zip); + $sharedStrings = $zip->locateName('xl/sharedStrings.xml') !== false + ? $this->readSharedStrings($path) + : []; + } finally { + $zip->close(); + } + + return [ + $this->openZipEntry($path, $sheetPath), + $sharedStrings, + ]; + } + + private function detailSheetPath(ZipArchive $zip): string + { + $workbookXml = $zip->getFromName('xl/workbook.xml'); + $relationshipsXml = $zip->getFromName('xl/_rels/workbook.xml.rels'); + if ($workbookXml === false || $relationshipsXml === false) { + throw new RuntimeException('供应商汇总工作簿缺少工作簿结构文件。'); + } + + $workbook = new DOMDocument(); + $relationships = new DOMDocument(); + if ( + ! $workbook->loadXML($workbookXml, LIBXML_NONET) + || ! $relationships->loadXML($relationshipsXml, LIBXML_NONET) + ) { + throw new RuntimeException('供应商汇总工作簿结构无法读取。'); + } + + $relationshipId = null; + $xpath = new DOMXPath($workbook); + $xpath->registerNamespace( + 'main', + 'http://schemas.openxmlformats.org/spreadsheetml/2006/main', + ); + $xpath->registerNamespace( + 'office', + 'http://schemas.openxmlformats.org/officeDocument/2006/relationships', + ); + foreach ($xpath->query('/main:workbook/main:sheets/main:sheet') ?: [] as $sheet) { + if ($sheet instanceof \DOMElement + && $sheet->getAttribute('name') === self::DETAIL_SHEET + ) { + $relationshipId = $sheet->getAttributeNS( + 'http://schemas.openxmlformats.org/officeDocument/2006/relationships', + 'id', + ); + break; + } + } + if ($relationshipId === null || $relationshipId === '') { + throw new RuntimeException('供应商汇总表缺少「明细」子表。'); + } + + $target = null; + foreach ($relationships->getElementsByTagName('Relationship') as $relationship) { + if ($relationship->getAttribute('Id') === $relationshipId) { + $target = $relationship->getAttribute('Target'); + break; + } + } + if ($target === null || $target === '') { + throw new RuntimeException('「明细」子表的工作簿关系不存在。'); + } + + return $this->normalizeZipPath( + str_starts_with($target, '/') ? ltrim($target, '/') : 'xl/' . $target, + ); + } + + private function normalizeZipPath(string $path): string + { + $segments = []; + foreach (explode('/', str_replace('\\', '/', $path)) as $segment) { + if ($segment === '' || $segment === '.') { + continue; + } + if ($segment === '..') { + array_pop($segments); + continue; + } + $segments[] = $segment; + } + + return implode('/', $segments); + } + + /** @return list */ + private function readSharedStrings(string $path): array + { + $reader = $this->openZipEntry($path, 'xl/sharedStrings.xml'); + $strings = []; + $current = null; + $stringDepth = null; + $phoneticDepth = null; + + try { + while ($reader->read()) { + if ( + $reader->nodeType === XMLReader::ELEMENT + && $reader->name === 'si' + ) { + $current = ''; + $stringDepth = $reader->depth; + $phoneticDepth = null; + continue; + } + if ($current === null) { + continue; + } + if ( + $reader->nodeType === XMLReader::ELEMENT + && $reader->name === 'rPh' + ) { + $phoneticDepth = $reader->depth; + continue; + } + if ( + $reader->nodeType === XMLReader::END_ELEMENT + && $reader->name === 'rPh' + && $reader->depth === $phoneticDepth + ) { + $phoneticDepth = null; + continue; + } + if ( + $reader->nodeType === XMLReader::ELEMENT + && $reader->name === 't' + && $phoneticDepth === null + ) { + $current .= $reader->readString(); + continue; + } + if ( + $reader->nodeType === XMLReader::END_ELEMENT + && $reader->name === 'si' + && $reader->depth === $stringDepth + ) { + $strings[] = $current; + $current = null; + } + } + } finally { + $reader->close(); + } + + return $strings; + } + + private function openZipEntry(string $path, string $entry): XMLReader + { + $reader = new XMLReader(); + $flags = LIBXML_NONET | LIBXML_COMPACT; + if (! $reader->open("zip://{$path}#{$entry}", null, $flags)) { + throw new RuntimeException("无法读取工作簿内容:{$entry}"); + } + + return $reader; + } + + /** + * @param list $sharedStrings + * @return array + */ + private function readRow(XMLReader $reader, array $sharedStrings): array + { + $values = []; + $rowDepth = $reader->depth; + while ($reader->read()) { + if ( + $reader->nodeType === XMLReader::END_ELEMENT + && $reader->name === 'row' + && $reader->depth === $rowDepth + ) { + break; + } + if ( + $reader->nodeType !== XMLReader::ELEMENT + || $reader->name !== 'c' + ) { + continue; + } + + $reference = $reader->getAttribute('r') ?? ''; + if ( + preg_match('/^([A-Z]{1,3})\d+$/i', $reference, $matches) !== 1 + ) { + continue; + } + $values[$this->columnIndex($matches[1]) - 1] = $this->readCell( + $reader, + $sharedStrings, + ); + } + + return $values; + } + + /** @param list $sharedStrings */ + private function readCell(XMLReader $reader, array $sharedStrings): mixed + { + if ($reader->isEmptyElement) { + return null; + } + + $type = $reader->getAttribute('t') ?? ''; + $cellDepth = $reader->depth; + $cached = null; + $formula = null; + $hasFormula = false; + $inlineParts = []; + $phoneticDepth = null; + + while ($reader->read()) { + if ( + $reader->nodeType === XMLReader::END_ELEMENT + && $reader->name === 'c' + && $reader->depth === $cellDepth + ) { + break; + } + if ($reader->nodeType === XMLReader::ELEMENT) { + if ($reader->name === 'f') { + $hasFormula = true; + $formula = $reader->readString(); + continue; + } + if ($reader->name === 'v') { + $cached = $reader->readString(); + continue; + } + if ($reader->name === 'rPh') { + $phoneticDepth = $reader->depth; + continue; + } + if ( + $reader->name === 't' + && $phoneticDepth === null + ) { + $inlineParts[] = $reader->readString(); + } + } + if ( + $reader->nodeType === XMLReader::END_ELEMENT + && $reader->name === 'rPh' + && $reader->depth === $phoneticDepth + ) { + $phoneticDepth = null; + } + } + + $value = match ($type) { + 's' => is_numeric($cached) + ? ($sharedStrings[(int) $cached] ?? null) + : null, + 'inlineStr' => implode('', $inlineParts), + 'b' => $cached === null ? null : ($cached === '1' ? '1' : '0'), + default => $cached ?? implode('', $inlineParts), + }; + + if ($hasFormula) { + return [ + '__formula' => (string) $formula, + '__cached' => $value, + ]; + } + + return $value; + } + + private function columnIndex(string $column): int + { + $index = 0; + foreach (str_split(strtoupper($column)) as $letter) { + $index = ($index * 26) + (ord($letter) - 64); + } + + return $index; + } +} diff --git a/app/Console/Commands/ImportSupplierSummaryInbound.php b/app/Console/Commands/ImportSupplierSummaryInbound.php new file mode 100644 index 00000000..a2e410ed --- /dev/null +++ b/app/Console/Commands/ImportSupplierSummaryInbound.php @@ -0,0 +1,121 @@ +option('month')); + if ($month === '') { + $month = now('Asia/Shanghai')->subMonthNoOverflow()->format('Y-m'); + } + + $lock = Cache::lock('inventory-supplier-summary-inbound-import', 21600); + if (! $lock->get()) { + return $this->finish([ + 'status' => 'failed', + 'report_month' => $month, + 'source_filename' => basename((string) $this->argument('file')), + 'success' => 0, + 'duplicates' => 0, + 'failed' => 1, + 'mapped' => 0, + 'total_in_table' => 0, + 'failure_reason_count' => 1, + 'failure_reasons' => [[ + 'row' => 0, + 'reason' => '已有供应商汇总导入任务正在执行', + ]], + ]); + } + + try { + $result = $importer->import((string) $this->argument('file'), $month); + } catch (Throwable $exception) { + report($exception); + $result = [ + 'status' => 'failed', + 'report_month' => $month, + 'source_filename' => basename((string) $this->argument('file')), + 'success' => 0, + 'duplicates' => 0, + 'failed' => 1, + 'mapped' => 0, + 'total_in_table' => 0, + 'failure_reason_count' => 1, + 'failure_reasons' => [[ + 'row' => 0, + 'reason' => $exception->getMessage(), + ]], + ]; + } finally { + $lock->release(); + } + + return $this->finish($result); + } + + /** @param array $result */ + private function finish(array $result): int + { + $reasons = is_array($result['failure_reasons'] ?? null) + ? $result['failure_reasons'] + : []; + $result['failure_reason_count'] = count($reasons); + + $json = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + if (! is_string($json)) { + $json = '{"status":"failed","failure_reasons":[{"row":0,"reason":"结果 JSON 编码失败"}]}'; + } + + $resultPath = trim((string) $this->option('result-json')); + if ($resultPath !== '') { + File::ensureDirectoryExists(dirname($resultPath)); + File::put($resultPath, $json . PHP_EOL); + } + + $this->line(sprintf( + '%s 导入状态=%s,成功=%s,重复=%s,失败=%s,目标月份总行数=%s', + (string) ($result['report_month'] ?? ''), + (string) ($result['status'] ?? 'failed'), + number_format((int) ($result['success'] ?? 0)), + number_format((int) ($result['duplicates'] ?? 0)), + number_format((int) ($result['failed'] ?? 0)), + number_format((int) ($result['total_in_table'] ?? 0)), + )); + foreach (array_slice($reasons, 0, 10) as $reason) { + $this->warn(sprintf( + '第 %s 行:%s', + (string) ($reason['row'] ?? ''), + (string) ($reason['reason'] ?? '未知错误'), + )); + } + if (count($reasons) > 10) { + $this->line(sprintf( + '另有 %s 条失败原因,完整清单见结果 JSON。', + number_format(count($reasons) - 10), + )); + } + + return ($result['status'] ?? 'failed') === 'success' + ? self::SUCCESS + : self::FAILURE; + } +} diff --git a/deploy/finance-batch-imports.json b/deploy/finance-batch-imports.json index 9bddc7b7..8e641b1b 100644 --- a/deploy/finance-batch-imports.json +++ b/deploy/finance-batch-imports.json @@ -12,6 +12,7 @@ "finance-ledger:import-snapshot", "internal-flow:import", "inventory:inbound-detail:import", + "inventory:supplier-summary:import", "business-mirror:import-inventory-report-month", "business-mirror:import-jd-daily", "business-mirror:import-jd-wallet", diff --git a/scripts/decrypt_supplier_summary.py b/scripts/decrypt_supplier_summary.py new file mode 100755 index 00000000..d3f7e50a --- /dev/null +++ b/scripts/decrypt_supplier_summary.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import argparse +import os +import shutil +import tempfile +from pathlib import Path + +import msoffcrypto + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("source") + parser.add_argument("destination") + parser.add_argument("--password-file", required=True) + args = parser.parse_args() + + source = Path(args.source) + destination = Path(args.destination) + password_file = Path(args.password_file) + if not source.is_file(): + raise SystemExit(f"源文件不存在:{source}") + if not password_file.is_file(): + raise SystemExit(f"密码文件不存在:{password_file}") + + password = password_file.read_text(encoding="utf-8").strip() + if password == "": + raise SystemExit("供应商汇总表密码为空") + + destination.parent.mkdir(parents=True, exist_ok=True) + temp_fd, temp_name = tempfile.mkstemp( + prefix=destination.stem + ".", + suffix=".xlsx", + dir=destination.parent, + ) + os.close(temp_fd) + temp_path = Path(temp_name) + + try: + with source.open("rb") as source_handle: + office = msoffcrypto.OfficeFile(source_handle) + if office.is_encrypted(): + office.load_key(password=password) + with temp_path.open("wb") as destination_handle: + office.decrypt(destination_handle) + else: + shutil.copyfile(source, temp_path) + os.replace(temp_path, destination) + finally: + temp_path.unlink(missing_ok=True) + + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/find_supplier_summary.py b/scripts/find_supplier_summary.py new file mode 100755 index 00000000..4d32ec92 --- /dev/null +++ b/scripts/find_supplier_summary.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import argparse +from pathlib import Path + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("source_dir") + parser.add_argument("month", help="YYYY-MM") + args = parser.parse_args() + + year, month = args.month.split("-", 1) + month_number = int(month) + source_dir = Path(args.source_dir).expanduser() + if not source_dir.is_dir(): + raise SystemExit(f"供应商汇总目录不存在:{source_dir}") + + candidates = [] + for path in source_dir.rglob("*.xlsx"): + name = path.name + if name.startswith("~$") or name.startswith(".~"): + continue + if "供应商汇总表" not in name or "终版" not in name: + continue + if f"{month_number}月" not in name: + continue + candidates.append(path) + + if not candidates: + raise SystemExit( + f"未找到 {args.month} 的供应商汇总终版表:{source_dir}" + ) + + preferred = [path for path in candidates if year in path.name] + selectable = preferred or candidates + selected = max(selectable, key=lambda path: path.stat().st_mtime) + print(selected) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/install-supplier-summary-schedule-macos.sh b/scripts/install-supplier-summary-schedule-macos.sh new file mode 100755 index 00000000..39356b3c --- /dev/null +++ b/scripts/install-supplier-summary-schedule-macos.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +RUNTIME_DIR="${SUPPLIER_SUMMARY_RUNTIME_DIR:-$REPO_ROOT/runtime/private/supplier-summary-scheduler}" +PASSWORD_FILE="${SUPPLIER_SUMMARY_PASSWORD_FILE:-$REPO_ROOT/runtime/private/supplier-summary-password}" +SOURCE_DIR="${SUPPLIER_SUMMARY_DIR:-$HOME/Desktop/加权平均单价}" +BASE_PYTHON="${SUPPLIER_SUMMARY_SYSTEM_PYTHON:-/usr/bin/python3}" +VENV_DIR="$RUNTIME_DIR/venv" +LABEL='hk.caiwuxitong.supplier-summary-import' +PLIST_PATH="$HOME/Library/LaunchAgents/$LABEL.plist" +RUN_SCRIPT="$REPO_ROOT/scripts/run-supplier-summary-import-macos.sh" +DOCKER_BIN="$(command -v docker || true)" + +if [ -z "$DOCKER_BIN" ]; then + printf '找不到 docker 命令,无法安装供应商汇总定时任务。\n' >&2 + exit 2 +fi +DOCKER_DIR="$(dirname "$DOCKER_BIN")" + +mkdir -p "$RUNTIME_DIR" "$HOME/Library/LaunchAgents" +chmod 700 "$RUNTIME_DIR" + +if [ ! -f "$PASSWORD_FILE" ]; then + if [ -z "${SUPPLIER_SUMMARY_PASSWORD:-}" ]; then + printf '首次安装需设置 SUPPLIER_SUMMARY_PASSWORD 环境变量。\n' >&2 + exit 2 + fi + umask 077 + printf '%s\n' "$SUPPLIER_SUMMARY_PASSWORD" > "$PASSWORD_FILE" +fi +chmod 600 "$PASSWORD_FILE" + +if [ ! -x "$VENV_DIR/bin/python" ]; then + "$BASE_PYTHON" -m venv "$VENV_DIR" +fi +"$VENV_DIR/bin/python" -m pip install \ + --disable-pip-version-check \ + --quiet \ + 'msoffcrypto-tool>=5.4,<6' + +"$BASE_PYTHON" - "$PLIST_PATH" "$LABEL" "$REPO_ROOT" "$RUN_SCRIPT" \ + "$RUNTIME_DIR" "$SOURCE_DIR" "$DOCKER_DIR" <<'PY' +import plistlib +import sys +from pathlib import Path + +plist_path = Path(sys.argv[1]) +label, repo_root, run_script, runtime_dir, source_dir, docker_dir = sys.argv[2:] +payload = { + "Label": label, + "ProgramArguments": ["/bin/bash", run_script], + "WorkingDirectory": repo_root, + "EnvironmentVariables": { + "PATH": f"{docker_dir}:/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin", + "SUPPLIER_SUMMARY_DIR": source_dir, + "SUPPLIER_SUMMARY_RUNTIME_DIR": runtime_dir, + }, + "StartCalendarInterval": { + "Day": 16, + "Hour": 17, + "Minute": 0, + }, + "RunAtLoad": False, + "StandardOutPath": str(Path(runtime_dir) / "launchd.out.log"), + "StandardErrorPath": str(Path(runtime_dir) / "launchd.err.log"), +} +with plist_path.open("wb") as handle: + plistlib.dump(payload, handle, sort_keys=False) +PY + +chmod 644 "$PLIST_PATH" +chmod +x "$RUN_SCRIPT" +chmod +x "$SCRIPT_DIR/find_supplier_summary.py" +chmod +x "$SCRIPT_DIR/summarize_supplier_summary_result.py" +chmod +x "$SCRIPT_DIR/decrypt_supplier_summary.py" + +domain="gui/$(id -u)" +launchctl bootout "$domain/$LABEL" >/dev/null 2>&1 || true +launchctl bootstrap "$domain" "$PLIST_PATH" +launchctl enable "$domain/$LABEL" + +printf '已安装定时任务:%s\n' "$LABEL" +printf '执行时间:每月 16 日 17:00(导入上一个月)\n' +printf '源目录:%s\n' "$SOURCE_DIR" +printf 'LaunchAgent:%s\n' "$PLIST_PATH" diff --git a/scripts/run-supplier-summary-import-macos.sh b/scripts/run-supplier-summary-import-macos.sh new file mode 100755 index 00000000..00b2930a --- /dev/null +++ b/scripts/run-supplier-summary-import-macos.sh @@ -0,0 +1,166 @@ +#!/bin/bash + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +RUNTIME_DIR="${SUPPLIER_SUMMARY_RUNTIME_DIR:-$REPO_ROOT/runtime/private/supplier-summary-scheduler}" +SOURCE_DIR="${SUPPLIER_SUMMARY_DIR:-$HOME/Desktop/加权平均单价}" +CONTAINER="${SUPPLIER_SUMMARY_CONTAINER:-jiangling_app}" +PASSWORD_FILE="${SUPPLIER_SUMMARY_PASSWORD_FILE:-$REPO_ROOT/runtime/private/supplier-summary-password}" +SYSTEM_PYTHON="${SUPPLIER_SUMMARY_SYSTEM_PYTHON:-/usr/bin/python3}" +CRYPTO_PYTHON="${SUPPLIER_SUMMARY_PYTHON:-$RUNTIME_DIR/venv/bin/python}" + +target_month="$(date -v-1m '+%Y-%m')" +explicit_file='' +notify=1 + +while [ "$#" -gt 0 ]; do + case "$1" in + --month) + target_month="$2" + shift + ;; + --file) + explicit_file="$2" + shift + ;; + --no-notify) + notify=0 + ;; + *) + printf '未知参数:%s\n' "$1" >&2 + exit 2 + ;; + esac + shift +done + +if [[ ! "$target_month" =~ ^[0-9]{4}-[0-9]{2}$ ]]; then + printf '月份格式应为 YYYY-MM:%s\n' "$target_month" >&2 + exit 2 +fi + +timestamp="$(date '+%Y%m%dT%H%M%S')" +run_dir="$RUNTIME_DIR/runs/$timestamp" +history_dir="$RUNTIME_DIR/history" +mkdir -p "$run_dir" "$history_dir" +work_dir="$(mktemp -d "${TMPDIR:-/tmp}/supplier-summary-import.XXXXXX")" +container_file='' +container_result='' + +cleanup() { + if [ -n "$container_file" ]; then + docker exec "$CONTAINER" unlink "$container_file" >/dev/null 2>&1 || true + fi + if [ -n "$container_result" ]; then + docker exec "$CONTAINER" unlink "$container_result" >/dev/null 2>&1 || true + fi + rm -rf "$work_dir" +} +trap cleanup EXIT + +notify_user() { + local title="$1" + local message="$2" + if [ "$notify" -eq 1 ] && command -v osascript >/dev/null 2>&1; then + osascript \ + -e 'on run argv' \ + -e 'display notification (item 2 of argv) with title (item 1 of argv)' \ + -e 'end run' \ + "$title" "$message" >/dev/null 2>&1 || true + fi +} + +record_failure() { + local message="$1" + local brief + brief="$( + "$SYSTEM_PYTHON" "$REPO_ROOT/scripts/summarize_supplier_summary_result.py" \ + --month "$target_month" \ + --result-json "$run_dir/result.json" \ + --error "$message" 2>/dev/null + )" || true + [ -n "$brief" ] || brief="$target_month 导入失败:$message" + printf '%s\n' "$brief" > "$run_dir/brief.txt" + cp "$run_dir/result.json" "$RUNTIME_DIR/last-run.json" 2>/dev/null || true + cp "$run_dir/result.json" "$history_dir/$timestamp.json" 2>/dev/null || true + notify_user '供应商汇总导入失败' "$brief" + printf '%s\n' "$brief" + exit 1 +} + +if [ -n "$explicit_file" ]; then + source_file="$explicit_file" +else + source_file="$( + "$SYSTEM_PYTHON" "$REPO_ROOT/scripts/find_supplier_summary.py" \ + "$SOURCE_DIR" "$target_month" + )" || record_failure "未找到 ${target_month} 的供应商汇总终版表" +fi + +if [ ! -f "$source_file" ] || [ ! -r "$source_file" ]; then + record_failure "供应商汇总文件不存在或不可读取:$source_file" +fi +if [ ! -x "$CRYPTO_PYTHON" ]; then + record_failure "解密环境未安装,请先运行 scripts/install-supplier-summary-schedule-macos.sh" +fi +if [ ! -f "$PASSWORD_FILE" ]; then + record_failure "供应商汇总表密码文件不存在:$PASSWORD_FILE" +fi +if ! command -v docker >/dev/null 2>&1; then + record_failure "找不到 docker 命令,无法导入本地容器" +fi + +source_name="$(basename "$source_file")" +decrypted_file="$work_dir/$source_name" +printf '开始导入供应商汇总:月份=%s,文件=%s\n' "$target_month" "$source_name" \ + | tee "$run_dir/run.log" + +"$CRYPTO_PYTHON" "$REPO_ROOT/scripts/decrypt_supplier_summary.py" \ + "$source_file" "$decrypted_file" \ + --password-file "$PASSWORD_FILE" >> "$run_dir/run.log" 2>&1 \ + || record_failure "供应商汇总文件解密失败,请确认密码及文件完整性" + +chmod 644 "$decrypted_file" +container_file="/tmp/supplier-summary-$timestamp.xlsx" +container_result="/tmp/supplier-summary-$timestamp.json" +docker cp "$decrypted_file" "$CONTAINER:$container_file" >> "$run_dir/run.log" 2>&1 \ + || record_failure "无法将解密后的供应商汇总文件传入本地应用容器" +docker exec "$CONTAINER" chmod 644 "$container_file" >> "$run_dir/run.log" 2>&1 \ + || record_failure "无法设置容器内供应商汇总文件权限" + +set +e +docker exec -u www-data -e HOME=/tmp "$CONTAINER" \ + php artisan inventory:supplier-summary:import "$container_file" \ + --month="$target_month" \ + --result-json="$container_result" \ + >> "$run_dir/import.log" 2>&1 +import_exit=$? +set -e + +if ! docker cp "$CONTAINER:$container_result" "$work_dir/result.json" >> "$run_dir/run.log" 2>&1; then + record_failure "导入命令未生成结果文件,退出码:$import_exit" +fi + +set +e +brief="$( + "$SYSTEM_PYTHON" "$REPO_ROOT/scripts/summarize_supplier_summary_result.py" \ + --month "$target_month" \ + --result-json "$run_dir/result.json" \ + --input "$work_dir/result.json" +)" +summary_exit=$? +set -e + +printf '%s\n' "$brief" | tee "$run_dir/brief.txt" +cp "$run_dir/result.json" "$RUNTIME_DIR/last-run.json" +cp "$run_dir/result.json" "$history_dir/$timestamp.json" + +if [ "$import_exit" -ne 0 ] || [ "$summary_exit" -ne 0 ]; then + notify_user '供应商汇总导入需处理' "$brief" + exit 1 +fi + +notify_user '供应商汇总导入完成' "$brief" +exit 0 diff --git a/scripts/summarize_supplier_summary_result.py b/scripts/summarize_supplier_summary_result.py new file mode 100755 index 00000000..841898b4 --- /dev/null +++ b/scripts/summarize_supplier_summary_result.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +import argparse +import json +from datetime import datetime +from pathlib import Path + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument("--month", required=True) + parser.add_argument("--result-json", required=True) + parser.add_argument("--input") + parser.add_argument("--error") + args = parser.parse_args() + + if args.input: + result = json.loads(Path(args.input).read_text(encoding="utf-8")) + else: + result = { + "status": "failed", + "report_month": args.month, + "source_filename": None, + "success": 0, + "duplicates": 0, + "failed": 1, + "mapped": 0, + "total_in_table": 0, + "failure_reasons": [ + { + "row": 0, + "reason": args.error or "供应商汇总导入未执行", + } + ], + } + + status = result.get("status", "failed") + success = int(result.get("success") or 0) + duplicates = int(result.get("duplicates") or 0) + failed = int(result.get("failed") or 0) + reasons = result.get("failure_reasons") or [] + report_month = str(result.get("report_month") or f"{args.month}-01") + try: + month_label = f"{int(report_month[5:7])} 月" + except (TypeError, ValueError): + month_label = args.month + brief = ( + f"成功导入 {success:,} 条 / 上月 = {month_label}" + f";重复 {duplicates:,} 条 / 失败 {failed:,} 条" + ) + if status != "success" and reasons: + first = str(reasons[0].get("reason", "未知错误")) + row = reasons[0].get("row") + location = f"第 {row} 行:" if row else "" + brief += f";{location}{first}" + + payload = { + "finished_at": datetime.now().astimezone().isoformat(timespec="seconds"), + "brief": brief, + "result": result, + } + output = Path(args.result_json) + output.parent.mkdir(parents=True, exist_ok=True) + output.write_text( + json.dumps(payload, ensure_ascii=False, indent=2) + "\n", + encoding="utf-8", + ) + print(brief) + return 0 if status == "success" else 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tests/Feature/Console/SupplierSummaryInboundImportTest.php b/tests/Feature/Console/SupplierSummaryInboundImportTest.php new file mode 100644 index 00000000..315aaec8 --- /dev/null +++ b/tests/Feature/Console/SupplierSummaryInboundImportTest.php @@ -0,0 +1,155 @@ +create([ + 'company_id' => 1, + 'warehouse_provider' => $provider, + 'warehouse' => $provider . '主仓', + 'warehouse_short_name' => '测试简称', + ]); + $ambiguousProvider = '测试歧义仓储方-' . uniqid(); + foreach (['甲', '乙'] as $name) { + InventoryWarehouseNameMapping::query()->create([ + 'company_id' => 1, + 'warehouse_provider' => $ambiguousProvider, + 'warehouse' => $ambiguousProvider . $name . '主仓', + 'warehouse_short_name' => '歧义简称' . $name, + ]); + } + + $connection = (string) config('upstream_mirrors.connection', 'business_mirror'); + DB::connection($connection)->table('inventory_inbound_detail_rows')->insert([ + 'report_month' => '2026-08-01', + 'row_key' => sha1('historic-row'), + 'imported_at' => now(), + ]); + + $path = $this->temporaryPath('supplier-summary-', '.xlsx'); + $firstResult = $this->temporaryPath('supplier-result-', '.json'); + $secondResult = $this->temporaryPath('supplier-result-', '.json'); + try { + $this->writeWorkbook($path, $provider, $ambiguousProvider); + + $this->artisan('inventory:supplier-summary:import', [ + 'file' => $path, + '--month' => '2026-09', + '--result-json' => $firstResult, + ]) + ->expectsOutputToContain('成功=1') + ->expectsOutputToContain('另有 2 条失败原因') + ->assertExitCode(1); + + $result = json_decode((string) file_get_contents($firstResult), true); + $this->assertSame('partial', $result['status']); + $this->assertSame('2026-09-01', $result['report_month']); + $this->assertSame(1, $result['success']); + $this->assertSame(1, $result['duplicates']); + $this->assertSame(12, $result['failed']); + $this->assertSame(12, $result['failure_reason_count']); + $this->assertSame(4, $result['failure_reasons'][0]['row']); + $this->assertStringContainsString('仓库未匹配', $result['failure_reasons'][0]['reason']); + $this->assertSame( + '仓库未匹配到唯一仓库简称:' . $ambiguousProvider, + $result['failure_reasons'][11]['reason'], + ); + + $this->assertSame( + 1, + DB::connection($connection)->table('inventory_inbound_detail_rows') + ->where('report_month', '2026-08-01')->count(), + ); + $september = DB::connection($connection)->table('inventory_inbound_detail_rows') + ->where('report_month', '2026-09-01') + ->get(); + $this->assertCount(1, $september); + $this->assertSame('BARCODE-1', $september->first()->barcode); + $this->assertSame('测试简称', $september->first()->warehouse_short_name); + $this->assertSame('2026-09-03 10:00:00', $september->first()->created_at_raw); + $this->assertEquals(20, $september->first()->amount); + $this->assertEquals(22, $september->first()->estimated_amount); + + $this->artisan('inventory:supplier-summary:import', [ + 'file' => $path, + '--month' => '2026-09', + '--result-json' => $secondResult, + ])->assertExitCode(1); + + $second = json_decode((string) file_get_contents($secondResult), true); + $this->assertSame('failed', $second['status']); + $this->assertSame(0, $second['success']); + $this->assertSame(2, $second['duplicates']); + $this->assertSame(12, $second['failed']); + $this->assertSame( + 1, + DB::connection($connection)->table('inventory_inbound_detail_rows') + ->where('report_month', '2026-09-01')->count(), + ); + } finally { + foreach ([$path, $firstResult, $secondResult] as $file) { + @unlink($file); + } + } + } + + private function writeWorkbook( + string $path, + string $provider, + string $ambiguousProvider, + ): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->setTitle('明细'); + $sheet->fromArray([ + '来源单号', "单据编号\n(采购单号)", '创建时间', '供应商', '货号', '条码', + '规格名', '库存变动量', '单价', '金额', '预估单价', '预估金额', + '供应商条码', '备注', '仓库', '创建时间', '快递名称', '快递单号', + '未发出', '负责人', + ], null, 'A1'); + + for ($row = 2; $row <= 14; $row++) { + $warehouse = $row <= 3 ? $provider : '不存在的仓库'; + $sheet->fromArray([ + 'SRC-1', 'PO-1', '46245', '测试供应商', 'ITEM-1', 'BARCODE-1', + '测试规格', 2, 10, null, 11, null, 'SUP-BAR-1', '测试备注', + $warehouse, '2026-09-03 10:00:00', '顺丰', 'SF123', '否', + '测试负责人', + ], null, 'A' . $row); + $sheet->setCellValue('J' . $row, '=H' . $row . '*I' . $row); + $sheet->setCellValue('L' . $row, '=K' . $row . '*H' . $row); + } + + $sheet->fromArray([ + 'SRC-2', 'PO-2', '46246', '测试供应商', 'ITEM-2', 'BARCODE-2', + '测试规格二', 1, 10, null, 10, null, 'SUP-BAR-2', null, + $ambiguousProvider, '2026-09-04 10:00:00', null, null, null, + '测试负责人', + ], null, 'A15'); + $sheet->setCellValue('J15', '=H15*I15'); + $sheet->setCellValue('L15', '=K15*H15'); + + (new Xlsx($spreadsheet))->save($path); + $spreadsheet->disconnectWorksheets(); + } + + private function temporaryPath(string $prefix, string $suffix): string + { + $temporary = tempnam(sys_get_temp_dir(), $prefix); + $path = $temporary . $suffix; + rename($temporary, $path); + + return $path; + } +}