kernel/classes/vathandlers/ezdefaultvathandler.php
function chooseVatType( $productCategory, $country )
    {
        $vatRules = eZVatRule::fetchList();

        $catID = $productCategory->attribute( 'id' );

        $vatPriorities = array();
        foreach ( $vatRules as $rule )
        {
            $ruleCountry = $rule->attribute( 'country_code' );
            $ruleCatIDs  = $rule->attribute( 'product_categories_ids' );
            $ruleVatID   = $rule->attribute( 'vat_type' );

            $categoryMatch = 0;
            $countryMatch  = 0;

            if ( $ruleCountry == '*' )
                $countryMatch = 1;
            elseif ( $ruleCountry == $country )
                $countryMatch = 2;

            if ( !$ruleCatIDs )
                $categoryMatch = 1;
            elseif ( in_array( $catID, $ruleCatIDs ) )
                $categoryMatch = 2;

            if ( $countryMatch && $categoryMatch )
                $vatPriority = $countryMatch * 2 + $categoryMatch - 2;
            else
                $vatPriority = 0;

            if ( !isset( $vatPriorities[$vatPriority] ) )
                $vatPriorities[$vatPriority] = $ruleVatID;
        }

        krsort( $vatPriorities, SORT_NUMERIC );


        $bestPriority = 0;
        if ( $vatPriorities )
        {
            $tmpKeys = array_keys( $vatPriorities );
            $bestPriority = array_shift( $tmpKeys );
        }

        if ( $bestPriority == 0 )
        {
            eZDebug::writeError( "Cannot find a suitable VAT type " .
                                 "for country '" . $country . "'" .
                                 " and category '" . $productCategory->attribute( 'name' ). "'." );

            return new eZVatType( array( "id" => 0,
                                         "name" => ezpI18n::tr( 'kernel/shop', 'None' ),
                                         "percentage" => 0.0 ) );
        }

        $bestVatTypeID = array_shift( $vatPriorities );
        $bestVatType = eZVatType::fetch( $bestVatTypeID );

        eZDebug::writeDebug(
            sprintf( "Best matching VAT for '%s'/'%s' is '%s' (%d%%)",
                     $country,
                     $productCategory->attribute( 'name' ),
                     $bestVatType->attribute( 'name' ),
                     $bestVatType->attribute( 'percentage' ) ) );

        return $bestVatType;
    }
function chooseVatType( $productCategory, $country )
    {
        $vatRules = eZVatRule::fetchList();

        $catID = $productCategory->attribute( 'id' );

        $vatPriorities = array();
        foreach ( $vatRules as $rule )
        {
            $ruleCountry = $rule->attribute( 'country_code' );
            $ruleCatIDs  = $rule->attribute( 'product_categories_ids' );
            $ruleVatID   = $rule->attribute( 'vat_type' );

            $categoryMatch = 0;
            $countryMatch  = 0;

            if ( $ruleCountry == '*' )
                $countryMatch = 1;
            elseif ( $ruleCountry == $country )
                $countryMatch = 2;

            if ( !$ruleCatIDs )
                $categoryMatch = 1;
            elseif ( in_array( $catID, $ruleCatIDs ) )
                $categoryMatch = 2;

            if ( $countryMatch && $categoryMatch )
                $vatPriority = $countryMatch * 2 + $categoryMatch - 2;
            else
                $vatPriority = 0;

            if ( !isset( $vatPriorities[$vatPriority] ) )
                $vatPriorities[$vatPriority] = $ruleVatID;
        }

        krsort( $vatPriorities, SORT_NUMERIC );


        $bestPriority = 0;
        if ( $vatPriorities )
        {
            $tmpKeys = array_keys( $vatPriorities );
            $bestPriority = array_shift( $tmpKeys );
        }

        if ( $bestPriority == 0 )
        {
            eZDebug::writeError( "Cannot find a suitable VAT type " .
                                 "for country '" . $country . "'" .
                                 " and category '" . $productCategory->attribute( 'name' ). "'." );

            return new eZVatType( array( "id" => 0,
                                         "name" => ezpI18n::tr( 'kernel/shop', 'None' ),
                                         "percentage" => 0.0 ) );
        }

        $bestVatTypeID = array_shift( $vatPriorities );
        $bestVatType = eZVatType::fetch( $bestVatTypeID );

        eZDebug::writeDebug(
            sprintf( "Best matching VAT for '%s'/'%s' is '%s' (%d%%)",
                     $country,
                     $productCategory->attribute( 'name' ),
                     $bestVatType->attribute( 'name' ),
                     $bestVatType->attribute( 'percentage' ) ) );

        return $bestVatType;
    }
function chooseVatType ( $ productCategory , $ country ) { $ vatRules = eZVatRule :: fetchList ( ) ; $ catID = $ productCategory -> attribute ( 'id' ) ; $ vatPriorities = array ( ) ; foreach ( $ vatRules as $ rule ) { $ ruleCountry = $ rule -> attribute ( 'country_code' ) ; $ ruleCatIDs = $ rule -> attribute ( 'product_categories_ids' ) ; $ ruleVatID = $ rule -> attribute ( 'vat_type' ) ; $ categoryMatch = 0 ; $ countryMatch = 0 ; if ( $ ruleCountry == '*' ) $ countryMatch = 1 ; elseif ( $ ruleCountry == $ country ) $ countryMatch = 2 ; if ( ! $ ruleCatIDs ) $ categoryMatch = 1 ; elseif ( in_array ( $ catID , $ ruleCatIDs ) ) $ categoryMatch = 2 ; if ( $ countryMatch && $ categoryMatch ) $ vatPriority = $ countryMatch * 2 + $ categoryMatch - 2 ; else $ vatPriority = 0 ; if ( ! isset ( $ vatPriorities [ $ vatPriority ] ) ) $ vatPriorities [ $ vatPriority ] = $ ruleVatID ; } krsort ( $ vatPriorities , SORT_NUMERIC ) ; $ bestPriority = 0 ; if ( $ vatPriorities ) { $ tmpKeys = array_keys ( $ vatPriorities ) ; $ bestPriority = array_shift ( $ tmpKeys ) ; } if ( $ bestPriority == 0 ) { eZDebug :: writeError ( "Cannot find a suitable VAT type " . "for country '" . $ country . "'" . " and category '" . $ productCategory -> attribute ( 'name' ) . "'." ) ; return new eZVatType ( array ( "id" => 0 , "name" => ezpI18n :: tr ( 'kernel/shop' , 'None' ) , "percentage" => 0.0 ) ) ; } $ bestVatTypeID = array_shift ( $ vatPriorities ) ; $ bestVatType = eZVatType :: fetch ( $ bestVatTypeID ) ; eZDebug :: writeDebug ( sprintf ( "Best matching VAT for '%s'/'%s' is '%s' (%d%%)" , $ country , $ productCategory -> attribute ( 'name' ) , $ bestVatType -> attribute ( 'name' ) , $ bestVatType -> attribute ( 'percentage' ) ) ) ; return $ bestVatType ; }
Choose the best matching VAT type for given product category and country.

We calculate priority for each VAT type and then choose
the VAT type having the highest priority
(or first of those having the highest priority).

VAT type priority is calculated from county match and category match as following:

CountryMatch  = 0
CategoryMatch = 1

if ( there is exact match on country )
CountryMatch = 2
elseif ( there is weak match on country )
CountryMatch = 1

if ( there is exact match on product category )
CategoryMatch = 2
elseif ( there is weak match on product category )
CategoryMatch = 1

if ( there is match on both country and category )
VatTypePriority = CountryMatch * 2 + CategoryMatch - 2
else
VatTypePriority = 0

\private
\static
Choose the best matching VAT type for given product category and country .