Hoan thien core finance v2 - Calculation Pipeline, Form Templates
This commit is contained in:
@@ -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
23
app/Models/FormField.php
Normal 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);
|
||||
}
|
||||
}
|
||||
29
app/Models/FormPrintLog.php
Normal file
29
app/Models/FormPrintLog.php
Normal 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');
|
||||
}
|
||||
}
|
||||
28
app/Models/FormTemplate.php
Normal file
28
app/Models/FormTemplate.php
Normal 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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user