Hoan thien core finance v2
This commit is contained in:
40
app/Services/DiscountEngine.php
Normal file
40
app/Services/DiscountEngine.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class DiscountEngine
|
||||
{
|
||||
/**
|
||||
* Tính tổng chiết khấu và giá trị sau chiết khấu.
|
||||
*
|
||||
* @param float $totalValue Giá trị gốc
|
||||
* @param array|null $discountDetails Dữ liệu chiết khấu từ contract
|
||||
* @return array ['discount_amount' => float, 'final_value' => float]
|
||||
*/
|
||||
public static function calculate(float $totalValue, ?array $discountDetails): array
|
||||
{
|
||||
if (empty($discountDetails)) {
|
||||
return [
|
||||
'discount_amount' => 0,
|
||||
'final_value' => $totalValue,
|
||||
];
|
||||
}
|
||||
|
||||
$discountAmount = 0;
|
||||
|
||||
// Ưu tiên total_amount nếu có
|
||||
if (! empty($discountDetails['total_amount'])) {
|
||||
$discountAmount = (float) $discountDetails['total_amount'];
|
||||
} elseif (! empty($discountDetails['total_percentage'])) {
|
||||
$discountAmount = $totalValue * ((float) $discountDetails['total_percentage'] / 100);
|
||||
}
|
||||
|
||||
// Đảm bảo chiết khấu không vượt quá giá trị hợp đồng
|
||||
$discountAmount = min($discountAmount, $totalValue);
|
||||
|
||||
return [
|
||||
'discount_amount' => $discountAmount,
|
||||
'final_value' => $totalValue - $discountAmount,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user