Hoan thien core finance v2 - Calculation Pipeline, Form Templates

This commit is contained in:
2026-04-28 03:57:18 +00:00
parent 002c9a8b99
commit 49aa20a634
24 changed files with 1043 additions and 875 deletions

View File

@@ -138,22 +138,33 @@ class ContractForm
->label('Bảng chi tiết chiết khấu (Dạng Key-Value)')
->columnSpanFull(),
Placeholder::make('final_value_display')
->label('Giá trị sau chiết khấu')
Placeholder::make('price_sheet')
->label('Phiếu tính giá')
->columnSpanFull()
->content(function ($record, $get) {
$totalValue = $record ? (float) $record->total_value : (float) ($get('total_value') ?? 0);
$discountDetails = $record ? $record->discount_details : ($get('discount_details') ?? []);
if ($totalValue <= 0) {
return new HtmlString("<div style='font-size: 0.9rem; color: #9ca3af;'>Chưa có giá trị hợp đồng để tính chiết khấu.</div>");
->content(function ($record) {
if (! $record || ! $record->calculation_log) {
return new HtmlString("<div style='font-size: 0.9rem; color: #9ca3af;'>Chưa có dữ liệu tính toán. Vui lòng lưu hợp đồng để tạo phiếu tính giá.</div>");
}
$result = \App\Services\DiscountEngine::calculate($totalValue, $discountDetails);
$final = number_format($result['final_value']);
$discount = number_format($result['discount_amount']);
$steps = $record->calculation_log['price_sheet'] ?? [];
if (empty($steps)) {
return '-';
}
return new HtmlString("<div style='font-size: 1.1rem; font-weight: bold; color: #16a34a;'>{$final} VNĐ</div><div style='font-size: 0.8rem; color: #9ca3af;'>Đã chiết khấu: {$discount} VNĐ</div>");
$html = '<div style="background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px;">';
$html .= '<table style="width: 100%; border-collapse: collapse; font-size: 0.9rem;">';
$html .= '<thead><tr style="border-bottom: 2px solid #e5e7eb;"><th style="text-align: left; padding: 8px;">Diễn giải</th><th style="text-align: right; padding: 8px;">Giá trị (VNĐ)</th></tr></thead><tbody>';
foreach ($steps as $step) {
$desc = $step['description'];
$value = number_format($step['value']);
$isOverride = $step['is_overridden'] ? ' <span style="color: #f59e0b; font-size: 0.75rem;">(ghi đè)</span>' : '';
$style = str_contains(strtolower($desc), 'tổng') ? 'font-weight: bold; border-top: 1px solid #e5e7eb;' : '';
$html .= "<tr style='{$style}'><td style='padding: 8px;'>{$desc}{$isOverride}</td><td style='text-align: right; padding: 8px;'>{$value}</td></tr>";
}
$html .= '</tbody></table></div>';
return new HtmlString($html);
}),
]),

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Filament\Resources\FormTemplates;
use App\Filament\Resources\FormTemplates\Pages;
use App\Models\FormTemplate;
use App\Enums\NavigationGroup;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables\Table;
use App\Filament\Resources\FormTemplates\Schemas\FormTemplateForm;
use App\Filament\Resources\FormTemplates\Tables\FormTemplatesTable;
class FormTemplateResource extends Resource
{
protected static ?string $model = FormTemplate::class;
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-document-duplicate';
protected static string | \UnitEnum | null $navigationGroup = NavigationGroup::SETTING->value;
protected static ?int $navigationSort = 10;
protected static ?string $modelLabel = 'Biểu mẫu';
protected static ?string $pluralModelLabel = 'Biểu mẫu in ấn';
public static function form(Schema $schema): Schema
{
return FormTemplateForm::configure($schema);
}
public static function table(Table $table): Table
{
return FormTemplatesTable::configure($table);
}
public static function getPages(): array
{
return [
'index' => Pages\ListFormTemplates::route('/'),
'create' => Pages\CreateFormTemplate::route('/create'),
'edit' => Pages\EditFormTemplate::route('/{record}/edit'),
];
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Resources\FormTemplates\Pages;
use App\Filament\Resources\FormTemplates\FormTemplateResource;
use Filament\Resources\Pages\CreateRecord;
class CreateFormTemplate extends CreateRecord
{
protected static string $resource = FormTemplateResource::class;
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Resources\FormTemplates\Pages;
use App\Filament\Resources\FormTemplates\FormTemplateResource;
use Filament\Resources\Pages\EditRecord;
class EditFormTemplate extends EditRecord
{
protected static string $resource = FormTemplateResource::class;
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\FormTemplates\Pages;
use App\Filament\Resources\FormTemplates\FormTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListFormTemplates extends ListRecords
{
protected static string $resource = FormTemplateResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace App\Filament\Resources\FormTemplates\Schemas;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Repeater;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Schema;
class FormTemplateForm
{
public static function configure(Schema $schema): Schema
{
return $schema
->components([
// BLOCK 1: Thông tin cơ bản
Section::make('Thông tin biểu mẫu')
->schema([
Grid::make(3)
->schema([
TextInput::make('name')
->label('Tên biểu mẫu')
->required(),
TextInput::make('code')
->label('Mã biểu mẫu')
->required()
->unique(ignoreRecord: true),
Select::make('target_model')
->label('Áp dụng cho')
->options([
'App\Models\Contract' => 'Hợp đồng',
'App\Models\Product' => 'Sản phẩm',
'App\Models\Customer' => 'Khách hàng',
])
->required(),
]),
Grid::make(3)
->schema([
Select::make('paper_size')
->label('Khổ giấy')
->options([
'A4' => 'A4',
'A5' => 'A5',
'Letter' => 'Letter',
])
->default('A4')
->required(),
]),
]),
// BLOCK 2: Danh sách trường dữ liệu
Section::make('Danh sách trường dữ liệu (Merge Fields)')
->schema([
Repeater::make('fields')
->relationship('fields')
->schema([
Grid::make(3)
->schema([
TextInput::make('code')
->label('Mã trường')
->required()
->placeholder('ten_khach_hang'),
TextInput::make('label')
->label('Tên hiển thị')
->required()
->placeholder('Tên khách hàng'),
Select::make('source_type')
->label('Nguồn dữ liệu')
->options([
'db_column' => 'Cột trong DB',
'db_relation' => 'Quan hệ (relation)',
'formula' => 'Công thức tính toán',
'input' => 'Nhập tay khi in',
'static' => 'Giá trị cố định',
])
->required()
->live(),
]),
Grid::make(3)
->schema([
KeyValue::make('source_config')
->label('Cấu hình nguồn')
->keyLabel('Tham số')
->valueLabel('Giá trị')
->helperText(fn ($get) => match ($get('source_type')) {
'db_column' => 'Ví dụ: column => land_value',
'db_relation' => 'Ví dụ: relation => customers, column => full_name, index => 0',
'formula' => 'Ví dụ: expression => land_value + foundation_value',
'input' => 'Ví dụ: default => (giá trị mặc định)',
'static' => 'Ví dụ: value => Hà Nội',
default => 'Nhập cấu hình phù hợp với loại nguồn dữ liệu',
}),
Select::make('format')
->label('Định dạng hiển thị')
->options([
'text' => 'Văn bản',
'number' => 'Số',
'currency' => 'Tiền tệ (VNĐ)',
'date' => 'Ngày tháng',
'percent' => 'Phần trăm',
])
->default('text'),
TextInput::make('decimal_places')
->label('Số thập phân')
->numeric()
->default(0)
->visible(fn ($get) => in_array($get('format'), ['number', 'currency', 'percent'])),
]),
TextInput::make('display_order')
->label('Thứ tự')
->numeric()
->default(0)
->hidden(),
])
->addActionLabel('Thêm trường dữ liệu')
->reorderable()
->orderColumn('display_order')
->defaultItems(0)
->collapsible()
->columnSpanFull(),
]),
// BLOCK 3: Nội dung mẫu in - FULL WIDTH, TO RỘNG
Section::make('Nội dung mẫu in')
->schema([
RichEditor::make('html_template')
->label('')
->required()
->placeholder('Soạn thảo nội dung biểu mẫu...')
->helperText('Chèn trường dữ liệu bằng cú pháp {{ma_truong}}. Ví dụ: Tên khách hàng: {{ten_khach_hang}}')
->columnSpanFull(),
]),
]);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Filament\Resources\FormTemplates\Tables;
use Filament\Tables;
use Filament\Tables\Table;
class FormTemplatesTable
{
public static function configure(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label('Tên biểu mẫu')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('code')
->label('Mã')
->searchable()
->copyable(),
Tables\Columns\TextColumn::make('target_model')
->label('Áp dụng cho')
->formatStateUsing(fn ($state) => match ($state) {
'App\Models\Contract' => 'Hợp đồng',
'App\Models\Product' => 'Sản phẩm',
'App\Models\Customer' => 'Khách hàng',
default => $state,
}),
Tables\Columns\TextColumn::make('paper_size')
->label('Khổ giấy')
->badge(),
Tables\Columns\IconColumn::make('is_active')
->label('Hoạt động')
->boolean(),
Tables\Columns\TextColumn::make('fields_count')
->label('Số trường')
->counts('fields'),
])
->filters([
Tables\Filters\SelectFilter::make('target_model')
->label('Áp dụng cho')
->options([
'App\Models\Contract' => 'Hợp đồng',
'App\Models\Product' => 'Sản phẩm',
'App\Models\Customer' => 'Khách hàng',
]),
])
->defaultSort('name');
}
}

View File

@@ -16,6 +16,7 @@ class Contract extends Model
protected $casts = [
'metadata' => 'array',
'discount_details' => 'array',
'calculation_log' => 'array',
'total_value' => 'decimal:2',
'land_value' => 'decimal:2',
'foundation_value' => 'decimal:2',
@@ -79,16 +80,28 @@ class Contract extends Model
}
/**
* Giá trị sau chiết khấu.
* Giá trị sau chiết khấu (qua PriceCalculationService).
*/
public function getFinalValueAttribute(): float
{
$result = \App\Services\DiscountEngine::calculate(
(float) $this->total_value,
$this->discount_details
);
if ($this->calculation_log) {
return (float) ($this->calculation_log['final_values']['total_payment'] ?? 0);
}
return $result['final_value'];
// Fallback: tính nhanh nếu chưa có calculation_log
$result = \App\Services\Calculation\PriceCalculationService::calculateForContract($this);
return (float) ($result->get('total_payment') ?? 0);
}
/**
* Lấy phiếu tính giá chi tiết.
*/
public function getPriceSheetAttribute(): ?array
{
if ($this->calculation_log) {
return $this->calculation_log['price_sheet'] ?? null;
}
return null;
}
protected static function booted()
@@ -110,5 +123,19 @@ class Contract extends Model
$contract->remaining_amount = (float) ($contract->total_value ?? 0) - (float) ($contract->paid_amount ?? 0);
});
static::saved(function ($contract) {
// Tự động tính toán và lưu snapshot sau khi lưu
if ($contract->land_value || $contract->foundation_value) {
$result = \App\Services\Calculation\PriceCalculationService::calculateForContract($contract);
$contract->calculation_log = [
'steps' => $result->getSteps(),
'final_values' => $result->getValues(),
'price_sheet' => $result->toPriceSheet(),
'calculated_at' => now()->toDateTimeString(),
];
$contract->saveQuietly();
}
});
}
}

23
app/Models/FormField.php Normal file
View File

@@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormField extends Model
{
use HasUuids, HasFactory;
protected $guarded = [];
protected $casts = [
'source_config' => 'array',
];
public function template()
{
return $this->belongsTo(FormTemplate::class);
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormPrintLog extends Model
{
use HasUuids, HasFactory;
protected $guarded = [];
protected $casts = [
'snapshot_data' => 'array',
'printed_at' => 'datetime',
];
public function template()
{
return $this->belongsTo(FormTemplate::class);
}
public function printedBy()
{
return $this->belongsTo(User::class, 'printed_by');
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FormTemplate extends Model
{
use HasUuids, HasFactory;
protected $guarded = [];
protected $casts = [
'is_active' => 'boolean',
];
public function fields()
{
return $this->hasMany(FormField::class, 'template_id')->orderBy('display_order');
}
public function printLogs()
{
return $this->hasMany(FormPrintLog::class, 'template_id');
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Services\Calculation;
class CalculationPipeline
{
protected array $steps = [];
public function addStep(CalculationStep $step): static
{
$this->steps[] = $step;
return $this;
}
public function execute(array $initialData): CalculationResult
{
$data = $initialData;
$result = new CalculationResult();
foreach ($this->steps as $step) {
$stepResult = $step->execute($data);
$result->addStep($stepResult);
$data[$step->outputKey()] = $stepResult['rounded_value'];
}
return $result;
}
public function getSteps(): array
{
return $this->steps;
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Services\Calculation;
class CalculationResult
{
protected array $steps = [];
protected array $values = [];
public function addStep(array $stepResult): void
{
$this->steps[] = $stepResult;
$this->values[$stepResult['output_key']] = $stepResult['rounded_value'];
}
public function get(string $key): ?int
{
return $this->values[$key] ?? null;
}
public function getSteps(): array
{
return $this->steps;
}
public function getValues(): array
{
return $this->values;
}
public function toArray(): array
{
return [
'steps' => $this->steps,
'final_values' => $this->values,
];
}
public function toPriceSheet(): array
{
$sheet = [];
foreach ($this->steps as $step) {
$sheet[] = [
'description' => $step['name'],
'value' => $step['rounded_value'],
'is_overridden' => $step['is_overridden'],
];
}
return $sheet;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace App\Services\Calculation;
class CalculationStep
{
protected string $name;
protected string $outputKey;
protected \Closure $formula;
protected RoundingRule $roundingRule;
protected ?int $overrideValue = null;
protected bool $isOverridden = false;
protected array $dependencies = [];
public function __construct(
string $name,
string $outputKey,
\Closure $formula,
RoundingRule $roundingRule = RoundingRule::UNIT,
array $dependencies = []
) {
$this->name = $name;
$this->outputKey = $outputKey;
$this->formula = $formula;
$this->roundingRule = $roundingRule;
$this->dependencies = $dependencies;
}
public function name(): string
{
return $this->name;
}
public function outputKey(): string
{
return $this->outputKey;
}
public function dependencies(): array
{
return $this->dependencies;
}
public function override(int $value): static
{
$this->overrideValue = $value;
$this->isOverridden = true;
return $this;
}
public function isOverridden(): bool
{
return $this->isOverridden;
}
public function execute(array $data): array
{
if ($this->isOverridden) {
return [
'name' => $this->name,
'output_key' => $this->outputKey,
'formula_raw' => null,
'calculated_value' => null,
'rounded_value' => $this->overrideValue,
'is_overridden' => true,
'dependencies' => $this->dependencies,
];
}
$rawValue = call_user_func($this->formula, $data);
$roundedValue = $this->roundingRule->apply($rawValue);
return [
'name' => $this->name,
'output_key' => $this->outputKey,
'formula_raw' => $rawValue,
'calculated_value' => $rawValue,
'rounded_value' => $roundedValue,
'is_overridden' => false,
'dependencies' => $this->dependencies,
];
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace App\Services\Calculation;
use App\Models\Contract;
class PriceCalculationService
{
public static function forContract(Contract $contract): CalculationPipeline
{
$pipeline = new CalculationPipeline();
// Bước 1: Tổng giá trị trước chiết khấu
$pipeline->addStep(new CalculationStep(
name: 'Giá trị QSDĐ',
outputKey: 'land_value',
formula: fn ($data) => (float) ($data['land_value'] ?? 0),
roundingRule: RoundingRule::UNIT,
dependencies: []
));
$pipeline->addStep(new CalculationStep(
name: 'Giá trị Móng',
outputKey: 'foundation_value',
formula: fn ($data) => (float) ($data['foundation_value'] ?? 0),
roundingRule: RoundingRule::UNIT,
dependencies: []
));
$pipeline->addStep(new CalculationStep(
name: 'Tổng giá trị trước chiết khấu',
outputKey: 'subtotal',
formula: fn ($data) => $data['land_value'] + $data['foundation_value'],
roundingRule: RoundingRule::UNIT,
dependencies: ['land_value', 'foundation_value']
));
// Bước 2: Chiết khấu
$pipeline->addStep(new CalculationStep(
name: 'Chiết khấu',
outputKey: 'discount_amount',
formula: function ($data) {
$details = $data['discount_details'] ?? [];
if (!empty($details['total_amount'])) {
return (float) $details['total_amount'];
}
if (!empty($details['total_percentage'])) {
return $data['subtotal'] * ((float) $details['total_percentage'] / 100);
}
return 0;
},
roundingRule: RoundingRule::UNIT,
dependencies: ['subtotal', 'discount_details']
));
// Bước 3: Sau chiết khấu
$pipeline->addStep(new CalculationStep(
name: 'Giá trị sau chiết khấu',
outputKey: 'net_value',
formula: fn ($data) => $data['subtotal'] - $data['discount_amount'],
roundingRule: RoundingRule::UNIT,
dependencies: ['subtotal', 'discount_amount']
));
// Bước 4: VAT (nếu có)
$pipeline->addStep(new CalculationStep(
name: 'Thuế VAT',
outputKey: 'vat_amount',
formula: function ($data) {
$vatRate = (float) ($data['vat_rate'] ?? 0);
return $data['net_value'] * ($vatRate / 100);
},
roundingRule: RoundingRule::UNIT,
dependencies: ['net_value', 'vat_rate']
));
// Bước 5: Tổng thanh toán
$pipeline->addStep(new CalculationStep(
name: 'Tổng thanh toán',
outputKey: 'total_payment',
formula: fn ($data) => $data['net_value'] + $data['vat_amount'],
roundingRule: RoundingRule::UNIT,
dependencies: ['net_value', 'vat_amount']
));
return $pipeline;
}
public static function calculateForContract(Contract $contract, array $overrides = []): CalculationResult
{
$pipeline = self::forContract($contract);
$data = [
'land_value' => (float) $contract->land_value,
'foundation_value' => (float) $contract->foundation_value,
'discount_details' => $contract->discount_details ?? [],
'vat_rate' => (float) ($contract->metadata['vat_rate'] ?? 0),
];
// Áp dụng ghi đè nếu có
if (!empty($overrides)) {
foreach ($pipeline->getSteps() as $step) {
if (isset($overrides[$step->outputKey()])) {
$step->override((int) $overrides[$step->outputKey()]);
}
}
}
return $pipeline->execute($data);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Services\Calculation;
enum RoundingRule: string
{
case NONE = 'none'; // Không làm tròn
case UNIT = 'unit'; // Làm tròn đến đồng (số nguyên)
case THOUSAND = 'thousand'; // Làm tròn đến nghìn
case MILLION = 'million'; // Làm tròn đến triệu
public function apply(float $value): int
{
return match ($this) {
self::NONE => (int) $value,
self::UNIT => (int) round($value),
self::THOUSAND => (int) (round($value / 1000) * 1000),
self::MILLION => (int) (round($value / 1000000) * 1000000),
};
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace App\Services\Forms;
use App\Models\FormTemplate;
use App\Models\FormPrintLog;
class MailMergeService
{
/**
* Evaluate all fields for a given record and return values array.
*/
public static function evaluateFields(FormTemplate $template, Model $record): array
{
$values = [];
foreach ($template->fields as $field) {
$values[$field->code] = self::evaluateSingleField($field, $record, $values);
}
return $values;
}
protected static function evaluateSingleField($field, Model $record, array $evaluatedValues): mixed
{
$config = $field->source_config ?? [];
return match ($field->source_type) {
'db_column' => self::getDbColumnValue($record, $config['column'] ?? null),
'db_relation' => self::getRelationValue($record, $config),
'formula' => self::evaluateFormula($config['expression'] ?? '', $evaluatedValues),
'input' => $config['default'] ?? '',
'static' => $config['value'] ?? '',
default => '',
};
}
protected static function getDbColumnValue(Model $record, ?string $column): mixed
{
if (! $column) return '';
return $record->{$column} ?? '';
}
protected static function getRelationValue(Model $record, array $config): mixed
{
$relation = $config['relation'] ?? null;
$column = $config['column'] ?? null;
$index = $config['index'] ?? null;
if (! $relation || ! $column) return '';
$related = $record->{$relation};
if (is_null($related)) return '';
if ($related instanceof \Illuminate\Database\Eloquent\Collection) {
if ($index !== null) {
$item = $related->skip($index)->first();
return $item?->{$column} ?? '';
}
return $related->pluck($column)->implode(', ');
}
return $related->{$column} ?? '';
}
protected static function evaluateFormula(string $expression, array $values): float
{
if (empty($expression)) return 0;
// Thay thế tên biến bằng giá trị
$evalExpression = $expression;
foreach ($values as $key => $value) {
if (is_numeric($value)) {
$evalExpression = str_replace($key, (float) $value, $evalExpression);
}
}
// Chỉ cho phép số và các phép toán cơ bản
$evalExpression = preg_replace('/[^0-9.\+\-\*\/\(\)\s]/', '', $evalExpression);
if (empty($evalExpression)) return 0;
try {
// Eval an toàn với chỉ phép toán
$result = self::safeEval($evalExpression);
return (float) $result;
} catch (\Throwable $e) {
return 0;
}
}
protected static function safeEval(string $expression): float
{
// Loại bỏ các hàm nguy hiểm, chỉ giữ phép toán
$expression = preg_replace('/[^0-9.\+\-\*\/\(\)\s]/', '', $expression);
if (empty($expression) || preg_match('/[a-zA-Z]/', $expression)) {
throw new \InvalidArgumentException('Invalid expression');
}
// Dùng bc math nếu có, hoặc eval đơn giản
return (float) eval('return ' . $expression . ';');
}
/**
* Format value theo kiểu field.
*/
public static function formatValue(mixed $value, string $format, int $decimals = 0): string
{
return match ($format) {
'number' => number_format((float) $value, $decimals, ',', '.'),
'currency' => number_format((float) $value, 0, ',', '.') . ' VNĐ',
'percent' => number_format((float) $value, $decimals, ',', '.') . '%',
'date' => $value ? \Carbon\Carbon::parse($value)->format('d/m/Y') : '',
default => (string) $value,
};
}
/**
* Render template with evaluated values.
*/
public static function render(FormTemplate $template, Model $record): array
{
$rawValues = self::evaluateFields($template, $record);
$formattedValues = [];
foreach ($template->fields as $field) {
$code = $field->code;
$rawValue = $rawValues[$code] ?? '';
$formattedValues[$code] = self::formatValue(
$rawValue,
$field->format,
$field->decimal_places
);
}
$html = $template->html_template;
foreach ($formattedValues as $code => $value) {
$html = str_replace('{{' . $code . '}}', (string) $value, $html);
}
return [
'html' => $html,
'raw_values' => $rawValues,
'formatted_values' => $formattedValues,
];
}
/**
* Save print log with snapshot.
*/
public static function savePrintLog(FormTemplate $template, Model $record, array $renderResult, int $userId): FormPrintLog
{
return FormPrintLog::create([
'template_id' => $template->id,
'target_model' => get_class($record),
'target_id' => $record->id,
'target_number' => $record->contract_number ?? $record->code ?? null,
'snapshot_data' => [
'raw_values' => $renderResult['raw_values'],
'formatted_values' => $renderResult['formatted_values'],
],
'rendered_html' => $renderResult['html'],
'printed_by' => $userId,
'printed_at' => now(),
]);
}
}