InvoiceTextCode.php 2.73 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Vertex\Tax\Model\ExceptionLogger;

/**
 * Performs Datastore-related actions for the InvoiceTextCode repository
 */
class InvoiceTextCode extends AbstractDb
{
    const FIELD_CODE = 'invoice_text_code';
    const FIELD_ID = 'item_id';
    const TABLE = 'vertex_sales_order_item_invoice_text_code';

    /** @var ExceptionLogger */
    private $logger;

    /** @var null|string */
    private $table;

    /**
     * @param Context $context
     * @param ExceptionLogger $logger
     * @param null|string $table
     */
    public function __construct(Context $context, ExceptionLogger $logger, $table = self::TABLE)
    {
        $this->table = $table;
        $this->logger = $logger;
        parent::__construct($context);
    }

    /**
     * @inheritdoc
     *
     * MEQP2 Warning: Protected method.  Needed to override AbstractDb's _construct
     */
    protected function _construct()
    {
        $this->_isPkAutoIncrement = false;
        $this->_init($this->table ?: self::TABLE, static::FIELD_ID);
    }

    /**
     * Retrieve rows from db by order item id array
     *
     * @param int[] $itemIdArray
     * @return string[]
     */
    public function getInvoiceTextCodeByItemIdArray(array $itemIdArray)
    {
        $returnArray = [];

        if (!count($itemIdArray)) {
            return $returnArray;
        }

        try {
            $select = $this->getConnection()
                ->select()
                ->from($this->getMainTable())
                ->where(self::FIELD_ID . ' IN (?)', $itemIdArray);

            foreach ($this->getConnection()->fetchAll($select) as $resultArray) {
                $returnArray[reset($resultArray)] = end($resultArray);
            }
        } catch (\Exception $exception) {
            $this->logger->critical($exception);
        }

        return $returnArray;
    }

    /**
     * Store array of data to preferred attribute resource
     *
     * @param array $insertData
     * @return void
     */
    public function saveMultiple(array $insertData)
    {
        $connection = $this->getConnection();
        $connection->beginTransaction();
        try {
            $processed = array_map('array_pop', $insertData);
            $connection->insertMultiple($this->getTable($this->table ?: self::TABLE), $processed);
            $connection->commit();
        } catch (\Exception $exception) {
            $this->logger->critical($exception);
            $connection->rollBack();
        }
    }
}