/**
  * @param Queue $queue
  * @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $entity
  * @param \ClassyLlama\AvaTax\Api\Data\GetTaxResponseInterface $processSalesResponse
  */
 protected function completeQueueProcessing(Queue $queue, $entity, GetTaxResponseInterface $processSalesResponse)
 {
     $message = __('%1 #%2 was submitted to AvaTax', ucfirst($queue->getEntityTypeCode()), $entity->getIncrementId());
     $queueMessage = '';
     if ($processSalesResponse->getIsUnbalanced()) {
         $adjustmentMessage = null;
         if ($entity instanceof CreditmemoInterface) {
             if (abs($entity->getBaseAdjustmentNegative()) > 0 || abs($entity->getBaseAdjustmentPositive()) > 0) {
                 $adjustmentMessage = __('The difference was at least partly caused by the fact that the creditmemo ' . 'contained an adjustment of %1 and Magento doesn\'t factor that into its calculation, ' . 'but AvaTax does.', $entity->getBaseAdjustment());
             }
         }
         $queueMessage = __('Unbalanced Response - Collected: %1, AvaTax Actual: %2', $entity->getBaseTaxAmount(), $processSalesResponse->getBaseAvataxTaxAmount());
         if ($adjustmentMessage) {
             $queueMessage .= ' — ' . $adjustmentMessage;
         }
         // add comment about unbalanced amount
         $message .= '<br/>' . __('When submitting the %1 to AvaTax the amount calculated for tax differed from what was' . ' recorded in Magento.', $queue->getEntityTypeCode()) . '<br/>' . __('There was a difference of %1', $entity->getBaseTaxAmount() - $processSalesResponse->getBaseAvataxTaxAmount()) . '<br/>';
         if ($adjustmentMessage) {
             $message .= '<strong>' . $adjustmentMessage . '</strong><br/>';
         }
         $message .= __('Magento listed a tax amount of %1', $entity->getBaseTaxAmount()) . '<br/>' . __('AvaTax calculated the tax to be %1', $processSalesResponse->getBaseAvataxTaxAmount()) . '<br/>';
     }
     $queue->setMessage($queueMessage);
     $queue->setQueueStatus(Queue::QUEUE_STATUS_COMPLETE);
     $queue->save();
     // Add comment to order
     $this->addOrderComment($entity->getOrderId(), $message);
 }
Example #2
0
 /**
  * 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;
 }