00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 class eZDefaultVATHandler
00044 {
00045
00046
00047
00048
00049 function getVatPercent( $object, $country )
00050 {
00051 $productCategory = eZDefaultVATHandler::getProductCategory( $object );
00052
00053
00054 if ( $productCategory === null )
00055 {
00056 require_once( 'kernel/classes/ezproductcategory.php' );
00057
00058
00059
00060 $productCategory = new eZProductCategory( array( 'id' => -1,
00061 'name' => '*' ) );
00062 }
00063
00064
00065 if ( !$country )
00066 {
00067
00068
00069 $country = '*';
00070 }
00071
00072
00073
00074
00075
00076
00077 $vatType = eZDefaultVATHandler::chooseVatType( $productCategory, $country );
00078
00079 return $vatType->attribute( 'percentage' );
00080 }
00081
00082
00083
00084
00085
00086
00087
00088 function getProductCategory( $object )
00089 {
00090 $ini =& eZINI::instance( 'shop.ini' );
00091 if ( !$ini->hasVariable( 'VATSettings', 'ProductCategoryAttribute' ) )
00092 {
00093 eZDebug::writeError( "Cannot find product category: please specify its attribute identifier " .
00094 "in the following setting: shop.ini.[VATSettings].ProductCategoryAttribute" );
00095 return null;
00096 }
00097
00098 $categoryAttributeName = $ini->variable( 'VATSettings', 'ProductCategoryAttribute' );
00099
00100 if ( !$categoryAttributeName )
00101 {
00102 eZDebug::writeError( "Cannot find product category: empty attribute name specified " .
00103 "in the following setting: shop.ini.[VATSettings].ProductCategoryAttribute" );
00104
00105 return null;
00106 }
00107
00108 $productDataMap = $object->attribute( 'data_map' );
00109
00110 if ( !isset( $productDataMap[$categoryAttributeName] ) )
00111 {
00112 eZDebug::writeError( "Cannot find product category: there is no attribute '$categoryAttributeName' in object '" .
00113 $object->attribute( 'name' ) .
00114 "' of class '" .
00115 $object->attribute( 'class_name' ) . "'." );
00116 return null;
00117 }
00118
00119 $categoryAttribute = $productDataMap[$categoryAttributeName];
00120 $productCategory = $categoryAttribute->attribute( 'content' );
00121
00122 if ( $productCategory === null )
00123 {
00124 eZDebug::writeNotice( "Product category is not specified in object '" .
00125 $object->attribute( 'name' ) .
00126 "' of class '" .
00127 $object->attribute( 'class_name' ) . "'." );
00128 return null;
00129 }
00130
00131 return $productCategory;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 function chooseVatType( $productCategory, $country )
00165 {
00166 require_once( 'kernel/classes/ezvatrule.php' );
00167
00168 $vatRules = eZVatRule::fetchList();
00169
00170 $catID = $productCategory->attribute( 'id' );
00171
00172 $vatPriorities = array();
00173 foreach ( $vatRules as $rule )
00174 {
00175 $ruleCountry = $rule->attribute( 'country_code' );
00176 $ruleCatIDs = $rule->attribute( 'product_categories_ids' );
00177 $ruleVatID = $rule->attribute( 'vat_type' );
00178
00179 $categoryMatch = 0;
00180 $countryMatch = 0;
00181
00182 if ( $ruleCountry == '*' )
00183 $countryMatch = 1;
00184 elseif ( $ruleCountry == $country )
00185 $countryMatch = 2;
00186
00187 if ( !$ruleCatIDs )
00188 $categoryMatch = 1;
00189 elseif ( in_array( $catID, $ruleCatIDs ) )
00190 $categoryMatch = 2;
00191
00192 if ( $countryMatch && $categoryMatch )
00193 $vatPriority = $countryMatch * 2 + $categoryMatch - 2;
00194 else
00195 $vatPriority = 0;
00196
00197
00198
00199
00200
00201
00202
00203
00204 if ( !isset( $vatPriorities[$vatPriority] ) )
00205 $vatPriorities[$vatPriority] = $ruleVatID;
00206 }
00207
00208 krsort( $vatPriorities, SORT_NUMERIC );
00209
00210
00211 $bestPriority = 0;
00212 if ( $vatPriorities )
00213 $bestPriority = array_shift( array_keys( $vatPriorities ) );
00214
00215 if ( $bestPriority == 0 )
00216 {
00217 eZDebug::writeError( "Cannot find a suitable VAT type " .
00218 "for country '" . $country . "'" .
00219 " and category '" . $productCategory->attribute( 'name' ). "'." );
00220
00221 return new eZVATType( array( "id" => 0,
00222 "name" => ezi18n( 'kernel/shop', 'None' ),
00223 "percentage" => 0.0 ) );
00224 }
00225
00226 $bestVatTypeID = array_shift( $vatPriorities );
00227 $bestVatType = eZVatType::fetch( $bestVatTypeID );
00228
00229 eZDebug::writeDebug(
00230 sprintf( "Best matching VAT for '%s'/'%s' is '%s' (%d%%)",
00231 $country,
00232 $productCategory->attribute( 'name' ),
00233 $bestVatType->attribute( 'name' ),
00234 $bestVatType->attribute( 'percentage' ) ) );
00235
00236 return $bestVatType;
00237 }
00238 }
00239
00240 ?>