This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Feature/421 43 refund to store credit #55
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
efaf541
Merge branch 'release/0.3.0'
erikhansen 0f86a78
Merge branch 'release/0.3.1'
erikhansen 7fdf689
Merge branch 'release/0.3.2'
erikhansen a4c97f5
Merge branch 'release/0.3.3'
erikhansen 1ab4a2e
Merge branch 'release/0.3.4'
erikhansen 62d9dae
#421_43 - Refund to store credit
6c46ab5
#421_43 - Refund to store credit credit
ebd6f98
#421_43 - Refund to store credit
73c2670
#421_43 - Update changelog
7379e58
#421_43 - Refund to store credit
45f6d88
#421_43 - Refund to store credit
64204b1
#421_43 - Correct doc block
e43df7e
Refactored call to load AvataxRecord utilizing constant for field name
f65df5a
Merge remote-tracking branch 'origin/develop' into feature/421_43-ref…
858d76e
#421_43 - Refund to store credit
8fe14e8
#421_43 - Change upgrade data script to account for columns with “0” …
erikhansen 411619b
Revert "#421_43 - Change upgrade data script to account for columns w…
erikhansen ca3b691
Add clarification to the readme
erikhansen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* @category ClassyLlama | ||
* @copyright Copyright (c) 2017 Classy Llama Studios, LLC | ||
*/ | ||
|
||
namespace ClassyLlama\AvaTax\Model; | ||
|
||
/** | ||
* CreditMemo | ||
* | ||
* @method int getParentId() getParentId() | ||
* @method string getIsUnbalanced() getIsUnbalanced() | ||
* @method float getBaseAvataxTaxAmount() getBaseAvataxTaxAmount() | ||
* @method CreditMemo setParentId() setParentId(int $parentId) | ||
* @method CreditMemo setIsUnbalanced() setIsUnbalanced(string $isUnbalanced) | ||
* @method CreditMemo setBaseAvataxTaxAmount() setBaseAvataxTaxAmount(float $baseAvataxTaxAmount) | ||
*/ | ||
class CreditMemo | ||
extends \Magento\Framework\Model\AbstractModel | ||
implements \Magento\Framework\DataObject\IdentityInterface | ||
{ | ||
const CACHE_TAG = 'classyllama_avatax_creditMemo'; | ||
|
||
protected function _construct() | ||
{ | ||
$this->_init('ClassyLlama\AvaTax\Model\ResourceModel\CreditMemo'); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getIdentities() | ||
{ | ||
return [self::CACHE_TAG . '_' . $this->getId()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* @category ClassyLlama | ||
* @copyright Copyright (c) 2017 Classy Llama Studios, LLC | ||
*/ | ||
|
||
namespace ClassyLlama\AvaTax\Model; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where magic getters and setters are used on your model you should include references to them in the DocBlock section of the class definition so that the IDE knows about them and can help catch any typo/validation issues. It also allows the IDE to provide auto complete support when using the class. Example \ClassyLlama\AvaTax\Model\Queue:
|
||
/** | ||
* Invoice | ||
* | ||
* @method int getParentId() getParentId() | ||
* @method string getIsUnbalanced() getIsUnbalanced() | ||
* @method float getBaseAvataxTaxAmount() getBaseAvataxTaxAmount() | ||
* @method Invoice setParentId() setParentId(int $parentId) | ||
* @method Invoice setIsUnbalanced() setIsUnbalanced(string $isUnbalanced) | ||
* @method Invoice setBaseAvataxTaxAmount() setBaseAvataxTaxAmount(float $baseAvataxTaxAmount) | ||
*/ | ||
class Invoice | ||
extends \Magento\Framework\Model\AbstractModel | ||
implements \Magento\Framework\DataObject\IdentityInterface | ||
{ | ||
const CACHE_TAG = 'classyllama_avatax_invoice'; | ||
|
||
protected function _construct() | ||
{ | ||
$this->_init('ClassyLlama\AvaTax\Model\ResourceModel\Invoice'); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getIdentities() | ||
{ | ||
return [self::CACHE_TAG . '_' . $this->getId()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ | |
use ClassyLlama\AvaTax\Model\Queue; | ||
use ClassyLlama\AvaTax\Framework\Interaction\Tax\Get; | ||
use ClassyLlama\AvaTax\Api\Data\GetTaxResponseInterface; | ||
use ClassyLlama\AvaTax\Model\Invoice; | ||
use ClassyLlama\AvaTax\Model\InvoiceFactory; | ||
use ClassyLlama\AvaTax\Model\CreditMemo; | ||
use ClassyLlama\AvaTax\Model\CreditMemoFactory; | ||
use Magento\Framework\Stdlib\DateTime\DateTime; | ||
use Magento\Sales\Api\InvoiceRepositoryInterface; | ||
use Magento\Sales\Api\CreditmemoRepositoryInterface; | ||
|
@@ -32,6 +36,8 @@ | |
use Magento\Sales\Api\Data\CreditmemoExtensionFactory; | ||
use Magento\Eav\Model\Config as EavConfig; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use ClassyLlama\AvaTax\Model\ResourceModel\CreditMemo as CreditMemoResourceModel; | ||
use ClassyLlama\AvaTax\Model\ResourceModel\Invoice as InvoiceResourceModel; | ||
|
||
/** | ||
* Queue Processing | ||
|
@@ -64,9 +70,9 @@ class Processing | |
protected $invoiceRepository; | ||
|
||
/** | ||
* @var InvoiceRepositoryInterface | ||
* @var CreditmemoRepositoryInterface | ||
*/ | ||
protected $creditmemoRepository; | ||
protected $creditMemoRepository; | ||
|
||
/** | ||
* @var OrderRepositoryInterface | ||
|
@@ -91,27 +97,39 @@ class Processing | |
/** | ||
* @var CreditmemoExtensionFactory | ||
*/ | ||
protected $creditmemoExtensionFactory; | ||
protected $creditMemoExtensionFactory; | ||
|
||
/** | ||
* @var EavConfig | ||
*/ | ||
protected $eavConfig; | ||
|
||
/** | ||
* @var InvoiceFactory | ||
*/ | ||
protected $avataxInvoiceFactory; | ||
|
||
/** | ||
* @var CreditMemoFactory | ||
*/ | ||
protected $avataxCreditMemoFactory; | ||
|
||
/** | ||
* Processing constructor. | ||
* @param AvaTaxLogger $avaTaxLogger | ||
* @param Config $avaTaxConfig | ||
* @param Get $interactionGetTax | ||
* @param DateTime $dateTime | ||
* @param InvoiceRepositoryInterface $invoiceRepository | ||
* @param CreditmemoRepositoryInterface $creditmemoRepository | ||
* @param CreditMemoRepositoryInterface $creditmemoRepository | ||
* @param OrderRepositoryInterface $orderRepository | ||
* @param OrderManagementInterface $orderManagement | ||
* @param OrderStatusHistoryInterfaceFactory $orderStatusHistoryFactory | ||
* @param InvoiceExtensionFactory $invoiceExtensionFactory | ||
* @param CreditmemoExtensionFactory $creditmemoExtensionFactory | ||
* @param EavConfig $eavConfig | ||
* @param InvoiceFactory $avataxInvoiceFactory | ||
* @param CreditMemoFactory $avataxCreditMemoFactory | ||
*/ | ||
public function __construct( | ||
AvaTaxLogger $avaTaxLogger, | ||
|
@@ -125,7 +143,9 @@ public function __construct( | |
OrderStatusHistoryInterfaceFactory $orderStatusHistoryFactory, | ||
InvoiceExtensionFactory $invoiceExtensionFactory, | ||
CreditmemoExtensionFactory $creditmemoExtensionFactory, | ||
EavConfig $eavConfig | ||
EavConfig $eavConfig, | ||
InvoiceFactory $avataxInvoiceFactory, | ||
CreditMemoFactory $avataxCreditMemoFactory | ||
) { | ||
$this->avaTaxLogger = $avaTaxLogger; | ||
$this->avaTaxConfig = $avaTaxConfig; | ||
|
@@ -139,6 +159,8 @@ public function __construct( | |
$this->invoiceExtensionFactory = $invoiceExtensionFactory; | ||
$this->creditmemoExtensionFactory = $creditmemoExtensionFactory; | ||
$this->eavConfig = $eavConfig; | ||
$this->avataxInvoiceFactory = $avataxInvoiceFactory; | ||
$this->avataxCreditMemoFactory = $avataxCreditMemoFactory; | ||
} | ||
|
||
/** | ||
|
@@ -160,8 +182,8 @@ public function execute(Queue $queue) | |
// Process entity with AvaTax | ||
$processSalesResponse = $this->processWithAvaTax($queue, $entity); | ||
|
||
// update invoice with additional fields | ||
$this->updateAdditionalEntityAttributes($entity, $processSalesResponse); | ||
// Create AvaTax record | ||
$this->saveAvaTaxRecord($entity, $processSalesResponse); | ||
|
||
// Update the queue record status | ||
// and add comment to order | ||
|
@@ -373,119 +395,102 @@ protected function processWithAvaTax(Queue $queue, $entity) | |
/** | ||
* @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $entity | ||
* @param \ClassyLlama\AvaTax\Api\Data\GetTaxResponseInterface $processSalesResponse | ||
* @throws \Exception | ||
*/ | ||
protected function updateAdditionalEntityAttributes( | ||
protected function saveAvaTaxRecord( | ||
$entity, | ||
GetTaxResponseInterface $processSalesResponse | ||
) { | ||
$entityExtension = $entity->getExtensionAttributes(); | ||
if ($entityExtension == null) { | ||
$entityExtension = $this->getEntityExtensionInterface($entity); | ||
} | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parenthesis and curly brace should be on the same line in this situation:
|
||
{ | ||
// Get the associated AvataxEntity record (related to extension attributes) for this entity type | ||
$avaTaxRecord = $this->getAvataxEntity($entity); | ||
|
||
// check to see if the AvataxIsUnbalanced is already set on this entity | ||
$avataxIsUnbalancedToSave = false; | ||
if ($entityExtension->getAvataxIsUnbalanced() === null) { | ||
$entityExtension->setAvataxIsUnbalanced($processSalesResponse->getIsUnbalanced()); | ||
$avataxIsUnbalancedToSave = true; | ||
} else { | ||
// check to see if any existing value is different from the new value | ||
if ($processSalesResponse->getIsUnbalanced() <> $entityExtension->getAvataxIsUnbalanced()) { | ||
// Log the warning | ||
$this->avaTaxLogger->warning( | ||
__('When processing an entity in the queue there was an existing AvataxIsUnbalanced and ' . | ||
'the new value was different than the old one. The old value was overwritten.'), | ||
[ /* context */ | ||
'old_is_unbalanced' => $entityExtension->getAvataxIsUnbalanced(), | ||
'new_is_unbalanced' => $processSalesResponse->getIsUnbalanced(), | ||
] | ||
); | ||
$entityExtension->setAvataxIsUnbalanced($processSalesResponse->getIsUnbalanced()); | ||
if ($avaTaxRecord->getParentId()) { | ||
// Record exists, compare existing values to new | ||
|
||
// Check to see if isUnbalanced is already set on this entity | ||
$avataxIsUnbalancedToSave = false; | ||
if ($avaTaxRecord->getIsUnbalanced() == null) { | ||
$avaTaxRecord->setIsUnbalanced($processSalesResponse->getIsUnbalanced()); | ||
$avataxIsUnbalancedToSave = true; | ||
} else { | ||
// check to see if any existing value is different from the new value | ||
if ($processSalesResponse->getIsUnbalanced() <> $avaTaxRecord->getIsUnbalanced()) { | ||
// Log the warning | ||
$this->avaTaxLogger->warning( | ||
__('When processing an entity in the queue there was an existing AvataxIsUnbalanced and ' . | ||
'the new value was different than the old one. The old value was overwritten.'), | ||
[ /* context */ | ||
'old_is_unbalanced' => $avaTaxRecord->getIsUnbalanced(), | ||
'new_is_unbalanced' => $processSalesResponse->getIsUnbalanced(), | ||
] | ||
); | ||
$avaTaxRecord->setIsUnbalanced($processSalesResponse->getIsUnbalanced()); | ||
$avataxIsUnbalancedToSave = true; | ||
} | ||
} | ||
} | ||
|
||
// check to see if the BaseAvataxTaxAmount is already set on this entity | ||
$baseAvataxTaxAmountToSave = false; | ||
if ($entityExtension->getBaseAvataxTaxAmount() === null) { | ||
$entityExtension->setBaseAvataxTaxAmount($processSalesResponse->getBaseAvataxTaxAmount()); | ||
$baseAvataxTaxAmountToSave = true; | ||
} else { | ||
// check to see if any existing value is different from the new value | ||
if ($processSalesResponse->getBaseAvataxTaxAmount() <> $entityExtension->getBaseAvataxTaxAmount()) { | ||
// Log the warning | ||
$this->avaTaxLogger->warning( | ||
__('When processing an entity in the queue there was an existing BaseAvataxTaxAmount and ' . | ||
'the new value was different than the old one. The old value was overwritten.'), | ||
[ /* context */ | ||
'old_base_avatax_tax_amount' => $entityExtension->getBaseAvataxTaxAmount(), | ||
'new_base_avatax_tax_amount' => $processSalesResponse->getBaseAvataxTaxAmount(), | ||
] | ||
); | ||
$entityExtension->setBaseAvataxTaxAmount($processSalesResponse->getBaseAvataxTaxAmount()); | ||
// Check to see if the BaseAvataxTaxAmount is already set on this entity | ||
$baseAvataxTaxAmountToSave = false; | ||
if ($avaTaxRecord->getBaseAvataxTaxAmount() == null) { | ||
$avaTaxRecord->setBaseAvataxTaxAmount($processSalesResponse->getBaseAvataxTaxAmount()); | ||
$baseAvataxTaxAmountToSave = true; | ||
} else { | ||
// Check to see if any existing value is different from the new value | ||
if ($processSalesResponse->getBaseAvataxTaxAmount() <> $avaTaxRecord->getBaseAvataxTaxAmount()) { | ||
// Log the warning | ||
$this->avaTaxLogger->warning( | ||
__('When processing an entity in the queue there was an existing BaseAvataxTaxAmount and ' . | ||
'the new value was different than the old one. The old value was overwritten.'), | ||
[ /* context */ | ||
'old_base_avatax_tax_amount' => $avaTaxRecord->getBaseAvataxTaxAmount(), | ||
'new_base_avatax_tax_amount' => $processSalesResponse->getBaseAvataxTaxAmount(), | ||
] | ||
); | ||
$avaTaxRecord->setBaseAvataxTaxAmount($processSalesResponse->getBaseAvataxTaxAmount()); | ||
$baseAvataxTaxAmountToSave = true; | ||
} | ||
} | ||
} else { | ||
// No entry exists for entity ID, add data to entry and set store flags to true | ||
$avataxIsUnbalancedToSave = true; | ||
$baseAvataxTaxAmountToSave = true; | ||
$avaTaxRecord->setParentId($entity->getId()); | ||
$avaTaxRecord->setIsUnbalanced($processSalesResponse->getIsUnbalanced()); | ||
$avaTaxRecord->setBaseAvataxTaxAmount($processSalesResponse->getBaseAvataxTaxAmount()); | ||
} | ||
|
||
// save the ExtensionAttributes on the entity object | ||
if ($avataxIsUnbalancedToSave || $baseAvataxTaxAmountToSave) { | ||
$entity->setExtensionAttributes($entityExtension); | ||
|
||
// get the repository for this entity type | ||
$entityRepository = $this->getEntityRepository($entity); | ||
|
||
// save the entity object using the repository | ||
$entityRepository->save($entity); | ||
// Save the AvaTax entry | ||
$avaTaxRecord->save(); | ||
} | ||
} | ||
|
||
/** | ||
* @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $entity | ||
* @return \Magento\Sales\Api\Data\InvoiceExtension|\Magento\Sales\Api\Data\CreditmemoExtension | ||
* @return CreditMemo|Invoice | ||
* @throws \Exception | ||
*/ | ||
protected function getEntityExtensionInterface($entity) | ||
protected function getAvataxEntity($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); | ||
} | ||
} | ||
/** @var Invoice $avaTaxRecord */ | ||
$avaTaxRecord = $this->avataxInvoiceFactory->create(); | ||
|
||
/** | ||
* @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $entity | ||
* @return \Magento\Eav\Model\Entity\Type | ||
* @throws \Exception | ||
*/ | ||
protected function getEntityType($entity) | ||
{ | ||
if ($entity instanceof InvoiceInterface) { | ||
return $this->eavConfig->getEntityType(Queue::ENTITY_TYPE_CODE_INVOICE); | ||
} elseif ($entity instanceof CreditmemoInterface) { | ||
return $this->eavConfig->getEntityType(Queue::ENTITY_TYPE_CODE_CREDITMEMO); | ||
} else { | ||
$message = __('Did not receive a valid entity instance to determine the entity type to return'); | ||
throw new \Exception($message); | ||
} | ||
} | ||
// Load existing AvaTax entry for this entity, if exists | ||
$avaTaxRecord->load($entity->getId(), InvoiceResourceModel::PARENT_ID_FIELD_NAME); | ||
|
||
/** | ||
* @param \Magento\Sales\Api\Data\InvoiceInterface|\Magento\Sales\Api\Data\CreditmemoInterface $entity | ||
* @return \Magento\Sales\Api\InvoiceRepositoryInterface|\Magento\Sales\Api\CreditmemoRepositoryInterface | ||
* @throws \Exception | ||
*/ | ||
protected function getEntityRepository($entity) | ||
{ | ||
if ($entity instanceof InvoiceInterface) { | ||
return $this->invoiceRepository; | ||
return $avaTaxRecord; | ||
} elseif ($entity instanceof CreditmemoInterface) { | ||
return $this->creditmemoRepository; | ||
/** @var CreditMemo $avaTaxRecord */ | ||
$avaTaxRecord = $this->avataxCreditMemoFactory->create(); | ||
|
||
// Load existing AvaTax entry for this entity, if exists | ||
$avaTaxRecord->load($entity->getId(), CreditMemoResourceModel::PARENT_ID_FIELD_NAME); | ||
|
||
return $avaTaxRecord; | ||
} else { | ||
$message = __('Did not receive a valid entity instance to determine the repository type to return'); | ||
$message = __('Did not receive a valid entity instance to determine the factory type to return'); | ||
throw new \Exception($message); | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to the readme to provide more information about this release