Esempio n. 1
0
 public function callGetTaxForQuote(Mage_Sales_Model_Quote $quote)
 {
     /** @var Aoe_AvaTax_Helper_Soap $helper */
     $helper = Mage::helper('Aoe_AvaTax/Soap');
     $address = $quote->getShippingAddress();
     if ($address->validate() !== true) {
         $resultArray = array('ResultCode' => 'Skip', 'Messages' => array(), 'TaxLines' => array());
         return $resultArray;
     }
     $store = $quote->getStore();
     $hideDiscountAmount = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT, $store);
     $timestamp = $quote->getCreatedAt() ? Varien_Date::toTimestamp($quote->getCreatedAt()) : now();
     $date = new Zend_Date($timestamp);
     $request = new AvaTax\GetTaxRequest();
     $request->setCompanyCode($this->limit($helper->getConfig('company_code', $store), 25));
     $request->setDocType(AvaTax\DocumentType::$SalesOrder);
     $request->setCommit(false);
     $request->setDetailLevel(AvaTax\DetailLevel::$Tax);
     $request->setDocDate($date->toString('yyyy-MM-dd'));
     $request->setCustomerCode($helper->getCustomerDocCode($quote->getCustomer()) ?: $helper->getQuoteDocCode($quote));
     $request->setCurrencyCode($this->limit($quote->getBaseCurrencyCode(), 3));
     $request->setDiscount($hideDiscountAmount ? 0.0 : $store->roundPrice($address->getBaseDiscountAmount()));
     if ($quote->getCustomerTaxvat()) {
         $request->setBusinessIdentificationNo($this->limit($quote->getCustomerTaxvat(), 25));
     }
     $request->setOriginAddress($this->getOriginAddress($store));
     $request->setDestinationAddress($this->getAddress($address));
     $taxLines = array();
     $itemPriceIncludesTax = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $store);
     foreach ($this->getHelper()->getActionableQuoteAddressItems($address) as $k => $item) {
         /** @var Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Quote_Address_Item $item */
         $itemAmount = $store->roundPrice($itemPriceIncludesTax ? $item->getBaseRowTotalInclTax() : $item->getBaseRowTotal());
         //$itemAmount = $store->roundPrice($item->getBaseRowTotal());
         $itemAmount -= $store->roundPrice($item->getBaseDiscountAmount());
         $taxLine = new AvaTax\Line();
         $taxLine->setNo($this->limit($k, 50));
         $taxLine->setItemCode($this->limit($item->getSku(), 50));
         $taxLine->setQty(round($item->getQty(), 4));
         $taxLine->setAmount($itemAmount);
         $taxLine->setDescription($this->limit($item->getName(), 255));
         $taxLine->setTaxCode($this->limit($helper->getProductTaxCode($item->getProduct()), 25));
         $taxLine->setDiscounted($item->getBaseDiscountAmount() > 0.0);
         $taxLine->setTaxIncluded($itemPriceIncludesTax);
         $taxLine->setRef1($this->limit($helper->getQuoteItemRef1($item, $store), 250));
         $taxLine->setRef2($this->limit($helper->getQuoteItemRef2($item, $store), 250));
         $taxLines[] = $taxLine;
     }
     $shippingPriceIncludesTax = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_INCLUDES_TAX, $store);
     $shippingAmount = $store->roundPrice($shippingPriceIncludesTax ? $address->getBaseShippingInclTax() : $address->getBaseShippingAmount());
     //$shippingAmount = $store->roundPrice($address->getBaseShippingAmount());
     $shippingAmount -= $store->roundPrice($address->getBaseShippingDiscountAmount());
     $taxLine = new AvaTax\Line();
     $taxLine->setNo('SHIPPING');
     $taxLine->setItemCode('SHIPPING');
     $taxLine->setQty(1);
     $taxLine->setAmount($shippingAmount);
     $taxLine->setDescription($this->limit("Shipping: " . $address->getShippingMethod(), 255));
     $taxLine->setTaxCode($this->limit($helper->getShippingTaxCode($store), 25));
     $taxLine->setDiscounted($address->getBaseShippingDiscountAmount() > 0.0);
     $taxLine->setTaxIncluded($shippingPriceIncludesTax);
     $taxLine->setRef1($this->limit($address->getShippingMethod(), 25));
     $taxLines[] = $taxLine;
     $request->setLines($taxLines);
     Mage::dispatchEvent('aoe_avatax_soapapi_get_tax_for_quote_before', array('request' => $request, 'quote' => $quote));
     // TODO: Handle giftwrapping
     return $this->callGetTax($store, $request);
 }