Tax.php 3.83 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Weee\Model\ResourceModel;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Condition\ConditionInterface;

/**
 * Wee tax resource model
 *
 * @api
 * @since 100.0.2
 */
class Tax extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $dateTime;

    /**
     * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
     * @param \Magento\Framework\Stdlib\DateTime $dateTime
     * @param string $connectionName
     */
    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
        \Magento\Framework\Stdlib\DateTime $dateTime,
        $connectionName = null
    ) {
        $this->dateTime = $dateTime;
        parent::__construct($context, $connectionName);
    }

    /**
     * Resource initialization
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('weee_tax', 'value_id');
    }

    /**
     * Fetch one
     *
     * @param \Magento\Framework\DB\Select|string $select
     * @return string
     */
    public function fetchOne($select)
    {
        return $this->getConnection()->fetchOne($select);
    }

    /**
     * @param int $countryId
     * @param int $regionId
     * @param int $websiteId
     * @return boolean
     */
    public function isWeeeInLocation($countryId, $regionId, $websiteId)
    {
        // Check if there is a weee_tax for the country and region
        $attributeSelect = $this->getConnection()->select();
        $attributeSelect->from(
            $this->getTable('weee_tax'),
            'value'
        )->where(
            'website_id IN(?)',
            [$websiteId, 0]
        )->where(
            'country = ?',
            $countryId
        )->where(
            'state = ?',
            $regionId
        )->limit(
            1
        );

        $value = $this->getConnection()->fetchOne($attributeSelect);
        if ($value) {
            return true;
        }

        return false;
    }

    /**
     * @param int $countryId
     * @param int $regionId
     * @param int $websiteId
     * @param int $storeId
     * @param int $entityId
     * @return array[]
     */
    public function fetchWeeeTaxCalculationsByEntity($countryId, $regionId, $websiteId, $storeId, $entityId)
    {
        $attributeSelect = $this->getConnection()->select();
        $attributeSelect->from(
            ['eavTable' => $this->getTable('eav_attribute')],
            ['eavTable.attribute_code', 'eavTable.attribute_id', 'eavTable.frontend_label']
        )->joinLeft(
            ['eavLabel' => $this->getTable('eav_attribute_label')],
            'eavLabel.attribute_id = eavTable.attribute_id and eavLabel.store_id = ' .((int) $storeId),
            'eavLabel.value as label_value'
        )->joinInner(
            ['weeeTax' => $this->getTable('weee_tax')],
            'weeeTax.attribute_id = eavTable.attribute_id',
            'weeeTax.value as weee_value'
        )->where(
            'eavTable.frontend_input = ?',
            'weee'
        )->where(
            'weeeTax.website_id IN(?)',
            [$websiteId, 0]
        )->where(
            'weeeTax.country = ?',
            $countryId
        )->where(
            'weeeTax.state IN(?)',
            [$regionId, 0]
        )->where(
            'weeeTax.entity_id = ?',
            (int)$entityId
        );

        $order = ['weeeTax.state ' . \Magento\Framework\DB\Select::SQL_DESC,
            'weeeTax.website_id ' . \Magento\Framework\DB\Select::SQL_DESC];
        $attributeSelect->order($order);

        $values = $this->getConnection()->fetchAll($attributeSelect);

        if ($values) {
            return $values;
        }

        return [];
    }
}