/** * Clear the queue of complete and failed records */ public function clearQueue() { $this->avaTaxLogger->debug(__('Starting queue clearing')); $this->clearCompleteQueue(); $this->clearFailedQueue(); $this->avaTaxLogger->debug(__('Finished queue clearing'), ['delete_complete_count' => $this->deleteCompleteCount, 'delete_failed_count' => $this->deleteFailedCount]); }
/** * Clear the queue of complete and failed records */ public function clearLogs() { $this->avaTaxLogger->debug(__('Starting queue clearing')); /** @var $collection \ClassyLlama\AvaTax\Model\ResourceModel\Log\Collection */ $collection = $this->logCollectionFactory->create(); // Get configuration for record lifetime $lifetimeDays = $this->avaTaxConfig->getLogDbLifetime(); // Calculate the number of seconds to adjust the filter // 86400 seconds == 60 seconds * 60 minutes * 24 hours == 1 day $secondsBeforeNow = $lifetimeDays * 60 * 60 * 24; // Add filters $collection->addCreatedAtBeforeFilter($secondsBeforeNow); // Process each queued entity /** @var $log Log */ foreach ($collection as $log) { // Remove the queue record $log->delete(); $this->deleteCount++; } $this->avaTaxLogger->debug(__('Finished clearing log entries'), ['delete_count' => $this->deleteCount]); }
/** * @param \Magento\Sales\Model\Spi\InvoiceResourceInterface $subject * @param \Closure $proceed * * I include both the extended AbstractModel and implemented Interface here for the IDE's benefit * @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Api\Data\InvoiceInterface $entity * @return \Magento\Sales\Model\Spi\InvoiceResourceInterface * @throws \Magento\Framework\Exception\CouldNotSaveException */ public function aroundSave(InvoiceResourceInterface $subject, \Closure $proceed, AbstractModel $entity) { // Check to see if this is a newly created entity and store the determination for later evaluation after // the entity is saved via plugin closure. After the entity is saved it will not be listed as new any longer. $isObjectNew = $entity->isObjectNew(); // Save AvaTax extension attributes if ($this->avaTaxConfig->isModuleEnabled($entity->getStoreId())) { // check to see if any extension attributes exist and set them on the model for saving to the db $extensionAttributes = $entity->getExtensionAttributes(); if ($extensionAttributes && $extensionAttributes->getAvataxIsUnbalanced() !== null) { $entity->setData('avatax_is_unbalanced', $extensionAttributes->getAvataxIsUnbalanced()); } if ($extensionAttributes && $extensionAttributes->getBaseAvataxTaxAmount() !== null) { $entity->setData('base_avatax_tax_amount', $extensionAttributes->getBaseAvataxTaxAmount()); } // Updating a field to trigger a change to the record when avatax_is_unbalanced and base_avatax_tax_amount // are both false or 0 which evaluate the same as null in the isModified check if ($extensionAttributes && ($extensionAttributes->getAvataxIsUnbalanced() !== null && ($entity->getOrigData('avatax_is_unbalanced') === null || $extensionAttributes->getAvataxIsUnbalanced() != $entity->getOrigData('avatax_is_unbalanced')) || $extensionAttributes->getBaseAvataxTaxAmount() !== null && ($entity->getOrigData('base_avatax_tax_amount') === null || $extensionAttributes->getBaseAvataxTaxAmount() != $entity->getOrigData('base_avatax_tax_amount')))) { $entity->setUpdatedAt($this->dateTime->gmtDate()); } } /** @var \Magento\Sales\Model\Spi\InvoiceResourceInterface $resultEntity */ $resultEntity = $proceed($entity); /** @var \Magento\Sales\Model\Order $order */ $order = $entity->getOrder(); $isVirtual = $order->getIsVirtual(); $address = $isVirtual ? $entity->getBillingAddress() : $entity->getShippingAddress(); $storeId = $entity->getStoreId(); // Queue the entity to be sent to AvaTax if ($this->avaTaxConfig->isModuleEnabled($entity->getStoreId()) && $this->avaTaxConfig->getTaxMode($entity->getStoreId()) == Config::TAX_MODE_ESTIMATE_AND_SUBMIT && $this->avaTaxConfig->isAddressTaxable($address, $storeId)) { // Add this entity to the avatax processing queue if this is a new entity if ($isObjectNew) { /** @var Queue $queue */ $queue = $this->queueFactory->create(); $queue->build($entity->getStoreId(), Queue::ENTITY_TYPE_CODE_INVOICE, $entity->getEntityId(), $entity->getIncrementId(), Queue::QUEUE_STATUS_PENDING); $queue->save(); $this->avaTaxLogger->debug(__('Added entity to the queue'), ['queue_id' => $queue->getId(), 'entity_type_code' => Queue::ENTITY_TYPE_CODE_INVOICE, 'entity_id' => $entity->getEntityId()]); } } return $resultEntity; }