diff --git a/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php b/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php index eb1c208a..2e7bc47c 100644 --- a/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php +++ b/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php @@ -41,6 +41,10 @@ class KukaReconciliationPageData private const INVENTORY_SAP_CODE = 'OA01207289'; + private const LINGSHAN_SECOND_ADDRESS_START = 44; + + private const LINGSHAN_THIRD_ADDRESS_START = 84; + private const VIRTUAL_SUPPLIER_NAME = '欧伯娜'; private const VIRTUAL_SUPPLIER_CODE = self::INVENTORY_SAP_CODE; @@ -372,6 +376,15 @@ class KukaReconciliationPageData $empty['period'] = $period; $empty['generated_at'] = $generatedAt; $empty['views'] = $views; + $empty['statement'] = [ + 'lingshan' => $this->lingshanStatement( + $period, + $virtualRows, + $erpRecipients, + $virtualQuantityTotal, + $virtualSalesAmountTotal, + ), + ]; $empty['summary'] = [ 'source_rows' => $validMaterialRows + $skippedMissingMaterial, 'valid_material_rows' => $validMaterialRows, @@ -448,7 +461,6 @@ class KukaReconciliationPageData ); } - ksort($groups, SORT_NATURAL); $rows = []; $serial = 0; foreach ($groups as $materialNumber => $group) { @@ -480,6 +492,80 @@ class KukaReconciliationPageData return $rows; } + /** + * @param list> $virtualRows + * @param list $recipients + * @return array + */ + private function lingshanStatement( + string $period, + array $virtualRows, + array $recipients, + string $quantityTotal, + string $amountTotal, + ): array { + $rows = []; + foreach ($virtualRows as $row) { + $serial = (int) ($row[2] ?? (count($rows) + 1)); + $recipientIndex = match (true) { + $serial >= self::LINGSHAN_THIRD_ADDRESS_START => 2, + $serial >= self::LINGSHAN_SECOND_ADDRESS_START => 1, + default => 0, + }; + $lastRecipient = $recipients === [] ? null : $recipients[array_key_last($recipients)]; + $recipient = $recipients[$recipientIndex] ?? $lastRecipient; + $rows[] = [ + 'serial' => $serial, + 'code' => (string) ($row[3] ?? ''), + 'product_name' => (string) ($row[4] ?? ''), + 'specification' => '', + 'unit' => '套', + 'quantity' => (string) ($row[5] ?? '0'), + 'unit_price' => (string) ($row[8] ?? '0.00'), + 'amount' => (string) ($row[9] ?? '0.00'), + 'delivery_address' => $this->fullAddress($recipient), + 'status' => '已完成', + ]; + } + + $year = substr($period, 0, 4); + $month = (int) substr($period, 5, 2); + + return [ + 'title' => "常州灵珊家居有限公司对账单({$year}年{$month}月份)", + 'period' => $period, + 'rows' => $rows, + 'quantity_total' => $this->formatQuantity($quantityTotal), + 'amount_total' => $amountTotal, + ]; + } + + private function fullAddress(?object $recipient): string + { + if ($recipient === null) { + return ''; + } + + $province = trim((string) ($recipient->receiver_state ?? '')); + $city = trim((string) ($recipient->receiver_city ?? '')); + $district = trim((string) ($recipient->receiver_district ?? '')); + $address = trim((string) ($recipient->receiver_address ?? '')); + $prefix = $province.$city.$district; + + if ($address === '') { + return $prefix; + } + if (($province !== '' && str_starts_with($address, $province)) + || ($city !== '' && str_starts_with($address, $city)) + || ($district !== '' && str_starts_with($address, $district))) { + return ($province !== '' && ! str_starts_with($address, $province) ? $province : '') + .($city !== '' && ! str_starts_with($address, $province) && ! str_starts_with($address, $city) ? $city : '') + .$address; + } + + return $prefix.$address; + } + /** * @param list> $virtualRows * @return list> @@ -814,6 +900,9 @@ class KukaReconciliationPageData 'periods' => $periods, 'page' => $page, 'views' => $views, + 'statement' => [ + 'lingshan' => $this->lingshanStatement($period, [], [], '0', '0.00'), + ], 'generated_at' => null, 'summary' => [ 'source_rows' => 0, diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index e16b298a..95efe64b 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -8590,6 +8590,28 @@ class Pages extends Controller ]); } + public function printKukaLingshanStatement(Request $request) + { + try { + $data = app(KukaReconciliationPageData::class)->build( + $request, + (int) (company_id() ?: 1), + ); + } catch (Throwable $exception) { + report($exception); + abort(503, '灵珊对账单生成失败,请稍后重试。'); + } + + $statement = $data['statement']['lingshan'] ?? null; + if (! empty($data['error']) || ! is_array($statement)) { + abort(503, (string) ($data['error'] ?? '灵珊对账单数据尚未生成。')); + } + + return view('business.pages.posting-data.lingshan-statement-print', [ + 'statement' => $statement, + ]); + } + public function export(Request $request, string $section, string $page, string $tab) { if ($section === 'financial-operations' && $page === 'shop-flows') { diff --git a/resources/views/business/pages/posting-data/kuka-reconciliation.blade.php b/resources/views/business/pages/posting-data/kuka-reconciliation.blade.php index fa508b40..69a5cffc 100644 --- a/resources/views/business/pages/posting-data/kuka-reconciliation.blade.php +++ b/resources/views/business/pages/posting-data/kuka-reconciliation.blade.php @@ -129,7 +129,7 @@ 'slug' => 'lingshan-statement', 'title' => '灵珊对账单', 'icon' => 'receipt_long', - 'description' => '灵珊对账单页面已建立,待接入对账数据。', + 'description' => '按表二格式生成灵珊月度对账单,可直接打印。', ], [ 'slug' => 'oberna-statement', @@ -213,7 +213,14 @@ 'amountTotal' => $kukaReconciliation['summary']['invoice_amount_total'] ?? '0.00', ]) @endif - @if (! empty($tab['headers'])) + @if ($tab['slug'] === 'lingshan-statement' && empty($kukaReconciliation['error'])) + @include('business.pages.posting-data.partials.kuka-lingshan-statement-tab', [ + 'statement' => $kukaReconciliation['statement']['lingshan'] ?? [], + 'printUrl' => route('business.kuka-reconciliation.lingshan-statement.print', [ + 'period' => $kukaReconciliation['period'], + ]), + ]) + @elseif (! empty($tab['headers'])) @include($sourceTable, [ 'title' => $tab['title'], 'description' => $tab['description'], diff --git a/resources/views/business/pages/posting-data/lingshan-statement-print.blade.php b/resources/views/business/pages/posting-data/lingshan-statement-print.blade.php new file mode 100644 index 00000000..016f5913 --- /dev/null +++ b/resources/views/business/pages/posting-data/lingshan-statement-print.blade.php @@ -0,0 +1,73 @@ + + + {{ $statement['title'] ?? '灵珊对账单' }} + + + + @include('business.pages.posting-data.partials.kuka-lingshan-statement-sheet', [ + 'statement' => $statement, + ]) + + + + diff --git a/resources/views/business/pages/posting-data/partials/kuka-lingshan-statement-sheet.blade.php b/resources/views/business/pages/posting-data/partials/kuka-lingshan-statement-sheet.blade.php new file mode 100644 index 00000000..5b15bc23 --- /dev/null +++ b/resources/views/business/pages/posting-data/partials/kuka-lingshan-statement-sheet.blade.php @@ -0,0 +1,149 @@ +@php + $rows = $statement['rows'] ?? []; +@endphp +
+

{{ $statement['title'] ?? '常州灵珊家居有限公司对账单' }}

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @forelse ($rows as $row) + + + + + + + + + + + + + @empty + + + + @endforelse + + + + + + + + + +
序号代码产品名称商品规格单位数量单价金额交货地址状态
{{ $row['serial'] }}{{ $row['code'] }}{{ $row['product_name'] }}{{ $row['specification'] }}{{ $row['unit'] }}{{ $row['quantity'] }}{{ number_format((float) $row['unit_price'], 2, '.', ',') }}{{ number_format((float) $row['amount'], 2, '.', ',') }}{{ $row['delivery_address'] }}{{ $row['status'] }}
当前账期没有可生成的对账单数据。
总金额:{{ $statement['quantity_total'] ?? '0' }}{{ number_format((float) ($statement['amount_total'] ?? 0), 2, '.', ',') }}
+ +
+ 公司盖章: + +
+
+ + diff --git a/resources/views/business/pages/posting-data/partials/kuka-lingshan-statement-tab.blade.php b/resources/views/business/pages/posting-data/partials/kuka-lingshan-statement-tab.blade.php new file mode 100644 index 00000000..72832710 --- /dev/null +++ b/resources/views/business/pages/posting-data/partials/kuka-lingshan-statement-tab.blade.php @@ -0,0 +1,123 @@ +
+
+
+ +
+ 表二打印预览 + 按当前账期数据实时生成,打印时使用 A4 横向版式。 +
+
+ + + 打印对账单 + +
+ +
+ @include('business.pages.posting-data.partials.kuka-lingshan-statement-sheet', [ + 'statement' => $statement, + ]) +
+
+ + diff --git a/routes/admin.php b/routes/admin.php index d3aa8494..2f143817 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -681,6 +681,10 @@ Route::get('workspace/posting-data/kuka-reconciliation/posting-data/export', 'Bu ->middleware('business.page:posting-data,kuka-reconciliation') ->name('business.kuka-reconciliation.posting-data.export'); +Route::get('workspace/posting-data/kuka-reconciliation/lingshan-statement/print', 'Business\Pages@printKukaLingshanStatement') + ->middleware('business.page:posting-data,kuka-reconciliation') + ->name('business.kuka-reconciliation.lingshan-statement.print'); + Route::get('workspace/invoice-management/invoice-quota/export', 'Business\Pages@exportInvoiceQuota') ->middleware('business.page:invoice-management,invoice-quota') ->name('business.invoice-quota.export'); diff --git a/tests/Feature/Business/KukaReconciliationPageTest.php b/tests/Feature/Business/KukaReconciliationPageTest.php index db019b30..c16e122e 100644 --- a/tests/Feature/Business/KukaReconciliationPageTest.php +++ b/tests/Feature/Business/KukaReconciliationPageTest.php @@ -51,6 +51,9 @@ class KukaReconciliationPageTest extends FeatureTestCase ->assertSee('data-kuka-erp-summary-card="paid-amount"', false) ->assertSee('data-kuka-invoice-summary-card="quantity"', false) ->assertSee('data-kuka-invoice-summary-card="amount"', false) + ->assertSee('data-kuka-lingshan-statement-sheet', false) + ->assertSee('data-kuka-lingshan-statement-print', false) + ->assertSeeText('表二打印预览') ->assertDontSeeText('有效物料行') ->assertSeeText('供货商金额') ->assertSeeText('N单号') @@ -60,7 +63,6 @@ class KukaReconciliationPageTest extends FeatureTestCase ->assertSeeText('提交人电话(手机)') ->assertSeeText('toElsAccount') ->assertSeeText('实际可用库存数量') - ->assertSeeText('灵珊对账单页面已建立') ->assertSeeText('欧伯娜对账单页面已建立'); $html = $response->getContent(); @@ -129,6 +131,89 @@ class KukaReconciliationPageTest extends FeatureTestCase ->assertSeeText('发货数量'); } + public function test_lingshan_statement_preview_and_print_page_use_table_two_layout(): void + { + $connection = DB::connection('business_mirror'); + $now = now(); + $connection->table('kuka_posting_base_imports')->insert([ + 'company_id' => $this->company->id, + 'period' => '2026-10', + 'row_count' => 2, + 'store_count' => 1, + 'order_count' => 2, + 'store_counts' => '{"天猫-HX旗舰店101":2}', + 'file_name' => 'test.xlsx', + 'file_sha256' => str_repeat('e', 64), + 'source_sheet' => '发货明细表', + 'completed_at' => $now, + 'created_at' => $now, + 'updated_at' => $now, + ]); + + foreach ([2, 3] as $rowNumber) { + $payload = [ + '内部订单号' => 'INTERNAL-'.$rowNumber, + '线上订单号' => 'ONLINE-'.$rowNumber, + '订单状态' => '已发货', + '发货仓' => '测试仓', + '店铺' => '天猫-HX旗舰店101', + '商品编码' => 'PRODUCT-1', + '物料编号' => 'MATERIAL-1', + '型号' => '测试物料', + '商品名称' => '测试商品', + '成本' => '10.00', + '实发数量' => '1', + '过账标签' => '是', + ]; + $encoded = json_encode( + $payload, + JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, + ); + $connection->table('kuka_posting_base_rows')->insert([ + 'company_id' => $this->company->id, + 'period' => '2026-10', + 'store_name' => '天猫-HX旗舰店101', + 'internal_order_no' => $payload['内部订单号'], + 'online_order_no' => $payload['线上订单号'], + 'product_code' => 'PRODUCT-1', + 'order_status' => '已发货', + 'row_hash' => hash('sha256', $encoded), + 'source_values' => $encoded, + 'source_row_number' => $rowNumber, + 'imported_at' => $now, + ]); + } + + $this->loginAs() + ->get(route('business.pages.show', [ + 'section' => 'posting-data', + 'page' => 'kuka-reconciliation', + 'period' => '2026-10', + 'reconciliation_view' => 'lingshan-statement', + ])) + ->assertOk() + ->assertSeeText('常州灵珊家居有限公司对账单(2026年10月份)') + ->assertSeeText('表二打印预览') + ->assertSeeText('打印对账单') + ->assertSeeText('代码') + ->assertSeeText('产品名称') + ->assertSeeText('交货地址') + ->assertSeeText('公司盖章:') + ->assertSee('data-kuka-lingshan-statement-sheet', false) + ->assertSee('data-kuka-lingshan-statement-print', false); + + $this->loginAs() + ->get(route('business.kuka-reconciliation.lingshan-statement.print', [ + 'period' => '2026-10', + ])) + ->assertOk() + ->assertSee('onload="window.print();"', false) + ->assertSeeText('常州灵珊家居有限公司对账单(2026年10月份)') + ->assertSeeText('公司盖章:') + ->assertSeeText('2') + ->assertSeeText('22.22'); + } + public function test_virtual_posting_renders_material_pivot_without_rounding_footer(): void { $connection = DB::connection('business_mirror'); diff --git a/tests/Unit/Business/KukaReconciliationPageDataTest.php b/tests/Unit/Business/KukaReconciliationPageDataTest.php index f84e4fe9..999357b4 100644 --- a/tests/Unit/Business/KukaReconciliationPageDataTest.php +++ b/tests/Unit/Business/KukaReconciliationPageDataTest.php @@ -329,6 +329,24 @@ class KukaReconciliationPageDataTest extends TestCase '2', '', ], $data['views']['inventory-management-template']['rows'][0]); + + $statement = $data['statement']['lingshan']; + $this->assertSame('常州灵珊家居有限公司对账单(2026年10月份)', $statement['title']); + $this->assertCount(1, $statement['rows']); + $this->assertSame('2', $statement['quantity_total']); + $this->assertSame('22.22', $statement['amount_total']); + $this->assertSame([ + 'serial' => 1, + 'code' => 'M-1', + 'product_name' => '测试物料', + 'specification' => '', + 'unit' => '套', + 'quantity' => '2', + 'unit_price' => '11.11', + 'amount' => '22.22', + 'delivery_address' => '江苏省常州市武进区测试路1号', + 'status' => '已完成', + ], $statement['rows'][0]); } public function test_virtual_sales_summary_uses_pivoted_supplier_amount(): void @@ -482,6 +500,63 @@ class KukaReconciliationPageDataTest extends TestCase } } + public function test_virtual_pivot_preserves_source_material_order(): void + { + $connection = DB::connection('business_mirror'); + $connection->table('kuka_posting_base_imports')->insert([ + 'company_id' => 1, + 'period' => '2026-10', + 'completed_at' => '2026-11-01 08:00:00', + ]); + $this->insertBaseRow(2, $this->payload('O-1', 'P-2', 'S-2', 'I-2', 'M-2', '1', '10.00')); + $this->insertBaseRow(3, $this->payload('O-2', 'P-1', 'S-1', 'I-1', 'M-1', '1', '10.00')); + + $data = app(KukaReconciliationPageData::class)->build( + Request::create('/', 'GET', ['period' => '2026-10']), + 1, + ); + + $this->assertSame(['M-2', 'M-1'], array_column($data['views']['lingshan-virtual-posting']['rows'], 3)); + $this->assertSame(['M-2', 'M-1'], array_column($data['statement']['lingshan']['rows'], 'code')); + } + + public function test_statement_uses_three_consecutive_delivery_address_groups(): void + { + $connection = DB::connection('business_mirror'); + $connection->table('kuka_posting_base_imports')->insert([ + 'company_id' => 1, + 'period' => '2026-10', + 'completed_at' => '2026-11-01 08:00:00', + ]); + for ($index = 1; $index <= 84; $index++) { + $this->insertBaseRow( + $index + 1, + $this->payload( + 'O-'.$index, + 'P-'.$index, + 'S-'.$index, + 'I-'.$index, + sprintf('M-%03d', $index), + '1', + '10.00', + ), + ); + } + + $data = app(KukaReconciliationPageData::class)->build( + Request::create('/', 'GET', ['period' => '2026-10']), + 1, + ); + $rows = $data['statement']['lingshan']['rows']; + + $this->assertCount(84, $rows); + $this->assertSame('江苏省常州市武进区测试路1号', $rows[0]['delivery_address']); + $this->assertSame('江苏省常州市武进区测试路1号', $rows[42]['delivery_address']); + $this->assertSame('浙江省杭州市西湖区测试路2号', $rows[43]['delivery_address']); + $this->assertSame('浙江省杭州市西湖区测试路2号', $rows[82]['delivery_address']); + $this->assertSame('上海市浦东新区测试路3号', $rows[83]['delivery_address']); + } + public function test_posting_export_returns_every_material_row_without_pagination(): void { $connection = DB::connection('business_mirror');