Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/Core/Setup/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/Core/Setup/Taxes.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Hervé Guétin <[email protected]> <@herveguetin>
 * @copyright Copyright (c) 2016 Agence Soon (http://www.agence-soon.fr)
 */
declare(strict_types=1);

namespace Soon\Core\Setup;

use Magento\Config\Model\ResourceModel\Config;
use Magento\Config\Model\ResourceModel\ConfigFactory;
use Magento\Directory\Model\ResourceModel\Country\Collection;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Module\Setup;
use Magento\Framework\Module\SetupFactory;
use Magento\Tax\Model\ClassModel;

class Taxes
{
    /**
     * @var Setup
     */
    private $setup;

    /**
     * @var array
     */
    private $taxTables = [
        'tax_calculation',
        'tax_class',
        'tax_calculation_rate',
        'tax_calculation_rate_title',
        'tax_calculation_rule',
    ];

    /**
     * @var array
     */
    private $taxClasses = [
        'product_20' =>
            [
                'class_id' => 2,
                'class_name' => 'Biens et services soumis à la TVA à 20%',
                'class_type' => ClassModel::TAX_CLASS_TYPE_PRODUCT
            ],
        'customer_user' =>
            [
                'class_id' => 3,
                'class_name' => 'Particuliers',
                'class_type' => ClassModel::TAX_CLASS_TYPE_CUSTOMER
            ],
        'product_10' =>
            [
                'class_id' => 4,
                'class_name' => 'Biens et services soumis à la TVA à 10%',
                'class_type' => ClassModel::TAX_CLASS_TYPE_PRODUCT
            ],
        'product_5.5' =>
            [
                'class_id' => 5,
                'class_name' => 'Biens et services soumis à la TVA à 5,5%',
                'class_type' => ClassModel::TAX_CLASS_TYPE_PRODUCT
            ],
    ];

    /**
     * @var array
     */
    private $taxRules = [
        [
            'tax_calculation_rule_id' => 1,
            'code' => 'Particuliers - CE - 20%',
            'priority' => 1,
            'position' => 1,
            'rate' => '20' // for installer use, not used in DB
        ],
        [
            'tax_calculation_rule_id' => 2,
            'code' => 'Particuliers - CE - 10%',
            'priority' => 2,
            'position' => 2,
            'rate' => '10' // for installer use, not used in DB
        ],
        [
            'tax_calculation_rule_id' => 3,
            'code' => 'Particuliers - CE - 5,5%',
            'priority' => 3,
            'position' => 3,
            'rate' => '5.5' // for installer use, not used in DB
        ]
    ];
    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;
    /**
     * @var Collection
     */
    private $countryCollection;
    /**
     * @var Config
     */
    private $configResource;

    /**
     * Taxes constructor.
     * @param SetupFactory $setupFactory
     * @param ScopeConfigInterface $scopeConfig
     * @param CollectionFactory $countryCollectionFactory
     * @param ConfigFactory $configFactory
     */
    public function __construct(
        SetupFactory $setupFactory,
        ScopeConfigInterface $scopeConfig,
        CollectionFactory $countryCollectionFactory,
        ConfigFactory $configFactory
    ) {
        $this->setup = $setupFactory->create();
        $this->scopeConfig = $scopeConfig;
        $this->countryCollection = $countryCollectionFactory->create();
        $this->configResource = $configFactory->create();
    }

    /**
     * Install exposed method
     */
    public function install(): void
    {
        $this->cleanTables();
        $this->installTaxClasses();
        $this->installTaxCalculationRules();
        $this->installCalculations();
        $this->setDiscountTaxConfig();
    }

    /**
     * Clean tax tables
     */
    private function cleanTables(): void
    {
        array_map(function ($taxTable) {
            $this->setup->getConnection()->truncateTable($this->setup->getTable($taxTable));
        }, $this->taxTables);
    }

    /**
     * Install tax classes
     */
    private function installTaxClasses(): void
    {
        array_map(function ($row) {
            $this->setup->getConnection()->insertForce($this->setup->getTable('tax_class'), $row);
        }, $this->taxClasses);
    }

    /**
     * Install tax calculation rules
     */
    private function installTaxCalculationRules(): void
    {
        array_map(function ($taxRule) {
            // To avoid Column not found: 1054 Unknown column 'rate' in 'field list' error when inserting in DB
            unset($taxRule['rate']);
            $this->setup->getConnection()->insertForce($this->setup->getTable('tax_calculation_rule'), $taxRule);
        }, $this->taxRules);
    }

    /**
     * Install tax calculations
     */
    private function installCalculations(): void
    {
        $euCountries = explode(',', $this->scopeConfig->getValue('general/country/eu_countries'));
        $countries = $this->countryCollection
            ->addFieldToFilter('country_id', ['in' => $euCountries])
            ->toOptionArray(false);

        $taxRateId = 0;
        foreach ($countries as $k => $country) {
            foreach ($this->taxRules as $taxRule) {
                $taxRateId++;
                $rate = [
                    'tax_calculation_rate_id' => $taxRateId,
                    'tax_country_id' => $country['value'],
                    'tax_region_id' => '*',
                    'tax_postcode' => '*',
                    'code' => $country['value'] . " - {$taxRule['rate']}%",
                    'rate' => $taxRule['rate']
                ];

                $this->setup->getConnection()->insertForce($this->setup->getTable('tax_calculation_rate'), $rate);

                $calculation = [
                    'tax_calculation_rate_id' => $taxRateId,
                    'tax_calculation_rule_id' => $taxRule['tax_calculation_rule_id'],
                    'customer_tax_class_id' => 3,
                    'product_tax_class_id' => $this->taxClasses['product_' . $taxRule['rate']]['class_id']
                ];

                $this->setup->getConnection()->insertForce($this->setup->getTable('tax_calculation'), $calculation);
            }
        }
    }

    /**
     * Save some config to core_config_data
     * @see /config/default/tax/calculation/discount_tax node in vendor/soon/module-core/etc/config.xml
     */
    private function setDiscountTaxConfig(): void
    {
        $this->configResource->saveConfig('tax/calculation/discount_tax', 1, 'default', 0);
    }
}

Spamworldpro Mini