<?php

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

class Datamigrate_model extends CI_Model {

    public function user_all() {
		$this->db->from(SUB_DB.'demodb.tbl_usermaster');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }


	public function checkEmail_byName($companyName) {
		$this->db->select('a.*');
		$this->db->select('c.companyName');
		$this->db->select('b.companyID, b.userID');
		$this->db->from(SUB_DB.'demodb.tbl_usermaster a');
		$this->db->like('a.userid', $companyName);
		$this->db->join(SUB_DB.'demodb.user_module_access b', 'a.fldid = b.userID', 'LEFT');
		$this->db->join(SUB_DB.'demodb.company c', 'b.companyID = c.ID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function checkEmail_byUserID($userID) {
		$this->db->select('a.*');
		$this->db->select('c.*');
		$this->db->select('b.companyID, b.userID');
		$this->db->from(SUB_DB.'demodb.tbl_usermaster a');
		$this->db->like('a.fldid', $userID);
		$this->db->join(SUB_DB.'demodb.user_module_access b', 'a.fldid = b.userID', 'LEFT');
		$this->db->join(SUB_DB.'demodb.company c', 'b.companyID = c.ID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	public function checkEmail_byNameInAdminDatabase($companyName) {
		$this->db->select('a.*');
		$this->db->select('c.companyName');
		$this->db->select('b.companyID, b.userID');
		$this->db->from('tbl_usermaster a');
		$this->db->like('a.userid', $companyName);
		$this->db->join('user_module_access b', 'a.fldid = b.userID', 'LEFT');
		$this->db->join('company c', 'b.companyID = c.ID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	
	public function checkCompany_byName($companyName) {
		$this->db->like('companyName', $companyName);
		$this->db->from('company');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
    }
	
	
	public function insertUser($tname, $data){
		$sql = $this->db->insert_string($tname, $data);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			return FALSE;
		}
	}
	
	public function insertCompany($tname, $data){
		$sql = $this->db->insert_string($tname, $data);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return $this->db->insert_id();
		} else {
			return FALSE;
		}
	}
	
	public function insert($tname, $data){
		$sql = $this->db->insert_string($tname, $data);
		$query = $this->db->query($sql);
		if($query === TRUE){
			//return TRUE;
			return $this->db->insert_id();
		} else {
			return FALSE;
		}
	}
	
	
	public function getNextServerAllocation(){
		$this->db->from('view_next_server_allocation');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	public function getChartOfAcct_byID($tname, $companyID){
		$this->db->select('ID');
		$this->db->where('companyID', $companyID);
		$this->db->where('acctGroupID', 3);
		$this->db->where('acctCode >', 220000000);
		$this->db->from($tname);
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	public function getFromDemo($tname, $companyID){
		$this->db->where('companyID', $companyID);
		$this->db->from($tname);
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	public function getCompany_byIDDesc(){
		$this->db->from('company');
		$this->db->order_by('ID', 'DESC');
		$query = $this->db->get();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	/*public function getPaymentOther($companyID, $subdomainCompID){
		$this->db->select('a.*, c.companyID as subdomainCompID, c.ID as subdomainAcctID');
		$this->db->where('a.companyID', $companyID);
		$this->db->from(SUB_DB.'demodb.comp_payment_others a');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct b', 'a.accountID = b.ID', 'LEFT');
		$this->db->join(SUB_DB.'tekun1db.comp_chart_of_acct c', 'c.acctCode = b.acctCode', 'LEFT');
		$this->db->join(SUB_DB.'tekun1db.comp_bank d', 'd.linkAccountID = c.ID', 'LEFT');
		$query = $this->db->get();
		echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}*/
	
	public function getPaymentOther($companyID){
		$this->db->select('a.*, b.ID as chartID, b.acctCode as chartCode, c.linkAccountID as chartBankID, d.acctCode as chartBankCode');
		$this->db->where('a.companyID', $companyID);
		$this->db->from(SUB_DB.'demodb.comp_payment_others a');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct b', 'a.accountID = b.ID', 'LEFT');
		$this->db->join(SUB_DB.'demodb.comp_bank c', 'c.ID = a.bankId', 'LEFT');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct d', 'c.linkAccountID = d.ID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	/*public function getPaymentOther_without_item($companyID, $subdomainCompID){
		$this->db->select('a.*, c.companyID as subdomainCompID, c.ID as subdomainAcctID, d.itemdescription as subdomainItemDesc, e.ID as subdomainBankID');
		$this->db->where('a.companyID', $companyID);
		$this->db->from(SUB_DB.'demodb.comp_payment_others a');
		$this->db->join(SUB_DB.'demodb.comp_payment_others_without_item d', 'a.ID = d.otherPaymentID', 'LEFT');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct b', 'd.accountID = b.ID', 'LEFT');
		$this->db->join(SUB_DB.'tekun1db.comp_chart_of_acct c', 'c.acctCode = b.acctCode', 'LEFT');
		$this->db->join(SUB_DB.'tekun1db.comp_bank e', 'e.linkAccountID = c.ID', 'LEFT');
		$query = $this->db->get();
		echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}*/
	
	public function getPaymentOther_without_item($paymentOthersID){
		$this->db->select('a.*, b.acctCode as chartCode');
		$this->db->where('a.otherPaymentID', $paymentOthersID);
		$this->db->from(SUB_DB.'demodb.comp_payment_others_without_item a');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct b', 'a.accountID = b.ID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	
	public function getReceivePaymentOther($companyID){
		$this->db->select('a.*, c.acctCode as chartBankCode');
		$this->db->where('a.companyID', $companyID);
		$this->db->from(SUB_DB.'demodb.comp_receive_payment_others a');
		$this->db->join(SUB_DB.'demodb.comp_bank b', 'a.bankID = b.ID', 'LEFT');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct c', 'c.ID = b.linkAccountID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	public function getReceivePaymentOther_detail($receivePaymentOthersID){
		$this->db->select('a.*, b.acctCode as chartCode');
		$this->db->where('a.receivePaymentOthersID', $receivePaymentOthersID);
		$this->db->from(SUB_DB.'demodb.comp_receive_payment_others_detail a');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct b', 'a.accountNoID = b.ID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	
	public function getCompJournal($companyID){
		$this->db->where('companyID', $companyID);
		$this->db->from(SUB_DB.'demodb.comp_journal');
		$query = $this->db->get();
		//echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	public function getCompJournal_detail($tname, $companyID, $newCompID){
		$this->db->select('a.*, c.ID as chartAcctID');
		$this->db->where('a.companyID', $companyID);
		$this->db->from(SUB_DB.'demodb.comp_journal_detail a');
		$this->db->join(SUB_DB.'demodb.comp_chart_of_acct b', 'a.acctCode = b.ID', 'LEFT');
		$this->db->join($tname.' c', 'b.acctCode = c.acctCode and c.companyID = '.$newCompID, 'LEFT');
		$query = $this->db->get();
		echo $this->db->last_query();
        if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	
	
	public function getChartOfAcctNewDB($tname, $companyID, $acctCode){
		$this->db->where('companyID', $companyID);
		$this->db->where('acctCode', $acctCode);
		$this->db->from($tname);
		$query = $this->db->get();
		//echo $this->db->last_query();
		if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	
	public function getChartOfAcctBankNewDB($tname, $companyID, $acctCode){
		$this->db->select('a.*, b.ID as chartbankID');
		$this->db->where('a.companyID', $companyID);
		$this->db->where('a.acctCode', $acctCode);
		$this->db->from($tname.'.comp_chart_of_acct a');
		$this->db->join($tname.'.comp_bank b', 'a.ID = b.linkAccountID', 'LEFT');
		$query = $this->db->get();
		//echo $this->db->last_query();
		if($query->num_rows() >= 1){
            return $query->result();
        } else {
            return FALSE;
        }
	}
	

}
