<?php
/*********************************************************************************
 * The contents of this file are subject to the TimeTrex Public License Version
 * 1.1.0 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.TimeTrex.com/TPL
 * 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 TimeTrex" logo and
 *    (ii) the TimeTrex copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: TimeTrex Open Source
 * The Initial Developer of the Original Code is TimeTrex Payroll Services
 * Portions created by TimeTrex are Copyright (C) 2004-2007 TimeTrex Payroll Services;
 * All Rights Reserved.
 *
 ********************************************************************************/
/*
 * $Revision: 2158 $
 * $Id: PayrollDeduction_Base.class.php 2158 2008-09-15 23:26:15Z ipso $
 * $Date: 2008-09-15 16:26:15 -0700 (Mon, 15 Sep 2008) $
 */

/**
 * @package PayrollDeduction
 */
class PayrollDeduction_Base {
	var $data = array();

	function setCompany($company_id) {
		$this->data['company_id'] = $company_id;

		return TRUE;
	}
	function getCompany() {
		if ( isset($this->data['company_id']) ) {
			return $this->data['company_id'];
		}

		return FALSE;
	}

	function setUser($user_id) {
		$this->data['user_id'] = $user_id;

		return TRUE;
	}
	function getUser() {
		if ( isset($this->data['user_id']) ) {
			return $this->data['user_id'];
		}

		return FALSE;
	}

	function setCountry($country) {
		$this->data['country'] = strtoupper(trim($country));

		return TRUE;
	}
	function getCountry() {
		if ( isset($this->data['country']) ) {
			return $this->data['country'];
		}

		return FALSE;
	}

	function setProvince($province) {
		$this->data['province'] = strtoupper(trim($province));

		return TRUE;
	}
	function getProvince() {
		if ( isset($this->data['province']) ) {
			return $this->data['province'];
		}

		return FALSE;
	}

	function setDistrict($district) {
		$this->data['district'] = strtoupper(trim($district));

		return TRUE;
	}
	function getDistrict() {
		if ( isset($this->data['district']) ) {
			return $this->data['district'];
		}

		return FALSE;
	}

	//
	// Generic
	//

	function setUserValue1($value) {
		$this->data['user_value1'] = $value;

		return TRUE;
	}
	function getUserValue1() {
		if ( isset($this->data['user_value1']) ) {
			return $this->data['user_value1'];
		}

		return FALSE;
	}

	function setUserValue2($value) {
		$this->data['user_value2'] = $value;

		return TRUE;
	}
	function getUserValue2() {
		if ( isset($this->data['user_value2']) ) {
			return $this->data['user_value2'];
		}

		return FALSE;
	}

	function setUserValue3($value) {
		$this->data['user_value3'] = $value;

		return TRUE;
	}
	function getUserValue3() {
		if ( isset($this->data['user_value3']) ) {
			return $this->data['user_value3'];
		}

		return FALSE;
	}

	function setUserValue4($value) {
		$this->data['user_value4'] = $value;

		return TRUE;
	}
	function getUserValue4() {
		if ( isset($this->data['user_value4']) ) {
			return $this->data['user_value4'];
		}

		return FALSE;
	}

	function setDate($epoch) {
		$this->data['date'] = $epoch;

		return TRUE;
	}
	function getDate() {
		if ( isset($this->data['date']) ) {
			return $this->data['date'];
		}

		return FALSE;
	}

	function setAnnualPayPeriods($value) {
		$this->data['annual_pay_periods'] = $value;

		return TRUE;
	}
	function getAnnualPayPeriods() {
		if ( isset($this->data['annual_pay_periods']) ) {
			return $this->data['annual_pay_periods'];
		}

		return FALSE;
	}

	function getCountryPrimaryCurrency() {
		if ( isset($this->country_primary_currency) AND $this->country_primary_currency != '' ) {
			return $this->country_primary_currency;
		}

		return FALSE;
	}

	function getCountryPrimaryCurrencyID() {
		$iso_code = $this->getCountryPrimaryCurrency(); //ISO Code

		if ( $iso_code != '' AND is_numeric( $this->getCompany() ) ) {
			$clf = new CurrencyListFactory();
			$clf->getByCompanyIdAndISOCode( $this->getCompany(), $iso_code );
			if ( $clf->getRecordCount() > 0 ) {
				$currency_id = $clf->getCurrent()->getId();
				Debug::Text('Country Primary Currency ID: '. $currency_id , __FILE__, __LINE__, __METHOD__, 10 );
				return $currency_id;
			}
		}

		Debug::Text('Country Primary Currency does not exist: '. $iso_code , __FILE__, __LINE__, __METHOD__, 10 );
		return FALSE;
	}

	// OSC: Set the user currency for calculations
	function setUserCurrency( $currency_id ) {
		Debug::Text('Settitng currency for calculate income tax: '. $currency_id, __FILE__, __LINE__, __METHOD__, 10 );

		$this->data['user_currency_id'] = $currency_id;

		return TRUE;
	}

	// OSC: Get the user currency for calculations
	function getUserCurrency() {
		if ( isset($this->data['user_currency_id']) ) {
			Debug::Text('Currency income: '. $this->data['user_currency_id'], __FILE__, __LINE__, __METHOD__, 10);

			return $this->data['user_currency_id'];
		}

		//If no currency is set, return the country primary currency, so no conversion takes place.
		return FALSE;
	}

	function setGrossPayPeriodIncome($income) {
		//A = Annual Taxable Income
		Debug::text('Setting gross pay period income: '. $income, __FILE__, __LINE__, __METHOD__,10);
		$income = $this->convertToCountryCurrency( $income );
		Debug::text('Setting converted gross pay period income: '. $income, __FILE__, __LINE__, __METHOD__,10);

		$this->data['gross_pay_period_income'] = $income;

		return TRUE;
	}

	function getGrossPayPeriodIncome() {
		if ( isset($this->data['gross_pay_period_income']) ) {
			Debug::text('Gross Pay Period Income: I: '. $this->data['gross_pay_period_income'], __FILE__, __LINE__, __METHOD__,10);

			return $this->data['gross_pay_period_income'];
		}

		return FALSE;
	}

	// OSC: This function convert '$amount' from the user currency, to the country currency for calculations
	function convertToCountryCurrency($amount) {
		$user_currency_id = $this->getUserCurrency();
		$country_currency_id = $this->getCountryPrimaryCurrencyID();

		if ( $user_currency_id !== FALSE AND $country_currency_id !== FALSE ) {
			$retval = CurrencyFactory::convertCurrency( $this->getUserCurrency(), $this->getCountryPrimaryCurrencyID(), $amount);
		} else {
			//Conversion failed, return original amount.
			$retval = $amount;
		}

		return $retval;
	}

	// OSC: This function convert '$amount' from the country currency, to the user currency.
	function convertToUserCurrency($amount) {
		$user_currency_id = $this->getUserCurrency();
		$country_currency_id = $this->getCountryPrimaryCurrencyID();

		if ( $user_currency_id !== FALSE AND $country_currency_id !== FALSE ) {
			$retval =  CurrencyFactory::convertCurrency( $this->getCountryPrimaryCurrencyID(), $this->getUserCurrency(), $amount);
		} else {
			$retval = $amount;
		}

		return $retval;
	}

	protected function getDataFromRateArray($epoch, $arr) {
		if ( !is_array($arr) ) {
			return FALSE;
		}

		if ( $epoch == '' ) {
			return FALSE;
		}

		krsort($arr);
		foreach( $arr as $date => $val ) {
			if ( $epoch >= $date ) {
				return $val;
			}
		}

		return FALSE;
	}

}
?>
