0, 'insrt' => 0 ]; foreach ($data as $key => $value) { $total = 0; if (count($data[$key]) > 1) { foreach ($data[$key] as $key2 => $value2) { $total += $data[$key][$key2]['pay']; } } else { $total += $data[$key][0]['pay']; } $word_tag = 'I' . ''; switch ($data[$key][0]['branch_id']) { case '2': $word_tag = '1501'; break; case '3': $word_tag = '0701'; break; case '4': $word_tag = '2901'; break; case '5': $word_tag = '0602'; break; case '6': $word_tag = 'KM01'; break; case '7': $word_tag = 'VS01'; break; case '8': $word_tag = 'VS02'; break; case '9': $word_tag = 'PT07'; break; case '12': $word_tag = '2902'; break; case '13': $word_tag = '1502'; break; default: $err['select'] += 1; break; } $date_time = $data[$key][0]['date_time']; $id = $data[$key][0]['id']; $number += 1; $number_style = date('y', strtotime($date_time)) . $word_tag . str_pad($number, 7, '0', STR_PAD_LEFT); $tretment_id = $data[$key][0]['tretment_id']; $price = $total; $branch_id = $data[$key][0]['branch_id']; // if ( // !mysqli_query( // $db, // "INSERT INTO `VAT_report_2024_I`(`pay`, `number`, `number_style`, `invoices_id`, `branch_id`, `date_time`, `user_id`, `activation_id`) // VALUE ('$price','$number','$number_style','$id','$branch_id','$date_time','1','2') // " // ) // ) { // $err['insrt'] += 1; // } } print_r($err); print_r($data); /** * دالة لحساب الخصم المطلوب لتحقيق السعر الثابت بعد الضريبة. * * @param float $totalPrice السعر الشامل للضريبة. * @param float $taxRate نسبة ضريبة القيمة المضافة. * @param float $fixedTotalPrice السعر الثابت المطلوب بعد الضريبة. * @return array مصفوفة تحتوي على السعر الأساسي، الخصم المطلوب، السعر بعد الخصم، مبلغ الضريبة، والسعر النهائي. */ function calculateFixedPriceDiscount($totalPrice, $taxRate, $fixedTotalPrice) { // حساب السعر الأساسي من السعر الشامل للضريبة $basePrice = $totalPrice / (1 + $taxRate / 100); // حساب السعر الأساسي الذي يجب أن يكون بعد الخصم ليكون السعر النهائي هو السعر الثابت $basePriceForFixedPrice = $fixedTotalPrice / (1 + $taxRate / 100); // حساب الخصم المطلوب للوصول إلى السعر الثابت $fixedPriceDiscount = $basePrice - $basePriceForFixedPrice; // التأكد من أن الخصم الثابت لا يتجاوز السعر الأساسي if ($fixedPriceDiscount > $basePrice) { $fixedPriceDiscount = $basePrice; } // حساب السعر بعد تطبيق الخصم $priceAfterDiscount = $basePrice - $fixedPriceDiscount; // حساب الضريبة على السعر بعد الخصم $taxAmount = $priceAfterDiscount * ($taxRate / 100); // حساب السعر النهائي بعد الخصم والضريبة $finalPrice = $priceAfterDiscount + $taxAmount; // التقريب لأقرب رقمين بعد الفاصلة العشرية $basePrice = round($basePrice, 2); $fixedPriceDiscount = round($fixedPriceDiscount, 2); $priceAfterDiscount = round($priceAfterDiscount, 2); $taxAmount = round($taxAmount, 2); $finalPrice = round($finalPrice, 2); return [ 'base_price' => $basePrice, 'fixed_price_discount' => $fixedPriceDiscount, 'price_after_discount' => $priceAfterDiscount, 'tax_amount' => $taxAmount, 'final_price' => $finalPrice ]; } // مثال للاستخدام $totalPrice = 172.5; // السعر الشامل للضريبة $taxRate = 15.0; // نسبة الضريبة $fixedTotalPrice = 115.0; // السعر الثابت المطلوب بعد الضريبة // $result = calculateFixedPriceDiscount($totalPrice, $taxRate, $fixedTotalPrice); // echo "السعر الأساسي: " . $result['base_price'] . "\n"; // echo "الخصم المطلوب لتحقيق السعر الثابت: " . $result['fixed_price_discount'] . "\n"; // echo "السعر بعد الخصم: " . $result['price_after_discount'] . "\n"; // echo "مبلغ الضريبة: " . $result['tax_amount'] . "\n"; // echo "السعر النهائي بعد الخصم والضريبة: " . $result['final_price'] . "\n"; ?>