Add account entity token support (balance_amount); factories + feature test

This commit is contained in:
Simon Pocrnjič 2025-10-06 19:20:05 +02:00
parent 80b3c7230b
commit 84d15ac715
4 changed files with 106 additions and 1 deletions

View File

@ -78,6 +78,9 @@ private function entityAttribute(string $entity, string $attr, Contract $contrac
$person = optional(optional($contract->clientCase)->person);
return (string) $person->{$attr};
case 'account':
$account = optional($contract->account);
return (string) $account->{$attr};
default:
return '';
}

View File

@ -17,7 +17,12 @@ class AccountFactory extends Factory
public function definition(): array
{
return [
//
'reference' => $this->faker->uuid(),
'description' => $this->faker->sentence(4),
'type_id' => \App\Models\AccountType::factory(),
'active' => 1,
'initial_amount' => 0,
'balance_amount' => 0,
];
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Database\Factories;
use App\Models\AccountType;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<AccountType>
*/
class AccountTypeFactory extends Factory
{
protected $model = AccountType::class;
public function definition(): array
{
return [
'name' => $this->faker->unique()->word(),
'description' => $this->faker->sentence(3),
];
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace Tests\Feature;
use App\Models\Account;
use App\Models\Contract;
use App\Models\DocumentSetting;
use App\Models\Role;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class DocumentAccountTokenTest extends TestCase
{
public function test_account_balance_token_renders(): void
{
Storage::fake('public');
$user = User::factory()->create();
$role = Role::firstOrCreate(['slug' => 'admin'], ['name' => 'Admin']);
$user->roles()->sync([$role->id]);
$this->actingAs($user);
// Extend whitelist with account.balance_amount
$settings = DocumentSetting::instance();
$wl = $settings->whitelist;
$wl['account'] = array_values(array_unique(array_merge($wl['account'] ?? [], ['balance_amount'])));
$settings->whitelist = $wl;
$settings->save();
app(\App\Services\Documents\DocumentSettings::class)->refresh();
// Prepare contract + account
$contract = Contract::factory()->create();
$account = Account::factory()->create([
'contract_id' => $contract->id,
]);
$account->update(['balance_amount' => 987.65]);
// Build docx
$tmp = tempnam(sys_get_temp_dir(), 'doc');
$zip = new \ZipArchive;
$zip->open($tmp, \ZipArchive::OVERWRITE);
$zip->addFromString('[Content_Types].xml', '<Types></Types>');
$zip->addFromString('word/document.xml', '<w:document><w:body>{{account.balance_amount}}</w:body></w:document>');
$zip->close();
$bytes = file_get_contents($tmp);
Storage::disk('public')->put('templates/account-balance.docx', $bytes);
$template = new \App\Models\DocumentTemplate;
$template->fill([
'name' => 'AccountTest',
'slug' => 'account-template',
'core_entity' => 'contract',
'version' => 1,
'engine' => 'docx',
'file_path' => 'templates/account-balance.docx',
'file_hash' => sha1($bytes),
'file_size' => strlen($bytes),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'active' => true,
'output_filename_pattern' => null,
'fail_on_unresolved' => false,
'entities' => [],
'columns' => [],
'tokens' => [],
'created_by' => $user->id,
'updated_by' => $user->id,
]);
$template->save();
$resp = $this->postJson(route('contracts.generate-document', ['contract' => $contract->uuid]), [
'template_slug' => 'account-template',
]);
$resp->assertOk();
}
}