VertexTaxCode.php 2.7 KB

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