From db78e87ea5f64d3cdcc265e41dd7a1ae1e6ab1b1 Mon Sep 17 00:00:00 2001 From: shanshi <3316401569@qq.com> Date: Wed, 25 Nov 2026 17:11:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=B4=A2=E5=8A=A1=E5=8F=B0=E8=B4=A6):=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=AD=E5=9B=BD=E9=93=B6=E8=A1=8C=E9=9D=9E?= =?UTF-8?q?=E7=9B=B4=E8=BF=9E=E8=B4=A6=E5=8D=95=E6=A8=A1=E6=9D=BF=20(#1429?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/BankBillStatementImporter.php | 304 ++++++++++++++++-- .../Services/BankBillTemplateExporter.php | 85 ++++- tests/Feature/Business/BankBillPageTest.php | 154 ++++++++- 3 files changed, 506 insertions(+), 37 deletions(-) diff --git a/modules/EcommerceFinance/Services/BankBillStatementImporter.php b/modules/EcommerceFinance/Services/BankBillStatementImporter.php index 7b66541f..54f01195 100644 --- a/modules/EcommerceFinance/Services/BankBillStatementImporter.php +++ b/modules/EcommerceFinance/Services/BankBillStatementImporter.php @@ -73,6 +73,8 @@ final class BankBillStatementImporter if ($scope === BankBillTemplateExporter::SCOPE_NON_DIRECT) { if ($bankName === '华夏银行') { [$headerRow, $headers, $metadata] = $this->huaxiaMonthlyHeaderAndMetadata($reader); + } elseif ($bankName === '中国银行') { + [$headerRow, $headers, $metadata] = $this->bocMonthlyHeaderAndMetadata($reader); } else { [$headerRow, $headers] = $this->headerRow($reader); } @@ -101,6 +103,8 @@ final class BankBillStatementImporter $accountsByNumber = $this->accountsByNumber( $scope === BankBillTemplateExporter::SCOPE_DIRECT ? $bankName : '', ); + $isBocMonthly = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT + && $bankName === '中国银行'; $templateAccount = $scope === BankBillTemplateExporter::SCOPE_NON_DIRECT && $bankName === '华夏银行' ? $this->huaxiaMonthlyAccount((string) ($metadata['company_name'] ?? '')) @@ -171,9 +175,10 @@ final class BankBillStatementImporter continue; } - $accountNo = $bankName === '网商银行' - ? (string) ($metadata['account_no'] ?? '') - : $this->accountNumber($valuesByLabel); + $accountNo = match (true) { + $bankName === '网商银行', $isBocMonthly => (string) ($metadata['account_no'] ?? ''), + default => $this->accountNumber($valuesByLabel), + }; $account = $accountsByNumber[$this->digits($accountNo)] ?? null; if ($account === null && $templateAccount !== null) { $account = $templateAccount; @@ -186,12 +191,14 @@ final class BankBillStatementImporter } $rowBankName = $bankName; - $companyName = $bankName === '网商银行' - ? (string) ($metadata['company_name'] ?? '') - : (string) ($metadata['company_name'] ?? $this->firstValue( + $companyName = match (true) { + $bankName === '网商银行' => (string) ($metadata['company_name'] ?? ''), + $isBocMonthly => $this->bocAccountHolderName($valuesByLabel), + default => (string) ($metadata['company_name'] ?? $this->firstValue( $valuesByLabel, ['单位名称', '公司名称', '账号名称', '账户名称', '己方银行户名'], - )); + )), + }; $accountShort = (string) ($account->account_short ?? ''); if ($accountShort === '') { $accountShort = $this->firstValue( @@ -206,6 +213,13 @@ final class BankBillStatementImporter if ($bankName === '网商银行') { $income = $this->amount($this->firstValue($valuesByLabel, ['借方金额(收)', '借方金额(收)'])); $expense = $this->amount($this->firstValue($valuesByLabel, ['贷方金额(支)', '贷方金额(支)'])); + } elseif ($isBocMonthly) { + $signedAmount = $this->signedAmount($this->firstValue( + $valuesByLabel, + ['交易金额[ Trade Amount ]'], + )); + $income = $signedAmount > 0 ? round($signedAmount, 2) : 0.0; + $expense = $signedAmount < 0 ? round(abs($signedAmount), 2) : 0.0; } else { $income = $this->amount($this->firstValue( $valuesByLabel, @@ -218,27 +232,51 @@ final class BankBillStatementImporter } $balanceValue = $this->firstValue( $valuesByLabel, - ['账户余额', '己方账户余额(元)', '余额'], + ['账户余额', '己方账户余额(元)', '余额', '交易后余额[ After-transaction balance ]'], ); $balance = $balanceValue === '' ? null : $this->amount($balanceValue); $recordCode = $this->firstValue( $valuesByLabel, - ['账务流水号', '银行流水号', '核心流水号', '流水号', '凭证号', '凭证号码'], - ); - $counterpartyName = $this->firstValue( - $valuesByLabel, - ['对方户名', '对手方别名', '收(付)方名称'], - ); - $counterpartyAccount = $this->firstValue( - $valuesByLabel, - ['对方账号', '对手方银行账号', '收(付)方账号'], - ); - $counterpartyBank = $this->firstValue( - $valuesByLabel, - ['对方机构', '对方开户行', '对方行名', '对手方开户行', '收(付)方开户行名'], + [ + '账务流水号', + '银行流水号', + '核心流水号', + '流水号', + '凭证号', + '凭证号码', + '交易流水号[ Transaction reference number ]', + ], ); + if ($isBocMonthly) { + $counterparty = $this->bocCounterparty($valuesByLabel); + $counterpartyName = $counterparty['name']; + $counterpartyAccount = $counterparty['account']; + $counterpartyBank = $counterparty['bank']; + } else { + $counterpartyName = $this->firstValue( + $valuesByLabel, + ['对方户名', '对手方别名', '收(付)方名称'], + ); + $counterpartyAccount = $this->firstValue( + $valuesByLabel, + ['对方账号', '对手方银行账号', '收(付)方账号'], + ); + $counterpartyBank = $this->firstValue( + $valuesByLabel, + ['对方机构', '对方开户行', '对方行名', '对手方开户行', '收(付)方开户行名'], + ); + } $detailParts = []; - foreach (['交易类型', '摘要', '业务摘要', '其它摘要', '交易描述', '渠道'] as $label) { + foreach ([ + '交易类型', + '业务类型[ Business type ]', + '摘要', + '摘要[ Reference ]', + '业务摘要', + '其它摘要', + '交易描述', + '渠道', + ] as $label) { $value = trim((string) ($valuesByLabel[$label] ?? '')); if ($value !== '' && ! in_array($value, $detailParts, true)) { $detailParts[] = $value; @@ -246,13 +284,30 @@ final class BankBillStatementImporter } $paymentRemark = $this->firstValue( $valuesByLabel, - ['备注', '用途', '附言', '附言(用途)', '明细标注', '业务参考号'], + [ + '备注', + '用途', + '附言', + '附言(用途)', + '明细标注', + '业务参考号', + '交易附言[ Remark ]', + '用途[ Purpose ]', + '备注[ Remarks ]', + ], ); $currency = $bankName === '网商银行' ? (string) ($metadata['currency'] ?? '人民币') : ( (string) ($metadata['currency'] ?? '') - ?: ($this->firstValue($valuesByLabel, ['币种']) ?: '人民币') + ?: ( + $isBocMonthly + ? $this->bocCurrency($this->firstValue( + $valuesByLabel, + ['交易货币[ Trade Currency ]'], + )) + : ($this->firstValue($valuesByLabel, ['币种']) ?: '人民币') + ) ); $rowKey = hash('sha256', json_encode([ $rowBankName, @@ -346,6 +401,28 @@ final class BankBillStatementImporter round($importedIncome, 2), round($importedExpense, 2), $metadata, + [ + 'income_rows' => '收入笔数', + 'expense_rows' => '支出笔数', + 'income' => '收入金额', + 'expense' => '支出金额', + ], + ); + } elseif ($isBocMonthly) { + $this->validateBillControl( + '中国银行', + $stats, + $incomeRows, + $expenseRows, + round($importedIncome, 2), + round($importedExpense, 2), + $metadata, + [ + 'income_rows' => '贷方笔数', + 'expense_rows' => '借方笔数', + 'income' => '贷方金额', + 'expense' => '借方金额', + ], ); } if (! $dryRun) { @@ -570,6 +647,135 @@ final class BankBillStatementImporter ]; } + /** + * @return array{0:int,1:array,2:array} + */ + private function bocMonthlyHeaderAndMetadata(PayablesWorkbookStreamReader $reader): array + { + $headerRow = null; + $headers = []; + $rawMetadata = []; + $metadataLabels = [ + '查询账号[ Inquirer account number ]' => 'account_no', + '总笔数[ Total number ]' => 'expected_rows', + '借方发生总笔数[ Total Numbers of Debited Payments ]' => 'expected_expense_rows', + '借方发生总额[ Total Debit Amount of Payments ]' => 'expected_expense', + '贷方发生总笔数[ Total Numbers of Credited Payments ]' => 'expected_income_rows', + '贷方发生总额[ Total Credit Amount of Payments ]' => 'expected_income', + ]; + $requiredHeaders = [ + '交易类型[ Transaction Type ]', + '交易日期[ Transaction Date ]', + '交易时间[ Transaction time ]', + '交易金额[ Trade Amount ]', + '交易后余额[ After-transaction balance ]', + '交易流水号[ Transaction reference number ]', + ]; + + foreach ($reader->rows(0, null, 20) as $rowNumber => $cells) { + $values = array_map(fn (mixed $value): string => $this->normaliseCell($value), $cells); + $keys = array_keys($values); + foreach ($values as $index => $value) { + $metaKey = $metadataLabels[$value] ?? null; + if ($metaKey === null || array_key_exists($metaKey, $rawMetadata)) { + continue; + } + $position = array_search($index, $keys, true); + for ($cursor = (int) $position + 1; $cursor < count($keys); $cursor++) { + $candidate = trim((string) ($values[$keys[$cursor]] ?? '')); + if ($candidate !== '') { + $rawMetadata[$metaKey] = $candidate; + break; + } + } + } + + $candidateHeaders = []; + foreach ($values as $index => $value) { + if ($value !== '') { + $candidateHeaders[(int) $index] = $value; + } + } + if (count(array_intersect(array_values($candidateHeaders), $requiredHeaders)) + === count($requiredHeaders)) { + $headerRow = $rowNumber; + $headers = $candidateHeaders; + break; + } + } + + if ($headerRow === null) { + throw new InvalidArgumentException('没有找到中国银行账单明细表头。'); + } + $accountNo = $this->digits((string) ($rawMetadata['account_no'] ?? '')); + if ($accountNo === '') { + throw new InvalidArgumentException('中国银行账单缺少“查询账号”。'); + } + + return [ + $headerRow, + $headers, + [ + 'account_no' => $accountNo, + 'expected_rows' => $this->countFromText($rawMetadata['expected_rows'] ?? null), + 'expected_income_rows' => $this->countFromText($rawMetadata['expected_income_rows'] ?? null), + 'expected_expense_rows' => $this->countFromText($rawMetadata['expected_expense_rows'] ?? null), + 'expected_income' => $this->moneyFromText($rawMetadata['expected_income'] ?? null), + 'expected_expense' => $this->moneyFromText($rawMetadata['expected_expense'] ?? null), + ], + ]; + } + + private function bocAccountHolderName(array $values): string + { + return str_contains($this->bocDirection($values), '往账') + ? $this->firstValue($values, ["付款人名称[ Payer's Name ]"]) + : $this->firstValue($values, ["收款人名称[ Payee's Name ]"]); + } + + /** + * @return array{name:string,account:string,bank:string} + */ + private function bocCounterparty(array $values): array + { + $outgoing = str_contains($this->bocDirection($values), '往账'); + + return $outgoing + ? [ + 'name' => $this->firstValue($values, ["收款人名称[ Payee's Name ]"]), + 'account' => $this->firstValue($values, ["收款人账号[ Payee's Account Number ]"]), + 'bank' => $this->firstValue($values, [ + '收款人开户行名[ Beneficiary account bank ]', + '名义收款人开户行名[ Opening bank name of nominal payee ]', + ]), + ] + : [ + 'name' => $this->firstValue($values, ["付款人名称[ Payer's Name ]"]), + 'account' => $this->firstValue($values, ['付款人账号[ Debit Account No. ]']), + 'bank' => $this->firstValue($values, [ + '付款人开户行名[ Payer account bank ]', + '名义付款人开户行名[ Opening bank name of nominal payer ]', + ]), + ]; + } + + private function bocDirection(array $values): string + { + return $this->firstValue($values, ['交易类型[ Transaction Type ]']); + } + + private function bocCurrency(string $value): string + { + return match (strtoupper(trim($value))) { + '', 'CNY' => '人民币', + 'USD' => '美元', + 'EUR' => '欧元', + 'HKD' => '港币', + 'JPY' => '日元', + default => trim($value), + }; + } + private function huaxiaMonthlyAccount(string $companyName): object { $companyName = trim($companyName); @@ -617,12 +823,21 @@ final class BankBillStatementImporter float $income, float $expense, array $metadata, + array $labels = [], ): void { + $labels = array_merge([ + 'total' => '总笔数', + 'income_rows' => '借方笔数', + 'expense_rows' => '贷方笔数', + 'income' => '借方金额', + 'expense' => '贷方金额', + ], $labels); $checks = [ - ['label' => '借方笔数', 'actual' => $incomeRows, 'expected' => $metadata['expected_income_rows'] ?? null], - ['label' => '贷方笔数', 'actual' => $expenseRows, 'expected' => $metadata['expected_expense_rows'] ?? null], - ['label' => '借方金额', 'actual' => $income, 'expected' => $metadata['expected_income'] ?? null], - ['label' => '贷方金额', 'actual' => $expense, 'expected' => $metadata['expected_expense'] ?? null], + ['label' => $labels['total'], 'actual' => $stats['rows'], 'expected' => $metadata['expected_rows'] ?? null], + ['label' => $labels['income_rows'], 'actual' => $incomeRows, 'expected' => $metadata['expected_income_rows'] ?? null], + ['label' => $labels['expense_rows'], 'actual' => $expenseRows, 'expected' => $metadata['expected_expense_rows'] ?? null], + ['label' => $labels['income'], 'actual' => $income, 'expected' => $metadata['expected_income'] ?? null], + ['label' => $labels['expense'], 'actual' => $expense, 'expected' => $metadata['expected_expense'] ?? null], ]; foreach ($checks as $check) { $expected = $check['expected']; @@ -780,8 +995,18 @@ final class BankBillStatementImporter */ private function transactionDateTime(array $values): ?array { - $dateValue = $this->firstValue($values, ['交易日', '交易日期']); - $timeValue = $this->firstValue($values, ['交易时间']); + $dateValue = $this->firstValue($values, [ + '交易日', + '交易日期', + '交易日期[ Transaction Date ]', + ]); + $timeValue = $this->firstValue($values, [ + '交易时间', + '交易时间[ Transaction time ]', + ]); + if (preg_match('/^(\d{4})(\d{2})(\d{2})$/', $dateValue, $matches) === 1) { + $dateValue = "{$matches[1]}-{$matches[2]}-{$matches[3]}"; + } $candidate = trim($dateValue . ' ' . $timeValue); if ($candidate === '') { return null; @@ -809,7 +1034,13 @@ final class BankBillStatementImporter private function accountNumber(array $values): string { - return $this->firstValue($values, ['账号', '银行账号', '己方银行账号']); + return $this->firstValue($values, [ + '账号', + '银行账号', + '己方银行账号', + '付款人账号[ Debit Account No. ]', + "收款人账号[ Payee's Account Number ]", + ]); } private function firstValue(array $values, array $labels): string @@ -836,6 +1067,17 @@ final class BankBillStatementImporter return round(abs((float) $normalised) * ($negative ? -1 : 1), 2); } + private function signedAmount(string $value): float + { + $value = trim($value); + if ($value === '' || $value === '-' || $value === '—') { + return 0.0; + } + $normalised = preg_replace('/[^\d.\-]/u', '', $value) ?? ''; + + return round((float) $normalised, 2); + } + private function digits(string $value): string { return preg_replace('/\D+/', '', $value) ?? ''; diff --git a/modules/EcommerceFinance/Services/BankBillTemplateExporter.php b/modules/EcommerceFinance/Services/BankBillTemplateExporter.php index d6f7de78..be4b3c54 100644 --- a/modules/EcommerceFinance/Services/BankBillTemplateExporter.php +++ b/modules/EcommerceFinance/Services/BankBillTemplateExporter.php @@ -128,6 +128,46 @@ final class BankBillTemplateExporter '币种', '渠道', ], + '中国银行' => [ + '交易类型[ Transaction Type ]', + '业务类型[ Business type ]', + '付款人开户行号[ Account holding bank number of payer ]', + '付款人开户行名[ Payer account bank ]', + '付款人账号[ Debit Account No. ]', + "付款人名称[ Payer's Name ]", + '收款人开户行行号[ Account holding bank number of beneficiary ]', + '收款人开户行名[ Beneficiary account bank ]', + "收款人账号[ Payee's Account Number ]", + "收款人名称[ Payee's Name ]", + '交易日期[ Transaction Date ]', + '交易时间[ Transaction time ]', + '交易货币[ Trade Currency ]', + '交易金额[ Trade Amount ]', + '交易后余额[ After-transaction balance ]', + '起息日期[ Value Date ]', + '汇率[ Exchange rate ]', + '交易流水号[ Transaction reference number ]', + '客户申请号[ Online Banking Transaction Ref.(Bank Ref.) ]', + '客户业务编号[ Customer Transaction Ref.(Customer Ref.) ]', + '凭证类型[ Voucher type ]', + '凭证号码[ Voucher number ]', + '记录标识号[ Record ID ]', + '摘要[ Reference ]', + '用途[ Purpose ]', + '交易附言[ Remark ]', + '备注[ Remarks ]', + '预留项1[ Reserve1 ]', + '预留项2[ Reserve2 ]', + '预留项3[ Reserve3 ]', + '名义付款人开户行行号[ Opening bank number of nominal payer ]', + '名义付款人开户行名[ Opening bank name of nominal payer ]', + '名义付款人账号[ Payment A/C No. ]', + '名义付款人名称[ Name of nominal payer ]', + '名义收款人开户行号[ Opening bank number of nominal payee ]', + '名义收款人开户行名[ Opening bank name of nominal payee ]', + '名义收款人账号[ Account number of nominal payee ]', + '名义收款人名称[ Name of nominal payee ]', + ], ]; /** @@ -231,15 +271,29 @@ final class BankBillTemplateExporter } $headers = self::headers($bank, $scope); $isHuaXiaMonthly = $scope === self::SCOPE_NON_DIRECT && $bank === '华夏银行'; - $displayName = $isHuaXiaMonthly ? '华夏银行月度' : $bank; + $isBocMonthly = $scope === self::SCOPE_NON_DIRECT && $bank === '中国银行'; + $displayName = match (true) { + $isHuaXiaMonthly => '华夏银行月度', + $isBocMonthly => '中国银行月度', + default => $bank, + }; $spreadsheet = new Spreadsheet(); $spreadsheet->getProperties() ->setCreator('CaiWuXiTong') ->setTitle($displayName . '账单导入模板'); $sheet = $spreadsheet->getActiveSheet(); - $sheet->setTitle($isHuaXiaMonthly ? '华夏月度明细' : $bank); - $headerRow = $isHuaXiaMonthly ? 12 : ($bank === '网商银行' ? 5 : 1); + $sheet->setTitle(match (true) { + $isHuaXiaMonthly => '华夏月度明细', + $isBocMonthly => '中行月度明细', + default => $bank, + }); + $headerRow = match (true) { + $isHuaXiaMonthly => 12, + $isBocMonthly => 9, + $bank === '网商银行' => 5, + default => 1, + }; if ($isHuaXiaMonthly) { $sheet->fromArray([ @@ -265,6 +319,25 @@ final class BankBillTemplateExporter foreach (['A6', 'E6', 'A7', 'A8', 'A9', 'A10', 'E10', 'A11', 'E11'] as $cell) { $sheet->getStyle($cell)->getFont()->setBold(true); } + } elseif ($isBocMonthly) { + $sheet->fromArray([ + ['中国银行交易明细'], + ['查询账号[ Inquirer account number ]', '请填写账号'], + ['总笔数[ Total number ]', '0'], + ['借方发生总笔数[ Total Numbers of Debited Payments ]', '0'], + ['借方发生总额[ Total Debit Amount of Payments ]', '0.00'], + ['贷方发生总笔数[ Total Numbers of Credited Payments ]', '0'], + ['贷方发生总额[ Total Credit Amount of Payments ]', '0.00'], + ['查询时间范围[ Time Range ]', '请填写起止日期'], + ], null, 'A1', true); + $sheet->mergeCells('A1:AL1'); + $sheet->getStyle('A1:AL1')->getFont()->setBold(true)->setSize(14); + $sheet->getStyle('A1:AL1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); + $sheet->getStyle('A2:B8')->getFill() + ->setFillType(Fill::FILL_SOLID) + ->getStartColor() + ->setARGB('FFF7F0DE'); + $sheet->getStyle('A2:A8')->getFont()->setBold(true); } elseif ($bank === '网商银行') { $sheet->fromArray([ ['浙江网商银行企业账户交易明细'], @@ -359,10 +432,12 @@ final class BankBillTemplateExporter if (str_contains($label, '金额') || str_contains($label, '余额')) { return '#,##0.00'; } - if ($label === '交易时间' || $label === '提交时间') { + if (str_starts_with($label, '交易时间') || $label === '提交时间') { return 'yyyy-mm-dd hh:mm:ss'; } - if ($label === '交易日' || $label === '起息日') { + if ($label === '交易日' + || str_starts_with($label, '交易日期') + || str_starts_with($label, '起息日期')) { return 'yyyy-mm-dd'; } diff --git a/tests/Feature/Business/BankBillPageTest.php b/tests/Feature/Business/BankBillPageTest.php index adcd020c..a5fa0f89 100644 --- a/tests/Feature/Business/BankBillPageTest.php +++ b/tests/Feature/Business/BankBillPageTest.php @@ -513,11 +513,13 @@ class BankBillPageTest extends FeatureTestCase BankBillTemplateExporter::SCOPE_NON_DIRECT, )); - $this->assertCount(2, $templates); + $this->assertCount(3, $templates); $huaXia = $templates->firstWhere('bank', '华夏银行'); $hangzhou = $templates->firstWhere('bank', '杭州银行'); + $boc = $templates->firstWhere('bank', '中国银行'); $this->assertIsArray($huaXia); $this->assertIsArray($hangzhou); + $this->assertIsArray($boc); $this->assertSame([ '序号', '交易日期', @@ -553,6 +555,12 @@ class BankBillPageTest extends FeatureTestCase '币种', '渠道', ], $hangzhou['headers']); + $this->assertCount(38, $boc['headers']); + $this->assertSame('交易类型[ Transaction Type ]', $boc['headers'][0]); + $this->assertSame('交易金额[ Trade Amount ]', $boc['headers'][13]); + $this->assertSame('交易后余额[ After-transaction balance ]', $boc['headers'][14]); + $this->assertSame('交易流水号[ Transaction reference number ]', $boc['headers'][17]); + $this->assertSame('名义收款人名称[ Name of nominal payee ]', $boc['headers'][37]); $huaXiaTemplate = app(BankBillTemplateExporter::class)->create( '华夏银行', @@ -572,6 +580,18 @@ class BankBillPageTest extends FeatureTestCase ); $this->assertSame('杭州银行账单导入模板.xlsx', $hangzhouTemplate['filename']); @unlink($hangzhouTemplate['path']); + + $bocTemplate = app(BankBillTemplateExporter::class)->create( + '中国银行', + BankBillTemplateExporter::SCOPE_NON_DIRECT, + ); + $this->assertSame('中国银行月度账单导入模板.xlsx', $bocTemplate['filename']); + $spreadsheet = IOFactory::load($bocTemplate['path']); + $workbookHeaders = $spreadsheet->getActiveSheet() + ->rangeToArray('A9:AL9', null, true, false)[0]; + $spreadsheet->disconnectWorksheets(); + $this->assertSame($boc['headers'], $workbookHeaders); + @unlink($bocTemplate['path']); } public function test_hua_xia_template_route_downloads_the_workbook(): void @@ -905,6 +925,138 @@ class BankBillPageTest extends FeatureTestCase } } + public function test_boc_non_direct_import_maps_signed_amounts_and_directional_counterparties(): void + { + $this->user->roles()->sync([ + Role::where('name', 'admin')->firstOrFail()->id, + ]); + $this->user->unsetRelation('roles'); + $connection = DB::connection('business_mirror'); + $accountId = $connection->table('fin_accounts')->insertGetId([ + 'company_name' => '中国银行测试公司', + 'account_short' => '新佰广中行测试', + 'account_type' => '公户', + 'bank_name' => '中国银行常州经济开发区支行', + 'account_no' => '5547485000000001', + 'account_should_keep' => '是', + 'current_balance' => 1000, + ]); + $headers = collect(BankBillTemplateExporter::templates( + BankBillTemplateExporter::SCOPE_NON_DIRECT, + ))->firstWhere('bank', '中国银行')['headers']; + $indexes = array_flip($headers); + $row = static function (array $values) use ($indexes, $headers): array { + $result = array_fill(0, count($headers), null); + foreach ($values as $header => $value) { + $result[$indexes[$header]] = $value; + } + + return $result; + }; + $path = $this->workbook([ + ['中国银行交易明细'], + ['查询账号[ Inquirer account number ]', '5547485000000001'], + ['总笔数[ Total number ]', '2'], + ['借方发生总笔数[ Total Numbers of Debited Payments ]', '1'], + ['借方发生总额[ Total Debit Amount of Payments ]', '30.00'], + ['贷方发生总笔数[ Total Numbers of Credited Payments ]', '1'], + ['贷方发生总额[ Total Credit Amount of Payments ]', '120.00'], + ['查询时间范围[ Time Range ]', '20261101-20261125'], + $headers, + $row([ + '交易类型[ Transaction Type ]' => '往账', + '业务类型[ Business type ]' => '网上支付', + '付款人开户行名[ Payer account bank ]' => '中国银行常州经济开发区支行', + '付款人账号[ Debit Account No. ]' => '5547485000000001', + "付款人名称[ Payer's Name ]" => '中国银行测试公司', + '收款人开户行名[ Beneficiary account bank ]' => '招商银行常州分行', + "收款人账号[ Payee's Account Number ]" => '6222000000000002', + "收款人名称[ Payee's Name ]" => '外部收款人', + '交易日期[ Transaction Date ]' => '20261120', + '交易时间[ Transaction time ]' => '09:00:00', + '交易货币[ Trade Currency ]' => 'CNY', + '交易金额[ Trade Amount ]' => -30, + '交易后余额[ After-transaction balance ]' => '970.00', + '起息日期[ Value Date ]' => '20261120', + '交易流水号[ Transaction reference number ]' => 'BOC-OUT-001', + '摘要[ Reference ]' => '测试支出', + ]), + $row([ + '交易类型[ Transaction Type ]' => '来账', + '业务类型[ Business type ]' => '网上支付', + '付款人开户行名[ Payer account bank ]' => '华夏银行股份有限公司', + '付款人账号[ Debit Account No. ]' => '6222000000000003', + "付款人名称[ Payer's Name ]" => '外部付款人', + '收款人开户行名[ Beneficiary account bank ]' => '中国银行常州经济开发区支行', + "收款人账号[ Payee's Account Number ]" => '5547485000000001', + "收款人名称[ Payee's Name ]" => '中国银行测试公司', + '交易日期[ Transaction Date ]' => '20261121', + '交易时间[ Transaction time ]' => '10:00:00', + '交易货币[ Trade Currency ]' => 'CNY', + '交易金额[ Trade Amount ]' => 120, + '交易后余额[ After-transaction balance ]' => '1,090.00', + '起息日期[ Value Date ]' => '20261121', + '交易流水号[ Transaction reference number ]' => 'BOC-IN-001', + '摘要[ Reference ]' => '测试收入', + ]), + ]); + $payload = [ + 'scope' => BankBillTemplateExporter::SCOPE_NON_DIRECT, + 'bank' => '中国银行', + 'file' => new UploadedFile($path, '新佰广中行测试.xlsx', null, null, true), + ]; + $previewUrl = route('business.financial-ledger.bank-bills.import.preview', [ + 'company_id' => $this->company->id, + ]); + $importUrl = route('business.financial-ledger.bank-bills.import', [ + 'company_id' => $this->company->id, + ]); + + try { + $this->loginAs()->post($previewUrl, $payload) + ->assertOk() + ->assertJsonPath('result.rows', 2) + ->assertJsonPath('result.accounts', 1) + ->assertJsonPath('result.first_date', '2026-11-20') + ->assertJsonPath('result.last_date', '2026-11-21'); + + $this->loginAs()->post($importUrl, $payload) + ->assertOk() + ->assertJsonPath('result.inserted', 2) + ->assertJsonPath('result.synced_accounts', 0); + + $this->assertDatabaseHas('fin_bank_direct_bills', [ + 'finance_account_id' => $accountId, + 'bank_name' => '中国银行', + 'account_short' => '新佰广中行测试', + 'record_code' => 'BOC-OUT-001', + 'counterparty_name' => '外部收款人', + 'counterparty_account' => '6222000000000002', + 'counterparty_bank' => '招商银行常州分行', + 'income' => 0, + 'expense' => 30, + 'balance' => 970, + 'currency' => '人民币', + 'source' => 'bank-monthly', + ], 'business_mirror'); + $this->assertDatabaseHas('fin_bank_direct_bills', [ + 'finance_account_id' => $accountId, + 'bank_name' => '中国银行', + 'record_code' => 'BOC-IN-001', + 'counterparty_name' => '外部付款人', + 'counterparty_account' => '6222000000000003', + 'counterparty_bank' => '华夏银行股份有限公司', + 'income' => 120, + 'expense' => 0, + 'balance' => 1090, + 'currency' => '人民币', + 'source' => 'bank-monthly', + ], 'business_mirror'); + } finally { + @unlink($path); + } + } + public function test_huaxia_non_direct_import_resolves_account_from_unique_company_and_validates_totals(): void { $this->user->roles()->sync([