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

class PurchaseReports extends CI_Controller {
	
	public $data = array();
	

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

	public function poList() {
		//$ID = isset($_REQUEST['ID'])?$_REQUEST['ID']:"";
		$mode = $this->uri->segment(3);
		$this->load->library('layouts');
		$this->lang->load("purchaseOrder",$this->session->userdata('language'));
		$this->load->model('company/FormSetup_model');
		$this->load->model('purchase/purchase_model');
		
		
//		$dateFrom = $this->input->post('selDateFrom');
//		$dateTo = $this->input->post('selDateTo');

		$data['mode'] = $mode; 
		
		$dateFrom = date("Y-m-d",strtotime($this->uri->segment(4)));
		$dateTo = date("Y-m-d",strtotime($this->uri->segment(5)));
		
		if ($this->uri->segment(4) == ""){
			$dateFrom = date("Y-m-01");
		}
		if($this->uri->segment(5) == ""){
			$dateTo = date("Y-m-t");
		}
		if($dateFrom > $dateTo){
			$dateTo = date("Y-m-t", strtotime($dateFrom));
		}
		
		$ID = $this->input->post('ID');
		$query = $this->purchase_model->updatePosting($ID);
		
		
		$data['dateFrom'] = $dateFrom;
		$data['dateTo'] = $dateTo;
		$data['datatbls'] = $this->purchase_model->purchaseOrderDate($dateFrom, $dateTo);
		
//		echo $this->db->last_query();
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('purchase/transactions/purchaseOrderList', array('latest' => 'sidebar/latest'), $data);
	}
	public function rptPO (){
		
			 //$mode = $this->uri->segment(3);
		     $this->load->library('layouts');
			 $this->lang->load("purchaseOrder",$this->session->userdata('language'));
			 $this->load->model('company/FormSetup_model');
			 $this->load->model('purchase/purchase_model');
			 $this->load->model('company/company_info_model');
			 
			 
			 //$data['mode'] = $mode; 
		
			$dateFrom = date("Y-m-d",strtotime($this->uri->segment(3)));
			$dateTo = date("Y-m-d",strtotime($this->uri->segment(4)));
		
			/*if ($this->uri->segment(4) == ""){
				$dateFrom = date("Y-m-01");
			}
			if($this->uri->segment(5) == ""){
				$dateTo = date("Y-m-t");
			}
			if($dateFrom > $dateTo){
				$dateTo = date("Y-m-t", strtotime($dateFrom));
			}*/
		
			$ID = $this->input->post('ID');
			$query = $this->purchase_model->updatePosting($ID);
		
		
			$data['dateFrom'] = $dateFrom;
			$data['dateTo'] = $dateTo;
			$data['datatbls'] = $this->purchase_model->purchaseOrderDate($dateFrom, $dateTo);
		  
            // $headerData['id'] = $id;
             //$headerData['rightContentHeader'] = "Name Supplier/ ot";
//             $headerData['rptTitle'] = "Purchase Order List";
             $headerData['rptTitle'] = $this->lang->line("rptTitle");
             $headerData['rightContent'] = array($this->lang->line("dateFrom")." ".date('d-m-Y', strtotime($dateFrom))." ".$this->lang->line("dateTo")." ".date('d-m-Y', strtotime($dateTo)),
												
											);
             //$headerData['leftContent'] = array("Purchase Order List : All");
             
            $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
              
             $bodyData['rptDataDetail'] = $this->purchase_model->purchaseOrderDate($dateFrom, $dateTo);
             $bodyData['tableHeader'] = array("Form No","Supplier Name", "Purchase Order No", "Sales Agent Name", "Order Date"); 
             $bodyData['selectFields'] = array("purchaseNo","supplierName", "purchaseOrderNo", "salesAgentName", "poDates");
          
            
            $this->load->view('layouts/report/header', $headerData);
            $this->load->view('layouts/report/body', $bodyData);
            $this->load->view('layouts/report/footer','');
        }
	
 
//Printing  PO report detail
	public function PODetails($purchaseOrderID,$mode = false){
           	$this->load->model('company/company_info_model');
			$this->load->model('purchase/purchase_model');
			$this->load->model('supplier/Supplier_model');
			$this->load->model('Location');
		
            $this->lang->load("purchaseOrder",$this->session->userdata('language'));
			//Gather report data

			$data['datatbls'] = $this->purchase_model->purchaseOrderPrintDetails_ID($purchaseOrderID); //detail
			$data['transInfo'] = $this->purchase_model->purchaseOrder_id($purchaseOrderID);			//master
			$supplierInfo = $this->Supplier_model->suppliermaster_id($data['transInfo'][0]->supplierID);
				
			$dataLocation = (isset($data['transInfo'][0]->deliveryToID)?$this->Location->location_id($data['transInfo'][0]->deliveryToID):NULL); 
//			 var_dump($data['transInfo'][0]);           
             //$headerData['rightContentHeader'] = "Name supplier/ ot";
             $headerData['rptTitle'] = $this->lang->line("rptTitleDetail");
             $headerData['leftContent'] = array(
												"<b>".$data['transInfo'][0]->supplierName."</b>",
												$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(
												"<b>".$this->lang->line("formNo").":".$data['transInfo'][0]->purchaseOrderNo."</b>",
												$this->lang->line("shipto").":".(isset($dataLocation[0]->desc)?$dataLocation[0]->desc:''),
												$this->lang->line("poDate").":".date('d-m-Y', strtotime($data['transInfo'][0]->poDate))
												,
											);
             
          //   $headerData['company'] = $this->company_info_model->companyById(element('compID', $this->session->userdata('logged_in')));
              
			$bodyData['rptDataDetail'] = $data['datatbls'];
			$bodyData['tableHeader'] = array($this->lang->line("itemName"),$this->lang->line("itemDescription"), $this->lang->line("unitPrice"), $this->lang->line("qtyOrder")); 
			$bodyData['selectFields'] = array("item","description","unitPrice","quantityOrder");
			$bodyData ['tdAmount'] = array("unitPrice","quantityOrder");
			//$bodyData['tableFooter'] = array ("footer : value");
			$bodyData['excelTitle'] = "Purchase Order Details";
			//right aligned columns
			$bodyData['tdNoFormat'] = array("quantityOrder","quantityReceivedTotal");

				
		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);
		}	
	}
	
}
?>