<?php
class classAccounts
{
	private $organizationID;
	
	private $accountHeadID;
	private $accountHeadName;
	private $accountHeadNumber;
	private $accountHeadIdentifier;
	private $accountHeadBlocked;
	
	private $accountID;
	private $accountName;
	private $accountNumber;
	private $accountIdentifier;
	private $accountType;
	private $accountBlocked;
	private $accountHospShare;
	
	######################################### SET FUNCTION ###################################################
	
	public function setOrganizationID($organizationID)
	{
		if($organizationID!="")
		{
			$this->organizationID=$organizationID;	
		}
		else
		{
			throw new MyException(122100);
			
		}
	}
	
	public function setAccountHeadID($accountHeadID)
	{
		if($accountHeadID!="")
		{
			$this->accountHeadID=$accountHeadID;	
		}
		else
		{
			throw new MyException(122101);
			
		}
	}
	public function setAccountHeadName($accountHeadName)
	{
		if($accountHeadName!="")
		{
			$this->accountHeadName=$accountHeadName;	
		}
		else
		{
			throw new MyException(122102);
			
		}
	}
	public function setAccountHeadNumber($accountHeadNumber)
	{
		if($accountHeadNumber!="")
		{
			$this->accountHeadNumber=$accountHeadNumber;	
		}
		else
		{
			throw new MyException(122103);
			
		}
	}
	public function setAccountHeadIdentifier($accountHeadIdentifier)
	{
		if($accountHeadIdentifier!="")
		{
			$this->accountHeadIdentifier=$accountHeadIdentifier;	
		}
		else
		{
			throw new MyException(122104);
			
		}
	}
	public function setAccountHeadBlocked($accountHeadBlocked)
	{
		if($accountHeadBlocked!="")
		{
			$this->accountHeadBlocked=$accountHeadBlocked;	
		}
		else
		{
			//throw new MyException(122105);
			
		}
	}
	
	public function setAccountID($accountID)
	{
		if($accountID!="")
		{
			$this->accountID=$accountID;	
		}
		else
		{
			throw new MyException(122106);
			
		}
	}
	public function setAccountName($accountName)
	{
		if($accountName!="")
		{
			$this->accountName=$accountName;	
		}
		else
		{
			throw new MyException(122107);
			
		}
	}
	public function setAccountNumber($accountNumber)
	{
		if($accountName!="")
		{
			$this->accountName=$accountName;	
		}
		else
		{
			throw new MyException(122108);
			
		}
	}
	public function setAccountIdentifier($accountIdentifier)
	{
		if($accountName!="")
		{
			$this->accountName=$accountName;	
		}
		else
		{
			throw new MyException(122109);
			
		}
	}
	public function setAccountType($accountType)
	{
		if($accountType!="")
		{
			$this->accountType=$accountType;	
		}
		else
		{
			throw new MyException(122110);
			
		}
	}
	public function setAccountBlocked($accountBlocked)
	{
		if($accountName!="")
		{
			$this->accountName=$accountName;	
		}
		else
		{
			//throw new MyException(122111);
			
		}
	}
	public function setAccountHospShare($accountHospShare)
	{
		if($accountHospShare!="")
		{
			$this->accountHospShare=$accountHospShare;	
		}
		else
		{
			throw new MyException(122112);
			
		}
	}
	
	
	######################################### GET FUNCTION ###################################################
	
	public function getOrganizationID()
	{
		if($this->organizationID!="")
		{
			return $this->organizationID;	
		}
		else
		{
			throw new MyException(122113);
			
		}
	}
	
	public function getAccountHeadID()
	{
		if($this->accountHeadID!="")
		{
			return $this->accountHeadID;	
		}
		else
		{
			throw new MyException(122114);
			
		}
	}
	public function getAccountHeadName()
	{
		if($this->accountHeadName!="")
		{
			return $this->accountHeadName;	
		}
		else
		{
			throw new MyException(122115);
			
		}
	}
	public function getAccountHeadNumber()
	{
		if($this->accountHeadNumber!="")
		{
			return $this->accountHeadNumber;	
		}
		else
		{
			throw new MyException(122116);
			
		}
	}
	public function getAccountHeadIdentifier()
	{
		if($this->accountHeadIdentifier!="")
		{
			return $this->accountHeadIdentifier;	
		}
		else
		{
			throw new MyException(122117);
			
		}
	}
	public function getAccountHeadBlocked()
	{
		if($this->accountHeadBlocked!="")
		{
			return $this->accountHeadBlocked;	
		}
		else
		{
			//throw new MyException(122118);
			
		}
	}
	
	public function getAccountID()
	{
		if($this->accountID!="")
		{
			return $this->accountID;	
		}
		else
		{
			throw new MyException(122119);
			
		}
	}
	public function getAccountName()
	{
		if($this->accountName!="")
		{
			return $this->accountName;	
		}
		else
		{
			throw new MyException(122120);
			
		}
	}
	public function getAccountNumber()
	{
		if($this->accountNumber!="")
		{
			return $this->accountNumber;	
		}
		else
		{
			throw new MyException(122121);
			
		}
	}
	public function getAccountIdentifier()
	{
		if($this->accountIdentifier!="")
		{
			return $this->accountIdentifier;	
		}
		else
		{
			throw new MyException(122122);
			
		}
	}
	public function getAccountType()
	{
		if($this->accountType!="")
		{
			return $this->accountType;	
		}
		else
		{
			throw new MyException(122123);
			
		}
	}
	public function getAccountBlocked()
	{
		if($this->accountBlocked!="")
		{
			return $this->accountBlocked;	
		}
		else
		{
			//throw new MyException(122124);
			
		}
	}
	public function getAccountHospShare()
	{
		if($this->accountHospShare!="")
		{
			return $this->accountHospShare;	
		}
		else
		{
			throw new MyException(122125);
			
		}
	}
	
	######################################### MAIN CHECK FUNCTION ###################################################
		
	function checkAccHeadNumberExist($org_id,$acchead_no)
	{
		$query = "SELECT acchead_id FROM accheads_default WHERE acchead_no = '$acchead_no' AND org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($array[0]['acchead_id'])&&$array[0]['acchead_id']!='')
		{
			throw new MyException(122126);
		}
		else
		{
			return 1;
		}
		
	}
	
	function checkAccountNumberExist($org_id,$account_no)
	{
		$query = "SELECT account_id FROM accounts_default WHERE account_no = '$account_no' AND org_id = '$org_id'";
		$queryOb = new classQueryGlobal;
		$array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		if(isset($array[0]['account_id'])&&$array[0]['account_id']!='')
		{
			throw new MyException(122127);
		}
		else
		{
			return 1;
		}
		
	}
	
	######################################### MAIN INSERT FUNCTION ###################################################	
	
	function insertAccountHeads()
	{
		if($this->checkAccHeadNumberExist($this->getOrganizationID(),$this->getAccountHeadNumber()))
		{
			$acchead_name = $this->getAccountHeadName();
			$acchead_no = $this->getAccountHeadNumber();
			$acchead_identifier = $this->getAccountHeadIdentifier();
			$acchead_blocked = $this->getAccountHeadBlocked();
			$org_id = $this->getOrganizationID();
		
		
			$query = "INSERT INTO accheads_default 
			(
				acchead_name,
				acchead_no,
				acchead_identifier,
				acchead_blocked,
				org_id
			) 
			VALUES 
			(
				'$acchead_name',
				'$acchead_no',
				'$acchead_identifier',
				'$acchead_blocked',
				'$org_id'
			)";
			$queryOb = new classQueryGlobal;
			$this->accountHeadID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		}
		
	}
	
	function insertAccounts()
	{
		if($this->checkAccountNumberExist($this->getOrganizationID(),$this->getAccountNumber()))
		{
			$account_no = $this->getAccountNumber();
			$account_name = $this->getAccountName();
			$account_identifier = $this->getAccountIdentifier();
			$account_blocked = $this->getAccountBlocked();
			$account_type = $this->getAccountType();
			$acchead_id = $this->getAccountHeadID();
			$org_id = $this->getOrganizationID();
		
		
			$query = "INSERT INTO accounts_default 
			(
				account_no,
				account_name,
				account_identifier,
				account_blocked,
				account_type,
				acchead_id,
				org_id
			) 
			VALUES 
			(
				'$account_no',
				'$account_name',
				'$account_identifier',
				'$account_blocked',
				'$account_type',
				'$acchead_id',
				'$org_id'
			)";
			$queryOb = new classQueryGlobal;
			$this->accountID = $queryOb->check_fetch_query($query,'insert',$server="",$debug_mode="off");
		}
		
	}
	
	######################################### MAIN UPDATE FUNCTION ###################################################	
	
	function updateAccountHeads()
	{
		$acchead_name = $this->getAccountHeadName();
		$acchead_no = $this->getAccountHeadNumber();
		$acchead_identifier = $this->getAccountHeadIdentifier();
		$acchead_blocked = $this->getAccountHeadBlocked();
		$acchead_id = $this->getAccountHeadID();
		$org_id = $this->getOrganizationID();
		
		$query = "UPDATE accheads_default SET 
		acchead_name = '$acchead_name' ,
		acchead_no = '$acchead_no' ,
		acchead_identifier = '$acchead_identifier' ,
		acchead_blocked = '$acchead_blocked' ,
		org_id = '$org_id'
		WHERE acchead_id = '$acchead_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	function updateAccounts()
	{
		$account_no = $this->getAccountNumber();
		$account_name = $this->getAccountName();
		$account_identifier = $this->getAccountIdentifier();
		$account_blocked = $this->getAccountBlocked();
		$account_type = $this->getAccountType();
		$account_id = $this->getAccountID();
		$org_id = $this->getOrganizationID();
		
		$query = "UPDATE accounts_default SET 
		account_no = '$account_no' ,
		account_name = '$account_name' ,
		account_identifier = '$account_identifier' ,
		account_blocked = '$account_blocked' ,
		account_type = '$account_type' ,
		org_id = '$org_id'
		WHERE account_id = '$account_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'update',$server="",$debug_mode="off");
	}
	
	######################################### MAIN DELETE FUNCTION ###################################################	
	
	function deleteAccountHeads()
	{
		$acchead_id = $this->getAccountHeadID();		
		$query = "DELETE FROM accheads_default WHERE acchead_id = '$acchead_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	function deleteAccounts()
	{
		$account_id = $this->getAccountID();	
		$query = "DELETE FROM accounts_default WHERE account_id = '$account_id'";
		$queryOb = new classQueryGlobal;
		$queryOb->check_fetch_query($query,'delete',$server="",$debug_mode="off");
	}
	
	######################################### MAIN SELECT FUNCTION ###################################################
	
	function retrieveAccountHeadByID()
	{
		$acchead_id = $this->getAccountHeadID();	
		$query = "SELECT * FROM accheads_default WHERE acchead_id = '$acchead_id'";
		$queryOb = new classQueryGlobal;
		$accounthead_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		return $accounthead_array[0];
	}
	
	function retrieveAccountByHeadID()
	{
		$acchead_id = $this->getAccountHeadID();
		$query = "SELECT * FROM accounts_default WHERE acchead_id = '$acchead_id'";
		$queryOb = new classQueryGlobal;
		$account_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		return $account_array[0];
	}
	
	function retrieveAccountByID()
	{
		$account_id = $this->getAccountID();	
		$query = "SELECT * FROM accounts_default WHERE account_id = '$account_id'";
		$queryOb = new classQueryGlobal;
		$account_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		return $account_array[0];
	}
	
	function retrieveAccountHeadsAndAccounts($org_id,$account_no=NULL)
	{
		$query = "SELECT 
		DISTINCT accounts_default.acchead_id , 
		GROUP_CONCAT(DISTINCT accounts_default.account_id ORDER BY 	account_no) AS child_id_list ,
		accheads_default.acchead_id
		FROM accounts_default 
		RIGHT JOIN accheads_default ON accounts_default.acchead_id = accheads_default.acchead_id"; 
		$query .= " WHERE accheads_default.org_id = '$org_id'";
		
		if(!is_null($account_no))
		{
			$query .= " AND account_no = '$account_no'";
		}
		
		$query .= " GROUP BY accheads_default.acchead_id ORDER BY acchead_no ASC";
		
		$queryOb = new classQueryGlobal;
		$accounts_array = $queryOb->check_fetch_query($query,'select',$server="",$debug_mode="off");
		
		$acchead_accounts_array = array();
		if(isset($accounts_array[0]['acchead_id']))
		{
			for($wb=0;$wb<count($accounts_array);$wb++)
			{
				$this->accountHeadID = $accounts_array[$wb]['acchead_id'];
				$acchead_accounts_array[$wb] = $this->retrieveAccountHeadByID();
				
				if($accounts_array[$wb]['child_id_list']!='')
				{
					$account_ids = explode(',',$accounts_array[$wb]['child_id_list']);	
					foreach($account_ids as $account_id)
					{
						$this->accountID = 	$account_id;
						$acchead_accounts_array[$wb]['account_details'][] = $this->retrieveAccountByID();	
					}
				}
				else
				{
					$acchead_accounts_array[$wb]['account_details'] = NULL;
				}
			}
		}
		
		/*echo '<pre>';
		print_r($acchead_accounts_array);
		echo '</pre>';	
		exit;*/
					
		return $acchead_accounts_array;
		
	}	
}