/** * Get an AvaTax address object with fields as specified in data * * Note: AvaTax only allows 3 street fields according to the API documentation. The customer/address/street_lines * allows the admin to create fields for 1 to 4 street lines. This configuration is currently disabled in * Magento/CustomerCustomAttributes/etc/adminhtml/system.xml. As a result not currently doing anything with this. * Likely no special consideration since the code is already sending all addresses (up to 3) to AvaTax if present. * * @param $data \Magento\Customer\Api\Data\AddressInterface|\Magento\Quote\Api\Data\AddressInterface|\Magento\Sales\Api\Data\OrderAddressInterface|array * @return \AvaTax\Address * @throws LocalizedException */ public function getAddress($data) { switch (true) { case $data instanceof \Magento\Customer\Api\Data\AddressInterface: $data = $this->convertCustomerAddressToAvaTaxAddress($data); break; case $data instanceof \Magento\Quote\Api\Data\AddressInterface: $data = $this->convertQuoteAddressToAvaTaxAddress($data); break; case $data instanceof \Magento\Sales\Api\Data\OrderAddressInterface: $data = $this->convertOrderAddressToAvaTaxAddress($data); break; case !is_array($data): throw new LocalizedException(__('Input parameter "$data" was not of a recognized/valid type: "%1".', [gettype($data)])); } if (isset($data['RegionId'])) { $data['Region'] = $this->getRegionCodeById($data['RegionId']); unset($data['RegionId']); } try { $data = $this->metaDataObject->validateData($data); } catch (MetaData\ValidationException $e) { $this->avaTaxLogger->error('Error validating address: ' . $e->getMessage(), ['data' => var_export($data, true)]); // Rethrow exception as if internal validation fails, don't send address to AvaTax throw $e; } $address = $this->addressFactory->create(); return $this->populateAddress($data, $address); }
/** * Create cache key by calling specified methods and concatenating and hashing * * @param $object * @return string * @throws LocalizedException */ protected function getCacheKey($object) { return $this->metaDataObject->getCacheKeyFromObject($object); }
/** * Creates and returns a populated getTaxRequest for a invoice * * @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $object * @return GetTaxRequest */ public function getGetTaxRequestForSalesObject($object) { $order = $this->orderRepository->get($object->getOrderId()); $lines = []; $items = $object->getItems(); $this->taxClassHelper->populateCorrectTaxClasses($items, $object->getStoreId()); /** @var \Magento\Tax\Api\Data\QuoteDetailsItemInterface $item */ foreach ($items as $item) { $line = $this->interactionLine->getLine($item); if ($line) { $lines[] = $line; } } $objectIsCreditMemo = $object instanceof \Magento\Sales\Api\Data\CreditmemoInterface; $credit = $objectIsCreditMemo; $line = $this->interactionLine->getShippingLine($object, $credit); if ($line) { $lines[] = $line; } $line = $this->interactionLine->getGiftWrapItemsLine($object, $credit); if ($line) { $lines[] = $line; } $line = $this->interactionLine->getGiftWrapOrderLine($object, $credit); if ($line) { $lines[] = $line; } $line = $this->interactionLine->getGiftWrapCardLine($object, $credit); if ($line) { $lines[] = $line; } if ($objectIsCreditMemo) { $line = $this->interactionLine->getPositiveAdjustmentLine($object); if ($line) { $lines[] = $line; } $line = $this->interactionLine->getNegativeAdjustmentLine($object); if ($line) { $lines[] = $line; } } /** @var \Magento\Sales\Api\Data\OrderAddressInterface $address */ if (!$order->getIsVirtual()) { $address = $order->getShippingAddress(); } else { $address = $order->getBillingAddress(); } $avaTaxAddress = $this->address->getAddress($address); $store = $this->storeRepository->getById($object->getStoreId()); $currentDate = $this->getFormattedDate($store, $object->getCreatedAt()); $taxOverride = null; if ($object instanceof \Magento\Sales\Api\Data\InvoiceInterface) { $docType = DocumentType::$SalesInvoice; if ($this->areTimesDifferentDays($order->getCreatedAt(), $object->getCreatedAt(), $object->getStoreId())) { $taxCalculationDate = $this->getFormattedDate($store, $order->getCreatedAt()); // Set the tax date for calculation $taxOverride = $this->taxOverrideFactory->create(); $taxOverride->setTaxDate($taxCalculationDate); $taxOverride->setTaxOverrideType(\AvaTax\TaxOverrideType::$TaxDate); $taxOverride->setTaxAmount(0.0); $taxOverride->setReason(self::AVATAX_INVOICE_OVERRIDE_REASON); } } else { $docType = DocumentType::$ReturnInvoice; $invoice = $this->getInvoice($object->getInvoiceId()); // If a Creditmemo was generated for an invoice, use the created_at value from the invoice if ($invoice) { $taxCalculationDate = $this->getFormattedDate($store, $invoice->getCreatedAt()); } else { $taxCalculationDate = $this->getFormattedDate($store, $order->getCreatedAt()); } // Set the tax date for calculation $taxOverride = $this->taxOverrideFactory->create(); $taxOverride->setTaxDate($taxCalculationDate); $taxOverride->setTaxOverrideType(\AvaTax\TaxOverrideType::$TaxDate); $taxOverride->setTaxAmount(0.0); $taxOverride->setReason(self::AVATAX_CREDITMEMO_OVERRIDE_REASON); } $customer = $this->getCustomerById($order->getCustomerId()); $customerUsageType = $customer ? $this->taxClassHelper->getAvataxTaxCodeForCustomer($customer) : null; $data = ['StoreId' => $store->getId(), 'Commit' => $this->config->getCommitSubmittedTransactions($store), 'TaxOverride' => $taxOverride, 'CurrencyCode' => $order->getOrderCurrencyCode(), 'CustomerCode' => $this->getCustomerCode($order), 'CustomerUsageType' => $customerUsageType, 'DestinationAddress' => $avaTaxAddress, 'DocCode' => $object->getIncrementId() . '123-' . rand(10000000, 90000000000), 'DocDate' => $currentDate, 'DocType' => $docType, 'ExchangeRate' => $this->getExchangeRate($store, $order->getBaseCurrencyCode(), $order->getOrderCurrencyCode()), 'ExchangeRateEffDate' => $currentDate, 'Lines' => $lines, 'DetailLevel' => DetailLevel::$Document, 'PaymentDate' => $currentDate, 'PurchaseOrderNo' => $object->getIncrementId()]; $data = array_merge($this->retrieveGetTaxRequestFields($store, $address, $object), $data); try { $data = $this->metaDataObject->validateData($data); } catch (ValidationException $e) { $this->avaTaxLogger->error('Error validating data: ' . $e->getMessage(), ['data' => var_export($data, true)]); } /** @var $getTaxRequest GetTaxRequest */ $getTaxRequest = $this->getTaxRequestFactory->create(); $this->populateGetTaxRequest($data, $getTaxRequest); return $getTaxRequest; }