Beispiel #1
0
 /**
  * Validates the address with the AvaTax validation API.
  * Returns true on success and an array with an error on failure.
  *
  * @return array|bool
  * @throws OnePica_AvaTax_Model_Avatax_Address_Exception
  */
 public function validate()
 {
     if (!$this->_mageAddress) {
         throw new OnePica_AvaTax_Model_Avatax_Address_Exception($this->__('An address must be set before validation.'));
     }
     /** @var Mage_Sales_Model_Quote $quote */
     $quote = $this->_mageAddress->getQuote();
     $isAddressValidationOn = $this->_getDataHelper()->isAddressValidationOn($this->_mageAddress, $this->_storeId);
     $isAddressNormalizationOn = $this->_getDataHelper()->isAddressNormalizationOn($this->_mageAddress, $this->_storeId);
     $isAddressActionable = $this->_getDataHelper()->isAddressActionable($this->_mageAddress, $quote->getStoreId());
     //if there is no use cases for AvaTax services, return address as valid without doing a lookup
     if (!$isAddressValidationOn && !$isAddressNormalizationOn && !$isAddressActionable) {
         return true;
     }
     //lookup in AvaTax (with caching)
     $key = $this->_mageAddress->getCacheHashKey();
     if (array_key_exists($key, $this->_cache)) {
         $result = unserialize($this->_cache[$key]);
     } elseif ($this->_mageAddress->getPostcode() && $this->_mageAddress->getPostcode() != '-') {
         $checkFieldsResult = $this->_checkFields();
         if ($checkFieldsResult) {
             return $checkFieldsResult;
         }
         $result = $this->_sendAddressValidationRequest();
         $this->_cache[$key] = serialize($result);
     } else {
         $errors = array();
         $errors[] = $this->__('Invalid ZIP/Postal Code.');
         return $errors;
     }
     $this->_addressNormalization($isAddressNormalizationOn, $result);
     $addressValidationResult = $this->_addressValidation($isAddressValidationOn, $isAddressActionable, $result);
     if ($addressValidationResult) {
         return $addressValidationResult;
     }
     return true;
 }
Beispiel #2
0
 /**
  * Get Weee amounts associated with a product
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Mage_Customer_Model_Address_Abstract $shipping
  * @param Mage_Customer_Model_Address_Abstract $billing
  * @param mixed $website
  * @param boolean $calculateTax
  * @param boolean $ignoreDiscount
  * @return array|\Varien_Object
  */
 public function getProductWeeeAttributes($product, $shipping = null, $billing = null, $website = null, $calculateTax = null, $ignoreDiscount = false)
 {
     $result = array();
     $allWeee = $this->getWeeeTaxAttributeCodes();
     if (!$allWeee) {
         return $result;
     }
     $websiteId = Mage::app()->getWebsite($website)->getId();
     $store = Mage::app()->getWebsite($website)->getDefaultGroup()->getDefaultStore();
     $customer = null;
     if ($shipping) {
         $customerTaxClass = $shipping->getQuote()->getCustomerTaxClassId();
         $customer = $shipping->getQuote()->getCustomer();
     } else {
         $customerTaxClass = null;
     }
     $calculator = Mage::getModel('tax/calculation');
     if ($customer) {
         $calculator->setCustomer($customer);
     }
     $rateRequest = $calculator->getRateRequest($shipping, $billing, $customerTaxClass, $store);
     $currentPercent = $product->getTaxPercent();
     if (!$currentPercent) {
         $currentPercent = Mage::getSingleton('tax/calculation')->getRate($rateRequest->setProductClassId($product->getTaxClassId()));
     }
     $discountPercent = 0;
     if (!$ignoreDiscount && Mage::helper('weee')->isDiscounted($store)) {
         $discountPercent = $this->_getDiscountPercentForProduct($product);
     }
     $productAttributes = $product->getTypeInstance(true)->getSetAttributes($product);
     foreach ($productAttributes as $code => $attribute) {
         if (in_array($code, $allWeee)) {
             $attributeSelect = $this->getResource()->getReadConnection()->select();
             $attributeSelect->from($this->getResource()->getTable('weee/tax'), 'value')->where('attribute_id = ?', (int) $attribute->getId())->where('website_id IN(?)', array($websiteId, 0))->where('country = ?', $rateRequest->getCountryId())->where('state IN(?)', array($rateRequest->getRegionId(), '*'))->where('entity_id = ?', (int) $product->getId())->limit(1);
             $order = array('state ' . Varien_Db_Select::SQL_DESC, 'website_id ' . Varien_Db_Select::SQL_DESC);
             $attributeSelect->order($order);
             $value = $this->getResource()->getReadConnection()->fetchOne($attributeSelect);
             if ($value) {
                 if ($discountPercent) {
                     $value = Mage::app()->getStore()->roundPrice($value - $value * $discountPercent / 100);
                 }
                 $taxAmount = 0;
                 $amount = $value;
                 if ($calculateTax && Mage::helper('weee')->isTaxable($store)) {
                     if ($this->_taxHelper->isCrossBorderTradeEnabled($store)) {
                         $defaultPercent = $currentPercent;
                     } else {
                         $defaultRateRequest = $calculator->getDefaultRateRequest($store);
                         $defaultPercent = Mage::getModel('tax/calculation')->getRate($defaultRateRequest->setProductClassId($product->getTaxClassId()));
                     }
                     if (Mage::helper('weee')->isTaxIncluded($store)) {
                         $taxAmount = Mage::app()->getStore()->roundPrice($value / (100 + $defaultPercent) * $currentPercent);
                         $amount = $amount - $taxAmount;
                     } else {
                         $appliedRates = Mage::getModel('tax/calculation')->getAppliedRates($rateRequest);
                         if (count($appliedRates) > 1) {
                             $taxAmount = 0;
                             foreach ($appliedRates as $appliedRate) {
                                 $taxRate = $appliedRate['percent'];
                                 $taxAmount += Mage::app()->getStore()->roundPrice($value * $taxRate / 100);
                             }
                         } else {
                             $taxAmount = Mage::app()->getStore()->roundPrice($value * $currentPercent / 100);
                         }
                     }
                 }
                 $one = new Varien_Object();
                 $one->setName(Mage::helper('catalog')->__($attribute->getFrontend()->getLabel()))->setAmount($amount)->setTaxAmount($taxAmount)->setCode($attribute->getAttributeCode());
                 $result[] = $one;
             }
         }
     }
     return $result;
 }
Beispiel #3
0
 /**
  * Validates the address with the AvaTax validation API.
  * Returns true on success and an array with an error on failure.
  *
  * @return array|bool
  * @throws OnePica_AvaTax_Model_Avatax_Address_Exception
  */
 public function validate()
 {
     if (!$this->_mageAddress) {
         throw new OnePica_AvaTax_Model_Avatax_Address_Exception($this->__('An address must be set before validation.'));
     }
     /** @var OnePica_AvaTax_Model_Config $config */
     $config = Mage::getSingleton('avatax/config')->init($this->_storeId);
     /** @var Mage_Sales_Model_Quote $quote */
     $quote = $this->_mageAddress->getQuote();
     $isAddressValidationOn = $this->_getDataHelper()->isAddressValidationOn($this->_mageAddress, $this->_storeId);
     $isAddressNormalizationOn = $this->_getDataHelper()->isAddressNormalizationOn($this->_mageAddress, $this->_storeId);
     $isAddressActionable = $this->_getDataHelper()->isAddressActionable($this->_mageAddress, $quote->getStoreId());
     //if there is no use cases for AvaTax services, return address as valid without doing a lookup
     if (!$isAddressValidationOn && !$isAddressNormalizationOn && !$isAddressActionable) {
         return true;
     }
     //lookup in AvaTax (with caching)
     $key = $this->_mageAddress->getCacheHashKey();
     if (array_key_exists($key, $this->_cache)) {
         $result = unserialize($this->_cache[$key]);
     } elseif ($this->_mageAddress->getPostcode() && $this->_mageAddress->getPostcode() != '-') {
         $session = Mage::getSingleton('checkout/session');
         if ($session->getPostType() == 'onepage') {
             $requiredFields = explode(",", $this->getHelper()->getFieldRequiredList());
             $fieldRules = explode(",", $this->getHelper()->getFieldRule());
             foreach ($requiredFields as $field) {
                 $requiredFlag = 0;
                 foreach ($fieldRules as $rule) {
                     if (preg_match("/street\\d/", $field)) {
                         $field = "street";
                     }
                     if ($field == "country") {
                         $field = "country_id";
                     }
                     if ($this->_mageAddress->getData($field) == $rule || !$this->_mageAddress->getData($field)) {
                         $requiredFlag = 1;
                     }
                 }
                 if ($requiredFlag) {
                     $errors = array();
                     $errors[] = $this->__('Invalid ') . $this->__($field);
                     return $errors;
                 }
             }
         }
         $client = $config->getAddressConnection();
         $request = new ValidateRequest($this->_requestAddress, TextCase::$Mixed, 0);
         $request->setTaxability(true);
         $result = $client->Validate($request);
         $this->_log(OnePica_AvaTax_Model_Source_Logtype::VALIDATE, $request, $result, $this->_storeId, $config->getParams());
         $this->_cache[$key] = serialize($result);
     } else {
         $errors = array();
         $errors[] = $this->__('Invalid ZIP/Postal Code.');
         return $errors;
     }
     //normalization
     if ($isAddressNormalizationOn && $result->getResultCode() == SeverityLevel::$Success) {
         $responseAddress = $result->getValidAddresses();
         $responseAddress = array_pop($responseAddress);
         if ($responseAddress instanceof ValidAddress) {
             $this->_responseAddress = $responseAddress;
             $this->_convertResponseAddress();
         } else {
             throw new OnePica_AvaTax_Model_Avatax_Address_Exception($this->__('Invalid response address type.'));
         }
     }
     //validation
     if ($isAddressValidationOn == 1) {
         if ($result->getResultCode() == SeverityLevel::$Success) {
             $this->_mageAddress->setAddressValidated(true);
             return true;
         } else {
             $errors = array();
             foreach ($result->getMessages() as $message) {
                 $errors[] = $this->__($message->getSummary());
             }
             return $errors;
         }
     } elseif ($isAddressValidationOn == 2) {
         $this->_mageAddress->setAddressValidated(true);
         if ($result->getResultCode() == SeverityLevel::$Success) {
             return true;
         } else {
             if (!$this->_mageAddress->getAddressNotified()) {
                 $this->_mageAddress->setAddressNotified(true);
                 foreach ($result->getMessages() as $message) {
                     Mage::getSingleton('core/session')->addNotice($this->__($message->getSummary()));
                 }
             }
             return true;
         }
         //a valid address isn't required, but Avalara has to say there is
         //enough info to drill down to a tax jurisdiction to calc on
     } elseif (!$isAddressValidationOn && $isAddressActionable) {
         if ($result->isTaxable()) {
             $this->_mageAddress->setAddressValidated(true);
             return true;
         } else {
             $errors = array();
             foreach ($result->getMessages() as $message) {
                 $errors[] = $this->__($message->getSummary());
             }
             return $errors;
         }
     }
     return true;
 }
 public function setAddress(Mage_Customer_Model_Address_Abstract $address)
 {
     $this->_storeId = $address->getQuote()->getStore()->getId();
     $this->_magentoAddress = $address;
     return $this->_ATSetupAddress();
 }