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

class SupplierStatement extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->load->model('menu');
		
		//loading number to word language pack
		$this->lang->load("num_to_word",$this->session->userdata('language'));
		$this->load->helper('num_to_word');
	}

	public function index()
	{   		
		echo 'default index supplier statement controller';		
	}
	
	public function calendar()
    {
	}
	public function get_bySupplierID() 
	{
		$ID = $this->uri->segment(3);
		$this->load->library('layouts');
		$this->load->helper('form');
	  	$data['title'] = 'Supplier Statement';
		$this->lang->load("supplierstatement",$this->session->userdata('language'));
		$this->load->model('accountpayable/accountPayable_model');
		$companyID=element('compID', $this->session->userdata('logged_in'));
		$data['supplierInvoice']=$this->accountPayable_model->get_bySupplierID($ID,$companyID);
		
		
		$this->load->model('supplier/supplier_model');
		$dtl =$this->supplier_model->suppliermaster_id($ID);
		//echo $this->db->last_query();
		$dtl2 =$this->supplier_model->supplierAging_byID($ID);
		//echo $this->db->last_query();
		$dtl3 =$this->supplier_model->supplierStatement_byID($ID);
		$data['supplierDetails']=$dtl[0];
		//echo $this->db->last_query();
		$data['supplierAging']=$dtl2;
		//echo var_dump($dtl3);
		$data['supplierStatement']=$dtl3;
		
		$this->load->model('company/company_info_model');
		$dtlCompany =$this->company_info_model->company_id($companyID);
		$data['companyInfo']=$dtlCompany[0];
		
		//Fetching dictionary for number to word conversiont.
		$data['dictionary'] = $this->lang->line("dictionary");
//	    echo var_dump($dtl);  
		if ($dtl) {
			$this->load->view('supplierstatement/supplierStatement', $data);
		} else echo '<script>alert("'.$this->lang->line('invalidSupplier').'");</script>';
	}
	
	public function get_bySupplierID2() 
	{
		$ID = $this->uri->segment(5);
		$this->load->library('layouts');
		$this->load->helper('form');
	  	//$data['title'] = 'Supplier Statement';
		$this->lang->load("supplierStatement",$this->session->userdata('language'));
		$this->load->model('accountpayable/accountPayable_model');
		$companyID=element('compID', $this->session->userdata('logged_in'));
		$data['supplierInvoice']=$this->accountPayable_model->get_bySupplierID($ID,$companyID);
		
		$dateFrom = date("Y-m-d",strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		
		if ($this->uri->segment(3) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(4) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		
		$this->load->model('supplier/supplier_model');
		$dtl =$this->supplier_model->suppliermaster_id($ID);
		//echo $this->db->last_query();
		$dtl2 =$this->supplier_model->supplierAging_byID($ID);
		//echo $this->db->last_query();
		$dtl3 =$this->supplier_model->supplierStatement_byID($ID);
		$data['supplierDetails']=$dtl[0];
		//echo $this->db->last_query();
		$data['supplierAging']=$dtl2;
		//echo var_dump($dtl3);
		$data['supplierStatement']=$dtl3;
		
		$this->load->model('company/company_info_model');
		$dtlCompany =$this->company_info_model->company_id($companyID);
		$data['companyInfo']=$dtlCompany[0];
		
		
	      
	$this->load->view('supplierstatement/supplierStatement2', $data);
	}
	/*public function supplierStatement() 
	{
		$ID = $this->uri->segment(3);
		$this->load->library('layouts');
		$this->load->helper('form');
	  	$data['title'] = 'Supplier Statement';
		$this->lang->load("supplierstatement",$this->session->userdata('language'));
		$this->load->model('accountpayable/accountPayable_model');
		$companyID=element('compID', $this->session->userdata('logged_in'));
		$data['supplierInvoice']=$this->accountPayable_model->get_bySupplierID($ID,$companyID);
		
		
		$this->load->model('supplier/supplier_model');
		$dtl =$this->supplier_model->suppliermaster_id($ID);
		//echo $this->db->last_query();
		$dtl2 =$this->supplier_model->supplierAging_byID($ID);
		//echo $this->db->last_query();
		$dtl3 =$this->supplier_model->supplierStatement_byID($ID);
		$data['supplierDetails']=$dtl[0];
		//echo $this->db->last_query();
		$data['supplierAging']=$dtl2;
		//echo var_dump($dtl3);
		$data['supplierStatement']=$dtl3;
		
		$this->load->model('company/company_info_model');
		$dtlCompany =$this->company_info_model->company_id($companyID);
		$data['companyInfo']=$dtlCompany[0];
		
		//echo var_dump($dtl3);
		$this->layouts->add_includes('js/charts.js')
			->add_includes('js/validationengine/languages/jquery.validationEngine-en.js')
			->add_includes('js/validationengine/jquery.validationEngine.js');
	      
	$this->layouts->view('supplierstatement/supplierStatement', array('latest' => 'sidebar/latest'), $data);
	}*/

	public function supplierList() //list of supplier to date account status
	{
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('supplierstatement/supplierstatement_model');
//		$data['datatbls'] = $this->supplierstatement_model->supplierstatement_all();
		$this->load->model('accountpayable/accountPayable_model');
		$this->load->model('company/company_info_model');
		$this->load->model('supplier/supplier_model');
		$this->lang->load("supplierstatement",$this->session->userdata('language'));
		$this->load->model('company/FormSetup_model');
		
		$dateFrom = date("Y-m-01",strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		$ID = $this->uri->segment(5)?$this->uri->segment(5):'';
		
		if ($this->uri->segment(3) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(4) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		//$ID = $this->input->post('ID');
		//$query = $this->SupplierPayment_model->updatePosting($ID);
		$supplier = $this->supplier_model->suppliermaster_all();
		$data['supplier'] = $supplier;
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['ID'] = $ID;
		if ($ID <> ''){
			$data['datatbls'] = $this->supplierstatement_model->supplierstatement_name($dateFrom, $dateTo, $ID);
			$data['monthOpening'] = $this->supplierstatement_model->getOpening($dateFrom, $dateTo, $ID);
		}
		else if ($ID == ''){
			$data['datatbls'] = $this->supplierstatement_model->supplierstatementList_date($dateFrom, $dateTo);
		}		
		
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('supplierstatement/suppstatList', array('latest' => 'sidebar/latest'), $data);
	
	}
	
	public function supplierListprint($mode=false)
	{
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('supplierstatement/supplierstatement_model');
//		$data['datatbls'] = $this->supplierstatement_model->supplierstatement_all();
		$this->load->model('accountpayable/accountPayable_model');
		$this->load->model('company/company_info_model');
		$this->load->model('supplier/supplier_model');
		$this->lang->load("supplierstatement",$this->session->userdata('language'));
		$this->load->model('company/FormSetup_model');
		
		$dateFrom = date("Y-m-d",strtotime($this->uri->segment(3)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		$ID = $this->uri->segment(5)?$this->uri->segment(5):'';
		
		$mode = $this->uri->segment(6);
		if ($this->uri->segment(3) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(4) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		$supplier = $this->supplier_model->suppliermaster_all();
		$data['supplier'] = $supplier;
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['ID'] = $ID;
		
		
		$headerData['rightContentHeader'] = "Name supplier/ ot";
        $headerData['rptTitle'] = "Supplier Statement";
		
		if ($ID != ''){
        	$headerData['rightContent'] = array($this->lang->line('dateFrom')." ".date('d-m-Y',strtotime($dateFrom))." ".$this->lang->line('dateTo')." ".date('d-m-Y',strtotime($dateTo)));
		}
		
        
             
        $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
              
		
		if ($ID != 0){
			$bodyData['rptDataDetail'] = $this->supplierstatement_model->supplierstatement_name($dateFrom, $dateTo, $ID);
			$bal = $data['monthOpening'] = $this->supplierstatement_model->getOpening($dateFrom, $dateTo, $ID);
			
			if(!empty($bodyData['rptDataDetail'])){
				foreach($bodyData['rptDataDetail'] as &$row){
					$bal += $row->dr - $row->cr;
					$row->bal = $bal; 
				}
				$headerData['leftContent'] = array("Supplier Name: ".$bodyData['rptDataDetail'][0]->supplierName);
			}
		}
		
        $bodyData['tableHeader'] = array($this->lang->line('formNo'), $this->lang->line('transDate'), $this->lang->line('refNo'), $this->lang->line('debit'), $this->lang->line('credit'),$this->lang->line('Balance'));
	 
        $bodyData['selectFields'] = array("formNo", "transDate", "refNo", "dr", "cr","bal");
        $bodyData ['tdAmount'] = array("dr", "cr","bal");
        $bodyData['tableFooter'] = array ("Balance : ".number_format($bal,2));
                
        $bodyData['excelTitle'] = "Supplier Statement";
        
        if(!$mode){
        	//print
        	$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($bodyData);
        }
	
	}
	
	public function supplierStatDetail($supplierID,$mode = false) //list of supplier to date account status
	{
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('supplierstatement/supplierstatement_model');
//		$data['datatbls'] = $this->supplierstatement_model->supplierstatement_all();
		$this->load->model('accountpayable/accountPayable_model');
		$this->load->model('company/company_info_model');
		$this->load->model('supplier/supplier_model');
		$this->load->model('company/FormSetup_model');
		
		$this->lang->load("supplierstatement",$this->session->userdata('language'));
		
		$data['datatbls'] = $this->supplierstatement_model->suppStat_byID($supplierID); //detail
		$data['transInfo'] = $this->supplierstatement_model->supplierstatementPrintDetails_ID($supplierID);
		$supplierInfo = $this->supplier_model->suppliermaster_id($data['transInfo'][0]->supplierID);
		
		//var_dump($data['datatbls'][0]);           
             //$headerData['rightContentHeader'] = "Name supplier/ ot";
             $headerData['rptTitle'] = $this->lang->line("rptTitle");
             $headerData['leftContent'] = array(
												$data['transInfo'][0]->supplierName,
												$data['transInfo'][0]->supplierCompanyNo,
												$data['transInfo'][0]->line1,
												$data['transInfo'][0]->line2,
												$data['transInfo'][0]->line3,
												$data['transInfo'][0]->supplierPostCode.' '.$data['transInfo'][0]->city
											);
			 $headerData['rightContent'] = array(
												$this->lang->line("formNo").":".$data['transInfo'][0]->supplierStatementNo,
												$this->lang->line("date").":".date('d-m-Y', strtotime($data['transInfo'][0]->transDate))
											);
             
          //   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
			$bodyData['rptDataDetail'] = $data['datatbls'];
			$bodyData['tableHeader'] = array($this->lang->line("formNo"), $this->lang->line("refNo"), $this->lang->line("transDate"), $this->lang->line("debit"), $this->lang->line("credit")); 
			$bodyData['selectFields'] = array("formNo","refNo","transDate","dr","cr");
			$bodyData ['tdAmount'] = array("dr","cr");
			//$bodyData['tableFooter'] = array ("footer : value");
			$bodyData['excelTitle'] = "Location Transfer Detail";
				
		if(!$mode){
			//print
			$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($bodyData);
		}
	}
}

   
    