<?php
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/
/*********************************************************************************
 * $Id: Currency.php,v 1.16 2005/02/09 07:08:54 andrew Exp $
 ********************************************************************************/
include_once('config.php');
require_once('include/logging.php');
require_once('include/database/PearDatabase.php');
require_once('data/SugarBean.php');

// Contact is used to store customer information.
class Currency extends SugarBean 
{
	// Stored fields
	var $id;
	var $iso4217;
	var $name;
	var $status; 
	var $conversion_rate;
	var $deleted;
	var $date_entered;
	var $date_modified;
	var $symbol;
	var $hide = '';
	var $unhide = '';
	var $field_name_map;

	var $table_name = "currencies";
	var $object_name = "Currency";
	
	var $new_schema = true;

	var $column_fields = Array("id"
		,"name"
		,"conversion_rate"
		,"iso4217"
		,"symbol"
		,'status'
                ,"deleted"
                ,"date_entered"
                ,"date_modified"
		);
	var $list_fields ;
	
	var $field_defs = array(
		array("name"=>"name", "vname"=>"LBL_LIST_NAME","type"=>"varchar","len"=>"50"),
		array("name"=>"symbol", "vname"=>"LBL_LIST_SYMBOL","type"=>"varchar","len"=>"50"),
		array("name"=>"conversion_rate", "vname"=>"LBL_LIST_RATE","type"=>"float","len"=>"50"),
		array("name"=>"iso4217", "vname"=>"LBL_LIST_ISO4217","type"=>"varchar","len"=>"50"),
		array("name"=>"status","vname"=>"LBL_STATUS","type"=>"enum",'options'=>'currency_status_dom'),
       	array('name'=>'date_entered','vname'=>'LBL_DATE_ENTERED','type'=>'date'),
       	array('name'=>'date_modified','vname'=>'LBL_DATE_MODIFIED','type'=>'date'),

	);
	var $required_fields = array('name'=>1, 'symbol'=>2, 'conversion_rate'=>3, 'iso4217'=>4 , 'status'=>5);
	

	function Currency() 
	{
		$this->log = LoggerManager::getLogger('currency');
		$this->db = new PearDatabase();
		$this->list_fields =  array_merge($this->column_fields, array('hide', 'unhide'));
		foreach ($this->field_defs as $field)
		{
			$this->field_name_map[$field['name']] = $field;
		}



	}

	function create_tables () 
	{
		$query = 'CREATE TABLE '.$this->table_name.' ( ';
		$query .='id char(36) NOT NULL';
		$query .=', name char(36) NOT NULL';
		$query .=', symbol char(36) NOT NULL';
		$query .=', iso4217 char(3) NOT NULL';
		$query .=', conversion_rate DOUBLE NOT NULL';
		$query .= ',status char(25)';
		$query .=', deleted bool NOT NULL default 0';
        $query .=', date_entered datetime NOT NULL';
        $query .=', date_modified datetime NOT NULL';
        $query .=', PRIMARY KEY ( ID ) )';

		
		
		
		$this->db->query($query,true,"Error creating table: ".$this->table_name. ":" );


		//TODO Clint 4/27 - add exception handling logic here if the table can't be created.
	
		// Create the indexes
                $this->create_index("create index idx_cont_name on ".$this->table_name." ( name, deleted)");
	}

	function drop_tables () 
	{
		$query = 'DROP TABLE IF EXISTS '.$this->table_name;

		
			
		$this->db->query($query);

		//TODO Clint 4/27 - add exception handling logic here if the table can't be dropped.

	}
	
	function convertToDollar($amount){
		return $amount / $this->conversion_rate;	
		
	}
	
	function convertFromDollar($amount){	
		return $amount * $this->conversion_rate;
	}
	
	function getDefaultCurrencyName(){
		return translate('LBL_US_DOLLAR', 'Currencies');	
	}
	
	 function retrieveIDBySymbol($symbol, $encode = true){
	 	$query = "select id from currencies where symbol='$symbol' and deleted=0;";
	 	$result = $this->db->query($query);
	 	if($result){
	 	$row = $this->db->fetchByAssoc($result);
	 	if($row){
	 		return $row['id'];	
	 	}
	 	}
	 	return '';	
	 }
	 
	 
	 function list_view_parse_additional_sections(&$list_form)
	{
		global $isMerge;
		
		if(isset($isMerge) && $isMerge && $this->id != '-99'){
		$list_form->assign('PREROW', '<input name="mergecur[]" type="checkbox" value="'.$this->id.'">');
		}
		return $list_form;
	}
     function retrieve($id, $encode = true){
     	if($id == '-99'){
     		$this->name = 	$this->getDefaultCurrencyName();
     		$this->symbol = '$';
     		$this->id = '-99';
     		$this->conversion_rate = 1;
     		$this->iso4217 = 'USD';
     		$this->deleted = 0;
     		$this->status = 'Active';
     		$this->hide = '<!--';
     		$this->unhide = '-->';
     	}else{
     		parent::retrieve($id, $encode);	
     	}
     	if(!isset($this->name) || $this->deleted == 1){
     		$this->name = 	$this->getDefaultCurrencyName();
     		$this->symbol = '$';
     		$this->conversion_rate = 1;
     		$this->iso4217 = 'USD';
     		$this->id = '-99';
     		$this->deleted = 0;
     		$this->status = 'Active';
     		$this->hide = '<!--';
     		$this->unhide = '-->';
     	}
     	
     }
        

}


?>
