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

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

	public function index()
	{
		
	}
	

	public function postedjournal(){
        /*$this->load->model('UseraccessDetails');
        $query = $this->UseraccessDetails->UseraccessDetails_id_code('GLRPJLPS');
        if (!$query) {
            echo '<script>alert("Not authorised..");
				window.location.replace("' . base_url() . 'Error");
				</script>';
        }*/
		$startDate = $this->uri->segment(3);
		$endDate = $this->uri->segment(4);
		
		if(!$startDate){
			$startDate = date("01-m-Y");
		}
		if(!$endDate){
			$endDate = date("t-m-Y");
		}
		if($endDate < $startDate){
			$endDate = $startDate;
		}
		
		$this->lang->load("journalpost",$this->session->userdata('language'));
		$this->load->model('Journal');
		/*$this->load->model('Compfinancialyear');
		
		$data['yrs'] = $this->Compfinancialyear->get_financialYear();*/
		
		$data['dateFrom'] = $startDate;
		$data['dateTo'] = $endDate;
		/*$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}*/
		$data['datatbls'] = $this->Journal->journal_posted_startDateEndDate(date("Y-m-d", strtotime($startDate)), date("Y-m-d", strtotime($endDate)));
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('gl/postedjournal', array('Menu'=>'layouts/menu'), $data);
	}
	
	
	public function postedjournalPrint($startDate, $endDate, $mode=false){
        /*$this->load->model('UseraccessDetails');
        $query = $this->UseraccessDetails->UseraccessDetails_id_code('GLRPJLPS');
        if (!$query) {
            echo '<script>alert("Not authorised..");
				window.location.replace("' . base_url() . 'Error");
				</script>';
        }*/
		$this->load->model('company/company_info_model');
		
		$startDate = $this->uri->segment(3);
		$endDate = $this->uri->segment(4);
		
		if(!$startDate){
			$startDate = date("01-m-Y");
		}
		if(!$endDate){
			$endDate = date("t-m-Y");
		}
		if($endDate < $startDate){
			$endDate = $startDate;
		}
		
		$this->lang->load("journalpost",$this->session->userdata('language'));
		$this->load->model('Journal');
		/*$this->load->model('Compfinancialyear');
		
		$data['yrs'] = $this->Compfinancialyear->get_financialYear();*/
		
		$data['dateFrom'] = $startDate;
		$data['dateTo'] = $endDate;
		/*$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}*/
		$data['datatbls'] = $this->Journal->journal_posted_startDateEndDate(date("Y-m-d", strtotime($startDate)), date("Y-m-d", strtotime($endDate)));
		
		
		$headerData['rptTitle'] = $this->lang->line('title');

        $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));

        $headerData['rightContent'] = array(
            $this->lang->line("dateFrom") . " : " . $data['dateFrom'] . " " . $this->lang->line("dateTo") . " : " . $data['dateTo']
        );

        $bodyData['rptDataDetail'] = $data['datatbls'];

        $bodyData['tableHeader'] = array($this->lang->line('jlnumber'), $this->lang->line('year'), $this->lang->line('description'), $this->lang->line('total'));

        $bodyData['selectFields'] = array("journalID", "date", "description", "total_amount_dr");
        $bodyData ['tdAmount'] = array("total_amount_dr");
        //$bodyData['tableFooter'] = array ("footer : value");

		if(!$mode){
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body', $bodyData);
			$this->load->view('layouts/report/footer', '');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($headerData,array($bodyData),/*$footerData*/'');
		}
		
		
	}
	
	
	public function journalDetails(){
		$ID = $this->uri->segment(3);
		$data['ID'] = $ID;
		$data['fad'] = 0;
		$this->load->model('Journal');
		$query = $this->Journal->journal_ID($ID);
		$data['datatbls'] = $this->Journal->journalDetails_journalno($query[0]->journalID);
		$data['journalHead'] = $query[0];
		$this->lang->load("journaldetails",$this->session->userdata('language'));
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('gl/journal_details', array('Menu'=>'layouts/menu'), $data);
	}
	
	
	public function journalDetailsPrint($ID, $mode = false){
		
		$this->load->model('company/company_info_model');
		
		$ID = $this->uri->segment(3);
		
		$this->lang->load("journaldetails",$this->session->userdata('language'));
		
		$data['ID'] = $ID;
		$data['fad'] = 0;
		$this->load->model('Journal');
		$query = $this->Journal->journal_ID($ID);
		
		$data['journalHead'] = $query[0];
		
		$dateConvert = date("d-m-Y", strtotime($data['journalHead']->effective_date));
		
		$data['datatbls'] = $this->Journal->journalDetails_journalno($query[0]->journalID);
		
		$headerData['rptTitle'] = $this->lang->line('title');

        $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));

        $headerData['leftContent'] = array(
            "<strong>" . $this->lang->line("journalNo") . " : " . $data['journalHead']->journalID . "</strong>",
			$this->lang->line("description") . " : " . $data['journalHead']->description,
			$this->lang->line("effective_date") . " : " . $dateConvert,
			$this->lang->line("amount_dr_ttl") . " : " . number_format($data['journalHead']->total_amount_dr, 2) . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $this->lang->line("amount_cr_ttl") . " : " . number_format($data['journalHead']->total_amount_cr, 2),
        );

        $bodyData['rptDataDetail'] = $data['datatbls'];
		
		$fad = $bodyData['rptDataDetail'];
		
		$totalDebit = 0;
		$totalCredit = 0;
		
		if($fad){
			foreach($fad as $data){
				$totalDebit += $data->amount_dr;
				$totalCredit += $data->amount_cr;
			}
		}

        $bodyData['tableHeader'] = array($this->lang->line('acctCode'), $this->lang->line('description'), $this->lang->line('amount_dr'), $this->lang->line('amount_cr'));

        $bodyData['selectFields'] = array("kodAkaun", "description", "amount_dr", "amount_cr");
        $bodyData ['tdAmount'] = array("amount_dr", "amount_cr");
		$bodyData['tableFooter'] = array (
                               $this->lang->line('total'),
							   number_format($totalDebit,2,".",","),
							   number_format($totalCredit,2,".",","),
							   );
        //$bodyData['tableFooter'] = array ("footer : value");

		if(!$mode){
			$this->load->view('layouts/report/header', $headerData);
			$this->load->view('layouts/report/body-gst', $bodyData);
			$this->load->view('layouts/report/footer', '');
		}else{
			//download excell
			$this->load->helper('excel');
			excel($headerData,array($bodyData),/*$footerData*/'');
		}
		
	}
	
	
	public function smeCashFlowList(){
		$this->lang->load("smeCashFlow",$this->session->userdata('language'));
		
		$this->load->model('report/smeCashFlow_model');
		
		if($this->uri->segment(3)){
			$year = $this->uri->segment(3);
		}
		else{
			$year = date('Y');
		}
		
		$data['year'] = $year;
		
		$data['datatbls1'] = $this->smeCashFlow_model->cashFlowList_allGroup3($year);
		$data['datatbls2'] = $this->smeCashFlow_model->cashFlowList_allGroup8($year);
		$data['datatbls3'] = $this->smeCashFlow_model->cashFlowList_allGroup10($year);
		$data['datatbls4'] = $this->smeCashFlow_model->cashFlowList_allGroup2($year);
		$data['datatbls5'] = $this->smeCashFlow_model->cashFlowList_allGroup4($year);
		
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('report/smeCashFlowList', array('Menu'=>'layouts/menu'), $data);
	}
	
	
	public function smeCashFlowListPrint(){
		$this->load->model('company/company_info_model');
		
		$this->lang->load("smeCashFlow",$this->session->userdata('language'));
		
		$this->load->model('report/smeCashFlow_model');
		
		$data['rptTitle'] = $this->lang->line('title');

        $data['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
		
		
		if($this->uri->segment(3)){
			$year = $this->uri->segment(3);
		}
		else{
			$year = date('Y');
		}
		
		$data['year'] = $year;
		
		$data['datatbls1'] = $this->smeCashFlow_model->cashFlowList_allGroup3($year);
		$data['datatbls2'] = $this->smeCashFlow_model->cashFlowList_allGroup8($year);
		$data['datatbls3'] = $this->smeCashFlow_model->cashFlowList_allGroup10($year);
		$data['datatbls4'] = $this->smeCashFlow_model->cashFlowList_allGroup2($year);
		$data['datatbls5'] = $this->smeCashFlow_model->cashFlowList_allGroup4($year);
		
		if (isset($_POST['submit'])){
			$this->load->library('parser');
			
			$data['cashinhandJanExcel'] = $this->input->post('cashinhandJanExcel');
			$data['cashinhandFebExcel'] = $this->input->post('cashinhandFebExcel');
			$data['cashinhandMacExcel'] = $this->input->post('cashinhandMacExcel');
			$data['cashinhandAprExcel'] = $this->input->post('cashinhandAprExcel');
			$data['cashinhandMayExcel'] = $this->input->post('cashinhandMayExcel');
			$data['cashinhandJunExcel'] = $this->input->post('cashinhandJunExcel');
			$data['cashinhandJulExcel'] = $this->input->post('cashinhandJulExcel');
			$data['cashinhandAugExcel'] = $this->input->post('cashinhandAugExcel');
			$data['cashinhandSepExcel'] = $this->input->post('cashinhandSepExcel');
			$data['cashinhandOctExcel'] = $this->input->post('cashinhandOctExcel');
			$data['cashinhandNovExcel'] = $this->input->post('cashinhandNovExcel');
			$data['cashinhandDisExcel'] = $this->input->post('cashinhandDisExcel');
			
			$data['endMonthJan'] = $this->input->post('endMonthJan');
			$data['endMonthFeb'] = $this->input->post('endMonthFeb');
			$data['endMonthMac'] = $this->input->post('endMonthMac');
			$data['endMonthApr'] = $this->input->post('endMonthApr');
			$data['endMonthMay'] = $this->input->post('endMonthMay');
			$data['endMonthJun'] = $this->input->post('endMonthJun');
			$data['endMonthJul'] = $this->input->post('endMonthJul');
			$data['endMonthAug'] = $this->input->post('endMonthAug');
			$data['endMonthSep'] = $this->input->post('endMonthSep');
			$data['endMonthOct'] = $this->input->post('endMonthOct');
			$data['endMonthNov'] = $this->input->post('endMonthNov');
			$data['endMonthDis'] = $this->input->post('endMonthDis');
			
			//$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
			$myFile = "Cash_Flow.xls";
	 
			//pass retrieved data into template and return as a string
			$stringData = $this->parser->parse('report/smeCashFlowListExcel', $data, true);
	 
			//open excel and write string into excel
			$fh = fopen($myFile, 'w') or die("can't open file");
			fwrite($fh, $stringData);
	 
			fclose($fh);
			//download excel file
			$this->downloadExcel($myFile);
		}
		else{
			
			//$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
			$this->load->view('report/smeCashFlowListPrint', $data);
		}
	}
	
	
	public function smeCashFlowListExcel(){
		$this->load->library('parser');
		
		$this->load->model('company/company_info_model');
		
		$this->lang->load("smeCashFlow",$this->session->userdata('language'));
		
		$this->load->model('report/smeCashFlow_model');
		
		
		$data['rptTitle'] = $this->lang->line('title');

        $data['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
		
		
		if($this->uri->segment(3)){
			$year = $this->uri->segment(3);
		}
		else{
			$year = date('Y');
		}
		
		$data['year'] = $year;
		
		$data['datatbls1'] = $this->smeCashFlow_model->cashFlowList_allGroup3($year);
		$data['datatbls2'] = $this->smeCashFlow_model->cashFlowList_allGroup8($year);
		$data['datatbls3'] = $this->smeCashFlow_model->cashFlowList_allGroup10($year);
		$data['datatbls4'] = $this->smeCashFlow_model->cashFlowList_allGroup2($year);
		$data['datatbls5'] = $this->smeCashFlow_model->cashFlowList_allGroup4($year);
		
		//$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$myFile = "Cash_Flow.xls";
 
        //pass retrieved data into template and return as a string
        $stringData = $this->parser->parse('report/smeCashFlowListExcel', $data, true);
 
        //open excel and write string into excel
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $stringData);
 
        fclose($fh);
        //download excel file
        $this->downloadExcel($myFile);
	}
	
	
	
	public function smeCashFlowSearchList(){
		$this->lang->load("smeCashFlow",$this->session->userdata('language'));
		
		$this->load->model('report/smeCashFlow_model');
		
		$year = date('Y');
		
		$monthFrom = $this->uri->segment(4);
		
		$monthTo = $this->uri->segment(5);
		
		$data['monthFrom'] = $monthFrom;
		
		$data['monthTo'] = $monthTo;
		
		$monthName = array();
		$monthSort = array();
		
		for($i=$monthFrom; $i <= $monthTo; $i++){
			$monthName[$i] = date('M', strtotime('+'.$i.' month'));
			$monthSort[$i] = date('F', strtotime('+'.$i.' month'));
		}
		
		$data['monthSort'] = $monthSort;
		$data['monthName'] = $monthName;
		
		//$fadd = array();
		$monthPrevious = date('F', strtotime('+2 month'));
		
		$data['year'] = $year;
		
		$data['month3'] = $this->smeCashFlow_model->cashFlowList_month3($monthPrevious, $monthSort, $year);
		$data['month8'] = $this->smeCashFlow_model->cashFlowList_month8($monthPrevious, $monthSort, $year);
		$data['month10'] = $this->smeCashFlow_model->cashFlowList_month10($monthPrevious, $monthSort, $year);
		$data['month2'] = $this->smeCashFlow_model->cashFlowList_month2($monthPrevious, $monthSort, $year);
		$data['month4'] = $this->smeCashFlow_model->cashFlowList_month4($monthPrevious, $monthSort, $year);
		
		
		/*$data['datatbls1'] = $this->smeCashFlow_model->cashFlowList_allGroup3($year);
		$data['datatbls2'] = $this->smeCashFlow_model->cashFlowList_allGroup8($year);
		$data['datatbls3'] = $this->smeCashFlow_model->cashFlowList_allGroup10($year);
		$data['datatbls4'] = $this->smeCashFlow_model->cashFlowList_allGroup2($year);
		$data['datatbls5'] = $this->smeCashFlow_model->cashFlowList_allGroup4($year);*/
		
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('report/smeCashFlowSearchList', array('Menu'=>'layouts/menu'), $data);
	}

	
	public function assetReportPrint(){
		$this->load->model('company/company_info_model');
		
		$this->lang->load("asset",$this->session->userdata('language'));
		
		$data['rptTitle'] = $this->lang->line('loanReportTitle');

        $data['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
		
     	$year = $this->uri->segment(3);
     	if(!$year){
     		$year = date('Y');
     	}
		$this->lang->load("asset",$this->session->userdata('language'));
		$this->load->model('compAcctDet');
		$data['loans'] = $this->compAcctDet->getAssetReport($year);
		$data['year'] = $year;
		$this->load->view('gl/reports/assetreportPrint', $data);
	}
	
	
	public function assetReportExcel(){
		$this->load->library('parser');
		
		$this->load->model('company/company_info_model');
		
		$this->lang->load("asset",$this->session->userdata('language'));
		
		$data['rptTitle'] = $this->lang->line('loanReportTitle');

        $data['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
		
     	$year = $this->uri->segment(3);
     	if(!$year){
     		$year = date('Y');
     	}
		$this->lang->load("asset",$this->session->userdata('language'));
		$this->load->model('compAcctDet');
		$data['loans'] = $this->compAcctDet->getAssetReport($year);
		$data['year'] = $year;
		
		$myFile = "Asset_Report.xls";
 
        //pass retrieved data into template and return as a string
        $stringData = $this->parser->parse('gl/reports/assetreportExcel', $data, true);
 
        //open excel and write string into excel
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $stringData);
 
        fclose($fh);
        //download excel file
        $this->downloadExcel($myFile);
		
	}
	
	
	public function loanReportPrint(){
     	
		$this->load->model('company/company_info_model');
		
		$this->lang->load("loan",$this->session->userdata('language'));
		
		$data['rptTitle'] = $this->lang->line('loanReportTitle');

        $data['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
		
     	$year = $this->uri->segment(3);
		
     	if(!$year){
     		$year = date('Y');
     	}
		
		$this->load->model('compAcctDet');
		$data['loans'] = $this->compAcctDet->getLoanReport($year);
		$data['year'] = $year;
		$this->load->view('gl/reports/loanreportPrint', $data);
	}
	
	
	public function loanReportExcel(){
     	$this->load->library('parser');
		
		$this->load->model('company/company_info_model');
		
		$this->lang->load("loan",$this->session->userdata('language'));
		
		$data['rptTitle'] = $this->lang->line('loanReportTitle');

        $data['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
		
     	$year = $this->uri->segment(3);
		
     	if(!$year){
     		$year = date('Y');
     	}
		
		$this->load->model('compAcctDet');
		$data['loans'] = $this->compAcctDet->getLoanReport($year);
		$data['year'] = $year;
		
		$myFile = "Loan_Report.xls";
 
        //pass retrieved data into template and return as a string
        $stringData = $this->parser->parse('gl/reports/loanreportExcel', $data, true);
 
        //open excel and write string into excel
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $stringData);
 
        fclose($fh);
        //download excel file
        $this->downloadExcel($myFile);
		
	}
	
	
	/* download created excel file */
    function downloadExcel($myFile) {
        header("Content-Length: " . filesize($myFile));
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename='.$myFile);
 
        readfile($myFile);
    }
	  
}

	
/* End of file gltransaction.php */
/* Location: ./application/controller/gltransaction.php */
