
Код: Выделить всё
#если пользователь выбрал тариф
if(isset($_POST['n_tariff_id'])) {
$n_tariff_id=$_POST['n_tariff_id']; //ID нового тарифа
$cur_date = strtotime(date("Y-m-d")); //текущая дата
$td_check = $cur_date-259200; //-30 дней от текущей даты
Код: Выделить всё
$ntsum_query = mysql_query("SELECT cost FROM tariffs_services_link, tariffs, periodic_services_data WHERE tariffs.id=tariffs_services_link.tariff_id AND periodic_services_data.id=tariffs_services_link.service_id AND tariffs.id='$n_tariff_id'") or die ("Не удалось узнать стоимость нового тарифа");
$ntsum_row = mysql_fetch_array($ntsum_query);
if ($balance<$ntsum_row[0]) die ("<br><br><center>На вашем счету недостаточно средств для смены тарифа</center>");
Код: Выделить всё
$ltc_query = mysql_query("SELECT id, change_date FROM wk_tariffs_change WHERE account_id='$account_id' AND change_date BETWEEN $td_check AND $cur_date") or die("Не удается получить дату последней смены тарифа");
if (mysql_num_rows($ltc_query)!=NULL) die("<center><br><br>Вы меняли тариф менее чем 30 дней назад. Смена тарифа невозможна</center>");
Узнаем стоимостт старого тарифа. для компенсации
Код: Выделить всё
$tsum_query = mysql_query("SELECT cost FROM tariffs_services_link, tariffs, periodic_services_data WHERE tariffs.id=tariffs_services_link.tariff_id AND periodic_services_data.id=tariffs_services_link.service_id AND tariffs.id='$tariff_id'") or die ("Не удалось узнать стоимость текущего тарифа");
$tsum_numresults = mysql_num_rows($tsum_query);
for ($i=0; $i <$tsum_numresults; $i++){
$tsum_row = mysql_fetch_array($tsum_query);
$tsum=$tsum+$tsum_row[0];
}
Код: Выделить всё
$start_date = date("Y-m-d"); //текущая дата
$dperiod_query = mysql_query("SELECT id FROM discount_periods WHERE start_date=UNIX_TIMESTAMP('$start_date 06:00:00') AND periodic_type=3 AND is_expired=0 LIMIT 1") or die ("Не удалось выполнить запрос на получение рассчетного периода");
$dperiod_numrows = mysql_num_rows($dperiod_query);
if($dperiod_numrows==NULL) die("Отсутствует подходящий рассчетный период. Смена тарифа невозможна");
$dperiod_row=mysql_fetch_array($dperiod_query);
$dperiod=$dperiod_row[0]; //ID рассчетного периода для нового тарифа
Код: Выделить всё
$ipaddr_query = mysql_query("SELECT sl.user_id, u.login, INET_NTOA(ip & 4294967295) as ip, i.mask FROM ip_groups i, iptraffic_service_links isl, service_links sl, users u WHERE i.is_deleted='0' AND isl.is_deleted='0' AND sl.is_deleted='0' AND i.ip_group_id=isl.ip_group_id AND isl.id=sl.id AND sl.user_id=u.id AND ip_type=1 AND login='$login'");
$ipaddr_numresults = mysql_num_rows ($ipaddr_query);
$ipaddr_row = mysql_fetch_array($ipaddr_query);
$user_ip=$ipaddr_row[2]; //IP адрес пользователя
выключаем пользователю интернет
Код: Выделить всё
exec("$utils_path/utm5_urfaclient -h $utm5corehost -l $utm5coreuser -P $utm5corepass -x $utils_path/xml/ -a edit_account -account_id $account_id -int_status 0");
удаляем старый тарифный план
Код: Выделить всё
exec("$utils_path/utm5_urfaclient -h $utm5corehost -l $utm5coreuser -P $utm5corepass -x $utils_path/xml/ -a unlink_tariff -user_id $id -account_id $account_id -tariff_link_id $tariff_link_id");
#таймаут 10 секунд. на всякий случай
sleep(5);
#присваиваем новый тарифный план
exec("$utils_path/utm5_urfaclient -h $utm5corehost -l $utm5coreuser -P $utm5corepass -x $utils_path/xml/ -a link_tariff_with_services -user_id $id -account_id $account_id -tariff_current $n_tariff_id -discount_period_id $dperiod -ip_address $user_ip -ip_not_vpn 1");
делаем компенсацию
Код: Выделить всё
$days_to_dperiod = $dperiod_end_date_unixtime-$cur_date;
$days_to_dperiod = $days_to_dperiod/86400; //считаем сколько осталось дней до конца расчетного периода
$days_to_dperiod = round($days_to_dperiod,0); //округляем количество дней
$compday=$tsum/30; //стоимость 1-го дня старого тарифа
$compensation=$compday*$days_to_dperiod;
$compensation=round($compensation,0);
exec("$utils_path/utm5_urfaclient -h $utm5corehost -l $utm5coreuser -P $utm5corepass -x $utils_path/xml/ -a add_payment -account_id $account_id -payment $compensation -payment_method 104 -comment \"Tariff change compensation\"");
включаем пользователю интернет
Код: Выделить всё
exec("$utils_path/utm5_urfaclient -h $utm5corehost -l $utm5coreuser -P $utm5corepass -x $utils_path/xml/ -a edit_account -account_id $account_id -int_status 1");
вставляем запись о смене тарифа
Код: Выделить всё
mysql_query("INSERT INTO wk_tariffs_change (account_id, change_date, tariff_id) VALUES ('$account_id', '$cur_date', '$n_tariff_id')") or die ("Не удается сделать запись о смене тарифа");
print("<br><br><center>Тариф успешно сменен</center>");