Joywork/ajax/installment_calculation.php

96 lines
3.1 KiB
PHP
Raw Normal View History

2026-05-22 20:21:54 +02:00
<?php
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
$apartment_id = intval($_GET['apartment_id']);
$user_id = intval($_SESSION['id']);
$calculation_info = $_POST['calculation_info'];
$catalog_id = $_POST['catalog_id'];
function round2($num) {
return round($num, 2);
}
$P = floatval($calculation_info['P']);
$I = floatval($calculation_info['I']);
$S = floatval($calculation_info['S']);
$T = intval($calculation_info['T']);
$L = intval($calculation_info['L']);
$type_payment = isset($calculation_info['type_payment']) ? $calculation_info['type_payment'] : 'equal_payments';
$R = $P - $I - $S;
$result = array(
'type' => '',
'R' => round2($R),
'A' => 0,
'A_o' => null,
'details' => array()
);
if ($T % $L === 0) {
$N = $T / $L;
$result['type'] = 'equal_no_remainder';
$result['A'] = round2($R / $N);
$result['details'] = array('N' => $N);
} else {
$fullPeriods = floor($T / $L);
$remainder = $T % $L;
if ($type_payment === 'equal_payments') {
$N = $fullPeriods + 1;
$result['type'] = 'equal_with_remainder';
$result['A'] = round2($R / $N);
$result['details'] = array(
'fullPeriods' => $fullPeriods,
'remainder' => $remainder,
'N' => $N
);
} else {
$Am = $R / $T;
$result['type'] = 'decreasing';
$result['A'] = round2($Am * $L);
$result['A_o'] = round2($Am * $remainder);
$result['details'] = array(
'fullPeriods' => $fullPeriods,
'remainder' => $remainder,
'T' => $T
);
}
}
$compare = array(
'R' => array('frontend' => floatval($calculation_info['R']), 'backend' => $result['R']),
'A' => array('frontend' => floatval($calculation_info['A']), 'backend' => $result['A']),
'A_o' => array('frontend' => isset($calculation_info['A_o']) ? floatval($calculation_info['A_o']) : null, 'backend' => $result['A_o'])
);
foreach ($compare as $key => $val) {
echo "$key: frontend = {$val['frontend']}, backend = {$val['backend']}";
if ($val['frontend'] == $val['backend']) {
echo " ✅ OK";
} else {
echo "НЕ СОВПАДАЕТ";
}
echo "\n";
}
foreach (array('R', 'A', 'A_o') as $key) {
$calculation_info[$key] = isset($result[$key]) ? $result[$key] : null;
}
if ($apartment_id && $user_id && is_array($calculation_info)) {
$jsonData = mysql_real_escape_string(json_encode($calculation_info, JSON_UNESCAPED_UNICODE));
$new_installment_price = isset($calculation_info['P']) ? floatval($calculation_info['P']) : 0;
$sql_check = "SELECT id FROM sended_pdf_complexes WHERE id_agent = '$user_id' AND id_object = '$apartment_id' AND sent_catalog_id = '$catalog_id'";
$res_check = mysql_query($sql_check);
if (mysql_num_rows($res_check) > 0) {
$sql_update = "UPDATE sended_pdf_complexes
SET calculation_data = '$jsonData', new_installment_price = '$new_installment_price'
WHERE id_agent = '$user_id' AND id_object = '$apartment_id' AND sent_catalog_id = '$catalog_id' ";
mysql_query($sql_update);
}
}
?>