<?php


    $users = $sql->assoc_array('select * from user_kid order by uk_name asc;');

    foreach ($users as $key => $value) {
        $user = new user_kid();
        $user->set_user_data_by_id($value['uk_id']);


        $action_list_query = "
            SELECT
            object_id,
            timestamp(object_date) as object_date,
            object_type,
            (SELECT
                    count(distinct date(tr_date))
                FROM
                    presence
                        JOIN
                    training ON tr_id = pr_training_tr_id
                WHERE
                    YEAR(tr_date) = YEAR(object_date)
                        AND MONTH(tr_date) = MONTH(object_date)
                        AND tr_date <= object_date
                        AND pr_user_kid_uk_id = ".$user->get_uk_id()."
                        AND tr_date > (select
                            if(max(trd) is null,
                                    '1900-01-01',
                                    max(trd))
                        from
                            (select
                                tr_date trd
                            from
                                presence
                            join training ON tr_id = pr_training_tr_id
                            join user_kid ON uk_id = pr_user_kid_uk_id
                            where
                                pr_user_kid_uk_id = ".$user->get_uk_id()."
                                    and tr_date >= (SELECT
                                        tr_date
                                    from
                                        presence
                                    join training ON tr_id = pr_training_tr_id
                                    join user_kid ON uk_id = pr_user_kid_uk_id
                                    where
                                        pr_user_kid_uk_id = ".$user->get_uk_id()."
                                            and date(tr_date) = uk_first_training
                                    ORDER BY tr_date ASC
                                    limit 1)
                            order by tr_date ASC
                            limit 2) as elso_ket_edzes)) as 'training_per_month',
            (SELECT
                    count(pr_id)
                FROM
                    presence
                        JOIN
                    training ON tr_id = pr_training_tr_id
                WHERE
                    DATE(tr_date) = DATE(object_date)
                        AND tr_date <= object_date
                        AND pr_user_kid_uk_id = ".$user->get_uk_id().") as 'training_per_day',
            (select
                    if(sum(if(trd = object_date, 1, 0)) > 0,
                            1,
                            0)
                from
                    (select
                        tr_date trd
                    from
                        presence
                    join training ON tr_id = pr_training_tr_id
                    join user_kid ON uk_id = pr_user_kid_uk_id
                    where
                        pr_user_kid_uk_id = ".$user->get_uk_id()."
                            and tr_date >= (SELECT
                                tr_date
                            from
                                presence
                            join training ON tr_id = pr_training_tr_id
                            join user_kid ON uk_id = pr_user_kid_uk_id
                            where
                                pr_user_kid_uk_id = ".$user->get_uk_id()."
                                    and date(tr_date) = uk_first_training
                            ORDER BY tr_date ASC
                            limit 1)
                    order by tr_date ASC
                    limit 2) elso2edzes) as 'first_two'
        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
            FROM
                presence
            JOIN training ON tr_id = pr_training_tr_id
            WHERE
                pr_user_kid_uk_id = ".$user->get_uk_id()."
                    AND tr_deleted = 0) UNION (SELECT
                mod_id,
                    timestamp(mi_date),
                    if(mod_id is not null, 'money_deposit', null) as object_type
            FROM
                money_deposit
            JOIN
                money_income ON mi_id = mod_money_income_mi_id
            WHERE
                mod_user_kid_uk_id = ".$user->get_uk_id()."
                    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'], $action['training_per_month'], $action['training_per_day'], $action['first_two'], $new_training);
    		}
    		elseif ($action['object_type'] == 'money_deposit') {
    			$new_mod = new money_deposit();
    			$new_mod->set_mod_data_by_id($action['object_id']);
    			//$actions[] = $new_mod;
    			$new_diary_entry = new diary_entry($action['object_id'], $action['object_date'], $action['object_type'], $action['training_per_month'], $action['training_per_day'], $action['first_two'], 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
    	$user->calculate_balance($de_array, $user);
    	//var_dump($de_array);
    	//$smarty->assign('actions', $actions);


    }

    header('Location: /admin/balance_list' . ($this->is_id()?'/'.$this->get_id():''));

?>
