From a5b34312e130e0a0ffc643d75ccbfdc6a15e19b2 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Wed, 5 Feb 2014 12:03:24 +0100 Subject: [PATCH 1/3] Retrieve country data from logged in customer who has no valid quote yet --- .../community/Xonu/FGP/Model/Calculation.php | 83 +++++++++++-------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/app/code/community/Xonu/FGP/Model/Calculation.php b/app/code/community/Xonu/FGP/Model/Calculation.php index 176286f..84aa02c 100644 --- a/app/code/community/Xonu/FGP/Model/Calculation.php +++ b/app/code/community/Xonu/FGP/Model/Calculation.php @@ -7,11 +7,6 @@ class Xonu_FGP_Model_Calculation extends Mage_Tax_Model_Calculation { protected $adminSession = false; - private function log($msg) { - $log = file_get_contents('/log.txt'); - file_put_contents( '/log.txt', $log . "\n" . $msg); - } - // set origin to destination public function getRateOriginRequest($store = null) { @@ -35,9 +30,25 @@ public function getRateOriginRequest($store = null) return $request; } - else{ - return $this->getDefaultDestination(); - } + if (Mage::getSingleton('customer/session')->isLoggedIn()) { + + $customer = Mage::getSingleton('customer/session')->getCustomer(); + if (($billingAddress = $customer->getDefaultBillingAddress()) + && ($shippingAddress = $customer->getDefaultShippingAddress())) { + + // use destination of the existing quote as origin if quote exists + $request = $this->getRateRequest( + $shippingAddress, + $billingAddress, + $customer->getTaxClassId(), + $store + ); + + return $request; + } + } + + return $this->getDefaultDestination(); } else // quote is not available when switching the currency { @@ -86,41 +97,43 @@ public function getRateRequest( $billingAddress = $defBilling; } else if ($basedOn == 'shipping' && $defShipping && $defShipping->getCountryId()) { $shippingAddress = $defShipping; + } else { + if($session->hasQuote() || $this->adminSession) + { + $quote = $session->getQuote(); + $isActive = $quote->getIsActive(); + if($isActive) + { + $shippingAddress = $quote->getShippingAddress(); + $billingAddress = $quote->getBillingAddress(); + } + else{ + $basedOn = 'default'; + } + } + else{ + $basedOn = 'default'; + } } - } + } else { - // if still got no address, try to get it from quote - if (($basedOn == 'billing' && !$billingAddress) || ($basedOn == 'shipping' && !$shippingAddress)) { - if ($session->hasQuote() || $this->adminSession) { + if($session->hasQuote() || $this->adminSession) + { $quote = $session->getQuote(); $isActive = $quote->getIsActive(); - - if ($isActive) { - $quoteBillingAddress = $quote->getBillingAddress(); - $quoteShippingAddress = $quote->getShippingAddress(); - - // check if the addresses have a country - if ($basedOn == 'billing' && $quoteBillingAddress && $quoteBillingAddress->getCountryId()) { - $billingAddress = $quoteBillingAddress; - } else if ($basedOn == 'shipping' && $quoteShippingAddress && $quoteShippingAddress->getCountryId()) { - $shippingAddress = $quoteShippingAddress; - } else { - $basedOn = 'default'; - } - } else { + if($isActive) + { + $shippingAddress = $quote->getShippingAddress(); + $billingAddress = $quote->getBillingAddress(); + } + else { $basedOn = 'default'; } - } else { + } + else { $basedOn = 'default'; } } - - // if we got an address, but it has no country, calculate tax based on default - if (($basedOn == 'billing' && $billingAddress && !$billingAddress->getCountryId()) - || ($basedOn == 'shipping' && $shippingAddress && !$shippingAddress->getCountryId()) - ) { - $basedOn = 'default'; - } } } switch ($basedOn) { @@ -160,8 +173,6 @@ public function getRateRequest( private function getDefaultDestination($store = null) { - $this->log('default dest'); - $address = new Varien_Object(); $request = new Varien_Object(); From c6c1ec0d66f2bfea4b201aa4c4a944c6f846c574 Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Wed, 5 Feb 2014 12:05:24 +0100 Subject: [PATCH 2/3] Adjust to Magento coding standards --- .../community/Xonu/FGP/Model/Calculation.php | 302 +++++++++--------- 1 file changed, 148 insertions(+), 154 deletions(-) diff --git a/app/code/community/Xonu/FGP/Model/Calculation.php b/app/code/community/Xonu/FGP/Model/Calculation.php index 84aa02c..f6b9448 100644 --- a/app/code/community/Xonu/FGP/Model/Calculation.php +++ b/app/code/community/Xonu/FGP/Model/Calculation.php @@ -8,35 +8,36 @@ class Xonu_FGP_Model_Calculation extends Mage_Tax_Model_Calculation protected $adminSession = false; // set origin to destination - public function getRateOriginRequest($store = null) - { + public function getRateOriginRequest($store = null) + { if (Mage::app()->getStore()->isAdmin() || Mage::getDesign()->getArea() == 'adminhtml') { $this->adminSession = true; // creating order in the backend } $session = $this->getSession(); - if($session->hasQuote() || $this->adminSession) // getQuote() would lead to infinite loop here when switching currency - { - $quote = $session->getQuote(); - if($quote->getIsActive() || $this->adminSession) - { + if ($session->hasQuote() || $this->adminSession) { // getQuote() would lead to infinite loop here when switching currency + + $quote = $session->getQuote(); + + if ($quote->getIsActive() || $this->adminSession) { // use destination of the existing quote as origin if quote exists - $request = $this->getRateRequest( - $quote->getShippingAddress(), - $quote->getBillingAddress(), - $quote->getCustomerTaxClassId(), - $store - ); - - return $request; - } + $request = $this->getRateRequest( + $quote->getShippingAddress(), + $quote->getBillingAddress(), + $quote->getCustomerTaxClassId(), + $store + ); + + return $request; + } + if (Mage::getSingleton('customer/session')->isLoggedIn()) { $customer = Mage::getSingleton('customer/session')->getCustomer(); if (($billingAddress = $customer->getDefaultBillingAddress()) && ($shippingAddress = $customer->getDefaultShippingAddress())) { - // use destination of the existing quote as origin if quote exists + // use customer addresses as origin if customer is logged in $request = $this->getRateRequest( $shippingAddress, $billingAddress, @@ -49,155 +50,148 @@ public function getRateOriginRequest($store = null) } return $this->getDefaultDestination(); - } - else // quote is not available when switching the currency - { - return $this->getDefaultDestination(); - } - } - - public function getSession() + } + + // quote is not available when switching the currency + + return $this->getDefaultDestination(); + + } + + public function getSession() { if ($this->adminSession) { return Mage::getSingleton('adminhtml/session_quote'); // order creation in the backend } else { - return Mage::getSingleton('checkout/session'); // default order creation in the frontend - } + return Mage::getSingleton('checkout/session'); // default order creation in the frontend + } } // sometimes it is required to use shipping address from the quote instead of the default address - public function getRateRequest( - $shippingAddress = null, - $billingAddress = null, - $customerTaxClass = null, - $store = null) - { - if ($shippingAddress === false && $billingAddress === false && $customerTaxClass === false) { - return $this->getRateOriginRequest($store); - } - $address = new Varien_Object(); - $customer = $this->getCustomer(); - $basedOn = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_BASED_ON, $store); - - if (($shippingAddress === false && $basedOn == 'shipping') - || ($billingAddress === false && $basedOn == 'billing')) { - $basedOn = 'default'; - } else { - if ((($billingAddress === false || is_null($billingAddress) || !$billingAddress->getCountryId()) - && $basedOn == 'billing') - || (($shippingAddress === false || is_null($shippingAddress) || !$shippingAddress->getCountryId()) - && $basedOn == 'shipping') - ){ - $session = Mage::getSingleton('checkout/session'); - if ($customer) { - $defBilling = $customer->getDefaultBillingAddress(); - $defShipping = $customer->getDefaultShippingAddress(); - - if ($basedOn == 'billing' && $defBilling && $defBilling->getCountryId()) { - $billingAddress = $defBilling; - } else if ($basedOn == 'shipping' && $defShipping && $defShipping->getCountryId()) { - $shippingAddress = $defShipping; - } else { - if($session->hasQuote() || $this->adminSession) - { - $quote = $session->getQuote(); - $isActive = $quote->getIsActive(); - if($isActive) - { - $shippingAddress = $quote->getShippingAddress(); - $billingAddress = $quote->getBillingAddress(); - } - else{ - $basedOn = 'default'; - } - } - else{ - $basedOn = 'default'; - } - } - } else { - - if($session->hasQuote() || $this->adminSession) - { - $quote = $session->getQuote(); - $isActive = $quote->getIsActive(); - if($isActive) - { - $shippingAddress = $quote->getShippingAddress(); - $billingAddress = $quote->getBillingAddress(); - } - else { - $basedOn = 'default'; - } - } - else { - $basedOn = 'default'; - } - } - } - } - switch ($basedOn) { - case 'billing': - $address = $billingAddress; - break; - case 'shipping': - $address = $shippingAddress; - break; - case 'origin': - $address = $this->getRateOriginRequest($store); - break; - case 'default': - $address - ->setCountryId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, $store)) - ->setRegionId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_REGION, $store)) - ->setPostcode(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, $store)); - break; - } - - if (is_null($customerTaxClass) && $customer) { - $customerTaxClass = $customer->getTaxClassId(); - } elseif (($customerTaxClass === false) || !$customer) { - $customerTaxClass = $this->getDefaultCustomerTaxClass($store); - } - - $request = new Varien_Object(); - $request - ->setCountryId($address->getCountryId()) - ->setRegionId($address->getRegionId()) - ->setPostcode($address->getPostcode()) - ->setStore($store) - ->setCustomerClassId($customerTaxClass); - return $request; - } - - - private function getDefaultDestination($store = null) - { + public function getRateRequest( + $shippingAddress = null, + $billingAddress = null, + $customerTaxClass = null, + $store = null) + { + if ($shippingAddress === false && $billingAddress === false && $customerTaxClass === false) { + return $this->getRateOriginRequest($store); + } + $address = new Varien_Object(); + $customer = $this->getCustomer(); + $basedOn = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_BASED_ON, $store); + + if (($shippingAddress === false && $basedOn == 'shipping') + || ($billingAddress === false && $basedOn == 'billing')) { + $basedOn = 'default'; + } else { + if ((($billingAddress === false || is_null($billingAddress) || !$billingAddress->getCountryId()) + && $basedOn == 'billing') + || (($shippingAddress === false || is_null($shippingAddress) || !$shippingAddress->getCountryId()) + && $basedOn == 'shipping') + ) { + $session = Mage::getSingleton('checkout/session'); + if ($customer) { + $defBilling = $customer->getDefaultBillingAddress(); + $defShipping = $customer->getDefaultShippingAddress(); + + if ($basedOn == 'billing' && $defBilling && $defBilling->getCountryId()) { + $billingAddress = $defBilling; + } else if ($basedOn == 'shipping' && $defShipping && $defShipping->getCountryId()) { + $shippingAddress = $defShipping; + } else { + if($session->hasQuote() || $this->adminSession) { + $quote = $session->getQuote(); + $isActive = $quote->getIsActive(); + if($isActive) { + $shippingAddress = $quote->getShippingAddress(); + $billingAddress = $quote->getBillingAddress(); + } else { + $basedOn = 'default'; + } + } else { + $basedOn = 'default'; + } + } + } else { + + if($session->hasQuote() || $this->adminSession) { + $quote = $session->getQuote(); + $isActive = $quote->getIsActive(); + if ($isActive) { + $shippingAddress = $quote->getShippingAddress(); + $billingAddress = $quote->getBillingAddress(); + } else { + $basedOn = 'default'; + } + } else { + $basedOn = 'default'; + } + } + } + } + switch ($basedOn) { + case 'billing': + $address = $billingAddress; + break; + case 'shipping': + $address = $shippingAddress; + break; + case 'origin': + $address = $this->getRateOriginRequest($store); + break; + case 'default': + $address + ->setCountryId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, $store)) + ->setRegionId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_REGION, $store)) + ->setPostcode(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, $store)); + break; + } + + if (is_null($customerTaxClass) && $customer) { + $customerTaxClass = $customer->getTaxClassId(); + } elseif (($customerTaxClass === false) || !$customer) { + $customerTaxClass = $this->getDefaultCustomerTaxClass($store); + } + + $request = new Varien_Object(); + $request + ->setCountryId($address->getCountryId()) + ->setRegionId($address->getRegionId()) + ->setPostcode($address->getPostcode()) + ->setStore($store) + ->setCustomerClassId($customerTaxClass); + return $request; + } + + + protected function getDefaultDestination($store = null) + { $address = new Varien_Object(); - $request = new Varien_Object(); + $request = new Varien_Object(); - $address - ->setCountryId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, $store)) - ->setRegionId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_REGION, $store)) - ->setPostcode(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, $store)); + $address + ->setCountryId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, $store)) + ->setRegionId(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_REGION, $store)) + ->setPostcode(Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_DEFAULT_POSTCODE, $store)); - $customerTaxClass = null; - $customer = $this->getCustomer(); + $customerTaxClass = null; + $customer = $this->getCustomer(); - if (is_null($customerTaxClass) && $customer) { - $customerTaxClass = $customer->getTaxClassId(); - } elseif (($customerTaxClass === false) || !$customer) { - $customerTaxClass = $this->getDefaultCustomerTaxClass($store); - } + if (is_null($customerTaxClass) && $customer) { + $customerTaxClass = $customer->getTaxClassId(); + } elseif (($customerTaxClass === false) || !$customer) { + $customerTaxClass = $this->getDefaultCustomerTaxClass($store); + } - $request - ->setCountryId($address->getCountryId()) - ->setRegionId($address->getRegionId()) - ->setPostcode($address->getPostcode()) - ->setStore($store) - ->setCustomerClassId($customerTaxClass); + $request + ->setCountryId($address->getCountryId()) + ->setRegionId($address->getRegionId()) + ->setPostcode($address->getPostcode()) + ->setStore($store) + ->setCustomerClassId($customerTaxClass); - return $request; - } + return $request; + } } From 9b091379a0d6852b50b2c2f72e0ab5614314838a Mon Sep 17 00:00:00 2001 From: Andreas von Studnitz Date: Wed, 5 Feb 2014 12:15:47 +0100 Subject: [PATCH 3/3] Added composer.json and modman files --- composer.json | 16 ++++++++++++++++ modman | 3 +++ 2 files changed, 19 insertions(+) create mode 100644 composer.json create mode 100644 modman diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ab83341 --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name":"xonu/fgp", + "type":"magento-module", + "license":"http://xonu.de/license/", + "homepage":"https://github.com/xonu/FGP", + "description":"This extension will fix the gross price such that all customers pay the same while calculating country-specific tax rate.", + "authors":[ + { + "name":"Pawel Kazakow", + "email":"support@xonu.de" + } + ], + "require":{ + "magento-hackathon/magento-composer-installer":"*" + } +} \ No newline at end of file diff --git a/modman b/modman new file mode 100644 index 0000000..7cb112e --- /dev/null +++ b/modman @@ -0,0 +1,3 @@ +app/etc/modules/Xonu_FGP.xml app/etc/modules/Xonu_FGP.xml + +app/code/community/Xonu/FGP app/code/community/Xonu/FGP