documents

This commit is contained in:
Simon Pocrnjič
2025-10-12 12:24:17 +02:00
parent 3ab1c05fcc
commit e0303ece74
22 changed files with 898 additions and 88 deletions
@@ -41,9 +41,22 @@ public function render(DocumentTemplate $template, Contract $contract, User $use
// Determine effective unresolved policy early (template override -> global -> config)
$globalSettingsEarly = app(\App\Services\Documents\DocumentSettings::class)->get();
$effectivePolicy = $template->fail_on_unresolved ? 'fail' : ($globalSettingsEarly->unresolved_policy ?? config('documents.unresolved_policy', 'fail'));
$resolved = $this->resolver->resolve($tokens, $template, $contract, $user, $effectivePolicy);
// Resolve with support for custom.* tokens: per-generation overrides and defaults from template meta or global settings.
$customOverrides = request()->input('custom', []); // if called via HTTP context; otherwise pass explicitly from caller
$customDefaults = is_array($template->meta['custom_defaults'] ?? null) ? $template->meta['custom_defaults'] : null;
$resolved = $this->resolver->resolve(
$tokens,
$template,
$contract,
$user,
$effectivePolicy,
is_array($customOverrides) ? $customOverrides : [],
$customDefaults,
'empty'
);
$values = $resolved['values'];
$initialUnresolved = $resolved['unresolved'];
$customTypes = $resolved['customTypes'] ?? [];
// Formatting options
$fmt = $template->formatting_options ?? [];
$decimals = (int) ($fmt['number_decimals'] ?? 2);
@@ -55,8 +68,10 @@ public function render(DocumentTemplate $template, Contract $contract, User $use
$globalSettings = app(\App\Services\Documents\DocumentSettings::class)->get();
$globalDateFormats = $globalSettings->date_formats ?? [];
foreach ($values as $k => $v) {
// Date formatting (heuristic based on key ending with _date or .date)
if (is_string($v) && ($k === 'generation.date' || preg_match('/(^|\.)[A-Za-z_]*date$/i', $k))) {
$isTypedDate = ($customTypes[$k] ?? null) === 'date';
$isTypedNumber = ($customTypes[$k] ?? null) === 'number';
// Date formatting (typed or heuristic based on key ending with _date or .date)
if (is_string($v) && ($isTypedDate || $k === 'generation.date' || preg_match('/(^|\.)[A-Za-z_]*date$/i', $k))) {
$dateFmtOverrides = $fmt['date_formats'] ?? [];
$desiredFormat = $dateFmtOverrides[$k]
?? ($globalDateFormats[$k] ?? null)
@@ -75,9 +90,11 @@ public function render(DocumentTemplate $template, Contract $contract, User $use
}
}
}
if (is_numeric($v)) {
// Number formatting: only for explicitly typed numbers or common monetary fields
$isFinanceField = (bool) preg_match('/(^|\.)\b(amount|balance|total|price|cost)\b$/i', $k);
if (($isTypedNumber || $isFinanceField) && is_numeric($v)) {
$num = number_format((float) $v, $decimals, $decSep, $thouSep);
if ($currencySymbol && preg_match('/(amount|balance|total|price|cost)/i', $k)) {
if ($currencySymbol && $isFinanceField) {
$space = $currencySpace ? ' ' : '';
if ($currencyPos === 'after') {
$num = $num.$space.$currencySymbol;