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

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

	public function index()
	{
		
	}
	
	public function processGAF(){
		$this->lang->load("taxGAF",$this->session->userdata('language'));
		$this->load->model('Journal');
		$this->load->model('company/Company_info_model');
		$this->load->model('Compfinancialyear');
		$this->load->model('TaxTrans');
		$this->load->model('CompAcctDet');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		$query1 = $this->Compfinancialyear->compfinancialyear_date1(date("Y-m-d"));
		if(!$add_year){
			$add_year = $query1[0]->financialYear;
		}
		if(!$add_period){
			$add_period = $query1[0]->period;
		}
		$query3 = $this->Compfinancialyear->financialYear_yearperiod($add_year,$add_period);
		if($query3){
		$data['startDate'] = $query3[0]->startDate;
		$data['endDate'] = $query3[0]->endDate;
		$startDate = $query3[0]->startDate;
		$endDate = $query3[0]->endDate;
		}else{
		}
		$data['yrs'] = range(date('Y'), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['companyTables'] = $this->Company_info_model->company_id(element('compID', $this->session->userdata('logged_in')));
//		$data['purchaseTables'] = $this->TaxTrans->taxTransPurchase_GAF($startDate, $endDate);;
		$data['datatbls'] = $this->TaxTrans->gafFiles($startDate, $endDate);
		$data['glDetails'] = $this->CompAcctDet->AcctDet_allfy1_period($add_year, $add_period);
		$data['journalDetails'] = $this->Journal->journalDetails_yearperiod1($add_year, $add_period);
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('tax/transactions/gafFilesProcess', array('latest' => 'sidebar/latest'), $data);
//		$data['datatbls'] = $this->creditnote_model->creditnote_unpost($dateFrom, $dateTo);
	}
	
	public function processGAF_excell(){
        $this->load->library('parser');
		$this->lang->load("taxGAF",$this->session->userdata('language'));
		$this->load->model('Journal');
		$this->load->model('company/Company_info_model');
		$this->load->model('Compfinancialyear');
		$this->load->model('TaxTrans');
		$this->load->model('CompAcctDet');
		$add_year = $this->uri->segment(3);
		$add_period = $this->uri->segment(4);
		$query1 = $this->Compfinancialyear->compfinancialyear_date1(date("Y-m-d"));
		if(!$add_year){
			$add_year = $query1[0]->financialYear;
		}
		if(!$add_period){
			$add_period = $query1[0]->period;
		}
		$query3 = $this->Compfinancialyear->financialYear_yearperiod($add_year,$add_period);
		if($query3){
		$data['startDate'] = $query3[0]->startDate;
		$data['endDate'] = $query3[0]->endDate;
		$startDate = $query3[0]->startDate;
		$endDate = $query3[0]->endDate;
		}else{
		}
		$data['yrs'] = range(date('Y'), 1900);
		$data['selyear'] = $add_year;
		$data['selper'] = $add_period;
		
		$query = $this->Compfinancialyear->financialYear_year($add_year);
		if($query){
			$data['period'] = $query;
		}else{
			$data['period'] = NULL;
		}
		$data['companyTables'] = $this->Company_info_model->company_id(element('compID', $this->session->userdata('logged_in')));
//		$data['purchaseTables'] = $this->TaxTrans->taxTransPurchase_GAF($startDate, $endDate);;
		$data['datatbls'] = $this->TaxTrans->gafFiles($startDate, $endDate);
		$data['glDetails'] = $this->CompAcctDet->AcctDet_allfy1_period($add_year, $add_period);
		$data['journalDetails'] = $this->Journal->journalDetails_yearperiod1($add_year, $add_period);
        $myFile = "GAF(".$add_year."_".$add_period.").txt";
 
        //pass retrieved data into template and return as a string
        $stringData = $this->parser->parse('tax/transactions/gafFilesProcess_excell', $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);
//		$this->load->view('tax/transactions/gafFilesProcess_excell', $data);
//		$data['datatbls'] = $this->creditnote_model->creditnote_unpost($dateFrom, $dateTo);
	}
/* 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 taxTransaction.php */
/* Location: ./application/controller/taxTransaction.php */
