$insurance_update = mysqli_query($CONNECT, "UPDATE `insurance` SET `to_accrual` = '$calc_insurance' WHERE `users` = '$insurance_user_update' AND `hash` = '$data_ID'");
mysqli_query($CONNECT, "UPDATE `users` SET `balance` = `balance` + '$calc_insurance' WHERE `login` = '$insurance_user_update'");
if( isset($data['management_a_scam']) ) {
$data_id = $data['management_a_id'];
$data_url = $data['management_a_url'];
$data_url = $data['management_a_url'];
$user_input_query = mysqli_query($CONNECT, "SELECT `users` AS name_user, sum(`vklad`) AS sum_vklad, sum(TIMESTAMPDIFF(DAY, `date`, CURDATE())) AS `sum_date_diff` FROM `refback` WHERE `moderation` = 'off' AND `status` = 'on' AND `url` = '$data_url' GROUP BY `users`");
foreach($user_input as $massiv) {
$derived_from_game = (($massiv['sum_vklad']*29/30)/100)*$massiv['sum_date_diff']; #Подсчитываем вывод с игры пользователем за N-ый промежуток времени
$amount_loss_user = $massiv['sum_vklad']-$derived_from_game; #Подсчитываем сумму убытока пользователя
if ($amount_loss_user > 0) {
$insurance = R::dispense('insurance');
$insurance->games = $data['management_a_name'];
$insurance->hash = $data['management_a_id'];
$insurance->users = $massiv['name_user'];
$insurance->loss = number_format($amount_loss_user, 2, '.', '');
$insurance->to_accrual = number_format(0, 2, '.', '');
R::store($insurance);
$data_ID = $data['management_a_id'];
$insurance_total_summ_query = mysqli_query($CONNECT, "SELECT sum(`loss`) FROM `insurance` WHERE `hash` = '$data_ID'");
$insurance_total_summ = mysqli_fetch_array($insurance_total_summ_query);
$calc_insurance = ($data['management_a_insurance']/$insurance_total_summ[0] )*$amount_loss_user;
$insurance_user_update = $massiv['name_user'];
$insurance_update = mysqli_query($CONNECT, "UPDATE `insurance` SET `to_accrual` = '$calc_insurance' WHERE `users` = '$insurance_user_update' AND `hash` = '$data_ID'");
mysqli_query($CONNECT, "UPDATE `users` SET `balance` = `balance` + '$calc_insurance' WHERE `login` = '$insurance_user_update'");
}
}
$other = mysqli_query($CONNECT, "UPDATE `games` SET `scam` = 'on' WHERE `id`='$data_id'");
mysqli_query($CONNECT, "UPDATE `refback` SET `status` = 'off' WHERE `url`='$data_url'");
}