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

class salesinv_model extends CI_Model{
	
	
	public function salesinv_all(){
		$this->db->where('comp_sales_invoice.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->select('comp_sales_invoice.*');
		$this->db->select('customer.customerName as customerName');
		$this->db->from('comp_sales_invoice');
		$this->db->join('customer', 'customer.ID = comp_sales_invoice.customerID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function salesinv_Date($dateFrom, $dateTo){

		$sql1 = "SELECT comp_sales_invoice.*, if (comp_sales_invoice.updateAble=1, 'Yes', 'No' ) as confirm,    customer.customerName as customerName, comp_sales_invoice.salesInvoiceDate";
		$sql2 = " FROM (comp_sales_invoice JOIN customer ON customer.ID = comp_sales_invoice.customerID)";						        $sql3 = " WHERE comp_sales_invoice.companyID = '1' AND comp_supplier_invoice.salesInvoiceDate between '".$dateFrom."' and '".$dateTo."'";
		$sql4 = " ORDER BY salesInvoiceDate desc"; 
		$query = $this->db->query($sql1.$sql2.$sql3.$sql4);
		
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
	
	public function updatePosting($ID){
		$this->db->where('ID', $ID);
		$updatepayment = array('updateAble'=>0);
		$query = $this->db->update('comp_sales_invoice', $updatepayment); 
		if($query){
			return TRUE;
		}else{
			return FALSE;
		}
		
	}
	
	public function salesinv_Update($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_sales_invoice', $data); 
		if($query === TRUE){
			return $this->db->last_query();
		} else {
			return FALSE;
		}
	}
	
		public function salesinv_unpost($dateFrom, $dateTo){
		$this->db->select('comp_sales_invoice.*');
		$this->db->select('comp_sales_invoice.updateAble as Yes');
		$this->db->select('customer.customerName');
		$this->db->where('comp_sales_invoice.companyID', element('compID',	$this->session->userdata('logged_in')));
		$this->db->where('salesInvoiceDate >=', $dateFrom);
		$this->db->where('salesInvoiceDate <=', $dateTo);
		$this->db->where('comp_sales_invoice.updateAble', 1);
		$this->db->order_by('salesInvoiceDate', 'desc');
		$this->db->from('comp_sales_invoice');
		$this->db->join('customer', 'customer.ID = comp_sales_invoice.customerID');
//		$this->db->join('comp_sales_creditnote','comp_sales_creditnote.ID = comp_sales_creditnote.salesInvoiceID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}
     
	 public function salesinv_post($id, $data){
		$this->db->where('ID', $id);
		$this->db->where('updateAble', 1);
		$query = $this->db->update('comp_sales_invoice', $data);
		if($query){
			$this->db->flush_cache();
			$this->db->where('ID', $id);
			$this->db->from('comp_sales_invoice');
			$query1 = $this->db->get();
			return $query1->result();
		}else{
			return false;
		}
	 }
	 
}
	