diff --git a/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php b/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php index e5f4f392..eb1c208a 100644 --- a/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php +++ b/app/Business/KukaPostingReconciliation/KukaReconciliationPageData.php @@ -146,10 +146,6 @@ class KukaReconciliationPageData $lineItems = []; $posting = []; $orders = []; - $postingQuantityTotal = '0'; - $postingSupplierAmountTotal = '0.00'; - $postingRowSalesAmountTotal = '0.00'; - $postingSalesAmountTotal = '0.00'; $supplierAmountTotal = '0.00'; $salesAmountTotal = '0.00'; $shippingMatched = 0; @@ -301,18 +297,6 @@ class KukaReconciliationPageData $posting[$materialNumber]['supplier_amount'], $supplierAmount, ); - $postingQuantityTotal = $this->addDecimal( - $postingQuantityTotal, - $quantity, - ); - $postingSupplierAmountTotal = $this->addMoney( - $postingSupplierAmountTotal, - $supplierAmount, - ); - $postingRowSalesAmountTotal = $this->addMoney( - $postingRowSalesAmountTotal, - $salesAmount, - ); } $supplierAmountTotal = $this->addMoney($supplierAmountTotal, $supplierAmount); @@ -321,14 +305,6 @@ class KukaReconciliationPageData ksort($posting, SORT_NATURAL); $generatedAt = now()->format('Y-m-d H:i:s'); - $postingSalesAmountTotal = $this->divideMoney( - $postingSupplierAmountTotal, - self::SALES_MARKUP_DIVISOR, - ); - $virtualSalesRoundingAdjustment = $this->subtractMoney( - $postingSalesAmountTotal, - $postingRowSalesAmountTotal, - ); $postingRows = array_map(fn (array $row): array => [ $row['year'], @@ -340,6 +316,14 @@ class KukaReconciliationPageData $row['supplier_amount'], ], array_values($posting)); $virtualRows = $this->virtualRows($lineItems, $generatedAt); + $virtualQuantityTotal = '0'; + $virtualSupplierAmountTotal = '0.00'; + $virtualSalesAmountTotal = '0.00'; + foreach ($virtualRows as $virtualRow) { + $virtualQuantityTotal = $this->addDecimal($virtualQuantityTotal, (string) ($virtualRow[5] ?? '0')); + $virtualSupplierAmountTotal = $this->addMoney($virtualSupplierAmountTotal, (string) ($virtualRow[7] ?? '0')); + $virtualSalesAmountTotal = $this->addMoney($virtualSalesAmountTotal, (string) ($virtualRow[9] ?? '0')); + } $erpRecipients = $this->loadErpRecipients($companyId); $erpRows = $this->erpRows($virtualRows, $generatedAt, $erpRecipients); $inventoryRows = $this->inventoryRows($virtualRows, $generatedAt); @@ -394,19 +378,19 @@ class KukaReconciliationPageData 'generated_rows' => count($lineItems), 'material_count' => count($materials), 'posting_material_count' => count($posting), - 'posting_quantity_total' => $this->formatQuantity($postingQuantityTotal), - 'posting_supplier_amount_total' => $postingSupplierAmountTotal, - 'posting_sales_amount_total' => $postingSalesAmountTotal, - 'posting_row_sales_amount_total' => $postingRowSalesAmountTotal, - 'virtual_sales_rounding_adjustment' => $virtualSalesRoundingAdjustment, - 'erp_quantity_total' => $this->formatQuantity($postingQuantityTotal), - 'erp_detail_amount_total' => $postingRowSalesAmountTotal, - 'erp_amount_rounding_adjustment' => $virtualSalesRoundingAdjustment, - 'erp_amount_total' => $postingSalesAmountTotal, - 'erp_paid_amount_total' => $postingSalesAmountTotal, + 'posting_quantity_total' => $this->formatQuantity($virtualQuantityTotal), + 'posting_supplier_amount_total' => $virtualSupplierAmountTotal, + 'posting_sales_amount_total' => $virtualSalesAmountTotal, + 'posting_row_sales_amount_total' => $virtualSalesAmountTotal, + 'virtual_sales_rounding_adjustment' => '0.00', + 'erp_quantity_total' => $this->formatQuantity($virtualQuantityTotal), + 'erp_detail_amount_total' => $virtualSalesAmountTotal, + 'erp_amount_rounding_adjustment' => '0.00', + 'erp_amount_total' => $virtualSalesAmountTotal, + 'erp_paid_amount_total' => $virtualSalesAmountTotal, 'invoice_quantity_total' => $this->formatQuantity($invoiceQuantityTotal), 'invoice_amount_total' => $invoiceAmountTotal, - 'toc_quantity_total' => $this->formatQuantity($postingQuantityTotal), + 'toc_quantity_total' => $this->formatQuantity($virtualQuantityTotal), 'order_count' => count($orders), 'skipped_missing_material_rows' => $skippedMissingMaterial, 'skipped_zero_quantity_rows' => $skippedZeroQuantity, @@ -432,25 +416,58 @@ class KukaReconciliationPageData */ private function virtualRows(array $lineItems, string $generatedAt): array { - $rows = []; - $serial = 0; + $groups = []; $datePrefix = str_replace('-', '', substr($generatedAt, 0, 10)); foreach ($lineItems as $row) { if (($row['posting_tag'] ?? '') !== '是') { continue; } + + $materialNumber = trim((string) ($row['material_number'] ?? '')); + if ($materialNumber === '') { + continue; + } + if (! isset($groups[$materialNumber])) { + $groups[$materialNumber] = [ + 'year' => (string) ($row['year'] ?? ''), + 'month' => (string) ($row['month'] ?? ''), + 'description' => trim((string) ($row['description'] ?? '')), + 'quantity' => '0', + 'supplier_amount' => '0.00', + ]; + } elseif (($groups[$materialNumber]['description'] ?? '') === '') { + $groups[$materialNumber]['description'] = trim((string) ($row['description'] ?? '')); + } + $groups[$materialNumber]['quantity'] = $this->addDecimal( + $groups[$materialNumber]['quantity'], + (string) $row['quantity'], + ); + $groups[$materialNumber]['supplier_amount'] = $this->addMoney( + $groups[$materialNumber]['supplier_amount'], + (string) $row['supplier_amount'], + ); + } + + ksort($groups, SORT_NATURAL); + $rows = []; + $serial = 0; + foreach ($groups as $materialNumber => $group) { $serial++; + $quantity = (string) $group['quantity']; + $supplierAmount = (string) $group['supplier_amount']; + // 按料号汇总供应商金额后再计算销售金额,避免逐行四舍五入累积尾差。 + $salesAmount = $this->divideMoney($supplierAmount, self::SALES_MARKUP_DIVISOR); $rows[] = [ - $row['year'], - $row['month'], + $group['year'], + $group['month'], (string) $serial, - $row['material_number'], - $row['description'], - $this->formatQuantity($row['quantity']), - $row['unit_price'], - $row['supplier_amount'], - $row['sales_unit_price'], - $row['sales_amount'], + (string) $materialNumber, + $group['description'], + $this->formatQuantity($quantity), + $this->divideMoney($supplierAmount, $quantity), + $supplierAmount, + $this->divideMoney($salesAmount, $quantity), + $salesAmount, self::VIRTUAL_SUPPLIER_NAME, self::VIRTUAL_SUPPLIER_CODE, $datePrefix.str_pad((string) $serial, 6, '0', STR_PAD_LEFT), 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 c718922e..fa508b40 100644 --- a/resources/views/business/pages/posting-data/kuka-reconciliation.blade.php +++ b/resources/views/business/pages/posting-data/kuka-reconciliation.blade.php @@ -45,7 +45,7 @@ 'slug' => 'lingshan-virtual-posting', 'title' => '灵珊虚拟过账', 'icon' => 'swap_horiz', - 'description' => '已按工作簿4“灵珊虚拟过账”子表放入表头,数据待接入。', + 'description' => '按顾家料号透视,汇总采购数量与金额后生成联动模板。', 'source_sheet' => '灵珊虚拟过账', 'numeric_columns' => [0, 1, 2, 5, 6, 7, 8, 9], 'money_columns' => [6, 7, 8, 9], diff --git a/tests/Feature/Business/KukaReconciliationPageTest.php b/tests/Feature/Business/KukaReconciliationPageTest.php index 2243b764..db019b30 100644 --- a/tests/Feature/Business/KukaReconciliationPageTest.php +++ b/tests/Feature/Business/KukaReconciliationPageTest.php @@ -129,7 +129,7 @@ class KukaReconciliationPageTest extends FeatureTestCase ->assertSeeText('发货数量'); } - public function test_virtual_posting_renders_dynamic_sales_rounding_adjustment(): void + public function test_virtual_posting_renders_material_pivot_without_rounding_footer(): void { $connection = DB::connection('business_mirror'); $now = now(); @@ -182,7 +182,7 @@ class KukaReconciliationPageTest extends FeatureTestCase ]); } - $this->loginAs() + $response = $this->loginAs() ->get(route('business.pages.show', [ 'section' => 'posting-data', 'page' => 'kuka-reconciliation', @@ -190,10 +190,17 @@ class KukaReconciliationPageTest extends FeatureTestCase 'reconciliation_view' => 'lingshan-virtual-posting', ])) ->assertOk() - ->assertSeeText('销售金额尾差调整') - ->assertSeeText('-0.01') - ->assertSeeText('表格逐行金额合计 0.12') - ->assertSeeText('尾差调整 -0.01'); + ->assertDontSeeText('销售金额尾差调整'); + + $html = $response->getContent(); + $this->assertMatchesRegularExpression( + '/data-kuka-virtual-summary-card="quantity".*?2<\/strong>/s', + $html, + ); + $this->assertMatchesRegularExpression( + '/data-kuka-virtual-summary-card="sales-amount".*?0\.11<\/strong>/s', + $html, + ); } public function test_reconciliation_panels_render_rows_generated_from_shipment_rules(): void diff --git a/tests/Unit/Business/KukaReconciliationPageDataTest.php b/tests/Unit/Business/KukaReconciliationPageDataTest.php index acde871d..f84e4fe9 100644 --- a/tests/Unit/Business/KukaReconciliationPageDataTest.php +++ b/tests/Unit/Business/KukaReconciliationPageDataTest.php @@ -331,7 +331,7 @@ class KukaReconciliationPageDataTest extends TestCase ], $data['views']['inventory-management-template']['rows'][0]); } - public function test_virtual_sales_summary_uses_total_supplier_amount(): void + public function test_virtual_sales_summary_uses_pivoted_supplier_amount(): void { $connection = DB::connection('business_mirror'); $connection->table('kuka_posting_base_imports')->insert([ @@ -348,12 +348,13 @@ class KukaReconciliationPageDataTest extends TestCase ); $this->assertNull($data['error']); + $this->assertCount(1, $data['views']['lingshan-virtual-posting']['rows']); $this->assertSame('0.10', $data['summary']['posting_supplier_amount_total']); $this->assertSame('0.11', $data['summary']['posting_sales_amount_total']); - $this->assertSame('0.12', $data['summary']['posting_row_sales_amount_total']); - $this->assertSame('-0.01', $data['summary']['virtual_sales_rounding_adjustment']); - $this->assertSame('0.12', $data['summary']['erp_detail_amount_total']); - $this->assertSame('-0.01', $data['summary']['erp_amount_rounding_adjustment']); + $this->assertSame('0.11', $data['summary']['posting_row_sales_amount_total']); + $this->assertSame('0.00', $data['summary']['virtual_sales_rounding_adjustment']); + $this->assertSame('0.11', $data['summary']['erp_detail_amount_total']); + $this->assertSame('0.00', $data['summary']['erp_amount_rounding_adjustment']); $this->assertSame('0.11', $data['summary']['erp_amount_total']); $this->assertSame('0.11', $data['summary']['erp_paid_amount_total']); } @@ -398,7 +399,7 @@ class KukaReconciliationPageDataTest extends TestCase 'P-'.$index, 'S-'.$index, 'I-'.$index, - 'M-1', + 'M-'.$index, '1', '10.00', ), @@ -424,7 +425,7 @@ class KukaReconciliationPageDataTest extends TestCase foreach ($erpRows as $index => $erp) { $this->assertSame('616', $erp[2]); $this->assertSame($virtualRows[$index][12], $erp[3]); - $this->assertSame('M-1', $erp[4]); + $this->assertSame('M-'.($index + 1), $erp[4]); $this->assertSame('1', $erp[5]); $this->assertSame('0', $erp[7]); $this->assertSame('11.11', $erp[8]); @@ -434,7 +435,7 @@ class KukaReconciliationPageDataTest extends TestCase } } - public function test_inventory_template_maps_every_virtual_row_without_aggregation(): void + public function test_linked_templates_use_the_same_material_pivot_rows(): void { $connection = DB::connection('business_mirror'); $connection->table('kuka_posting_base_imports')->insert([ @@ -452,13 +453,25 @@ class KukaReconciliationPageDataTest extends TestCase $virtualRows = $data['views']['lingshan-virtual-posting']['rows']; $inventoryRows = $data['views']['inventory-management-template']['rows']; - $this->assertCount(2, $virtualRows); - $this->assertCount(2, $inventoryRows); - $this->assertSame(['M-1', 'M-1'], array_column($inventoryRows, 5)); - $this->assertSame(['1', '2'], array_column($inventoryRows, 7)); - $this->assertSame(['1', '2'], array_column($inventoryRows, 8)); - $this->assertSame(['1', '2'], array_column($inventoryRows, 26)); - $this->assertSame([$data['generated_at'], $data['generated_at']], array_column($inventoryRows, 25)); + $erpRows = $data['views']['erp-product-import']['rows']; + $invoiceRows = $data['views']['invoice-entry-template']['rows']; + $this->assertCount(1, $virtualRows); + $this->assertCount(1, $inventoryRows); + $this->assertCount(1, $erpRows); + $this->assertCount(1, $invoiceRows); + $this->assertSame('3', $virtualRows[0][5]); + $this->assertSame('10.00', $virtualRows[0][6]); + $this->assertSame('30.00', $virtualRows[0][7]); + $this->assertSame('11.11', $virtualRows[0][8]); + $this->assertSame('33.33', $virtualRows[0][9]); + $this->assertSame('3', $inventoryRows[0][7]); + $this->assertSame('3', $inventoryRows[0][8]); + $this->assertSame('3', $inventoryRows[0][26]); + $this->assertSame($data['generated_at'], $inventoryRows[0][25]); + $this->assertSame('3', $erpRows[0][5]); + $this->assertSame('33.33', $erpRows[0][9]); + $this->assertSame('3', $invoiceRows[0][9]); + $this->assertSame('33.33', $invoiceRows[0][10]); foreach ($inventoryRows as $row) { $this->assertSame('常州欧伯娜家居有限公司', $row[4]); $this->assertSame('测试物料', $row[6]);