<?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');	
		$startDate = $this->input->post('startDate');
		
		if ($period){
			$month = $period - 1;
		    $startDate = date('Y-m-01', strtotime($startDate));
		    $endDate = date('Y-m-t', strtotime($startDate .'+'. $month .' months'));
			print json_encode(array("status" => "success", "startDate" => date('d-m-Y', strtotime($startDate)), "endDate" => date('d-m-Y', strtotime($endDate))));
		}
       
	}
	
	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){
			print json_encode(array(
				"status"=>"success", 
				"message"=>$query , 
				"financialYear"=>$financialYear,
				));
		}
		else{
			print json_encode(array("status"=>"fail", "message2"=>"afsf"));
		}
       
	}
	
	public function createCalendar()
	{
	  		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){
					for($i=0; $i<$period; $i++){
						$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);
					}//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->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/createCompanyFinancialCalendar', 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->layouts->set_title('iAccount::Chart of Accounts');
		$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);
		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 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')));
		}
	}
}

