-
Notifications
You must be signed in to change notification settings - Fork 15
Conversation
- Add code to create two new AvaTax DB tables - Add code to populate tables on queue processing
- Remove code that is now unused - Add code to migrate data from old columns to new tables - Refactor code to load data from new tables into ext attributes
- Add doc blocks - Revise code to remove unused methods - Add upgrade data script
- Remove code for Refund plugin - Add code to remove unused AvaTax columns from core tables - Refactor code so new install will create new tables instead of adding columns to core tables
namespace ClassyLlama\AvaTax\Model\ResourceModel; | ||
|
||
class CreditMemo extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb | ||
{ |
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.
It appears that the database field names are used in a few places, and I would suggest setting a constant for each of the field names on the resource and reference those instead of having several strings typed out in the code directly representing the field names. This will allow for more strongly typed code, and the ability to find any references where those field names are used throughout the code. It also aids in any event that the field names need to be changed in the future as they are only typed out in one location.
You can find an example of this to this on \ClassyLlama\AvaTax\Model\ResourceModel\Queue
/**#@+
* Field Names
*/
const QUEUE_STATUS_FIELD_NAME = 'queue_status';
const UPDATED_AT_FIELD_NAME = 'updated_at';
const CREATED_AT_FIELD_NAME = 'created_at';
/**#@-*/
Model/ResourceModel/Invoice.php
Outdated
|
||
class Invoice extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb | ||
{ | ||
protected function _construct() |
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.
Database field names could be defined as constants and referenced instead of statically typed strings throughout the code.
An example of this to this on: \ClassyLlama\AvaTax\Model\ResourceModel\Queue
Model/CreditMemo.php
Outdated
|
||
namespace ClassyLlama\AvaTax\Model; | ||
|
||
class CreditMemo |
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.
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:
/**
* Queue
*
* @method string getCreatedAt() getCreatedAt()
* @method string getUpdatedAt() getUpdatedAt()
* @method int getStoreId() getStoreId()
*/ | ||
|
||
namespace ClassyLlama\AvaTax\Model; | ||
|
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.
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:
/**
* Queue
*
* @method string getCreatedAt() getCreatedAt()
* @method string getUpdatedAt() getUpdatedAt()
* @method int getStoreId() getStoreId()
$baseAvataxTaxAmount = $entity->getData('base_avatax_tax_amount'); | ||
// Get the AvaTax record | ||
/** @var CreditMemo $avataxRecord */ | ||
$avataxRecord = $this->avataxCreditMemo->loadByParentId($entity->getId()); |
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 think you can call load() on the model with an additional parameter to specify the field you want to load by, and thus eliminate the loadByParentId() methods on both your model and resourceModel classes.
$this->avataxCreditMemo->load($entity->getId(), AvaTaxCreditMemoResource::PARENT_ID_FIELD_NAME);
Reference: \Magento\Framework\Model\ResourceModel\Db\AbstractDb::load
$baseAvataxTaxAmount = $entity->getData('base_avatax_tax_amount'); | ||
// Get the AvaTax record | ||
/** @var Invoice $invoice */ | ||
$avataxRecord = $this->avataxInvoice->loadByParentId($entity->getId()); |
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.
Eliminate the loadByParentId() method on your model and resourceModel by using the Abstract load() method with a field name parameter as indicated on the CreditMemo comment.
Reference: \Magento\Framework\Model\ResourceModel\Db\AbstractDb::load
We should review the use of extension attributes for Magento\Sales\Api\Data\CreditmemoInterface in etc/extension_attributes.xml I'm wondering how this works when used with a SearchResult on a list of multiple CreditMemo results. I'm not sure we have an existing use case for this, but it is something to be aware of at least. There is some support for extension attributes that utilize a reference table (reference_table, reference_field, join_on_field) http://devdocs.magento.com/guides/v2.0/extension-dev-guide/attributes.html. |
/** @var CreditMemo $avataxRecord */ | ||
$avataxRecord = $this->avataxCreditMemo->loadByParentId($entity->getId()); | ||
$avataxIsUnbalanced = $avataxRecord->getData('is_unbalanced'); | ||
$baseAvataxTaxAmount = $avataxRecord->getData('base_avatax_tax_amount'); |
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.
If the magic getters and setters are defined on the class you shouldn't need to make calls to getData()
with the direct field name. You could just call $avataxRecord->getBaseAvataxTaxAmount()
Model/Queue/Processing.php
Outdated
{ | ||
$avaTaxRecord = $this->createAvataxEntity($entity); | ||
// Load existing AvaTax entry for this entity, if exists | ||
$avaTaxRecord->loadByParentId($entity->getId()); |
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.
Another place where the loadByParentId()
method could be eliminated in favor of load($entity->getId(), 'parent_id')`
Model/Queue/Processing.php
Outdated
] | ||
); | ||
$entityExtension->setAvataxIsUnbalanced($processSalesResponse->getIsUnbalanced()); | ||
if ($avaTaxRecord->getData('parent_id')) { |
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.
If the magic getters are specified in the docblock on the class you could call this as $avaTaxRecord->getParentId()
- Revise code to utilize magic getters and setters - Revise code to utilize constants for column names - Remove unnecessary methods
Removed backslash in fully qualified use references
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 committed a minor refactor to Processing.php and corrected a couple fully qualified use statements.
If you can run a quick test with the latest commit checked out to confirm that it still loads an already existing record to update when saving the AvataxRecord, this will be good to merge.
You will probably also want to resolve branch conflicts before your final test. |
…und-to-store-credit # Conflicts: # CHANGELOG.md
- Revise version number in appropriate locations
…instead of “NULL” values
…ith “0” instead of “NULL” values" - this change is actually not needed, since each invoice should always record values, even if it’s not imbalanced This reverts commit 8fe14e8.
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.
Overall, this looks great. I tested locally and confirmed that the following works:
- Upgrading existing install moves data into new tables
- New invoices created get appropriately logged to the new tables
if ($entityExtension == null) { | ||
$entityExtension = $this->getEntityExtensionInterface($entity); | ||
} | ||
) |
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.
The parenthesis and curly brace should be on the same line in this situation:
) {
Setup/UpgradeData.php
Outdated
'avatax_is_unbalanced', | ||
'base_avatax_tax_amount' | ||
]) | ||
->where('(base_avatax_tax_amount IS NOT NULL AND base_avatax_tax_amount <> 0) OR (avatax_is_unbalanced IS NOT NULL AND avatax_is_unbalanced <> 0)'); |
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 tested this locally and found that the avatax_is_unbalanced
and base_avatax_tax_amount
columns were sometimes "0" instead of "NULL":
As a result of this, the entries in the avatax_sales_invoice
and avatax_sales_creditmemo
tables were getting entries inserted into it like this:
In order to prevent this, I changed this conditional in this commit:
I tested this change and now only the correct values are getting inserted:
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.
Actually, never mind. I reverted that commit. I forgot that those columns got populated regardless of whether the invoice/credit memo was unbalanced.
* Add code to create new database tables dedicated to storing AvaTax data | ||
* Add code to migrate existing data from AvaTax columns on sales_invoice and sales_creditmemo tables to new tables | ||
* Refactor code to store AvaTax data in new tables instead of attaching to entities | ||
* Previous versions of this extension added two fields to the native Magento invoice and credit memo tables. When this extension |
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
columns to core tables