<?php
/*
    USER_KID osztály
    GYEREKEK osztálya, a USER_PARENT osztályból öröklődik
*/
class user_kid extends user_parent {
    private $user_id;
    private $user_name;
    private $user_is_active;
    private $user_balance;
    private $user_balance_expires;
    private $user_balance_expire_date;
    private $user_last_deposit;
    private $user_last_name;
    private $user_first_name;
    private $user_password;
    private $user_email;
    private $user_last_login;
    private $user_gender;
    private $user_address_scc_id; //SCC ID
    private $user_address; //SCC OBJ
    private $user_birth_date;
    private $user_birth_year;
    private $user_first_training;
    private $user_beforehand;
    private $user_hand;
    private $user_level;
    private $user_last_modified;
    private $logged_in;
    private $user_type;
    private $user_shirt_size;
    private $user_shirt_note;
    private $user_school_sc_id; //SCHOOL ID
    private $user_school; //SCHOOL OBJ
    private $user_parent_1;
    private $user_parent_2;
    private $user_phone;
    private $user_facebook;
    private $user_region;
    private $user_age_category;
    private $user_official_age_category;
    private $user_contact;
    private $user_other;
    private $user_balance_transfer;
    private $user_notify_name;
    private $user_notify_email;
    private $user_last_notification;
    private $user_credit;
    private $user_lease;
    private $user_deleted;

    public function set_uk_id($_uid) {
        $this->user_id = $_uid;
    }
    public function set_uk_name($_uname) {
        $this->user_name = $_uname;
    }
    public function set_uk_is_active($_active) {
        $this->user_is_active = $_active;
    }
    public function set_uk_password($_u_pass) {
        $this->user_password = $_u_pass;
    }
    public function set_uk_email($_u_email) {
        $this->user_email = $_u_email;
    }
    public function set_uk_phone($_u_phone) {
        $this->user_phone = $_u_phone;
    }
    public function set_uk_facebook($_facebook) {
        $this->user_facebook = $_facebook;
    }
    public function set_uk_last_login($_u_last_login) {
        $this->user_last_login = $_u_last_login;
    }
    public function set_uk_shirt_size_ss_id($_shirt_size) {
        $this->user_shirt_size = $_shirt_size;
    }
    public function set_uk_shirt_note($_shirt_note) {
        $this->user_shirt_note = $_shirt_note;
    }
    public function set_uk_school_sc_id($_school) {
        $this->user_school_sc_id = $_school;
    }
    public function set_uk_parent_1($_uk_parent_1) {
        $this->user_parent_1 = $_uk_parent_1;
    }
    public function set_uk_parent_2($_uk_parent_2) {
        $this->user_parent_2 = $_uk_parent_2;
    }
    public function set_uk_region_reg_id($_uk_region) {
        $this->user_region = $_uk_region;
    }
    public function set_uk_contact($_uk_contact) {
        $this->user_contact = $_uk_contact;
    }
    public function set_uk_other($_uk_other) {
        $this->user_other = $_uk_other;
    }
    public function set_uk_deleted($_uk_deleted) {
        $this->user_deleted = $_uk_deleted;
    }

    public function get_uk_id() {
        return $this->user_id;
    }
    public function get_uk_name() {
        return $this->user_name;
    }
    public function get_uk_last_name() {
        return $this->user_last_name;
    }
    public function get_uk_first_name() {
        return $this->user_first_name;
    }
    public function get_uk_password() {
        return $this->user_password;
    }
    public function get_uk_email() {
        return $this->user_email;
    }
    public function get_uk_phone() {
        return $this->user_phone;
    }
    public function get_uk_facebook() {
        return $this->user_facebook;
    }
    public function get_uk_shirt_size() {
        return $this->user_shirt_size;
    }
    public function get_uk_shirt_size_name() {
        global $sql;
        if ($this->user_shirt_size) return $sql->single_variable("select shirt_name from shirt where shirt_id = " . $this->user_shirt_size);
        else return null;
    }

    public function get_uk_shirt_note() {
        return $this->user_shirt_note;
    }
    public function get_uk_school_sc_id() {
        return $this->user_school_sc_id;
    }
    public function get_uk_school_name() {
        global $sql;
        if ($this->user_school_sc_id) return $sql->single_variable('select sc_name from school where sc_id = ' . $this->user_school_sc_id);
        else return null;
    }
    public function get_uk_school_city() {
        global $sql;
        if ($this->user_school_city_scc_id) return $sql->single_variable('select scc_city from school_city where scc_id = ' . $this->user_school_city_scc_id);
        else return null;
    }
    public function get_uk_parent_1() {
        return $this->user_parent_1;
    }
    public function get_uk_parent_2() {
        return $this->user_parent_2;
    }
    public function get_uk_region_reg_id() {
        return $this->user_region;
    }
    public function get_uk_region_name() {
        global $sql;
        if ($this->user_region) return $sql->single_variable('select reg_name from region where reg_id = ' . $this->user_region);
        else return null;
    }
    public function get_uk_contact() {
        return $this->user_contact;
    }
    public function get_uk_other() {
        return $this->user_other;
    }
    public function get_uk_deleted() {
        return $this->user_deleted;
    }


    public function get_uk_presence($_training_id) {
        global $sql;
        return $sql->num_of_rows('select * from presence where pr_user_kid_uk_id = ' . $this->get_uk_id() . ' AND pr_training_tr_id = ' . $_training_id);
    }

    public function get_uk_presence_on_previous_trainings($_trainigs) {
        //az előző edzések ID-ját kapja paraméterül
        //megadja, hogy hányszor volt a gyerek
        global $sql;

        return $sql->single_variable("SELECT count(*) FROM presence WHERE pr_training_tr_id IN (" . implode(',', $_trainigs) . ") AND pr_user_kid_uk_id = " . $this->get_uk_id() . ";");
    }

    public function set_uk_gender($_gender) {
        $this->user_gender = $_gender;
    }
    public function get_uk_gender() {
        return $this->user_gender;
    }
    public function set_uk_birth_date($_birth_date) {
        $this->user_birth_date = $_birth_date;
    }
    public function get_uk_birth_date() {
        return $this->user_birth_date;
    }
    public function set_uk_birth_year($_birth_year) {
        $this->user_birth_year = $_birth_year;
    }
    public function get_uk_birth_year() {
        return $this->user_birth_year;
    }
    public function set_uk_first_training($_first_training) {
        $this->user_first_training = $_first_training;
    }
    public function get_uk_first_training() {
        return $this->user_first_training;
    }
    public function set_uk_hand($_hand) {
        $this->user_hand = $_hand;
    }
    public function get_uk_hand() {
        return $this->user_hand;
    }
    public function set_uk_level($_level) {
        $this->user_level = $_level;
    }
    public function get_uk_level() {
        return $this->user_level;
    }
    public function set_uk_last_modified($_last_modified) {
        $this->user_last_modified = $_last_modified;
    }
    public function get_uk_last_modified() {
        return $this->user_last_modified;
    }
    public function set_uk_address_scc_id($_address) {
        $this->user_address_scc_id = $_address;
    }
    public function get_uk_address_scc_id() {
        return $this->user_address_scc_id;
    }
    public function set_uk_address($_address) {
        $this->user_address = $_address;
    }
    public function get_uk_address() {
        return $this->user_address;
    }
    public function set_uk_beforehand($_beforehand) {
        $this->user_beforehand = $_beforehand;
    }
    public function get_uk_beforehand() {
        return $this->user_beforehand;
    }
    public function set_uk_age_category($_age_category) {
        $this->user_age_category = $_age_category;
    }
    public function get_uk_age_category() {
        return $this->user_age_category;
    }
    public function set_uk_official_age_category($_age_category) {
        $this->user_official_age_category = $_age_category;
    }
    public function get_uk_official_age_category() {
        return $this->user_official_age_category;
    }
    public function set_uk_school($_school) {
        $this->user_school = $_school;
    }
    public function get_uk_school() {
        return $this->user_school;
    }
    public function set_uk_notify_name($_notify_name) {
        $this->user_notify_name = $_notify_name;
    }
    public function get_uk_notify_name() {
        return $this->user_notify_name;
    }
    public function set_uk_notify_email($_notify_email) {
        $this->user_notify_email = $_notify_email;
    }
    public function get_uk_notify_email() {
        return $this->user_notify_email;
    }
    public function set_uk_last_notification($_last_notification) {
        $this->user_last_notification = $_last_notification;
    }
    public function get_uk_last_notification() {
        return $this->user_last_notification;
    }
    public function set_uk_balance($_balance) {
        $this->user_balance = $_balance;
    }
    public function get_uk_balance() {
        return $this->user_balance;
    }
    public function set_uk_balance_expires($_balance_expires) {
        $this->user_balance_expires = $_balance_expires;
    }
    public function get_uk_balance_expires() {
        return $this->user_balance_expires;
    }
    public function set_uk_balance_expire_date($_balance_expire_date) {
        $this->user_balance_expire_date = $_balance_expire_date;
    }
    public function get_uk_balance_expire_date() {
        return $this->user_balance_expire_date;
    }
    public function set_uk_last_deposit($_last_deposit) {
        $this->user_last_deposit = $_last_deposit;
    }
    public function get_uk_last_deposit() {
        return $this->user_last_deposit;
    }
    public function set_uk_balance_transfer($_balance_transfer) {
        $this->user_balance_transfer = $_balance_transfer;
    }
    public function get_uk_balance_transfer() {
        return $this->user_balance_transfer;
    }
    public function set_uk_credit($_credit) {
        $this->user_credit = $_credit;
    }
    public function get_uk_credit() {
        return $this->user_credit;
    }
    public function set_uk_lease($_lease) {
        $this->user_lease = $_lease;
    }
    public function get_uk_lease() {
        return $this->user_lease;
    }
    public function is_logged_in() {
        //leellenőrzi cookie alapján h be vagyunk-e jelentkezve
        //JAVÍTVA: adja vissza az adattag igazságértékét
        return $this->logged_in;
    }
    public function set_login($_login) {
        //bool-t kap paraméterül
        $this->logged_in = $_login;
    }

    public function get_training_number_in_month($_year, $_month) {
        //évet és hónapot kap paraméterül, az edzések számát adja vissza
        global $sql;
        //var_dump($_date);
        return $sql->single_variable(
            "SELECT count( DISTINCT pr_id )
            FROM `presence`
            JOIN `training` ON `tr_id` = `pr_training_tr_id`
            WHERE `pr_user_kid_uk_id` = ". $this->get_uk_id()."
            AND `tr_date` LIKE '".$_year."-".$_month."%'
            AND tr_deleted = 0
            AND tr_locked = 1
            ;"
        );
    }

    public function update_login_time($_uk_id = null) {
        global $sql;
        //az adott user_id-n updateli a login_time-ot
        $sql->update_table('user_kid', array('uk_last_login' => date('Y-m-d')), array('uk_id' => (empty($_uk_id)?$this->get_uk_id():$_uk_id)));
    }

    public function set_user_data_by_id($_uk_id) {
        global $sql, $user;
        $user_data_assoc_array = $sql->assoc_array("select * from user_kid where uk_id = " . $_uk_id);
        $user_data_array = $user_data_assoc_array[0];
        foreach ($user_data_array as $field => $value) {
            $function_name = "set_" . $field;
            $this->$function_name($value); //alapadatok beállítása
            if ($field == 'uk_school_sc_id' && !empty($value)) {
                $school = new school();
                $school->set_school_data_by_id($value);
                $this->set_uk_school($school);
            }
            if ($field == 'uk_address_scc_id' && !empty($value)) {
                $city = new school_city();
                $city->set_school_city_data_by_id($value);
                $this->set_uk_address($city);
            }
            //$this->set_ua_type(2); //kid típus beállítása
        }
        $this->set_login(true);
    }
    public static function add_new_parent($_parent_name, $_email, $_facebook, $_phone) {
        global $sql;
        //beilleszti AB-ba
        //visszaadja az ID-t

        if ($_email == '') $_email = 'null';
        if ($_facebook == '') $_facebook = 'null';
        if ($_phone == '') $_phone = 'null';

        return $sql->insert_into('user_parent',
            array(
                'up_name' => $_parent_name,
                'up_email' => $_email,
                'up_facebook' => $_facebook,
                'up_phone' => $_phone
                )
        );
    }
    public static function create_user($_user_value_array) {
        global $sql;
        //SCHOOL_HANDLER
        if (isset($_user_value_array['add_school']) && $_user_value_array['add_school'] != "") {
            //megnézzük adott-e az iskolához települést, ha nem, akkor null-ra állítjuk
            //if (isset($_user_value_array['uk_school_city_scc_id']) && $_user_value_array['uk_school_city_scc_id'] == '') $_user_value_array['uk_school_city_scc_id'] = 'null';
            //$new_school_id = $sql->insert_into('school', array('sc_name' => $_user_value_array['add_school'], 'sc_school_city_scc_id' => $_user_value_array['uk_school_city_scc_id']));
            $new_school_id = school::create_school($_user_value_array['add_school'], $_user_value_array['uk_school_city_scc_id']);
            log::register('new_school', $new_school_id);
            $_user_value_array['uk_school_sc_id'] = $new_school_id;
        }
        unset($_user_value_array['add_school']);
        unset($_user_value_array['uk_school_city_scc_id']);
        //PARENT_1 HANDLER
        if (isset($_user_value_array['add_parent_1']) && $_user_value_array['add_parent_1'] != "") {
            //lehet, hogy csak a neve van megadva, ezért meg kell vizsgálni, hogy a többi létezik-e; ha nem => null
            //var_dump($_user_value_array);
            //die("hiba");
            $_user_value_array['uk_parent_1'] = self::add_new_parent(
                $_user_value_array['add_parent_1'],
                $_user_value_array['parent_1_email'],
                $_user_value_array['parent_1_facebook'], $_user_value_array['parent_1_phone']);
            log::register('new_parent', $_user_value_array['uk_parent_1']);
        }
        if (isset($_user_value_array['add_parent_1'])) unset($_user_value_array['add_parent_1']);
        if (isset($_user_value_array['parent_1_email'])) unset($_user_value_array['parent_1_email']);
        if (isset($_user_value_array['parent_1_facebook'])) unset($_user_value_array['parent_1_facebook']);
        if (isset($_user_value_array['parent_1_phone'])) unset($_user_value_array['parent_1_phone']);
        //PARENT_2 HANDLER
        if (isset($_user_value_array['add_parent_2']) && $_user_value_array['add_parent_2'] != "") {
            $_user_value_array['uk_parent_2'] = self::add_new_parent(
                $_user_value_array['add_parent_2'],
                $_user_value_array['parent_2_email'], $_user_value_array['parent_2_facebook'],
                $_user_value_array['parent_2_phone']);
            log::register('new_parent', $_user_value_array['uk_parent_2']);
        }
        if (isset($_user_value_array['add_parent_2'])) unset($_user_value_array['add_parent_2']);
        if (isset($_user_value_array['parent_2_email'])) unset($_user_value_array['parent_2_email']);
        if (isset($_user_value_array['parent_2_facebook'])) unset($_user_value_array['parent_2_facebook']);
        if (isset($_user_value_array['parent_2_phone'])) unset($_user_value_array['parent_2_phone']);

        //date handler
        if (!isset($_user_value_array['uk_first_training']) || $_user_value_array['uk_first_training'] == "") {
            $_user_value_array['uk_first_training'] = 'null';
        }
        if (!isset($_user_value_array['uk_birth_date']) || $_user_value_array['uk_birth_date'] == "") {
            $_user_value_array['uk_birth_date'] = 'null';
        }
        if (!isset($_user_value_array['uk_birth_year']) || $_user_value_array['uk_birth_year'] == "") {
            $_user_value_array['uk_birth_year'] = 'null';
        }

        foreach ($_user_value_array as $index => $value) {
            if ($value == "") $_user_value_array[$index] = 'null';
        }

        if (!isset($_user_value_array['uk_is_active'])) $_user_value_array['uk_is_active'] = 0;
        if (!isset($_user_value_array['uk_lease'])) $_user_value_array['uk_lease'] = 0;

        $trt_ids = array();
        if (isset($_user_value_array['level']) && is_array($_user_value_array['level'])) {
            foreach ($_user_value_array['level'] as $key => $trt_id) {
                $trt_ids[] = $trt_id;
            }
            unset($_user_value_array['level']);
        }

        $kidId = $sql->insert_into('user_kid', $_user_value_array);

        if (!empty($trt_ids)) {
            foreach ($trt_ids as $key => $trt_id) {
                $sql->insert_into('user_kid_training_type', array(
                        'uktt_user_kid_uk_id' => $kidId,
                        'uktt_training_type_tt_id' => $trt_id,
                    )
                );
            }
        }

        return $kidId;
    }

    public static function update_user($_user_value_array, $_user_id) {
        global $sql;
        //a módosítás dátumát updateljük
        $_user_value_array['uk_last_modified'] = date("Y-m-d");
        //PARENT_1 HANDLER
        if (isset($_user_value_array['add_parent_1']) && $_user_value_array['add_parent_1'] != "") {
            $_user_value_array['uk_parent_1'] = self::add_new_parent(
                $_user_value_array['add_parent_1'],
                $_user_value_array['parent_1_email'],
                $_user_value_array['parent_1_facebook'], $_user_value_array['parent_1_phone']);
            log::register('new_parent', $_user_value_array['uk_parent_1']);
        }
        else {
            //meglévő szülő updatelése
            user_parent::update_parent(array(
                'up_email' => $_user_value_array['parent_1_email'],
                'up_facebook' => $_user_value_array['parent_1_facebook'],
                'up_phone' => $_user_value_array['parent_1_phone']), $_user_value_array['uk_parent_1']);

        }
        if (isset($_user_value_array['add_parent_1'])) unset($_user_value_array['add_parent_1']);
        if (isset($_user_value_array['parent_1_email'])) unset($_user_value_array['parent_1_email']);
        if (isset($_user_value_array['parent_1_facebook'])) unset($_user_value_array['parent_1_facebook']);
        if (isset($_user_value_array['parent_1_phone'])) unset($_user_value_array['parent_1_phone']);
        //PARENT_2 HANDLER
        if (isset($_user_value_array['add_parent_2']) && $_user_value_array['add_parent_2'] != "") {
            $_user_value_array['uk_parent_2'] = self::add_new_parent(
                $_user_value_array['add_parent_2'],
                $_user_value_array['parent_2_email'], $_user_value_array['parent_2_facebook'],
                $_user_value_array['parent_2_phone']);
            log::register('new_parent', $_user_value_array['uk_parent_2']);
        }
        else {
            //meglévő szülő updatelése
            //var_dump($_user_value_array['uk_parent_2']);
            user_parent::update_parent(array(
                'up_email' => $_user_value_array['parent_2_email'],
                'up_facebook' => $_user_value_array['parent_2_facebook'],
                'up_phone' => $_user_value_array['parent_2_phone']), $_user_value_array['uk_parent_2']);

        }
        if (isset($_user_value_array['add_parent_2'])) unset($_user_value_array['add_parent_2']);
        if (isset($_user_value_array['parent_2_email'])) unset($_user_value_array['parent_2_email']);
        if (isset($_user_value_array['parent_2_facebook'])) unset($_user_value_array['parent_2_facebook']);
        if (isset($_user_value_array['parent_2_phone'])) unset($_user_value_array['parent_2_phone']);

        //SCHOOL HANDLER
        if (isset($_user_value_array['add_school']) && $_user_value_array['add_school'] != "") {
            //$new_school_id = $sql->insert_into('school', array('sc_name' => $_user_value_array['add_school'], 'sc_school_city_scc_id' => $_user_value_array['uk_school_city_scc_id']));
            $new_school_id = school::create_school($_user_value_array['add_school'], $_user_value_array['uk_school_city_scc_id']);
            log::register('new_school', $new_school_id);
            $_user_value_array['uk_school_sc_id'] = $new_school_id;
        }

        unset($_user_value_array['add_school']);
        unset($_user_value_array['uk_school_city_scc_id']);

        //date handler
        if (!isset($_user_value_array['uk_first_training']) || $_user_value_array['uk_first_training'] == "") {
            $_user_value_array['uk_first_training'] = 'null';
        }
        if (!isset($_user_value_array['uk_birth_date']) || $_user_value_array['uk_birth_date'] == "") {
            $_user_value_array['uk_birth_date'] = 'null';
        }
        if (!isset($_user_value_array['uk_birth_year']) || $_user_value_array['uk_birth_year'] == "") {
            $_user_value_array['uk_birth_year'] = 'null';
        }

        foreach ($_user_value_array as $index => $value) {
            if ($value == "") $_user_value_array[$index] = 'null';
        }

        //ha nincs bejelölve h aktív akkor nem kapja meg ezt az értéket, manuálisan kell beállítani
        if (!isset($_user_value_array['uk_is_active'])) $_user_value_array['uk_is_active'] = 0;
        if (!isset($_user_value_array['uk_balance_expires'])) $_user_value_array['uk_balance_expires'] = 0;
        if (!isset($_user_value_array['uk_lease'])) $_user_value_array['uk_lease'] = 0;

        $trt_ids = array();
        if (isset($_user_value_array['level']) && is_array($_user_value_array['level'])) {
            foreach ($_user_value_array['level'] as $key => $trt_id) {
                $trt_ids[] = $trt_id;
            }
            unset($_user_value_array['level']);
        }

        $sql->execute_query("DELETE FROM user_kid_training_type WHERE uktt_user_kid_uk_id = {$_user_id}");
        if (!empty($trt_ids)) {
            foreach ($trt_ids as $key => $trt_id) {
                $sql->insert_into('user_kid_training_type', array(
                        'uktt_user_kid_uk_id' => $_user_id,
                        'uktt_training_type_tt_id' => $trt_id,
                    )
                );
            }
        }

        $sql->update_table('user_kid', $_user_value_array, array('uk_id' => $_user_id));
    }

    public function calculate_balance(&$_de_obj_array, $_user_obj = null, $update = true) {
        //kiszámolja az egyenleget a diary entry-k alapján, amik tömbben érkeznek
        //a tömbben feltölti a pénzmozgást, és minden lépésben befrissíti az egyenleget
        //az update-elt tömböt adja vissza
        //ha user_obj null, akkor az aktuális usernél állítja be
        global $sql;

        $actualExpireDate = '2100-01-01';

        //kezdetben 0
        if (is_object($_user_obj)) {
            if (null == $_user_obj->get_uk_balance_transfer()) {
                $balance = 0;
            }
            else {
                $balance = $_user_obj->get_uk_balance_transfer();
            }
        }
        else {
            if (null == $this->get_uk_balance_transfer()) {
                $balance = 0;
            }
            else {
                $balance = $this->get_uk_balance_transfer();
            }
        }
        foreach ($_de_obj_array as $i => $_de) {
            //minden lépésben az balance-t be kell állítani az előző lépésben updatelt balance-ra
            if (isset($_de_obj_array[$i-1])) {
                $_de->set_de_balance($_de_obj_array[$i-1]->get_de_balance());
            }
            else {
                $_de->set_de_balance($balance);
            }
/*            if ($_de->get_de_date() > date('Y-m-d 23:59:59', strtotime($actualExpireDate))) {
                $_de->set_de_expired(true);
            }*/

            if ($_de->get_de_date() > date('Y-m-d 23:59:59', strtotime($actualExpireDate)) && $_de->get_de_balance() > 0) {
                $_de->set_de_balance(0);
            }

            if ($_de->get_de_type() == 'training') {

                //$balance -= 1200;
                $training = $_de->get_de_training();
                $trainingPrice = $training->get_tr_price();
                $_de->set_de_transaction(-$trainingPrice); //beállítjuk, mennyivel csökken az egyenleg
                if (0 == $i) {
                    $_de->set_de_balance($balance+$_de->get_de_transaction()); //beállítjuk az új egyenleget
                }
                else {
                    $_de->set_de_balance($_de->get_de_balance()+$_de->get_de_transaction()); //beállítjuk az új egyenleget
                }
                //echo $_de->get_de_date() . " minusz 1200<br><br>";

            }
            elseif ($_de->get_de_type() == 'money_deposit') {
                $actualExpireDate = $_de->get_de_money_deposit()->get_mod_expire_date();
                $_de->set_de_transaction($_de->get_de_money_deposit()->get_mod_money_income()->get_mi_sum());

                //ha ez a befizetés az utolsó és lejárt, akkor ne adjuk hozzá a balancehoz
                if ($i < (count($_de_obj_array) -1 ) || !$_de->get_de_expired()) {
                    $_de->set_de_balance($_de->get_de_balance()+$_de->get_de_transaction());
                }

                /*if (!$_de->get_de_expired() || !$update) {
                    $_de->set_de_balance($_de->get_de_balance()+$_de->get_de_transaction());
                } elseif($update) {
                    $_de->set_de_balance(0);
                }*/
                //$balance += $_de->get_de_money_deposit()->get_mod_sum();
                //echo $_de->get_de_date() . " plussz " . $_de->get_de_money_deposit()->get_mod_sum() . "<br><br>";
            }

            //var_dump('balance: '.$_de->get_de_balance());
        }
        if ($update) {
            if (isset($_de)) $sql->update_table('user_kid', (array('uk_balance' => $_de->get_de_balance())), array('uk_id' => (is_object($_user_obj)?$_user_obj->get_uk_id():$this->get_uk_id())), false);
            else $sql->update_table('user_kid', (array('uk_balance' => 0)), array('uk_id' => (is_object($_user_obj)?$_user_obj->get_uk_id():$this->get_uk_id())), false);
        }

        return $_de_obj_array;
    }

    // public function get_groups() {
    //     global $sql;

    //     $group_array = array();
    //     $g_assoc = $sql->assoc_array("select * from user_group_kid join user_group on ug_id = ugk_user_group_ug_id where ug_is_automatic = 1 and ugk_user_kid_uk_id = ".$this->get_uk_id().";");

    //     foreach ($g_assoc as $g) {
    //         $group = new user_group();
    //         $group->set_ug_data_by_id($g['ugk_user_group_ug_id']);
    //         $group_array[] = $group;
    //     }

    //     return $group_array;
    // }

    public function update_balance() {
        global $sql;

        $userId = $this->get_uk_id();

        $action_list_query = "
                        SELECT
                            object_id,
                            TIMESTAMP(object_date) AS object_date,
                            object_type,
                            expire_date,
                            IF(expire_date <= NOW(), 1, 0) AS expired
                        FROM
                            ((SELECT
                                pr_training_tr_id AS object_id,
                                    TIMESTAMP(tr_date) AS object_date,
                                    IF(pr_training_tr_id IS NOT NULL, 'training', NULL) AS object_type,
                                    NULL AS expire_date
                            FROM
                                presence
                            JOIN training ON (tr_id = pr_training_tr_id
                                AND tr_locked = 1)
                            WHERE
                                pr_user_kid_uk_id = ".$userId."
                                    AND tr_deleted = 0) UNION (SELECT
                                mod_id,
                                    TIMESTAMP(mi_date),
                                    IF(mod_id IS NOT NULL, 'money_deposit', NULL) AS object_type,
                                    mod_expire_date AS expire_date
                            FROM
                                money_deposit
                            JOIN money_income ON mi_id = mod_money_income_mi_id
                            WHERE
                                mod_user_kid_uk_id = ".$userId."
                                    AND mod_deleted = 0)) actions
                        ORDER BY object_date ASC;
                    ";

        $action_assoc_array = $sql->assoc_array($action_list_query);
        $actions = array();
        $de_array = array();
        foreach ($action_assoc_array as $action) {


            if ($action['object_type'] == 'training') {
                $new_training = new training();
                $new_training->set_training_data_by_id($action['object_id']);
                //$actions[] = $new_training;
                $new_diary_entry = new diary_entry($action['object_id'], $action['object_date'], $action['object_type'], 0, 0, 0, 0, $new_training, null);
            }
            elseif ($action['object_type'] == 'money_deposit') {
                $new_mod = new money_deposit();
                $new_mod->set_mod_data_by_id($action['object_id']);
            //var_dump($new_mod);
                //$actions[] = $new_mod;
                $new_diary_entry = new diary_entry($action['object_id'], $action['object_date'], $action['object_type'], 0, 0, 0, $action['expired'], null, $new_mod);
            }

            $de_array[] = $new_diary_entry;
        }

        //itt csak hivatkozással adjuk át a tömböt, a calculate_balance kiszámolja, belerakja és visszadja
        $this->calculate_balance($de_array, $this);
    }

    public function get_credit_color($has_credit, $credit, $trainingPrice) {
        //vizsgálni kell, hogy: negatív egyenleg előfordulhat-e
            //ha igen, van-e hitelezés
                //ha igen, túllepte-e PIROS
                    //ha nem, akkor SÁRGA
            //ha nincs, akkor azonnal PIROS
        if ($this->get_uk_balance() < 0 || ($this->get_uk_balance() - $trainingPrice) < 0) {
            return 'credit-warning';
        }
        return false;
    }

    public function balance_meta_update() {
        global $sql;

        $lastDeposit = 'null';
        $expireDate = 'null';

        $deposits = $sql->assoc_array('
            SELECT
                *
            FROM
                money_deposit
                    JOIN
                money_income ON mi_id = mod_money_income_mi_id
            WHERE
                mod_user_kid_uk_id = '.$this->get_uk_id().'
                    AND mod_deleted = 0
                    AND mod_expire_date > NOW()
            ORDER BY mi_date DESC
            LIMIT 1;
        ');

        if (count($deposits)) {
            $lastDeposit = $deposits[0]['mi_date'];
            $expireDate = money_deposit::calculate_expire_date($deposits[0]['mod_id']);
        }

        $sql->update_table('user_kid',
            array(
                'uk_last_deposit' => $lastDeposit,
                'uk_balance_expire_date' => $expireDate,
            ),
            array(
                'uk_id' => $this->get_uk_id(),
            )
        );
    }

    public static function has_training_type($_uk_id, $_trt_id) {
        global $sql;

        return $sql->num_of_rows("SELECT DISTINCT uktt_id FROM user_kid_training_type WHERE uktt_user_kid_uk_id = {$_uk_id} AND uktt_training_type_tt_id = {$_trt_id};");
    }
}
?>
