parent
4ca8685324
commit
10fd8bfdb4
@ -22,6 +22,7 @@ use Illuminate\View\View;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerAccountCreator;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerAccountEditor;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerAccountTemplate;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerAlipayBasicImporter;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerCalculator;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerFlowEditor;
|
||||
use Modules\EcommerceFinance\Services\FinanceLedgerFlowSummaryEditor;
|
||||
@ -104,6 +105,41 @@ class FinanceLedger extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
public function importAlipayBasic(
|
||||
Request $request,
|
||||
FinanceLedgerAlipayBasicImporter $importer,
|
||||
): RedirectResponse {
|
||||
abort_unless(
|
||||
FinanceLedgerAccess::canEditAccountStatus(user()),
|
||||
403,
|
||||
'只有财务台账管理员可以导入支付宝基本信息。',
|
||||
);
|
||||
|
||||
$data = $request->validate([
|
||||
'file' => ['required', 'file', 'mimes:xlsx,xls', 'max:5120'],
|
||||
]);
|
||||
|
||||
try {
|
||||
$result = $importer->import((string) $data['file']->getRealPath());
|
||||
} catch (Throwable $exception) {
|
||||
throw ValidationException::withMessages([
|
||||
'file' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->back()->with(
|
||||
'success',
|
||||
sprintf(
|
||||
'支付宝基本信息导入完成:共 %d 条,新增 %d 条,更新 %d 条,未变化 %d 条,保留表格外现有账户 %d 条。',
|
||||
$result['total'],
|
||||
$result['inserted'],
|
||||
$result['updated'],
|
||||
$result['unchanged'],
|
||||
$result['preserved'],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function storeAccount(Request $request, FinanceLedgerAccountCreator $creator): JsonResponse
|
||||
{
|
||||
abort_unless(
|
||||
|
||||
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
private function connection(): string
|
||||
{
|
||||
return (string) config('upstream_mirrors.connection', 'business_mirror');
|
||||
}
|
||||
|
||||
public function up(): void
|
||||
{
|
||||
$schema = Schema::connection($this->connection());
|
||||
if (! $schema->hasTable('fin_accounts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$schema->table('fin_accounts', function (Blueprint $table) use ($schema): void {
|
||||
if (! $schema->hasColumn('fin_accounts', 'alipay_merchant_no')) {
|
||||
$table->string('alipay_merchant_no', 100)->nullable()->after('account_no');
|
||||
}
|
||||
if (! $schema->hasColumn('fin_accounts', 'alipay_account_kind')) {
|
||||
$table->string('alipay_account_kind', 20)->nullable()->after('account_type');
|
||||
}
|
||||
if (! $schema->hasColumn('fin_accounts', 'alipay_store_name')) {
|
||||
$table->string('alipay_store_name', 200)->nullable()->after('bank_city_branch');
|
||||
}
|
||||
});
|
||||
|
||||
if (! collect($schema->getIndexes('fin_accounts'))
|
||||
->contains('name', 'fin_accounts_alipay_merchant_index')) {
|
||||
$schema->table('fin_accounts', function (Blueprint $table): void {
|
||||
$table->index(['alipay_merchant_no'], 'fin_accounts_alipay_merchant_index');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$schema = Schema::connection($this->connection());
|
||||
if (! $schema->hasTable('fin_accounts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (collect($schema->getIndexes('fin_accounts'))
|
||||
->contains('name', 'fin_accounts_alipay_merchant_index')) {
|
||||
$schema->table('fin_accounts', function (Blueprint $table): void {
|
||||
$table->dropIndex('fin_accounts_alipay_merchant_index');
|
||||
});
|
||||
}
|
||||
|
||||
$schema->table('fin_accounts', function (Blueprint $table) use ($schema): void {
|
||||
foreach (['alipay_store_name', 'alipay_account_kind', 'alipay_merchant_no'] as $column) {
|
||||
if ($schema->hasColumn('fin_accounts', $column)) {
|
||||
$table->dropColumn($column);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,441 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\EcommerceFinance\Services;
|
||||
|
||||
use App\Utilities\FinanceLedgerAccountShouldKeep;
|
||||
use App\Utilities\FinanceLedgerAccountStatus;
|
||||
use App\Utilities\FinanceLedgerAccountType;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* 合并导入支付宝基本信息。
|
||||
*
|
||||
* 优先按支付宝账号匹配现有记录,其次按商户号,最后只在公司候选唯一时
|
||||
* 按公司匹配。未出现在表格中的现有支付宝账户会保留,避免历史流水失联。
|
||||
*/
|
||||
class FinanceLedgerAlipayBasicImporter
|
||||
{
|
||||
private const MAX_ROWS = 1000;
|
||||
|
||||
private const REQUIRED_HEADERS = [
|
||||
'公司名称',
|
||||
'账户简称',
|
||||
'法人',
|
||||
'账户类型',
|
||||
'商户号',
|
||||
'支付宝账号',
|
||||
'账户状态',
|
||||
'是否保留',
|
||||
'店铺',
|
||||
'备注',
|
||||
];
|
||||
|
||||
private readonly ConnectionInterface $connection;
|
||||
|
||||
public function __construct(DatabaseManager $databases)
|
||||
{
|
||||
$this->connection = $databases->connection(
|
||||
(string) config('upstream_mirrors.connection', 'business_mirror'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{total:int,inserted:int,updated:int,unchanged:int,preserved:int}
|
||||
*/
|
||||
public function import(string $path): array
|
||||
{
|
||||
if (! is_file($path)) {
|
||||
throw new RuntimeException('导入文件不存在。');
|
||||
}
|
||||
|
||||
$reader = IOFactory::createReaderForFile($path);
|
||||
$reader->setReadDataOnly(true);
|
||||
$spreadsheet = $reader->load($path);
|
||||
|
||||
try {
|
||||
$sheet = $spreadsheet->getSheetByName('支付宝基本信息')
|
||||
?? $spreadsheet->getSheet(0);
|
||||
$rows = $this->rows($sheet);
|
||||
} finally {
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
return $this->connection->transaction(function () use ($rows): array {
|
||||
$current = $this->connection->table('fin_accounts')
|
||||
->where(function ($scope): void {
|
||||
$scope->where('account_short', 'like', '%支付宝%')
|
||||
->orWhere('account_type', 'like', '%支付宝%');
|
||||
})
|
||||
->orderBy('id')
|
||||
->lockForUpdate()
|
||||
->get()
|
||||
->all();
|
||||
|
||||
$byAccountNo = [];
|
||||
$byMerchantNo = [];
|
||||
$byCompany = [];
|
||||
foreach ($current as $account) {
|
||||
$accountNo = $this->normalizeAccountNo($account->account_no ?? null);
|
||||
if ($accountNo !== '') {
|
||||
$byAccountNo[$accountNo] = $account;
|
||||
}
|
||||
$merchantNo = $this->normalizeAccountNo($account->alipay_merchant_no ?? null);
|
||||
if ($merchantNo !== '') {
|
||||
$byMerchantNo[$merchantNo] = $account;
|
||||
}
|
||||
$byCompany[(string) $account->company_name][] = $account;
|
||||
}
|
||||
|
||||
$matches = [];
|
||||
$unmatched = [];
|
||||
foreach ($rows as $index => $row) {
|
||||
$account = $byAccountNo[$row['account_no']] ?? null;
|
||||
if ($account === null && $row['merchant_no'] !== '') {
|
||||
$account = $byMerchantNo[$row['merchant_no']] ?? null;
|
||||
}
|
||||
if ($account !== null) {
|
||||
$matches[$index] = $account;
|
||||
} else {
|
||||
$unmatched[$index] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
$used = [];
|
||||
foreach ($matches as $account) {
|
||||
$used[(int) $account->id] = true;
|
||||
}
|
||||
|
||||
$unmatchedByCompany = [];
|
||||
foreach ($unmatched as $index => $row) {
|
||||
$unmatchedByCompany[$row['company_name']][] = $index;
|
||||
}
|
||||
foreach ($unmatchedByCompany as $companyName => $indexes) {
|
||||
$candidates = array_values(array_filter(
|
||||
$byCompany[$companyName] ?? [],
|
||||
static fn (object $account): bool => ! isset($used[(int) $account->id]),
|
||||
));
|
||||
if (count($indexes) !== 1 || count($candidates) !== 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$index = $indexes[0];
|
||||
$account = $candidates[0];
|
||||
$matches[$index] = $account;
|
||||
$used[(int) $account->id] = true;
|
||||
unset($unmatched[$index]);
|
||||
}
|
||||
|
||||
$inserted = 0;
|
||||
$updated = 0;
|
||||
$unchanged = 0;
|
||||
$now = now();
|
||||
$actor = (string) (user()?->name ?? '支付宝基本信息导入');
|
||||
|
||||
foreach ($rows as $index => $row) {
|
||||
$account = $matches[$index] ?? null;
|
||||
if ($account !== null) {
|
||||
$values = $this->updateValues($row, $account);
|
||||
if ($values !== []) {
|
||||
$this->connection->table('fin_accounts')->where('id', $account->id)->update([
|
||||
...$values,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
$updated++;
|
||||
} else {
|
||||
$unchanged++;
|
||||
}
|
||||
$this->updateStatus((string) $account->account_short, $row['status'], $actor, $now);
|
||||
continue;
|
||||
}
|
||||
|
||||
$accountShort = $this->uniqueAccountShort(
|
||||
$row['account_short'] !== ''
|
||||
? $row['account_short']
|
||||
: $this->deriveAccountShort($row['company_name']),
|
||||
);
|
||||
$this->connection->table('fin_accounts')->insert([
|
||||
'company_name' => $row['company_name'],
|
||||
'account_short' => $accountShort,
|
||||
'legal_representative' => $row['legal_representative'],
|
||||
'region' => null,
|
||||
'account_no' => $row['account_no'],
|
||||
'alipay_merchant_no' => $row['merchant_no'] !== '' ? $row['merchant_no'] : null,
|
||||
'bank_name' => '/',
|
||||
'bank_city_branch' => null,
|
||||
'alipay_store_name' => $row['store_name'],
|
||||
'account_type' => FinanceLedgerAccountType::ALIPAY,
|
||||
'alipay_account_kind' => $row['account_kind'],
|
||||
'company_plan' => $row['company_plan'],
|
||||
'account_should_keep' => $row['account_should_keep'],
|
||||
'current_balance' => 0,
|
||||
'blocked_amount' => 0,
|
||||
'daily_available' => 0,
|
||||
'remark' => $row['remark'],
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
$this->ensureCompany($row['company_name'], $now);
|
||||
$this->updateStatus($accountShort, $row['status'], $actor, $now);
|
||||
$inserted++;
|
||||
}
|
||||
|
||||
return [
|
||||
'total' => count($rows),
|
||||
'inserted' => $inserted,
|
||||
'updated' => $updated,
|
||||
'unchanged' => $unchanged,
|
||||
'preserved' => count(array_filter(
|
||||
$current,
|
||||
static fn (object $account): bool => ! isset($used[(int) $account->id]),
|
||||
)),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{
|
||||
* company_name:string,account_short:string,legal_representative:?string,
|
||||
* account_no:string,merchant_no:string,account_kind:string,
|
||||
* status:?string,company_plan:string,account_should_keep:string,
|
||||
* store_name:?string,remark:?string
|
||||
* }>
|
||||
*/
|
||||
private function rows(Worksheet $sheet): array
|
||||
{
|
||||
$lastColumnIndex = Coordinate::columnIndexFromString($sheet->getHighestDataColumn());
|
||||
$lastColumn = Coordinate::stringFromColumnIndex($lastColumnIndex);
|
||||
$headers = $sheet->rangeToArray('A1:' . $lastColumn . '1')[0] ?? [];
|
||||
$indexes = [];
|
||||
foreach ($headers as $index => $header) {
|
||||
$key = $this->text($header);
|
||||
if ($key !== '') {
|
||||
$indexes[$key] = $index;
|
||||
}
|
||||
}
|
||||
|
||||
$missing = array_values(array_diff(self::REQUIRED_HEADERS, array_keys($indexes)));
|
||||
if ($missing !== []) {
|
||||
throw new RuntimeException('表头缺少:' . implode('、', $missing));
|
||||
}
|
||||
|
||||
$highestRow = $sheet->getHighestDataRow();
|
||||
if ($highestRow - 1 > self::MAX_ROWS) {
|
||||
throw new RuntimeException('单次最多导入 ' . self::MAX_ROWS . ' 条支付宝账户。');
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
$seenAccountNos = [];
|
||||
$seenMerchantNos = [];
|
||||
foreach ($sheet->rangeToArray('A2:' . $lastColumn . $highestRow, null, false, false, false) as $index => $row) {
|
||||
$line = $index + 2;
|
||||
$value = static fn (string $header): mixed => $row[$indexes[$header]] ?? null;
|
||||
$company = $this->text($value('公司名称'));
|
||||
$accountNo = $this->normalizeAccountNo($value('支付宝账号'));
|
||||
if ($company === '' && $accountNo === '') {
|
||||
continue;
|
||||
}
|
||||
if ($company === '') {
|
||||
throw new RuntimeException("第 {$line} 行缺少公司名称。");
|
||||
}
|
||||
if ($accountNo === '') {
|
||||
throw new RuntimeException("第 {$line} 行缺少支付宝账号。");
|
||||
}
|
||||
if (isset($seenAccountNos[$accountNo])) {
|
||||
throw new RuntimeException("第 {$line} 行支付宝账号与第 {$seenAccountNos[$accountNo]} 行重复。");
|
||||
}
|
||||
$seenAccountNos[$accountNo] = $line;
|
||||
|
||||
$merchantNo = $this->normalizeAccountNo($value('商户号'));
|
||||
if ($merchantNo !== '' && isset($seenMerchantNos[$merchantNo])) {
|
||||
throw new RuntimeException("第 {$line} 行商户号与第 {$seenMerchantNos[$merchantNo]} 行重复。");
|
||||
}
|
||||
if ($merchantNo !== '') {
|
||||
$seenMerchantNos[$merchantNo] = $line;
|
||||
}
|
||||
|
||||
[$companyPlan, $accountShouldKeep] = $this->planning($value('是否保留'), $line);
|
||||
$rows[] = [
|
||||
'company_name' => $company,
|
||||
'account_short' => $this->text($value('账户简称')),
|
||||
'legal_representative' => $this->nullableText($value('法人')),
|
||||
'account_no' => $accountNo,
|
||||
'merchant_no' => $merchantNo,
|
||||
'account_kind' => $this->nullableText($value('账户类型')),
|
||||
'status' => $this->status($value('账户状态'), $line),
|
||||
'company_plan' => $companyPlan,
|
||||
'account_should_keep' => $accountShouldKeep,
|
||||
'store_name' => $this->nullableText($value('店铺')),
|
||||
'remark' => $this->nullableText($value('备注')),
|
||||
];
|
||||
}
|
||||
|
||||
if ($rows === []) {
|
||||
throw new RuntimeException('表格中没有可导入的支付宝账户。');
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $row
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private function updateValues(array $row, object $account): array
|
||||
{
|
||||
$values = [
|
||||
'company_name' => $row['company_name'],
|
||||
'account_no' => $row['account_no'],
|
||||
'account_type' => trim((string) ($account->account_type ?? '')) !== ''
|
||||
? (string) $account->account_type
|
||||
: FinanceLedgerAccountType::ALIPAY,
|
||||
'alipay_merchant_no' => $row['merchant_no'] !== ''
|
||||
? $row['merchant_no']
|
||||
: ($account->alipay_merchant_no ?? null),
|
||||
'alipay_account_kind' => $row['account_kind'] ?? ($account->alipay_account_kind ?? null),
|
||||
'alipay_store_name' => $row['store_name'] ?? ($account->alipay_store_name ?? null),
|
||||
'legal_representative' => $row['legal_representative'] ?? ($account->legal_representative ?? null),
|
||||
'company_plan' => $row['company_plan'],
|
||||
'account_should_keep' => $row['account_should_keep'],
|
||||
'remark' => $row['remark'] ?? ($account->remark ?? null),
|
||||
];
|
||||
|
||||
$changed = [];
|
||||
foreach ($values as $field => $value) {
|
||||
$current = $account->{$field} ?? null;
|
||||
if ((string) ($value ?? '') !== (string) ($current ?? '')) {
|
||||
$changed[$field] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $changed;
|
||||
}
|
||||
|
||||
/** @return array{0:string,1:string} */
|
||||
private function planning(mixed $value, int $line): array
|
||||
{
|
||||
$text = $this->text($value);
|
||||
if (in_array($text, ['保留', '是', '需要保留'], true)) {
|
||||
return ['保留', FinanceLedgerAccountShouldKeep::YES];
|
||||
}
|
||||
if (in_array($text, ['不保留', '否', '注销', '待注销'], true)) {
|
||||
return ['待注销', FinanceLedgerAccountShouldKeep::NO];
|
||||
}
|
||||
|
||||
throw new RuntimeException("第 {$line} 行“是否保留”只能填写保留或不保留。");
|
||||
}
|
||||
|
||||
private function status(mixed $value, int $line): ?string
|
||||
{
|
||||
$text = $this->text($value);
|
||||
if ($text === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$status = array_search($text, FinanceLedgerAccountStatus::OPTIONS, true);
|
||||
if ($status !== false) {
|
||||
return $status;
|
||||
}
|
||||
if (array_key_exists($text, FinanceLedgerAccountStatus::OPTIONS)) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
throw new RuntimeException("第 {$line} 行账户状态不在允许范围内。");
|
||||
}
|
||||
|
||||
private function updateStatus(string $accountShort, ?string $status, string $actor, mixed $now): void
|
||||
{
|
||||
if ($status === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$statuses = $this->connection->table('fin_account_statuses');
|
||||
$existing = $statuses->where('account_short', $accountShort)->exists();
|
||||
$values = [
|
||||
'status' => $status,
|
||||
'updated_by' => $actor,
|
||||
'updated_at' => $now,
|
||||
];
|
||||
if ($existing) {
|
||||
$statuses->where('account_short', $accountShort)->update($values);
|
||||
} else {
|
||||
$statuses->insert($values + [
|
||||
'account_short' => $accountShort,
|
||||
'created_at' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function ensureCompany(string $companyName, mixed $now): void
|
||||
{
|
||||
if ($this->connection->table('fin_companies')->where('name', $companyName)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->connection->table('fin_companies')->insert([
|
||||
'name' => $companyName,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
private function uniqueAccountShort(string $base): string
|
||||
{
|
||||
$base = mb_substr($this->text($base), 0, 90);
|
||||
$candidate = $base;
|
||||
$suffix = 1;
|
||||
while ($this->connection->table('fin_accounts')->where('account_short', $candidate)->exists()) {
|
||||
$suffix++;
|
||||
$candidate = $base . $suffix;
|
||||
}
|
||||
|
||||
return $candidate;
|
||||
}
|
||||
|
||||
private function deriveAccountShort(string $companyName): string
|
||||
{
|
||||
$name = preg_replace(
|
||||
'/^(?:常州市?|天津市?|上海市?|深圳市?|杭州市?|义乌市?|南召县?|佛山市?|苏州市?|南京市?|青岛市?|株洲市?|广州市?|永康市?|武进区|临沂市?|滕州市?)/u',
|
||||
'',
|
||||
$companyName,
|
||||
) ?? $companyName;
|
||||
$name = preg_replace('/^支付宝/u', '', $name) ?? $name;
|
||||
$name = preg_replace(
|
||||
'/(?:有限责任公司|股份有限公司|有限公司|个人独资|商贸行|家具有限公司|家具店|公司)$/u',
|
||||
'',
|
||||
$name,
|
||||
) ?? $name;
|
||||
$name = mb_substr(trim($name) !== '' ? trim($name) : $companyName, 0, 18);
|
||||
|
||||
return $name . '支付宝';
|
||||
}
|
||||
|
||||
private function normalizeAccountNo(mixed $value): string
|
||||
{
|
||||
return mb_strtolower(preg_replace('/[\s-]+/u', '', $this->text($value)) ?? '');
|
||||
}
|
||||
|
||||
private function text(mixed $value): string
|
||||
{
|
||||
if ($value === null) {
|
||||
return '';
|
||||
}
|
||||
$text = trim((string) $value);
|
||||
if (in_array($text, ['#N/A', '#DIV/0!', '#VALUE!', '#REF!', 'None'], true)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return preg_replace('/\s+/u', ' ', $text) ?? $text;
|
||||
}
|
||||
|
||||
private function nullableText(mixed $value): ?string
|
||||
{
|
||||
$text = $this->text($value);
|
||||
|
||||
return $text !== '' ? $text : null;
|
||||
}
|
||||
}
|
||||
@ -2115,6 +2115,8 @@ class FinanceLedgerPageData
|
||||
'company_name' => ['sort' => $a . '.company_name', 'type' => 'text'],
|
||||
'account_short' => ['sort' => $a . '.account_short', 'type' => 'text'],
|
||||
'legal_representative' => ['sort' => $a . '.legal_representative', 'type' => 'text'],
|
||||
'alipay_account_kind' => ['sort' => $a . '.alipay_account_kind', 'type' => 'text'],
|
||||
'alipay_merchant_no' => ['sort' => $a . '.alipay_merchant_no', 'type' => 'text'],
|
||||
'bank_affiliation' => ['sort' => $this->bankAffiliationSqlExpression($a . '.account_short'), 'type' => 'enum'],
|
||||
'account_status' => ['sort' => $statusExpression, 'type' => 'enum'],
|
||||
'region' => ['sort' => $a . '.region', 'type' => 'text'],
|
||||
@ -2122,6 +2124,7 @@ class FinanceLedgerPageData
|
||||
'bank_name' => ['sort' => $a . '.bank_name', 'type' => 'text'],
|
||||
'bound_alipay' => ['sort' => $a . '.bound_alipay', 'type' => 'text'],
|
||||
'bank_city_branch' => ['sort' => $a . '.bank_city_branch', 'type' => 'text'],
|
||||
'alipay_store_name' => ['sort' => $a . '.alipay_store_name', 'type' => 'text'],
|
||||
'company_plan' => ['sort' => $a . '.company_plan', 'type' => 'enum'],
|
||||
'account_should_keep' => ['sort' => $keepExpression, 'type' => 'enum'],
|
||||
'account_last4' => ['sort' => $a . '.account_last4', 'type' => 'text'],
|
||||
|
||||
@ -294,6 +294,23 @@
|
||||
]);
|
||||
}
|
||||
}
|
||||
if ($standaloneAccountView === 'non_store') {
|
||||
array_splice($ledgerTableHeaders['accounts'], 2, 0, [
|
||||
['legal_representative', '法人', 'text'],
|
||||
['alipay_account_kind', '账户类型', 'text'],
|
||||
['alipay_merchant_no', '商户号', 'text'],
|
||||
]);
|
||||
$bankCityBranchIndex = array_search(
|
||||
'bank_city_branch',
|
||||
array_column($ledgerTableHeaders['accounts'], 0),
|
||||
true,
|
||||
);
|
||||
if ($bankCityBranchIndex !== false) {
|
||||
array_splice($ledgerTableHeaders['accounts'], $bankCityBranchIndex + 1, 0, [
|
||||
['alipay_store_name', '店铺', 'text'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
$accountTableColumnCount = count($ledgerTableHeaders['accounts']);
|
||||
@endphp
|
||||
|
||||
@ -840,6 +857,12 @@
|
||||
导入网商基本信息
|
||||
</button>
|
||||
@endif
|
||||
@if ($standaloneAccountView === 'non_store' && $canEditAccountStatus)
|
||||
<button class="ledger-btn" type="button" data-alipay-import-trigger>
|
||||
<span class="material-icons-outlined" aria-hidden="true">upload_file</span>
|
||||
导入支付宝基本信息
|
||||
</button>
|
||||
@endif
|
||||
<a class="ledger-btn" href="{{ route('business.financial-ledger.accounts.template') }}" download data-account-template-download><span class="material-icons-outlined">download</span>导出录入模板</a>
|
||||
</div>
|
||||
<div class="ledger-download-status" data-account-template-download-status role="status" aria-live="polite" hidden>下载已开始,请查看浏览器下载目录。</div>
|
||||
@ -850,6 +873,12 @@
|
||||
<input type="file" name="file" accept=".xlsx" data-wangshang-import-file required>
|
||||
</form>
|
||||
@endif
|
||||
@if ($standaloneAccountView === 'non_store' && $canEditAccountStatus)
|
||||
<form method="POST" action="{{ route('business.financial-ledger.accounts.alipay.import') }}" enctype="multipart/form-data" data-alipay-import-form hidden>
|
||||
@csrf
|
||||
<input type="file" name="file" accept=".xlsx,.xls" data-alipay-import-file required>
|
||||
</form>
|
||||
@endif
|
||||
@if ($standaloneAccountView === null)
|
||||
<nav class="ledger-subtabbar" aria-label="账户总表账户分类">
|
||||
<a class="ledger-subtab {{ $accountView === 'bank' ? 'is-active' : '' }}" href="{{ $accountViewUrl('bank') }}" @if ($accountView === 'bank') aria-current="page" @endif>银行账户</a>
|
||||
@ -1154,6 +1183,11 @@
|
||||
@if ($standaloneAccountView === 'wangshang')
|
||||
<td data-account-legal>{{ $row->legal_representative }}</td>
|
||||
@endif
|
||||
@if ($standaloneAccountView === 'non_store')
|
||||
<td>{{ $row->legal_representative }}</td>
|
||||
<td>{{ $row->alipay_account_kind }}</td>
|
||||
<td>{{ $row->alipay_merchant_no }}</td>
|
||||
@endif
|
||||
<td>{{ $row->bank_affiliation ?: '未填写' }}</td>
|
||||
<td class="ledger-status-column">
|
||||
@if ($canEditAccountStatus)
|
||||
@ -1174,6 +1208,9 @@
|
||||
<td data-account-bound-alipay>{{ $row->bound_alipay }}</td>
|
||||
@endif
|
||||
<td>{{ $row->bank_city_branch }}</td>
|
||||
@if ($standaloneAccountView === 'non_store')
|
||||
<td>{{ $row->alipay_store_name }}</td>
|
||||
@endif
|
||||
<td class="ledger-plan-column">
|
||||
<span class="ledger-account-readonly" data-account-edit-display data-account-plan-display>{{ $row->company_plan ?: '未填写' }}</span>
|
||||
@if ($canEditAccountStatus)
|
||||
@ -3454,16 +3491,24 @@
|
||||
status.textContent = '下载已开始,请查看浏览器下载目录:财务台账-账户总表录入模板.xlsx';
|
||||
}
|
||||
});
|
||||
const wangshangImportTrigger = page.querySelector('[data-wangshang-import-trigger]');
|
||||
const wangshangImportForm = page.querySelector('[data-wangshang-import-form]');
|
||||
const wangshangImportFile = page.querySelector('[data-wangshang-import-file]');
|
||||
wangshangImportTrigger?.addEventListener('click', () => wangshangImportFile?.click());
|
||||
wangshangImportFile?.addEventListener('change', () => {
|
||||
if (!wangshangImportFile.files?.length || !wangshangImportForm) return;
|
||||
wangshangImportTrigger.disabled = true;
|
||||
wangshangImportTrigger.textContent = '正在导入…';
|
||||
wangshangImportForm.requestSubmit();
|
||||
});
|
||||
const bindAccountBasicImport = (triggerSelector, formSelector, fileSelector) => {
|
||||
const trigger = page.querySelector(triggerSelector);
|
||||
const form = page.querySelector(formSelector);
|
||||
const file = page.querySelector(fileSelector);
|
||||
trigger?.addEventListener('click', () => {
|
||||
if (!file) return;
|
||||
file.value = '';
|
||||
file.click();
|
||||
});
|
||||
file?.addEventListener('change', () => {
|
||||
if (!file.files?.length || !form) return;
|
||||
trigger.disabled = true;
|
||||
trigger.textContent = '正在导入…';
|
||||
form.requestSubmit();
|
||||
});
|
||||
};
|
||||
bindAccountBasicImport('[data-wangshang-import-trigger]', '[data-wangshang-import-form]', '[data-wangshang-import-file]');
|
||||
bindAccountBasicImport('[data-alipay-import-trigger]', '[data-alipay-import-form]', '[data-alipay-import-file]');
|
||||
|
||||
const availableTrigger = page.querySelector('[data-ledger-available-trigger]');
|
||||
const availableDialog = page.querySelector('[data-ledger-available-dialog]');
|
||||
|
||||
@ -1020,6 +1020,10 @@ Route::post('workspace/financial-ledger/accounts/wangshang/import', [\App\Http\C
|
||||
->middleware('business.page:financial-operations,ledger')
|
||||
->name('business.financial-ledger.accounts.wangshang.import');
|
||||
|
||||
Route::post('workspace/financial-ledger/accounts/alipay/import', [\App\Http\Controllers\Business\FinanceLedger::class, 'importAlipayBasic'])
|
||||
->middleware('business.page:financial-operations,ledger')
|
||||
->name('business.financial-ledger.accounts.alipay.import');
|
||||
|
||||
Route::post('workspace/financial-ledger/accounts/todo', [\App\Http\Controllers\Business\FinanceLedger::class, 'updateAccountTodo'])
|
||||
->middleware('business.page:financial-operations,ledger')
|
||||
->name('business.financial-ledger.accounts.todo');
|
||||
|
||||
233
tests/Feature/Business/FinanceLedgerAlipayBasicImportTest.php
Normal file
233
tests/Feature/Business/FinanceLedgerAlipayBasicImportTest.php
Normal file
@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Business;
|
||||
|
||||
use App\Models\Auth\Permission;
|
||||
use App\Models\Auth\Role;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
class FinanceLedgerAlipayBasicImportTest extends FeatureTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->user->attachPermission(
|
||||
Permission::where('name', 'read-admin-panel')->firstOrFail(),
|
||||
);
|
||||
}
|
||||
|
||||
private function pageUrl(): string
|
||||
{
|
||||
return route('business.financial-ledger.account-basic', [
|
||||
'company_id' => $this->company->id,
|
||||
'view' => 'non_store',
|
||||
]);
|
||||
}
|
||||
|
||||
private function importUrl(): string
|
||||
{
|
||||
return route('business.financial-ledger.accounts.alipay.import', [
|
||||
'company_id' => $this->company->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_alipay_page_exposes_basic_import_and_columns(): void
|
||||
{
|
||||
DB::connection('business_mirror')->table('fin_accounts')->insert([
|
||||
'company_name' => '页面展示支付宝公司',
|
||||
'account_short' => '展示支付宝',
|
||||
'alipay_merchant_no' => '208800000001',
|
||||
'alipay_account_kind' => '企业',
|
||||
'alipay_store_name' => '非店铺',
|
||||
'account_type' => '支付宝',
|
||||
'account_no' => 'display@example.com',
|
||||
'bank_name' => '/',
|
||||
'account_should_keep' => '否',
|
||||
]);
|
||||
|
||||
$this->loginAs()->get($this->pageUrl())
|
||||
->assertOk()
|
||||
->assertSee('导入支付宝基本信息')
|
||||
->assertSee('data-alipay-import-form', false)
|
||||
->assertSee('商户号')
|
||||
->assertSee('账户类型')
|
||||
->assertSee('店铺')
|
||||
->assertSee('208800000001')
|
||||
->assertSee('display@example.com');
|
||||
}
|
||||
|
||||
public function test_import_updates_existing_adds_new_and_preserves_other_accounts(): void
|
||||
{
|
||||
$connection = DB::connection('business_mirror');
|
||||
$accounts = [
|
||||
[
|
||||
'company_name' => '支付宝导入甲公司',
|
||||
'account_short' => '导入甲支付宝',
|
||||
'account_type' => '支付宝',
|
||||
'account_no' => 'alpha@example.com',
|
||||
'bank_name' => '/',
|
||||
'current_balance' => 100,
|
||||
'remark' => '原备注保留',
|
||||
'account_should_keep' => '是',
|
||||
'company_plan' => '保留',
|
||||
],
|
||||
[
|
||||
'company_name' => '支付宝导入乙公司',
|
||||
'account_short' => '导入乙支付宝',
|
||||
'account_type' => '支付宝',
|
||||
'account_no' => 'old-beta@example.com',
|
||||
'bank_name' => '/',
|
||||
'current_balance' => 50,
|
||||
'account_should_keep' => '是',
|
||||
'company_plan' => '保留',
|
||||
],
|
||||
[
|
||||
'company_name' => '表格外支付宝公司',
|
||||
'account_short' => '表格外支付宝',
|
||||
'account_type' => '支付宝',
|
||||
'account_no' => 'outside@example.com',
|
||||
'bank_name' => '/',
|
||||
'current_balance' => 25,
|
||||
'account_should_keep' => '是',
|
||||
'company_plan' => '保留',
|
||||
],
|
||||
[
|
||||
'company_name' => '银行账户公司',
|
||||
'account_short' => '测试银行账户',
|
||||
'account_type' => '公户',
|
||||
'account_no' => '888800001',
|
||||
'bank_name' => '测试银行',
|
||||
],
|
||||
];
|
||||
foreach ($accounts as $account) {
|
||||
$connection->table('fin_accounts')->insert($account);
|
||||
}
|
||||
|
||||
$this->loginAs()->post($this->importUrl(), [
|
||||
'file' => $this->workbook(),
|
||||
])->assertRedirect()->assertSessionHas('success');
|
||||
|
||||
$this->assertDatabaseHas('fin_accounts', [
|
||||
'account_short' => '导入甲支付宝',
|
||||
'company_name' => '支付宝导入甲公司',
|
||||
'legal_representative' => '法人甲',
|
||||
'alipay_merchant_no' => '208800000101',
|
||||
'alipay_account_kind' => '企业',
|
||||
'alipay_store_name' => '非店铺',
|
||||
'account_no' => 'alpha@example.com',
|
||||
'company_plan' => '待注销',
|
||||
'account_should_keep' => '否',
|
||||
'current_balance' => 100,
|
||||
'remark' => '原备注保留',
|
||||
], 'business_mirror');
|
||||
|
||||
$this->assertDatabaseHas('fin_accounts', [
|
||||
'account_short' => '导入乙支付宝',
|
||||
'company_name' => '支付宝导入乙公司',
|
||||
'legal_representative' => '法人乙',
|
||||
'alipay_merchant_no' => '208800000102',
|
||||
'alipay_account_kind' => '个人',
|
||||
'alipay_store_name' => '天猫-测试店',
|
||||
'account_no' => 'beta@example.com',
|
||||
'company_plan' => '保留',
|
||||
'account_should_keep' => '是',
|
||||
'current_balance' => 50,
|
||||
'remark' => '新备注',
|
||||
], 'business_mirror');
|
||||
|
||||
$this->assertDatabaseHas('fin_accounts', [
|
||||
'company_name' => '支付宝新增丙公司',
|
||||
'account_short' => '新增丙支付宝',
|
||||
'legal_representative' => '法人丙',
|
||||
'alipay_merchant_no' => '208800000103',
|
||||
'alipay_account_kind' => '企业',
|
||||
'alipay_store_name' => '非店铺',
|
||||
'account_no' => 'gamma@example.com',
|
||||
'account_type' => '支付宝',
|
||||
'bank_name' => '/',
|
||||
'company_plan' => '待注销',
|
||||
'account_should_keep' => '否',
|
||||
], 'business_mirror');
|
||||
|
||||
$this->assertDatabaseHas('fin_accounts', [
|
||||
'account_short' => '表格外支付宝',
|
||||
'current_balance' => 25,
|
||||
], 'business_mirror');
|
||||
$this->assertDatabaseHas('fin_accounts', [
|
||||
'account_short' => '测试银行账户',
|
||||
'account_type' => '公户',
|
||||
], 'business_mirror');
|
||||
$this->assertDatabaseMissing('fin_accounts', [
|
||||
'account_no' => 'old-beta@example.com',
|
||||
'account_short' => '导入乙支付宝',
|
||||
], 'business_mirror');
|
||||
$this->assertDatabaseHas('fin_companies', [
|
||||
'name' => '支付宝新增丙公司',
|
||||
], 'business_mirror');
|
||||
|
||||
$this->loginAs()->post($this->importUrl(), [
|
||||
'file' => $this->workbook(),
|
||||
])->assertRedirect()->assertSessionHas(
|
||||
'success',
|
||||
'支付宝基本信息导入完成:共 3 条,新增 0 条,更新 0 条,未变化 3 条,保留表格外现有账户 1 条。',
|
||||
);
|
||||
$this->assertDatabaseCount('fin_accounts', 5, 'business_mirror');
|
||||
}
|
||||
|
||||
public function test_import_rejects_duplicate_accounts_and_requires_editor_role(): void
|
||||
{
|
||||
$this->withExceptionHandling()->loginAs()->post($this->importUrl(), [
|
||||
'file' => $this->workbook(true),
|
||||
])->assertRedirect()->assertSessionHasErrors('file');
|
||||
|
||||
$viewer = user_model_class()::create([
|
||||
'name' => '支付宝只读用户',
|
||||
'email' => 'alipay-viewer-' . uniqid() . '@example.com',
|
||||
'password' => 'local-test-password',
|
||||
'locale' => 'zh-CN',
|
||||
'enabled' => 1,
|
||||
'landing_page' => 'dashboard',
|
||||
'created_from' => 'core::test',
|
||||
]);
|
||||
$viewer->companies()->attach($this->company->id);
|
||||
$viewer->roles()->attach(Role::where('name', 'user')->firstOrFail()->id);
|
||||
$viewer->attachPermission(Permission::where('name', 'read-admin-panel')->firstOrFail());
|
||||
|
||||
$this->withExceptionHandling()->loginAs($viewer)->post($this->importUrl(), [
|
||||
'file' => $this->workbook(),
|
||||
])->assertForbidden();
|
||||
}
|
||||
|
||||
private function workbook(bool $duplicate = false): UploadedFile
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$rows = [
|
||||
['公司名称', '账户简称', '法人', '账户类型', '商户号', '支付宝账号', '账户状态', '是否保留', '店铺', '备注'],
|
||||
['支付宝导入甲公司', null, '法人甲', '企业', '208800000101', 'alpha@example.com', null, '不保留', '非店铺', null],
|
||||
['支付宝导入乙公司', null, '法人乙', '个人', '208800000102', 'beta@example.com', null, '保留', '天猫-测试店', '新备注'],
|
||||
['支付宝新增丙公司', null, '法人丙', '企业', '208800000103', 'gamma@example.com', null, '不保留', '非店铺', null],
|
||||
];
|
||||
if ($duplicate) {
|
||||
$rows[2][5] = 'alpha@example.com';
|
||||
}
|
||||
$sheet->fromArray($rows, null, 'A1');
|
||||
|
||||
$path = tempnam(sys_get_temp_dir(), 'alipay-basic-');
|
||||
(new Xlsx($spreadsheet))->save($path);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
||||
return new UploadedFile(
|
||||
$path,
|
||||
'支付宝基本信息.xlsx',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
null,
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user