feat(供应商对账): 原始账单支持 Excel 导出 (#1720)
原始账单支持按厂家流式导出完整 Excel,保持模板表头并限制为财务端权限。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com>
This commit is contained in:
parent
9b465c111a
commit
aea68c2d2d
@ -15,6 +15,7 @@ use Modules\EcommerceFinance\Services\SupplierReturnConfirmation;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementMatcher;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementTemplate;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementWorkbookExporter;
|
||||
use Modules\EcommerceFinance\Services\SupplierSummaryConfirmation;
|
||||
use Modules\EcommerceFinance\Services\SupplierZeroDifferenceConfirmation;
|
||||
use Modules\EcommerceFinance\View\SupplierReconciliationPageData;
|
||||
@ -38,6 +39,7 @@ class SupplierReconciliation extends Controller
|
||||
private readonly SupplierPortalAccess $supplierPortalAccess,
|
||||
private readonly SupplierSummaryConfirmation $summaryConfirmations,
|
||||
private readonly SupplierZeroDifferenceConfirmation $zeroDifferenceConfirmations,
|
||||
private readonly SupplierStatementWorkbookExporter $statementWorkbookExporter,
|
||||
) {
|
||||
$this->middleware('auth');
|
||||
$this->middleware('company.identify');
|
||||
@ -166,6 +168,23 @@ class SupplierReconciliation extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
public function exportRawStatement(Request $request): StreamedResponse
|
||||
{
|
||||
[$supplier, $companyId] = $this->returnTarget($request);
|
||||
$latestUpload = $this->statements->uploads($supplier, $companyId, 1)[0] ?? null;
|
||||
if ($latestUpload === null) {
|
||||
throw ValidationException::withMessages([
|
||||
'raw_statement_export' => '该厂家还没有上传对账单,暂无原始账单可导出。',
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->statementWorkbookExporter->download(
|
||||
$this->statements->streamRows($supplier, $companyId),
|
||||
$supplier,
|
||||
(string) ($latestUpload['uploaded_at'] ?? ''),
|
||||
);
|
||||
}
|
||||
|
||||
public function saveDingTalkRobot(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
|
||||
@ -324,6 +324,28 @@ class SupplierPayableOverrideStore
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按记录顺序逐行读取数据集,供大文件导出使用。
|
||||
*
|
||||
* @return iterable<int,array<string,mixed>>
|
||||
*/
|
||||
public function streamRows(string $dataset, int $companyId): iterable
|
||||
{
|
||||
$records = $this->connection()->table('supplier_payable_overrides')
|
||||
->where('company_id', $companyId)
|
||||
->where('dataset', $dataset)
|
||||
->orderBy('id')
|
||||
->select(['record_key', 'values'])
|
||||
->cursor();
|
||||
|
||||
foreach ($records as $record) {
|
||||
$values = json_decode((string) $record->values, true) ?: [];
|
||||
$values['_action_key'] = (string) $record->record_key;
|
||||
|
||||
yield $values;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按厂家汇总指定筛选范围的数量和金额,供财务端进度概览使用。
|
||||
*
|
||||
|
||||
@ -41,6 +41,12 @@ class SupplierStatementStore
|
||||
return $this->store->listRows(self::PREFIX . $supplier, $companyId);
|
||||
}
|
||||
|
||||
/** @return iterable<int,array<string,mixed>> */
|
||||
public function streamRows(string $supplier, int $companyId): iterable
|
||||
{
|
||||
yield from $this->store->streamRows(self::PREFIX . $supplier, $companyId);
|
||||
}
|
||||
|
||||
/** @return array{rows:list<array<string,mixed>>,total:int} */
|
||||
public function paginate(string $supplier, int $companyId, int $perPage, int $page): array
|
||||
{
|
||||
|
||||
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\EcommerceFinance\Services;
|
||||
|
||||
use App\Support\CourierBills\StreamingWorkbook;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
/** 财务端导出厂家最新一版上传对账单。 */
|
||||
class SupplierStatementWorkbookExporter
|
||||
{
|
||||
private const FIELDS = [
|
||||
'expressName',
|
||||
'expressNo',
|
||||
'purchaseNo',
|
||||
'createdAt',
|
||||
'supplier',
|
||||
'barcode',
|
||||
'skuName',
|
||||
'qty',
|
||||
'unitPrice',
|
||||
'amount',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param iterable<int,array<string,mixed>> $rows
|
||||
*/
|
||||
public function download(
|
||||
iterable $rows,
|
||||
string $supplier,
|
||||
string $uploadedAt = '',
|
||||
): StreamedResponse {
|
||||
$safeSupplier = preg_replace('/[\\\\\\/:*?"<>|]+/u', '_', $supplier) ?: '厂家';
|
||||
$timestamp = $this->fileTimestamp($uploadedAt);
|
||||
$filename = $safeSupplier . '_原始账单_' . $timestamp . '.xlsx';
|
||||
|
||||
return response()->streamDownload(function () use ($rows): void {
|
||||
$writer = new StreamingWorkbook();
|
||||
$writer->row('原始账单', array_values(SupplierStatementTemplate::COLUMNS), true);
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$cells = [];
|
||||
foreach (self::FIELDS as $field) {
|
||||
$value = $row[$field] ?? '';
|
||||
$cells[] = in_array($field, ['qty', 'unitPrice', 'amount'], true)
|
||||
? $this->numberCell($value)
|
||||
: trim((string) $value);
|
||||
}
|
||||
$writer->row('原始账单', $cells);
|
||||
}
|
||||
|
||||
$output = fopen('php://output', 'wb');
|
||||
if ($output === false) {
|
||||
throw new RuntimeException('无法创建原始账单导出文件。');
|
||||
}
|
||||
$writer->writeTo($output);
|
||||
}, $filename, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Cache-Control' => 'private, no-store',
|
||||
]);
|
||||
}
|
||||
|
||||
private function numberCell(mixed $value): int|float|string
|
||||
{
|
||||
$text = str_replace([',', ',', ' '], '', trim((string) $value));
|
||||
if ($text === '') {
|
||||
return '';
|
||||
}
|
||||
if (preg_match('/^-?\d+$/', $text) === 1) {
|
||||
return (int) $text;
|
||||
}
|
||||
if (is_numeric($text)) {
|
||||
return (float) $text;
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
private function fileTimestamp(string $uploadedAt): string
|
||||
{
|
||||
$uploadedAt = trim($uploadedAt);
|
||||
if ($uploadedAt === '') {
|
||||
return now('Asia/Shanghai')->format('Ymd_His');
|
||||
}
|
||||
|
||||
try {
|
||||
return \Illuminate\Support\Carbon::createFromFormat(
|
||||
'Y-m-d H:i:s',
|
||||
$uploadedAt,
|
||||
'Asia/Shanghai',
|
||||
)->format('Ymd_His');
|
||||
} catch (\Throwable) {
|
||||
return now('Asia/Shanghai')->format('Ymd_His');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -294,6 +294,11 @@
|
||||
<p class="rs-hint">当前显示最新一版上传数据,共 {{ number_format($statementTotal) }} 行。</p>
|
||||
</div>
|
||||
<div class="rs-head-actions">
|
||||
@if ($latestUpload)
|
||||
<a class="rs-button is-primary" href="{{ route('business.supplier-reconciliation.raw-statement.export', [
|
||||
'supplier' => $selectedSupplier,
|
||||
]) }}">导出 Excel</a>
|
||||
@endif
|
||||
<a class="rs-button" href="{{ route('business.pages.show', [
|
||||
'section' => $sectionSlug,
|
||||
'page' => 'finance-data',
|
||||
|
||||
@ -614,6 +614,10 @@ Route::get('workspace/supplier-reconciliation/comparison/export', [SupplierRecon
|
||||
->middleware('business.page:supplier-reconciliation,finance-data')
|
||||
->name('business.supplier-reconciliation.comparison.export');
|
||||
|
||||
Route::get('workspace/supplier-reconciliation/raw-statement/export', [SupplierReconciliationActions::class, 'exportRawStatement'])
|
||||
->middleware('business.page:supplier-reconciliation,finance-data')
|
||||
->name('business.supplier-reconciliation.raw-statement.export');
|
||||
|
||||
// 退货扣减确认:逐行确认/异议 + 一键全部确认
|
||||
Route::post('workspace/supplier-reconciliation/returns/confirm', [SupplierReconciliationActions::class, 'confirmReturn'])
|
||||
->middleware('business.page:supplier-reconciliation,supplier-data')
|
||||
|
||||
@ -24,6 +24,7 @@ use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
||||
use Modules\EcommerceFinance\Services\SupplierStatementTemplate;
|
||||
use Modules\EcommerceFinance\Services\SupplierSummaryConfirmation;
|
||||
use Modules\EcommerceFinance\View\SupplierReconciliationPageData;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class SupplierReconciliationTest extends FeatureTestCase
|
||||
@ -268,6 +269,8 @@ class SupplierReconciliationTest extends FeatureTestCase
|
||||
$firstPage->assertSeeText('厂家上传原始账单')
|
||||
->assertSeeText('原始账单厂家 · 原始账单')
|
||||
->assertSeeText('statement.csv')
|
||||
->assertSeeText('导出 Excel')
|
||||
->assertSee('raw-statement/export', false)
|
||||
->assertSeeText('第 1 / 2 页,每页 50 行')
|
||||
->assertSeeText('RAW-01')
|
||||
->assertSeeText('RAW-50')
|
||||
@ -288,6 +291,29 @@ class SupplierReconciliationTest extends FeatureTestCase
|
||||
->assertOk()
|
||||
->assertSeeText('原始账单')
|
||||
->assertSee('raw-statements', false);
|
||||
|
||||
$export = $this->loginAs()->get(route('business.supplier-reconciliation.raw-statement.export', [
|
||||
'supplier' => '原始账单厂家',
|
||||
]));
|
||||
$export->assertOk()
|
||||
->assertHeader('content-type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||
$tempPath = tempnam(sys_get_temp_dir(), 'raw-statement-test-');
|
||||
if ($tempPath === false) {
|
||||
$this->fail('无法创建原始账单导出测试文件。');
|
||||
}
|
||||
file_put_contents($tempPath, $export->streamedContent());
|
||||
$book = IOFactory::load($tempPath);
|
||||
try {
|
||||
$rows = $book->getActiveSheet()->toArray(null, true, true, false);
|
||||
$this->assertSame(array_values(SupplierStatementTemplate::COLUMNS), $rows[0]);
|
||||
$this->assertCount(53, $rows);
|
||||
$this->assertSame('RAW-01', $rows[1][5]);
|
||||
$this->assertEquals(1, $rows[1][7]);
|
||||
$this->assertEquals(10, $rows[1][9]);
|
||||
} finally {
|
||||
$book->disconnectWorksheets();
|
||||
@unlink($tempPath);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_supplier_portal_cannot_view_the_finance_raw_statement_page(): void
|
||||
@ -302,6 +328,12 @@ class SupplierReconciliationTest extends FeatureTestCase
|
||||
'supplier' => '晨丰',
|
||||
]))
|
||||
->assertForbidden();
|
||||
|
||||
$this->actingAsSupplierPortal($supplierUser)
|
||||
->get(route('business.supplier-reconciliation.raw-statement.export', [
|
||||
'supplier' => '晨丰',
|
||||
]))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_raw_statement_page_inherits_finance_data_page_permission(): void
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user