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

@@ -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),
};
}
}