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

class Currency extends CI_Model{
	
	public function currency_all(){
		$this->db->order_by('fldcurr_code');
		$query = $this->db->get('_ref_currency');
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}

public function getConversionRate($currencyIDto){
	//select rateConve from comp_currency_conv 
	//where companyID = element('compID', $this->session->userdata('logged_in')))
	//and currencyto = $currencyIDto
	//order by id desc limit 1
	$sql = 'select rateConv from comp_currency_conv ';
	$sql .= ' where companyID = '.element('compID', $this->session->userdata('logged_in')) ;
	$sql .= ' and currencyto = '.$currencyIDto ;
	$sql .= ' order by id desc limit 1';
	$query = $this->db->query($sql);
	//echo $this->db->last_query().'<br>';
	//echo var_dump($query->result()).'<br>';
	$rate = $query->result();
	return (sizeof($rate)>0) ? $rate[0] : false;

}

	public function currency_insert($newCurrency){
		$sql = $this->db->insert_string('_ref_currency', $newCurrency);
		$query = $this->db->query($sql);
		if($query === TRUE){
			return TRUE;
		} else {
			$last_query = $this->db->last_query();
			return $last_query;
		}
	}

	public function currency_save($fldid, $data){
		$this->db->where('fldid', $fldid);
		$query = $this->db->update('_ref_currency', $data); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function currency_delete($fldid){
		$this->db->where('fldid', $fldid);
		$query = $this->db->delete('_ref_currency'); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
// currency conversion	
	public function currencyConv_insert(){
		$newData = array(
			'companyID'=>element('compID', $this->session->userdata('logged_in')),
			'currencyFrom'=>$this->input->post('currencyFrom'),
			'currencyTo'=>$this->input->post('currencyTo'),
			'startDate'=>date("Y-m-d", strtotime($this->input->post('startDate'))),
			'endDate'=>date("Y-m-d", strtotime($this->input->post('endDate'))),
			'rateConv'=>$this->input->post('rateConv'),
			'createdBy'=>element('role', $this->session->userdata('logged_in')),
		);
		$query = $this->db->insert('comp_currency_conv', $newData);
		if($query){
			return $this->db->insert_id();
		}else{
			return FALSE;
		}
	}
	public function currencyConv_save($ID, $data){
		$this->db->where('ID', $ID);
		$query = $this->db->update('comp_currency_conv', $data); 
		if($query === TRUE){
			return TRUE;
		} else {
			return FALSE;
		}
	}
	public function currencyConv_fromTo($From, $To){
		$this->db->select('a.*');
		$this->db->select('b.fldcurr_code as currFrom');
		$this->db->select('c.fldcurr_code as currTo');
		$this->db->where('a.currencyFrom', $From);
		$this->db->where('a.currencyTo', $To);
		$this->db->where('companyID', element('compID', $this->session->userdata('logged_in')));
		$this->db->from('comp_currency_conv a');
		$this->db->order_by('startDate desc');
		$this->db->join('_ref_currency b', 'b.fldid = a.currencyFrom', 'LEFT OUTER');	
		$this->db->join('_ref_currency c', 'c.fldid = a.currencyTo', 'LEFT OUTER');	
		$query = $this->db->get();
		if($query->num_rows() >= 1){
			return $query->result();
		}else{
			return FALSE;
		}
	}
	public function currencyConv_id($ID){
		$this->db->where('currencyTo', $ID);
		$this->db->where('startDate <=', date("Y-m-d", strtotime($this->input->post('effdate'))));
		$this->db->where('endDate >=', date("Y-m-d", strtotime($this->input->post('effdate'))));
		$this->db->where('currencyFrom', element('compCurrency', $this->session->userdata('logged_in')));
		$query = $this->db->get('comp_currency_conv'); 
		if($query->num_rows() >= 1){
			return $query->result();
		} else {
			return FALSE;
		}
	}
}