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