[ '账号', '账号名称', '币种', '交易日', '交易时间', '起息日', '交易类型', '借方金额', '贷方金额', '余额', '摘要', '流水号', '流程实例号', '业务名称', '用途', '业务参考号', '业务摘要', '其它摘要', '收(付)方分行名', '收(付)方名称', '收(付)方账号', '收(付)方开户行行号', '收(付)方开户行名', '收(付)方开户行地址', '母(子)公司账号分行名', '母(子)公司账号', '母(子)公司名称', '信息标志', '有否附件信息', '冲账标志', '扩展摘要', '交易分析码', '票据号', '商务支付订单号', '内部编号', '公司一卡通号', ], '华夏银行' => [ '单位名称', '银行账号', '账户名称', '账户别名', '银行名称', '交易时间', '收入金额', '支出金额', '账户余额', '币种', '对方账号', '对方户名', '对方开户行', '附言', '摘要', '交易描述', '凭证号', '回单认领状态', ], '网商银行' => [ '账务流水号', '提交时间', '交易时间', '交易名称', '借方金额(收)', '贷方金额(支)', '余额', '对方户名', '对方账号', '对方机构', '备注', ], ]; private const NON_DIRECT_HEADERS = [ '华夏银行' => [ '序号', '交易日期', '交易时间', '支出金额', '收入金额', '余额', '对方账号', '对方户名', '对方行名', '核心流水号', '交易描述', '摘要', '凭证号码', '明细标注', '记账日期', ], '华夏银行个人卡' => [ '记账日期', '摘要', '收入金额', '支出金额', '余额', '交易机构', '对方姓名', '对方卡/账号', '对方开户行', '附言', ], '杭州银行' => [ '序号', '交易时间', '对手方别名', '对手方银行账号', '收入金额(元)', '支出金额(元)', '己方账户余额(元)', '摘要', '附言(用途)', '银行流水号', '己方银行户名', '己方银行账号', '己方开户行', '对手方开户行', '币种', '渠道', ], '农业银行' => [ '交易时间', '收入金额', '支出金额', '账户余额', '对方账号', '对方户名', '对方开户行', '摘要', ], '江南农商行' => [ '序号', '交易日期', '币种', '收入', '支出', '余额', '摘要', '备注', '对方账号', '对方账户名称', ], '中国银行' => [ '交易类型[ 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 ]', ], '中国工商银行' => [ '凭证号', '对方账号', '交易时间', '借贷标志', '对方单位', '对方行号', '用途', '摘要', '附言', '转入金额', '转出金额', '余额', ], ]; /** * @return array */ public static function scopes(): array { return [ [ 'key' => self::SCOPE_DIRECT, 'label' => '银企直连账户账单', 'schedule' => '每日导入一次', ], [ 'key' => self::SCOPE_NON_DIRECT, 'label' => '非银企直连账户账单', 'schedule' => '每月 4 号前导入上月账单', ], ]; } /** * @return array,scope:string,format:string}> */ public static function templates(string $scope = self::SCOPE_DIRECT): array { if ($scope === self::SCOPE_NON_DIRECT) { return collect(self::NON_DIRECT_HEADERS) ->map(static fn (array $headers, string $bank): array => [ 'bank' => $bank, 'label' => match ($bank) { '中国工商银行' => '工商银行 PDF 明细', '华夏银行个人卡' => '华夏银行个人卡明细', '农业银行' => '农业银行明细', default => $bank . '月度模板', }, 'headers' => $headers, 'scope' => self::SCOPE_NON_DIRECT, 'format' => self::format($bank, self::SCOPE_NON_DIRECT), ]) ->values() ->all(); } return collect(self::HEADERS) ->map(static fn (array $headers, string $bank): array => [ 'bank' => $bank, 'label' => $bank . '模板', 'headers' => $headers, 'scope' => self::SCOPE_DIRECT, 'format' => self::format($bank, self::SCOPE_DIRECT), ]) ->sortBy(static fn (array $template): int => (int) array_search( $template['bank'], self::BANK_ORDER, true, )) ->values() ->all(); } /** * @return array,scope:string,format:string}>> */ public static function templatesByScope(): array { return [ self::SCOPE_DIRECT => self::templates(self::SCOPE_DIRECT), self::SCOPE_NON_DIRECT => self::templates(self::SCOPE_NON_DIRECT), ]; } /** * @return list */ public static function banks(string $scope = self::SCOPE_DIRECT): array { return array_column(self::templates($scope), 'bank'); } /** * @return list */ public static function headers(string $bank, string $scope = self::SCOPE_DIRECT): array { if ($scope === self::SCOPE_NON_DIRECT && array_key_exists($bank, self::NON_DIRECT_HEADERS)) { return self::NON_DIRECT_HEADERS[$bank]; } if (! array_key_exists($bank, self::HEADERS)) { throw new InvalidArgumentException('请选择支持导入的银行模板。'); } return self::HEADERS[$bank]; } public static function format(string $bank, string $scope = self::SCOPE_DIRECT): string { if ($scope !== self::SCOPE_NON_DIRECT) { return 'xlsx'; } return match ($bank) { '中国工商银行' => 'pdf', '华夏银行个人卡', '农业银行', '江南农商行' => self::FORMAT_BANK_FILE, default => 'xlsx', }; } public static function storageBankName(string $templateBank): string { return $templateBank === '华夏银行个人卡' ? '华夏银行' : $templateBank; } public static function uploadOnlyMessage(string $bank, string $scope): ?string { return match (self::format($bank, $scope)) { 'pdf' => '工商银行 PDF 明细无需下载模板,请直接上传银行导出的 PDF。', self::FORMAT_BANK_FILE => sprintf( '%s请直接上传银行导出的 XLS/XLSX 明细,无需下载模板。', $bank, ), default => null, }; } /** * @return array{path:string,filename:string} */ public function create( string $bank, string $scope = self::SCOPE_DIRECT, ): array { $bank = trim($bank); if (! in_array($bank, self::banks($scope), true)) { throw new InvalidArgumentException('请选择支持导入的银行模板。'); } $uploadOnlyMessage = self::uploadOnlyMessage($bank, $scope); if ($uploadOnlyMessage !== null) { throw new InvalidArgumentException($uploadOnlyMessage); } $headers = self::headers($bank, $scope); $isHuaXiaMonthly = $scope === self::SCOPE_NON_DIRECT && $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(match (true) { $isHuaXiaMonthly => '华夏月度明细', $isBocMonthly => '中行月度明细', default => $bank, }); $headerRow = match (true) { $isHuaXiaMonthly => 12, $isBocMonthly => 9, $bank === '网商银行' => 5, default => 1, }; if ($isHuaXiaMonthly) { $sheet->fromArray([ ['华夏银行交易明细'], [], [], [], [], ['币种:', '人民币', null, null, '户名:', '请填写户名'], ['查询日期:', '请填写起止日期'], ['交易方向:', '全部'], ['排序方向:', '由近及远'], ['收入总金额:', '0.00', null, null, '收入总笔数:', '0'], ['支出总金额:', '0.00', null, null, '支出总笔数:', '0'], ], null, 'A1', true); $sheet->mergeCells('A1:O1'); $sheet->getStyle('A1:O1')->getFont()->setBold(true)->setSize(14); $sheet->getStyle('A1:O1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('A6:F11')->getFill() ->setFillType(Fill::FILL_SOLID) ->getStartColor() ->setARGB('FFF7F0DE'); 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([ ['浙江网商银行企业账户交易明细'], ['企业名称', '请填写企业名称', null, null, '企业账号', '请填写企业账号(人民币)'], ['借方交易笔数', '0笔', null, null, '借方交易金额', '¥0.00'], ['贷方交易笔数', '0笔', null, null, '贷方交易金额', '¥0.00'], ], null, 'A1', true); $sheet->mergeCells('A1:K1'); $sheet->getStyle('A1:K1')->getFont()->setBold(true)->setSize(14); $sheet->getStyle('A1:K1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); $sheet->getStyle('A2:F4')->getFill() ->setFillType(Fill::FILL_SOLID) ->getStartColor() ->setARGB('FFF7F0DE'); $sheet->getStyle('A2:A4')->getFont()->setBold(true); $sheet->getStyle('E2:E4')->getFont()->setBold(true); } $sheet->fromArray([$headers], null, 'A' . $headerRow, true); $lastColumn = Coordinate::stringFromColumnIndex(count($headers)); $headerRange = "A{$headerRow}:{$lastColumn}{$headerRow}"; $sheet->getStyle($headerRange)->applyFromArray([ 'fill' => [ 'fillType' => Fill::FILL_SOLID, 'startColor' => ['argb' => 'FF1D1D1F'], ], 'font' => [ 'bold' => true, 'color' => ['argb' => 'FFFFFFFF'], 'size' => 11, ], 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, ], ]); $sheet->getRowDimension($headerRow)->setRowHeight(26); $sheet->freezePane('A' . ($headerRow + 1)); $sheet->setAutoFilter($headerRange); foreach ($headers as $index => $label) { $columnIndex = $index + 1; $column = Coordinate::stringFromColumnIndex($columnIndex); $sheet->getColumnDimension($column)->setWidth($this->columnWidth($label)); $format = $this->numberFormat($label); if ($format !== null) { $sheet->getStyle($column . ($headerRow + 1) . ':' . $column . '5000') ->getNumberFormat() ->setFormatCode($format); } } $temporary = tempnam(sys_get_temp_dir(), 'caiwu-bank-template-'); if ($temporary === false) { throw new RuntimeException('无法创建银行模板临时文件。'); } @unlink($temporary); $path = $temporary . '.xlsx'; try { (new Xlsx($spreadsheet))->save($path); } finally { $spreadsheet->disconnectWorksheets(); } return [ 'path' => $path, 'filename' => $displayName . '账单导入模板.xlsx', ]; } private function columnWidth(string $label): float { return match (true) { str_contains($label, '公司名称'), str_contains($label, '单位名称'), str_contains($label, '账号名称'), str_contains($label, '账户名称'), str_contains($label, '账户别名'), str_contains($label, '对方户名') => 28, str_contains($label, '开户行'), str_contains($label, '对方机构') => 34, str_contains($label, '账号'), str_contains($label, '流水号'), str_contains($label, '凭证号') => 24, default => 16, }; } private function numberFormat(string $label): ?string { if (str_contains($label, '金额') || str_contains($label, '余额')) { return '#,##0.00'; } if (str_starts_with($label, '交易时间') || $label === '提交时间') { return 'yyyy-mm-dd hh:mm:ss'; } if ($label === '交易日' || str_starts_with($label, '交易日期') || str_starts_with($label, '起息日期')) { return 'yyyy-mm-dd'; } return null; } }