<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Company extends CI_Model {

    public function getCompany_all() {
		$this->db->where('is_delete', 0);
        $this->db->from('company');
		$query = $this->db->get();
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function getCompany_byID($companyID) {
        $this->db->select('a.*');
		$this->db->select('b.fldid, b.fldname');
		$this->db->select('c.ID as parlimenID, c.parlimen');
		$this->db->select('d.fldid, d.fldcountry');
		$this->db->where('a.ID', $companyID);
		$this->db->from('company a');
		$this->db->join('_ref_state b', 'a.companyStateID = b.fldid', 'LEFT');
		$this->db->join('parlimen c', 'a.parlimenID = c.ID', 'LEFT');
		$this->db->join('_ref_country d', 'a.countryID = d.fldid', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function getParlimen_all() {
        $this->db->from('parlimen');
		$query = $this->db->get();
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function getParlimen_byStateID($stateID) {
        $this->db->where('stateID', $stateID);
		$this->db->from('parlimen');
		$query = $this->db->get();
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function getState_all($ID) {
		$this->db->where('fldregion_id', $ID);
        $this->db->from('_ref_state');
		$query = $this->db->get();
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function getState_byID($stateID) {
		$this->db->where('fldid', $stateID);
        $this->db->from('_ref_state');
		$query = $this->db->get();
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function getParlimen_byID($parlimenID) {
		$this->db->where('ID', $parlimenID);
        $this->db->from('parlimen');
		$query = $this->db->get();
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function getBusinessType() {
		//$this->db->where('ID', element('compID', $this->session->userdata('logged_in')));
		$this->db->from('_ref_business_type');

		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	
	public function companyInsert($newCompany){
		$sql = $this->db->insert_string('company', $newCompany);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			return false;
		}
	}
	
	public function companyInsert2($newCompany, $database){
		$sql = $this->db->insert_string(SUB_DB.$database.'.company', $newCompany);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			return false;
		}
	}
	
	
	public function companyUpdate($ID, $data) {
        $this->db->where('ID', $ID);
        $query = $this->db->update('company', $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	public function userAccessInsert($new){
		$sql = $this->db->insert_string('user_module_access', $new);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			return false;
		}
	}
	
	public function userAccessUpdate($data, $ID){
		$this->db->where('userID', $ID);
        $query = $this->db->update('user_module_access', $data);
        if ($query === TRUE) {
            return TRUE;
        } else {
            return FALSE;
        }
	}
	
	public function getChartOfAcct($database) {
		//$this->db->where('ID', element('compID', $this->session->userdata('logged_in')));
		$this->db->from(SUB_DB.$database.'._ref_chart_of_acct');

		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function insertChartOfAcct($newCompany, $database){
		$sql = $this->db->insert_string(SUB_DB.$database.'.comp_chart_of_acct', $newCompany);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			return false;
		}
	}

}
