[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZVatType class 4 // 5 // Created on: <26-Nov-2002 16:00:45 wy> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZVatType ezvattype.php 31 \brief eZVatType handles different VAT types 32 \ingroup eZKernel 33 34 */ 35 36 include_once ( "kernel/classes/ezpersistentobject.php" ); 37 38 class eZVatType extends eZPersistentObject 39 { 40 /*! 41 */ 42 function eZVatType( $row ) 43 { 44 $this->eZPersistentObject( $row ); 45 } 46 47 function definition() 48 { 49 return array( "fields" => array( "id" => array( 'name' => 'ID', 50 'datatype' => 'integer', 51 'default' => 0, 52 'required' => true ), 53 "name" => array( 'name' => "Name", 54 'datatype' => 'string', 55 'default' => '', 56 'required' => true ), 57 "percentage" => array( 'name' => "Percentage", 58 'datatype' => 'float', 59 'default' => 0, 60 'required' => true ) ), 61 "function_attributes" => array( 'is_dynamic' => 'isDynamic' ), 62 "keys" => array( "id" ), 63 "increment_key" => "id", 64 "class_name" => "eZVatType", 65 "name" => "ezvattype" ); 66 } 67 68 function getPercentage( $object, $country ) 69 { 70 if ( $this->ID == -1 ) 71 { 72 require_once ( 'kernel/classes/ezvatmanager.php' ); 73 $percentage = eZVATManager::getVAT( $object, $country ); 74 if ( $percentage === null ) 75 $percentage = -1; // indicate that VAT percentage is unknown 76 } 77 else 78 $percentage = $this->Percentage; 79 80 return $percentage; 81 } 82 83 function dynamicVatType( $asObject = true ) 84 { 85 $row = array( 'id' => -1, 86 'name' => eZVatType::dynamicVatTypeName(), 87 'percentage' => 0.0 ); 88 89 if ( !$asObject ) 90 return $row; 91 92 return new eZVatType( $row ); 93 } 94 95 /** 96 * Return name of the "fake" dynamic VAT type. 97 * 98 * \private 99 * \static 100 */ 101 function dynamicVatTypeName() 102 { 103 if ( !isset( $GLOBALS['eZVatType_dynamicVatTypeName'] ) ) 104 { 105 $shopINI = eZINI::instance( 'shop.ini' ); 106 $desc = $shopINI->variable( 'VATSettings', 'DynamicVatTypeName' ); 107 $GLOBALS['eZVatType_dynamicVatTypeName'] = $desc; 108 } 109 110 return $GLOBALS['eZVatType_dynamicVatTypeName']; 111 } 112 113 function fetch( $id, $asObject = true ) 114 { 115 require_once ( 'kernel/classes/ezvatmanager.php' ); 116 117 if ( $id == -1 && eZVATManager::isDynamicVatChargingEnabled() ) 118 return eZVatType::dynamicVatType( $asObject ); 119 120 return eZPersistentObject::fetchObject( eZVatType::definition(), 121 null, 122 array( "id" => $id ), 123 $asObject ); 124 } 125 126 function &isDynamic() 127 { 128 $retVal = ( $this->ID == -1 ); 129 return $retVal; 130 } 131 132 /** 133 * \param $skipDynamic if false, include dynamic VAT type to the list being returned. 134 */ 135 function fetchList( $asObject = true, $skipDynamic = false ) 136 { 137 // Fetch "real" VAT types, stored in DB. 138 $VATTypes = eZPersistentObject::fetchObjectList( eZVatType::definition(), 139 null, null, array( 'id' => false ), null, 140 $asObject ); 141 if ( !$VATTypes ) 142 $VATTypes = array(); 143 144 // Add "fake" VAT type: dynamic. 145 if ( !$skipDynamic ) 146 { 147 require_once ( 'kernel/classes/ezvatmanager.php' ); 148 if ( eZVATManager::isDynamicVatChargingEnabled() ) 149 $VATTypes[] = eZVatType::dynamicVatType( $asObject ); 150 } 151 152 return $VATTypes; 153 } 154 155 /** 156 * Fetches number of products using given VAT type. 157 * 158 * \public 159 * \static 160 * \param $vatID id of VAT type to count dependent products for. 161 * \return Number of dependent products. 162 */ 163 function fetchDependentProductsCount( $vatID ) 164 { 165 $vatID = (int) $vatID; // prevent SQL injection 166 167 // We need DISTINCT here since there might be several object translations. 168 $query = "SELECT COUNT(DISTINCT coa.contentobject_id) AS count " . 169 "FROM ezcontentobject_attribute coa, ezcontentobject co " . 170 "WHERE " . 171 "coa.contentobject_id=co.id AND " . 172 "coa.version=co.current_version AND " . 173 "data_type_string IN ('ezprice', 'ezmultiprice') " . 174 "AND data_text LIKE '$vatID,%'"; 175 176 require_once ( 'lib/ezdb/classes/ezdb.php' ); 177 $db = eZDB::instance(); 178 $rslt = $db->arrayQuery( $query ); 179 $nProducts = $rslt[0]['count']; 180 return $nProducts; 181 } 182 183 /** 184 * Fetches number of product classes having given VAT type set as default. 185 * 186 * \public 187 * \static 188 * \return Number of dependent product classes. 189 */ 190 function fetchDependentClassesCount( $vatID ) 191 { 192 $vatID = (int) $vatID; // prevent SQL injection 193 194 $query = "SELECT COUNT(DISTINCT cc.id) AS count " . 195 "FROM ezcontentclass cc, ezcontentclass_attribute cca " . 196 "WHERE cc.id=cca.contentclass_id AND " . 197 "cca.data_type_string IN ('ezprice', 'ezmultiprice') AND data_float1=$vatID"; 198 199 $db = eZDB::instance(); 200 $rslt = $db->arrayQuery( $query ); 201 $nClasses = $rslt[0]['count']; 202 return $nClasses; 203 } 204 205 function create() 206 { 207 /* 208 $row = array( 209 "id" => null, 210 "name" => ezi18n( 'kernel/shop', 'VAT type' ), 211 "percentage" => null ); 212 */ 213 $row = array( 214 "id" => 0, 215 "name" => ezi18n( 'kernel/shop', 'VAT type' ), 216 "percentage" => 0.0 ); 217 return new eZVatType( $row ); 218 } 219 220 /** 221 * Change VAT type in all products from $oldVAT to the default VAT of a product class. 222 * 223 * \private 224 * \static 225 * \param $oldVAT old VAT type id. 226 * \param $newVAT new VAT type id. 227 */ 228 function resetToDefaultInProducts( $oldVAT ) 229 { 230 $db =& eZDB::instance(); 231 $db->begin(); 232 233 $selectProductsQuery = 234 "SELECT coa.id, data_text, cca.data_float1 AS default_vat " . 235 "FROM ezcontentclass cc, ezcontentclass_attribute cca, ezcontentobject_attribute coa, ezcontentobject co " . 236 "WHERE " . 237 "cc.id=cca.contentclass_id AND " . 238 "cca.id=coa.contentclassattribute_id AND " . 239 "coa.contentobject_id=co.id AND " . 240 "coa.version=co.current_version AND " . 241 "cca.data_type_string IN ('ezprice', 'ezmultiprice') " . 242 "AND data_text LIKE '$oldVAT,%'"; 243 244 // Fetch the attributes by small portions to avoid memory overflow. 245 for ( $offset = 0; true; $offset += 50 ) 246 { 247 $rows = $db->arrayQuery( $selectProductsQuery, array( 'offset' => $offset, 'limit' => 50 ) ); 248 if ( !$rows ) 249 break; 250 251 foreach ( $rows as $row ) 252 { 253 list( $oldVatType, $vatExInc ) = explode( ',', $row['data_text'], 2 ); 254 $updateQuery = "UPDATE ezcontentobject_attribute " . 255 "SET data_text = '" . $row['default_vat'] . ",$vatExInc' " . 256 "WHERE id=" . $row['id']; 257 $db->query( $updateQuery ); 258 } 259 260 if ( count( $rows ) < 50 ) 261 break; 262 } 263 264 $db->commit(); 265 } 266 267 /** 268 * Remove given VAT type and all references to it. 269 * 270 * Drops VAT charging rules referencing the VAT type. 271 * Resets VAT type in associated products to its default value for a product class. 272 * 273 * \param $vatID id of VAT type to remove. 274 * \public 275 * \static 276 */ 277 function remove( $vatID ) 278 { 279 $db =& eZDB::instance(); 280 $db->begin(); 281 282 // remove dependent VAT rules 283 require_once ( 'kernel/classes/ezvatrule.php' ); 284 $dependentRules = eZVatRule::fetchByVatType( $vatID ); 285 foreach ( $dependentRules as $rule ) 286 eZVatRule::remove( $rule->attribute( 'id' ) ); 287 288 // replace VAT type in dependent products. 289 eZVatType::resetToDefaultInProducts( $vatID ); 290 291 // Remove the VAT type itself. 292 eZPersistentObject::removeObject( eZVatType::definition(), 293 array( "id" => $vatID ) ); 294 295 $db->commit(); 296 } 297 298 function &VATTypeList() 299 { 300 if ( !isset( $this->VatTypeList ) ) 301 { 302 $this->VatTypeList = eZVatType::fetchList(); 303 if ( !isset( $this->VatTypeList ) ) 304 $this->VatTypeList = array(); 305 } 306 307 return $this->VatTypeList; 308 } 309 310 var $VatTypeList; 311 } 312 313 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |