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

class SalesQuote extends CI_Model{
	
	public function SalesQuote_all(){
		$this->db->select('a.*');
		$this->db->select('b.customerName');
		$this->db->select('c.fldcurr_code');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->order_by('a.companyID');
		$this->db->order_by('a.quotationDate desc');
		$this->db->from('comp_quotation a');
		$this->db->join('customer b', 'b.ID = a.customerID');	
		$this->db->join('_ref_currency c', 'c.fldid = a.currencyID');	
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}

	/***** Edited by Sayem (17th july, 2014) ****
	,CONCAT(a.formNo," [",lpad(a.ID,8,0),"]") as formNo',false
	*********************************************/
	public function SalesQuote_all_date($startDate, $endDate){
		$this->db->select('a.*,CONCAT(a.formNo," [",lpad(a.ID,8,0),"]") as formNo',false);
		$this->db->select('b.customerName');
		$this->db->select('c.fldcurr_code');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('a.quotationDate >=', $startDate);
		$this->db->where('a.quotationDate <=', $endDate);
		$this->db->order_by('a.companyID');
		$this->db->order_by('a.quotationDate desc');
		$this->db->from('comp_quotation a');
		$this->db->join('customer b', 'b.ID = a.customerID');	
		$this->db->join('_ref_currency c', 'c.fldid = a.currencyID');	
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function SalesQuote_ID($ID){
		$this->db->select('a.*,CONCAT(a.formNo," [",lpad(a.ID,8,0),"]") as quoteNo',FALSE);
		$this->db->select('b.customerName,b.customerCompanyNo,b.line1,b.line2,b.line3,b.city,b.customerPostCode');
		$this->db->select('c.fldcurr_code');
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('a.ID', $ID);
		$this->db->order_by('a.companyID');
		$this->db->order_by('a.quotationDate desc');
		$this->db->from('comp_quotation a');
		$this->db->join('customer b', 'b.ID = a.customerID');	
		$this->db->join('_ref_currency c', 'c.fldid = a.currencyID');	
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function SalesQuote_Insert(){
		$newData = array(
			'companyID' => element('compID', $this->session->userdata('logged_in')),
			'customerID' => $this->input->post('customerID'),
			'employeeID' => $this->input->post('employeeID'),
			'formNo' => $this->input->post('formNo'),
			'quotationDate' => date("Y-m-d", strtotime($this->input->post('effdate'))),
			'deliverToID' => $this->input->post('deliveryToID'),
			'currencyID' => $this->input->post('currencyID'),
			'memo' => $this->input->post('memo'),
			'exchangeRate' => $this->input->post('exchangeRate'),
			'employeeID' => $this->input->post('salesPerson'),
			'totalTaxAmount' => $this->input->post('totalAmount'),
			'createdBy' => element('role', $this->session->userdata('logged_in'))
		);
		$query = $this->db->insert('comp_quotation', $newData);
		if($query){
			return $this->db->insert_id();
		}else{
			return FALSE;
		}
	}
	public function SalesQuote_Update($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_quotation', $data); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
	public function SalesQuote_delete($id){
		$this->db->where('id', $id);
		$query = $this->db->delete('comp_quotation'); 
		$this->db->flush_cache();
		$this->db->where('quotationID', $id);
		$query = $this->db->delete('comp_quotation_detail'); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
// Sales quote details 
	public function SalesQuoteDetails_Insert($quoteID){
		$newData = array(
			'quotationID' => $quoteID,
			'itemID' => $this->input->post('itemID'),
			'unitOfPrice' => $this->input->post('unitPrice'),
			'quantityOrder' => $this->input->post('quoteQty'),
			'amountExcludeTax' => $this->input->post('amount'),
			'taxID' => $this->input->post('taxID'),
			'taxRate' => $this->input->post('taxRate'),
			'umID' => $this->input->post('unitmeasure'),
			'description' => $this->input->post('description'),
			'taxAmount' => $this->input->post('taxAmount'),
			'amountIncudeTax' => $this->input->post('IncTaxAmount'),
			'createdBy' => element('role', $this->session->userdata('logged_in'))
		);
		$query = $this->db->insert('comp_quotation_detail', $newData);
		if($query){
			return $this->db->insert_id();
		}else{
			return FALSE;
		}
	}
	public function SalesQuoteDetails_all_id($ID){
		$this->db->select('a.*');
		$this->db->select('b.code');
		$this->db->select('c.itemCode,c.description,CONCAT(c.name," [",c.itemCode,"]") as itemCodeName',false);
		$this->db->where('a.quotationID', $ID);
		$this->db->from('comp_quotation_detail a');
		$this->db->join('_sys_um b', 'b.fldid = a.umID');	
		$this->db->join('item c', 'c.ID = a.itemID');	
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function SalesQuoteDetails_delete($id){
		$this->db->where('id', $id);
		$query = $this->db->delete('comp_quotation_detail'); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
// Sequence number
	public function seqNumber(){
		$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('formID', 48);
		$this->db->from('comp_form_control');
		$seqFormat = $this->db->get();
		if($seqFormat->num_rows() >= 1){
			$temp = $seqFormat->result();
			
		}else{
			
		}
	}
	
}

/* End of file SalesOrder.php */
/* Location: ./application/models/SalesOrder.php */
/* Created By : Yahaya Abdollah */
