00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 include_once( "kernel/classes/ezpersistentobject.php" );
00039
00040 class eZVatType extends eZPersistentObject
00041 {
00042
00043
00044 function eZVatType( $row )
00045 {
00046 $this->eZPersistentObject( $row );
00047 }
00048
00049 function definition()
00050 {
00051 return array( "fields" => array( "id" => array( 'name' => 'ID',
00052 'datatype' => 'integer',
00053 'default' => 0,
00054 'required' => true ),
00055 "name" => array( 'name' => "Name",
00056 'datatype' => 'string',
00057 'default' => '',
00058 'required' => true ),
00059 "percentage" => array( 'name' => "Percentage",
00060 'datatype' => 'float',
00061 'default' => 0,
00062 'required' => true ) ),
00063 "function_attributes" => array( 'is_dynamic' => 'isDynamic' ),
00064 "keys" => array( "id" ),
00065 "increment_key" => "id",
00066 "class_name" => "eZVatType",
00067 "name" => "ezvattype" );
00068 }
00069
00070 function getPercentage( $object, $country )
00071 {
00072 if ( $this->ID == -1 )
00073 {
00074 require_once( 'kernel/classes/ezvatmanager.php' );
00075 $percentage = eZVATManager::getVAT( $object, $country );
00076 if ( $percentage === null )
00077 $percentage = -1;
00078 }
00079 else
00080 $percentage = $this->Percentage;
00081
00082 return $percentage;
00083 }
00084
00085 function dynamicVatType( $asObject = true )
00086 {
00087 $row = array( 'id' => -1,
00088 'name' => eZVatType::dynamicVatTypeName(),
00089 'percentage' => 0.0 );
00090
00091 if ( !$asObject )
00092 return $row;
00093
00094 return new eZVatType( $row );
00095 }
00096
00097
00098
00099
00100
00101
00102
00103 function dynamicVatTypeName()
00104 {
00105 if ( !isset( $GLOBALS['eZVatType_dynamicVatTypeName'] ) )
00106 {
00107 $shopINI = eZINI::instance( 'shop.ini' );
00108 $desc = $shopINI->variable( 'VATSettings', 'DynamicVatTypeName' );
00109 $GLOBALS['eZVatType_dynamicVatTypeName'] = $desc;
00110 }
00111
00112 return $GLOBALS['eZVatType_dynamicVatTypeName'];
00113 }
00114
00115 function fetch( $id, $asObject = true )
00116 {
00117 require_once( 'kernel/classes/ezvatmanager.php' );
00118
00119 if ( $id == -1 && eZVATManager::isDynamicVatChargingEnabled() )
00120 return eZVatType::dynamicVatType( $asObject );
00121
00122 return eZPersistentObject::fetchObject( eZVatType::definition(),
00123 null,
00124 array( "id" => $id ),
00125 $asObject );
00126 }
00127
00128 function &isDynamic()
00129 {
00130 $retVal = ( $this->ID == -1 );
00131 return $retVal;
00132 }
00133
00134
00135
00136
00137 function fetchList( $asObject = true, $skipDynamic = false )
00138 {
00139
00140 $VATTypes = eZPersistentObject::fetchObjectList( eZVatType::definition(),
00141 null, null, array( 'id' => false ), null,
00142 $asObject );
00143 if ( !$VATTypes )
00144 $VATTypes = array();
00145
00146
00147 if ( !$skipDynamic )
00148 {
00149 require_once( 'kernel/classes/ezvatmanager.php' );
00150 if ( eZVATManager::isDynamicVatChargingEnabled() )
00151 $VATTypes[] = eZVatType::dynamicVatType( $asObject );
00152 }
00153
00154 return $VATTypes;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 function fetchDependentProductsCount( $vatID )
00166 {
00167 $vatID = (int) $vatID;
00168
00169
00170 $query = "SELECT COUNT(DISTINCT coa.contentobject_id) AS count " .
00171 "FROM ezcontentobject_attribute coa, ezcontentobject co " .
00172 "WHERE " .
00173 "coa.contentobject_id=co.id AND " .
00174 "coa.version=co.current_version AND " .
00175 "data_type_string IN ('ezprice', 'ezmultiprice') " .
00176 "AND data_text LIKE '$vatID,%'";
00177
00178 require_once( 'lib/ezdb/classes/ezdb.php' );
00179 $db = eZDB::instance();
00180 $rslt = $db->arrayQuery( $query );
00181 $nProducts = $rslt[0]['count'];
00182 return $nProducts;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192 function fetchDependentClassesCount( $vatID )
00193 {
00194 $vatID = (int) $vatID;
00195
00196 $query = "SELECT COUNT(DISTINCT cc.id) AS count " .
00197 "FROM ezcontentclass cc, ezcontentclass_attribute cca " .
00198 "WHERE cc.id=cca.contentclass_id AND " .
00199 "cca.data_type_string IN ('ezprice', 'ezmultiprice') AND data_float1=$vatID";
00200
00201 $db = eZDB::instance();
00202 $rslt = $db->arrayQuery( $query );
00203 $nClasses = $rslt[0]['count'];
00204 return $nClasses;
00205 }
00206
00207 function create()
00208 {
00209 $row = array(
00210 "id" => null,
00211 "name" => ezi18n( 'kernel/shop', 'VAT type' ),
00212 "percentage" => 0.0 );
00213 return new eZVatType( $row );
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 function resetToDefaultInProducts( $oldVAT )
00225 {
00226 $db =& eZDB::instance();
00227 $db->begin();
00228
00229 $selectProductsQuery =
00230 "SELECT coa.id, data_text, cca.data_float1 AS default_vat " .
00231 "FROM ezcontentclass cc, ezcontentclass_attribute cca, ezcontentobject_attribute coa, ezcontentobject co " .
00232 "WHERE " .
00233 "cc.id=cca.contentclass_id AND " .
00234 "cca.id=coa.contentclassattribute_id AND " .
00235 "coa.contentobject_id=co.id AND " .
00236 "coa.version=co.current_version AND " .
00237 "cca.data_type_string IN ('ezprice', 'ezmultiprice') " .
00238 "AND data_text LIKE '$oldVAT,%'";
00239
00240
00241 for ( $offset = 0; true; $offset += 50 )
00242 {
00243 $rows = $db->arrayQuery( $selectProductsQuery, array( 'offset' => $offset, 'limit' => 50 ) );
00244 if ( !$rows )
00245 break;
00246
00247 foreach ( $rows as $row )
00248 {
00249 list( $oldVatType, $vatExInc ) = explode( ',', $row['data_text'], 2 );
00250 $updateQuery = "UPDATE ezcontentobject_attribute " .
00251 "SET data_text = '" . $row['default_vat'] . ",$vatExInc' " .
00252 "WHERE id=" . $row['id'];
00253 $db->query( $updateQuery );
00254 }
00255
00256 if ( count( $rows ) < 50 )
00257 break;
00258 }
00259
00260 $db->commit();
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 function remove( $vatID )
00274 {
00275 $db =& eZDB::instance();
00276 $db->begin();
00277
00278
00279 require_once( 'kernel/classes/ezvatrule.php' );
00280 $dependentRules = eZVatRule::fetchByVatType( $vatID );
00281 foreach ( $dependentRules as $rule )
00282 eZVatRule::remove( $rule->attribute( 'id' ) );
00283
00284
00285 eZVatType::resetToDefaultInProducts( $vatID );
00286
00287
00288 eZPersistentObject::removeObject( eZVatType::definition(),
00289 array( "id" => $vatID ) );
00290
00291 $db->commit();
00292 }
00293
00294 function &VATTypeList()
00295 {
00296 if ( !isset( $this->VatTypeList ) )
00297 {
00298 $this->VatTypeList = eZVatType::fetchList();
00299 if ( !isset( $this->VatTypeList ) )
00300 $this->VatTypeList = array();
00301 }
00302
00303 return $this->VatTypeList;
00304 }
00305
00306 var $VatTypeList;
00307 }
00308
00309 ?>