Example #1
0
 /**
  * \param $skipDynamic if false, include dynamic VAT type to the list being returned.
  */
 static function fetchList($asObject = true, $skipDynamic = false)
 {
     // Fetch "real" VAT types, stored in DB.
     $VATTypes = eZPersistentObject::fetchObjectList(eZVatType::definition(), null, null, array('id' => false), null, $asObject);
     if (!$VATTypes) {
         $VATTypes = array();
     }
     // Add "fake" VAT type: dynamic.
     if (!$skipDynamic) {
         if (eZVATManager::isDynamicVatChargingEnabled()) {
             $VATTypes[] = eZVatType::dynamicVatType($asObject);
         }
     }
     return $VATTypes;
 }
Example #2
0
 static function getUserCountry()
 {
     return eZVATManager::getUserCountry(false, false);
 }
    static function isDynamicVatChargingEnabled()
    {
        if ( isset( $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] ) )
            return $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'];

        $enabled = is_object( eZVATManager::loadVATHandler() );
        $GLOBALS['eZVATManager_isDynamicVatChargingEnabled'] = $enabled;
        return $enabled;
    }
 function handleUserCountry($orderID)
 {
     // If user country is not required to calculate VAT then do nothing.
     if (!eZVATManager::isDynamicVatChargingEnabled() || !eZVATManager::isUserCountryRequired()) {
         return array('status' => eZModuleOperationInfo::STATUS_CONTINUE);
     }
     $user = eZUser::currentUser();
     // Get order's account information and extract user country from it.
     $order = eZOrder::fetch($orderID);
     if (!$order) {
         eZDebug::writeError("No such order: {$orderID}");
         return array('status' => eZModuleOperationInfo::STATUS_CANCELLED);
     }
     if ($user->attribute('is_logged_in')) {
         $userCountry = eZVATManager::getUserCountry($user, false);
     }
     $acctInfo = $order->attribute('account_information');
     if (isset($acctInfo['country'])) {
         $country = $acctInfo['country'];
         // If user is registered and logged in
         // and country is not yet specified for the user
         // then save entered country to the user information.
         if (!isset($userCountry) || !$userCountry) {
             eZVATManager::setUserCountry($user, $country);
         }
     } elseif (isset($userCountry) && $userCountry) {
         // If country is not set in shop account handler, we get it from logged user's information.
         $country = $userCountry;
     } else {
         $header = ezpI18n::tr('kernel/shop', 'Error checking out');
         $msg = ezpI18n::tr('kernel/shop', 'Unable to calculate VAT percentage because your country is unknown. ' . 'You can either fill country manually in your account information (if you are a registered user) ' . 'or contact site administrator.');
         $tpl = eZTemplate::factory();
         $tpl->setVariable("error_header", $header);
         $tpl->setVariable("error_list", array($msg));
         $operationResult = array('status' => eZModuleOperationInfo::STATUS_CANCELLED, 'result' => array('content' => $tpl->fetch("design:shop/cancelconfirmorder.tpl")));
         return $operationResult;
     }
     // Recalculate VAT for order's product collection items
     // according to the specified user country.
     $productCollection = $order->attribute('productcollection');
     if (!$productCollection) {
         eZDebug::writeError("Cannot find product collection for order " . $order->attribute('id'), "eZShopOperationCollection::handleUserCountry");
         return array('status' => eZModuleOperationInfo::STATUS_CONTINUE);
     }
     $items = eZProductCollectionItem::fetchList(array('productcollection_id' => $productCollection->attribute('id')));
     $vatIsKnown = true;
     $db = eZDB::instance();
     $db->begin();
     foreach ($items as $item) {
         $productContentObject = $item->attribute('contentobject');
         // Look up price object.
         $priceObj = null;
         $attributes = $productContentObject->contentObjectAttributes();
         foreach ($attributes as $attribute) {
             $dataType = $attribute->dataType();
             if (eZShopFunctions::isProductDatatype($dataType->isA())) {
                 $priceObj = $attribute->content();
                 break;
             }
         }
         if (!is_object($priceObj)) {
             continue;
         }
         // If the product is assigned a fixed VAT type then skip the product.
         $vatType = $priceObj->VATType();
         if (!$vatType->attribute('is_dynamic')) {
             continue;
         }
         // Update item's VAT percentage.
         $vatValue = $priceObj->VATPercent($productContentObject, $country);
         eZDebug::writeNotice("Updating product item collection item ('" . $productContentObject->attribute('name') . "'): " . "setting VAT {$vatValue}% according to order's country '{$country}'.");
         $item->setAttribute("vat_value", $vatValue);
         $item->store();
     }
     $db->commit();
     return array('status' => eZModuleOperationInfo::STATUS_CONTINUE);
 }