/** * @param \Magento\Sales\Model\Spi\CreditmemoResourceInterface $subject * @param \Closure $proceed * * Include both the extended AbstractModel and implemented Interface here for the IDE's benefit * @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Api\Data\CreditmemoInterface $entity * @param $value * @param null $field * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ public function aroundLoad(CreditmemoResourceInterface $subject, \Closure $proceed, AbstractModel $entity, $value, $field = null) { /** @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resultEntity */ $resultEntity = $proceed($entity, $value, $field); // Load AvaTax extension attributes if ($this->avaTaxConfig->isModuleEnabled($entity->getStoreId())) { // Get the AvaTax Attributes from the AbstractModel $avataxIsUnbalanced = $entity->getData('avatax_is_unbalanced'); $baseAvataxTaxAmount = $entity->getData('base_avatax_tax_amount'); // Check the AvaTax Entity to see if we need to add extension attributes if ($avataxIsUnbalanced !== null || $baseAvataxTaxAmount !== null) { // Get any existing extension attributes or create a new one $entityExtension = $entity->getExtensionAttributes(); if (!$entityExtension) { $entityExtension = $this->creditmemoExtensionFactory->create(); } // Set the attributes if ($avataxIsUnbalanced !== null) { $entityExtension->setAvataxIsUnbalanced($avataxIsUnbalanced); } if ($baseAvataxTaxAmount !== null) { $entityExtension->setBaseAvataxTaxAmount($baseAvataxTaxAmount); } // save the ExtensionAttributes on the entity object $entity->setExtensionAttributes($entityExtension); } } return $resultEntity; }
/** * @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $entity * @return \Magento\Sales\Api\Data\InvoiceExtension|\Magento\Sales\Api\Data\CreditmemoExtension * @throws \Exception */ protected function getEntityExtensionInterface($entity) { if ($entity instanceof InvoiceInterface) { return $this->invoiceExtensionFactory->create(); } elseif ($entity instanceof CreditmemoInterface) { return $this->creditmemoExtensionFactory->create(); } else { $message = __('Did not receive a valid entity instance to determine the extension to return'); throw new \Exception($message); } }