70 lines
2.8 KiB
PHP
70 lines
2.8 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
use App\Models\AdWallet;
|
||
use App\Models\LegalEntity;
|
||
use App\Models\SaasInvoice;
|
||
use App\Models\Tenant;
|
||
use App\Models\User;
|
||
use App\Services\Billing\Invoice\InvoicePaymentService;
|
||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Illuminate\Support\Facades\Storage;
|
||
|
||
uses(DatabaseTransactions::class);
|
||
|
||
/**
|
||
* Рекламный кошелёк, Часть A, Task 10 (финал) — счёт с credit_target=advertising
|
||
* зачисляет ad_wallets, НЕ tenants.balance_rub (leads). Зеркалит
|
||
* seedPaidScenario из tests/Feature/Billing/InvoiceMarkPaidTest.php.
|
||
*/
|
||
function seedInvoiceForCreditTarget(string $balance, string $amount, string $creditTarget): array
|
||
{
|
||
$tenant = Tenant::factory()->create(['balance_rub' => $balance]);
|
||
User::factory()->create(['tenant_id' => $tenant->id]);
|
||
$le = LegalEntity::create([
|
||
'code' => 'mp_'.uniqid(), 'name' => 'ИП Лидерра', 'legal_form' => 'IP',
|
||
'inn' => '770000000099', 'is_default' => true,
|
||
]);
|
||
$invoice = SaasInvoice::create([
|
||
'tenant_id' => $tenant->id, 'legal_entity_id' => $le->id,
|
||
'invoice_number' => 'СЧ-2026-00'.random_int(1000, 9999), 'payer_type' => 'legal', 'payer_name' => 'ООО К',
|
||
'payer_inn' => '5000000000', 'amount_net' => $amount, 'amount_total' => $amount,
|
||
'credit_target' => $creditTarget,
|
||
'status' => SaasInvoice::STATUS_ISSUED, 'issued_at' => now(), 'expires_at' => now()->addDays(5),
|
||
]);
|
||
|
||
return [$tenant, $invoice];
|
||
}
|
||
|
||
it('счёт с credit_target=advertising зачисляет рекламный кошелёк, не баланс за лиды', function () {
|
||
Storage::fake('local');
|
||
Mail::fake();
|
||
[$tenant, $invoice] = seedInvoiceForCreditTarget('100.00', '1000.00', 'advertising');
|
||
|
||
app(InvoicePaymentService::class)->markPaid($invoice->id);
|
||
|
||
$invoice->refresh();
|
||
$tenant->refresh();
|
||
$wallet = AdWallet::where('tenant_id', $tenant->id)->first();
|
||
|
||
expect($invoice->status)->toBe(SaasInvoice::STATUS_PAID)
|
||
->and((string) $wallet->balance_rub)->toBe('1000.00')
|
||
->and((string) $tenant->balance_rub)->toBe('100.00'); // баланс за лиды НЕ тронут
|
||
});
|
||
|
||
it('счёт с credit_target=leads (умолчание) зачисляет баланс за лиды, не рекламный кошелёк', function () {
|
||
Storage::fake('local');
|
||
Mail::fake();
|
||
[$tenant, $invoice] = seedInvoiceForCreditTarget('100.00', '1500.00', 'leads');
|
||
|
||
app(InvoicePaymentService::class)->markPaid($invoice->id);
|
||
|
||
$tenant->refresh();
|
||
$wallet = AdWallet::where('tenant_id', $tenant->id)->first();
|
||
|
||
expect((string) $tenant->balance_rub)->toBe('1600.00')
|
||
->and($wallet)->toBeNull();
|
||
});
|