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

class supplierstatement_model extends CI_Model{
	
public function supplierstatement_all(){
	    $this->db->where('is_delete', 0);
	    //$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get('view_supplier_statement');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function supplier_statement_id($ID){
		$this->db->where('ID', $ID);
	    $this->db->where('is_delete', 0);
	   // $this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get('view_supplier_statement');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
// Created by Sayem Ahmed	
	public function getOpening($dateFrom = '', $dateTo = '', $ID = ''){
		$yearMonth = date('Ym',strtotime($dateFrom));
		$query = $this->db->select('balance')
						->from('comp_supplier_closing')
						->where(array('companyID' => element('compID', $this->session->userdata('logged_in')),'supplierID'=>$ID,'yearMonth'=>$yearMonth))
						->get();
		if($query->num_rows() >=1 ){
			$result = $query->result();
			return $result[0]->balance;
		}else{
			return 0;			
		}		
						
	}
	
	public function supplierstatementPrintDetails_ID($ID){
		$this->db->select('a.*');
		$this->db->select('b.*');
		$this->db->select('concat(a.formNo,"[",lpad(a.serialNo,8,"0"),"]") as supplierStatementNo', false);
		$this->db->where('a.ID', $ID);
		$this->db->where('a.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->from('view_supplier_statement a');
		$this->db->join('supplier b', 'b.ID = a.supplierID');
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	}

	public function supplierstatementList_date($dateFrom, $dateTo){
	    //$this->db->where('is_delete', 0);
		$this->db->where('view_supplier_statement.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('transDate between  "'.$dateFrom.'" and "'.$dateTo.'"');
		$this->db->select('sum(dr) as dr');
		$this->db->select('sum(cr) as cr');
		$this->db->from('view_supplier_statement');
		$this->db->select('supplier.*');
//		$this->db->select('supplier.supplierCode as supplierCode');
		$this->db->join('supplier', 'supplier.ID = view_supplier_statement.supplierID');
		$this->db->order_by('transDate desc');
	    //$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get();
		//echo $this->db->last_query();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
		
	public function supplierstatement_date($dateFrom, $dateTo){
	    //$this->db->where('is_delete', 0);
		$this->db->where('view_supplier_statement.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('transDate between  "'.$dateFrom.'" and "'.$dateTo.'"');
		$this->db->select('view_supplier_statement.*');
		$this->db->from('view_supplier_statement');
		$this->db->select('supplier.supplierName as supplierName');
		$this->db->select('supplier.supplierCode as supplierCode');
		$this->db->join('supplier', 'supplier.ID = view_supplier_statement.supplierID');
		$this->db->order_by('transDate desc');
	    //$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function supplierstatement_name($dateFrom, $dateTo, $ID){
	    //$this->db->where('is_delete', 0);
		$this->db->where('view_supplier_statement.companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->where('transDate between  "'.$dateFrom.'" and "'.$dateTo.'"');
		$this->db->select('view_supplier_statement.*');
		$this->db->from('view_supplier_statement');
		$this->db->select('supplier.supplierName as supplierName');
		$this->db->where('supplier.ID', $ID);
		$this->db->select('supplier.supplierCode as supplierCode');
		$this->db->select('supplier.supplierName');
		$this->db->join('supplier', 'supplier.ID = view_supplier_statement.supplierID');
		$this->db->order_by('transDate ASC');
	    //$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	
	public function suppStat_byID($ID){
		$this->db->where('supplierID', $ID);
		$query = $this->db->get('view_supplier_statement');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return false;
		}
	 }
	
/* //statement should no update/delete -akmal
	public function supplier_statement_edit($updatestat, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_supplier_statement', $updatestat); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function supplier_statement_insert($newstatement){
		$sql = $this->db->insert_string('comp_supplier_statement', $newstatement);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			$last_query = $this->db->last_query();
			return $last_query;
		}
	}
	public function suppplier_statement_delete($deletepstat, $ID){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_supplier_statement', $deletestat); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	*/
}

