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

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

	public function __construct(){
		parent::__construct();
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->layouts->set_title('iAccount');

		$this->load->model('menu');
	}

	
	
	
	
	public function index1() {
		//echo '<h1>Purchase Invoice default page<h1><br>';
		//echo '<div><a href="purchaseInvoice/piList"><h3>List</h3></a></div>'; //call controller
		

			$this->load->model('company/formSetup_Model');
			$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
//			$filter['formID']=41;
//			$data['formNo']= $this->formSetup_Model->getFormNo($filter); 
			$this->lang->load("invoices",$this->session->userdata('language'));
			$this->load->model('companyChartAcct');
		
			$this->load->model('Usermaster');
			$data['records'] = $this->Usermaster->usermaster_all();

			
			$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
			->add_includes('js/validationengine/jquery.validationEngine.js');
			
			$this->load->model('Supplier/supplier_model');
			$data['supplier'] = $this->supplier_model->suppliermaster_all();
			
			$this->load->model('Project/project_model');
			$data['project'] = $this->project_model->project_all();
			
			$this->load->model('address_model');
			$data['shipto'] = $this->address_model->getCompanyAdresses();
			
			$this->load->model('Terms/terms_model');
			$data['terms'] = $this->terms_model->terms_all();
			
			$this->load->model('location');
			$data['location'] = $this->location->location_all();
			
			$this->load->model('companyChartAcct');
			$data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
			
			
			$this->load->model('taxmaster');
			$data['taxmaster'] = $this->taxmaster->taxmaster_all();
			
			$data['itemCodes'] = $this->getItemCodes();
			
//			$this->load->model('company/formSetup_Model');
			$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
			$filter['formID']=41;
			$data['formNo']= $this->formSetup_Model->getFormNo($filter); 

			$this->load->model('Usermaster');
			$data['records'] = $this->Usermaster->usermaster_all();
			
			//$this->layouts->view('accountPayable/ap_purchaseinvoice', '', $data);
			$this->layouts->view('purchase/transactions/purchaseinvoice', '', $data);
			
			
			
	}
	
	
	
	
	public function piList() {
		echo '<h1>Purchase and Direct Purchase Invoice List</h1>';
	}
	
		function deleteCompanyDetail(){
			$this->load->model('accountPayable/accountPayable_model');
			echo $this->accountPayable_model->deleteCompanyDetail($this->input->post('detailsId'));
		}
		
		
			
		function edit ($id) {
			
			$this->load->model('company/formSetup_Model');
			$filter['companyID']=element('compID', $this->session->userdata('logged_in'));
			$filter['formID']=41;
			$data['formNo']= $this->formSetup_Model->getFormNo($filter); 
			$this->lang->load("invoices",$this->session->userdata('language'));
			$this->load->model('companyChartAcct');
		
			$this->load->model('Usermaster');
			$data['records'] = $this->Usermaster->usermaster_all();
			$this->load->library('layouts');
			$this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
			->add_includes('js/validationengine/jquery.validationEngine.js');
			
			$this->lang->load("ap_edit",$this->session->userdata('language'));
			
			$this->load->model('Usermaster');
			$data['records'] = $this->Usermaster->usermaster_all();
			
			$data['itemCodes'] = $this->getItemCodes();
			
			$this->load->model('companyChartAcct');
			$data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
			
			$this->load->model('taxmaster');
			$data['taxmaster'] = $this->taxmaster->taxmaster_all();
			
			$this->load->model('accountPayable/accountPayable_model');
			$data['pdetails']=$this->accountPayable_model->get_Detail_byID($id);
			$data['pInvoice']=$this->accountPayable_model->get_byID("comp_supplier_invoice",$id);

			$data['charge']=$this->accountPayable_model->getChargeByid($id);
			
			$this->layouts->view('accountPayable/ap_edit', '', $data);
	}
	
	 private function getItemCodes(){
		$JsonRecords = '[';
		$i= 0;
		$this->load->model('item/itemSetup_model');
		//$default = $this->itemSetup_model->get_byCode($this->input->post('itemCode'));
		$default = $this->itemSetup_model->get_all();
		$n = sizeof($default);
		if($default)
		 foreach($default as $row){
			$JsonRecords .= '&quot;'.$row->itemCode.'&quot;';
			if ($i < $n - 1)
				$JsonRecords .= ',';
			$i++;
		}
		 $JsonRecords .= ']';
		 return $JsonRecords;
	}
	
	
	/********************************************
	 ******************SAYEM*********************
	********************************************/
	
	//Purchase invoice listing
	public function index(){
		$this->load->model('company/formSetup_Model');
		$filter['companyID'] = element('compID', $this->session->userdata('logged_in'));
		$this->lang->load("purchaseInvoice", $this->session->userdata('language'));
		$this->load->model('companyChartAcct');
		 
		$this->load->model('Usermaster');
		$data['records'] = $this->Usermaster->usermaster_all();
		 
		$this->load->model('Supplier/supplier_model');
		$data['supplier'] = $this->supplier_model->suppliermaster_all();
		 
		$this->load->model('Project/project_model');
		$data['project'] = $this->project_model->project_all();
		 
		$this->load->model('address_model');
		$data['shipto'] = $this->address_model->getCompanyAdresses();
		 
		$this->load->model('employee/employee_model');
		$data['purchaser'] = $this->employee_model->employeemaster_allopt();
		
		$this->load->model('master/masterCode_model');
		$data['shippingmethod'] = $this->masterCode_model->get_all(array('masterID' => 2));
		 
		$this->load->model('Terms/terms_model');
		$data['terms'] = $this->terms_model->terms_all();
		 
		$this->load->model('location');
		$data['location'] = $this->location->location_all();
		 
		$this->load->model('companyChartAcct');
		$data['chargeAccount'] = $this->companyChartAcct->chartAcct_all();
		 
		 
		$this->load->model('taxmaster');
		$data['taxmaster'] = $this->taxmaster->taxmaster_all();
		 
		$this->load->model('item/itemSetup_model');
		//$default = $this->itemSetup_model->get_byCode($this->input->post('itemCode'));
		$data['items'] = $this->itemSetup_model->get_all();
		 
		//			$this->load->model('company/formSetup_Model');
		$filter['companyID'] = element('compID', $this->session->userdata('logged_in'));
		$filter['formID'] = 41;
		$data['formNo'] = $this->formSetup_Model->getFormNo($filter);
		 
		$this->load->model('Usermaster');
		$data['records'] = $this->Usermaster->usermaster_all();
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js')
					->add_includes('js/validationengine/languages/jquery.validationEngine-'.$this->session->userdata('language').'.js')
					->add_includes('js/accounting.js')
					->add_includes('js/validationengine/jquery.validationEngine.js');
		$this->layouts->view('purchase/transactions/purchaseinvoice', '', $data);
	}
	//temporary
	/*public function setPurOrderNo(){
		$this->load->model('purchase/purchaseInvoice_model');
		$invoices = $this->purchaseInvoice_model->setPurchaseOrder();
		
		exit();
	}*/
	
	//Get Order item list by customer id
	public function getOrderItemBySupp(){
		$this->load->model('purchase/purchaseInvoice_model');
		$invoices = $this->purchaseInvoice_model->getOrderItems($this->input->post('supplierID'),date('Y-m-d', strtotime($this->input->post('date'))));
		echo json_encode($invoices);
		exit();
	}
	
	//Get Order list by customer id
	public function getOrderBySupp(){
		$this->load->model('purchase/purchaseInvoice_model');
		$invoices = $this->purchaseInvoice_model->getOrder($this->input->post('supplierID'));
		echo $invoices;
		exit();
	}
	//Gathering detail information about the order
	public function getOrderDetail(){
		$this->load->model('purchase/purchaseInvoice_model');
		$invoices = $this->purchaseInvoice_model->getOrderByID($this->input->post('ordID'));
		echo json_encode($invoices);
		exit();
	}
	//Get Invoice list by customer id
	public function getInvoiceBySupp(){
		$this->load->model('purchase/purchaseInvoice_model');
		$invoices = $this->purchaseInvoice_model->getInvoice($this->input->post('supplierID'));
		echo $invoices;
		exit();
	}
	
	//Gathering detail information about the invoice.
	public function getInvoiceDetail(){
		$this->load->model('purchase/purchaseInvoice_model');
		$invoices = $this->purchaseInvoice_model->getInvoiceByID($this->input->post('invID'));
		echo json_encode($invoices);
		exit();
	}
	
	public function saveCharge(){
		$this->load->model('purchase/purchaseInvoice_model');
	
		$companyID =  element('compID', $this->session->userdata('logged_in')) ;
		$datainsert = array (
				'companyID' => $companyID,
				'purchaseInvoiceCodeID' => $this->input->post('purchaseInvoiceCodeID'),
				'purchaserID' => $this->input->post('purchaserID'),
				'formNo' => $this->input->post('formNo'),
				'invoiceDate' => date('Y-m-d', strtotime($this->input->post('invoiceDate'))),
				'supplierID' => $this->input->post('supplierID'),
				'supplierInvoiceNo' => $this->input->post('supplierInvoiceNo'),
				'projectID' => $this->input->post('projectID'),
				'agentID' => $this->input->post('agentID'),
				'termsID' => $this->input->post('termsID'),
				'shipToID' => $this->input->post('shipToID'),
				//'customerID' => $this->input->post('customerID'),
				'exchangeRate' => $this->input->post('exchangeRate'),
				'locationID' => $this->input->post('locationID'),
				'memo' => $this->input->post('memo'),
				'employeeID' => $this->input->post('employeeID'),
				'shippingMethodID' => $this->input->post('shippingMethodID'),
				'createdBy' => element('role', $this->session->userdata('logged_in')),
	
				'totalTaxAmount' => $this->input->post('totalTaxAmount'),
				'totalPayable' => $this->input->post('totalPayable'),
				'totalAmount' => $this->input->post('total')
		);
	
	
		$invID = $this->purchaseInvoice_model->insert("comp_supplier_invoice",$datainsert);
		$this->purchaseInvoice_model->updatePurchaseInvDet($invID,$this->input->post('suppInvDetail'));
	
		if($invID){
	
			$datinsertDetails = array (
					'invoiceID' =>  $invID,
					'lineNo' => $this->input->post('lineNo'),
					'createdBy' => element('role', $this->session->userdata('logged_in')),
					'accountNoID' => $this->input->post('accountNoID'),
					'chargeDescription' => $this->input->post('chargeDescription'),
					'amountExcludedTax' => $this->input->post('amountExcludedTax'),
					'taxID' => $this->input->post('taxID'),
					'taxRate' => $this->input->post('taxRate'),
					'taxAmount' => $this->input->post('taxAmount'),
					'amountIncludedTax' => $this->input->post('amountIncludedTax')
			);
			$detail = $this->purchaseInvoice_model->insert('comp_supplier_invoice_charges',$datinsertDetails);
	
			echo '{"status":"success","invoiceID":"'.$invID.'","chargeId":"'.$detail.'"}';
		}else{
			echo "false";
		}
	}
	
	//Saveing purchase order as invoie
	public function saveInvoice(){
		$this->load->model('purchase/purchaseInvoice_model');
		
		$companyID =  element('compID', $this->session->userdata('logged_in')) ;
		$datainsert = array (
				'companyID' => $companyID,
				'purchaseInvoiceCodeID' => $this->input->post('purchaseInvoiceCodeID'),
				'purchaserID' => $this->input->post('purchaserID'),
				'formNo' => $this->input->post('formNo'),
				'invoiceDate' => date('Y-m-d', strtotime($this->input->post('invoiceDate'))),
				'supplierID' => $this->input->post('supplierID'),
				'supplierInvoiceNo' => $this->input->post('supplierInvoiceNo'),
				'projectID' => $this->input->post('projectID'),
				'agentID' => $this->input->post('agentID'),
				'termsID' => $this->input->post('termsID'),
				'shipToID' => $this->input->post('shipToID'),
				//'customerID' => $this->input->post('customerID'),
				'exchangeRate' => $this->input->post('exchangeRate'),
				'locationID' => $this->input->post('locationID'),
				'memo' => $this->input->post('memo'),
				'employeeID' => $this->input->post('employeeID'),
				'shippingMethodID' => $this->input->post('shippingMethodID'),
				'createdBy' => element('role', $this->session->userdata('logged_in')),
		
				'totalTaxAmount' => $this->input->post('totalTaxAmount'),
				'totalPayable' => $this->input->post('totalPayable'),
				'totalAmount' => $this->input->post('totalAmount')
		);
		
		
		$invID = $this->purchaseInvoice_model->insert("comp_supplier_invoice",$datainsert);
		$this->purchaseInvoice_model->updatePurchaseInvDet($invID,$this->input->post('suppInvDetail'));
		
		echo '{"status":"success","invoiceID":"'.$invID.'"}';
	}
	
	public function updateInvoice(){
		$data = array(
				"totalAmount"=>$this->input->post('totalAmount'),
				"totalPayable"=>$this->input->post('totalPayable'),
				"totalTaxAmount"=>$this->input->post('totalTaxAmount'),
		);
		$this->load->model('purchase/purchaseInvoice_model');
		return $this->purchaseInvoice_model->updateInvoice($this->input->post('invoiceId'),$data);
	}
	/******************************************
	 *
	*  - Sayem
	*  end
	*
	*/
	
	


	
} //big end
?>