Skip to content
This repository has been archived by the owner on Apr 22, 2019. It is now read-only.

Commit

Permalink
Fix issue where invoice/credit memo may be mistakenly sent to AvaTax …
Browse files Browse the repository at this point in the history
…multiple times

Fixes #24
  • Loading branch information
erikhansen committed Jul 9, 2016
1 parent b4c9028 commit d7eac72
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
26 changes: 26 additions & 0 deletions Model/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
12 changes: 7 additions & 5 deletions Model/Queue/Processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
33 changes: 28 additions & 5 deletions Model/Queue/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down

0 comments on commit d7eac72

Please sign in to comment.