From c658bbed99b50c586ec3f7fdaa6eb5d4596ab0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=9C=A8?= <594326199@qq.com> Date: Sun, 15 Nov 2026 11:28:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=9C=AA=E5=AE=9A?= =?UTF-8?q?=E4=BB=B7=E8=B4=A6=E6=9C=9F=E6=B1=87=E6=80=BB=E4=B8=8E=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=20(#1179)=20Co-authored-by:=20=E8=8B=8F=E6=9C=A8=20<5?= =?UTF-8?q?94326199@qq.com>=20Co-committed-by:=20=E8=8B=8F=E6=9C=A8=20<594?= =?UTF-8?q?326199@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Business/Pages.php | 40 ++++ .../Business/PayablesUnpricedCycleExport.php | 214 ++++++++++++++++++ ...previous-unpriced-current-priced.blade.php | 31 ++- routes/admin.php | 8 + .../Business/PayablesUnpricedCycleTest.php | 36 ++- 5 files changed, 325 insertions(+), 4 deletions(-) create mode 100644 app/Http/Controllers/Business/PayablesUnpricedCycleExport.php diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 6c3f7306..5ea76524 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -4411,6 +4411,8 @@ class Pages extends Controller ), static fn (array $row): bool => (string) ($row['_accounting_period'] ?? '') === $period, )); + $deliverySummary = $this->unpricedCycleSummary($deliveryRows, 'amount', 'guessAmount'); + $returnSummary = $this->unpricedCycleSummary($returnRows, 'amount', 'guess_amount'); $deliveryPage = max(1, (int) request('delivery_page', 1)); $returnPage = max(1, (int) request('return_page', 1)); @@ -4419,6 +4421,12 @@ class Pages extends Controller 'dataset' => 'previous-unpriced-current-priced', 'period' => $period, 'active_tab' => $activeTab, + 'delivery_summary' => $deliverySummary, + 'return_summary' => $returnSummary, + 'export_url' => route( + 'business.supplier-payables-final.previous-unpriced-current-priced.export', + ['month' => $period], + ), 'delivery_fields' => collect($registry->fields($sectionSlug, 'delivery-unpriced')) ->map(static fn (array $definition, string $name): array => ['name' => $name] + $definition) ->values() @@ -4452,6 +4460,38 @@ class Pages extends Controller ]; } + /** + * @param list> $rows + * @return array{record_count:int,quantity:float,amount:float,estimated_amount:float} + */ + private function unpricedCycleSummary( + array $rows, + string $amountField, + string $estimatedAmountField, + ): array { + $quantity = 0.0; + $amount = 0.0; + $estimatedAmount = 0.0; + foreach ($rows as $row) { + if (is_numeric($row['qty'] ?? null)) { + $quantity += (float) $row['qty']; + } + if (is_numeric($row[$amountField] ?? null)) { + $amount += (float) $row[$amountField]; + } + if (is_numeric($row[$estimatedAmountField] ?? null)) { + $estimatedAmount += (float) $row[$estimatedAmountField]; + } + } + + return [ + 'record_count' => count($rows), + 'quantity' => $quantity, + 'amount' => $amount, + 'estimated_amount' => $estimatedAmount, + ]; + } + /** @return array{0:LengthAwarePaginator,1:?string} */ private function inventoryReportRows(): array { diff --git a/app/Http/Controllers/Business/PayablesUnpricedCycleExport.php b/app/Http/Controllers/Business/PayablesUnpricedCycleExport.php new file mode 100644 index 00000000..aa5801e6 --- /dev/null +++ b/app/Http/Controllers/Business/PayablesUnpricedCycleExport.php @@ -0,0 +1,214 @@ +middleware('auth'); + $this->middleware('company.identify'); + } + + public function export(Request $request): StreamedResponse + { + $requestedPeriod = trim((string) $request->query('month', '')); + $period = preg_match('/^\d{4}-\d{2}$/', $requestedPeriod) === 1 + ? $requestedPeriod + : now()->subMonthNoOverflow()->format('Y-m'); + $companyId = (int) (company_id() ?: 1); + + $deliveryRows = $this->periodRows( + PayablesUnpricedCycleProcessor::DELIVERY_PRICED_DATASET, + $period, + $companyId, + ); + $returnRows = $this->periodRows( + PayablesUnpricedCycleProcessor::RETURN_PRICED_DATASET, + $period, + $companyId, + ); + $deliverySummary = $this->summary($deliveryRows, 'guessAmount', 'amount'); + $returnSummary = $this->summary($returnRows, 'guess_amount', 'amount'); + + $spreadsheet = new Spreadsheet(); + $summarySheet = $spreadsheet->getActiveSheet(); + $summarySheet->setTitle('账期汇总'); + $summarySheet->fromArray([ + ['类型', '记录数', '件数', '金额', '预估金额'], + ['送货未定价', $deliverySummary['count'], $deliverySummary['quantity'], $deliverySummary['amount'], $deliverySummary['estimated_amount']], + ['退货未定价', $returnSummary['count'], $returnSummary['quantity'], $returnSummary['amount'], $returnSummary['estimated_amount']], + ], null, 'A1'); + $this->styleSheet($summarySheet, 5, 3, [2 => 'number', 3 => 'number', 4 => 'number', 5 => 'number']); + $summarySheet->getColumnDimension('A')->setWidth(18); + foreach (['B', 'C', 'D', 'E'] as $column) { + $summarySheet->getColumnDimension($column)->setWidth(18); + } + + $deliverySheet = $spreadsheet->createSheet(); + $this->writeDetailSheet( + $deliverySheet, + '送货未定价', + $this->registry->fields(PayablesFinalRegistry::SECTION_SUPPLIER, 'delivery-unpriced'), + $deliveryRows, + ); + + $returnSheet = $spreadsheet->createSheet(); + $this->writeDetailSheet( + $returnSheet, + '退货未定价', + $this->registry->fields(PayablesFinalRegistry::SECTION_SUPPLIER, 'return-unpriced'), + $returnRows, + ); + $spreadsheet->setActiveSheetIndex(0); + + $fileName = '之前月未定价-本月定价_' . $period . '_' . now()->format('Ymd_His') . '.xlsx'; + + return response()->streamDownload(function () use ($spreadsheet): void { + (new Xlsx($spreadsheet))->save('php://output'); + $spreadsheet->disconnectWorksheets(); + }, $fileName, [ + 'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'Cache-Control' => 'private, no-store', + ]); + } + + /** + * @return list> + */ + private function periodRows(string $dataset, string $period, int $companyId): array + { + return array_values(array_filter( + $this->store->listRows($dataset, $companyId), + static fn (array $row): bool => (string) ($row['_accounting_period'] ?? '') === $period, + )); + } + + /** + * @param list> $rows + * @return array{count:int,quantity:float,amount:float,estimated_amount:float} + */ + private function summary(array $rows, string $estimatedAmountField, string $amountField): array + { + $quantity = 0.0; + $amount = 0.0; + $estimatedAmount = 0.0; + foreach ($rows as $row) { + if (is_numeric($row['qty'] ?? null)) { + $quantity += (float) $row['qty']; + } + if (is_numeric($row[$amountField] ?? null)) { + $amount += (float) $row[$amountField]; + } + if (is_numeric($row[$estimatedAmountField] ?? null)) { + $estimatedAmount += (float) $row[$estimatedAmountField]; + } + } + + return [ + 'count' => count($rows), + 'quantity' => $quantity, + 'amount' => $amount, + 'estimated_amount' => $estimatedAmount, + ]; + } + + /** + * @param array $fields + * @param list> $rows + */ + private function writeDetailSheet( + \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet, + string $title, + array $fields, + array $rows, + ): void { + $sheet->setTitle($title); + $headers = array_merge(['序号'], array_column($fields, 'label')); + $sheet->fromArray($headers, null, 'A1'); + + $data = []; + foreach ($rows as $index => $row) { + $line = [$index + 1]; + foreach ($fields as $field => $definition) { + $value = $row[$field] ?? ''; + $line[] = $definition['type'] === 'number' && is_numeric($value) + ? (float) $value + : (string) $value; + } + $data[] = $line; + } + if ($data !== []) { + $sheet->fromArray($data, null, 'A2'); + } + + $lastColumn = Coordinate::stringFromColumnIndex(count($headers)); + $lastRow = max(1, count($data) + 1); + $types = []; + $columnIndex = 2; + foreach ($fields as $definition) { + if ($definition['type'] === 'number') { + $types[$columnIndex] = 'number'; + } + $column = Coordinate::stringFromColumnIndex($columnIndex); + $sheet->getColumnDimension($column)->setWidth(min(32, max(12, mb_strlen($definition['label']) * 2 + 5))); + $columnIndex++; + } + $sheet->getColumnDimension('A')->setWidth(9); + $this->styleSheet($sheet, count($headers), $lastRow, $types); + $sheet->freezePane('A2'); + $sheet->setAutoFilter('A1:' . $lastColumn . $lastRow); + } + + /** + * @param array $types + */ + private function styleSheet( + \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet, + int $columnCount, + int $rowCount, + array $types, + ): void { + $lastColumn = Coordinate::stringFromColumnIndex($columnCount); + $headerRange = 'A1:' . $lastColumn . '1'; + $sheet->getStyle($headerRange)->applyFromArray([ + 'font' => ['bold' => true, 'color' => ['rgb' => 'FFFFFF']], + 'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => '856632']], + 'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER], + 'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => 'D8D2C7']]], + ]); + $sheet->getRowDimension(1)->setRowHeight(24); + if ($rowCount > 1) { + $sheet->getStyle('A2:' . $lastColumn . $rowCount) + ->getBorders() + ->getAllBorders() + ->setBorderStyle(Border::BORDER_THIN) + ->getColor() + ->setRGB('E7E2D8'); + } + foreach ($types as $index => $type) { + if ($type !== 'number') { + continue; + } + $column = Coordinate::stringFromColumnIndex($index); + $sheet->getStyle($column . '2:' . $column . max(2, $rowCount)) + ->getNumberFormat() + ->setFormatCode('#,##0.00'); + } + } +} diff --git a/resources/views/business/pages/partials/previous-unpriced-current-priced.blade.php b/resources/views/business/pages/partials/previous-unpriced-current-priced.blade.php index f05705eb..156791bc 100644 --- a/resources/views/business/pages/partials/previous-unpriced-current-priced.blade.php +++ b/resources/views/business/pages/partials/previous-unpriced-current-priced.blade.php @@ -5,6 +5,9 @@ $returnFields = $cycle['return_fields'] ?? []; $deliveryRows = $cycle['delivery_rows']; $returnRows = $cycle['return_rows']; + $deliverySummary = $cycle['delivery_summary'] ?? []; + $returnSummary = $cycle['return_summary'] ?? []; + $exportUrl = $cycle['export_url'] ?? ''; @endphp
@@ -30,8 +33,11 @@ display: inline-flex; height: 2.25rem; align-items: center; justify-content: center; gap: 0.35rem; padding: 0 0.85rem; border: 1px solid var(--cycle-border); border-radius: 0.5rem; background: var(--vip-content-accent-dark, #856632); color: #fff; - font-size: 0.8125rem; font-weight: 600; cursor: pointer; + font-size: 0.8125rem; font-weight: 600; cursor: pointer; text-decoration: none; } + .previous-priced-button.is-secondary { background: var(--vip-surface, #FFFFFF); color: var(--vip-content-ink, #171717); } + .previous-priced-button.is-secondary:hover, + .previous-priced-button.is-secondary:focus { border-color: var(--vip-content-accent-dark, #856632); color: var(--vip-content-accent-dark, #856632); } .previous-priced-tabs { display: flex; gap: 1.5rem; margin-bottom: 0.75rem; padding: 0 1rem; border: 1px solid var(--cycle-border); border-radius: 0.5rem 0.5rem 0 0; @@ -58,6 +64,13 @@ border: 1px solid var(--cycle-border); border-top: 0; border-radius: 0 0 0.5rem 0.5rem; background: var(--vip-surface, #FFFFFF); overflow: hidden; } + .previous-priced-summary { + display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem 1.4rem; + margin-bottom: 0.75rem; padding: 0.75rem 1rem; border: 1px solid var(--cycle-border); + border-radius: 0.5rem; background: var(--vip-surface, #FFFFFF); + } + .previous-priced-summary__item { display: inline-flex; align-items: baseline; gap: 0.35rem; color: var(--vip-content-muted, #595959); font-size: 0.75rem; } + .previous-priced-summary__value { color: var(--vip-content-ink, #171717); font-size: 0.8125rem; font-weight: 700; font-variant-numeric: tabular-nums; } .previous-priced-scroll { width: 100%; overflow-x: auto; } .previous-priced-table { width: max-content; min-width: 100%; border-collapse: collapse; font-size: 0.8125rem; } .previous-priced-table th, @@ -99,6 +112,10 @@ 查询 + + + 导出 +
@@ -132,6 +149,12 @@ data-unpriced-cycle-pane="delivery" @if ($activeTab !== 'delivery') hidden @endif > +
+ 记录数 {{ number_format((int) ($deliverySummary['record_count'] ?? 0)) }} + 件数 {{ number_format((float) ($deliverySummary['quantity'] ?? 0), 2) }} + 金额 ¥{{ number_format((float) ($deliverySummary['amount'] ?? 0), 2) }} + 预估金额 ¥{{ number_format((float) ($deliverySummary['estimated_amount'] ?? 0), 2) }} +
@@ -190,6 +213,12 @@ data-unpriced-cycle-pane="return" @if ($activeTab !== 'return') hidden @endif > +
+ 记录数 {{ number_format((int) ($returnSummary['record_count'] ?? 0)) }} + 件数 {{ number_format((float) ($returnSummary['quantity'] ?? 0), 2) }} + 金额 ¥{{ number_format((float) ($returnSummary['amount'] ?? 0), 2) }} + 预估金额 ¥{{ number_format((float) ($returnSummary['estimated_amount'] ?? 0), 2) }} +
diff --git a/routes/admin.php b/routes/admin.php index 5fd7b11a..98f23b0d 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -17,6 +17,7 @@ use App\Http\Controllers\Business\ServiceCompanyReconciliation as ServiceCompany use App\Http\Controllers\Business\SupplierPayableActions; use App\Http\Controllers\Business\SupplierNegativeAlertDingTalkActions; use App\Http\Controllers\Business\PayablesFinalActions; +use App\Http\Controllers\Business\PayablesUnpricedCycleExport; use App\Http\Controllers\Business\SupplierReconciliation as SupplierReconciliationActions; use App\Http\Controllers\Business\SystemizationTodos; use App\Http\Controllers\Business\WithdrawalManagement; @@ -460,6 +461,13 @@ foreach ($payablesFinalDatasets as $finalSection => $finalDatasets) { } } +Route::get( + 'workspace/supplier-payables-final/actions/previous-unpriced-current-priced/export', + [PayablesUnpricedCycleExport::class, 'export'], +) + ->middleware('business.page:supplier-payables-final,previous-unpriced-current-priced') + ->name('business.supplier-payables-final.previous-unpriced-current-priced.export'); + // 供应商对账:对账单模板下载 + 厂家对账单上传(严格按模板校验) Route::get('workspace/supplier-portal', [\App\Http\Controllers\Business\SupplierPortal::class, 'home']) ->middleware('business.page:supplier-reconciliation,supplier-data') diff --git a/tests/Feature/Business/PayablesUnpricedCycleTest.php b/tests/Feature/Business/PayablesUnpricedCycleTest.php index fc4cb149..b93a92b2 100644 --- a/tests/Feature/Business/PayablesUnpricedCycleTest.php +++ b/tests/Feature/Business/PayablesUnpricedCycleTest.php @@ -134,13 +134,13 @@ class PayablesUnpricedCycleTest extends FeatureTestCase $store->saveBatch(PayablesUnpricedCycleProcessor::DELIVERY_PRICED_DATASET, [ 'id:page:delivery' => [ - 'barCode' => 'DELIVERY-PRICE-1', 'qty' => '2', 'unitPrice' => '10', 'amount' => '20', + 'barCode' => 'DELIVERY-PRICE-1', 'qty' => '2', 'unitPrice' => '10', 'amount' => '20', 'guessAmount' => '30', '_accounting_period' => '2026-10', ], ], $companyId, (int) $this->user->id, 'test'); $store->saveBatch(PayablesUnpricedCycleProcessor::RETURN_PRICED_DATASET, [ 'id:page:return' => [ - 'barcode' => 'RETURN-PRICE-1', 'qty' => '1', 'unit_price' => '8', 'amount' => '8', + 'barcode' => 'RETURN-PRICE-1', 'qty' => '1', 'unit_price' => '8', 'amount' => '8', 'guess_amount' => '10', '_accounting_period' => '2026-10', ], ], $companyId, (int) $this->user->id, 'test'); @@ -159,7 +159,37 @@ class PayablesUnpricedCycleTest extends FeatureTestCase ->assertSeeText('采购金额') ->assertSeeText('DELIVERY-PRICE-1') ->assertSeeText('RETURN-PRICE-1') - ->assertSee('value="2026-10"', false); + ->assertSee('value="2026-10"', false) + ->assertSee('data-cycle-summary="delivery-record-count">1', false) + ->assertSee('data-cycle-summary="delivery-quantity">2.00', false) + ->assertSee('data-cycle-summary="delivery-amount">¥20.00', false) + ->assertSee('data-cycle-summary="return-estimated-amount">¥10.00', false) + ->assertSee('>导出', false); + + $export = $this->loginAs()->get(route( + 'business.supplier-payables-final.previous-unpriced-current-priced.export', + ['month' => '2026-10'], + ))->assertOk(); + $path = tempnam(sys_get_temp_dir(), 'caiwu-cycle-export') . '.xlsx'; + + try { + file_put_contents($path, $export->streamedContent()); + $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path); + $this->assertSame(['账期汇总', '送货未定价', '退货未定价'], $spreadsheet->getSheetNames()); + $summary = $spreadsheet->getSheetByName('账期汇总')->rangeToArray('A1:E3'); + $this->assertSame(['类型', '记录数', '件数', '金额', '预估金额'], $summary[0]); + $this->assertSame('送货未定价', $summary[1][0]); + $this->assertSame(2.0, (float) $summary[1][2]); + $this->assertSame(20.0, (float) $summary[1][3]); + $deliveryHeader = $spreadsheet->getSheetByName('送货未定价')->rangeToArray('A1:T1')[0]; + $this->assertContains('计划单号', $deliveryHeader); + $this->assertContains('预估金额', $deliveryHeader); + $returnHeader = $spreadsheet->getSheetByName('退货未定价')->rangeToArray('A1:Q1')[0]; + $this->assertContains('采购金额', $returnHeader); + $spreadsheet->disconnectWorksheets(); + } finally { + @unlink($path); + } } private function makeFile(string $name, string $content): string