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

class Glsetting extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('Useraccess');
		$query = $this->Useraccess->useraccess_id_module2('1', 'setup');
		if(!$query){
			echo '<script>alert("Not authorised..");
				window.location.replace("'.base_url().'Error");
				</script>';
		}
		$this->load->model('menu');
	}

// - Accounts	
	public function accmasterlist(){
			$this->load->library('layouts');
			$this->load->helper('form');
			$this->layouts->set_title('Change Management');
			$data['our_company'] = 'this is my company';
			$data['module'] = 'General Ledger';
			$data['title'] = 'Account Master List';
			$this->load->helper('menu');
			$this->load->model('Accmaster');
			$data['datatbls'] = $this->Accmaster->accmaster_all();
			$data['headertbl'] = $this->session->userdata('menuactive');
			$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
			$this->layouts->view('glsetting/accmasterlist', array('latest' => 'sidebar/latest'), $data);
	}

	public function accmaster_save(){
			$this->load->model('Accmaster');
			$fldid = $this->input->post('fldid');
			$fieldname = $this->input->post('fieldname');
			$value = $this->input->post('content');
			$data = array($fieldname=>$value);
		  	$query = $this->Accmaster->accmaster_save($fldid, $data);
			if($query){
				return TRUE;
			}else{
				return FALSE;
			}
	}
	
	public function accmaster_delete(){
			$this->load->model('Accmaster');
			$fldid = $this->input->post('fldid');
		  	$delete_menu = $this->Accmaster->accmaster_delete($fldid);
			if($delete_menu){
				return TRUE;
			}else{
				return FALSE;
			}
	}

/************************Sayem (3-8-2014)*******************/
// Financial year closeing date check
	public function financialYearCloseChk(){
		$date = $this->input->post('date');

		if($date == '')	exit('Failed');
		$this->load->model('Compfinancialyear');
		$status = $this->Compfinancialyear->closeingChk(date('Y-m-d',strtotime($date)));
		echo $status;
		exit();	
	}

// Financial Calendar
	public function financialCalendarCheck_yearperiod()
	{
		$this->load->model('Compfinancialyear');
		$query = $this->Compfinancialyear->financialYear_yearperiod($this->input->post('financialYear'),$this->input->post('period'));
		$startdate = date("d-m-Y", strtotime($query[0]->startDate));
		$enddate = date("d-m-Y", strtotime($query[0]->endDate));
		$ID = $query[0]->ID;
		
		if($query>0){
			print json_encode(array(
				"status"=>"false", 
				"message1"=>$query , 
				"startDate"=>$startdate,
				"endDate"=>$enddate,
				"ID"=>$ID
				));
		}
		else{
			print json_encode(array("status"=>"success", "message2"=>$query));
		}
	}
	
	public function calendar()
	{
	  		if($this->input->post('submit')){
				$this->load->model('Compfinancialyear');
				//var_dump($this->input->post('fcID'));
		  	 if($this->input->post('fcID')==NULL){	
				$query=$this->Compfinancialyear->startEndDateCheck_startDateEndDate();
				if(!$query){
			  		$financialCalendar = array(
			  			'ID'		=> $this->input->post('ID'), 
			  			'companyID'		=> element('compID', $this->session->userdata('logged_in')), 
			  			'financialYear'		=> $this->input->post('financialYear'),
						'period'		=> $this->input->post('period'),
						'startDate'		=> date('Y-m-d', strtotime($this->input->post('startDate'))), 
			  			'endDate'		=> date('Y-m-d', strtotime($this->input->post('endDate'))),
//		  				'createdID'		=> element('userid', $this->session->userdata('logged_in')), 
		  			);
		  			$insert_calendar = $this->Compfinancialyear->financialCalendarInsert($financialCalendar);
					if($insert_calendar){
						echo '<script>alert("Insert Data Success..");
						window.location.replace("financialCalendarList");
						</script>';
					}else{
						echo '<script>alert("Insert Data Failed.");
						history.go(-1);</script>';
					}
				}
				else{
					echo '<script>alert("You enter the wrong period of date.");
						history.go(-1);</script>';
				} // if !$query
		  		}
				else
				{
					$ID = $this->input->post('fcID');
		  			$financialCalendar = array(
			  			'financialYear'		=> $this->input->post('financialYear'),
						'period'		=> $this->input->post('period'),
						'startDate'		=> date('Y-m-d', strtotime($this->input->post('startDate'))), 
			  			'endDate'		=> date('Y-m-d', strtotime($this->input->post('endDate'))),
						'status'		=> $this->input->post('status'),
//		  				'createdID'		=> element('userid', $this->session->userdata('logged_in')), 
		  			);
					//$this->load->model(company/Company_info_model);
					$this->load->model('Compfinancialyear');
		  			$update_calendar = $this->Compfinancialyear->financialCalendar_edit($financialCalendar, $ID);
					if($update_calendar){
						echo '<script>alert("Update Success..");
						window.location.replace("financialCalendarList");
						</script>';
					}
					else{
						echo '<script>alert("Update Failed.");
						history.go(-1);</script>';
					}
		  		}
	  		} 
			else 
			{     
				$id = isset($_REQUEST['id'])?$_REQUEST['id']:"";
				$this->load->library('layouts');
				$this->lang->load("companyFinancialCalendar",$this->session->userdata('language'));
				$this->load->model('Compfinancialyear');
				if($id){
					$dataum = $this->Compfinancialyear->financialCalendar_id($id);
					$data['fycmaster'] = $dataum[0];
				}else{
					$data['fycmaster'] = NULL;
				}
				$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
					
				$this->layouts->view('gl/setups/companyFinancialCalendar', array('latest' => 'sidebar/latest'), $data);
			}
	}
	
	public function dateSearch(){
		$this->load->model('Compfinancialyear');
		
		$period = $this->input->post('period');
		$financialYear = $this->input->post('financialYear');	
		$startDate = $this->input->post('startDate');
		$endDate = $this->input->post('endDate');
		$startPeriod = $this->input->post('startPeriod');
		
			$addmonth = $period -1;
			$startPeriod = 1;
		    $startDate = date('Y-m-01', strtotime($startDate));
		    $endDate = date('Y-m-t', strtotime($startDate. '+'. $addmonth . ' month'));
			print json_encode(array("status" => "success", "startDate" => date('d-m-Y', strtotime($startDate)), "endDate" => date('d-m-Y', strtotime($endDate)), "startPeriod" => $startPeriod, "period" => $period));
       
	}
	
	public function checkYear(){
		$this->load->model('Compfinancialyear');
		
		$financialYear = $this->input->post('financialYear');
		$period = $this->input->post('period');
		
		$query = $this->Compfinancialyear->financialCalendarCheckYear($financialYear);
		//var_dump($query);
		
		if($query>0){
			$query1 = $this->Compfinancialyear->financialYear_checkperiod($financialYear);
			if ($query1->period > 0){
				$startPeriod = $query1->period + 1;
		    	$startDate = date('Y-m-01', strtotime($query1->startDate. ' +1 months'));
		    	$endDate = date('Y-m-t', strtotime($query1->startDate. ' +'.$period.' months'));
			}
			print json_encode(array(
				"status"=>"success", 
				"message"=>$query , 
				"financialYear"=>$financialYear,
				"startDate" => date('d-m-Y', strtotime($startDate)), 
				"endDate" => date('d-m-Y', strtotime($endDate)), 
				"startPeriod" => $startPeriod
				/*"period"=>$period,
				"startDate"=>$startDate,
				"endDate"=>$endDate,*/
				));
		}
		else{
			$startPeriod = 1;
		    $startDate = date('Y-m-01');
		    $endDate = date('Y-m-t');
			print json_encode(array(
				"status"=>"failed", 
				"message2"=>"Wrong Year",
				"startDate" => date('d-m-Y', strtotime($startDate)), 
				"endDate" => date('d-m-Y', strtotime($endDate)), 
				"startPeriod" => $startPeriod
				));
		}
       
	}
	
	public function checkYearPeriodStartEndDate(){
		$this->load->model('Compfinancialyear');
		
		$financialYear = $this->input->post('financialYear');
		$period = $this->input->post('period');
		$startDate = $this->input->post('startDate');
		$endDate = $this->input->post('endDate');
		$check = FALSE;
		
		$check = $this->Compfinancialyear->financialYear_yearperioddate($financialYear, $period, $startDate, $endDate, $check);
		//var_dump($query);
		
		if($check == true){
			print json_encode(array(
				"status"=>"success", 
				"message"=>$check ,
				"startDate"=>$startDate ,
				"endDate"=>$endDate ,
				"period"=>$period ,
				));
		}
		else{
			print json_encode(array("status"=>"fail", "message2"=>"false"));
		}
       
	}
	
	public function createCalendar()
	{
	  		if($this->input->post('submit')){
				$this->load->model('Compfinancialyear');
				
				$startDate=$this->input->post('startDate');
				$endDate=$this->input->post('endDate');
				$check = $this->Compfinancialyear->financialCalendarCheckDate($startDate, $endDate);
				
				$period = $this->input->post('period');
				$startPeriod = $this->input->post('startPeriod');	
				if(!$check){
					$i=$startPeriod;
					$m=0;
					while($i<$startPeriod+$period){
						$financialCalendar = array(
							'ID'			=> $this->input->post('ID'), 
							'companyID'		=> element('compID', $this->session->userdata('logged_in')), 
							'financialYear'	=> $this->input->post('financialYear'),
							'period'		=> $startPeriod + $m,
							'startDate'		=> date('Y-m-01', strtotime($this->input->post('startDate').'+'. $m .' month')), 
							'endDate'		=> date('Y-m-t', strtotime($this->input->post('startDate').'+'. $m .' month')),
			  				'createdBy'		=> element('role', $this->session->userdata('logged_in')), 
						);
						$insert_calendar = $this->Compfinancialyear->financialCalendarInsert($financialCalendar);
						$i++;
						$m++;
					}//end for
					if($insert_calendar){
						echo '<script>alert("Insert Data Success..");
						window.location.replace("financialCalendarList");
						</script>';
					}else{
						echo '<script>alert("Insert Data Failed.");
						history.go(-1);</script>';
					}
				}
				else{
					echo '<script>alert("You enter the wrong period of date.");
						history.go(-1);</script>';
				} // if $check
	  		} //if submit
			else 
			{     
//				$id = isset($_REQUEST['id'])?$_REQUEST['id']:"";
				$this->load->library('layouts');
				$this->lang->load("companyFinancialCalendar",$this->session->userdata('language'));
				$this->load->model('Compfinancialyear');
				$data['datatbls'] = $this->Compfinancialyear->financialCalendarCheckYear(date("Y"));
				if($data['datatbls']){
					$data['fycmaster'] = $this->Compfinancialyear->financialYear_checkperiod(date("Y"));
				}else{
					$data['fycmaster'] = NULL;
				}
				$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
					
				$this->layouts->view('gl/setups/createCompanyFinancialCalendar', array('latest' => 'sidebar/latest'), $data);
			}
	}

/* Create financial calendar one by one */	
	public function addCalendar()
	{
	  		if($this->input->post('submit')){
				$this->load->model('Compfinancialyear');
				
				$startDate=$this->input->post('startDate');
				$endDate=$this->input->post('endDate');
				$check = FALSE;
				$check = $this->Compfinancialyear->financialCalendarCheckDate($startDate, $endDate, $check);
				
				$period = $this->input->post('period');	
//				$query=$this->Compfinancialyear->startEndDateCheck_startDateEndDate();

				if($check == true){
						$financialCalendar = array(
							'ID'			=> $this->input->post('ID'), 
							'companyID'		=> element('compID', $this->session->userdata('logged_in')), 
							'financialYear'	=> $this->input->post('financialYear'),
							'period'		=> $i + 1,
							'startDate'		=> date('Y-m-01', strtotime($this->input->post('startDate').'+'. $i .' months')), 
							'endDate'		=> date('Y-m-t', strtotime($this->input->post('startDate').'+'. $i .' months')),
	//		  				'createdID'		=> element('userid', $this->session->userdata('logged_in')), 
						);
						$insert_calendar = $this->Compfinancialyear->financialCalendarInsert($financialCalendar);
					if($insert_calendar){
						echo '<script>alert("Insert Data Success..");
						window.location.replace("financialCalendarList");
						</script>';
					}else{
						echo '<script>alert("Insert Data Failed.");
						history.go(-1);</script>';
					}
				}
				else{
					echo '<script>alert("You enter the wrong period of date.");
						history.go(-1);</script>';
				} // if $check
	  		} //if submit
			else 
			{     
				$id = isset($_REQUEST['id'])?$_REQUEST['id']:"";
				$this->load->library('layouts');
				$this->lang->load("companyFinancialCalendar",$this->session->userdata('language'));
				$this->load->model('Compfinancialyear');
				$data['datatbls'] = $this->Compfinancialyear->financialCalendar_all();
				if($id){
					$dataum = $this->Compfinancialyear->financialCalendar_id($id);
					$data['fycmaster'] = $dataum[0];
				}else{
					$data['fycmaster'] = NULL;
				}
				$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
					
				$this->layouts->view('gl/setups/addCompanyFinancialCalendar', array('latest' => 'sidebar/latest'), $data);
			}
	}
	
	public function financialCalendarDelete(){
			$this->load->model('Compfinancialyear');
		  	$deleteuser = array('is_delete' => '1');
			$ID = isset($_REQUEST['ID'])?$_REQUEST['ID']:"";
			
		  	$delete_user = $this->Compfinancialyear->financialCalendar_delete($deleteuser, $ID);
			if($delete_user){
						echo '<script>alert("Delete Success..");
						window.location.replace("financialCalendarList");
						</script>';
			}else{
				return FALSE;
			}
	}
	public function financialCalendarList(){
			$this->load->library('layouts');
			$this->lang->load("companyFinancialCalendar",$this->session->userdata('language'));
			$this->load->model('Compfinancialyear');
			$data['datatbls'] = $this->Compfinancialyear->financialCalendar_all();
			$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
			$this->layouts->view('gl/setups/companyFinancialCalendarlist', array('latest' => 'sidebar/latest'), $data);
	}
	
// Chart of accounts	
	public function chartOfAcct()
	{
		if($this->input->post('submit')){
			$this->load->model('CompanyChartAcct');
			if($this->input->post('acctCodeID')==NULL){
				$accountCode = array(
			  			'ID'		=> $this->input->post('ID'), 
			  			'companyID'		=> element('compID', $this->session->userdata('logged_in')), 
						'acctGroupID'	=> $this->input->post('acctGroupID'),
						'acctCode'		=> preg_replace('/-|:/', null, $this->input->post('acctCode')),
						'acctName'		=> $this->input->post('acctName'),
		  				'createdBy'		=> element('userid', $this->session->userdata('logged_in')), 
		  				'createdTS'		=> $this->input->post('order_time') 
		  			);
					
				$insert_acctCode = $this->CompanyChartAcct->chartAcctInsert($accountCode);
				if($insert_acctCode){
					echo '<script>alert("Insert Data Success..");
					window.location.replace("chartOfAcctList");
					</script>';
				}else{
					echo '<script>alert("Insert Data Failed.");
					history.go(-1);</script>';
				}
			}else{
				$ID = $this->input->post('acctCodeID');
				$accountCode = array(
					'companyID'		=> element('compID', $this->session->userdata('logged_in')),
					'acctGroupID'	=> $this->input->post('acctGroupID'),
					'acctCode'		=> preg_replace('/-|:/', null, $this->input->post('acctCode')),
					'acctName'		=> $this->input->post('acctName'),
					'updatedBy'		=> element('userid', $this->session->userdata('logged_in')), 
				);
				//$this->load->model(company/Company_info_model);
				$this->load->model('CompanyChartAcct');
				$update_acctCode = $this->CompanyChartAcct->chartAcct_edit($accountCode, $ID);
				if($update_acctCode){
					echo '<script>alert("Update Success..");
					window.location.replace("chartOfAcctList");
					</script>';
				}
				else{
					echo '<script>alert("Update Failed.");
					history.go(-1);</script>';
				}
		  	}
	  	} 
		else 
			{    
				$id = isset($_REQUEST['id'])?$_REQUEST['id']:($this->uri->segment(3)?$this->uri->segment(3):"");
				$this->load->library('layouts');
				$this->lang->load("chartofaccount",$this->session->userdata('language'));
				$this->lang->load("pnl",$this->session->userdata('language'));
				$this->lang->load("bs",$this->session->userdata('language'));
				$this->load->model('CompAcctGroup_model');
				$data['accGroup'] = $this->CompAcctGroup_model->companyacctgroup_all2();
//				var_dump($data['accGroup']);
				$this->load->model('CompanyChartAcct');
				if($id){
					$dataum = $this->CompanyChartAcct->chartAcct_id($id);
					//var_dump($dataum[0]);
					$data['acmaster'] = $dataum[0];
				}else{
					$data['acmaster'] = NULL;
				}
//				echo $this->db->last_query(); 
//				var_dump($data);
				$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
					$this->layouts->view('gl/setups/companyChartAcct', array('latest' => 'sidebar/latest'), $data);
				
			}
	}
	
	public function chartOfAcctList(){
/*			$this->load->library('layouts');
			$this->load->model('CompanyChartAcct');
			$this->lang->load("chartofaccount",$this->session->userdata('language'));
			$data['datatbls'] = $this->CompanyChartAcct->chartAcct_all();
			$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
			$this->layouts->view('gl/setups/companyChartAcctList', array('latest' => 'sidebar/latest'), $data);
*/
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('Chartofaccount');
		$this->load->model('company/company_info_model');
		$companyID = element('compID', $this->session->userdata('logged_in'));
		$companyName = element('compName', $this->session->userdata('logged_in'));
//		$companyName = $this->company_info_model->companymaster_id($companyID);
//		$industry = $this->company_info_model->industry_id($companyID);
		//var_dump($companyName[0]);
		$data['companyName']= $companyName; 
		//Check wheather show dropdown or not
		$data['companyID']= $companyID ;
		$data['isShowDropdown'] = $this->Chartofaccount->isShowDropdown($companyID);
//		$data['isShowDropdown'] = $this->company_info_model->company_groupChartOfAccount_byCompanyID($companyID);
		$data['balance'] = $this->Chartofaccount->getInfo($companyID,0);
		$data['profitNloss'] = $this->Chartofaccount->getInfo($companyID,1);
		echo $this->db->last_query(); 
		if($data['isShowDropdown']){
			//Gether data for industry in business selector
			$data['industryInfo'] = $this->Chartofaccount->getdata($companyID);
		}
		$this->lang->load("comp_chartofaccount",$this->session->userdata('language'));
		$this->lang->load("acctGroup",$this->session->userdata('language'));
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('masterfile/chartofaccount', array('latest' => 'sidebar/latest'), $data);


}

public function balancesheetList_print($mode = true) 
	{
		$this->load->model('Chartofaccount');
		$this->load->model('company/company_info_model');
		
		$this->lang->load("comp_chartofaccount",$this->session->userdata('language'));
		$this->lang->load("acctGroup",$this->session->userdata('language'));
		$companyID = element('compID', $this->session->userdata('logged_in'));
		$companyName = element('compName', $this->session->userdata('logged_in'));
		
		$data['companyName']= $companyName;
		$data['companyID']= $companyID ;
		
		$data['balance'] = $this->Chartofaccount->getInfo($companyID,0);
		//$supplierInfo = $this->Supplier_model->suppliermaster_id($data['transInfo'][0]->supplierID);
		//echo $this->db->last_query();
		//var_dump($data['datatbls2'][0]);           
             //$headerData['rightContentHeader'] = "Name supplier/ ot";
             $headerData['rptTitle'] = $this->lang->line("balanceSheet");
             /*$headerData['leftContent'] = array(
												"<b>".$data['transInfo'][0]->customerName."</b>",
												$data['transInfo'][0]->customerCompanyNo,
												$data['transInfo'][0]->line1,
												$data['transInfo'][0]->line2,
												$data['transInfo'][0]->line3,
												$data['transInfo'][0]->customerPostCode.' '.$data['transInfo'][0]->city
											);*/
			 /*$headerData['rightContent'] = array(
												//"<b>".$this->lang->line("formNo").":".$data['transInfo'][0]->creditNoteNo."</b>",
												$this->lang->line("from")." ".date('d-m-Y', strtotime($dateFrom))." ".$this->lang->line("to")." ".date('d-m-Y', strtotime($dateTo)),
												//$this->lang->line("currency").":".$data['transInfo'][0]->fldcurr_code,
												//$this->lang->line("terms").":".$data['transInfo'][0]->termName
											);*/
             
          //   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
			$bodyData['rptDataDetail'] = $data['balance'];
			$bodyData['tableHeader'] = array($this->lang->line("accountno"), $this->lang->line("accountName"), $this->lang->line("accountGroup"), $this->lang->line("accountType")); 
			$bodyData['selectFields'] = array("acctCode","acctName","acctGroupName","acctTypeName");
			//$bodyData ['tdAmount'] = array("totalPayable","totalTaxAmount");
			//$bodyData['tableFooter'] = array ("footer : value");
			$bodyData['tableFooter'] = array ("");
			$bodyData['excelTitle'] = "Location Transfer Detail";
			
				
		if($mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			//var_dump($bodyData2['selectFields']);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}
	
	public function profitnlossList_print($mode = true) 
	{
		$this->load->model('Chartofaccount');
		$this->load->model('company/company_info_model');
		
		$this->lang->load("comp_chartofaccount",$this->session->userdata('language'));
		$this->lang->load("acctGroup",$this->session->userdata('language'));
		$companyID = element('compID', $this->session->userdata('logged_in'));
		$companyName = element('compName', $this->session->userdata('logged_in'));
		
		$data['companyName']= $companyName;
		$data['companyID']= $companyID ;
		
		$data['profitNloss'] = $this->Chartofaccount->getInfo($companyID,1);
		//$supplierInfo = $this->Supplier_model->suppliermaster_id($data['transInfo'][0]->supplierID);
		//echo $this->db->last_query();
		//var_dump($data['datatbls2'][0]);           
             //$headerData['rightContentHeader'] = "Name supplier/ ot";
             $headerData['rptTitle'] = $this->lang->line("profitnloss");
             /*$headerData['leftContent'] = array(
												"<b>".$data['transInfo'][0]->customerName."</b>",
												$data['transInfo'][0]->customerCompanyNo,
												$data['transInfo'][0]->line1,
												$data['transInfo'][0]->line2,
												$data['transInfo'][0]->line3,
												$data['transInfo'][0]->customerPostCode.' '.$data['transInfo'][0]->city
											);*/
			 /*$headerData['rightContent'] = array(
												//"<b>".$this->lang->line("formNo").":".$data['transInfo'][0]->creditNoteNo."</b>",
												$this->lang->line("from")." ".date('d-m-Y', strtotime($dateFrom))." ".$this->lang->line("to")." ".date('d-m-Y', strtotime($dateTo)),
												//$this->lang->line("currency").":".$data['transInfo'][0]->fldcurr_code,
												//$this->lang->line("terms").":".$data['transInfo'][0]->termName
											);*/
             
          //   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
			$bodyData['rptDataDetail'] = $data['profitNloss'];
			$bodyData['tableHeader'] = array($this->lang->line("accountno"), $this->lang->line("accountName"), $this->lang->line("accountGroup"), $this->lang->line("accountType")); 
			$bodyData['selectFields'] = array("acctCode","acctName","acctGroupName","acctTypeName");
			//$bodyData ['tdAmount'] = array("totalPayable","totalTaxAmount");
			//$bodyData['tableFooter'] = array ("footer : value");
			$bodyData['tableFooter'] = array ("");
			$bodyData['excelTitle'] = "Location Transfer Detail";
			
				
		if($mode){
			//print
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			//var_dump($bodyData2['selectFields']);
			$this->load->view('layouts/report/footer','');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($bodyData);
		}
	}
	
	public function acctCodeDelete(){
			$this->load->model('CompanyChartAcct');
		  	$deleteuser = array('is_delete' => '1');
			$ID = isset($_REQUEST['ID'])?$_REQUEST['ID']:"";
			
		  	$delete_user = $this->CompanyChartAcct->chartAcctDelete($deleteuser, $ID);
			if($delete_user){
						echo '<script>alert("Delete Success..");
						window.location.replace("chartOfAcctList");
						</script>';
			}else{
				return FALSE;
			}
	}
	
// Budget Entry

	public function budgetEntry(){
		if($this->input->post('submit')){
		}else{
			$add_year = $this->uri->segment(3);
			$this->load->model('CompanyChartAcct');
		$this->load->model('Budget');
			$this->load->library('layouts');
			$this->lang->load("budget",$this->session->userdata('language'));
			$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js')
				->add_includes('js/datatables/jquery.dataTables.min.js');
			if(!$add_year){
				$add_year = date("Y");
			}
			$data['selyear'] = $add_year;
			$query = $this->Budget->budget_all_year($add_year);
			if($query){
				$data['budgetdetails'] = $query;
				$this->layouts->view('gl/setups/budgetEdit', array('latest' => 'sidebar/latest'), $data);
			}else{
				$data['budgetdetails'] = '';
				$data['chartofaccount'] = $this->CompanyChartAcct->chartAcct_all_PNL();
				$this->layouts->view('gl/setups/budget', array('latest' => 'sidebar/latest'), $data);
			}
		}
	}
	
	public function budgetEdit(){
		$this->load->library('layouts');
		$this->lang->load("budget",$this->session->userdata('language'));
		$this->load->model('Budget');
		$add_year = $this->uri->segment(3);
		if(!$add_year){
			$add_year = date("Y");
		}
		$data['selyear'] = $add_year;
		$data['budgetdetails'] = $this->Budget->budget_all_year($add_year);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('gl/setups/budgetEdit', array('latest' => 'sidebar/latest'), $data);
	}
	
	public function budget_insert(){
		$this->lang->load("budget",$this->session->userdata('language'));
		$this->load->model('Budget');
		$query = $this->Budget->budget_insert();
    	print json_encode(array("status"=>"success", "message"=>$this->lang->line('message7')));
	}
	
	public function budget_save(){
		$this->load->model('Budget');
		$query = $this->Budget->budget_update();
		if($query){
    		print json_encode(array("status"=>"success", "message"=>$this->lang->line('message7')));
		}else{
    		print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message8')));
		}
	}
//new currency	
	public function newCurrency(){
		$this->load->model('currency');
		if($this->input->post('submit')){
			if($this->input->post('fldcurr_code')==NULL){
				$currency_add = array(
			  			'fldid'		=> $this->input->post('fldid'), 
						'fldcurr_desc'		=> $this->input->post('acctCode'), 
		  			);
					
		  			$insert_currency = $this->currency->currency_insert($currency_add);
					if($insert_currency){
						echo '<script>alert("Insert Data Success..");
						window.location.replace("chartOfAcctList");
						</script>';
					}else{
						echo '<script>alert("Insert Data Failed.");
						history.go(-1);</script>';
					}
		 	}else{
					$ID = $this->input->post('fldcurr_code');
		  			$currency_edit = array(
			  			'fldcurr_desc'		=> $this->input->post('acctCode'), 
		  			);
					//$this->load->model(company/Company_info_model);
					$this->load->model('CompanyChartAcct');
		  			$update_currency = $this->CompanyChartAcct->chartAcct_edit($currency_edit, $ID);
					if($update_currency){
						echo '<script>alert("Update Success..");
						window.location.replace("chartOfAcctList");
						</script>';
					}
					else{
						echo '<script>alert("Update Failed.");
						history.go(-1);</script>';
					}
		  		}
	  		} 
			else 
			{    //add default company id -akmal 20140605 
				$fldid = isset($_REQUEST['fldid'])?$_REQUEST['fldid']:"";
				//$id = element('compID', $this->session->userdata('logged_in'));
				$this->load->library('layouts');
				$this->lang->load("chartofaccount",$this->session->userdata('language'));
				
				$addCurr = $this->currency->currency_all();
				$data['addCurrency'] = $addCurr[0];
				
				$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
					$this->layouts->view('gl/setups/newCurrency', array('latest' => 'sidebar/latest'), $data);
				
			}
	}
	
// Currency Conversion
	
	public function currencyConvertion(){
		if($this->input->post('submit')){
		}else{
			$this->load->model('Currency');
			$this->load->library('layouts');
			$data['currency'] = $this->Currency->currency_all();
			$this->lang->load("currency",$this->session->userdata('language'));
			$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
				->add_includes('js/validationengine/jquery.validationEngine.js')
				->add_includes('js/accounting.js');
			$this->layouts->view('gl/setups/currencyConv', array('latest' => 'sidebar/latest'), $data);
		}
	}
	
	public function currencyConv_update(){
		$this->load->model('Currency');
		$data = $this->input->post('eContent');
		switch ($this->input->post('fieldname')){
			case 'startDate':
				$data = date("Y-m-d", strtotime($data));
			break;
			case 'endDate':
				$data = date("Y-m-d", strtotime($data));
			break;
		}
		$updateData = array(
			$this->input->post('fieldname')=>$data
		);
		$query = $this->Currency->currencyConv_save($this->input->post('fldid'),$updateData);
		if($query){
    		print json_encode(array("status"=>"success", "message"=>$this->lang->line('message7')));
		}else{
    		print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message8')));
		}
	}
	
	public function currencyConv_insert(){
		$this->load->model('Currency');
		$query = $this->Currency->currencyConv_insert();
		if($query){
    		print json_encode(array(
				"status"=>"success", 
				"message"=>$this->lang->line('message7'), 
				"details"=>$query));
		}else{
    		print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message8')));
		}
	}
	public function currencyConvSearch(){
		$this->load->model('Currency');
		$query = $this->Currency->currencyConv_fromTo($this->input->post('currFrom'),$this->input->post('currTo'));
		if($query){
    		print json_encode(array("status"=>"success", "message"=>$this->lang->line('message7'), "details"=>$query));
		}else{
    		print json_encode(array("status"=>"failed", "message"=>$this->lang->line('message8')));
		}
	}
}

