eZ Publish  [trunk]
ezorder.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZOrder class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZOrder ezorder.php
00013   \brief eZOrder handles orders
00014   \ingroup eZKernel
00015 
00016   \sa eZProductCollection eZBasket
00017 */
00018 
00019 class eZOrder extends eZPersistentObject
00020 {
00021     const SHOW_NORMAL = 0;
00022     const SHOW_ARCHIVED = 1;
00023     const SHOW_ALL = 2;
00024 
00025     function eZOrder( $row )
00026     {
00027         $this->eZPersistentObject( $row );
00028         $this->Status = null;
00029     }
00030 
00031     static function definition()
00032     {
00033         return array( "fields" => array( "id" => array( 'name' => 'ID',
00034                                                         'datatype' => 'integer',
00035                                                         'default' => 0,
00036                                                         'required' => true ),
00037                                          "order_nr" => array( 'name' => "OrderNr",
00038                                                               'datatype' => 'integer',
00039                                                               'default' => 0,
00040                                                               'required' => true ),
00041                                          "is_temporary" => array( 'name' => "IsTemporary",
00042                                                                   'datatype' => 'integer',
00043                                                                   'default' => 1,
00044                                                                   'required' => true ),
00045                                          "user_id" => array( 'name' => "UserID",
00046                                                              'datatype' => 'integer',
00047                                                              'default' => 0,
00048                                                              'required' => true,
00049                                                              'foreign_class' => 'eZUser',
00050                                                              'foreign_attribute' => 'contentobject_id',
00051                                                              'multiplicity' => '1..*' ),
00052                                          "productcollection_id" => array( 'name' => "ProductCollectionID",
00053                                                                           'datatype' => 'integer',
00054                                                                           'default' => 0,
00055                                                                           'required' => true,
00056                                                                           'foreign_class' => 'eZProductCollection',
00057                                                                           'foreign_attribute' => 'id',
00058                                                                           'multiplicity' => '1..*' ),
00059                                          "data_text_1" => array( 'name' => "DataText1",
00060                                                                  'datatype' => 'text',
00061                                                                  'default' => '',
00062                                                                  'required' => true ),
00063                                          "data_text_2" => array( 'name' => "DataText2",
00064                                                                  'datatype' => 'text',
00065                                                                  'default' => '',
00066                                                                  'required' => true ),
00067                                          "account_identifier" => array( 'name' => "AccountIdentifier",
00068                                                                         'datatype' => 'string',
00069                                                                         'default' => 'default',
00070                                                                         'required' => true ),
00071                                          "created" => array( 'name' => "Created",
00072                                                              'datatype' => 'integer',
00073                                                              'default' => 0,
00074                                                              'required' => true ),
00075                                          "ignore_vat" => array( 'name' => "IgnoreVAT",
00076                                                                 'datatype' => 'integer',
00077                                                                 'default' => 0,
00078                                                                 'required' => true ),
00079                                          "email" => array( 'name' => "Email",
00080                                                            'datatype' => 'string',
00081                                                            'default' => '',
00082                                                            'required' => true ),
00083                                          "status_id" => array( 'name' => 'StatusID',
00084                                                                'datatype' => 'integer',
00085                                                                'default' => 0,
00086                                                                'required' => false,
00087                                                                'foreign_class' => 'eZOrderStatus',
00088                                                                'foreign_attribute' => 'id',
00089                                                                'multiplicity' => '1..*' ),
00090                                          "status_modified" => array( 'name' => "StatusModified",
00091                                                                      'datatype' => 'integer',
00092                                                                      'default' => 0,
00093                                                                      'required' => true ),
00094                                          "status_modifier_id" => array( 'name' => "StatusModifierID",
00095                                                                         'datatype' => 'integer',
00096                                                                         'default' => 0,
00097                                                                         'required' => true,
00098                                                                         'foreign_class' => 'eZUser',
00099                                                                         'foreign_attribute' => 'contentobject_id',
00100                                                                         'multiplicity' => '1..*' ),
00101                                          "is_archived" => array( 'name' => "IsArchived",
00102                                                                         'datatype' => 'integer',
00103                                                                         'default' => 0,
00104                                                                         'required' => true ) ),
00105                       'function_attributes' => array( 'status_name' => 'statusName',
00106                                                       'status' => 'statusObject',
00107                                                       'status_modification_list' => 'statusModificationList',
00108                                                       'product_items' => 'productItems',
00109                                                       'product_items_ordered' => 'productItemsOrdered',
00110                                                       'order_items' => 'orderItems',
00111                                                       'product_total_inc_vat' => 'productTotalIncVAT',
00112                                                       'product_total_ex_vat' => 'productTotalExVAT',
00113                                                       'total_inc_vat' => 'totalIncVAT',
00114                                                       'total_ex_vat' => 'totalExVAT',
00115                                                       'user' => 'user',
00116                                                       'account_view_template' => 'accountViewTemplate',
00117                                                       'account_information' => 'accountInformation',
00118                                                       'account_name' => 'accountName',
00119                                                       'account_email' => 'accountEmail',
00120                                                       'productcollection' => 'productCollection',
00121                                                       'order_info' => 'orderInfo' ),
00122                       "keys" => array( "id" ),
00123                       "increment_key" => "id",
00124                       "class_name" => "eZOrder",
00125                       "name" => "ezorder" );
00126     }
00127 
00128 
00129     /*!
00130      Makes a copy of the product collection it currently points to
00131      and sets the copied collection as the current collection.
00132      \note This will store the order with the new product collection.
00133      \return the new collection or \c false if something failed.
00134      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00135      the calls within a db transaction; thus within db->begin and db->commit.
00136     */
00137     function detachProductCollection()
00138     {
00139         $collection = $this->productCollection();
00140         if ( !$collection )
00141         {
00142             $retValue = false;
00143             return $retValue;
00144         }
00145 
00146         $db = eZDB::instance();
00147         $db->begin();
00148         $newCollection = $collection->copy();
00149         if ( !$newCollection )
00150         {
00151             $db->commit();
00152             $retValue = false;
00153             return $retValue;
00154         }
00155         $this->setAttribute( 'productcollection_id', $newCollection->attribute( 'id' ) );
00156         $this->store();
00157 
00158         $db->commit();
00159         return $newCollection;
00160     }
00161 
00162     /*!
00163      \return the product collection which this order uses.
00164     */
00165     function productCollection()
00166     {
00167         return eZProductCollection::fetch( $this->attribute( 'productcollection_id' ) );
00168     }
00169 
00170     static function fetch( $id, $asObject = true )
00171     {
00172         return eZPersistentObject::fetchObject( eZOrder::definition(),
00173                                                 null,
00174                                                 array( "id" => $id ),
00175                                                 $asObject );
00176     }
00177 
00178     static function fetchList( $asObject = true )
00179     {
00180         return eZPersistentObject::fetchObjectList( eZOrder::definition(),
00181                                                     null, null,
00182                                                     array( "created" => "desc" ), null,
00183                                                     $asObject );
00184     }
00185 
00186     static function activeByUserID( $userID, $asObject = true )
00187     {
00188         return eZPersistentObject::fetchObjectList( eZOrder::definition(),
00189                                                     null,
00190                                                     array( "user_id" => $userID,
00191                                                            'is_temporary' => 0 ),
00192                                                     array( "created" => "desc" ), null,
00193                                                     $asObject );
00194     }
00195 
00196     static function getShowOrdersQuery( $show, $table = null )
00197     {
00198         $table = ( $table === null ? '' : $table . '.' );
00199 
00200         switch( $show )
00201         {
00202             case eZOrder::SHOW_NORMAL   : return $table."is_archived = '0'"; break;
00203             case eZOrder::SHOW_ARCHIVED : return $table."is_archived = '1'"; break;
00204             case eZOrder::SHOW_ALL      :
00205             default                   : return $table."is_archived IN (0, 1)"; break;
00206         }
00207     }
00208 
00209 
00210     /*!
00211      \return the active orders
00212     */
00213     static function active( $asObject = true, $offset, $limit, $sortField = "created", $sortOrder = "asc", $show = eZOrder::SHOW_NORMAL )
00214     {
00215         if ( $sortField == "user_name" )
00216         {
00217             $db = eZDB::instance();
00218 
00219             $db_params = array();
00220             $db_params["offset"] =(int) $offset;
00221             $db_params["limit"] =(int) $limit;
00222             $sortOrder = $db->escapeString( $sortOrder );
00223 
00224             $query = "SELECT ezorder.*
00225                       FROM
00226                             ezorder,
00227                             ezcontentobject
00228                       WHERE
00229                             ".eZOrder::getShowOrdersQuery( $show, "ezorder" )." AND
00230                             ezorder.is_temporary = '0' AND
00231                             ezcontentobject.id = ezorder.user_id
00232                       ORDER BY ezcontentobject.name $sortOrder";
00233             $orderArray = $db->arrayQuery( $query, $db_params );
00234             if ( $asObject )
00235             {
00236                 $retOrders = array();
00237                 foreach ( $orderArray as $order )
00238                 {
00239                     $order = new eZOrder( $order );
00240                     $retOrders[] = $order;
00241                 }
00242                 return $retOrders;
00243             }
00244             else
00245             {
00246                 return $orderArray;
00247             }
00248         }
00249         else
00250         {
00251             $where['is_temporary'] = 0;
00252             if ( $show != eZOrder::SHOW_ALL )
00253             {
00254                 $where['is_archived'] = $show;
00255             }
00256 
00257             return eZPersistentObject::fetchObjectList( eZOrder::definition(),
00258                                                         null,
00259                                                         $where ,
00260                                                         array( $sortField => $sortOrder ),
00261                                                         array( 'offset' => $offset,
00262                                                                'length' => $limit ), $asObject );
00263         }
00264     }
00265 
00266     /*!
00267      \return the number of active orders
00268     */
00269     static function activeCount( $show = eZOrder::SHOW_NORMAL )
00270     {
00271         $db = eZDB::instance();
00272 
00273         $query = 'SELECT count( * ) AS count FROM ezorder WHERE ' . eZOrder::getShowOrdersQuery( $show ) . ' AND is_temporary=\'0\'';
00274         $countArray = $db->arrayQuery( $query );
00275         return isset( $countArray[0]['count'] ) ? $countArray[0]['count'] : 0;
00276     }
00277 
00278     /*!
00279      \static
00280      \return the number of active orders
00281     */
00282     static function orderStatistics( $year = false, $month = false )
00283     {
00284         if ( $year == false )
00285         {
00286             $startDate = 0;
00287             $stopDate = mktime( 0, 0, 0, 12, 31, 2037 );
00288         }
00289         else if ( $year != false and $month == false )
00290         {
00291             $startDate = mktime( 0, 0, 0, 1, 1, $year );
00292             $stopDate = mktime( 0, 0, 0, 1, 1, $year + 1 );
00293         }
00294         else if ( $year != false and $month != false )
00295         {
00296             $startDate = mktime( 0, 0, 0, $month, 1, $year );
00297             $stopDate = mktime( 23, 59, 59, $month + 1, 0, $year );
00298         }
00299 
00300         $db = eZDB::instance();
00301         $productArray = $db->arrayQuery(  "SELECT ezproductcollection_item.*,  ignore_vat, ezorder.created, currency_code FROM ezorder, ezproductcollection_item, ezproductcollection
00302                                                 WHERE ezproductcollection.id=ezproductcollection_item.productcollection_id
00303                                                   AND ezproductcollection_item.productcollection_id=ezorder.productcollection_id
00304                                                   AND is_temporary='0'
00305                                                   AND ezorder.created >= '$startDate' AND ezorder.created < '$stopDate'
00306                                              ORDER BY contentobject_id, currency_code" );
00307         $currentContentObjectID = 0;
00308         $productItemArray = array();
00309         $itemCount = 0;
00310         $name = false;
00311         $productInfo = array();
00312         $totalSumInfo = array();
00313         $firstPass = true;
00314         // Hash of ContentObject ID, the value will be replaced by the correct object once all IDs are known.
00315         $contentObjectIDHash = array();
00316         foreach( $productArray as $productItem )
00317         {
00318             $itemCount++;
00319             $contentObjectID = $productItem['contentobject_id'];
00320 
00321             if ( $firstPass )
00322             {
00323                 $contentObjectIDHash[$currentContentObjectID] = true;
00324                 $currentContentObjectID = $contentObjectID;
00325                 $firstPass = false;
00326             }
00327 
00328             if ( $currentContentObjectID != $contentObjectID && $itemCount != 1 )
00329             {
00330                 $productItemArray[] = array(
00331                     'name' => $name,
00332                     // Reference to the entry that will contain the ContentObject at the end
00333                     'product' => &$contentObjectIDHash[$currentContentObjectID],
00334                     'product_info' => $productInfo
00335                 );
00336                 $productInfo = array();
00337                 $name = $productItem['name'];
00338                 $currentContentObjectID = $contentObjectID;
00339                 $contentObjectIDHash[$currentContentObjectID] = true;
00340             }
00341 
00342             $currencyCode = $productItem['currency_code'];
00343             if ( $currencyCode == '' )
00344             {
00345                 $currencyCode = eZOrder::fetchLocaleCurrencyCode();
00346             }
00347 
00348             if ( !isset( $productInfo[$currencyCode] ) )
00349             {
00350                 $productInfo[$currencyCode] = array( 'sum_count' => 0,
00351                                                      'sum_ex_vat' => 0,
00352                                                      'sum_inc_vat' => 0 );
00353             }
00354             if ( !isset( $totalSumInfo[$currencyCode] ) )
00355             {
00356                 $totalSumInfo[$currencyCode] = array( 'sum_ex_vat' => 0,
00357                                                       'sum_inc_vat' => 0 );
00358             }
00359 
00360             if ( $productItem['ignore_vat']== true )
00361             {
00362                 $vatValue = 0;
00363             }
00364             else
00365             {
00366                 $vatValue = $productItem['vat_value'];
00367             }
00368 
00369             $price = $productItem['price'];
00370 
00371             if ( $productItem['is_vat_inc'] )
00372             {
00373                 $priceExVAT = $price / ( 100 + $vatValue ) * 100;
00374                 $priceIncVAT = $price;
00375                 
00376             }
00377             else
00378             {
00379                 $priceExVAT = $price;
00380                 $priceIncVAT = $price * ( 100 + $vatValue ) / 100;
00381             }
00382             
00383             $count = $productItem['item_count'];
00384             $realPricePercent = ( 100 - $productItem['discount'] ) / 100;
00385             $totalPriceExVAT = round( $count * $priceExVAT * $realPricePercent, 2 );
00386             $totalPriceIncVAT = round( $count * $priceIncVAT * $realPricePercent, 2 );
00387             $totalSumInfo[$currencyCode]['sum_ex_vat'] += $totalPriceExVAT;
00388             $totalSumInfo[$currencyCode]['sum_inc_vat'] += $totalPriceIncVAT;
00389 
00390             $productInfo[$currencyCode]['sum_count'] += $count;
00391             $productInfo[$currencyCode]['sum_ex_vat'] += $totalPriceExVAT;
00392             $productInfo[$currencyCode]['sum_inc_vat'] += $totalPriceIncVAT;
00393         }
00394 
00395         // add last product info
00396         if ( !empty( $productArray ) )
00397             $productItemArray[] = array(
00398                 'name' => $name,
00399                 // Reference to the entry that will contain the ContentObject at the end
00400                 'product' => &$contentObjectIDHash[$currentContentObjectID],
00401                 'product_info' => $productInfo
00402             );
00403         
00404         // Fetching all ContentObject ids in one query, filling the hash with the corresponding ContentObject
00405         foreach ( eZContentObject::fetchList( true, array( "id" => array( array_keys( $contentObjectIDHash ) ) ) ) as $contentObject )
00406         {
00407             $contentObjectIDHash[$contentObject->ID] = $contentObject;
00408         }
00409 
00410         return array(
00411             array(
00412                 'product_list' => $productItemArray,
00413                 'total_sum_info' => $totalSumInfo
00414             )
00415         );
00416     }
00417 
00418     /*!
00419      \return list of products for a custom
00420     */
00421     static function orderList( $CustomID, $Email )
00422     {
00423         $db = eZDB::instance();
00424         $CustomID =(int) $CustomID;
00425         $Email = $db->escapeString( $Email );
00426         if ( $Email == false )
00427         {
00428             $orderArray = $db->arrayQuery( "SELECT ezorder.* FROM ezorder
00429                                             WHERE user_id='$CustomID'
00430                                               AND is_archived='0'
00431                                               AND is_temporary='0'
00432                                          ORDER BY order_nr" );
00433         }
00434         else
00435         {
00436             $orderArray = $db->arrayQuery( "SELECT ezorder.* FROM ezorder
00437                                             WHERE user_id='$CustomID'
00438                                               AND is_archived='0'
00439                                               AND is_temporary='0'
00440                                               AND email='$Email'
00441                                          ORDER BY order_nr" );
00442         }
00443         $retOrders = array();
00444         foreach( $orderArray as $orderRows )
00445         {
00446             $retOrders[] = new eZOrder( $orderRows );
00447         }
00448         return $retOrders;
00449     }
00450 
00451     /*!
00452      \return list of products for a custom
00453     */
00454     static function productList( $CustomID, $Email )
00455     {
00456         $db = eZDB::instance();
00457         $CustomID =(int) $CustomID;
00458         $Email = $db->escapeString( $Email );
00459         if ( $Email == false )
00460         {
00461             $productArray = $db->arrayQuery(  "SELECT ezproductcollection_item.*, ignore_vat, currency_code FROM ezorder, ezproductcollection_item, ezproductcollection
00462                                                 WHERE ezproductcollection.id=ezproductcollection_item.productcollection_id
00463                                                   AND ezproductcollection_item.productcollection_id=ezorder.productcollection_id
00464                                                   AND user_id='$CustomID'
00465                                                   AND is_archived='0'
00466                                                   AND is_temporary='0'
00467                                              ORDER BY contentobject_id, currency_code" );
00468         }
00469         else
00470         {
00471             $productArray = $db->arrayQuery(  "SELECT ezproductcollection_item.*, ignore_vat, currency_code FROM ezorder, ezproductcollection_item, ezproductcollection
00472                                                 WHERE ezproductcollection.id=ezproductcollection_item.productcollection_id
00473                                                   AND ezproductcollection_item.productcollection_id=ezorder.productcollection_id
00474                                                   AND user_id='$CustomID'
00475                                                   AND is_archived='0'
00476                                                   AND is_temporary='0'
00477                                                   AND email='$Email'
00478                                              ORDER BY contentobject_id, currency_code" );
00479         }
00480         $currentContentObjectID = 0;
00481         $productItemArray = array();
00482         $productObject = null;
00483         $itemCount = 0;
00484         $name = false;
00485         $productInfo = array();
00486 
00487         foreach( $productArray as $productItem )
00488         {
00489             $itemCount++;
00490             $contentObjectID = $productItem['contentobject_id'];
00491             if ( $productObject == null )
00492             {
00493                 if ( $contentObjectID != 0 )
00494                 {
00495                     $productObject = eZContentObject::fetch( $contentObjectID );
00496                 }
00497                 else
00498                 {
00499                     $productObject = null;
00500                     $name = $productItem['name'];
00501                 }
00502                 $currentContentObjectID = $contentObjectID;
00503             }
00504             if ( $currentContentObjectID != $contentObjectID && $itemCount != 1 )
00505             {
00506                 $productItemArray[] = array( 'name' => $name,
00507                                              'product' => $productObject,
00508                                              'product_info' => $productInfo );
00509                 unset( $productObject );
00510                 $productInfo = array();
00511                 $name = $productItem['name'];
00512                 $currentContentObjectID = $contentObjectID;
00513                 if ( $contentObjectID != 0 )
00514                 {
00515                     $productObject = eZContentObject::fetch( $currentContentObjectID );
00516                 }
00517                 else
00518                 {
00519                     $productObject = null;
00520                 }
00521             }
00522 
00523             $currencyCode = $productItem['currency_code'];
00524             if ( $currencyCode == '' )
00525             {
00526                 $currencyCode = eZOrder::fetchLocaleCurrencyCode();
00527             }
00528 
00529             if ( !isset( $productInfo[$currencyCode] ) )
00530             {
00531                 $productInfo[$currencyCode] = array( 'sum_count' => 0,
00532                                                      'sum_ex_vat' => 0,
00533                                                      'sum_inc_vat' => 0 );
00534             }
00535 
00536             if ( $productItem['ignore_vat'] == true )
00537             {
00538                 $vatValue = 0;
00539             }
00540             else
00541             {
00542                 $vatValue = $productItem['vat_value'];
00543             }
00544 
00545             $price = $productItem['price'];
00546 
00547             if ( $productItem['is_vat_inc'] )
00548             {
00549                 $priceExVAT = $price / ( 100 + $vatValue ) * 100;
00550                 $priceIncVAT = $price;
00551             }
00552             else
00553             {
00554                 $priceExVAT = $price;
00555                 $priceIncVAT = $price * ( 100 + $vatValue ) / 100;
00556             }
00557 
00558             $count = $productItem['item_count'];
00559             $realPricePercent = ( 100 - $productItem['discount'] ) / 100;
00560 
00561             $productInfo[$currencyCode]['sum_count'] += $count;
00562             $productInfo[$currencyCode]['sum_ex_vat'] += round( $count * $priceExVAT * $realPricePercent, 2 );
00563             $productInfo[$currencyCode]['sum_inc_vat'] += round( $count * $priceIncVAT * $realPricePercent, 2 );
00564         }
00565         if ( count( $productArray ) != 0 )
00566         {
00567             $productItemArray[] = array( 'name' => $name,
00568                                          'product' => $productObject,
00569                                          'product_info' => $productInfo );
00570         }
00571         return $productItemArray;
00572     }
00573 
00574     /*!
00575      \returns number of customers.
00576     */
00577     static function customerCount( )
00578     {
00579         $db = eZDB::instance();
00580         $countArray = $db->arrayQuery(  "SELECT count( DISTINCT email) AS count FROM ezorder WHERE is_temporary='0'" );
00581         return $countArray[0]['count'];
00582     }
00583 
00584     /*!
00585      \return the list customers.
00586     */
00587     static function customerList( $offset, $limit )
00588     {
00589         $db = eZDB::instance();
00590 
00591         $db_params = array();
00592         $db_params["offset"] =(int) $offset;
00593         $db_params["limit"] =(int) $limit;
00594 
00595         $customEmailResult = $db->arrayQuery( "SELECT DISTINCT email FROM ezorder WHERE is_temporary='0' ORDER BY email", $db_params );
00596         $customEmailArray = array();
00597 
00598         foreach( $customEmailResult as $customEmailRow )
00599         {
00600             $customEmail = $customEmailRow['email'];
00601             $customEmailArray[] = "'" . $customEmail . "'";
00602         }
00603 
00604         $emailString = implode( ", ", $customEmailArray );
00605         if ( !strlen( $emailString ) )
00606         {
00607             $emailString = "''";
00608         }
00609 
00610         $productItemArray = $db->arrayQuery(  "SELECT ezorder.id as order_id, user_id, email, ignore_vat, currency_code, ezproductcollection_item.*
00611                                                  FROM ezorder, ezproductcollection_item, ezproductcollection
00612                                                 WHERE ezproductcollection_item.productcollection_id=ezorder.productcollection_id
00613                                                   AND is_temporary='0'
00614                                                   AND ezproductcollection_item.productcollection_id=ezproductcollection.id
00615                                                   AND email in ( $emailString )
00616                                              ORDER BY user_id, email, order_id" );
00617 
00618 
00619         $siteIni = eZINI::instance();
00620         $anonymousUserID = $siteIni->variable( 'UserSettings', 'AnonymousUserID' );
00621 
00622         $currentUserID = 0;
00623         $currentOrderID = 0;
00624         $currentUserEmail = "";
00625         $customArray = array();
00626         $accountName = null;
00627         $itemCount = 0;
00628         $hash = 0;
00629         $currencyCode = '';
00630         $ordersInfo = array();
00631 
00632         foreach( $productItemArray as $productItem )
00633         {
00634             $itemCount++;
00635             $currencyCode = $productItem['currency_code'];
00636             if ( $currencyCode == '' )
00637             {
00638                 $currencyCode = eZOrder::fetchLocaleCurrencyCode();
00639             }
00640 
00641             $userID = $productItem['user_id'];
00642             $orderID = $productItem['order_id'];
00643             $order = eZOrder::fetch( $orderID );
00644 
00645             if ( $currentUserID != $userID && $itemCount != 1 )
00646             {
00647                 $customArray[] = array( 'account_name' => $accountName,
00648                                         'orders_info' => $ordersInfo,
00649                                         'user_id' => $currentUserID,
00650                                         'email' => urlencode( $currentUserEmail ) );
00651 
00652                 $ordersInfo = array();
00653                 $accountName = $order->attribute( 'account_name' );
00654                 $accountEmail = $order->attribute( 'account_email' );
00655             }
00656 
00657             $currentUserID = $userID;
00658 
00659             // If the custom is anoymous user
00660             if ( $currentUserID == $anonymousUserID )
00661             {
00662                 $accountEmail = $order->attribute( 'email' );
00663                 if ( $currentUserEmail == "" )
00664                 {
00665                     $accountName = $order->attribute( 'account_name' );
00666                     $currentUserEmail = $accountEmail;
00667                 }
00668 
00669                 if ( $currentUserEmail != $accountEmail )
00670                 {
00671                     $customArray[] = array( 'account_name' => $accountName,
00672                                             'orders_info' => $ordersInfo,
00673                                             'user_id' => $currentUserID,
00674                                             'email' => urlencode( $currentUserEmail ) );
00675 
00676                     $ordersInfo = array();
00677                     $accountName = $order->attribute( 'account_name' );
00678                     $accountEmail = $order->attribute( 'account_email' );
00679                     $currentUserEmail = $accountEmail;
00680                 }
00681                 $currentUserEmail = $accountEmail;
00682             }
00683             else
00684             {
00685                 $currentUserEmail = 0;
00686             }
00687 
00688             $accountName = $order->attribute( 'account_name' );
00689 
00690             if ( !isset( $ordersInfo[$currencyCode] ) )
00691             {
00692                 $ordersInfo[$currencyCode] = array( 'order_count' => 0,
00693                                                     'sum_ex_vat' => 0,
00694                                                     'sum_inc_vat' => 0 );
00695             }
00696 
00697             if (  $currentOrderID != $orderID )
00698             {
00699                 $ordersInfo[$currencyCode]['order_count']++;
00700             }
00701             $currentOrderID = $orderID;
00702 
00703             if ( $productItem['ignore_vat'] == true )
00704             {
00705                 $vatValue = 0;
00706             }
00707             else
00708             {
00709                 $vatValue = $productItem['vat_value'];
00710             }
00711 
00712             $price = $productItem['price'];
00713 
00714             if ( $productItem['is_vat_inc'] )
00715             {
00716                 $priceExVAT = $price / ( 100 + $vatValue ) * 100;
00717                 $priceIncVAT = $price;
00718             }
00719             else
00720             {
00721                 $priceExVAT = $price;
00722                 $priceIncVAT = $price * ( 100 + $vatValue ) / 100;
00723             }
00724 
00725             $count = $productItem['item_count'];
00726             $realPricePercent = ( 100 - $productItem['discount'] ) / 100;
00727             $ordersInfo[$currencyCode]['sum_ex_vat'] += round( $count * $priceExVAT * $realPricePercent, 2 );
00728             $ordersInfo[$currencyCode]['sum_inc_vat'] += round( $count * $priceIncVAT * $realPricePercent, 2 );
00729         }
00730         if ( count( $productItemArray ) != 0 )
00731             $customArray[] = array( 'account_name' => $accountName,
00732                                     'orders_info' => $ordersInfo,
00733                                     'user_id' => $currentUserID,
00734                                     'email' => urlencode( $currentUserEmail ) );
00735         return $customArray;
00736     }
00737 
00738     /*!
00739      \returns the discountrules for a user
00740     */
00741     static function discount( $userID, $object )
00742     {
00743         return eZDiscount::discountPercent(
00744             eZUser::fetch( $userID ),
00745             array(
00746                 'contentclass_id' => $object->attribute( 'contentclass_id'),
00747                 'contentobject_id' => $object->attribute( 'id' ),
00748                 'section_id' => $object->attribute( 'section_id')
00749             )
00750         );
00751     }
00752 
00753     /**
00754      * Fetch product items that bellongs ot the order
00755      *
00756      * @param bool $asObject
00757      * @param array|null $sorts Array with sort data sent directly to {@link eZPersistentObject::fetchObjectList()}
00758      */
00759     function productItems( $asObject = true, array $sorts = null )
00760     {
00761         $productItems = eZPersistentObject::fetchObjectList( eZProductCollectionItem::definition(),
00762                                                        null,
00763                                                        array( 'productcollection_id' => $this->ProductCollectionID ),
00764                                                        $sorts,
00765                                                        null,
00766                                                        $asObject );
00767 
00768         $addedProducts = array();
00769         foreach ( $productItems as  $productItem )
00770         {
00771             $contentObject = $productItem->attribute( 'contentobject' );
00772 
00773             if ( $this->IgnoreVAT == true )
00774             {
00775                 $vatValue = 0;
00776             }
00777             else
00778             {
00779                 $vatValue = $productItem->attribute( 'vat_value' );
00780             }
00781             if ( $contentObject )
00782             {
00783                 $nodeID = $contentObject->attribute( 'main_node_id' );
00784                 $objectName = $contentObject->attribute( 'name' );
00785             }
00786             else
00787             {
00788                 $nodeID = false;
00789                 $objectName = $productItem->attribute( 'name' );
00790             }
00791 
00792             $price = $productItem->attribute( 'price' );
00793 
00794             if ( $productItem->attribute( 'is_vat_inc' ) )
00795             {
00796                 $priceExVAT = $price / ( 100 + $vatValue ) * 100;
00797                 $priceIncVAT = $price;
00798             }
00799             else
00800             {
00801                 $priceExVAT = $price;
00802                 $priceIncVAT = $price * ( 100 + $vatValue ) / 100;
00803             }
00804             
00805             $count = $productItem->attribute( 'item_count' );
00806             $discountPercent = $productItem->attribute( 'discount' );
00807             $realPricePercent = ( 100 - $discountPercent ) / 100;
00808             $addedProducts[] = array( "id" => $productItem->attribute( 'id' ),
00809                                    "vat_value" => $vatValue,
00810                                    "item_count" => $count,
00811                                    "node_id" => $nodeID,
00812                                    "object_name" => $objectName,
00813                                    "price_ex_vat" => $priceExVAT,
00814                                    "price_inc_vat" => $priceIncVAT,
00815                                    "discount_percent" => $discountPercent,
00816                                    "total_price_ex_vat" => round( $count * $priceExVAT * $realPricePercent, 2 ),
00817                                    "total_price_inc_vat" => round( $count * $priceIncVAT * $realPricePercent, 2 ),
00818                                    'item_object' => $productItem );
00819         }
00820         return $addedProducts;
00821     }
00822 
00823     /**
00824      * Fetch product items ordered by id ( the order they where added to order )
00825      *
00826      * @param bool $asObject
00827      * @param bool $order True (default) for ascending[0->9] and false for decending[9->0]
00828      */
00829     function productItemsOrdered( $asObject = true, $order = true )
00830     {
00831         return $this->productItems( $asObject, array( 'id' => ( $order ? 'asc' : 'desc' )) );
00832     }
00833 
00834     function productTotalIncVAT()
00835     {
00836         $items = $this->productItems();
00837 
00838         $total = 0.0;
00839         foreach ( $items as $item )
00840         {
00841             $total += $item['total_price_inc_vat'];
00842         }
00843         return round( $total, 2 );
00844     }
00845 
00846     function productTotalExVAT()
00847     {
00848         $items = $this->productItems();
00849 
00850         $total = 0.0;
00851         foreach ( $items as $item )
00852         {
00853             $total += $item['total_price_ex_vat'];
00854         }
00855         return round( $total, 2 );
00856     }
00857 
00858     function orderTotalIncVAT()
00859     {
00860         $items = $this->orderItems();
00861 
00862         $total = 0.0;
00863         if ( count( $items ) > 0 )
00864         {
00865             foreach ( $items as $item )
00866             {
00867                 $total += $item->attribute( 'price_inc_vat' );
00868             }
00869         }
00870         return round( $total, 2 );
00871     }
00872 
00873     function orderTotalExVAT()
00874     {
00875         $items = $this->orderItems();
00876 
00877         $total = 0.0;
00878         if ( count( $items ) > 0 )
00879         {
00880             foreach ( $items as $item )
00881             {
00882                 $total += $item->attribute( 'price_ex_vat' );
00883             }
00884         }
00885         return round( $total, 2 );
00886     }
00887 
00888     function totalIncVAT()
00889     {
00890         return $this->productTotalIncVAT() + $this->orderTotalIncVAT();
00891     }
00892 
00893     function totalExVAT()
00894     {
00895         return $this->productTotalExVAT() + $this->orderTotalExVAT();
00896     }
00897 
00898     /*!
00899      Removes the order.
00900 
00901      shop/confirmorder calls this method passing $removeCollection set to \c false
00902      to purge the order if the user doesn't confirm it.
00903      Removing the product collection in this case would cause fatal error.
00904 
00905      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00906      the calls within a db transaction; thus within db->begin and db->commit.
00907      \param $removeCollection (bool) specifies if we should remove product collection the order uses.
00908     */
00909     function purge( $removeCollection = true )
00910     {
00911         $db = eZDB::instance();
00912         $db->begin();
00913         if ( $removeCollection )
00914             $this->removeCollection();
00915         $this->removeHistory();
00916         $this->removeOrderItems();
00917         $this->remove();
00918         $db->commit();
00919     }
00920 
00921     /*!
00922      Removes the product collection this order uses.
00923      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00924      the calls within a db transaction; thus within db->begin and db->commit.
00925     */
00926     function removeCollection()
00927     {
00928         $collection = eZProductCollection::fetch( $this->attribute( 'productcollection_id' ) );
00929         $collection->remove();
00930     }
00931 
00932     /*!
00933      Removes the order status history for this order.
00934      \note transaction unsafe
00935     */
00936     function removeHistory()
00937     {
00938         $db = eZDB::instance();
00939         $orderID = (int)$this->OrderNr;
00940         $db->query( "DELETE FROM ezorder_status_history WHERE order_id=$orderID" );
00941     }
00942 
00943     function removeOrderItems()
00944     {
00945         $db = eZDB::instance();
00946         $orderID = (int) $this->ID;
00947         $db->query( "DELETE FROM ezorder_item WHERE order_id=$orderID" );
00948     }
00949 
00950     /*!
00951      Removes the product collection item \a $itemID.
00952      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00953      the calls within a db transaction; thus within db->begin and db->commit.
00954     */
00955     static function removeItem( $itemID )
00956     {
00957         $item = eZProductCollectionItem::fetch( $itemID );
00958         $item->remove();
00959     }
00960 
00961     /*!
00962      \return the total VAT value of the order
00963     */
00964     function totalVAT()
00965     {
00966         return false;
00967     }
00968 
00969     function orderItems()
00970     {
00971         $id = $this->attribute( 'id' );
00972         return eZOrderItem::fetchList( $id );
00973     }
00974 
00975     /*!
00976     \param $type Order item type
00977     */
00978     function orderItemsByType( $itemType )
00979     {
00980         return eZOrderItem::fetchListByType( $this->ID, $itemType );
00981     }
00982 
00983     /*!
00984      \return the user who has created the order.
00985     */
00986     function user()
00987     {
00988         return eZUser::fetch( $this->UserID );
00989     }
00990 
00991     /*!
00992      \return the account information
00993      The shop account handler decides what is returned here
00994     */
00995     function accountInformation()
00996     {
00997         // Fetch the shop account handler
00998         $accountHandler = eZShopAccountHandler::instance();
00999         return $accountHandler->accountInformation( $this );
01000     }
01001 
01002 
01003     /*!
01004      \return the custom name
01005      The shop account handler decides what is returned here
01006     */
01007     function accountName()
01008     {
01009         // Fetch the shop account handler
01010         $accountHandler = eZShopAccountHandler::instance();
01011 
01012         return $accountHandler->accountName( $this );
01013     }
01014 
01015     /*!
01016      \return the account email
01017      The shop account handler decides what is returned here
01018     */
01019     function accountEmail()
01020     {
01021         // Fetch the shop account handler
01022         $accountHandler = eZShopAccountHandler::instance();
01023 
01024         return $accountHandler->email( $this );
01025     }
01026 
01027     /*!
01028      \return The status object if \a $asObject is \c true, otherwise the status ID.
01029     */
01030     function status( $asObject = false )
01031     {
01032         if ( $asObject )
01033             return eZOrderStatus::fetch( $this->StatusID );
01034         else
01035             return $this->StatusID;
01036     }
01037 
01038     /*!
01039       \return \c true if the user \a $user can modify the status to $statusID
01040     */
01041     function canModifyStatus( $statusID, $user = false )
01042     {
01043         if ( $user === false )
01044             $user = eZUser::currentUser();
01045         else if ( is_numeric( $user ) )
01046             $user = eZUser::fetch( $user );
01047 
01048         if ( !is_object( $user ) )
01049         {
01050             eZDebug::writeError( "Cannot check status access without a user", __METHOD__ );
01051             return false;
01052         }
01053 
01054         $accessResult = $user->hasAccessTo( 'shop' , 'setstatus' );
01055         $accessWord = $accessResult['accessWord'];
01056         $access = false;
01057 
01058         $currentStatusID = $this->attribute( "status_id" );
01059 
01060         if ( $accessWord == 'yes' )
01061             $access = true;
01062 
01063         if ( $accessWord == 'limited' )
01064         {
01065             $limitationList = $accessResult['policies'];
01066             $access = true;
01067             foreach ( $limitationList as $pid => $limit )
01068             {
01069                 $access = true;
01070                 foreach ( $limit as $name => $value )
01071                 {
01072                     if ( $name == 'FromStatus' )
01073                     {
01074                         if ( !in_array( $currentStatusID, $value )  )
01075                             $access = false;
01076                     }
01077                     if ( $name == 'ToStatus' )
01078                     {
01079                         if ( !in_array( $statusID, $value ) )
01080                             $access = false;
01081                     }
01082                     if ( !$access )
01083                         break;
01084                 }
01085                 if ( $access )
01086                     break;
01087             }
01088         }
01089         return $access;
01090     }
01091 
01092     /*!
01093      \return A list of status items that the current user can set this order to.
01094      \note If the user doesn't have any access at all for this order it will
01095            return an empty array.
01096     */
01097     function statusModificationList( $user = false )
01098     {
01099         if ( $user === false )
01100             $user = eZUser::currentUser();
01101         else if ( is_numeric( $user ) )
01102             $user = eZUser::fetch( $user );
01103 
01104         if ( !is_object( $user ) )
01105         {
01106             eZDebug::writeError( "Cannot calculate status access list without a user", __METHOD__ );
01107             return false;
01108         }
01109 
01110         $accessResult = $user->hasAccessTo( 'shop' , 'setstatus' );
01111         $accessWord = $accessResult['accessWord'];
01112         $access = false;
01113 
01114         $currentStatusID = $this->attribute( "status_id" );
01115 
01116         $statusList = array();
01117         if ( $accessWord == 'yes' )
01118         {
01119             // We have full access so we return all of them
01120             $statusList = eZOrderStatus::fetchOrderedList( true, false );
01121             return $statusList;
01122         }
01123 
01124         if ( $accessWord == 'limited' )
01125         {
01126             $limitationList = $accessResult['policies'];
01127             $access = true;
01128             // All 'to' statues will be appended to this array
01129             $accessList = array();
01130             foreach ( $limitationList as $pid => $limit )
01131             {
01132                 $access = true;
01133                 foreach ( $limit as $name => $value )
01134                 {
01135                     if ( $name == 'FromStatus' )
01136                     {
01137                         if ( !in_array( $currentStatusID, $value )  )
01138                             $access = false;
01139                     }
01140                     if ( !$access )
01141                         break;
01142                 }
01143                 if ( $access )
01144                 {
01145                     if ( isset( $limit['ToStatus'] ) )
01146                     {
01147                         $accessList = array_merge( $accessList, $limit['ToStatus'] );
01148                     }
01149                     else
01150                     {
01151                         // We have full access for the current status so we return all of them
01152                         $statusList = eZOrderStatus::fetchOrderedList( true, false );
01153                         return $statusList;
01154                     }
01155                 }
01156             }
01157             if ( count( $accessList ) > 0 )
01158             {
01159                 $accessList = array_unique( array_merge( $accessList, array( $currentStatusID ) ) );
01160                 $statuses = eZOrderStatus::fetchOrderedList( true, false );
01161                 foreach ( $statuses as $status )
01162                 {
01163                     if ( in_array( $status->attribute( 'status_id' ), $accessList ) )
01164                         $statusList[] = $status;
01165                 }
01166             }
01167         }
01168         return $statusList;
01169     }
01170 
01171     /*!
01172      Modifies the status on the order to $statusID.
01173      It will store the previous status in the history list using \a $userID.
01174      \param $statusID The ID of the status that is to be set, this must be the global ID not the DB ID.
01175      \param $userID The ID of the user that did the change, if \c false it will fetch the current user ID.
01176 
01177      \note transaction safe
01178      \note If you only want to change the status ID use the setStatus() function instead.
01179     */
01180     function modifyStatus( $statusID, $userID = false )
01181     {
01182         $db = eZDB::instance();
01183         $db->begin();
01184 
01185         $time = time();
01186         if ( $userID === false )
01187             $userID = eZUser::currentUserID();
01188 
01189         $history = eZOrderStatusHistory::create( $this->OrderNr, $statusID, $userID, $time );
01190         $history->store();
01191 
01192         $this->StatusID = $statusID;
01193         $this->StatusModified = $time;
01194         $this->StatusModifierID = $userID;
01195 
01196         $this->store();
01197 
01198         $db->commit();
01199     }
01200 
01201     /*!
01202      Creates a status history element from the the current status information
01203      and stores it in the database.
01204      \return The new history element that was stored in the database.
01205      \note This is usually only needed the first time an order is created.
01206      \note transaction unsafe
01207     */
01208     function createStatusHistory()
01209     {
01210         $history = eZOrderStatusHistory::create( $this->OrderNr, // Note: Use the order nr, not id
01211                                                  $this->StatusID,
01212                                                  $this->StatusModifierID,
01213                                                  $this->StatusModified );
01214         $history->store();
01215         return $history;
01216     }
01217 
01218     /*!
01219      Sets the status ID to \a $status and updates the status modification timestamp.
01220      \note This does not create a status history element, use modifyStatus() instead.
01221     */
01222     function setStatus( $status )
01223     {
01224         if ( $status instanceof eZOrderStatus )
01225             $this->StatusID = $status->attribute( 'id' );
01226         else
01227             $this->StatusID = $status;
01228         $this->setStatusModified( time() );
01229     }
01230 
01231     /*!
01232      Sets the modification time of the status change to \a $timestamp.
01233     */
01234     function setStatusModified( $timestamp )
01235     {
01236         $this->StatusModified = $timestamp;
01237     }
01238 
01239     /*!
01240      \return The name of the current status.
01241      \note It will cache the current status object in the $Status member variable
01242            to make multiple calls to this function fast.
01243     */
01244     function statusName()
01245     {
01246         if ( $this->Status === null )
01247         {
01248             $this->Status = eZOrderStatus::fetchByStatus( $this->StatusID );
01249         }
01250         return $this->Status->attribute( 'name' );
01251     }
01252 
01253     /*!
01254      \return The current status object.
01255      \note It will cache the current status object in the $Status member variable
01256            to make multiple calls to this function fast.
01257     */
01258     function statusObject()
01259     {
01260         if ( $this->Status === null )
01261         {
01262             $this->Status = eZOrderStatus::fetchByStatus( $this->StatusID );
01263         }
01264         return $this->Status;
01265     }
01266 
01267     /*!
01268      Creates a real order from the temporary state.
01269 
01270      The ezorder has an internal (id) and an external (order_nr) ID.
01271      The ID will be set as soon as a customer (who buys a product), approves the account information.
01272      A row is added to the ezorder table (the ID (auto_increment) is generated).
01273      This ID is needed for extensions, such as PaynetTerminal (The ID is given.)
01274 
01275      The active (confirmed and paid) orders have an order_nr.
01276      This order_nr is generated as soon as the order is processed.
01277 
01278      We use order_nr instead of the id to (externally) rever to an order:
01279      - We don't have gaps in the order_nr's.
01280      - The lowest order_nr goes to the order which is accepted first. (Not started first.)
01281 
01282 
01283      Another solution would be to use an temporary_order table, instead of adding two IDs.
01284      I think (rb) this is a cleaner solution.
01285 
01286      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
01287      the calls within a db transaction; thus within db->begin and db->commit.
01288     */
01289     function activate()
01290     {
01291         if( $this->attribute( 'is_temporary' ) )
01292         {
01293             $this->setAttribute( 'is_temporary', 0 );
01294 
01295             $nextID = $this->getNewID();
01296             $this->setAttribute( 'order_nr', $nextID );
01297             $this->store();
01298 
01299             // Create an order status history element that matches the current status
01300             $this->createStatusHistory();
01301         }
01302     }
01303 
01304     /**
01305      * Get a new auto_increment id from a separate table ezorder_nr_incr.
01306      *
01307      * @return int the inserted id
01308      */
01309     function getNewID()
01310     {
01311         $db = eZDB::instance();
01312         if( $db->supportsDefaultValuesInsertion() )
01313         {
01314             $db->query( 'INSERT INTO ezorder_nr_incr DEFAULT VALUES' );
01315         }
01316         else
01317         {
01318             $db->query( 'INSERT INTO ezorder_nr_incr(id) VALUES(DEFAULT)' );
01319         }
01320 
01321         return $db->lastSerialID( 'ezorder_nr_incr', 'id' );
01322     }
01323 
01324     /*!
01325      \return the template to use for account view
01326     */
01327     function accountViewTemplate()
01328     {
01329         return $this->AccountIdentifier;
01330     }
01331 
01332     /*!
01333      \static
01334      Removes an order and its related data from the database.
01335      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
01336      the calls within a db transaction; thus within db->begin and db->commit.
01337     */
01338     static function cleanupOrder( $orderID )
01339     {
01340         $db = eZDB::instance();
01341         $orderID =(int) $orderID;
01342         $rows = $db->arrayQuery( "SELECT productcollection_id, order_nr FROM ezorder WHERE id='$orderID'" );
01343         if ( count( $rows ) > 0 )
01344         {
01345             // Who deletes which order in shop should be logged.
01346             eZAudit::writeAudit( 'order-delete', array( 'Order ID' => $orderID,
01347                                                         'Comment' => 'Removed the order and its related data from the database: eZOrder::cleanupOrder()' ) );
01348 
01349             $productCollectionID = $rows[0]['productcollection_id'];
01350             $orderNr = (int)$rows[0]['order_nr'];
01351             $db = eZDB::instance();
01352             $db->begin();
01353             $db->query( "DELETE FROM ezorder where id='$orderID'" );
01354             $db->query( "DELETE FROM ezproductcollection where id='$productCollectionID'" );
01355             $db->query( "DELETE FROM ezproductcollection_item where productcollection_id='$productCollectionID'" );
01356             $db->query( "DELETE FROM ezorder_status_history WHERE order_id=$orderNr" );
01357             $db->commit();
01358         }
01359     }
01360 
01361     /*!
01362      \static
01363      Archive an order.
01364      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
01365      the calls within a db transaction; thus within db->begin and db->commit.
01366     */
01367     static function archiveOrder( $orderID )
01368     {
01369         $db = eZDB::instance();
01370         $orderID =(int) $orderID;
01371         $db->query( "UPDATE ezorder SET is_archived='1' WHERE id='$orderID' " );
01372     }
01373 
01374     /*!
01375      \static
01376      Unarchive an order, make it 'normal' again.
01377      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
01378      the calls within a db transaction; thus within db->begin and db->commit.
01379     */
01380     static function unArchiveOrder( $orderID )
01381     {
01382         $db = eZDB::instance();
01383         $orderID =(int) $orderID;
01384         $db->query( "UPDATE ezorder SET is_archived='0' WHERE id='$orderID' " );
01385     }
01386 
01387     /*!
01388      \static
01389      Removes all orders from the database.
01390      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
01391      the calls within a db transaction; thus within db->begin and db->commit.
01392     */
01393     static function cleanup()
01394     {
01395         $db = eZDB::instance();
01396         $rows = $db->arrayQuery( "SELECT productcollection_id FROM ezorder" );
01397 
01398         $db = eZDB::instance();
01399         $db->begin();
01400         if ( count( $rows ) > 0 )
01401         {
01402             $productCollectionIDList = array();
01403             foreach ( $rows as $row )
01404             {
01405                 $productCollectionIDList[] = $row['productcollection_id'];
01406             }
01407             eZProductCollection::cleanupList( $productCollectionIDList );
01408         }
01409         // Who deletes which order in shop should be logged.
01410         eZAudit::writeAudit( 'order-delete', array( 'Comment' => 'Removed all orders from the database: eZOrder::cleanup()' ) );
01411 
01412         eZOrderItem::cleanup();
01413         $db->query( "DELETE FROM ezorder_status_history" );
01414         $db->query( "DELETE FROM ezorder" );
01415         $db->commit();
01416     }
01417 
01418     function orderInfo()
01419     {
01420         $returnArray = array();
01421 
01422         $items = $this->productItems();
01423         foreach ( $items as $item )
01424         {
01425             $totalPriceExVat = $item['total_price_ex_vat'];
01426             $totalPriceIncVat = $item['total_price_inc_vat'];
01427             $totalPriceVat = $totalPriceIncVat - $totalPriceExVat;
01428             $vatValue = $item['vat_value'];
01429             if ( !isset( $returnArray['price_info']['items'][$vatValue]['total_price_ex_vat'] ) )
01430             {
01431                 $returnArray['price_info']['items'][$vatValue]['total_price_ex_vat'] = $totalPriceExVat;
01432                 $returnArray['price_info']['items'][$vatValue]['total_price_inc_vat'] = $totalPriceIncVat;
01433                 $returnArray['price_info']['items'][$vatValue]['total_price_vat'] = $totalPriceVat;
01434             }
01435             else
01436             {
01437                 $returnArray['price_info']['items'][$vatValue]['total_price_ex_vat'] += $totalPriceExVat;
01438                 $returnArray['price_info']['items'][$vatValue]['total_price_inc_vat'] += $totalPriceIncVat;
01439                 $returnArray['price_info']['items'][$vatValue]['total_price_vat'] += $totalPriceVat;
01440             }
01441 
01442             if ( !isset( $returnArray['total_price_info']['total_price_ex_vat']  ) )
01443             {
01444                 $returnArray['total_price_info']['total_price_ex_vat'] = $totalPriceExVat;
01445                 $returnArray['total_price_info']['total_price_inc_vat'] = $totalPriceIncVat;
01446                 $returnArray['total_price_info']['total_price_vat'] = $totalPriceVat;
01447             }
01448             else
01449             {
01450                 $returnArray['total_price_info']['total_price_ex_vat'] += $totalPriceExVat;
01451                 $returnArray['total_price_info']['total_price_inc_vat'] += $totalPriceIncVat;
01452                 $returnArray['total_price_info']['total_price_vat'] += $totalPriceVat;
01453             }
01454         }
01455 
01456         $orderItems = $this->orderItems();
01457         if ( count( $orderItems ) > 0 )
01458         {
01459             foreach ( $orderItems as $orderItem )
01460             {
01461                 $totalPriceExVat  = $orderItem->attribute( 'price_ex_vat' );
01462                 $totalPriceIncVat = $orderItem->attribute( 'price_inc_vat' );
01463                 $totalPriceVat    = $totalPriceIncVat - $totalPriceExVat;
01464                 $vatValue         = $orderItem->attribute( 'vat_value' );
01465                 $type             = $orderItem->attribute( 'type' );
01466 
01467                 if ( !isset( $returnArray['price_info']['items'][$vatValue]['total_price_ex_vat'] ) )
01468                 {
01469                     $returnArray['price_info']['items'][$vatValue]['total_price_ex_vat'] = $totalPriceExVat;
01470                     $returnArray['price_info']['items'][$vatValue]['total_price_inc_vat'] = $totalPriceIncVat;
01471                     $returnArray['price_info']['items'][$vatValue]['total_price_vat'] = $totalPriceVat;
01472                 }
01473                 else
01474                 {
01475                     $returnArray['price_info']['items'][$vatValue]['total_price_ex_vat'] += $totalPriceExVat;
01476                     $returnArray['price_info']['items'][$vatValue]['total_price_inc_vat'] += $totalPriceIncVat;
01477                     $returnArray['price_info']['items'][$vatValue]['total_price_vat'] += $totalPriceVat;
01478                 }
01479 
01480                 if ( !isset( $returnArray['total_price_info']['total_price_ex_vat']  ) )
01481                 {
01482                     $returnArray['total_price_info']['total_price_ex_vat'] = $totalPriceExVat;
01483                     $returnArray['total_price_info']['total_price_inc_vat'] = $totalPriceIncVat;
01484                     $returnArray['total_price_info']['total_price_vat'] = $totalPriceVat;
01485                 }
01486                 else
01487                 {
01488                     $returnArray['total_price_info']['total_price_ex_vat'] += $totalPriceExVat;
01489                     $returnArray['total_price_info']['total_price_inc_vat'] += $totalPriceIncVat;
01490                     $returnArray['total_price_info']['total_price_vat'] += $totalPriceVat;
01491                 }
01492 
01493                 if ( !isset( $returnArray['additional_info'][$type]['items'][$vatValue]['total_price_ex_vat'] ) )
01494                 {
01495                     $returnArray['additional_info'][$type]['items'][$vatValue]['total_price_ex_vat'] = $totalPriceExVat;
01496                     $returnArray['additional_info'][$type]['items'][$vatValue]['total_price_inc_vat'] = $totalPriceIncVat;
01497                     $returnArray['additional_info'][$type]['items'][$vatValue]['total_price_vat'] = $totalPriceVat;
01498                 }
01499                 else
01500                 {
01501                     $returnArray['additional_info'][$type]['items'][$vatValue]['total_price_ex_vat'] += $totalPriceExVat;
01502                     $returnArray['additional_info'][$type]['items'][$vatValue]['total_price_inc_vat'] += $totalPriceIncVat;
01503                     $returnArray['additional_info'][$type]['items'][$vatValue]['total_price_vat'] += $totalPriceVat;
01504                 }
01505 
01506                 if ( !isset( $returnArray['additional_info'][$type]['total']['total_price_ex_vat'] ) )
01507                 {
01508                     $returnArray['additional_info'][$type]['total']['total_price_ex_vat'] = $totalPriceExVat;
01509                     $returnArray['additional_info'][$type]['total']['total_price_inc_vat'] = $totalPriceIncVat;
01510                     $returnArray['additional_info'][$type]['total']['total_price_vat'] = $totalPriceVat;
01511                 }
01512                 else
01513                 {
01514                     $returnArray['additional_info'][$type]['total']['total_price_ex_vat'] += $totalPriceExVat;
01515                     $returnArray['additional_info'][$type]['total']['total_price_inc_vat'] += $totalPriceIncVat;
01516                     $returnArray['additional_info'][$type]['total']['total_price_vat'] += $totalPriceVat;
01517                 }
01518 
01519             }
01520 
01521             // sort the array based on VAT.
01522             foreach ( array_keys( $returnArray['additional_info'] ) as $type )
01523             {
01524                 ksort( $returnArray['additional_info'][$type]['items'] );
01525             }
01526         }
01527         else
01528         {
01529             $returnArray['additional_info'] = array();
01530         }
01531 
01532         ksort( $returnArray['price_info']['items'] );
01533 
01534         return $returnArray;
01535     }
01536 
01537     /*!
01538      \return the used currency code for the order. Will return the locale currency if
01539              only a emty string is found from the order.
01540      \param $collection may contain:
01541             - A string with the currency code.
01542             - An object of the class eZProductCollection.
01543             - false, to fetch the current productCollection from the order.
01544     */
01545     function currencyCode( $collection = false )
01546     {
01547         if ( is_string( $collection ) and $collection != '' )
01548         {
01549             $currencyCode = $collection;
01550         }
01551         else
01552         {
01553             if ( ( $collection instanceof eZProductCollection ) === false )
01554             {
01555                 $collection = eZProductCollection::fetch( $this->attribute( 'productcollection_id' ) );
01556             }
01557             $currencyCode = $collection->attribute( 'currency_code' );
01558         }
01559 
01560          // Backwards compability for orders done with the price datatype.
01561         if ( $currencyCode == '' )
01562         {
01563             $currencyCode = eZOrder::fetchLocaleCurrencyCode();
01564         }
01565         return $currencyCode;
01566     }
01567 
01568     /*!
01569       \static
01570       \returns the local currency code, used by the price datatype.
01571     */
01572     static function fetchLocaleCurrencyCode()
01573     {
01574         $locale = eZLocale::instance();
01575         $currencyCode = $locale->currencyShortName();
01576         return $currencyCode;
01577     }
01578 
01579     /// \privatesection
01580     /// The cached status object or \c null if not cached yet.
01581     public $Status;
01582 
01583 }
01584 
01585 ?>