Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdefaultvathandler.php
Go to the documentation of this file.
1 <?php
20 class eZDefaultVATHandler
21 {
26 function getVatPercent( $object, $country )
27 {
28 $productCategory = eZDefaultVATHandler::getProductCategory( $object );
29
30 // If product category is not specified
31 if ( $productCategory === null )
32 {
33 // Default to a fake product category (*) that will produce
34 // weak match on category for any VAT rule.
35 $productCategory = new eZProductCategory( array( 'id' => -1,
36 'name' => '*' ) );
37 }
38
39 // If country is not specified
40 if ( !$country )
41 {
42 // Default to a fake country that will produce
43 // weak match on country for any VAT rule.
44 $country = '*';
45 }
46
47 $vatType = eZDefaultVATHandler::chooseVatType( $productCategory, $country );
48
49 return $vatType->attribute( 'percentage' );
50 }
51
58 function getProductCategory( $object )
59 {
60 $ini = eZINI::instance( 'shop.ini' );
61 if ( !$ini->hasVariable( 'VATSettings', 'ProductCategoryAttribute' ) )
62 {
63 eZDebug::writeError( "Cannot find product category: please specify its attribute identifier " .
64 "in the following setting: shop.ini.[VATSettings].ProductCategoryAttribute" );
65 return null;
66 }
67
68 $categoryAttributeName = $ini->variable( 'VATSettings', 'ProductCategoryAttribute' );
69
70 if ( !$categoryAttributeName )
71 {
72 eZDebug::writeError( "Cannot find product category: empty attribute name specified " .
73 "in the following setting: shop.ini.[VATSettings].ProductCategoryAttribute" );
74
75 return null;
76 }
77
78 $productDataMap = $object->attribute( 'data_map' );
79
80 if ( !isset( $productDataMap[$categoryAttributeName] ) )
81 {
82 eZDebug::writeError( "Cannot find product category: there is no attribute '$categoryAttributeName' in object '" .
83 $object->attribute( 'name' ) .
84 "' of class '" .
85 $object->attribute( 'class_name' ) . "'." );
86 return null;
87 }
88
89 $categoryAttribute = $productDataMap[$categoryAttributeName];
90 $productCategory = $categoryAttribute->attribute( 'content' );
91
92 if ( $productCategory === null )
93 {
94 eZDebug::writeNotice( "Product category is not specified in object '" .
95 $object->attribute( 'name' ) .
96 "' of class '" .
97 $object->attribute( 'class_name' ) . "'." );
98 return null;
99 }
100
101 return $productCategory;
102 }
103
134 function chooseVatType( $productCategory, $country )
135 {
136 $vatRules = eZVatRule::fetchList();
137
138 $catID = $productCategory->attribute( 'id' );
139
140 $vatPriorities = array();
141 foreach ( $vatRules as $rule )
142 {
143 $ruleCountry = $rule->attribute( 'country_code' );
144 $ruleCatIDs = $rule->attribute( 'product_categories_ids' );
145 $ruleVatID = $rule->attribute( 'vat_type' );
146
147 $categoryMatch = 0;
148 $countryMatch = 0;
149
150 if ( $ruleCountry == '*' )
151 $countryMatch = 1;
152 elseif ( $ruleCountry == $country )
153 $countryMatch = 2;
154
155 if ( !$ruleCatIDs )
156 $categoryMatch = 1;
157 elseif ( in_array( $catID, $ruleCatIDs ) )
158 $categoryMatch = 2;
159
160 if ( $countryMatch && $categoryMatch )
161 $vatPriority = $countryMatch * 2 + $categoryMatch - 2;
162 else
163 $vatPriority = 0;
164
165 if ( !isset( $vatPriorities[$vatPriority] ) )
166 $vatPriorities[$vatPriority] = $ruleVatID;
167 }
168
169 krsort( $vatPriorities, SORT_NUMERIC );
170
171
172 $bestPriority = 0;
173 if ( $vatPriorities )
174 {
175 $tmpKeys = array_keys( $vatPriorities );
176 $bestPriority = array_shift( $tmpKeys );
177 }
178
179 if ( $bestPriority == 0 )
180 {
181 eZDebug::writeError( "Cannot find a suitable VAT type " .
182 "for country '" . $country . "'" .
183 " and category '" . $productCategory->attribute( 'name' ). "'." );
184
185 return new eZVatType( array( "id" => 0,
186 "name" => ezpI18n::tr( 'kernel/shop', 'None' ),
187 "percentage" => 0.0 ) );
188 }
189
190 $bestVatTypeID = array_shift( $vatPriorities );
191 $bestVatType = eZVatType::fetch( $bestVatTypeID );
192
193 eZDebug::writeDebug(
194 sprintf( "Best matching VAT for '%s'/'%s' is '%s' (%d%%)",
195 $country,
196 $productCategory->attribute( 'name' ),
197 $bestVatType->attribute( 'name' ),
198 $bestVatType->attribute( 'percentage' ) ) );
199
200 return $bestVatType;
201 }
202 }
203
204 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeDebug
static writeDebug( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:588
eZDebug\writeNotice
static writeNotice( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:459
eZDefaultVATHandler
Default VAT handler.
Definition: ezdefaultvathandler.php:21
eZDefaultVATHandler\getProductCategory
getProductCategory( $object)
Definition: ezdefaultvathandler.php:58
eZDefaultVATHandler\chooseVatType
chooseVatType( $productCategory, $country)
Definition: ezdefaultvathandler.php:134
eZDefaultVATHandler\getVatPercent
getVatPercent( $object, $country)
Definition: ezdefaultvathandler.php:26
eZProductCategory
Handles product categories used by the default VAT handler.
Definition: ezproductcategory.php:18
eZVatRule\fetchList
static fetchList()
Definition: ezvatrule.php:79
eZVatType
eZVatType handles different VAT types
Definition: ezvattype.php:19
eZVatType\fetch
static fetch( $id, $asObject=true)
Definition: ezvattype.php:86
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.