CaiWuXiTong/tests/Feature/Business/InvoiceQuotaPageTest.php
linchong 69ffa43d43 feat: 发票额度读取票聚销方可用额度 (#1233)
发票额度改为读取票聚销方管理的最新可用额度,并每 15 分钟同步共享镜像。
2026-11-18 15:35:10 +08:00

156 lines
5.8 KiB
PHP

<?php
namespace Tests\Feature\Business;
use App\Models\Auth\Permission;
use App\Models\Business\InvoiceBasic;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\Feature\FeatureTestCase;
class InvoiceQuotaPageTest extends FeatureTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->user->attachPermission(
Permission::where('name', 'read-admin-panel')->firstOrFail()
);
config(['upstream_mirrors.connection' => 'sqlite']);
Schema::dropIfExists('piaoju_seller_quota_snapshots');
Schema::create('piaoju_seller_quota_snapshots', function (Blueprint $table): void {
$table->bigIncrements('id');
$table->string('seller_key', 64)->unique();
$table->string('seller_name', 191);
$table->string('tax_number', 64)->nullable();
$table->boolean('seller_status')->nullable();
$table->string('auth_status', 32)->nullable();
$table->string('auth_status_desc', 64)->nullable();
$table->decimal('invoice_limit', 18, 2)->nullable();
$table->dateTime('invoice_limit_updated_at')->nullable();
$table->dateTime('source_fetched_at');
$table->dateTime('synced_at');
$table->timestamps();
});
$this->createInvoiceBasic('甲公司', '甲店铺组', 10000, 8500, '2026-11');
$this->createInvoiceBasic('乙公司', '乙店铺', 2000, 2500, '2026-11');
$this->createInvoiceBasic('丙公司', '丙店铺', 5000, 1000, '2026-11');
$this->createInvoiceBasic('零额度公司', '零额度店铺', 0, 0, '2026-11');
$this->createInvoiceBasic('历史公司', '历史店铺', 100, 100, '2026-10');
$this->createPiaojuSnapshot('pi-1', '甲公司', 15000, '2026-11-18 13:30:07');
$this->createPiaojuSnapshot('pi-2', '乙公司', -100, '2026-11-18 13:51:43');
$this->createPiaojuSnapshot('pi-3', '丙公司', 4000, '2026-11-18 13:29:10');
}
public function test_invoice_quota_is_listed_after_invoice_basic_information(): void
{
$pages = collect(config('business_navigation'))
->firstWhere('slug', 'invoice-management')['pages'];
$this->assertSame(
['automatic-invoicing', 'basic-information', 'invoice-quota', 'invoice-reports', 'store-backend-invoicing'],
array_column($pages, 'slug')
);
}
public function test_invoice_quota_defaults_to_latest_period_and_shows_ticket_union_summary(): void
{
$this->loginAs()
->get($this->pageUrl())
->assertOk()
->assertSeeText('发票额度')
->assertSeeText('当前开票额度')
->assertSeeText('17,000.00')
->assertSeeText('12,000.00')
->assertSeeText('18,900.00')
->assertSeeText('甲公司')
->assertSeeText('15,000.00')
->assertSeeText('8,500.00')
->assertDontSeeText('1,500.00')
->assertSeeText('乙公司')
->assertSeeText('-100.00')
->assertSeeText('额度不足')
->assertSeeText('未匹配')
->assertSeeText('不可查看 0 · 额度不足 1')
->assertSeeText('2026-11-18 13:30')
->assertDontSeeText('历史公司')
->assertSee('<option value="2026-11" selected>', false);
}
public function test_invoice_quota_filters_by_period_and_seller_entity(): void
{
$this->loginAs()
->get($this->pageUrl(['seller_entity' => '甲公司']))
->assertOk()
->assertSeeText('甲公司')
->assertSeeText('10,000.00')
->assertSeeText('15,000.00')
->assertSeeText('8,500.00')
->assertDontSeeText('乙公司')
->assertDontSeeText('丙公司');
$this->loginAs()
->get($this->pageUrl(['period' => '2026-10']))
->assertOk()
->assertSeeText('历史公司')
->assertDontSeeText('甲公司')
->assertSee('<option value="2026-10" selected>', false);
}
private function createPiaojuSnapshot(
string $sellerKey,
string $sellerName,
float $invoiceLimit,
string $updatedAt
): void {
DB::connection('sqlite')->table('piaoju_seller_quota_snapshots')->insert([
'seller_key' => $sellerKey,
'seller_name' => $sellerName,
'tax_number' => 'TAX-' . $sellerKey,
'seller_status' => true,
'auth_status' => 'AUTHENTICATED',
'auth_status_desc' => '已授权',
'invoice_limit' => $invoiceLimit,
'invoice_limit_updated_at' => $updatedAt,
'source_fetched_at' => '2026-11-18 14:00:00',
'synced_at' => '2026-11-18 14:00:01',
'created_at' => '2026-11-18 14:00:01',
'updated_at' => '2026-11-18 14:00:01',
]);
}
private function createInvoiceBasic(
string $entity,
string $storeName,
float $invoiceLimit,
float $outputAmount,
string $period
): void {
InvoiceBasic::create([
'company_id' => $this->company->id,
'store_name' => $storeName,
'store_entity' => $entity,
'invoice_issuer' => $entity . '开票员',
'invoice_limit' => $invoiceLimit,
'output_amount' => $outputAmount,
'input_amount' => 0,
'period' => $period,
]);
}
private function pageUrl(array $query = []): string
{
$url = route('business.pages.show', [
'section' => 'invoice-management',
'page' => 'invoice-quota',
]);
return $query === [] ? $url : $url . '?' . http_build_query($query);
}
}