eZ Publish  [4.0]
ezshippingmanager.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZShippingManager class
00004 //
00005 // Created on: <12-Dec-2005 12:00:06 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 /*! \file ezshippingmanager.php
00032 */
00033 
00034 /*!
00035   \class eZShippingManager ezshippingmanager.php
00036   \brief The class eZShippingManager does
00037 */
00038 
00039 class eZShippingManager
00040 {
00041     /*!
00042      \public
00043      \static
00044 
00045      The function are fetching the shipping info and need to be reimplemented in a new shippinghandler.
00046      It's also possible to return additional parameters to use in the templates.
00047 
00048      \return an array with shipping info.
00049      An example for an array that should be returned.
00050 
00051      \code
00052      array( 'shipping_items' => array( array( 'description'     => 'Shipping vat: 12%',
00053                                               'cost'            => 50.25,
00054                                               'vat_value'       => 12,
00055                                               'is_vat_inc'      => 0,
00056                                               'management_link' => '/myshippingmodule/options/12' ),
00057                                        array( 'description'     => 'Shipping vat: 25%',
00058                                               'cost'            => 100.75,
00059                                               'vat_value'       => 25,
00060                                               'is_vat_inc'      => 1,
00061                                               'management_link' => '/myshippingmodule/options/25' ) ),
00062             'description'     => 'Total Shipping',
00063             'cost'            => 10.25,
00064             'vat_value'       => false,
00065             'is_vat_inc'      => 1,
00066             'management_link' => '/myshippingmodule/options' );
00067      \endcode
00068 
00069      An example for the shippingvalues with only one shippingitem.
00070      \code
00071      array( 'description'     => 'Total Shipping vat: 16%',
00072             'cost'            => 10.25,
00073             'vat_value'       => 16,
00074             'is_vat_inc'      => 1,
00075             'management_link' => '/myshippingmodule/options/1234' );
00076      \endcode
00077 
00078      The returned array for each shipping item should consist of these keys:
00079      - description - An own description of the shipping item.
00080      - cost - A float value of the cost for the shipping. The value should be a float value.
00081      - vat_value - The vat value that should be added to the shipping item. The value should be an integer or
00082                    false if the cost is combined by several VAT prices.
00083      - is_vat_inc - Integer, either 0, 1. 0: The cost is excluded VAT.
00084                                           1: the cost is included VAT.
00085 
00086      - management_link - Example of an additional parameter that can be used
00087        in a template. Ex: basket.tpl
00088      */
00089     static function getShippingInfo( $productCollectionID )
00090     {
00091         if ( !is_object( $handler = eZShippingManager::loadShippingHandler() ) )
00092             return null;
00093 
00094         return $handler->getShippingInfo( $productCollectionID );
00095     }
00096 
00097     /*!
00098      \public
00099      \static
00100 
00101      The function to update any additional shippinginfo and need to be reimplemented
00102      in a new shippinghandler.
00103 
00104      \return true if all updates are ok.
00105              false if the update went wrong.
00106      */
00107     static function updateShippingInfo( $productCollectionID )
00108     {
00109         if ( is_object( $handler = eZShippingManager::loadShippingHandler() ) )
00110             return $handler->updateShippingInfo( $productCollectionID );
00111     }
00112 
00113     /*!
00114      Purge shipping information for a stale product collection that is about to be removed.
00115      \public
00116      \static
00117 
00118      Should be used when a basket is removed. All shipping information related to the
00119      productCollectionID should be removed.
00120 
00121 
00122      \return true if everything went ok.
00123              false if an error occurred.
00124      */
00125     static function purgeShippingInfo( $productCollectionID )
00126     {
00127         if ( is_object( $handler = eZShippingManager::loadShippingHandler() ) )
00128             return $handler->purgeShippingInfo( $productCollectionID );
00129     }
00130 
00131     /*!
00132      Load shipping handler (if specified).
00133 
00134      \private
00135      \static
00136      \return true if no handler specified,
00137              false if a handler specified but could not be loaded,
00138              handler object if handler specified and found.
00139      */
00140     static function loadShippingHandler()
00141     {
00142         $shopINI = eZINI::instance( 'shop.ini' );
00143 
00144         if ( !$shopINI->hasVariable( 'ShippingSettings', 'Handler' ) )
00145             return true;
00146 
00147         $handlerName = $shopINI->variable( 'ShippingSettings', 'Handler' );
00148         $repositoryDirectories = $shopINI->variable( 'ShippingSettings', 'RepositoryDirectories' );
00149         $extensionDirectories = $shopINI->variable( 'ShippingSettings', 'ExtensionDirectories' );
00150 
00151         $baseDirectory = eZExtension::baseDirectory();
00152         foreach ( $extensionDirectories as $extensionDirectory )
00153         {
00154             $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/shippinghandlers';
00155             if ( file_exists( $extensionPath ) )
00156                 $repositoryDirectories[] = $extensionPath;
00157         }
00158 
00159         $foundHandler = false;
00160         foreach ( $repositoryDirectories as $repositoryDirectory )
00161         {
00162             $includeFile = "$repositoryDirectory/{$handlerName}shippinghandler.php";
00163 
00164             if ( file_exists( $includeFile ) )
00165             {
00166                 $foundHandler = true;
00167                 break;
00168             }
00169         }
00170 
00171         if ( !$foundHandler )
00172         {
00173             eZDebug::writeError( "Shipping handler '$handlerName' not found, " .
00174                                    "searched in these directories: " .
00175                                    implode( ', ', $repositoryDirectories ),
00176                                  'eZShippingManager::loadShippingHandler' );
00177             return false;
00178         }
00179 
00180         require_once( $includeFile );
00181         $className = $handlerName . 'ShippingHandler';
00182         if ( !class_exists ( $className ) )
00183         {
00184             eZDebug::writeError( "Cannot instantiate non-existent class: '$className'",
00185                                  'eZShippingManager::loadShippingHandler' );
00186             return null;
00187         }
00188 
00189         return new $className;
00190     }
00191 
00192     /*!
00193      Load basketinfo handler (if specified).
00194 
00195      \private
00196      \static
00197      \return true if no handler specified,
00198              false if a handler specified but could not be loaded,
00199              handler object if handler specified and found.
00200      */
00201     static function loadBasketInfoHandler()
00202     {
00203         $shopINI = eZINI::instance( 'shop.ini' );
00204 
00205         if ( !$shopINI->hasVariable( 'BasketInfoSettings', 'Handler' ) )
00206             return true;
00207 
00208         $handlerName = $shopINI->variable( 'BasketInfoSettings', 'Handler' );
00209         $repositoryDirectories = $shopINI->variable( 'BasketInfoSettings', 'RepositoryDirectories' );
00210         $extensionDirectories = $shopINI->variable( 'BasketInfoSettings', 'ExtensionDirectories' );
00211 
00212         $baseDirectory = eZExtension::baseDirectory();
00213         foreach ( $extensionDirectories as $extensionDirectory )
00214         {
00215             $extensionPath = $baseDirectory . '/' . $extensionDirectory . '/basketinfohandlers';
00216             if ( file_exists( $extensionPath ) )
00217                 $repositoryDirectories[] = $extensionPath;
00218         }
00219 
00220         $foundHandler = false;
00221         foreach ( $repositoryDirectories as $repositoryDirectory )
00222         {
00223             $includeFile = "$repositoryDirectory/{$handlerName}basketinfohandler.php";
00224 
00225             if ( file_exists( $includeFile ) )
00226             {
00227                 $foundHandler = true;
00228                 break;
00229             }
00230         }
00231 
00232         if ( !$foundHandler )
00233         {
00234             eZDebug::writeError( "Basketinfo handler '$handlerName' not found, " .
00235                                    "searched in these directories: " .
00236                                    implode( ', ', $repositoryDirectories ),
00237                                  'eZShippingManager::loadBasketInfoHandler' );
00238             return false;
00239         }
00240 
00241         require_once( $includeFile );
00242         $className = $handlerName . 'BasketInfoHandler';
00243         if ( !class_exists ( $className ) )
00244         {
00245             eZDebug::writeError( "Cannot instantiate non-existent class: '$className'",
00246                                  'eZShippingManager::loadBasketInfoHandler' );
00247             return null;
00248         }
00249 
00250         return new $className;
00251     }
00252 
00253     /*!
00254      Calculate the vat prices returned by the shippinghandler.
00255      \public
00256      \static
00257 
00258      Need to receive the following values (In the arrays will also consist of
00259      additional parameters, see the function getShippingInfo() for more information):
00260 
00261      Option 1: An array with shipping items with atleast these values:
00262      \code
00263      array( 'shipping_items' => array( array( 'cost'       => 50.25,
00264                                               'vat_value'  => 12,
00265                                               'is_vat_inc' => 0 ),
00266                                        array( 'cost'       => 100.75,
00267                                               'vat_value'  => 25,
00268                                               'is_vat_inc' => 1 ) ),
00269             'cost'       => 157.03,
00270             'vat_value'  => false,
00271             'is_vat_inc' => 1 );
00272      \endcode
00273 
00274      Option 2, backwards compatible:
00275      \code
00276      array( 'cost'       => 50.25,
00277             'vat_value'  => 12,
00278             'is_vat_inc' => 0 );
00279      \endcode
00280 
00281      \return array with calculated vat prices.
00282              Example of return value:
00283      \code
00284      array( 'vat_shipping_list_ex_vat'  => array(  5 => 345.25,
00285                                                   25 => 112.50 ),
00286             'vat_shipping_list_inc_vat' => array(  5 => 125,
00287                                                   25 => 150.50 ),
00288             'total_shipping_ex_vat'  => 1234,
00289             'total_shipping_inc_vat' => 2345 );
00290      \endcode
00291 
00292      - vat_shipping_list_ex_vat  - contains an array with prices (float) ex vat
00293                                    where you have the vat (integer) value as the array key.
00294      - vat_shipping_list_inc_vat - contains an array with prices (float) inc vat
00295                                    where you have the vat value (integer) as the array key.
00296      - total_shipping_ex_vat     - contains the sum of all prices (float) ex vat.
00297      - total_shipping_inc_vat    - contains the sum of all prices (float) inc vat.
00298      */
00299     static function vatPriceInfo( $shippingInfo )
00300     {
00301         $totalShippingExVat = 0;
00302         $totalShippingIncVat = 0;
00303         $totalShippingVat = 0;
00304         $subShippingExVat = array();
00305         $subShippingIncVat = array();
00306         $shippingVatList = array();
00307         if ( isset( $shippingInfo['shipping_items'] ) )
00308         {
00309             foreach ( $shippingInfo['shipping_items'] as $shippingItem )
00310             {
00311                 $vatValue = $shippingItem['vat_value'];
00312                 $shippingCost = $shippingItem['cost'];
00313                 $isVatInc = $shippingItem['is_vat_inc'];
00314 
00315                 if ( $isVatInc == 1 )
00316                 {
00317                     $divideValue = ( $vatValue / 100 ) + 1;
00318                     if ( !isset( $subShippingExVat[$vatValue] ) )
00319                     {
00320                         $subShippingExVat[$vatValue] = ( $shippingCost / $divideValue );
00321                         $subShippingIncVat[$vatValue] = $shippingCost;
00322                         $subShippingVat[$vatValue] = ( $shippingCost - ( $shippingCost / $divideValue ) );
00323                     }
00324                     else
00325                     {
00326                         $subShippingExVat[$vatValue] += ( $shippingCost / $divideValue );
00327                         $subShippingIncVat[$vatValue] += $shippingCost;
00328                         $subShippingVat[$vatValue] += ( $shippingCost - ( $shippingCost / $divideValue ) );
00329                     }
00330                 }
00331                 else
00332                 {
00333                     $multiplier = ( $vatValue / 100 ) + 1;
00334                     if ( !isset( $subShippingExVat[$vatValue] ) )
00335                     {
00336                         $subShippingExVat[$vatValue] = $shippingCost;
00337                         $subShippingIncVat[$vatValue] = ( $shippingCost * $multiplier );
00338                         $subShippingVat[$vatValue] = ( ( $shippingCost * $multiplier ) - $shippingCost );
00339                     }
00340                     else
00341                     {
00342                         $subShippingExVat[$vatValue] += $shippingCost;
00343                         $subShippingIncVat[$vatValue] += ( $shippingCost * $multiplier );
00344                         $subShippingVat[$vatValue] += ( ( $shippingCost * $multiplier ) - $shippingCost );
00345                     }
00346                 }
00347 
00348                 if ( !isset( $shippingVatList[$vatValue]['shipping_ex_vat'] ) )
00349                 {
00350                     $shippingVatList[$vatValue]['shipping_ex_vat'] = $subShippingExVat[$vatValue];
00351                     $shippingVatList[$vatValue]['shipping_inc_vat'] = $subShippingIncVat[$vatValue];
00352                     $shippingVatList[$vatValue]['shipping_vat'] = $subShippingVat[$vatValue];
00353                 }
00354                 else
00355                 {
00356                     $shippingVatList[$vatValue]['shipping_ex_vat'] += $subShippingExVat[$vatValue];
00357                     $shippingVatList[$vatValue]['shipping_inc_vat'] += $subShippingIncVat[$vatValue];
00358                     $shippingVatList[$vatValue]['shipping_vat'] += $subShippingVat[$vatValue];
00359                 }
00360 
00361                 $totalShippingExVat += $subShippingExVat[$vatValue];
00362                 $totalShippingIncVat += $subShippingIncVat[$vatValue];
00363                 $totalShippingVat += $subShippingVat[$vatValue];
00364             }
00365         }
00366         else // made for backwards compability.
00367         {
00368             $vatValue = $shippingInfo['vat_value'];
00369             $shippingCost = $shippingInfo['cost'];
00370             $isVatInc = $shippingInfo['is_vat_inc'];
00371             if ( $isVatInc == 1 )
00372             {
00373                 $divideValue = ( $vatValue / 100 ) + 1;
00374                 $subShippingExVat[$vatValue] = $shippingCost;
00375                 $subShippingIncVat[$vatValue] = ( $shippingCost / $divideValue );
00376                 $subShippingVat[$vatValue] = ( $subShippingIncVat[$vatValue] - $subShippingExVat[$vatValue] );
00377             }
00378             else
00379             {
00380                 $multiplier = ( $vatValue / 100 ) + 1;
00381                 $subShippingExVat[$vatValue] = $shippingCost;
00382                 $subShippingIncVat[$vatValue] = ( $shippingCost * $multiplier );
00383                 $subShippingVat[$vatValue] = ( $subShippingIncVat[$vatValue] - $subShippingExVat[$vatValue] );
00384             }
00385 
00386             $shippingVatList[$vatValue]['shipping_ex_vat'] = $subShippingExVat[$vatValue];
00387             $shippingVatList[$vatValue]['shipping_inc_vat'] = $subShippingIncVat[$vatValue];
00388             $shippingVatList[$vatValue]['shipping_vat'] = $subShippingVat[$vatValue];
00389 
00390             $totalShippingExVat = $subShippingExVat[$vatValue];
00391             $totalShippingIncVat = $subShippingIncVat[$vatValue];
00392             $totalShippingVat = $subShippingVat[$vatValue];
00393         }
00394         $returnArray = array( 'vat_shipping_list_ex_vat' => $subShippingExVat,
00395                               'vat_shipping_list_inc_vat' => $subShippingIncVat,
00396                               'vat_shipping_list_vat' => $subShippingVat,
00397                               'shipping_vat_list' => $shippingVatList,
00398                               'total_shipping_ex_vat' => $totalShippingExVat,
00399                               'total_shipping_inc_vat' => $totalShippingIncVat,
00400                               'total_shipping_vat' => $totalShippingVat );
00401         return $returnArray;
00402     }
00403 
00404 
00405     /*!
00406      Update shipping price with calculated information based on original values.
00407      All values are changed or added directly in the array $basketInfo
00408      \public
00409      \static
00410 
00411      Example on a calculated $basketInfo variable:
00412      \code
00413      array( 'price_info' => array( 0 => array( 'price_ex_vat' => 231,
00414                                                'price_inc_vat' => 231,
00415                                                'price_vat' => 0,
00416                                                'total_price_ex_vat' => 231,
00417                                                'total_price_inc_vat' => 231,
00418                                                'total_price_vat' => 0 ),
00419                                    12 => array( 'total_price_ex_vat' => 50.25,
00420                                                 'total_price_inc_vat' => 56.28,
00421                                                 'total_price_vat' => 6.03 ),
00422                                    25 => array( 'total_price_ex_vat' => 80.6,
00423                                                 'total_price_inc_vat' => 100.75,
00424                                                 'total_price_vat' => 20.15 ) ),
00425             'total_price_info' => array( 'price_ex_vat' => 231,
00426                                          'price_inc_vat' => 231,
00427                                          'price_vat' => 0,
00428                                          'total_price_ex_vat' => 361.85,
00429                                          'total_price_inc_vat' => 388.03,
00430                                          'total_price_vat' => 26.18 ),
00431             'additional_info' => array( 'shipping_items' => array( 12 => array( 'total_price_ex_vat' => 50.25,
00432                                                                                 'total_price_inc_vat' => 56.28,
00433                                                                                 'total_price_vat' => 6.03 ),
00434                                                                    25 => array( 'total_price_ex_vat' => 80.6,
00435                                                                                 'total_price_inc_vat' => 100.75,
00436                                                                                 'total_price_vat' => 20.15 ) ),
00437                                         'shipping_total' => array( 'total_price_ex_vat' => 130.85,
00438                                                                    'total_price_inc_vat' => 157.03,
00439                                                                    'total_price_vat' => 26.18 ) ) );
00440      \endcode
00441     */
00442     static function updatePriceInfo( $productCollectionID, &$basketInfo )
00443     {
00444         $returnValue = false;
00445         if ( is_object( $handler = eZShippingManager::loadBasketInfoHandler() ) )
00446         {
00447             $returnValue = $handler->updatePriceInfo( $productCollectionID, $basketInfo );
00448         }
00449         return $returnValue;
00450     }
00451 }
00452 ?>