<?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: US.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_US extends PayrollDeduction_US_Data {
	//
	// Federal
	//
	function setFederalFilingStatus($value) {
		$this->data['federal_filing_status'] = $value;

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

		return 10; //Single
	}

	function setFederalAllowance($value) {
		$this->data['federal_allowance'] = $value;

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

		return FALSE;
	}

	function setFederalAdditionalDeduction($value) {
		$this->data['federal_additional_deduction'] = $value;

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

		return FALSE;
	}

	function setEICFilingStatus($value) {
		$this->data['eic_filing_status'] = $value;

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

		return FALSE;
	}

	function setYearToDateSocialSecurityContribution($value) {
		if ( $value > 0 ) {
			$this->data['social_security_ytd_contribution'] = $value;

			return TRUE;
		}

		return FALSE;
	}
	function getYearToDateSocialSecurityContribution() {
		if (isset($this->data['social_security_ytd_contribution'])) {
			return $this->data['social_security_ytd_contribution'];
		}

		return 0;
	}

	function setYearToDateFederalUIContribution($value) {
		if ( $value > 0 ) {
			$this->data['federal_ui_ytd_contribution'] = $value;

			return TRUE;
		}

		return FALSE;
	}
	function getYearToDateFederalUIContribution() {
		if (isset($this->data['federal_ui_ytd_contribution'])) {
			return $this->data['federal_ui_ytd_contribution'];
		}

		return 0;
	}

	function setFederalTaxExempt($value) {
		$this->data['federal_tax_exempt'] = $value;

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

		return FALSE;
	}

	//
	// State
	//
	function setStateFilingStatus($value) {
		$this->data['state_filing_status'] = $value;

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

		return 10; //Single
	}

	function setStateAllowance($value) {
		$this->data['state_allowance'] = $value;

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

		return FALSE;
	}

	function setStateAdditionalDeduction($value) {
		$this->data['state_additional_deduction'] = $value;

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

		return FALSE;
	}


	//
	// District
	//

	//Generic district functions that handle straight percentages for any district unless otherwise overloaded.
	//for custom formulas.
	function getDistrictPayPeriodDeductions() {
		return bcdiv($this->getDistrictTaxPayable(), $this->getAnnualPayPeriods() );
	}

	function getDistrictAnnualTaxableIncome() {
		$annual_income = $this->getAnnualTaxableIncome();

		return $annual_income;
	}

	function getDistrictTaxPayable() {
		$annual_income = $this->getDistrictAnnualTaxableIncome();

		if ( $annual_income > 0 ) {
			$rate = bcdiv( $this->getUserValue2(), 100);
			$retval = bcmul( $annual_income, $rate);
		}

		if ( !isset($retval) OR $retval < 0 ) {
			$retval = 0;
		}

		Debug::text('zzDistrict Annual Tax Payable: '. $retval .' User Value 2: '. $this->getUserValue2() .' Annual Income: '. $annual_income, __FILE__, __LINE__, __METHOD__,10);

		return $retval;
	}

	function setDistrictFilingStatus($value) {
		$this->data['district_filing_status'] = $value;

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

		return 10; //Single
	}

	function setDistrictAllowance($value) {
		$this->data['district_allowance'] = $value;

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

		return FALSE;
	}

	function setYearToDateStateUIContribution($value) {
		if ( $value > 0 ) {
			$this->data['state_ui_ytd_contribution'] = $value;

			return TRUE;
		}

		return FALSE;
	}
	function getYearToDateStateUIContribution() {
		if (isset($this->data['state_ui_ytd_contribution'])) {
			return $this->data['state_ui_ytd_contribution'];
		}

		return 0;
	}

	function setStateUIRate($value) {
		if ( $value > 0 ) {
			$this->data['state_ui_rate'] = $value;

			return TRUE;
		}

		return FALSE;
	}
	function getStateUIRate() {
		if (isset($this->data['state_ui_rate'])) {
			return $this->data['state_ui_rate'];
		}

		return 0;
	}

	function setStateUIWageBase($value) {
		if ( $value > 0 ) {
			$this->data['state_ui_wage_base'] = $value;

			return TRUE;
		}

		return FALSE;
	}
	function getStateUIWageBase() {
		if (isset($this->data['state_ui_wage_base'])) {
			return $this->data['state_ui_wage_base'];
		}

		return 0;
	}

	function setProvincialTaxExempt($value) {
		$this->data['provincial_tax_exempt'] = $value;

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

		return FALSE;
	}

	function setSocialSecurityExempt($value) {
		$this->data['social_security_exempt'] = $value;

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

		return FALSE;
	}

	function setMedicareExempt($value) {
		$this->data['medicare_exempt'] = $value;

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

		return FALSE;
	}

	function setUIExempt($value) {
		$this->data['ui_exempt'] = $value;

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

		return FALSE;
	}

	//
	// Calculation Functions
	//
	function getAnnualTaxableIncome() {

		$retval = bcmul( $this->getGrossPayPeriodIncome(), $this->getAnnualPayPeriods());

		Debug::text('Annual Taxable Income: '. $retval, __FILE__, __LINE__, __METHOD__,10);

		return $retval;
	}

	//
	// Federal Tax
	//
	function getFederalPayPeriodDeductions() {
		return bcdiv( $this->getFederalTaxPayable(), $this->getAnnualPayPeriods() );
	}

	function getFederalTaxPayable() {
		if ( $this->getFederalTaxExempt() == TRUE ) {
			Debug::text('Federal Tax Exempt!', __FILE__, __LINE__, __METHOD__,10);
			return 0;
		}

		$annual_taxable_income = $this->getAnnualTaxableIncome();
		$annual_allowance = bcmul($this->getFederalAllowanceAmount( $this->getDate() ), $this->getFederalAllowance() );

		Debug::text('Annual Taxable Income: '. $annual_taxable_income, __FILE__, __LINE__, __METHOD__,10);
		Debug::text('Allowance: '. $annual_allowance, __FILE__, __LINE__, __METHOD__,10);

		if ( $annual_taxable_income > $annual_allowance ) {
			$modified_annual_taxable_income = bcsub( $annual_taxable_income, $annual_allowance );
/*
			$federal_constant = $this->getData()->getFederalConstant( $modified_annual_taxable_income );
			$federal_rate = $this->getData()->getFederalRate( $modified_annual_taxable_income );

			$retval = bcmul( bcsub($modified_annual_taxable_income, $federal_constant), $federal_rate);
*/
			$rate = $this->getData()->getFederalRate($modified_annual_taxable_income);
			$federal_constant = $this->getData()->getFederalConstant($modified_annual_taxable_income);
			$federal_rate_income = $this->getData()->getFederalRatePreviousIncome($modified_annual_taxable_income);

			$retval = bcadd( bcmul( bcsub( $modified_annual_taxable_income, $federal_rate_income ), $rate ), $federal_constant );
		} else {
			Debug::text('Income is less then allowance: ', __FILE__, __LINE__, __METHOD__,10);

			$retval = 0;
		}

		if ( $retval < 0 ) {
			$retval = 0;
		}

		Debug::text('RetVal: '. $retval, __FILE__, __LINE__, __METHOD__,10);

		return $retval;
	}

	//
	// Social Security
	//
	function getEmployeeSocialSecurity() {
		if ( $this->getSocialSecurityExempt() == TRUE ) {
			return 0;
		}

		$pay_period_income = $this->getGrossPayPeriodIncome();
		$rate = bcdiv( $this->getSocialSecurityRate(), 100 );
		$maximum_contribution = $this->getSocialSecurityMaximumContribution();
		$ytd_contribution = $this->getYearToDateSocialSecurityContribution();

		Debug::text('Rate: '. $rate .' YTD Contribution: '. $ytd_contribution, __FILE__, __LINE__, __METHOD__,10);

		$amount = bcmul( $pay_period_income, $rate );
		$max_amount = bcsub( $maximum_contribution, $ytd_contribution );

		if ( $amount > $max_amount ) {
			$retval = $max_amount;
		} else {
			$retval = $amount;
		}

		return $retval;
	}

	function getEmployerSocialSecurity() {
		return $this->getEmployeeSocialSecurity();
	}

	//
	// Medicare
	//
	function getEmployeeMedicare() {
		if ( $this->getMedicareExempt() == TRUE ) {
			return 0;
		}

		$pay_period_income = $this->getGrossPayPeriodIncome();
		$rate = bcdiv( $this->getMedicareRate(), 100 );

		Debug::text('Rate: '. $rate, __FILE__, __LINE__, __METHOD__,10);

		$amount = bcmul( $pay_period_income, $rate );

		return $amount;
	}

	function getEmployerMedicare() {
		return $this->getEmployeeMedicare();
	}

	//
	// Federal UI
	//
	function getFederalEmployerUI() {
		if ( $this->getUIExempt() == TRUE ) {
			return 0;
		}

		$pay_period_income = $this->getGrossPayPeriodIncome();
		$rate = bcdiv( $this->getFederalUIRate(), 100 );
		$maximum_contribution = $this->getFederalUIMaximumContribution();
		$ytd_contribution = $this->getYearToDateFederalUIContribution();

		Debug::text('Rate: '. $rate .' YTD Contribution: '. $ytd_contribution .' Maximum: '. $maximum_contribution, __FILE__, __LINE__, __METHOD__,10);

		$amount = bcmul( $pay_period_income, $rate );
		$max_amount = bcsub( $maximum_contribution, $ytd_contribution );

		if ( $amount > $max_amount ) {
			$retval = $max_amount;
		} else {
			$retval = $amount;
		}

		return $retval;
	}

	function getPayPeriodTaxDeductions() {
		return bcadd( $this->getFederalPayPeriodDeductions(), $this->getStatePayPeriodDeductions() );
	}

	function getPayPeriodEmployeeTotalDeductions() {
		//return $this->getPayPeriodTaxDeductions() + $this->getEmployeeCPP() + $this->getEmployeeEI();
		return bcadd( bcadd( $this->getPayPeriodTaxDeductions(), $this->getEmployeeSocialSecurity() ), $this->getEmployeeMedicare() );
	}

	function getPayPeriodEmployeeNetPay() {
		return bcsub( $this->getGrossPayPeriodIncome(), $this->getPayPeriodEmployeeTotalDeductions() );
	}

	function RoundNearestDollar($amount) {
		return round($amount,0);
	}

	//
	// Earning Income Tax Credit (EIC, EITC)
	//
	function getEIC() {
		$eic_options = $this->getEICRateArray( $this->getAnnualTaxableIncome(), $this->getEICFilingStatus() );
		//Debug::Arr($eic_options, ' EIC Options: ', __FILE__, __LINE__, __METHOD__,10);

		if ( is_array($eic_options) AND isset($eic_options['calculation_type']) ) {
			$retval = 0;
			switch	($eic_options['calculation_type']) {
				case 10: //Percent
					if ( isset($eic_options['percent']) ) {
						$retval = bcmul( bcdiv($eic_options['percent'],100), $this->getAnnualTaxableIncome() );
					}
					break;
				case 20: //Amount
					if ( isset($eic_options['amount']) ) {
						$retval = $eic_options['amount'];
					}
					break;
				case 30: //Amount less percent
					if ( isset($eic_options['percent']) AND isset($eic_options['amount']) AND isset($eic_options['income']) ) {
						$retval = bcsub( $eic_options['amount'], bcmul( bcdiv($eic_options['percent'],100), bcsub( $this->getAnnualTaxableIncome(), $eic_options['income'] ) ) );
					}
					break;
			}
			Debug::Text(' Type: '. $eic_options['calculation_type'].' Annual Amount: '. $retval, __FILE__, __LINE__, __METHOD__,10);

			if ( isset($retval) AND $retval > 0 ) {
				$retval = bcdiv($retval, $this->getAnnualPayPeriods() );

				Debug::Text('EIC Amount: '. $retval, __FILE__, __LINE__, __METHOD__,10);
				return $retval*-1;
			} else {
				Debug::Text('Calculation didnt return valid amount...', __FILE__, __LINE__, __METHOD__,10);
			}
		}

		return 0;
	}

	/*
		Use this to get all useful values.
	*/
	function getArray() {

		$array = array(
						'gross_pay' => $this->getGrossPayPeriodIncome(),
						'federal_tax' => $this->getFederalPayPeriodDeductions(),
						'state_tax' => $this->getStatePayPeriodDeductions(),
/*
						'employee_social_security' => $this->getEmployeeSocialSecurity(),
						'employer_social_security' => $this->getEmployeeSocialSecurity(),
						'employee_medicare' => $this->getEmployeeMedicare(),
						'employer_medicare' => $this->getEmployerMedicare(),
*/
						'employee_social_security' => $this->getEmployeeSocialSecurity(),
						'federal_employer_ui' => $this->getFederalEmployerUI(),
//						'state_employer_ui' => $this->getStateEmployerUI(),

						);

		Debug::Arr($array, 'Deductions Array:', __FILE__, __LINE__, __METHOD__,10);

		return $array;
	}
}
?>