Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezvattype.php
Go to the documentation of this file.
1 <?php
18 class eZVatType extends eZPersistentObject
19 {
20 public $Percentage;
21 static function definition()
22 {
23 return array( "fields" => array( "id" => array( 'name' => 'ID',
24 'datatype' => 'integer',
25 'default' => 0,
26 'required' => true ),
27 "name" => array( 'name' => "Name",
28 'datatype' => 'string',
29 'default' => '',
30 'required' => true ),
31 "percentage" => array( 'name' => "Percentage",
32 'datatype' => 'float',
33 'default' => 0,
34 'required' => true ) ),
35 "function_attributes" => array( 'is_dynamic' => 'isDynamic' ),
36 "keys" => array( "id" ),
37 "increment_key" => "id",
38 "class_name" => "eZVatType",
39 "name" => "ezvattype" );
40 }
41
42 function getPercentage( $object, $country )
43 {
44 if ( $this->ID == -1 )
45 {
46 $percentage = eZVATManager::getVAT( $object, $country );
47 if ( $percentage === null )
48 $percentage = -1; // indicate that VAT percentage is unknown
49 }
50 else
51 $percentage = $this->Percentage;
52
53 return $percentage;
54 }
55
56 static function dynamicVatType( $asObject = true )
57 {
58 $row = array( 'id' => -1,
59 'name' => eZVatType::dynamicVatTypeName(),
60 'percentage' => 0.0 );
61
62 if ( !$asObject )
63 return $row;
64
65 return new eZVatType( $row );
66 }
67
74 static function dynamicVatTypeName()
75 {
76 if ( !isset( $GLOBALS['eZVatType_dynamicVatTypeName'] ) )
77 {
78 $shopINI = eZINI::instance( 'shop.ini' );
79 $desc = $shopINI->variable( 'VATSettings', 'DynamicVatTypeName' );
80 $GLOBALS['eZVatType_dynamicVatTypeName'] = $desc;
81 }
82
83 return $GLOBALS['eZVatType_dynamicVatTypeName'];
84 }
85
86 static function fetch( $id, $asObject = true )
87 {
88 if ( $id == -1 && eZVATManager::isDynamicVatChargingEnabled() )
89 return eZVatType::dynamicVatType( $asObject );
90
91 return eZPersistentObject::fetchObject( eZVatType::definition(),
92 null,
93 array( "id" => $id ),
94 $asObject );
95 }
96
97 function isDynamic()
98 {
99 return ( $this->ID == -1 );
100 }
101
105 static function fetchList( $asObject = true, $skipDynamic = false )
106 {
107 // Fetch "real" VAT types, stored in DB.
108 $VATTypes = eZPersistentObject::fetchObjectList( eZVatType::definition(),
109 null, null, array( 'id' => false ), null,
110 $asObject );
111 if ( !$VATTypes )
112 $VATTypes = array();
113
114 // Add "fake" VAT type: dynamic.
115 if ( !$skipDynamic )
116 {
117 if ( eZVATManager::isDynamicVatChargingEnabled() )
118 $VATTypes[] = eZVatType::dynamicVatType( $asObject );
119 }
120
121 return $VATTypes;
122 }
123
132 static function fetchDependentProductsCount( $vatID )
133 {
134 $vatID = (int) $vatID; // prevent SQL injection
135
136 // We need DISTINCT here since there might be several object translations.
137 $query = "SELECT COUNT(DISTINCT coa.contentobject_id) AS count " .
138 "FROM ezcontentobject_attribute coa, ezcontentobject co " .
139 "WHERE " .
140 "coa.contentobject_id=co.id AND " .
141 "coa.version=co.current_version AND " .
142 "data_type_string IN ('ezprice', 'ezmultiprice') " .
143 "AND data_text LIKE '$vatID,%'";
144
145 $db = eZDB::instance();
146 $rslt = $db->arrayQuery( $query );
147 $nProducts = $rslt[0]['count'];
148 return $nProducts;
149 }
150
158 static function fetchDependentClassesCount( $vatID )
159 {
160 $vatID = (int) $vatID; // prevent SQL injection
161
162 $query = "SELECT COUNT(DISTINCT cc.id) AS count " .
163 "FROM ezcontentclass cc, ezcontentclass_attribute cca " .
164 "WHERE cc.id=cca.contentclass_id AND " .
165 "cca.data_type_string IN ('ezprice', 'ezmultiprice') AND data_float1=$vatID";
166
167 $db = eZDB::instance();
168 $rslt = $db->arrayQuery( $query );
169 $nClasses = $rslt[0]['count'];
170 return $nClasses;
171 }
172
173 static function create()
174 {
175 $row = array(
176 "id" => null,
177 "name" => ezpI18n::tr( 'kernel/shop', 'VAT type' ),
178 "percentage" => 0.0 );
179 return new eZVatType( $row );
180 }
181
190 static function resetToDefaultInProducts( $oldVAT )
191 {
192 $db = eZDB::instance();
193 $db->begin();
194
195 $selectProductsQuery =
196 "SELECT coa.id, data_text, cca.data_float1 AS default_vat " .
197 "FROM ezcontentclass cc, ezcontentclass_attribute cca, ezcontentobject_attribute coa, ezcontentobject co " .
198 "WHERE " .
199 "cc.id=cca.contentclass_id AND " .
200 "cca.id=coa.contentclassattribute_id AND " .
201 "coa.contentobject_id=co.id AND " .
202 "coa.version=co.current_version AND " .
203 "cca.data_type_string IN ('ezprice', 'ezmultiprice') " .
204 "AND data_text LIKE '$oldVAT,%'";
205
206 // Fetch the attributes by small portions to avoid memory overflow.
207 for ( $offset = 0; true; $offset += 50 )
208 {
209 $rows = $db->arrayQuery( $selectProductsQuery, array( 'offset' => $offset, 'limit' => 50 ) );
210 if ( !$rows )
211 break;
212
213 foreach ( $rows as $row )
214 {
215 list( $oldVatType, $vatExInc ) = explode( ',', $row['data_text'], 2 );
216 $updateQuery = "UPDATE ezcontentobject_attribute " .
217 "SET data_text = '" . $row['default_vat'] . ",$vatExInc' " .
218 "WHERE id=" . $row['id'];
219 $db->query( $updateQuery );
220 }
221
222 if ( count( $rows ) < 50 )
223 break;
224 }
225
226 $db->commit();
227 }
228
239 function removeThis()
240 {
241 $vatID = $this->ID;
242 $db = eZDB::instance();
243 $db->begin();
244
245 // remove dependent VAT rules
246 $dependentRules = eZVatRule::fetchByVatType( $vatID );
247 foreach ( $dependentRules as $rule )
248 {
249 eZVatRule::removeVatRule( $rule->attribute( 'id' ) );
250 }
251
252 // replace VAT type in dependent products.
253 eZVatType::resetToDefaultInProducts( $vatID );
254
255 // Remove the VAT type itself.
256 eZPersistentObject::removeObject( eZVatType::definition(),
257 array( "id" => $vatID ) );
258
259 $db->commit();
260 }
261
262 function VATTypeList()
263 {
264 if ( !isset( $this->VatTypeList ) )
265 {
266 $this->VatTypeList = eZVatType::fetchList();
267 if ( !isset( $this->VatTypeList ) )
268 $this->VatTypeList = array();
269 }
270
271 return $this->VatTypeList;
272 }
273
274 public $VatTypeList;
275 }
276
277 ?>
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZPersistentObject
Definition: ezpersistentobject.php:33
eZPersistentObject\fetchObject
static fetchObject( $def, $field_filters, $conds, $asObject=true, $grouping=null, $custom_fields=null)
Definition: ezpersistentobject.php:213
eZPersistentObject\$ID
$ID
Definition: ezpersistentobject.php:1448
eZPersistentObject\removeObject
static removeObject( $def, $conditions=null, $extraConditions=null)
Definition: ezpersistentobject.php:271
eZPersistentObject\count
static count( $def, $conds=null, $field=null)
Definition: ezpersistentobject.php:187
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:806
eZVATManager\getVAT
static getVAT( $object, $country)
Definition: ezvatmanager.php:26
eZVATManager\isDynamicVatChargingEnabled
static isDynamicVatChargingEnabled()
Definition: ezvatmanager.php:255
eZVatRule\removeVatRule
static removeVatRule( $id)
Definition: ezvatrule.php:163
eZVatRule\fetchByVatType
static fetchByVatType( $vatID)
Definition: ezvatrule.php:94
eZVatType
eZVatType handles different VAT types
Definition: ezvattype.php:19
eZVatType\fetchDependentProductsCount
static fetchDependentProductsCount( $vatID)
Definition: ezvattype.php:132
eZVatType\isDynamic
isDynamic()
Definition: ezvattype.php:97
eZVatType\$Percentage
$Percentage
Definition: ezvattype.php:20
eZVatType\dynamicVatTypeName
static dynamicVatTypeName()
Definition: ezvattype.php:74
eZVatType\fetchList
static fetchList( $asObject=true, $skipDynamic=false)
Definition: ezvattype.php:105
eZVatType\definition
static definition()
Definition: ezvattype.php:21
eZVatType\VATTypeList
VATTypeList()
Definition: ezvattype.php:262
eZVatType\fetch
static fetch( $id, $asObject=true)
Definition: ezvattype.php:86
eZVatType\removeThis
removeThis()
Definition: ezvattype.php:239
eZVatType\resetToDefaultInProducts
static resetToDefaultInProducts( $oldVAT)
Definition: ezvattype.php:190
eZVatType\fetchDependentClassesCount
static fetchDependentClassesCount( $vatID)
Definition: ezvattype.php:158
eZVatType\dynamicVatType
static dynamicVatType( $asObject=true)
Definition: ezvattype.php:56
eZVatType\$VatTypeList
$VatTypeList
Definition: ezvattype.php:274
eZVatType\getPercentage
getPercentage( $object, $country)
Definition: ezvattype.php:42
eZVatType\create
static create()
Definition: ezvattype.php:173
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+.