<?php

/*
    MONEY_DEPOSIT CLASS
*/

class money_deposit {
    private $mod_id;
    private $mod_user_kid_uk_id; //ID
    private $mod_user_kid; //OBJECT
    private $mod_money_income_mi_id; //ID
    private $mod_money_income; //OBJECT
    private $mod_deleted;
    private $mod_sets_expire_date;
    private $mod_expire_date;
    private $mod_lease_l_id; //OBJECT
    private $mod_non_zero;

    public function set_mod_id($_id) {
        $this->mod_id = $_id;
    }

    public function set_mod_user_kid_uk_id($_user_kid_uk_id) {
        $this->mod_user_kid_uk_id = $_user_kid_uk_id;
    }

    public function set_mod_user_kid($_user_kid) {
        $this->mod_user_kid = $_user_kid;
    }

    public function set_mod_money_income_mi_id($_money_income_mi_id) {
        $this->mod_money_income_mi_id = $_money_income_mi_id;
    }

    public function set_mod_money_income($_money_income) {
        $this->mod_money_income = $_money_income;
    }

    public function set_mod_deleted($_deleted) {
        $this->mod_deleted = $_deleted;
    }

    public function set_mod_sets_expire_date($_sets_expire_date) {
        $this->mod_sets_expire_date = $_sets_expire_date;
    }

    public function set_mod_expire_date($_expire_date) {
        $this->mod_expire_date = $_expire_date;
    }

    public function set_mod_lease_l_id($_lease_l_id) {
        $this->mod_lease_l_id = $_lease_l_id;
    }

    public function set_mod_non_zero($_non_zero) {
        $this->mod_non_zero = $_non_zero;
    }

    public function get_mod_id() {
        return $this->mod_id;
    }

    public function get_mod_user_kid_uk_id() {
        return $this->mod_user_kid_uk_id;
    }

    public function get_mod_user_kid() {
        return $this->mod_user_kid;
    }

    public function get_mod_money_income_mi_id() {
        return $this->mod_money_income_mi_id;
    }

    public function get_mod_money_income() {
        return $this->mod_money_income;
    }

    public function get_mod_deleted() {
        return $this->mod_deleted;
    }

    public function get_mod_sets_expire_date() {
        return $this->mod_sets_expire_date;
    }

    public function get_mod_expire_date() {
        return $this->mod_expire_date;
    }

    public function get_mod_lease_l_id() {
        return $this->mod_lease_l_id;
    }

    public function get_mod_non_zero() {
        return $this->mod_non_zero;
    }

    public function set_mod_data_by_id($_id) {
        global $sql;
        $mod_query = "SELECT * FROM money_deposit WHERE mod_id = " . $_id . ";";
        $mod_assoc_array = $sql->assoc_array($mod_query);
        //var_dump($mod_assoc_array);
        foreach ($mod_assoc_array[0] as $field => $value) {
            $function_name = "set_" . $field;
            $this->$function_name($value); //alapadatok beállítása
            if ($field == "mod_user_kid_uk_id") {
                $new_user = new user_kid();
                $new_user->set_user_data_by_id($value);
                $this->set_mod_user_kid($new_user);
            }
            if ($field == "mod_money_income_mi_id") {
                $new_mi = new money_income();
                $new_mi->set_mi_data_by_id($value);
                $this->set_mod_money_income($new_mi);
            }
            if ($field == "mod_lease_l_id" && $value !== null) {
                $new_l = new lease();
                $new_l->set_l_data_by_id($value);
                $this->set_mod_lease_l_id($new_l);
            }
        }
    }
     public function is_expired() {
        return $this->get_mod_expire_date() < date('Y-m-d');
     }

    public static function create_money_deposit($_user_id, $_date, $_sum, $_lease, $_mod_expire_date, $_pt = 1, $_sets_date = false) {
        global $sql;

        $income_id = $sql->insert_into('money_income', array(
                'mi_date' => $_date,
                'mi_payment_type_pt_id' => $_pt,
                'mi_sum' => $_sum,
                'mi_item' => 'Edzésdíj',
                'mi_money_income_category_mic_id' => 4,
            )
        );

        $expireDate = 'null';

        if ($_sets_date) {
            $userLastDeposit = $sql->single_variable("SELECT uk_last_deposit FROM user_kid WHERE uk_id = {$_user_id}");
            if ($userLastDeposit < $_date) {
                $sql->update_table('user_kid', array(
                    'uk_last_deposit' => $_date,
                ), array(
                    'uk_id' => $_user_id,
                ));
            }

            //lekérjük, hogy milyen a lejárati típusa a bérletnek, és az alapján beállítjuk a lejárati dátumot az usernek

            $nonZero = false;
            if ('null' != $_lease) {
                $lease = new lease();
                $lease->set_l_data_by_id($_lease);

                $userBalanceExpireDate = $sql->single_variable("SELECT uk_balance_expire_date FROM user_kid WHERE uk_id = {$_user_id}");
                if ($userBalanceExpireDate < $_mod_expire_date && $_mod_expire_date > date('Y-m-d')) {
                    $sql->update_table('user_kid', array(
                        'uk_balance_expire_date' => $_mod_expire_date,
                    ), array(
                        'uk_id' => $_user_id,
                    ));
                }

                $nonZero = $lease->get_l_non_zero();
                /*if ($lease->get_l_expire_type() == 1) {
                    //naptári hónap vége
                    $sql->update_table('user_kid', array(
                        'uk_balance_expire_date' => date('Y-m-t', strtotime($_mod_expire_date))
                    ), array(
                        'uk_id' => $_user_id,
                    ));
                    $expireDate = date('Y-m-t');
                }
                elseif ($lease->get_l_expire_type() == 2) {
                    //következő hónapban
                    $sql->update_table('user_kid', array(
                        'uk_balance_expire_date' => date("Y-m-d", strtotime("+1 month", $_mod_expire_date)),
                    ), array(
                        'uk_id' => $_user_id,
                    ));
                    $expireDate = date("Y-m-d", strtotime("+1 month", $_mod_expire_date));
                }
                elseif ($lease->get_l_expire_type() == 3) {
                    //egyedi dátum
                    $sql->update_table('user_kid', array(
                        'uk_balance_expire_date' => $lease->get_l_expire_date(),
                    ), array(
                        'uk_id' => $_user_id,
                    ));
                    $expireDate = date("Y-m-d", strtotime($lease->get_l_expire_date()));
                }*/
            }
        }

        return $sql->insert_into('money_deposit', array(
            'mod_user_kid_uk_id' => $_user_id,
            'mod_money_income_mi_id' => $income_id,
            'mod_sets_expire_date' => $_sets_date,
            'mod_lease_l_id' => $_lease,
            'mod_expire_date' => $_mod_expire_date,
            'mod_non_zero' => $nonZero,
        ));
    }

    public static function update_money_deposit($_date, $_sum, $_mod_id, $_pt, $_mi_id) {
        global $sql;
        $sql->update_table('money_income', array(
                //'mod_user_kid_uk_id' => $_user_id,
                'mi_date' => $_date,
                'mi_payment_type_pt_id' => $_pt,
                'mi_sum' => $_sum
            ), array(
                'mi_id' => $_mi_id
        ));

        // $sql->update_table('money_deposit', array(
        //         'mod_user_kid_uk_id' => $_user_id,
        //         //'mod_lease_l_id' => $_lease,
        //     ), array(
        //         'mod_id' => $_mod_id
        // ));

    }

    public static function calculate_expire_date($_mod_id) {
        global $sql;

        $mod = new money_deposit();
        $mod->set_mod_data_by_id($_mod_id);

        $expireDate = null;

        $lease = $mod->get_mod_lease_l_id();
        if (null !== $lease) {
            if ($lease->get_l_expire_type() == 1) {
                $expireDate = date('Y-m-t', strtotime($mod->get_mod_money_income()->get_mi_date()));
            }
            elseif ($lease->get_l_expire_type() == 2) {
                $expireDate = date("Y-m-t", strtotime("+1 month", strtotime($mod->get_mod_money_income()->get_mi_date())));
            }
            elseif ($lease->get_l_expire_type() == 3) {
                //egyedi dátum
                $expireDate = date("Y-m-d", strtotime("+1 month", strtotime($mod->get_mod_money_income()->get_mi_date())));
            }
        }

        return $expireDate;
    }

    public static function get_month_mod_sum($_year, $_month) {
        global $sql;

        return $sql->single_variable("
            SELECT
                SUM(mi_sum)
            FROM
                money_deposit
                    JOIN
                money_income ON mi_id = mod_money_income_mi_id
            WHERE
                mi_date LIKE '%{$_year}-{$_month}%'
                    AND mi_deleted = 0
                    AND mod_deleted = 0
        ");
    }

}

?>
