|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZVatRule class 00004 // 00005 // Created on: <17-Feb-2006 17:00:26 vs> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! 00032 \class eZVatRule ezvatrule.php 00033 \brief eZVatRule handles VAT charging rules for the default VAT handler. 00034 \ingroup eZKernel 00035 */ 00036 00037 require_once( 'kernel/classes/ezpersistentobject.php' ); 00038 require_once( 'lib/ezdb/classes/ezdb.php' ); 00039 00040 class eZVatRule extends eZPersistentObject 00041 { 00042 function eZVatRule( $row ) 00043 { 00044 $this->ProductCategories = null; 00045 $this->eZPersistentObject( $row ); 00046 } 00047 00048 /** 00049 * \reimp 00050 */ 00051 static function definition() 00052 { 00053 return array( "fields" => array( "id" => array( 'name' => 'ID', 00054 'datatype' => 'integer', 00055 'default' => 0, 00056 'required' => true ), 00057 "country_code"=> array ( 'name' => 'CountryCode', 00058 'datatype' => 'string', 00059 'default' => null, 00060 'required' => true ), 00061 "vat_type" => array( 'name' => "VatType", 00062 'datatype' => 'integer', 00063 'default' => null, 00064 'required' => true ) ), 00065 "function_attributes" => array( 'product_categories' => 'productCategories', 00066 'product_categories_string' => 'productCategoriesString', 00067 'product_categories_ids' => 'productCategoriesIDs', 00068 'product_categories_names' => 'productCategoriesNames', 00069 'vat_type_object' => 'vatTypeObject', 00070 'vat_type_name' => 'vatTypeName', 00071 'country' => 'country' ), 00072 "keys" => array( "id" ), 00073 "increment_key" => "id", 00074 "class_name" => "eZVatRule", 00075 "name" => "ezvatrule" ); 00076 } 00077 00078 /** 00079 * \reimp 00080 */ 00081 function setAttribute( $attr, $val ) 00082 { 00083 switch( $attr ) 00084 { 00085 case 'product_categories': 00086 { 00087 $this->ProductCategories = $val; 00088 } break; 00089 00090 default: 00091 { 00092 eZPersistentObject::setAttribute( $attr, $val ); 00093 } break; 00094 } 00095 } 00096 00097 static function fetch( $id ) 00098 { 00099 return eZPersistentObject::fetchObject( eZVatRule::definition(), 00100 null, 00101 array( "id" => $id ), 00102 true ); 00103 } 00104 00105 static function fetchList() 00106 { 00107 return eZPersistentObject::fetchObjectList( eZVatRule::definition(), 00108 null, null, 00109 array( 'id' => 'asc' ), 00110 null, true ); 00111 } 00112 00113 /** 00114 * Fetch VAT rules referencing given VAT type. 00115 * 00116 * \param $vatID ID of VAT type to count VAT rules for. 00117 * \public 00118 * \static 00119 */ 00120 static function fetchByVatType( $vatID ) 00121 { 00122 return eZPersistentObject::fetchObjectList( eZVatRule::definition(), null, 00123 array( 'vat_type' => (int) $vatID ), 00124 null, null, true, false, null ); 00125 } 00126 00127 /** 00128 * Fetch number of VAT rules referencing given product categories 00129 * 00130 * @param $categories Category (single or list) to count VAT rules for. 00131 * 00132 * @return int 00133 */ 00134 static function fetchCountByCategory( $categories ) 00135 { 00136 $db = eZDB::instance(); 00137 00138 $query = "SELECT COUNT(*) AS count FROM ezvatrule vr, ezvatrule_product_category vrpc " . 00139 "WHERE vr.id=vrpc.vatrule_id AND "; 00140 00141 if ( is_array( $categories ) ) 00142 { 00143 $query .= $db->generateSQLINStatement( $categories, 'vrpc.product_category_id', false, false, 'int' ); 00144 } 00145 else 00146 { 00147 $query .= "vrpc.product_category_id =" . (int) $categories; 00148 } 00149 00150 $rows = $db->arrayQuery( $query ); 00151 00152 return $rows[0]['count']; 00153 } 00154 00155 /** 00156 * Return number of VAT rules referencing given VAT type. 00157 * 00158 * \param $vatID ID of VAT type to count VAT rules for. 00159 * \public 00160 * \static 00161 */ 00162 static function fetchCountByVatType( $vatID ) 00163 { 00164 $rows = eZPersistentObject::fetchObjectList( eZVatRule::definition(), 00165 array(), 00166 array( 'vat_type' => (int) $vatID ), 00167 false, 00168 null, 00169 false, 00170 false, 00171 array( array( 'operation' => 'count( * )', 00172 'name' => 'count' ) ) ); 00173 return $rows[0]['count']; 00174 } 00175 00176 static function create() 00177 { 00178 $row = array( 00179 "id" => null, 00180 "country_code" => null, 00181 "vat_type" => null 00182 ); 00183 return new eZVatRule( $row ); 00184 } 00185 00186 /** 00187 * Remove given VAT charging rule. 00188 */ 00189 static function removeVatRule( $id ) 00190 { 00191 $db = eZDB::instance(); 00192 00193 $db->begin(); 00194 00195 // Remove product categories associated with the rule. 00196 eZVatRule::removeProductCategories( $id ); 00197 00198 // Remove the rule itself. 00199 eZPersistentObject::removeObject( eZVatRule::definition(), array( "id" => $id ) ); 00200 00201 $db->commit(); 00202 } 00203 00204 /** 00205 * \reimp 00206 */ 00207 function store( $fieldFilters = null ) 00208 { 00209 $db = eZDB::instance(); 00210 $db->begin(); 00211 00212 // Store the rule itself. 00213 eZPersistentObject::store( $fieldFilters ); 00214 00215 // Store product categories associated with the rule, 00216 $this->removeProductCategories(); 00217 $categories = $this->attribute( 'product_categories' ); 00218 if ( $categories ) 00219 { 00220 foreach ( $categories as $category ) 00221 { 00222 $query = sprintf( "INSERT INTO ezvatrule_product_category " . 00223 "(vatrule_id, product_category_id) VALUES (%d,%d)" , 00224 $this->ID, $category['id'] ); 00225 $db->query( $query ); 00226 } 00227 } 00228 00229 $db->commit(); 00230 } 00231 00232 /** 00233 * \private 00234 */ 00235 function productCategories() 00236 { 00237 // If product categories for this rule have not been fetched yet 00238 if ( $this->ProductCategories === null ) 00239 { 00240 // fetch them 00241 $db = eZDB::instance(); 00242 $query = "SELECT pc.* FROM ezproductcategory pc, ezvatrule_product_category vrpc WHERE" . 00243 " pc.id = vrpc.product_category_id AND" . 00244 " vrpc.vatrule_id = {$this->ID}"; 00245 $this->ProductCategories = $db->arrayQuery( $query ); 00246 } 00247 00248 return $this->ProductCategories; 00249 } 00250 00251 /** 00252 * Return product categories as string, separated with commas. 00253 * 00254 * \private 00255 */ 00256 function productCategoriesString() 00257 { 00258 $categories = $this->attribute( 'product_categories' ); 00259 if ( !$categories ) 00260 { 00261 $result = ezi18n( 'kernel/shop', 'Any' ); 00262 return $result; 00263 } 00264 00265 $categoriesNames = array(); 00266 foreach ( $categories as $cat ) 00267 $categoriesNames[] = $cat['name']; 00268 00269 return join( ', ', $categoriesNames ); 00270 } 00271 00272 /** 00273 * Return IDs of product categories associated with the rule. 00274 * 00275 * \private 00276 */ 00277 function productCategoriesIDs() 00278 { 00279 $catIDs = array(); 00280 $categories = $this->attribute( 'product_categories' ); 00281 00282 if ( $categories ) 00283 { 00284 foreach ( $categories as $cat ) 00285 $catIDs[] = $cat['id']; 00286 } 00287 00288 return $catIDs; 00289 } 00290 00291 /** 00292 * Return names of product categories associated with the rule. 00293 * 00294 * \prrivate 00295 */ 00296 function productCategoriesNames() 00297 { 00298 $catNames = array(); 00299 $categories = $this->attribute( 'product_categories' ); 00300 00301 if ( $categories ) 00302 { 00303 foreach ( $categories as $cat ) 00304 $catNames[] = $cat['name']; 00305 } 00306 00307 return $catNames; 00308 } 00309 00310 00311 /** 00312 * Return VAT type name. 00313 */ 00314 function vatTypeName() 00315 { 00316 require_once( 'kernel/classes/ezvattype.php' ); 00317 $vatType = eZVatType::fetch( $this->attribute( 'vat_type' ) ); 00318 return $vatType->attribute( 'name' ); 00319 } 00320 00321 /** 00322 * Return VAT type object. 00323 */ 00324 function vatTypeObject() 00325 { 00326 require_once( 'kernel/classes/ezvattype.php' ); 00327 return eZVatType::fetch( $this->attribute( 'vat_type' ) ); 00328 } 00329 00330 /* 00331 * Returns country name 00332 */ 00333 function country() 00334 { 00335 if ( $this->attribute( 'country_code' ) != '*' ) 00336 { 00337 $countryINI = eZINI::instance( 'country.ini' ); 00338 $countryName = $countryINI->variable( $this->attribute( 'country_code' ) , 'Name' ); 00339 } 00340 else 00341 { 00342 $countryName = '*'; 00343 } 00344 return $countryName; 00345 } 00346 00347 /** 00348 * Remove product categories with the rule from DB. 00349 * 00350 * \private 00351 * \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00352 * the calls within a db transaction; thus within db->begin and db->commit. 00353 */ 00354 function removeProductCategories( $ruleID = false ) 00355 { 00356 if ( $ruleID === false ) 00357 $ruleID = $this->ID; 00358 $ruleID =(int) $ruleID; 00359 $db = eZDB::instance(); 00360 $db->query( "DELETE FROM ezvatrule_product_category WHERE vatrule_id = $ruleID" ); 00361 } 00362 00363 /** 00364 * Remove references to the given product category. 00365 * 00366 * \public 00367 * \static 00368 * \note Transaction unsafe. If you call several transaction unsafe methods you must enclose 00369 * the calls within a db transaction; thus within db->begin and db->commit. 00370 */ 00371 static function removeReferencesToProductCategory( $cartegoryID ) 00372 { 00373 $db = eZDB::instance(); 00374 $query = "DELETE FROM ezvatrule_product_category WHERE product_category_id=" . (int) $cartegoryID; 00375 $db->query( $query ); 00376 } 00377 00378 00379 public $ProductCategories; 00380 } 00381 00382 ?>