<?php

class M_login extends CI_Model {
    public $table       = 'tbl_user';
    public $company     = 'am_company';
    public $comp_gl     = 'at_compaccountgl';
    public $acc_gl      = 'am_accountgl';
    public $sub_type    = 'am_accountsubtype';
    public $ac_type     = 'am_accounttype';
    public $hlogin      = 'ah_loginhistory';
	
    function check_admin($username, $password) {
        $query = $this->db->get_where($this->table, array('EmployeeCode' => $username, 'EmployeePassword' => $password), 1, 0);
		return $query->result();
    }
	
	function check_user($username, $password) {
        $query = $this->db->get_where($this->table, array('EmployeeCode' => $username, 'EmployeePassword' => $password), 1, 0);

        if ($query->num_rows() > 0) {
            return TRUE;
        }
        else {
            return FALSE;
        }	
    }
	
    function get_branch(){
        $this->db->select('*');
        $this->db->from('am_branch');
        $query = $this->db->get();
        return $query->result();
    }
	
    function get_branch_by($branch){
        $this->db->select('*, am_company.CompanyName');
        $this->db->from('am_branch');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'inner');
        //$this->db->where('am_branch.BranchStatus', 0);
        $this->db->where('am_branch.BranchCode', $branch);
        $query = $this->db->get();
        return $query->row();
    }

    function user_login($data_login) {
        $this->db->insert('tbl_userlog', $data_login);
        $log_id = $this->db->insert_id();
        return $log_id;	
    }
	
    function user_logout($log_id, $data_login) {
        $this->db->update('tbl_userlog', $data_login, array('UserLogID' => $log_id));
    }
	
    function get_group_id($employee) {
        $this->db->select('GroupID');
        $this->db->from('tbl_user');
        $this->db->where('EmployeeCode', $employee);		
        $query = $this->db->get();    

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $group_id = $row->GroupID;
            }    

            return $group_id;
        }
        else {
            return FALSE;
        }	
    }
	
    function get_employee_name($employee) {
        $this->db->select('EmployeeName');
        $this->db->from('hm_employee');
        $this->db->where('EmployeeCode', $employee);		
        $query = $this->db->get();    

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                    $EmployeeName = $row->EmployeeName;
            }    

            return $EmployeeName;
        }
        else {
            return FALSE;
        }	
    }
	
    function get_main_authority($group) {
        $this->db->select('MainAuthority');
        $this->db->from('tbl_groupuser');
        $this->db->where('GroupID', $group); 
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $MainAuthority = $row->MainAuthority;
            }    

            return $MainAuthority;
        }
        else {
            return FALSE;
        }        
    }
	
    function get_code_authority($username) {
        $this->db->select('AuthorityCode');
        $this->db->from('tbl_groupauthority');
        $this->db->where('EmployeeCode', $username); 
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {	
                $code_authority[] = $row->AuthorityCode;
            }	    

            return $code_authority;
        }
        else {
            return FALSE;
        }    
    }
	
    function get_menu_id($group) {
        $this->db->select('MenuID');
        $this->db->from('tbl_groupauthority');
        $this->db->where('GroupID', $group); 
        $query = $this->db->get();    
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $menu_id[] = $row->MenuID;
            }    

            return $menu_id;
        }
        else {
            return FALSE;
        }  	
    }
	
    function get_parent_id($menu_id) {
        $this->db->select('ParentID');
        $this->db->from('tbl_menu');
        $this->db->where('MenuID', $menu_id); 
        $query = $this->db->get();    
        
        foreach ($query->result() as $row) {
            $parent_id = $row->ParentID;
        }    

        return $parent_id;
    }
	
    function get_menu_name($menu_id) {
        $this->db->select('MenuName');
        $this->db->from('tbl_menu');
        $this->db->where('MenuID', $menu_id); 
        $query = $this->db->get();    
        
        foreach ($query->result() as $row) {
            $menu_name = $row->MenuName;
        }    

        return $menu_name;
    }
	
    function get_menu_id_by_menu_name($menu_name) {
        $this->db->select('MenuID');
        $this->db->from('tbl_menu');
        $this->db->where('MenuName', $menu_name); 
        $query = $this->db->get();    
        
        foreach ($query->result() as $row) {
            $menu_id = $row->MenuID;
        }    

        return $menu_id;
    }

    function get_detail_employee($id){
        $this->db->select('tbl_user.UserID, tbl_user.EmployeeCode, tbl_user.GroupID, tbl_user.AdminAuthority, am_employee.BranchCode,'); 
        $this->db->select('am_employee.DepartmentCode, am_employee.JobTitleCode, am_jobtitle.JobTitleStatus, am_branch.CompanyCode, am_department.DepartmentName,');
        $this->db->select('am_company.CompanyName, am_branch.BranchName, am_branch.BranchStatus, am_jobtitle.JobTitleName');
        $this->db->from('tbl_user');
        $this->db->join('am_employee', 'am_employee.EmployeeCode = tbl_user.EmployeeCode', 'inner');
        $this->db->join('am_department', 'am_department.DepartmentCode = am_employee.DepartmentCode', 'inner');
        $this->db->join('am_jobtitle', 'am_jobtitle.JobTitleCode = am_employee.JobTitleCode', 'inner');
        $this->db->join('am_branch', 'am_branch.BranchCode = am_employee.BranchCode', 'inner');
        $this->db->join('am_company', 'am_company.CompanyCode = am_branch.CompanyCode', 'inner');
        $this->db->where('tbl_user.EmployeeCode', $id);
        $query = $this->db->get();
        
        return $query->result();
    }
	
	function check_full_admin(){
		$username = $this->input->post('username');
		
		$this->db->select('*');
		$this->db->from('tbl_user');
		$this->db->where('EmployeeCode',$username);
		$this->db->where('AdminAuthority',9);
		$query = $this->db->get();

                if($query->num_rows() > 0){
			return true;
		}
		else{
			return false;
		}
	}

    function check_year_transaction_in_coa($company_code){
        $this->db->select('max(GLSaldoYears) as year_coa');
        $this->db->where('CompanyCode',$company_code);
        $query = $this->db->get($this->comp_gl);

        if($query->num_rows()>0){
            $row = $query->row();
            return $row->year_coa;
        }else{
            return 0;
        }
    }

    function check_company_lock_unlock($company_code){
        $this->db->select('LockStatus');
        $this->db->where('CompanyCode',$company_code);
        $query = $this->db->get($this->company);

        if($query->num_rows()>0){
            $row = $query->row();
            return $row->LockStatus;
        }else{
            return 0;
        }
    }

    function lock_unlock_company($company_code,$val){
        $this->db->set('LockStatus',$val);
        $this->db->where('CompanyCode',$company_code);
        $this->db->update($this->company);
    }

    function get_coa_bycompany($company_code,$year){
        $this->db->select('gl.*,sub.AccountBSStatus');
        $this->db->from($this->comp_gl.' as gl');
        $this->db->join($this->acc_gl.' as ac','ac.AccountNumber=gl.AccountNumber','inner');
        $this->db->join($this->sub_type.' as sub','sub.AccountSubTypeID=ac.AccountSubTypeID','inner');
        $this->db->where('gl.CompanyCode',$company_code);
        $this->db->where('gl.GLSaldoYears',$year);
        $this->db->order_by('gl.BranchCode,gl.AccountNumber','ASC');

        $query = $this->db->get()->result();

        return $query;
    }

    function transaction_transfer_coa($array_coa){
        $this->db->insert($this->comp_gl,$array_coa);
    }

    function transfer_coa_transaction($trans_data){
        $this->db->trans_start();

        if($this->trans_data['total_coa'] > 0){

            $this->delete_session($trans_data['delete_session']['username']);
            $this->save_session($trans_data['data_session']);

            for($x=0;$x<count($trans_data['array_coa']);$x++){
                $this->transaction_transfer_coa($trans_data['array_coa'][$x]);
            }

            //$this->lock_unlock_company($trans_data['company'],0);

            $this->db->trans_complete();

            if ($this->db->trans_status() === FALSE) {
                return false;
            }else{
                return true;
            }
        }else{

            $this->delete_session($trans_data['delete_session']['username']);
            $this->save_session($trans_data['data_session']);

            $this->db->trans_complete();

            if ($this->db->trans_status() === FALSE) {
                return false;
            }else{
                return true;
            }
        }
    }

    function save_session($data_session){
        $this->db->insert($this->hlogin,$data_session);
    }

    function get_data_sessiondb($username){
        $this->db->from($this->hlogin);
        $this->db->where('username',$username);

        $query      = $this->db->get()->result();

        $return     = [];

        foreach($query  as $row){
            $return['session_id']   = $row->session_id;
            $return['user_id']      = $row->user_id;
            $return['username']     = $row->username;
            $return['group_menu']   = $row->group_menu;
            $return['company']      = $row->company;
            $return['company_name'] = $row->company_name;
            $return['department']   = $row->department;
            $return['branch']       = $row->branch;
            $return['branch_status']= $row->branch_status;
            $return['jobtitle']     = $row->jobtitle_status;
            $return['keydata']      = $row->keydata;
            $return['login']        = $row->login;
        }

        return $return;
    }

    function delete_session($username,$session_id=''){
        $this->db->where('username',$username);

        if($session_id!=''){
            $this->db->where('session_id',$session_id);
        }

        $this->db->delete($this->hlogin);
    }

    function get_session_id($session_id,$username){
        $this->db->select('session_id');
        $this->db->from($this->hlogin);
        $this->db->where('session_id',$session_id);
        $this->db->where('username',$username);

        $query = $this->db->get();

        if($query->num_rows()>0){
            return true;
        }else{
            return false;
        }
    }
}	
	
	