Exemplo n.º 1
0
 /**
  * Validate vat id if given and assign tax class to quote shipping address
  *
  * @param Varien_Event_Observer $observer
  */
 public function salesQuoteAddressSaveAfter(Varien_Event_Observer $observer)
 {
     /** @var Mage_Sales_Model_Quote_Address $quoteAddress  */
     $quoteAddress = $observer->getQuoteAddress();
     if ($quoteAddress->getAddressType() != 'shipping') {
         return;
     }
     $customer = $quoteAddress->getQuote()->getCustomer();
     if (!Mage::helper('customer/address')->isVatValidationEnabled($customer->getStore()) || Mage::registry(self::VIV_PROCESSED_FLAG)) {
         return;
     }
     try {
         Mage::register(self::VIV_PROCESSED_FLAG, true);
         /** @var $customerHelper Mage_Customer_Helper_Data */
         $customerHelper = Mage::helper('customer');
         if ($quoteAddress->getVatId() == '' || !Mage::helper('core')->isCountryInEU($quoteAddress->getCountry())) {
             $defaultGroupId = $customerHelper->getDefaultCustomerGroupId($customer->getStore());
             if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) {
                 $customerGroup = Mage::getModel('customer/group')->load($customer->getGroupId());
                 $quoteAddress->setTaxClassId($customerGroup->getTaxClassId());
                 $quoteAddress->save();
             }
         } else {
             $result = $customerHelper->checkVatNumber($quoteAddress->getCountryId(), $quoteAddress->getVatId());
             if (!$customer->getDisableAutoGroupChange()) {
                 $customerGroup = Mage::getModel('customer/group')->load($customer->getGroupId());
                 if ($result->getIsValid()) {
                     $quoteAddress->setTaxClassId($customerGroup->getTaxClassIdVatId());
                 } else {
                     $quoteAddress->setTaxClassId($customerGroup->getTaxClassId());
                 }
                 $quoteAddress->save();
             }
             if (!Mage::app()->getStore()->isAdmin()) {
                 $validationMessage = Mage::helper('customer')->getVatValidationUserMessage($quoteAddress, $customer->getDisableAutoGroupChange(), $result);
                 if (!$validationMessage->getIsError()) {
                     Mage::getSingleton('customer/session')->addSuccess($validationMessage->getMessage());
                 } else {
                     Mage::getSingleton('customer/session')->addError($validationMessage->getMessage());
                 }
             }
         }
     } catch (Exception $e) {
         Mage::register(self::VIV_PROCESSED_FLAG, false, true);
     }
 }
Exemplo n.º 2
0
 /**
  * Restore initial customer group ID in quote if needed on collect_totals_after event of quote address
  *
  * @param Varien_Event_Observer $observer
  */
 public function restoreQuoteCustomerGroupId($observer)
 {
     $quoteAddress = $observer->getQuoteAddress();
     $configAddressType = Mage::helper('customer/address')->getTaxCalculationAddressType();
     // Restore initial customer group ID in quote only if VAT is calculated based on shipping address
     if ($quoteAddress->hasPrevQuoteCustomerGroupId() && $configAddressType == Mage_Customer_Model_Address_Abstract::TYPE_SHIPPING) {
         $quoteAddress->getQuote()->setCustomerGroupId($quoteAddress->getPrevQuoteCustomerGroupId());
         $quoteAddress->unsPrevQuoteCustomerGroupId();
     }
 }
 /**
  * B4Requirement
  * Updating the shipping rates with the discount provided by the AS400
  * 
  * There is a special hook that we've had to hack the core and add, in order
  * for this to work. Without the special hook described below we wont be able
  * to adjust the rates before they are used by the rest of the system.
  * 
  * Specially injected code into file
  * C:\wamp\www\b4schools\app\code\core\Mage\Checkout\controllers\OnepageController.php
  * on line 298, just before the $session gets cleared() in the successAction method
  * 
  * // INJECTED FOR B4SCHOOLS
  * Mage::dispatchEvent('checkout_onepage_controller_success_before_action', array('order_ids' => array($lastOrderId))); 
  * // --- END INJECTION
  * 
  * We need to create our own hook event because the session gets cleared and 
  * we need catch the order_id before it's cleared and lost.
  * 
  * @param Varien_Event_Observer $observer
  */
 public function update_shipping_rates(Varien_Event_Observer $observer)
 {
     $address = $observer->getQuoteAddress();
     $rates = $observer->getShippingRates();
     try {
         foreach ($rates as $rate) {
             $price = $rate->getCost();
             if (!$price) {
                 $price = $rate->getPrice();
             }
             $rate->setCost($price);
             $rate->setPrice($price);
             if ($rate->getCarrier() != 'ups') {
                 continue;
             }
             if ($rate->getMethod() != '03') {
                 continue;
             }
             // get the discount from the ERP
             $discount = Mage::helper('idpas400')->getShippingDiscount($address->getData('postcode'), 'UPG', $address->getQuote());
             Mage::getModel('customer/session')->setData('shippingDiscount', $discount);
             Mage::getModel('customer/session')->setData('shippingRate', $price);
             if ($discount === true) {
                 $shippingPrice = 0;
             } else {
                 // adjusting shipping rate
                 $shippingPrice = 0;
                 $shippingPrice = $shippingPrice + $price;
                 $shippingPrice = $shippingPrice - $discount;
                 if ($shippingPrice < 0) {
                     $shippingPrice = 0;
                 }
             }
             $rate->setPrice($shippingPrice);
             $rate->setCost($shippingPrice);
         }
     } catch (Exception $e) {
         Mage::logException($e);
     }
 }