Location: PHPKode > scripts > Taxation laws validator > taxation-laws-validator/code_fiscal.class.php
<?php

/**
 * Validate fiscal code in Tunisia, apply a fix for taxes if necessary.
 * 
 * 
 * Class pour la validation du code fiscal de la tunisie
 * et de la TVA par rapport au code fiscal.
 * 
 * @package Gonx
 * @author Ben Yacoub Hatem <hide@address.com>
 * @copyright Copyright (c) 2004
 * @version $Id$ - 25/02/2004 10:44:53 - code_fiscal.class.php
 * @license Free for non commercial use
 * @access public
 **/
 
define ("__CODEFISCAL_EMPTY","Empty Tunisian Fiscal code");
define ("__CODEFISCAL_INCORRECT","Incorrect Tunisian Fiscal code");
 
class codefiscal{
	/**
     * Constructor
     * @access protected
     */
	function codefiscal(){
		
	}
	
	var $assujete_tva = array("A","B","P");
	var $majoration = 0.25;
	var $code;
	var $parsed;

	/**
	 * codefiscal::is_valid()		Validate fiscal code of a company
	 * 
	 * @param string $code
	 * @return boolean	true if valid code, or false
	 **/
	function is_valid($code = "",&$err){
		$code = trim(strtoupper($code));
		if ($code == "") {
		    $err = __CODEFISCAL_EMPTY;
			return FALSE;
		}

		if (!preg_match("|(\d{5})(\w{1})/(\w{1})/(\w{1})/(\d{3})|i",$code,$matches )) {
		    $err = __CODEFISCAL_INCORRECT;
			return FALSE;
		}
		$this->code = $code;
		$this->parsed = $matches;
		return TRUE;
	}
	
	/**
	 * codefiscal::fix_tva()		Fix the Taxe of a product
	 * 
	 * @param float $tva
	 * @return float $tva	the fixed value of the tva
	 **/
	function fix_tva($tva){
		if (!isset($this->code) or !$this->is_valid($this->code,$err)) {
		    return FALSE;
		}
		if (in_array($this->parsed[3],$this->assujete_tva )) {
		    return $tva;
		}
		if ($this->is_assujete()) {
		    return $tva;
		}
		return ($tva+($tva*$this->majoration));
	}
	
	/**
	 * codefiscal::is_assujete()		return true if a compagny
	 * don't have to pay supplementary Taxe.
	 * 
	 * @return true of false
	 **/
	function is_assujete(){
		if (!isset($this->code) or !$this->is_valid($this->code,$err)) {
		    return FALSE;
		}
		if (in_array($this->parsed[3],$this->assujete_tva )) {
		    return true;
		}
		return FALSE;
	}
	
}


?>
Return current item: Taxation laws validator