eZ Publish  [4.0]
ezvattype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZVatType class
00004 //
00005 // Created on: <26-Nov-2002 16:00:45 wy>
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 eZVatType ezvattype.php
00033   \brief eZVatType handles different VAT types
00034   \ingroup eZKernel
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     static 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; // indicate that VAT percentage is unknown
00078         }
00079         else
00080             $percentage = $this->Percentage;
00081 
00082         return $percentage;
00083     }
00084 
00085     static 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      * Return name of the "fake" dynamic VAT type.
00099      *
00100      * \private
00101      * \static
00102      */
00103     static 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     static 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         return ( $this->ID == -1 );
00131     }
00132 
00133     /**
00134      * \param $skipDynamic if false, include dynamic VAT type to the list being returned.
00135      */
00136     static function fetchList( $asObject = true, $skipDynamic = false )
00137     {
00138         // Fetch "real" VAT types, stored in DB.
00139         $VATTypes = eZPersistentObject::fetchObjectList( eZVatType::definition(),
00140                                                          null, null, array( 'id' => false ), null,
00141                                                          $asObject );
00142         if ( !$VATTypes )
00143             $VATTypes = array();
00144 
00145         // Add "fake" VAT type: dynamic.
00146         if ( !$skipDynamic )
00147         {
00148             require_once( 'kernel/classes/ezvatmanager.php' );
00149             if ( eZVATManager::isDynamicVatChargingEnabled() )
00150                 $VATTypes[] = eZVatType::dynamicVatType( $asObject );
00151         }
00152 
00153         return $VATTypes;
00154     }
00155 
00156     /**
00157      * Fetches number of products using given VAT type.
00158      *
00159      * \public
00160      * \static
00161      * \param $vatID id of VAT type to count dependent products for.
00162      * \return Number of dependent products.
00163      */
00164     static function fetchDependentProductsCount( $vatID )
00165     {
00166         $vatID = (int) $vatID; // prevent SQL injection
00167 
00168         // We need DISTINCT here since there might be several object translations.
00169         $query = "SELECT COUNT(DISTINCT coa.contentobject_id) AS count " .
00170                  "FROM ezcontentobject_attribute coa, ezcontentobject co " .
00171                  "WHERE " .
00172                  "coa.contentobject_id=co.id AND " .
00173                  "coa.version=co.current_version AND " .
00174                  "data_type_string IN ('ezprice', 'ezmultiprice') " .
00175                  "AND data_text LIKE '$vatID,%'";
00176 
00177         require_once( 'lib/ezdb/classes/ezdb.php' );
00178         $db = eZDB::instance();
00179         $rslt = $db->arrayQuery( $query );
00180         $nProducts = $rslt[0]['count'];
00181         return $nProducts;
00182     }
00183 
00184     /**
00185      * Fetches number of product classes having given VAT type set as default.
00186      *
00187      * \public
00188      * \static
00189      * \return Number of dependent product classes.
00190      */
00191     static function fetchDependentClassesCount( $vatID )
00192     {
00193         $vatID = (int) $vatID; // prevent SQL injection
00194 
00195         $query = "SELECT COUNT(DISTINCT cc.id) AS count " .
00196                  "FROM ezcontentclass cc, ezcontentclass_attribute cca " .
00197                  "WHERE cc.id=cca.contentclass_id AND " .
00198                  "cca.data_type_string IN ('ezprice', 'ezmultiprice') AND data_float1=$vatID";
00199 
00200         $db = eZDB::instance();
00201         $rslt = $db->arrayQuery( $query );
00202         $nClasses = $rslt[0]['count'];
00203         return $nClasses;
00204     }
00205 
00206     static function create()
00207     {
00208         $row = array(
00209             "id" => null,
00210             "name" => ezi18n( 'kernel/shop', 'VAT type' ),
00211             "percentage" => 0.0 );
00212         return new eZVatType( $row );
00213     }
00214 
00215     /**
00216      * Change VAT type in all products from $oldVAT to the default VAT of a product class.
00217      *
00218      * \private
00219      * \static
00220      * \param $oldVAT old VAT type id.
00221      * \param $newVAT new VAT type id.
00222      */
00223     static function resetToDefaultInProducts( $oldVAT )
00224     {
00225         $db = eZDB::instance();
00226         $db->begin();
00227 
00228         $selectProductsQuery =
00229             "SELECT coa.id, data_text, cca.data_float1 AS default_vat " .
00230             "FROM ezcontentclass cc, ezcontentclass_attribute cca, ezcontentobject_attribute coa, ezcontentobject co " .
00231             "WHERE " .
00232             "cc.id=cca.contentclass_id AND " .
00233             "cca.id=coa.contentclassattribute_id AND " .
00234             "coa.contentobject_id=co.id AND  " .
00235             "coa.version=co.current_version AND " .
00236             "cca.data_type_string IN ('ezprice', 'ezmultiprice') " .
00237             "AND data_text LIKE '$oldVAT,%'";
00238 
00239         // Fetch the attributes by small portions to avoid memory overflow.
00240         for ( $offset = 0; true; $offset += 50 )
00241         {
00242             $rows = $db->arrayQuery( $selectProductsQuery, array( 'offset' => $offset, 'limit' => 50 ) );
00243             if ( !$rows )
00244                 break;
00245 
00246             foreach ( $rows as $row )
00247             {
00248                 list( $oldVatType, $vatExInc ) = explode( ',', $row['data_text'], 2 );
00249                 $updateQuery = "UPDATE ezcontentobject_attribute " .
00250                                "SET data_text = '" . $row['default_vat'] . ",$vatExInc' " .
00251                                "WHERE id=" . $row['id'];
00252                 $db->query( $updateQuery );
00253             }
00254 
00255             if ( count( $rows ) < 50 )
00256                 break;
00257         }
00258 
00259         $db->commit();
00260     }
00261 
00262     /**
00263      * Remove given VAT type and all references to it.
00264      *
00265      * Drops VAT charging rules referencing the VAT type.
00266      * Resets VAT type in associated products to its default value for a product class.
00267      *
00268      * \param $vatID id of VAT type to remove.
00269      * \public
00270      * \static
00271      */
00272     function removeThis()
00273     {
00274         $vatID = $this->ID;
00275         $db = eZDB::instance();
00276         $db->begin();
00277 
00278         // remove dependent VAT rules
00279         require_once( 'kernel/classes/ezvatrule.php' );
00280         $dependentRules = eZVatRule::fetchByVatType( $vatID );
00281         foreach ( $dependentRules as $rule )
00282         {
00283             eZVatRule::removeVatRule( $rule->attribute( 'id' ) );
00284         }
00285 
00286         // replace VAT type in dependent products.
00287         eZVatType::resetToDefaultInProducts( $vatID );
00288 
00289         // Remove the VAT type itself.
00290         eZPersistentObject::removeObject( eZVatType::definition(),
00291                                           array( "id" => $vatID ) );
00292 
00293         $db->commit();
00294     }
00295 
00296     function VATTypeList()
00297     {
00298         if ( !isset( $this->VatTypeList ) )
00299         {
00300             $this->VatTypeList = eZVatType::fetchList();
00301             if ( !isset( $this->VatTypeList ) )
00302                 $this->VatTypeList = array();
00303         }
00304 
00305         return $this->VatTypeList;
00306     }
00307 
00308     public $VatTypeList;
00309 }
00310 
00311 ?>