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 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
00050
00051 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
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 function fetch( $id )
00098 {
00099 return eZPersistentObject::fetchObject( eZVatRule::definition(),
00100 null,
00101 array( "id" => $id ),
00102 true );
00103 }
00104
00105 function fetchList()
00106 {
00107 return eZPersistentObject::fetchObjectList( eZVatRule::definition(),
00108 null, null,
00109 array( 'id' => 'asc' ),
00110 null, true );
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120 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
00129
00130
00131
00132
00133
00134 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 vrpc.product_category_id";
00140
00141 if ( is_array( $categories ) )
00142 $query .= " IN ('" . $db->implodeWithTypeCast( ',', $categories, 'int' ) . "')";
00143 else
00144 $query .= "=" . (int) $categories;
00145
00146 $rows = $db->arrayQuery( $query );
00147
00148 return $rows[0]['count'];
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158 function fetchCountByVatType( $vatID )
00159 {
00160 $rows = eZPersistentObject::fetchObjectList( eZVatRule::definition(),
00161 array(),
00162 array( 'vat_type' => (int) $vatID ),
00163 false,
00164 null,
00165 false,
00166 false,
00167 array( array( 'operation' => 'count( * )',
00168 'name' => 'count' ) ) );
00169 return $rows[0]['count'];
00170 }
00171
00172
00173 function create()
00174 {
00175 $row = array(
00176 "id" => null,
00177 "country_code" => null,
00178 "vat_type" => null
00179 );
00180 return new eZVatRule( $row );
00181 }
00182
00183
00184
00185
00186 function remove( $id )
00187 {
00188 $db =& eZDB::instance();
00189
00190 $db->begin();
00191
00192
00193 eZVatRule::removeProductCategories( $id );
00194
00195
00196 eZPersistentObject::removeObject( eZVatRule::definition(), array( "id" => $id ) );
00197
00198 $db->commit();
00199 }
00200
00201
00202
00203
00204 function store()
00205 {
00206 $db =& eZDB::instance();
00207 $db->begin();
00208
00209
00210 eZPersistentObject::store();
00211
00212
00213 $this->removeProductCategories();
00214 $categories = $this->attribute( 'product_categories' );
00215 if ( $categories )
00216 {
00217 foreach ( $categories as $category )
00218 {
00219 $query = sprintf( "INSERT INTO ezvatrule_product_category " .
00220 "(vatrule_id, product_category_id) VALUES (%d,%d)" ,
00221 $this->ID, $category['id'] );
00222 $db->query( $query );
00223 }
00224 }
00225
00226 $db->commit();
00227 }
00228
00229
00230
00231
00232 function &productCategories()
00233 {
00234
00235 if ( $this->ProductCategories === null )
00236 {
00237
00238 $db =& eZDB::instance();
00239 $query = "SELECT pc.* FROM ezproductcategory pc, ezvatrule_product_category vrpc WHERE" .
00240 " pc.id = vrpc.product_category_id AND" .
00241 " vrpc.vatrule_id = {$this->ID}";
00242 $this->ProductCategories = $db->arrayQuery( $query );
00243 }
00244
00245 return $this->ProductCategories;
00246 }
00247
00248
00249
00250
00251
00252
00253 function &productCategoriesString()
00254 {
00255 $categories = $this->attribute( 'product_categories' );
00256 if ( !$categories )
00257 {
00258 $result = ezi18n( 'kernel/shop', 'Any' );
00259 return $result;
00260 }
00261
00262 $categoriesNames = array();
00263 foreach ( $categories as $cat )
00264 $categoriesNames[] = $cat['name'];
00265
00266 $result = join( ', ', $categoriesNames );
00267 return $result;
00268 }
00269
00270
00271
00272
00273
00274
00275 function &productCategoriesIDs()
00276 {
00277 $catIDs = array();
00278 $categories = $this->attribute( 'product_categories' );
00279
00280 if ( $categories )
00281 {
00282 foreach ( $categories as $cat )
00283 $catIDs[] = $cat['id'];
00284 }
00285
00286 return $catIDs;
00287 }
00288
00289
00290
00291
00292
00293
00294 function &productCategoriesNames()
00295 {
00296 $catNames = array();
00297 $categories = $this->attribute( 'product_categories' );
00298
00299 if ( $categories )
00300 {
00301 foreach ( $categories as $cat )
00302 $catNames[] = $cat['name'];
00303 }
00304
00305 return $catNames;
00306 }
00307
00308
00309
00310
00311
00312 function &vatTypeName()
00313 {
00314 require_once( 'kernel/classes/ezvattype.php' );
00315 $vatType = eZVatType::fetch( $this->attribute( 'vat_type' ) );
00316 $name = $vatType->attribute( 'name' );
00317 return $name;
00318 }
00319
00320
00321
00322
00323 function &vatTypeObject()
00324 {
00325 require_once( 'kernel/classes/ezvattype.php' );
00326 $retObject = eZVatType::fetch( $this->attribute( 'vat_type' ) );
00327 return $retObject;
00328 }
00329
00330
00331
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
00349
00350
00351
00352
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
00365
00366
00367
00368
00369
00370
00371 function removeReferencesToProductCategory( $cartegoryID )
00372 {
00373 $db =& eZDB::instance();
00374 $query = "DELETE FROM ezvatrule_product_category WHERE product_category_id=" . (int) $cartegoryID;
00375 eZDebug::writeDebug( $query, '$query' );
00376 $db->query( $query );
00377 }
00378
00379
00380 var $ProductCategories;
00381 }
00382
00383 ?>