[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/shop/ -> ezshopoperationcollection.php (source)

   1  <?php
   2  //
   3  // Definition of eZShopOperationCollection class
   4  //
   5  // Created on: <01-Nov-2002 13:51:17 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*! \file ezcontentoperationcollection.php
  30  */
  31  
  32  /*!
  33    \class eZShopOperationCollection ezcontentoperationcollection.php
  34    \brief The class eZShopOperationCollection does
  35  
  36  */
  37  
  38  class eZShopOperationCollection
  39  {
  40      /*!
  41       Constructor
  42      */
  43      function eZShopOperationCollection()
  44      {
  45      }
  46  
  47      function fetchOrder( $orderID )
  48      {
  49          return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
  50      }
  51  
  52      /*!
  53       Operation entry: Extracts user country from order account info and recalculates VAT with country given.
  54       */
  55      function handleUserCountry( $orderID )
  56      {
  57          // If user country is not required to calculate VAT then do nothing.
  58          require_once ( 'kernel/classes/ezvatmanager.php' );
  59          if ( !eZVATManager::isDynamicVatChargingEnabled() || !eZVATManager::isUserCountryRequired() )
  60              return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
  61  
  62          $user =& eZUser::currentUser();
  63  
  64          // Get order's account information and extract user country from it.
  65          $order = eZOrder::fetch( $orderID );
  66  
  67          if ( !$order )
  68          {
  69              eZDebug::writeError( "No such order: $orderID" );
  70              return array( 'status' => EZ_MODULE_OPERATION_CANCELED );
  71          }
  72  
  73          if ( $user->attribute( 'is_logged_in' ) )
  74              $userCountry = eZVATManager::getUserCountry( $user, false );
  75  
  76          $acctInfo = $order->attribute( 'account_information' );
  77          if ( isset( $acctInfo['country'] ) )
  78          {
  79              $country = $acctInfo['country'];
  80  
  81              // If user is registered and logged in
  82              // and country is not yet specified for the user
  83              // then save entered country to the user information.
  84              if ( !isset( $userCountry ) || !$userCountry )
  85                  eZVATManager::setUserCountry( $user, $country );
  86          }
  87          elseif ( isset( $userCountry ) && $userCountry )
  88          {
  89              // If country is not set in shop account handler, we get it from logged user's information.
  90              $country = $userCountry;
  91          }
  92          else
  93          {
  94              //eZDebug::writeError( $acctInfo, "User country was not found in the following account information" );
  95  
  96              $header = ezi18n( 'kernel/shop', 'Error checking out' );
  97              $msg = ezi18n( 'kernel/shop',
  98                             'Unable to calculate VAT percentage because your country is unknown. ' .
  99                             'You can either fill country manually in your account information (if you are a registered user) ' .
 100                             'or contact site administrator.' );
 101  
 102              include_once ( "kernel/common/template.php" );
 103              $tpl =& templateInit();
 104              $tpl->setVariable( "error_header",  $header );
 105              $tpl->setVariable( "error_list", array( $msg ) );
 106  
 107              $operationResult = array(
 108                  'status' => EZ_MODULE_OPERATION_CANCELED,
 109                  'result' => array( 'content' => $tpl->fetch( "design:shop/cancelconfirmorder.tpl" ) )
 110                  );
 111              return $operationResult;
 112          }
 113  
 114          // Recalculate VAT for order's product collection items
 115          // according to the specified user country.
 116  
 117          $productCollection =& $order->attribute( 'productcollection' );
 118          if ( !$productCollection )
 119          {
 120              eZDebug::writeError( "Cannot find product collection for order " . $order->attribute( 'id' ),
 121                                     "ezshopoperationcollection::handleUserCountry" );
 122              return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
 123          }
 124  
 125          require_once ( 'kernel/classes/ezproductcollectionitem.php' );
 126          $items = eZProductCollectionItem::fetchList( array( 'productcollection_id' => $productCollection->attribute( 'id' ) ) );
 127          $vatIsKnown = true;
 128          $db =& eZDB::instance();
 129          $db->begin();
 130          include_once ( 'kernel/shop/classes/ezshopfunctions.php' );
 131          foreach( array_keys( $items ) as $key )
 132          {
 133              $item =& $items[$key];
 134              $productContentObject =& $item->attribute( 'contentobject' );
 135  
 136              // Look up price object.
 137              $priceObj = null;
 138              $attributes =&  $productContentObject->contentObjectAttributes();
 139              foreach ( $attributes as $attribute )
 140              {
 141                  $dataType = $attribute->dataType();
 142                  if ( eZShopFunctions::isProductDatatype( $dataType->isA() ) )
 143                  {
 144                      $priceObj =& $attribute->content();
 145                      break;
 146                  }
 147              }
 148              if ( !is_object( $priceObj ) )
 149                  continue;
 150  
 151              // If the product is assigned a fixed VAT type then skip the product.
 152              $vatType = $priceObj->VATType();
 153              if ( !$vatType->attribute( 'is_dynamic' ) )
 154                  continue;
 155  
 156              // Update item's VAT percentage.
 157              $vatValue = $priceObj->VATPercent( $productContentObject, $country );
 158              eZDebug::writeNotice( "Updating product item collection item ('" .
 159                                    $productContentObject->attribute( 'name' ) . "'): " .
 160                                    "setting VAT $vatValue% according to order's country '$country'." );
 161              $item->setAttribute( "vat_value", $vatValue );
 162  
 163              $item->store();
 164          }
 165          $db->commit();
 166  
 167          return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
 168      }
 169  
 170      /*!
 171       Operation entry: Adds order item: shipping.
 172       \params $orderID contains the order id for the shipping handler.
 173  
 174       The function handleShipping() are runned in the process of confirmorder and
 175       is the final function for creating an order_item in the order confirmation.
 176  
 177       An example for an array that should be returned by the function
 178       eZShippingManager::getShippingInfo( $productCollectionID ):
 179       \code
 180       array( 'shipping_items' => array( array( 'description' => 'Shipping vat: 12%',
 181                                                'cost'        => 50.25,
 182                                                'vat_value'   => 12,
 183                                                'is_vat_inc'  => 0 ),
 184                                         array( 'description' => 'Shipping vat: 25%',
 185                                                'cost'        => 100.75,
 186                                                'vat_value'   => 25,
 187                                                'is_vat_inc'  => 0 ) ),
 188              'description' => 'Total Shipping',
 189              'cost'        => 182.22,
 190              'vat_value'   => false,
 191              'is_vat_inc'  => 1 );
 192       \endcode
 193  
 194       An example for the shippingvalues with only one shippingitem, old standard.
 195       \code
 196       array( 'description' => 'Total Shipping vat: 16%',
 197              'cost'        => 10.25,
 198              'vat_value'   => 16,
 199              'is_vat_inc'  => 1 );
 200       \endcode
 201  
 202       The returned array for each shipping item should consist of these keys:
 203       - order_id - The order id for the current order.
 204       - description - An own description of the shipping item.
 205       - cost - A float value of the cost for the shipping.
 206       - vat_value - The vat value that should be added to the shipping item.
 207       - is_vat_inc - Either 0, 1 or false. 0: The cost is excluded VAT.
 208                                            1: the cost is included VAT.
 209                                        false: The cost is combined by several other VAT prices.
 210  
 211       This function may also send additional parameters to be used in other templates, like
 212       in the basket.
 213      */
 214      function handleShipping( $orderID )
 215      {
 216          do // we prevent high nesting levels by using breaks
 217          {
 218              $order = eZOrder::fetch( $orderID );
 219              if ( !$order )
 220                  break;
 221              $productCollectionID =& $order->attribute( 'productcollection_id' );
 222  
 223              require_once ( 'kernel/classes/ezshippingmanager.php' );
 224              $shippingInfo = eZShippingManager::getShippingInfo( $productCollectionID );
 225              if ( !isset( $shippingInfo ) )
 226                  break;
 227  
 228              // check if the order item has been added before.
 229              $orderItems = $order->orderItemsByType( 'ezcustomshipping' );
 230  
 231              // If orderitems allready exists, remove them first.
 232              if ( $orderItems )
 233              {
 234                  foreach ( $orderItems as $orderItem )
 235                  {
 236                      $orderItem->remove();
 237                  }
 238                  $purgeStatus = eZShippingManager::purgeShippingInfo( $productCollectionID );
 239              }
 240  
 241              if ( isset( $shippingInfo['shipping_items'] ) and
 242                   is_array( $shippingInfo['shipping_items'] ) )
 243              {
 244                  // Add a new order item for each shipping.
 245                  foreach ( $shippingInfo['shipping_items'] as $orderItemShippingInfo )
 246                  {
 247                      $orderItem = new eZOrderItem( array( 'order_id' => $orderID,
 248                                                           'description' => $orderItemShippingInfo['description'],
 249                                                           'price' => $orderItemShippingInfo['cost'],
 250                                                           'vat_value' => $orderItemShippingInfo['vat_value'],
 251                                                           'is_vat_inc' => $orderItemShippingInfo['is_vat_inc'],
 252                                                           'type' => 'ezcustomshipping' ) );
 253                      $orderItem->store();
 254                  }
 255              }
 256              else
 257              {
 258                  // Made for backwards compability, if the array order_items are not supplied.
 259                  $orderItem = new eZOrderItem( array( 'order_id' => $orderID,
 260                                                       'description' => $shippingInfo['description'],
 261                                                       'price' => $shippingInfo['cost'],
 262                                                       'vat' => $shippingInfo['vat_value'],
 263                                                       'is_vat_inc' => $shippingInfo['is_vat_inc'],
 264                                                       'type' => 'ezcustomshipping' ) );
 265                  $orderItem->store();
 266              }
 267  
 268          } while ( false );
 269  
 270          return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
 271      }
 272  
 273      /*!
 274       Operation entry: Updates shipping info for items in the current basket.
 275      */
 276      function updateShippingInfo( $objectID, $optionList )
 277      {
 278          $basket =& eZBasket::currentBasket();
 279          require_once ( 'kernel/classes/ezshippingmanager.php' );
 280          $shippingInfo = eZShippingManager::updateShippingInfo( $basket->attribute( 'productcollection_id' ) );
 281          return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
 282      }
 283  
 284      function updateBasket( $itemCountList, $itemIDList )
 285      {
 286          if ( is_array( $itemCountList ) && is_array( $itemIDList ) && count( $itemCountList ) == count ( $itemIDList ) )
 287          {
 288              $basket =& eZBasket::currentBasket();
 289              if ( is_object( $basket ) )
 290              {
 291                  $productCollectionID =& $basket->attribute( 'productcollection_id' );
 292  
 293                  $i = 0;
 294                  foreach ( $itemIDList as $id )
 295                  {
 296                      $item = eZProductCollectionItem::fetch( $id );
 297                      if ( is_object( $item ) && $item->attribute( 'productcollection_id' ) == $productCollectionID )
 298                      {
 299                          if ( is_numeric( $itemCountList[$i] ) and $itemCountList[$i] == 0 )
 300                          {
 301                              $item->remove();
 302                          }
 303                          else
 304                          {
 305                              $item->setAttribute( 'item_count', $itemCountList[$i] );
 306                              $item->store();
 307                          }
 308                      }
 309                      ++$i;
 310                  }
 311              }
 312          }
 313      }
 314  
 315      /*!
 316       Operation entry: Adds the object \a $objectID with options \a $optionList to the current basket.
 317      */
 318      function addToBasket( $objectID, $optionList )
 319      {
 320          include_once ( 'kernel/shop/classes/ezshopfunctions.php' );
 321  
 322          $object = eZContentObject::fetch( $objectID );
 323          $nodeID = $object->attribute( 'main_node_id' );
 324          $price = 0.0;
 325          $isVATIncluded = true;
 326          $attributes = $object->contentObjectAttributes();
 327  
 328          $priceFound = false;
 329  
 330          foreach ( $attributes as $attribute )
 331          {
 332              $dataType = $attribute->dataType();
 333              if ( eZShopFunctions::isProductDatatype( $dataType->isA() ) )
 334              {
 335                  $priceObj =& $attribute->content();
 336                  $price += $priceObj->attribute( 'price' );
 337                  $priceFound = true;
 338              }
 339          }
 340  
 341          if ( !$priceFound )
 342          {
 343              eZDebug::writeError( 'Attempted to add object without price to basket.' );
 344              return array( 'status' => EZ_MODULE_OPERATION_CANCELED );
 345          }
 346  
 347          $currency = $priceObj->attribute( 'currency' );
 348  
 349          // Check for 'option sets' in option list.
 350          // If found each 'option set' will be added as a separate product purchase.
 351          $hasOptionSet = false;
 352          foreach ( array_keys( $optionList ) as $optionKey )
 353          {
 354              if ( substr( $optionKey, 0, 4 ) == 'set_' )
 355              {
 356                  $returnStatus = eZShopOperationCollection::addToBasket( $objectID, $optionList[$optionKey] );
 357                  // If adding one 'option set' fails we should stop immediately
 358                  if ( $returnStatus['status'] == EZ_MODULE_OPERATION_CANCELED )
 359                      return $returnStatus;
 360                  $hasOptionSet = true;
 361              }
 362          }
 363          if ( $hasOptionSet )
 364              return $returnStatus;
 365  
 366          $basket =& eZBasket::currentBasket();
 367  
 368          /* Check if the item with the same options is not already in the basket: */
 369          $itemID = false;
 370          $collection =& $basket->attribute( 'productcollection' );
 371          if ( !$collection )
 372          {
 373              eZDebug::writeError( 'Unable to find product collection.' );
 374              return array( 'status' => EZ_MODULE_OPERATION_CANCELED );
 375          }
 376          else
 377          {
 378              $collection->setAttribute( 'currency_code', $currency );
 379              $collection->store();
 380  
 381              $count = 0;
 382              /* Calculate number of options passed via the HTTP variable: */
 383              foreach ( array_keys( $optionList ) as $key )
 384              {
 385                  if ( is_array( $optionList[$key] ) )
 386                      $count += count( $optionList[$key] );
 387                  else
 388                      $count++;
 389              }
 390              $collectionItems =& $collection->itemList( false );
 391              foreach ( $collectionItems as $item )
 392              {
 393                  /* For all items in the basket which have the same object_id: */
 394                  if ( $item['contentobject_id'] == $objectID )
 395                  {
 396                      $options = eZProductCollectionItemOption::fetchList( $item['id'], false );
 397                      /* If the number of option for this item is not the same as in the HTTP variable: */
 398                      if ( count( $options ) != $count )
 399                      {
 400                          break;
 401                      }
 402                      $theSame = true;
 403                      foreach ( $options as $option )
 404                      {
 405                          /* If any option differs, go away: */
 406                          if ( ( is_array( $optionList[$option['object_attribute_id']] ) &&
 407                                 !in_array( $option['option_item_id'], $optionList[$option['object_attribute_id']] ) )
 408                               || ( !is_array( $optionList[$option['object_attribute_id']] ) &&
 409                                    $option['option_item_id'] != $optionList[$option['object_attribute_id']] ) )
 410                          {
 411                              $theSame = false;
 412                              break;
 413                          }
 414                      }
 415                      if ( $theSame )
 416                      {
 417                          $itemID = $item['id'];
 418                          break;
 419                      }
 420                  }
 421              }
 422  
 423              if ( $itemID )
 424              {
 425                  /* If found in the basket, just increment number of that items: */
 426                  $item = eZProductCollectionItem::fetch( $itemID );
 427                  $item->setAttribute( 'item_count', 1 + $item->attribute( 'item_count' ) );
 428                  $item->store();
 429              }
 430              else
 431              {
 432                  $item = eZProductCollectionItem::create( $basket->attribute( "productcollection_id" ) );
 433  
 434                  $item->setAttribute( 'name', $object->attribute( 'name' ) );
 435                  $item->setAttribute( "contentobject_id", $objectID );
 436                  $item->setAttribute( "item_count", 1 );
 437                  $item->setAttribute( "price", $price );
 438                  if ( $priceObj->attribute( 'is_vat_included' ) )
 439                  {
 440                      $item->setAttribute( "is_vat_inc", '1' );
 441                  }
 442                  else
 443                  {
 444                      $item->setAttribute( "is_vat_inc", '0' );
 445                  }
 446                  $item->setAttribute( "vat_value", $priceObj->attribute( 'vat_percent' ) );
 447                  $item->setAttribute( "discount", $priceObj->attribute( 'discount_percent' ) );
 448                  $item->store();
 449                  $priceWithoutOptions = $price;
 450  
 451                  $optionIDList = array();
 452                  foreach ( array_keys( $optionList ) as $key )
 453                  {
 454                      $attributeID = $key;
 455                      $optionString = $optionList[$key];
 456                      if ( is_array( $optionString ) )
 457                      {
 458                          foreach ( $optionString as $optionID )
 459                          {
 460                              $optionIDList[] = array( 'attribute_id' => $attributeID,
 461                                                       'option_string' => $optionID );
 462                          }
 463                      }
 464                      else
 465                      {
 466                          $optionIDList[] = array( 'attribute_id' => $attributeID,
 467                                                   'option_string' => $optionString );
 468                      }
 469                  }
 470  
 471                  $db =& eZDB::instance();
 472                  $db->begin();
 473                  foreach ( $optionIDList as $optionIDItem )
 474                  {
 475                      $attributeID = $optionIDItem['attribute_id'];
 476                      $optionString = $optionIDItem['option_string'];
 477  
 478                      $attribute = eZContentObjectAttribute::fetch( $attributeID, $object->attribute( 'current_version' ) );
 479                      $dataType = $attribute->dataType();
 480                      $optionData = $dataType->productOptionInformation( $attribute, $optionString, $item );
 481                      if ( $optionData )
 482                      {
 483                          $optionData['additional_price'] = eZShopFunctions::convertAdditionalPrice( $currency, $optionData['additional_price'] );
 484                          $optionItem = eZProductCollectionItemOption::create( $item->attribute( 'id' ), $optionData['id'], $optionData['name'],
 485                                                                               $optionData['value'], $optionData['additional_price'], $attributeID );
 486                          $optionItem->store();
 487                          $price += $optionData['additional_price'];
 488                      }
 489                  }
 490  
 491                  if ( $price != $priceWithoutOptions )
 492                  {
 493                      $item->setAttribute( "price", $price );
 494                      $item->store();
 495                  }
 496                  $db->commit();
 497              }
 498          }
 499  
 500          return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
 501      }
 502  
 503      function activateOrder( $orderID )
 504      {
 505          include_once ( "kernel/classes/ezbasket.php" );
 506          include_once ( 'kernel/classes/ezorder.php' );
 507  
 508          $order = eZOrder::fetch( $orderID );
 509  
 510          $db =& eZDB::instance();
 511          $db->begin();
 512          $order->activate();
 513  
 514          $basket =& eZBasket::currentBasket( true, $orderID);
 515          $basket->remove();
 516          $db->commit();
 517  
 518          include_once ( "lib/ezutils/classes/ezhttptool.php" );
 519          eZHTTPTool::setSessionVariable( "UserOrderID", $orderID );
 520          return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
 521      }
 522  
 523      function sendOrderEmails( $orderID )
 524      {
 525          include_once ( 'kernel/classes/ezorder.php' );
 526          $order = eZOrder::fetch( $orderID );
 527  
 528          // Fetch the shop account handler
 529          include_once ( 'kernel/classes/ezshopaccounthandler.php' );
 530  
 531          $accountHandler =& eZShopAccountHandler::instance();
 532          $email = $accountHandler->email( $order );
 533  
 534          // Fetch the confirm order handler
 535          include_once ( 'kernel/classes/ezconfirmorderhandler.php' );
 536          $confirmOrderHandler =& eZConfirmOrderHandler::instance();
 537          $params = array( 'email' => $email,
 538                           'order' => $order );
 539          $confirmOrderStatus = $confirmOrderHandler->execute( $params );
 540  
 541          return array( 'status' => EZ_MODULE_OPERATION_CONTINUE );
 542      }
 543  }
 544  
 545  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7