store->save($this->validated($request), $this->actor()); return $this->back('厂家记账配置已新增,厂家记账明细按新配置重新判定。'); } public function update(Request $request, int $configId) { $this->store->save($this->validated($request), $this->actor(), $configId); return $this->back('厂家记账配置已更新,厂家记账明细按新配置重新判定。'); } public function destroy(int $configId) { $this->store->changeDeleted($configId, true, $this->actor()); return $this->back('配置已删除,该供应商视同不在配置表,按正常记账扣款。'); } public function restore(int $configId) { $this->store->changeDeleted($configId, false, $this->actor()); return $this->back('厂家记账配置已恢复。'); } public function import(Request $request, SupplierBookkeepingConfigImporter $importer) { $request->validate(['file' => ['required', 'file', 'mimes:xlsx', 'max:10240']]); $path = $request->file('file')->getRealPath(); $result = $this->store->import($importer->read($path), $this->actor(), hash_file('sha256', $path)); return $this->back("导入完成:新增{$result['added']}条,更新{$result['updated']}条,未变化{$result['skipped']}条。"); } public function export(SupplierBookkeepingConfigPageData $data) { $rows = $this->store->filtered($data->filters())->get(); $book = new Spreadsheet(); $sheet = $book->getActiveSheet(); $sheet->setTitle('厂家记账配置'); $sheet->fromArray([['供应商', '记账方式', '生效开始日期', '生效结束日期', '原始说明', '创建人', '更新时间', '删除状态']]); foreach ($rows as $index => $row) { $values = [$row->supplier_name, Rule::MODES[$row->mode] ?? $row->mode, $row->effective_from, $row->effective_to, $row->source_text, $row->creator, $row->updated_at, $row->deleted_at ? '已删除' : '正常']; foreach ($values as $column => $value) { $sheet->setCellValueExplicit([$column + 1, $index + 2], (string) $value, DataType::TYPE_STRING); } } foreach (['A' => 24, 'B' => 20, 'C' => 14, 'D' => 14, 'E' => 44, 'F' => 16, 'G' => 20, 'H' => 10] as $column => $width) { $sheet->getColumnDimension($column)->setWidth($width); } $sheet->freezePane('A2'); return response()->streamDownload(function () use ($book): void { try { (new Xlsx($book))->save('php://output'); } finally { $book->disconnectWorksheets(); } }, '厂家记账配置.xlsx', ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); } private function validated(Request $request): array { return $request->validate([ 'supplier_name' => ['required', 'string', 'max:191'], 'mode' => ['required', ValidationRule::in(array_keys(Rule::MODES))], 'effective_from' => ['nullable', 'date_format:Y-m-d'], 'effective_to' => ['nullable', 'date_format:Y-m-d', 'after_or_equal:effective_from'], 'source_text' => ['nullable', 'string', 'max:255'], ], [], ['supplier_name' => '供应商', 'mode' => '记账方式', 'effective_from' => '生效开始日期', 'effective_to' => '生效结束日期', 'source_text' => '备注']); } private function actor(): string { return mb_substr((string) user()->name, 0, 191); } private function back(string $message) { return redirect()->route('business.pages.show', ['section' => 'supplier-payables', 'page' => 'manufacturer-bookkeeping', 'bk_view' => 'config'])->with('bookkeeping_config_message', $message); } }