From 84d15ac715de210f5f1cd593d9f38601a6532314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Pocrnji=C4=8D?= Date: Mon, 6 Oct 2025 19:20:05 +0200 Subject: [PATCH] Add account entity token support (balance_amount); factories + feature test --- app/Services/Documents/TokenValueResolver.php | 3 + database/factories/AccountFactory.php | 7 +- database/factories/AccountTypeFactory.php | 22 ++++++ tests/Feature/DocumentAccountTokenTest.php | 75 +++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 database/factories/AccountTypeFactory.php create mode 100644 tests/Feature/DocumentAccountTokenTest.php diff --git a/app/Services/Documents/TokenValueResolver.php b/app/Services/Documents/TokenValueResolver.php index 4a098c7..91a8f8c 100644 --- a/app/Services/Documents/TokenValueResolver.php +++ b/app/Services/Documents/TokenValueResolver.php @@ -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 ''; } diff --git a/database/factories/AccountFactory.php b/database/factories/AccountFactory.php index 12961fc..6a0ea6f 100644 --- a/database/factories/AccountFactory.php +++ b/database/factories/AccountFactory.php @@ -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, ]; } } diff --git a/database/factories/AccountTypeFactory.php b/database/factories/AccountTypeFactory.php new file mode 100644 index 0000000..867a6b0 --- /dev/null +++ b/database/factories/AccountTypeFactory.php @@ -0,0 +1,22 @@ + + */ +class AccountTypeFactory extends Factory +{ + protected $model = AccountType::class; + + public function definition(): array + { + return [ + 'name' => $this->faker->unique()->word(), + 'description' => $this->faker->sentence(3), + ]; + } +} diff --git a/tests/Feature/DocumentAccountTokenTest.php b/tests/Feature/DocumentAccountTokenTest.php new file mode 100644 index 0000000..594c990 --- /dev/null +++ b/tests/Feature/DocumentAccountTokenTest.php @@ -0,0 +1,75 @@ +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', ''); + $zip->addFromString('word/document.xml', '{{account.balance_amount}}'); + $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(); + } +}