109 lines
5.0 KiB
PHP
Executable File
109 lines
5.0 KiB
PHP
Executable File
<?php
|
|
|
|
// دالة لحساب السعر الأساسي بدون خصم
|
|
function calculateBasePrice($totalPrice, $taxRate) {
|
|
return $totalPrice / (1 + $taxRate / 100);
|
|
}
|
|
|
|
// دالة لحساب مبلغ الخصم بناءً على النسبة المئوية
|
|
function calculatePercentageDiscount($basePrice, $discountRate) {
|
|
return $basePrice * ($discountRate / 100);
|
|
}
|
|
|
|
// دالة لحساب مبلغ الخصم بناءً على عدد النقاط
|
|
function calculatePointsDiscount($points, $discountPerPoint) {
|
|
return $points * $discountPerPoint;
|
|
}
|
|
|
|
// دالة لحساب مبلغ الضريبة على السعر بعد الخصم
|
|
function calculateTaxAmount($basePriceAfterDiscount, $taxRate) {
|
|
return $basePriceAfterDiscount * ($taxRate / 100);
|
|
}
|
|
|
|
// دالة لحساب السعر بعد تطبيق الخصومات وقبل الضريبة
|
|
function calculatePriceAfterDiscounts($basePrice, $percentageDiscount, $fixedDiscount, $pointsDiscount) {
|
|
$totalDiscount = $percentageDiscount + $fixedDiscount + $pointsDiscount;
|
|
return $basePrice - $totalDiscount;
|
|
}
|
|
|
|
// دالة رئيسية تجمع كل العمليات السابقة
|
|
function calculatePriceWithAllDiscounts(
|
|
$totalPrice, $taxRate = 15.0, $discountRate = 0, $discountAmount = 0,
|
|
$points = 0, $discountPerPoint = 0, $useFixedPrice = false, $fixedTotalPrice = 0
|
|
) {
|
|
// حساب السعر الأساسي بدون خصم
|
|
$basePrice = calculateBasePrice($totalPrice, $taxRate);
|
|
|
|
// حساب الخصومات
|
|
$percentageDiscount = calculatePercentageDiscount($basePrice, $discountRate);
|
|
$pointsDiscount = calculatePointsDiscount($points, $discountPerPoint);
|
|
|
|
// إذا كان نظام تثبيت السعر نشطًا، نحسب الخصم بناءً على السعر الثابت
|
|
$fixedPriceDiscount = 0;
|
|
if ($useFixedPrice) {
|
|
// حساب السعر الأساسي الذي يجب أن يكون بعد الخصم قبل الضريبة ليكون السعر النهائي هو السعر الثابت
|
|
$basePriceForFixedPrice = $fixedTotalPrice / (1 + $taxRate / 100);
|
|
|
|
// حساب الخصم الثابت المطلوب للوصول إلى السعر الثابت
|
|
$fixedPriceDiscount = $basePrice - $basePriceForFixedPrice;
|
|
|
|
// التأكد من أن الخصم الثابت لا يتجاوز السعر الأساسي
|
|
if ($fixedPriceDiscount > $basePrice) {
|
|
$fixedPriceDiscount = $basePrice;
|
|
}
|
|
}
|
|
|
|
// حساب السعر بعد تطبيق الخصومات وقبل الضريبة
|
|
$priceAfterDiscounts = calculatePriceAfterDiscounts($basePrice, $percentageDiscount, $discountAmount, $pointsDiscount + $fixedPriceDiscount);
|
|
|
|
// التأكد من أن السعر بعد الخصم لا يكون سالب
|
|
if ($priceAfterDiscounts < 0) {
|
|
$priceAfterDiscounts = 0;
|
|
}
|
|
|
|
// حساب مبلغ الضريبة على السعر بعد الخصم
|
|
$taxAmount = calculateTaxAmount($priceAfterDiscounts, $taxRate);
|
|
|
|
// حساب السعر النهائي بعد الخصم والضريبة
|
|
$finalPrice = $priceAfterDiscounts + $taxAmount;
|
|
|
|
// التقريب لأقرب رقمين بعد الفاصلة العشرية
|
|
$basePrice = round($basePrice, 2);
|
|
$percentageDiscount = round($percentageDiscount, 2);
|
|
$pointsDiscount = round($pointsDiscount, 2);
|
|
$fixedPriceDiscount = round($fixedPriceDiscount, 2);
|
|
$priceAfterDiscounts = round($priceAfterDiscounts, 2);
|
|
$taxAmount = round($taxAmount, 2);
|
|
$finalPrice = round($finalPrice, 2);
|
|
|
|
return [
|
|
'base_price' => $basePrice,
|
|
'percentage_discount' => $percentageDiscount,
|
|
'points_discount' => $pointsDiscount,
|
|
'fixed_price_discount' => $fixedPriceDiscount,
|
|
'price_after_discounts' => $priceAfterDiscounts,
|
|
'tax_amount' => $taxAmount,
|
|
'final_price' => $finalPrice
|
|
];
|
|
}
|
|
|
|
// مثال للاستخدام
|
|
$totalPrice = 172.5;
|
|
$discountRate = 0; // نسبة الخصم 0%
|
|
$discountAmount = 0; // إذا كان الخصم نسبة، يتم تجاهل مبلغ الخصم
|
|
$points = 0; // عدد النقاط المتاحة
|
|
$discountPerPoint = 0; // قيمة الخصم لكل نقطة
|
|
$useFixedPrice = true; // تفعيل نظام تثبيت السعر
|
|
$fixedTotalPrice = 115.00; // السعر الثابت المراد تطبيقه
|
|
|
|
$result = calculatePriceWithAllDiscounts($totalPrice, 15.0, $discountRate, $discountAmount, $points, $discountPerPoint, $useFixedPrice, $fixedTotalPrice);
|
|
|
|
echo "السعر الأساسي: " . $result['base_price'] . "\n";
|
|
echo "الخصم بالنسبة المئوية: " . $result['percentage_discount'] . "\n";
|
|
echo "خصم النقاط: " . $result['points_discount'] . "\n";
|
|
echo "خصم السعر الثابت: " . $result['fixed_price_discount'] . "\n";
|
|
echo "السعر بعد الخصم: " . $result['price_after_discounts'] . "\n";
|
|
echo "مبلغ الضريبة: " . $result['tax_amount'] . "\n";
|
|
echo "السعر النهائي بعد الخصم والضريبة: " . $result['final_price'] . "\n";
|
|
?>
|