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