From 0a732d7942e75cf41d90122e5df4047a60d06fd6 Mon Sep 17 00:00:00 2001 From: chenxi Date: Thu, 29 Oct 2026 20:53:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=BA=BA=E5=B7=A5?= =?UTF-8?q?=E8=B4=B9=E7=94=A8=E6=95=B0=E6=8D=AE=E4=BF=9D=E5=AD=98=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Business/LaborExpenseDataStore.php | 141 +++++++++++++++ app/Http/Controllers/Business/Pages.php | 32 +++- app/Models/Business/LaborExpenseSnapshot.php | 27 +++ ...reate_business_labor_expense_snapshots.php | 29 +++ .../labor-expenses.blade.php | 168 +++++++++++++++++- routes/admin.php | 7 + .../Business/LaborExpensePageAccessTest.php | 106 +++++++++++ 7 files changed, 502 insertions(+), 8 deletions(-) create mode 100644 app/Business/LaborExpenseDataStore.php create mode 100644 app/Models/Business/LaborExpenseSnapshot.php create mode 100644 database/migrations/2026_10_29_180000_create_business_labor_expense_snapshots.php diff --git a/app/Business/LaborExpenseDataStore.php b/app/Business/LaborExpenseDataStore.php new file mode 100644 index 00000000..1ffc689b --- /dev/null +++ b/app/Business/LaborExpenseDataStore.php @@ -0,0 +1,141 @@ +>, row_count: int, source_name: ?string, saved_by: ?int, saved_at: ?string} + */ + public function load(): array + { + $snapshot = LaborExpenseSnapshot::query() + ->where('company_id', company_id()) + ->first(); + + return [ + 'rows' => is_array($snapshot?->rows) ? array_values($snapshot->rows) : [], + 'row_count' => (int) ($snapshot?->row_count ?? 0), + 'source_name' => $snapshot?->source_name, + 'saved_by' => $snapshot?->saved_by, + 'saved_at' => $snapshot?->saved_at?->toIso8601String(), + ]; + } + + /** + * @param array $rows + * @return array{rows: array>, row_count: int, source_name: ?string, saved_by: ?int, saved_at: string} + */ + public function save(array $rows, ?string $sourceName, ?int $savedBy): array + { + if (count($rows) > self::MAX_ROWS) { + throw ValidationException::withMessages([ + 'rows' => '单次最多保存 ' . number_format(self::MAX_ROWS) . ' 行,请拆分后再保存。', + ]); + } + + $fields = collect(config('labor_expense_table.fields', [])) + ->filter(static fn (array $field): bool => ($field['kind'] ?? null) !== 'sequence' + && blank($field['formula'] ?? null)) + ->values(); + + if ($fields->isEmpty()) { + throw ValidationException::withMessages([ + 'rows' => '人工费用表没有可保存的录入列。', + ]); + } + + $companyId = (int) company_id(); + if ($companyId < 1) { + throw ValidationException::withMessages([ + 'rows' => '当前没有可保存的公司。', + ]); + } + + $normalizedRows = []; + foreach (array_values($rows) as $index => $row) { + if (! is_array($row)) { + throw ValidationException::withMessages([ + 'rows' => '第 ' . ($index + 1) . ' 行数据格式无效。', + ]); + } + + $record = []; + $hasValue = false; + foreach ($fields as $field) { + $value = $this->normalizeCell($row[$field['key']] ?? null, $field, $index + 1); + $record[$field['key']] = $value; + $hasValue = $hasValue || $value !== ''; + } + + if ($hasValue) { + $normalizedRows[] = $record; + } + } + + $sourceName = trim((string) $sourceName); + $sourceName = $sourceName === '' ? null : mb_substr($sourceName, 0, 191); + $savedAt = now(); + + DB::transaction(function () use ($companyId, $sourceName, $savedBy, $savedAt, $normalizedRows): void { + LaborExpenseSnapshot::query()->updateOrCreate( + ['company_id' => $companyId], + [ + 'source_name' => $sourceName, + 'rows' => $normalizedRows, + 'row_count' => count($normalizedRows), + 'saved_by' => $savedBy, + 'saved_at' => $savedAt, + ], + ); + }); + + return [ + 'rows' => $normalizedRows, + 'row_count' => count($normalizedRows), + 'source_name' => $sourceName, + 'saved_by' => $savedBy, + 'saved_at' => $savedAt->toIso8601String(), + ]; + } + + private function normalizeCell(mixed $value, array $field, int $line): mixed + { + if ($value === null) { + return ''; + } + + $text = trim((string) $value); + if ($text === '') { + return ''; + } + + if (! in_array($field['kind'] ?? 'text', ['number', 'percent'], true)) { + if (mb_strlen($text) > 120) { + throw ValidationException::withMessages([ + 'rows' => "第 {$line} 行“{$field['label']}”不能超过 120 个字符。", + ]); + } + + return $text; + } + + $isPercentText = ($field['kind'] ?? null) === 'percent' && str_ends_with($text, '%'); + $number = str_replace([',', ',', '¥', '¥', '%'], '', $text); + if (! is_numeric($number)) { + throw ValidationException::withMessages([ + 'rows' => "第 {$line} 行“{$field['label']}”不是有效数字。", + ]); + } + + $normalized = (float) $number; + + return $isPercentText ? $normalized / 100 : $normalized; + } +} diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 1e95b163..bd0bc80f 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -8,6 +8,7 @@ use App\Business\InventoryInboundDetailImporter; use App\Business\InventoryWarehouseNameMappingWorkbookImporter; use App\Business\InventoryWarehouseShortNames; use App\Business\LaborExpenseWorkbookImporter; +use App\Business\LaborExpenseDataStore; use App\Business\CourierPayableSummaryFilter; use App\Business\CourierPayableSummaryStore; use App\Business\FundForecastTemplate; @@ -4740,10 +4741,17 @@ class Pages extends Controller protected function contentForLaborExpenses(): array { + $saved = app(LaborExpenseDataStore::class)->load(); + $sessionRows = session('labor_expense_import_rows'); + $hasSessionRows = is_array($sessionRows); + return [ 'view' => 'business.pages.financial-operations.labor-expenses', 'laborExpenseTable' => config('labor_expense_table'), - 'laborExpenseInitialRows' => session('labor_expense_import_rows', []), + 'laborExpenseInitialRows' => $hasSessionRows ? array_values($sessionRows) : $saved['rows'], + 'laborExpenseInitialDirty' => $hasSessionRows, + 'laborExpenseSavedAt' => $saved['saved_at'], + 'laborExpenseSourceName' => $saved['source_name'], 'laborExpenseImportMessage' => session('labor_expense_import_message'), ]; } @@ -4778,6 +4786,28 @@ class Pages extends Controller ->with('labor_expense_import_message', $message); } + public function saveLaborExpenses(Request $request) + { + $validated = $request->validate([ + 'rows' => ['present', 'array', 'max:' . LaborExpenseDataStore::MAX_ROWS], + 'source_name' => ['nullable', 'string', 'max:191'], + ]); + + $result = app(LaborExpenseDataStore::class)->save( + $validated['rows'], + $validated['source_name'] ?? null, + auth()->id(), + ); + $message = $result['row_count'] > 0 + ? "保存成功:共 {$result['row_count']} 行。" + : '保存成功:当前人工费用数据已清空。'; + + return response()->json([ + 'message' => $message, + ...$result, + ]); + } + /** * 税金预测表逻辑配置:规则清单、数据源状态和运行参数。 */ diff --git a/app/Models/Business/LaborExpenseSnapshot.php b/app/Models/Business/LaborExpenseSnapshot.php new file mode 100644 index 00000000..7088df4e --- /dev/null +++ b/app/Models/Business/LaborExpenseSnapshot.php @@ -0,0 +1,27 @@ + 'integer', + 'rows' => 'array', + 'row_count' => 'integer', + 'saved_by' => 'integer', + 'saved_at' => 'datetime', + ]; +} diff --git a/database/migrations/2026_10_29_180000_create_business_labor_expense_snapshots.php b/database/migrations/2026_10_29_180000_create_business_labor_expense_snapshots.php new file mode 100644 index 00000000..f98cf091 --- /dev/null +++ b/database/migrations/2026_10_29_180000_create_business_labor_expense_snapshots.php @@ -0,0 +1,29 @@ +bigIncrements('id'); + $table->unsignedInteger('company_id'); + $table->string('source_name', 191)->nullable(); + $table->json('rows'); + $table->unsignedInteger('row_count')->default(0); + $table->unsignedInteger('saved_by')->nullable(); + $table->timestamp('saved_at')->nullable(); + $table->timestamps(); + + $table->unique('company_id', 'labor_expense_snapshots_company_unique'); + }); + } + + public function down(): void + { + Schema::dropIfExists('business_labor_expense_snapshots'); + } +}; diff --git a/resources/views/business/pages/financial-operations/labor-expenses.blade.php b/resources/views/business/pages/financial-operations/labor-expenses.blade.php index 7344bc87..f3762dc7 100644 --- a/resources/views/business/pages/financial-operations/labor-expenses.blade.php +++ b/resources/views/business/pages/financial-operations/labor-expenses.blade.php @@ -158,10 +158,30 @@ color: #a22a2a; } + .labor-expense-import-status[data-state="pending"] { + border-color: #dfd0ae; + background: #fffaf0; + color: var(--vip-content-accent-dark, #856632); + } + .labor-expense-import-status[hidden] { display: none; } + .labor-expense-save-state { + color: var(--vip-content-muted, #595959); + font-size: 0.7rem; + white-space: nowrap; + } + + .labor-expense-save-state[data-state="dirty"] { + color: var(--vip-warning, #935F07); + } + + .labor-expense-save-state[data-state="saved"] { + color: var(--vip-success, #53793E); + } + .labor-expense-table-wrap { border: 1px solid var(--vip-border, #D8D2C7); border-radius: 0.5rem; @@ -505,7 +525,7 @@
-
页面不预置源表人员、花名或金额。可导入与当前表头一致的 xlsx、xls 或 csv;系统只读取黄色可录入列并自动重算公式。导入会替换当前页面已有行,数据不写入人员明细库,离开页面后清空。
+
页面首次不预置源表人员、花名或金额。可导入与当前表头一致的 xlsx、xls 或 csv;系统只读取黄色可录入列并自动重算公式。导入或手工修改后请点击“保存数据”,保存后下次进入会自动加载;数据只保存在本测算表,不写入人员明细库。
@@ -531,6 +551,16 @@ 导入 Excel/CSV + + 未保存