TaxCode.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\ResourceModel;
  7. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  8. use Magento\Framework\Model\ResourceModel\Db\Context;
  9. use Vertex\Tax\Model\ExceptionLogger;
  10. /**
  11. * Performs Datastore-related actions for the TaxCode repository
  12. */
  13. class TaxCode extends AbstractDb
  14. {
  15. const FIELD_ID = 'item_id';
  16. const FIELD_TAX_CODE = 'tax_code';
  17. const TABLE = 'vertex_sales_order_item_tax_code';
  18. /** @var ExceptionLogger */
  19. private $logger;
  20. /** @var null|string */
  21. private $table;
  22. /**
  23. * @param Context $context
  24. * @param ExceptionLogger $logger
  25. * @param null|string $table
  26. */
  27. public function __construct(Context $context, ExceptionLogger $logger, $table = self::TABLE)
  28. {
  29. $this->table = $table;
  30. $this->logger = $logger;
  31. parent::__construct($context);
  32. }
  33. /**
  34. * @inheritdoc
  35. *
  36. * MEQP2 Warning: Protected method. Needed to override AbstractDb's _construct
  37. */
  38. protected function _construct()
  39. {
  40. $this->_isPkAutoIncrement = false;
  41. $this->_init($this->table ?: self::TABLE, static::FIELD_ID);
  42. }
  43. /**
  44. * Retrieve rows from db by order item id array
  45. *
  46. * @param int[] $itemIdArray
  47. * @return string[]
  48. */
  49. public function getTaxCodeByItemIdArray(array $itemIdArray)
  50. {
  51. $returnArray = [];
  52. if (!count($itemIdArray)) {
  53. return $returnArray;
  54. }
  55. try {
  56. $select = $this->getConnection()
  57. ->select()
  58. ->from($this->getMainTable())
  59. ->where(self::FIELD_ID . ' IN (?)', $itemIdArray);
  60. foreach ($this->getConnection()->fetchAll($select) as $resultArray) {
  61. $returnArray[reset($resultArray)] = end($resultArray);
  62. }
  63. } catch (\Exception $exception) {
  64. $this->logger->critical($exception);
  65. }
  66. return $returnArray;
  67. }
  68. /**
  69. * Store array of data to preferred attribute resource
  70. *
  71. * @param array $insertData
  72. * @return void
  73. */
  74. public function saveMultiple(array $insertData)
  75. {
  76. $connection = $this->getConnection();
  77. $connection->beginTransaction();
  78. try {
  79. $processed = array_map('array_pop', $insertData);
  80. $connection->insertMultiple($this->getTable($this->table ?: self::TABLE), $processed);
  81. $connection->commit();
  82. } catch (\Exception $exception) {
  83. $this->logger->critical($exception);
  84. $connection->rollBack();
  85. }
  86. }
  87. }