152 lines
6.0 KiB
PHP
152 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Models\Auth\Permission;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class FinanceLedgerWangshangBasicImportTest extends FeatureTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Carbon::setTestNow('2026-11-13 18:00:00');
|
|
|
|
$this->user->attachPermission(
|
|
Permission::where('name', 'read-admin-panel')->firstOrFail(),
|
|
);
|
|
|
|
DB::connection('business_mirror')->table('fin_accounts')->insert([
|
|
[
|
|
'company_name' => '导入测试甲公司',
|
|
'account_short' => '测试甲网商',
|
|
'account_no' => '888800000001',
|
|
'bank_name' => '浙江网商银行',
|
|
'account_type' => '网商银行',
|
|
'account_should_keep' => '否',
|
|
'current_balance' => 100,
|
|
'blocked_amount' => 0,
|
|
'daily_available' => 100,
|
|
],
|
|
[
|
|
'company_name' => '导入测试乙公司',
|
|
'account_short' => '测试乙网商',
|
|
'account_no' => '888800000002',
|
|
'bank_name' => '浙江网商银行',
|
|
'account_type' => '网商银行',
|
|
'account_should_keep' => '否',
|
|
'current_balance' => 50,
|
|
'blocked_amount' => 0,
|
|
'daily_available' => 50,
|
|
],
|
|
[
|
|
'company_name' => '导入测试丙公司',
|
|
'account_short' => '测试丙网商',
|
|
'account_no' => '888800000003',
|
|
'bank_name' => '浙江网商银行',
|
|
'account_type' => '网商银行',
|
|
'account_should_keep' => '否',
|
|
'current_balance' => 25,
|
|
'blocked_amount' => 0,
|
|
'daily_available' => 25,
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function test_wangshang_page_exposes_import_and_new_columns(): void
|
|
{
|
|
$response = $this->loginAs()
|
|
->get(route('business.financial-ledger.account-basic', ['view' => 'wangshang']))
|
|
->assertOk()
|
|
->assertSee('导入网商基本信息')
|
|
->assertSee('法人')
|
|
->assertSee('绑定支付宝')
|
|
->assertSee('data-wangshang-import-form', false);
|
|
|
|
$html = $response->getContent();
|
|
$this->assertSame(
|
|
1,
|
|
preg_match('/<table class="ledger-table ledger-accounts-table">(.*?)<\/table>/s', $html, $table),
|
|
);
|
|
preg_match_all('/data-ledger-header-field="([^"]+)"/', $table[1], $columns);
|
|
$this->assertSame([
|
|
'company_name',
|
|
'account_short',
|
|
'legal_representative',
|
|
'company_plan',
|
|
'account_no',
|
|
'bank_name',
|
|
'bound_alipay',
|
|
'account_should_keep',
|
|
'account_last4',
|
|
'remark',
|
|
], $columns[1]);
|
|
}
|
|
|
|
public function test_import_updates_matches_adds_new_rows_and_preserves_other_accounts(): void
|
|
{
|
|
$response = $this->loginAs()->post(
|
|
route('business.financial-ledger.accounts.wangshang.import'),
|
|
['file' => $this->workbook()],
|
|
);
|
|
|
|
$response->assertRedirect();
|
|
$response->assertSessionHas('success');
|
|
|
|
$connection = DB::connection('business_mirror');
|
|
$first = $connection->table('fin_accounts')->where('account_short', '测试甲网商')->first();
|
|
$this->assertSame('法人甲', $first->legal_representative);
|
|
$this->assertSame('jia@example.com', $first->bound_alipay);
|
|
$this->assertSame('保留', $first->company_plan);
|
|
$this->assertSame('是', $first->account_should_keep);
|
|
$this->assertSame('0001', $first->account_last4);
|
|
|
|
$third = $connection->table('fin_accounts')->where('account_short', '测试丙网商')->first();
|
|
$this->assertSame('888800009999', $third->account_no);
|
|
$this->assertSame('待注销', $third->company_plan);
|
|
$this->assertSame('否', $third->account_should_keep);
|
|
|
|
$this->assertDatabaseHas('fin_accounts', [
|
|
'account_short' => '测试乙网商',
|
|
], 'business_mirror');
|
|
|
|
$new = $connection->table('fin_accounts')->where('account_no', '888800000004')->first();
|
|
$this->assertNotNull($new);
|
|
$this->assertSame('导入测试丁公司', $new->company_name);
|
|
$this->assertSame('网商银行', $new->account_type);
|
|
$this->assertSame('导入测试丁网商', $new->account_short);
|
|
$this->assertSame('法人丁', $new->legal_representative);
|
|
$this->assertSame('ding@example.com', $new->bound_alipay);
|
|
$this->assertSame('0004', $new->account_last4);
|
|
}
|
|
|
|
private function workbook(): UploadedFile
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->fromArray([
|
|
['公司名称', '账户简称', '法人', '账户状态', '网商账号', '开户行', '绑定支付宝', '是否保留', '后4位', '备注'],
|
|
['导入测试甲公司', null, '法人甲', '保留', '888800000001', '浙江网商银行', 'jia@example.com', '保留', null, null],
|
|
['导入测试丙公司', null, '法人丙', '不保留', '888800009999', '浙江网商银行', 'bing@example.com', '注销', null, '账号更新'],
|
|
['导入测试丁公司', null, '法人丁', '不保留', '888800000004', '浙江网商银行', 'ding@example.com', '注销', null, null],
|
|
], null, 'A1');
|
|
|
|
$path = tempnam(sys_get_temp_dir(), 'wangshang-basic-');
|
|
(new Xlsx($spreadsheet))->save($path);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
return new UploadedFile(
|
|
$path,
|
|
'网商银行基本信息.xlsx',
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
null,
|
|
true,
|
|
);
|
|
}
|
|
}
|