Skip to content

Conversation

@mimarcel
Copy link
Contributor

@mimarcel mimarcel commented Feb 27, 2017

This PR fixes possible SALES_ORDER_TAX_ITEM_TAX_ID_ITEM_ID duplicates caused for tax rates with loosely equal codes.

Example

In the below screenshot, product 90897 has tax WA STATE TAX - WA COUNTY TAX applied with two tax rates:

  • WA STATE TAX with code '53'
  • WA COUNTY TAX with code '053'

item_applied_taxes!

Cause and Solution

The system will throw an error caused by using the equal operator '==' instead of the identical operator '===' when comparing tax rates. This PR replaces '==' operator with '===' operator.

Full error stack trace is here:

Integrity constraint violation: 1062 Duplicate entry '81174-244092' for key 'SALES_ORDER_TAX_ITEM_TAX_ID_ITEM_ID' in /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /app/6x42bwop6jzim/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(95): Zend_Db_Statement_Pdo->_execute(Array)
#2 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array)
#3 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#4 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `sa...', Array)
#5 /app/6x42bwop6jzim/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(459): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `sa...', Array)
#6 /app/6x42bwop6jzim/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(514): Magento\Framework\DB\Adapter\Pdo\Mysql->_query('INSERT INTO `sa...', Array)
#7 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(576): Magento\Framework\DB\Adapter\Pdo\Mysql->query('INSERT INTO `sa...', Array)
#8 /app/6x42bwop6jzim/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(759): Zend_Db_Adapter_Abstract->insert('sales_order_tax...', Array)
#9 /app/6x42bwop6jzim/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(405): Magento\Framework\Model\ResourceModel\Db\AbstractDb->saveNewObject(Object(Magento\Sales\Model\Order\Tax\Item))
#10 /app/6x42bwop6jzim/vendor/magento/framework/Model/AbstractModel.php(631): Magento\Framework\Model\ResourceModel\Db\AbstractDb->save(Object(Magento\Sales\Model\Order\Tax\Item))
#11 /app/6x42bwop6jzim/vendor/magento/module-tax/Model/Plugin/OrderSave.php(186): Magento\Framework\Model\AbstractModel->save()
#12 /app/6x42bwop6jzim/vendor/magento/module-tax/Model/Plugin/OrderSave.php(46): Magento\Tax\Model\Plugin\OrderSave->saveOrderTax(Object(Magento\Sales\Model\Order\Interceptor))
#13 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(152): Magento\Tax\Model\Plugin\OrderSave->afterSave(Object(Magento\Sales\Model\OrderRepository\Interceptor), Object(Magento\Sales\Model\Order\Interceptor))
#14 /app/6x42bwop6jzim/init/var/generation/Magento/Sales/Model/OrderRepository/Interceptor.php(52): Magento\Sales\Model\OrderRepository\Interceptor->___callPlugins('save', Array, Array)
#15 /app/6x42bwop6jzim/vendor/magento/module-sales/Model/Service/OrderService.php(191): Magento\Sales\Model\OrderRepository\Interceptor->save(Object(Magento\Sales\Model\Order\Interceptor))
#16 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Sales\Model\Service\OrderService->place(Object(Magento\Sales\Model\Order\Interceptor))
#17 /app/6x42bwop6jzim/init/var/generation/Magento/Sales/Model/Service/OrderService/Interceptor.php(39): Magento\Sales\Model\Service\OrderService\Interceptor->___callPlugins('place', Array, Array)
#18 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/QuoteManagement.php(488): Magento\Sales\Model\Service\OrderService\Interceptor->place(Object(Magento\Sales\Model\Order\Interceptor))
#19 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/QuoteManagement.php(391): Magento\Quote\Model\QuoteManagement->submitQuote(Object(Magento\Quote\Model\Quote\Interceptor), Array)
#20 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/QuoteManagement.php(349): Magento\Quote\Model\QuoteManagement->submit(Object(Magento\Quote\Model\Quote\Interceptor))
#21 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/GuestCart/GuestCartManagement.php(87): Magento\Quote\Model\QuoteManagement->placeOrder('405078', NULL)
#22 /app/6x42bwop6jzim/vendor/magento/module-checkout/Model/GuestPaymentInformationManagement.php(81): Magento\Quote\Model\GuestCart\GuestCartManagement->placeOrder('ced4065de257c32...')
#23 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Checkout\Model\GuestPaymentInformationManagement->savePaymentInformationAndPlaceOrder('ced4065de257c32...', 'customer.email@g...', Object(Magento\Quote\Model\Quote\Payment), Object(Magento\Quote\Model\Quote\Address\Interceptor))
#24 /app/6x42bwop6jzim/init/var/generation/Magento/Checkout/Model/GuestPaymentInformationManagement/Interceptor.php(26): Magento\Checkout\Model\GuestPaymentInformationManagement\Interceptor->___callPlugins('savePaymentInfo...', Array, Array)
#25 [internal function]: Magento\Checkout\Model\GuestPaymentInformationManagement\Interceptor->savePaymentInformationAndPlaceOrder('ced4065de257c32...', 'customer.email@g...', Object(Magento\Quote\Model\Quote\Payment), Object(Magento\Quote\Model\Quote\Address\Interceptor))
#26 /app/6x42bwop6jzim/vendor/magento/module-webapi/Controller/Rest.php(307): call_user_func_array(Array, Array)
#27 /app/6x42bwop6jzim/vendor/magento/module-webapi/Controller/Rest.php(216): Magento\Webapi\Controller\Rest->processApiRequest()
#28 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#29 /app/6x42bwop6jzim/init/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(26): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)
#30 /app/6x42bwop6jzim/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#31 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Framework\App\Http->launch()
#32 /app/6x42bwop6jzim/init/var/generation/Magento/Framework/App/Http/Interceptor.php(26): Magento\Framework\App\Http\Interceptor->___callPlugins('launch', Array, Array)
#33 /app/6x42bwop6jzim/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http\Interceptor->launch()
#34 /app/6x42bwop6jzim/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#35 {main}

Next Zend_Db_Statement_Exception: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '81174-244092' for key 'SALES_ORDER_TAX_ITEM_TAX_ID_ITEM_ID', query was: INSERT INTO `sales_order_tax_item` (`tax_id`, `item_id`, `tax_percent`, `amount`, `base_amount`, `real_amount`, `real_base_amount`, `associated_item_id`, `taxable_item_type`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) in /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php:237
Stack trace:
#0 /app/6x42bwop6jzim/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(95): Zend_Db_Statement_Pdo->_execute(Array)
#1 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Statement.php(303): Magento\Framework\DB\Statement\Pdo\Mysql->_execute(Array)
#2 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#3 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `sa...', Array)
#4 /app/6x42bwop6jzim/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(459): Zend_Db_Adapter_Pdo_Abstract->query('INSERT INTO `sa...', Array)
#5 /app/6x42bwop6jzim/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(514): Magento\Framework\DB\Adapter\Pdo\Mysql->_query('INSERT INTO `sa...', Array)
#6 /app/6x42bwop6jzim/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(576): Magento\Framework\DB\Adapter\Pdo\Mysql->query('INSERT INTO `sa...', Array)
#7 /app/6x42bwop6jzim/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(759): Zend_Db_Adapter_Abstract->insert('sales_order_tax...', Array)
#8 /app/6x42bwop6jzim/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(405): Magento\Framework\Model\ResourceModel\Db\AbstractDb->saveNewObject(Object(Magento\Sales\Model\Order\Tax\Item))
#9 /app/6x42bwop6jzim/vendor/magento/framework/Model/AbstractModel.php(631): Magento\Framework\Model\ResourceModel\Db\AbstractDb->save(Object(Magento\Sales\Model\Order\Tax\Item))
#10 /app/6x42bwop6jzim/vendor/magento/module-tax/Model/Plugin/OrderSave.php(186): Magento\Framework\Model\AbstractModel->save()
#11 /app/6x42bwop6jzim/vendor/magento/module-tax/Model/Plugin/OrderSave.php(46): Magento\Tax\Model\Plugin\OrderSave->saveOrderTax(Object(Magento\Sales\Model\Order\Interceptor))
#12 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(152): Magento\Tax\Model\Plugin\OrderSave->afterSave(Object(Magento\Sales\Model\OrderRepository\Interceptor), Object(Magento\Sales\Model\Order\Interceptor))
#13 /app/6x42bwop6jzim/init/var/generation/Magento/Sales/Model/OrderRepository/Interceptor.php(52): Magento\Sales\Model\OrderRepository\Interceptor->___callPlugins('save', Array, Array)
#14 /app/6x42bwop6jzim/vendor/magento/module-sales/Model/Service/OrderService.php(191): Magento\Sales\Model\OrderRepository\Interceptor->save(Object(Magento\Sales\Model\Order\Interceptor))
#15 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Sales\Model\Service\OrderService->place(Object(Magento\Sales\Model\Order\Interceptor))
#16 /app/6x42bwop6jzim/init/var/generation/Magento/Sales/Model/Service/OrderService/Interceptor.php(39): Magento\Sales\Model\Service\OrderService\Interceptor->___callPlugins('place', Array, Array)
#17 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/QuoteManagement.php(488): Magento\Sales\Model\Service\OrderService\Interceptor->place(Object(Magento\Sales\Model\Order\Interceptor))
#18 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/QuoteManagement.php(391): Magento\Quote\Model\QuoteManagement->submitQuote(Object(Magento\Quote\Model\Quote\Interceptor), Array)
#19 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/QuoteManagement.php(349): Magento\Quote\Model\QuoteManagement->submit(Object(Magento\Quote\Model\Quote\Interceptor))
#20 /app/6x42bwop6jzim/vendor/magento/module-quote/Model/GuestCart/GuestCartManagement.php(87): Magento\Quote\Model\QuoteManagement->placeOrder('405078', NULL)
#21 /app/6x42bwop6jzim/vendor/magento/module-checkout/Model/GuestPaymentInformationManagement.php(81): Magento\Quote\Model\GuestCart\GuestCartManagement->placeOrder('ced4065de257c32...')
#22 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Checkout\Model\GuestPaymentInformationManagement->savePaymentInformationAndPlaceOrder('ced4065de257c32...', 'customer.email@g...', Object(Magento\Quote\Model\Quote\Payment), Object(Magento\Quote\Model\Quote\Address\Interceptor))
#23 /app/6x42bwop6jzim/init/var/generation/Magento/Checkout/Model/GuestPaymentInformationManagement/Interceptor.php(26): Magento\Checkout\Model\GuestPaymentInformationManagement\Interceptor->___callPlugins('savePaymentInfo...', Array, Array)
#24 [internal function]: Magento\Checkout\Model\GuestPaymentInformationManagement\Interceptor->savePaymentInformationAndPlaceOrder('ced4065de257c32...', 'customer.email@g...', Object(Magento\Quote\Model\Quote\Payment), Object(Magento\Quote\Model\Quote\Address\Interceptor))
#25 /app/6x42bwop6jzim/vendor/magento/module-webapi/Controller/Rest.php(307): call_user_func_array(Array, Array)
#26 /app/6x42bwop6jzim/vendor/magento/module-webapi/Controller/Rest.php(216): Magento\Webapi\Controller\Rest->processApiRequest()
#27 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#28 /app/6x42bwop6jzim/init/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(26): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)
#29 /app/6x42bwop6jzim/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#30 /app/6x42bwop6jzim/vendor/magento/framework/Interception/Interceptor.php(146): Magento\Framework\App\Http->launch()
#31 /app/6x42bwop6jzim/init/var/generation/Magento/Framework/App/Http/Interceptor.php(26): Magento\Framework\App\Http\Interceptor->___callPlugins('launch', Array, Array)
#32 /app/6x42bwop6jzim/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http\Interceptor->launch()
#33 /app/6x42bwop6jzim/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#34 {main}

@maghamed maghamed self-requested a review February 27, 2017 13:25
@maghamed maghamed self-assigned this Feb 27, 2017
@okorshenko okorshenko added this to the March 2017 milestone Mar 1, 2017
@maghamed
Copy link
Contributor

maghamed commented Mar 1, 2017

@mimarcel good catch, it's always better to use identical operator than just check for equality !

@okorshenko okorshenko self-assigned this Mar 10, 2017
@magento-team magento-team merged commit a3ce1bc into magento:develop Mar 11, 2017
@okorshenko
Copy link
Contributor

@mimarcel thank you for your contribution!

@mimarcel mimarcel deleted the fix/SALES_ORDER_TAX_ITEM_TAX_ID_ITEM_ID_duplicates branch March 13, 2017 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants