<?php

//add some security check
include('/var/www/cegled/_class/class_sql.php');
$sql = new sql('bcd_mysql','root','','badminton_coach');

//lekérjük az aktív tagokat, végigmegyünk rajtuk

//ha az user lejárós
    //ha be van állítva expire date és az >= actual_date, akkor nullázás
        //nullra állítjuk az expire date-et
    //ha null és (actual date >= last_deposit + 1 hónap VAGY actual date null), akkor nullázás

$users_assoc_array = $sql->assoc_array('SELECT * FROM user_kid WHERE uk_is_active = 1 AND uk_deleted = 0 AND uk_balance_expires = 1;');

foreach ($users_assoc_array as $key => $user_array) {
    $setZero = false;
    $expDateToNull = false;
    if (null !== $user_array['uk_balance_expire_date'] && $user_array['uk_balance_expire_date'] <= date('Y-m-d')) {
        $setZero = true;
        $expDateToNull = true;
    }
    elseif (null === $user_array['uk_balance_expire_date']) {
        var_dump($user_array['uk_id'], null === $user_array['uk_last_deposit']);
        if (null === $user_array['uk_last_deposit']) {
            $setZero = true;
        } else {
            $depositPlusOneMonth = date("Y-m-d", strtotime("+1 month", strtotime($user_array['uk_last_deposit'])));
            if (date('Y-m-d') >= $depositPlusOneMonth) {
                $setZero = true;
            }
        }

    }

    if ($setZero && $user_array['uk_balance'] > 0) {
        var_dump('setZero:' . $user_array['uk_id']);
        $sql->update_table('user_kid', array('uk_balance' => 0), array('uk_id' => $user_array['uk_id']));
    }

    if ($expDateToNull) {
        $sql->update_table('user_kid', array('uk_balance_expire_date' => 'null'), array('uk_id' => $user_array['uk_id']));
    }
}

?>
