
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class AccountsThirdLevelAccount extends MX_Controller

	{
	public

	function __construct()
		{
		parent::__construct();
		//$this->utils->allow();
		$this->load->model('Accounts_model_thirdlevelaccount');
		$this->load->library('form_validation');
		$this->load->helper('url');
		

		}

	public

	function index()
		{
		

		
		$data['listSecondLevelAccountName'] = $this->Accounts_model_thirdlevelaccount->listSecondLevelAccountName();		
		$data['listPrimaryAccountName'] = $this->Accounts_model_thirdlevelaccount->listPrimaryAccountName();
		
		$data['listThirdLevelAccount'] = $this->Accounts_model_thirdlevelaccount->listThirdLevelAccount();
		
		$data['accountClass'] = $this->Accounts_model_thirdlevelaccount->accountClass();
		
		$data['view'] = 'Accounts/AccountsViewAddThirdlevelaccount';
		$this->load->view('master', $data);

		// echo "index 2<br/>";

		}

		function getprimaryAccount()
		{
		$primaryAccount = $this->Accounts_model_thirdlevelaccount->getprimaryAccount();
		
		echo '<option value="">Select </option>';
		foreach($primaryAccount->result_array() as $pName)
			{
			echo '<option  value="' . $pName['primaryAccountId'] . '">' . $pName['primaryAccountName'] . '</option>';
			}
		}
		
		function getprimaryAccountEdit($thirdLevelAccountId)
		{
		$primaryAccount= $this->Accounts_model_thirdlevelaccount->getprimaryAccountEdit($thirdLevelAccountId);
		
		$query = $this->Accounts_model_thirdlevelaccount->listThirdLevelAccountByThirdLevelAccountId($thirdLevelAccountId); 
			foreach($query->result() as $TLA){ 
			
		$lpan='';
		 foreach($primaryAccount->result_array() as $r){
			  if($TLA->primaryAccountId==$r['primaryAccountId'])
			  {$lpan=$r['primaryAccountName'];  break;}}
			 
		echo '<option value="'.$TLA->primaryAccountId.'">'.$lpan.' </option>';
		
		foreach($primaryAccount->result_array() as $pName) 
			{
			echo '<option  value="' . $pName['primaryAccountId'] . '">' . $pName['primaryAccountName'] . '</option>';
			}
			
			}
			
			
		}
		
		function getSecondLevelAccount()
		{
		$secondaryName = $this->Accounts_model_thirdlevelaccount->getSecondLevelAccount();
		
		echo '<option value="">Select </option>';
		foreach($secondaryName->result_array() as $sName)
			{
			echo '<option value="' . $sName['ledgerId'] . '" >' . $sName['ledgerName'] . '</option>';
			}
		}
		
		
		function getSecondLevelAccountEdit($thirdLevelAccountId)
		{
			$secondaryName= $this->Accounts_model_thirdlevelaccount->getSecondLevelAccountEdit($thirdLevelAccountId);
			
			
		
		
		$query = $this->Accounts_model_thirdlevelaccount->listThirdLevelAccountByThirdLevelAccountId($thirdLevelAccountId); 
			foreach($query->result() as $TLA){ 
			
		$lpann='';
		 foreach($secondaryName->result_array() as $r){
			  if($TLA->ledgerId==$r['ledgerId'])
			  {$lpann=$r['ledgerName'];  break;}}
			 
		echo '<option value="'.$TLA->ledgerId.'">'.$lpann.' </option>';
		
		
		foreach($secondaryName->result_array() as $sName)
			{
			echo '<option value="' . $sName['ledgerId'] . '" >' . $sName['ledgerName'] . '</option>';
			}

		}
		}
		
	function insertThirdLevelAccount()
		{
		//$data['primaryAccountType'] = $this->input->post('accountType');
		$data['primaryAccountId'] = $this->input->post('primaryAccount');
		$data['ledgerId'] = $this->input->post('secondlevel');


		$data['thirdLevelAccountName'] = $this->input->post('thirdLevelAccountName');
       // $data['code'] = $this->input->post('code');
		$ledgerId=$data['ledgerId'];
		$thirdLevelAccountName=$data['thirdLevelAccountName'];

		$this->load->model('Accounts_model_thirdlevelaccount');
		
		$this->form_validation->set_rules('accountType', 'AccountType', 'required');
		$this->form_validation->set_rules('primaryAccount', 'primaryAccountName', 'required');
		$this->form_validation->set_rules('secondlevel', 'secondLevelAccountName', 'required');
		
		$this->form_validation->set_rules('thirdLevelAccountName', 'thirdLevelAccountName', 'required|min_length[4]|max_length[20]');
		if ($this->form_validation->run() != FALSE)
			{
				 $getThirdLevelAccountNameRowCount = $this->Accounts_model_thirdlevelaccount->getThirdLevelAccountNameRowCount($ledgerId,$thirdLevelAccountName);
				 
				 if ($getThirdLevelAccountNameRowCount < 1)
			{
			
				$this->Accounts_model_thirdlevelaccount->insertThirdLevelAccount($data);
				
		       }
		     else{ 
			   $message="The Third Level Account Name Already Exist";
			    $this->utils->actionMessage($message);	
			  /*echo "<script> alert('The  Third Level Account Name already exist.') </script>";*/
			 }
			
			
			
			}
			
		$this->index();
		}



	function deleteThirdLevelAccount($thirdLevelAccountId)
		{
		$this->Accounts_model_thirdlevelaccount->deleteThirdLevelAccount($thirdLevelAccountId);
		$this->index();
		}
		
		
		function editThirdLevelAccount($thirdLevelAccountId)
		{
		//$id = $this->uri->segment(3);
		$this->load->model('Accounts_model_thirdlevelaccount');
		$data['query'] = $this->Accounts_model_thirdlevelaccount->listThirdLevelAccountByThirdLevelAccountId($thirdLevelAccountId);
		
		$data['listSecondLevelAccountName'] = $this->Accounts_model_thirdlevelaccount->listSecondLevelAccountName();		
		$data['listPrimaryAccountName'] = $this->Accounts_model_thirdlevelaccount->listPrimaryAccountName();
		
		$data['listThirdLevelAccount'] = $this->Accounts_model_thirdlevelaccount->listThirdLevelAccount();
		
		$data['accountClass'] = $this->Accounts_model_thirdlevelaccount->accountClass();
		
		$data['view'] = 'Accounts/AccountsViewEditThirdlevelaccount';
		$this->load->view('master', $data);
		}
		
		
		function doEditThirdLevelAccount()
		{
			
		$primaryAccountId = $this->input->post('primaryAccount');
		$ledgerId = $this->input->post('secondlevel');


		$thirdLevelAccountName = $this->input->post('thirdLevelAccountName');
		$thirdLevelAccountId = $this->input->post('thirdLevelAccountId');	
			
		
		
	$this->form_validation->set_rules('primaryAccount', 'primaryAccountName', 'required');
		$this->form_validation->set_rules('secondlevel', 'secondLevelAccountName', 'required');
		
		$this->form_validation->set_rules('thirdLevelAccountName', 'thirdLevelAccountName', 'required|min_length[4]|max_length[20]');
		
		
		if ($this->form_validation->run() != FALSE)
			{
				  $getThirdLevelAccountNameRowCount = $this->Accounts_model_thirdlevelaccount->getThirdLevelAccountNameRowCount($ledgerId,$thirdLevelAccountName);
				 
				 if ($getThirdLevelAccountNameRowCount < 1)
			{
			
				$this->Accounts_model_thirdlevelaccount->doEditThirdLevelAccount($primaryAccountId, $ledgerId, $thirdLevelAccountName,$thirdLevelAccountId);
				
		       }
		     else{ 
			   $message="The Third Level Account Name Already Exist";
			   $this->utils->actionMessage($message);	
			 /* echo "<script> alert('The  Third Level Account Name already exist.') </script>";*/
			 }
			
			
			
			}
		$this->index();
		
		}

	}

?>
