/** * Convert quote/order/invoice/creditmemo to the AvaTax object and request tax from the Get Tax API * * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Tax\Api\Data\QuoteDetailsInterface $taxQuoteDetails * @param \Magento\Tax\Api\Data\QuoteDetailsInterface $baseTaxQuoteDetails * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment * @return \Magento\Tax\Api\Data\TaxDetailsInterface[] * @throws \ClassyLlama\AvaTax\Exception\TaxCalculationException * @throws \Exception */ public function getTaxDetailsForQuote(\Magento\Quote\Model\Quote $quote, \Magento\Tax\Api\Data\QuoteDetailsInterface $taxQuoteDetails, \Magento\Tax\Api\Data\QuoteDetailsInterface $baseTaxQuoteDetails, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment) { $storeId = $quote->getStoreId(); $taxService = $this->taxService; try { // Total quantity of an item can be determined by multiplying parent * child quantity, so it's necessary // to calculate total quantities on a list of all items $this->taxCalculation->calculateTotalQuantities($taxQuoteDetails->getItems()); $this->taxCalculation->calculateTotalQuantities($baseTaxQuoteDetails->getItems()); // Taxes need to be calculated on the base prices/amounts, not the current currency prices. As a result of this, // only the $baseTaxQuoteDetails will have taxes calculated for it. The taxes for the current currency will be // calculated by multiplying the base tax rates * currency conversion rate. /** @var $getTaxRequest GetTaxRequest */ $getTaxRequest = $this->interactionTax->getGetTaxRequestForQuote($quote, $baseTaxQuoteDetails, $shippingAssignment); if (is_null($getTaxRequest)) { $message = __('$quote was empty or address was not valid so not running getTax request.'); throw new \ClassyLlama\AvaTax\Exception\TaxCalculationException($message); } $getTaxResult = $taxService->getTax($getTaxRequest, $storeId, true); if ($getTaxResult->getResultCode() == \AvaTax\SeverityLevel::$Success) { $store = $quote->getStore(); $baseTaxDetails = $this->taxCalculation->calculateTaxDetails($baseTaxQuoteDetails, $getTaxResult, true, $store); /** * If quote is using a currency other than the base currency, calculate tax details for both quote * currency and base currency. Otherwise use the same tax details object. */ if ($quote->getBaseCurrencyCode() != $quote->getQuoteCurrencyCode()) { $taxDetails = $this->taxCalculation->calculateTaxDetails($taxQuoteDetails, $getTaxResult, false, $store); } else { $taxDetails = $baseTaxDetails; } return [self::KEY_TAX_DETAILS => $taxDetails, self::KEY_BASE_TAX_DETAILS => $baseTaxDetails]; } else { $message = __('Bad result code: %1', $getTaxResult->getResultCode()); $this->avaTaxLogger->warning($message, ['request' => var_export($getTaxRequest, true), 'result' => var_export($getTaxResult, true)]); throw new \ClassyLlama\AvaTax\Exception\TaxCalculationException($message); } } catch (\SoapFault $exception) { $message = "Exception: \n"; if ($exception) { $message .= $exception->faultstring; } $message .= $taxService->__getLastRequest() . "\n"; $message .= $taxService->__getLastResponse() . "\n"; $this->avaTaxLogger->error("Exception: \n" . $exception ? $exception->faultstring : "", ['request' => var_export($taxService->__getLastRequest(), true), 'result' => var_export($taxService->__getLastResponse(), true)]); throw new \ClassyLlama\AvaTax\Exception\TaxCalculationException($message); } catch (\Exception $exception) { $message = $exception->getMessage(); $this->avaTaxLogger->error($message); throw new \ClassyLlama\AvaTax\Exception\TaxCalculationException($message); } }
/** * @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $entity * @param \ClassyLlama\AvaTax\Api\Data\GetTaxResponseInterface $processSalesResponse */ protected function updateAdditionalEntityAttributes($entity, GetTaxResponseInterface $processSalesResponse) { $entityExtension = $entity->getExtensionAttributes(); if ($entityExtension == null) { $entityExtension = $this->getEntityExtensionInterface($entity); } // check to see if the AvataxIsUnbalanced is already set on this entity $avataxIsUnbalancedToSave = false; if ($entityExtension->getAvataxIsUnbalanced() === null) { $entityExtension->setAvataxIsUnbalanced($processSalesResponse->getIsUnbalanced()); $avataxIsUnbalancedToSave = true; } else { // check to see if any existing value is different from the new value if ($processSalesResponse->getIsUnbalanced() != $entityExtension->getAvataxIsUnbalanced()) { // Log the warning $this->avaTaxLogger->warning(__('When processing an entity in the queue there was an existing AvataxIsUnbalanced and ' . 'the new value was different than the old one. The old value was overwritten.'), ['old_is_unbalanced' => $entityExtension->getAvataxIsUnbalanced(), 'new_is_unbalanced' => $processSalesResponse->getIsUnbalanced()]); $entityExtension->setAvataxIsUnbalanced($processSalesResponse->getIsUnbalanced()); $avataxIsUnbalancedToSave = true; } } // check to see if the BaseAvataxTaxAmount is already set on this entity $baseAvataxTaxAmountToSave = false; if ($entityExtension->getBaseAvataxTaxAmount() === null) { $entityExtension->setBaseAvataxTaxAmount($processSalesResponse->getBaseAvataxTaxAmount()); $baseAvataxTaxAmountToSave = true; } else { // check to see if any existing value is different from the new value if ($processSalesResponse->getBaseAvataxTaxAmount() != $entityExtension->getBaseAvataxTaxAmount()) { // Log the warning $this->avaTaxLogger->warning(__('When processing an entity in the queue there was an existing BaseAvataxTaxAmount and ' . 'the new value was different than the old one. The old value was overwritten.'), ['old_base_avatax_tax_amount' => $entityExtension->getBaseAvataxTaxAmount(), 'new_base_avatax_tax_amount' => $processSalesResponse->getBaseAvataxTaxAmount()]); $entityExtension->setBaseAvataxTaxAmount($processSalesResponse->getBaseAvataxTaxAmount()); $baseAvataxTaxAmountToSave = true; } } // save the ExtensionAttributes on the entity object if ($avataxIsUnbalancedToSave || $baseAvataxTaxAmountToSave) { $entity->setExtensionAttributes($entityExtension); // get the repository for this entity type $entityRepository = $this->getEntityRepository($entity); // save the entity object using the repository $entityRepository->save($entity); } }