/** * Get percentage of VAT type corresponding to the given product and country the user is from. * * \return Percentage, or null on error. * \public * \static */ static function getVAT( $object, $country ) { // Load VAT handler. if ( !is_object( $handler = eZVATManager::loadVATHandler() ) ) { if ( $handler === true ) { eZDebug::writeWarning( "No VAT handler specified but dynamic VAT charging is used." ); } return null; } // Check if user country must be specified. $requireUserCountry = eZVATManager::isUserCountryRequired(); // Determine user country if it's not specified if ( $country === false ) $country = eZVATManager::getUserCountry(); if ( !$country && $requireUserCountry ) { eZDebug::writeNotice( "User country is not specified." ); } return $handler->getVatPercent( $object, $country ); }
static function getUserCountry() { return eZVATManager::getUserCountry(false, false); }
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); }