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

class CustomerStatement 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 customer statement controller';		
	}
	
	public function calendar()
    {
	}
	public function get_byCustomerID() 
	{
		$ID = $this->uri->segment(3);
		$this->load->library('layouts');
		$this->load->helper('form');
	  	$data['title'] = 'Customer Statement';
		$this->lang->load("customerstatement",$this->session->userdata('language'));
		$this->load->model('ar/saleInvoice_model');
		$companyID=element('compID', $this->session->userdata('logged_in'));
		$data['customerInvoice']=$this->saleInvoice_model->get_byCustomerID($ID,$companyID);
		
		
		$this->load->model('customer/customer_model');
		$dtl =$this->customer_model->customermaster_id($ID);
		//echo $this->db->last_query();
		$dtl2 =$this->customer_model->customerAging_byID($ID);
		//echo $this->db->last_query();
		$dtl3 =$this->customer_model->customerStatement_byID($ID);
		$data['customerDetails']=$dtl[0];
		//echo $this->db->last_query();
		$data['customerAging']=$dtl2;
		//echo var_dump($dtl3);
		$data['customerStatement']=$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('customerstatement/customerStatement', $data);
		} else echo '<script>alert("'.$this->lang->line('invalidCustomer').'");</script>';
	}
	
	public function get_byCustomerID2() 
	{
		$ID = $this->uri->segment(5);
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->lang->load("customerStatement",$this->session->userdata('language'));
		$this->load->model('ar/saleInvoice_model');
		$companyID=element('compID', $this->session->userdata('logged_in'));
		$data['customerInvoice']=$this->saleInvoice_model->get_byCustomerID($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('customer/customer_model');
		$dtl =$this->customer_model->customermaster_id($ID);
		//echo $this->db->last_query();
		$dtl2 =$this->customer_model->customerAging_byID($ID);
		//echo $this->db->last_query();
		$dtl3 =$this->customer_model->customerStatement_byID($ID);
		$data['customerDetails']=$dtl[0];
		//echo $this->db->last_query();
		$data['customerAging']=$dtl2;
		//echo var_dump($dtl3);
		$data['customerStatement']=$dtl3;
		
		$this->load->model('company/company_info_model');
		$dtlCompany =$this->company_info_model->company_id($companyID);
		$data['companyInfo']=$dtlCompany[0];
		
		
	      
	$this->load->view('customerstatement/customerStatement2', $data);
	}
	

	public function customerList() //list of customer to date account status
	{
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('customerstatement/customerstatement_model');
//		$data['datatbls'] = $this->supplierstatement_model->supplierstatement_all();
		$this->load->model('ar/saleInvoice_model');
		$this->load->model('company/company_info_model');
		$this->load->model('customer/customer_model');
		$this->lang->load("customerstatement",$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);
		$customer = $this->customer_model->customermaster_all();
		$data['customer'] = $customer;
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['ID'] = $ID;
		if ($ID <> ''){
			$data['datatbls'] = $this->customerstatement_model->customerstatement_name($dateFrom, $dateTo, $ID);
			$data['monthOpening'] = $this->customerstatement_model->getOpening($dateFrom, $dateTo, $ID);
		}
		else if ($ID == ''){
			$data['datatbls'] = $this->customerstatement_model->customerstatementList_date($dateFrom, $dateTo);
		}		
		
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('customerstatement/custstatList', array('latest' => 'sidebar/latest'), $data);
	
	}
	
	public function customerListprint($mode=false)
	{
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('customerstatement/customerstatement_model');
		$this->load->model('ar/saleInvoice_model');
		$this->load->model('accountpayable/accountPayable_model');
		$this->load->model('company/company_info_model');
		$this->load->model('customer/customer_model');
		$this->lang->load("customerstatement",$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));
		}
		
		$customer = $this->customer_model->customermaster_all();
		$data['customer'] = $customer;
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['ID'] = $ID;
		
		
		$headerData['rightContentHeader'] = "Name customer";
        $headerData['rptTitle'] = "Customer 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->customerstatement_model->cutomerstatement_name($dateFrom, $dateTo, $ID);
			$bal = $data['monthOpening'] = $this->customerstatement_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("Customer Name: ".$bodyData['rptDataDetail'][0]->customerName);
			}
		}
		
        $bodyData['tableHeader'] = array($this->lang->line('formNo'), $this->lang->line('transType'),$this->lang->line('transDate'), $this->lang->line('refNo'), $this->lang->line('debit'), $this->lang->line('credit'),$this->lang->line('Balance'));
	 
        $bodyData['selectFields'] = array("formNo", "transType","transDate", "refNo", "dr", "cr","bal");
        $bodyData ['tdAmount'] = array("dr", "cr","bal");
      //  $bodyData['tableFooter'] = array ("Balance:".number_format($bal,2));       
        $bodyData['excelTitle'] = "Customer 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 customerStatDetail($customerID,$mode = false) 
	{
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->load->model('customertstatement/customerstatement_model');
		$this->load->model('ar/saleInvoice_model');
		$this->load->model('accountpayable/accountPayable_model');
		$this->load->model('company/company_info_model');
		$this->load->model('customer/customer_model');
		$this->load->model('company/FormSetup_model');
		
		$this->lang->load("customerstatement",$this->session->userdata('language'));
		
		$data['datatbls'] = $this->customerstatement_model->customerStat_byID($customerID); //detail
		//var_dump($data['datatbls']);
		//echo $this->db->last_query();
		$data['transInfo'] = $this->customerstatement_model->customerstatementPrintDetails_ID($customerID);
		$customerInfo = $this->customer_model->customermaster_id($data['transInfo'][0]->customerID);
	
	
             $headerData['rptTitle'] = $this->lang->line("rptTitle");
             $headerData['leftContent'] = array(
												$data['transInfo'][0]->customerName,
												$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(
												$this->lang->line("formNo").":".$data['transInfo'][0]->customerStatementNo,
												$this->lang->line("date").":".date('d-m-Y', strtotime($data['transInfo'][0]->transDate))
											);
             

			$bodyData['rptDataDetail'] = $data['datatbls'];
			$bodyData['tableHeader'] = array($this->lang->line("formNo"), $this->lang->line("refNo"), $this->lang->line("transType"),$this->lang->line("transDate"), $this->lang->line("debit"), $this->lang->line("credit")); 
			$bodyData['selectFields'] = array("formNo","refNo","transType","transDate","dr","cr", "bal");
			$bodyData ['tdAmount'] = array("dr","cr", "ba;");
		    $bodyData['tableFooter'] = array ("Balance : ".number_format($bal,2)); 
			$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);
		}
	}
	
}

   
    