316 lines
13 KiB
PHP
316 lines
13 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Models\Auth\Permission;
|
|
use App\Models\Auth\Role;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class FinanceLedgerManualTransactionImportTest extends FeatureTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Carbon::setTestNow('2026-11-18 12:00:00');
|
|
|
|
$this->user->attachPermission(
|
|
Permission::where('name', 'read-admin-panel')->firstOrFail(),
|
|
);
|
|
|
|
$connection = DB::connection('business_mirror');
|
|
$connection->table('fin_accounts')->insert([
|
|
[
|
|
'company_name' => '导入测试公司',
|
|
'account_short' => '导入测试账户甲',
|
|
'account_type' => '公户',
|
|
'current_balance' => 100,
|
|
],
|
|
[
|
|
'company_name' => '导入测试公司',
|
|
'account_short' => '导入测试账户乙',
|
|
'account_type' => '公户',
|
|
'current_balance' => 0,
|
|
],
|
|
]);
|
|
$connection->table('fin_account_opening')->insert([
|
|
[
|
|
'account_short' => '导入测试账户甲',
|
|
'opening_date' => '2026-10-17',
|
|
'opening_balance' => 100,
|
|
],
|
|
[
|
|
'account_short' => '导入测试账户乙',
|
|
'opening_date' => '2026-10-17',
|
|
'opening_balance' => 0,
|
|
],
|
|
]);
|
|
$connection->table('fin_transactions')->insert([
|
|
'txn_date' => '2026-11-16',
|
|
'account_short' => '导入测试账户甲',
|
|
'counterparty_info' => '既有对方',
|
|
'detail' => '既有重复流水',
|
|
'income' => 50,
|
|
'expense' => 0,
|
|
'balance' => 150,
|
|
'payment_remark' => '既有备注',
|
|
'approval_no' => 'EXISTING-1',
|
|
'source' => 'wangshang-bank',
|
|
'source_value' => 'EXISTING-SOURCE-1',
|
|
'process_type' => '入账',
|
|
'row_key' => 'manual-import-existing-duplicate',
|
|
]);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Carbon::setTestNow();
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_transaction_page_places_import_after_custom_column_and_actions_after_approval(): void
|
|
{
|
|
DB::connection('business_mirror')->table('fin_transactions')->insert([
|
|
'txn_date' => '2026-11-17',
|
|
'account_short' => '导入测试账户甲',
|
|
'detail' => '可编辑手工明细',
|
|
'income' => 10,
|
|
'expense' => 0,
|
|
'approval_no' => 'EDITABLE-1',
|
|
'source' => 'manual',
|
|
'process_type' => '手工录入',
|
|
'row_key' => 'manual-import-action-column-row',
|
|
]);
|
|
|
|
$html = $this->loginAs()->get($this->pageUrl())->assertOk()->getContent();
|
|
$addColumn = strpos($html, 'data-ledger-statement-column-add');
|
|
$import = strpos($html, 'data-ledger-transaction-import-trigger');
|
|
$approvalHeader = strpos($html, 'data-ledger-header-field="approval_no"');
|
|
$actionHeader = strpos($html, 'data-ledger-action-column');
|
|
|
|
$this->assertNotFalse($addColumn);
|
|
$this->assertNotFalse($import);
|
|
$this->assertNotFalse($approvalHeader);
|
|
$this->assertNotFalse($actionHeader);
|
|
$this->assertLessThan($import, $addColumn);
|
|
$this->assertLessThan($actionHeader, $approvalHeader);
|
|
$this->assertStringContainsString('data-ledger-transaction-import-dialog', $html);
|
|
$this->assertStringContainsString('data-ledger-manual-edit', $html);
|
|
$this->assertStringContainsString('data-ledger-manual-delete', $html);
|
|
$this->assertMatchesRegularExpression(
|
|
'/<span class="ledger-transaction-approval-text"[^>]*>EDITABLE-1<\/span>\s*<\/td>\s*<td>\s*'
|
|
. '<div class="ledger-transaction-actions">.*?data-ledger-manual-edit.*?data-ledger-manual-delete/s',
|
|
$html,
|
|
);
|
|
}
|
|
|
|
public function test_import_template_downloads_with_required_columns(): void
|
|
{
|
|
$this->loginAs()->get($this->templateUrl())
|
|
->assertOk()
|
|
->assertDownload();
|
|
}
|
|
|
|
public function test_downloaded_xlsx_template_can_be_filled_and_previewed(): void
|
|
{
|
|
$download = $this->loginAs()->get($this->templateUrl())->assertOk();
|
|
$templatePath = tempnam(sys_get_temp_dir(), 'ledger-manual-import-template-');
|
|
$uploadPath = tempnam(sys_get_temp_dir(), 'ledger-manual-import-upload-');
|
|
file_put_contents($templatePath, $download->streamedContent());
|
|
|
|
try {
|
|
$spreadsheet = IOFactory::load($templatePath);
|
|
$spreadsheet->getSheetByName('动账明细')->fromArray([
|
|
'2026-11-17',
|
|
'导入测试账户乙',
|
|
'Excel 对方',
|
|
'66.66',
|
|
'',
|
|
'66.66',
|
|
'Excel 备注',
|
|
'Excel 模板导入',
|
|
'XLSX-IMPORT-1',
|
|
], null, 'A2');
|
|
(new Xlsx($spreadsheet))->save($uploadPath);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
$this->loginAs()->post(
|
|
$this->previewUrl(),
|
|
[
|
|
'import' => new UploadedFile(
|
|
$uploadPath,
|
|
'动账明细.xlsx',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
null,
|
|
true,
|
|
),
|
|
],
|
|
['Accept' => 'application/json'],
|
|
)->assertOk()
|
|
->assertJsonPath('stats.valid', 1)
|
|
->assertJsonPath('rows.0.display.account_short', '导入测试账户乙')
|
|
->assertJsonPath('rows.0.display.income', '66.66');
|
|
} finally {
|
|
@unlink($templatePath);
|
|
@unlink($uploadPath);
|
|
}
|
|
}
|
|
|
|
public function test_preview_reports_valid_duplicate_and_invalid_rows_without_writing(): void
|
|
{
|
|
$file = UploadedFile::fake()->createWithContent(
|
|
'手工动账.csv',
|
|
"\xEF\xBB\xBF" . implode("\n", [
|
|
'日期,账户简称,对方信息,收入(元),支出(元),余额(元),付款备注,明细,审批编号/业务单号',
|
|
'2026-11-17,导入测试账户甲,新对方,100.00,,200.00,新备注,可导入收入,IMP-NEW-1',
|
|
'2026-11-16,导入测试账户甲,既有对方,50.00,,150.00,既有备注,既有重复流水,EXISTING-1',
|
|
'2026-11-17,导入测试账户甲,错误方向,10.00,20.00,210.00,,收支同时填写,INVALID-1',
|
|
'2026-11-17,不存在账户,未知账户,30.00,,, ,未知账户流水,INVALID-2',
|
|
]) . "\n",
|
|
);
|
|
|
|
$response = $this->loginAs()->post(
|
|
$this->previewUrl(),
|
|
['import' => $file],
|
|
['Accept' => 'application/json'],
|
|
);
|
|
|
|
$response->assertOk()
|
|
->assertJsonPath('stats.total', 4)
|
|
->assertJsonPath('stats.valid', 1)
|
|
->assertJsonPath('stats.duplicate', 1)
|
|
->assertJsonPath('stats.invalid', 2)
|
|
->assertJsonPath('rows.0.status', 'ready')
|
|
->assertJsonPath('rows.1.status', 'duplicate')
|
|
->assertJsonPath('rows.2.status', 'invalid')
|
|
->assertJsonPath('rows.3.status', 'invalid');
|
|
$this->assertNotEmpty($response->json('preview_token'));
|
|
$this->assertSame(1, DB::connection('business_mirror')->table('fin_transactions')->count());
|
|
}
|
|
|
|
public function test_commit_imports_only_ready_rows_recalculates_flows_and_consumes_token(): void
|
|
{
|
|
$csv = implode("\n", [
|
|
'日期,账户简称,对方信息,收入(元),支出(元),余额(元),付款备注,明细,审批编号/业务单号',
|
|
'2026-11-17,导入测试账户甲,客户甲,100.00,,250.00,首款,导入收入一,IMP-READY-1',
|
|
'2026-11-18,导入测试账户甲,供应商乙,,30.00,220.00,尾款,导入支出一,IMP-READY-2',
|
|
'2026-11-18,导入测试账户甲,错误行,10.00,20.00,,,同时填写,INVALID-COMMIT',
|
|
]) . "\n";
|
|
$preview = $this->loginAs()->post(
|
|
$this->previewUrl(),
|
|
['import' => UploadedFile::fake()->createWithContent('手工动账.csv', $csv)],
|
|
['Accept' => 'application/json'],
|
|
)->assertOk()
|
|
->assertJsonPath('stats.valid', 2)
|
|
->assertJsonPath('stats.invalid', 1);
|
|
$token = (string) $preview->json('preview_token');
|
|
|
|
$this->loginAs()->postJson($this->commitUrl(), ['preview_token' => $token])
|
|
->assertOk()
|
|
->assertJsonPath('imported', 2)
|
|
->assertJsonPath('duplicate_skipped', 0)
|
|
->assertJsonPath('summary.count', 3);
|
|
|
|
$connection = DB::connection('business_mirror');
|
|
$this->assertSame(2, $connection->table('fin_transactions')
|
|
->where('process_type', '手工导入')
|
|
->where('source', 'manual')
|
|
->count());
|
|
$this->assertDatabaseHas('fin_transactions', [
|
|
'account_short' => '导入测试账户甲',
|
|
'detail' => '导入收入一',
|
|
'income' => 100,
|
|
'approval_no' => 'IMP-READY-1',
|
|
'created_by' => 'Test User',
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_daily_flows', [
|
|
'flow_date' => '2026-11-17',
|
|
'account_short' => '导入测试账户甲',
|
|
'inflow' => 100,
|
|
'closing_balance' => 250,
|
|
], 'business_mirror');
|
|
$this->assertDatabaseHas('fin_ledger_edit_events', [
|
|
'entity_type' => 'manual_transaction_import',
|
|
'user_name' => 'Test User',
|
|
], 'business_mirror');
|
|
|
|
$this->withExceptionHandling()->loginAs()->postJson(
|
|
$this->commitUrl(),
|
|
['preview_token' => $token],
|
|
)->assertUnprocessable()->assertJsonValidationErrors('preview_token');
|
|
|
|
$this->loginAs()->post(
|
|
$this->previewUrl(),
|
|
['import' => UploadedFile::fake()->createWithContent('手工动账.csv', $csv)],
|
|
['Accept' => 'application/json'],
|
|
)->assertOk()
|
|
->assertJsonPath('stats.valid', 0)
|
|
->assertJsonPath('stats.duplicate', 2)
|
|
->assertJsonPath('stats.invalid', 1);
|
|
}
|
|
|
|
public function test_import_endpoints_require_transaction_edit_permission(): void
|
|
{
|
|
$viewer = user_model_class()::create([
|
|
'name' => '动账导入只读用户',
|
|
'email' => 'manual-import-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)
|
|
->get($this->templateUrl())
|
|
->assertForbidden();
|
|
$this->withExceptionHandling()->loginAs($viewer)
|
|
->post(
|
|
$this->previewUrl(),
|
|
['import' => UploadedFile::fake()->createWithContent('手工动账.csv', "日期\n2026-11-17\n")],
|
|
['Accept' => 'application/json'],
|
|
)
|
|
->assertForbidden();
|
|
$this->withExceptionHandling()->loginAs($viewer)
|
|
->postJson($this->commitUrl(), ['preview_token' => str_repeat('a', 48)])
|
|
->assertForbidden();
|
|
}
|
|
|
|
private function pageUrl(): string
|
|
{
|
|
return route('business.pages.show', [
|
|
'company_id' => $this->company->id,
|
|
'section' => 'financial-operations',
|
|
'page' => 'ledger',
|
|
]) . '?tab=transactions';
|
|
}
|
|
|
|
private function templateUrl(): string
|
|
{
|
|
return route('business.financial-ledger.transactions.import.template', [
|
|
'company_id' => $this->company->id,
|
|
]);
|
|
}
|
|
|
|
private function previewUrl(): string
|
|
{
|
|
return route('business.financial-ledger.transactions.import.preview', [
|
|
'company_id' => $this->company->id,
|
|
]);
|
|
}
|
|
|
|
private function commitUrl(): string
|
|
{
|
|
return route('business.financial-ledger.transactions.import.commit', [
|
|
'company_id' => $this->company->id,
|
|
]);
|
|
}
|
|
}
|