From d7eac720aa1dbc38d5f2585b3db92f807608c76a Mon Sep 17 00:00:00 2001 From: Erik Hansen Date: Sat, 9 Jul 2016 18:33:50 -0500 Subject: [PATCH] Fix issue where invoice/credit memo may be mistakenly sent to AvaTax multiple times Fixes #24 --- Model/Queue.php | 26 ++++++++++++++++++++++++++ Model/Queue/Processing.php | 12 +++++++----- Model/Queue/Task.php | 33 ++++++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Model/Queue.php b/Model/Queue.php index 60318790..c5cdc23f 100644 --- a/Model/Queue.php +++ b/Model/Queue.php @@ -82,6 +82,12 @@ class Queue extends AbstractModel */ protected $eavConfig; + /** + * A boolean flag to determine whether record has been sent to AvaTax + * @var bool + */ + protected $hasRecordBeenSentToAvaTax = false; + /** * Queue constructor. * @param Context $context @@ -169,4 +175,24 @@ public function build($storeId, $entityTypeCode, $entityId, $incrementId, $queue $this->setQueueStatus($queueStatus); $this->setAttempts(0); } + + /** + * Set whether record has been sent to AvaTax + * + * @param $hasBeenSent + */ + public function setHasRecordBeenSentToAvaTax($hasBeenSent) + { + $this->hasRecordBeenSentToAvaTax = $hasBeenSent; + } + + /** + * Set whether record has been sent to AvaTax + * + * @return bool + */ + public function getHasRecordBeenSentToAvaTax() + { + return $this->hasRecordBeenSentToAvaTax; + } } diff --git a/Model/Queue/Processing.php b/Model/Queue/Processing.php index f7216a24..ba6328e8 100644 --- a/Model/Queue/Processing.php +++ b/Model/Queue/Processing.php @@ -326,19 +326,21 @@ protected function processWithAvaTax(Queue $queue, $entity) { try { $processSalesResponse = $this->interactionGetTax->processSalesObject($entity); - + $queue->setHasRecordBeenSentToAvaTax(true); } catch (\Exception $e) { $message = ''; if ($e instanceof \ClassyLlama\AvaTax\Exception\TaxCalculationException) { - $message .= __('An error occurred when attempting to send %1 #%2 to AvaTax.', + $message .= __('An error occurred when attempting to send %1 #%2 to AvaTax. Error: %3', ucfirst($queue->getEntityTypeCode()), - $entity->getIncrementId() + $entity->getIncrementId(), + $e->getMessage() ); } else { - $message .= __('An unexpected exception occurred when attempting to send %1 #%2 to AvaTax.', + $message .= __('An unexpected exception occurred when attempting to send %1 #%2 to AvaTax. Error: %3', ucfirst($queue->getEntityTypeCode()), - $entity->getIncrementId() + $entity->getIncrementId(), + $e->getMessage() ); } diff --git a/Model/Queue/Task.php b/Model/Queue/Task.php index 1b3c81f0..f5f5545a 100644 --- a/Model/Queue/Task.php +++ b/Model/Queue/Task.php @@ -169,16 +169,39 @@ public function processPendingQueue() // Increment error count statistic $this->errorCount++; - $this->errorMessages[] = $e->getMessage(); + $previousException = $e->getPrevious(); + $errorMessage = $e->getMessage(); + if ($previousException instanceof \Exception) { + $errorMessage .= " \nPREVIOUS ERROR: \n" . $previousException->getMessage(); + } + + // If record has been sent to AvaTax, immediately mark as failure + // to prevent duplicate records from being sent. This situation is likely to occur + // when associated record (invoice or credit memo) is unable to be saved. + if ($queue->getHasRecordBeenSentToAvaTax()) { + $errorMessage = 'Record was sent to AvaTax, but error occurred after sending record: ' + . $errorMessage; + // update queue record with new processing status + $queue->setQueueStatus(Queue::QUEUE_STATUS_FAILED) + ->setMessage($errorMessage) + ->save(); + } + + $this->errorMessages[] = $errorMessage; } } + $context = [ + 'error_count' => $this->errorCount, + 'process_count' => $this->processCount + ]; + if ($this->getErrorCount() > 0) { + $context['error_messages'] = implode("\n", $this->getErrorMessages()); + } + $this->avaTaxLogger->debug( __('Finished queue processing'), - [ /* context */ - 'error_count' => $this->errorCount, - 'process_count' => $this->processCount - ] + $context ); }