241 lines
7.8 KiB
PHP
241 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use InvalidArgumentException;
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use RuntimeException;
|
|
|
|
final class BankBillTemplateExporter
|
|
{
|
|
private const BANK_ORDER = ['华夏银行', '招商银行', '网商银行'];
|
|
|
|
private const HEADERS = [
|
|
'招商银行' => [
|
|
'账号',
|
|
'账号名称',
|
|
'币种',
|
|
'交易日',
|
|
'交易时间',
|
|
'起息日',
|
|
'交易类型',
|
|
'借方金额',
|
|
'贷方金额',
|
|
'余额',
|
|
'摘要',
|
|
'流水号',
|
|
'流程实例号',
|
|
'业务名称',
|
|
'用途',
|
|
'业务参考号',
|
|
'业务摘要',
|
|
'其它摘要',
|
|
'收(付)方分行名',
|
|
'收(付)方名称',
|
|
'收(付)方账号',
|
|
'收(付)方开户行行号',
|
|
'收(付)方开户行名',
|
|
'收(付)方开户行地址',
|
|
'母(子)公司账号分行名',
|
|
'母(子)公司账号',
|
|
'母(子)公司名称',
|
|
'信息标志',
|
|
'有否附件信息',
|
|
'冲账标志',
|
|
'扩展摘要',
|
|
'交易分析码',
|
|
'票据号',
|
|
'商务支付订单号',
|
|
'内部编号',
|
|
'公司一卡通号',
|
|
],
|
|
'华夏银行' => [
|
|
'单位名称',
|
|
'银行账号',
|
|
'账户名称',
|
|
'账户别名',
|
|
'银行名称',
|
|
'交易时间',
|
|
'收入金额',
|
|
'支出金额',
|
|
'账户余额',
|
|
'币种',
|
|
'对方账号',
|
|
'对方户名',
|
|
'对方开户行',
|
|
'附言',
|
|
'摘要',
|
|
'交易描述',
|
|
'凭证号',
|
|
'回单认领状态',
|
|
],
|
|
'网商银行' => [
|
|
'账务流水号',
|
|
'提交时间',
|
|
'交易时间',
|
|
'交易名称',
|
|
'借方金额(收)',
|
|
'贷方金额(支)',
|
|
'余额',
|
|
'对方户名',
|
|
'对方账号',
|
|
'对方机构',
|
|
'备注',
|
|
],
|
|
];
|
|
|
|
/**
|
|
* @return array<int,array{bank:string,headers:list<string>}>
|
|
*/
|
|
public static function templates(): array
|
|
{
|
|
return collect(self::HEADERS)
|
|
->map(static fn (array $headers, string $bank): array => [
|
|
'bank' => $bank,
|
|
'headers' => $headers,
|
|
])
|
|
->sortBy(static fn (array $template): int => (int) array_search(
|
|
$template['bank'],
|
|
self::BANK_ORDER,
|
|
true,
|
|
))
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
public static function banks(): array
|
|
{
|
|
return self::BANK_ORDER;
|
|
}
|
|
|
|
/**
|
|
* @return array{path:string,filename:string}
|
|
*/
|
|
public function create(string $bank): array
|
|
{
|
|
$bank = trim($bank);
|
|
if (! array_key_exists($bank, self::HEADERS)) {
|
|
throw new InvalidArgumentException('请选择支持导入的银行模板。');
|
|
}
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getProperties()
|
|
->setCreator('CaiWuXiTong')
|
|
->setTitle($bank . '账单导入模板');
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setTitle($bank);
|
|
$headers = self::HEADERS[$bank];
|
|
$headerRow = $bank === '网商银行' ? 5 : 1;
|
|
|
|
if ($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' => $bank . '账单导入模板.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 ($label === '交易时间' || $label === '提交时间') {
|
|
return 'yyyy-mm-dd hh:mm:ss';
|
|
}
|
|
if ($label === '交易日' || $label === '起息日') {
|
|
return 'yyyy-mm-dd';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|