Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezvatrule.php
Go to the documentation of this file.
1 <?php
17 class eZVatRule extends eZPersistentObject
18 {
19 public function __construct( $row )
20 {
21 $this->ProductCategories = null;
22 parent::__construct( $row );
23 }
24
25 static function definition()
26 {
27 return array( "fields" => array( "id" => array( 'name' => 'ID',
28 'datatype' => 'integer',
29 'default' => 0,
30 'required' => true ),
31 "country_code"=> array ( 'name' => 'CountryCode',
32 'datatype' => 'string',
33 'default' => null,
34 'required' => true ),
35 "vat_type" => array( 'name' => "VatType",
36 'datatype' => 'integer',
37 'default' => null,
38 'required' => true,
39 'foreign_class' => 'eZVatType',
40 'foreign_attribute' => 'id',
41 'multiplicity' => '1..*') ),
42 "function_attributes" => array( 'product_categories' => 'productCategories',
43 'product_categories_string' => 'productCategoriesString',
44 'product_categories_ids' => 'productCategoriesIDs',
45 'product_categories_names' => 'productCategoriesNames',
46 'vat_type_object' => 'vatTypeObject',
47 'vat_type_name' => 'vatTypeName',
48 'country' => 'country' ),
49 "keys" => array( "id" ),
50 "increment_key" => "id",
51 "class_name" => "eZVatRule",
52 "name" => "ezvatrule" );
53 }
54
55 function setAttribute( $attr, $val )
56 {
57 switch( $attr )
58 {
59 case 'product_categories':
60 {
61 $this->ProductCategories = $val;
62 } break;
63
64 default:
65 {
66 parent::setAttribute( $attr, $val );
67 } break;
68 }
69 }
70
71 static function fetch( $id )
72 {
73 return eZPersistentObject::fetchObject( eZVatRule::definition(),
74 null,
75 array( "id" => $id ),
76 true );
77 }
78
79 static function fetchList()
80 {
81 return eZPersistentObject::fetchObjectList( eZVatRule::definition(),
82 null, null,
83 array( 'id' => 'asc' ),
84 null, true );
85 }
86
94 static function fetchByVatType( $vatID )
95 {
96 return eZPersistentObject::fetchObjectList( eZVatRule::definition(), null,
97 array( 'vat_type' => (int) $vatID ),
98 null, null, true, false, null );
99 }
100
108 static function fetchCountByCategory( $categories )
109 {
110 $db = eZDB::instance();
111
112 $query = "SELECT COUNT(*) AS count FROM ezvatrule vr, ezvatrule_product_category vrpc " .
113 "WHERE vr.id=vrpc.vatrule_id AND ";
114
115 if ( is_array( $categories ) )
116 {
117 $query .= $db->generateSQLINStatement( $categories, 'vrpc.product_category_id', false, false, 'int' );
118 }
119 else
120 {
121 $query .= "vrpc.product_category_id =" . (int) $categories;
122 }
123
124 $rows = $db->arrayQuery( $query );
125
126 return $rows[0]['count'];
127 }
128
136 static function fetchCountByVatType( $vatID )
137 {
138 $rows = eZPersistentObject::fetchObjectList( eZVatRule::definition(),
139 array(),
140 array( 'vat_type' => (int) $vatID ),
141 false,
142 null,
143 false,
144 false,
145 array( array( 'operation' => 'count( * )',
146 'name' => 'count' ) ) );
147 return $rows[0]['count'];
148 }
149
150 static function create()
151 {
152 $row = array(
153 "id" => null,
154 "country_code" => null,
155 "vat_type" => null
156 );
157 return new eZVatRule( $row );
158 }
159
163 static function removeVatRule( $id )
164 {
165 $db = eZDB::instance();
166
167 $db->begin();
168
169 // Remove product categories associated with the rule.
170 eZVatRule::removeProductCategories( $id );
171
172 // Remove the rule itself.
173 eZPersistentObject::removeObject( eZVatRule::definition(), array( "id" => $id ) );
174
175 $db->commit();
176 }
177
178 function store( $fieldFilters = null )
179 {
180 $db = eZDB::instance();
181 $db->begin();
182
183 // Store the rule itself.
184 parent::store( $fieldFilters );
185
186 // Store product categories associated with the rule,
187 $this->removeProductCategories();
188 $categories = $this->attribute( 'product_categories' );
189 if ( $categories )
190 {
191 foreach ( $categories as $category )
192 {
193 $query = sprintf( "INSERT INTO ezvatrule_product_category " .
194 "(vatrule_id, product_category_id) VALUES (%d,%d)" ,
195 $this->ID, $category['id'] );
196 $db->query( $query );
197 }
198 }
199
200 $db->commit();
201 }
202
206 function productCategories()
207 {
208 // If product categories for this rule have not been fetched yet
209 if ( $this->ProductCategories === null )
210 {
211 // fetch them
212 $db = eZDB::instance();
213 $query = "SELECT pc.* FROM ezproductcategory pc, ezvatrule_product_category vrpc WHERE" .
214 " pc.id = vrpc.product_category_id AND" .
215 " vrpc.vatrule_id = {$this->ID}";
216 $this->ProductCategories = $db->arrayQuery( $query );
217 }
218
219 return $this->ProductCategories;
220 }
221
227 function productCategoriesString()
228 {
229 $categories = $this->attribute( 'product_categories' );
230 if ( !$categories )
231 {
232 $result = ezpI18n::tr( 'kernel/shop', 'Any' );
233 return $result;
234 }
235
236 $categoriesNames = array();
237 foreach ( $categories as $cat )
238 $categoriesNames[] = $cat['name'];
239
240 return join( ', ', $categoriesNames );
241 }
242
248 function productCategoriesIDs()
249 {
250 $catIDs = array();
251 $categories = $this->attribute( 'product_categories' );
252
253 if ( $categories )
254 {
255 foreach ( $categories as $cat )
256 $catIDs[] = $cat['id'];
257 }
258
259 return $catIDs;
260 }
261
267 function productCategoriesNames()
268 {
269 $catNames = array();
270 $categories = $this->attribute( 'product_categories' );
271
272 if ( $categories )
273 {
274 foreach ( $categories as $cat )
275 $catNames[] = $cat['name'];
276 }
277
278 return $catNames;
279 }
280
281
285 function vatTypeName()
286 {
287 $vatType = eZVatType::fetch( $this->attribute( 'vat_type' ) );
288 return $vatType->attribute( 'name' );
289 }
290
294 function vatTypeObject()
295 {
296 return eZVatType::fetch( $this->attribute( 'vat_type' ) );
297 }
298
302 function country()
303 {
304 if ( $this->attribute( 'country_code' ) != '*' )
305 {
306 $countryINI = eZINI::instance( 'country.ini' );
307 $countryName = $countryINI->variable( $this->attribute( 'country_code' ) , 'Name' );
308 }
309 else
310 {
311 $countryName = '*';
312 }
313 return $countryName;
314 }
315
323 function removeProductCategories( $ruleID = false )
324 {
325 if ( $ruleID === false )
326 $ruleID = $this->ID;
327 $ruleID =(int) $ruleID;
328 $db = eZDB::instance();
329 $db->query( "DELETE FROM ezvatrule_product_category WHERE vatrule_id = $ruleID" );
330 }
331
340 static function removeReferencesToProductCategory( $cartegoryID )
341 {
342 $db = eZDB::instance();
343 $query = "DELETE FROM ezvatrule_product_category WHERE product_category_id=" . (int) $cartegoryID;
344 $db->query( $query );
345 }
346
347
348 public $ProductCategories;
349 }
350
351 ?>
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\attribute
attribute( $attr, $noFunction=false)
Definition: ezpersistentobject.php:1329
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
eZVatRule
eZVatRule handles VAT charging rules for the default VAT handler.
Definition: ezvatrule.php:18
eZVatRule\productCategoriesString
productCategoriesString()
Definition: ezvatrule.php:227
eZVatRule\productCategories
productCategories()
Definition: ezvatrule.php:206
eZVatRule\productCategoriesNames
productCategoriesNames()
Definition: ezvatrule.php:267
eZVatRule\removeVatRule
static removeVatRule( $id)
Definition: ezvatrule.php:163
eZVatRule\fetchCountByVatType
static fetchCountByVatType( $vatID)
Definition: ezvatrule.php:136
eZVatRule\create
static create()
Definition: ezvatrule.php:150
eZVatRule\store
store( $fieldFilters=null)
Definition: ezvatrule.php:178
eZVatRule\productCategoriesIDs
productCategoriesIDs()
Definition: ezvatrule.php:248
eZVatRule\removeProductCategories
removeProductCategories( $ruleID=false)
Definition: ezvatrule.php:323
eZVatRule\country
country()
Definition: ezvatrule.php:302
eZVatRule\removeReferencesToProductCategory
static removeReferencesToProductCategory( $cartegoryID)
Definition: ezvatrule.php:340
eZVatRule\__construct
__construct( $row)
Definition: ezvatrule.php:19
eZVatRule\fetchList
static fetchList()
Definition: ezvatrule.php:79
eZVatRule\vatTypeObject
vatTypeObject()
Definition: ezvatrule.php:294
eZVatRule\fetchByVatType
static fetchByVatType( $vatID)
Definition: ezvatrule.php:94
eZVatRule\vatTypeName
vatTypeName()
Definition: ezvatrule.php:285
eZVatRule\definition
static definition()
Definition: ezvatrule.php:25
eZVatRule\setAttribute
setAttribute( $attr, $val)
Definition: ezvatrule.php:55
eZVatRule\$ProductCategories
$ProductCategories
Definition: ezvatrule.php:348
eZVatRule\fetch
static fetch( $id)
Definition: ezvatrule.php:71
eZVatRule\fetchCountByCategory
static fetchCountByCategory( $categories)
Definition: ezvatrule.php:108
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+.