Your IP : 127.0.0.1


Current Path : /home/dev2.destoffenstraat.com/app/code/Geissweb/Euvat-old/Helper/
Upload File :
Current File : /home/dev2.destoffenstraat.com/app/code/Geissweb/Euvat-old/Helper/Setup.php

<?php
/**
 * ||GEISSWEB| EU VAT Enhanced
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the GEISSWEB End User License Agreement
 * that is available through the world-wide-web at this URL: https://www.geissweb.de/legal-information/eula
 *
 * DISCLAIMER
 *
 * Do not edit this file if you wish to update the extension in the future. If you wish to customize the extension
 * for your needs please refer to our support for more information.
 *
 * @copyright   Copyright (c) 2015 GEISS Weblösungen (https://www.geissweb.de)
 * @license     https://www.geissweb.de/legal-information/eula GEISSWEB End User License Agreement
 */

namespace Geissweb\Euvat\Helper;

use Geissweb\Euvat\Helper\VatNumber\Formatter;
use Geissweb\Euvat\Model\Setup\Tax;
use Magento\Directory\Api\CountryInformationAcquirerInterface;
use Magento\Framework\App\Cache\Manager;
use Magento\Tax\Model\ClassModel;

/**
 * Class Setup
 */
class Setup
{
    /**
     * @var Functions
     */
    public $functionsHelper;

    /**
     * @var Manager
     */
    public $cacheManager;

    /**
     * @var CountryInformationAcquirerInterface
     */
    public $countryInformation;
    /**
     * @var Formatter
     */
    private $formatter;
    /**
     * @var \Geissweb\Euvat\Model\Setup\RatesCollector
     */
    private $ratesCollector;

    /**
     * Configuration constructor.
     *
     * @param Functions $functionsHelper
     * @param Formatter $formatter
     * @param Manager $cacheManager
     * @param CountryInformationAcquirerInterface $countryInformationAcquirer
     * @param \Geissweb\Euvat\Model\Setup\RatesCollector $ratesCollector
     */
    public function __construct(
        Functions $functionsHelper,
        Formatter $formatter,
        Manager $cacheManager,
        CountryInformationAcquirerInterface $countryInformationAcquirer,
        \Geissweb\Euvat\Model\Setup\RatesCollector $ratesCollector
    ) {
        $this->functionsHelper = $functionsHelper;
        $this->cacheManager = $cacheManager;
        $this->countryInformation = $countryInformationAcquirer;
        $this->formatter = $formatter;
        $this->ratesCollector = $ratesCollector;
    }

    /**
     * Clear Config Cache
     */
    public function clearConfigCache()
    {
        $this->cacheManager->clean(['config']);
    }

    /**
     * Compiles import array for store config value changes
     *
     * @param string $baseCc
     * @param string $key
     * @param string $vatId
     * @param string $installType
     * @param bool   $withRules
     *
     * @return array
     */
    public function getStoreConfigValues($baseCc, $key, $vatId, $installType, $withRules = false)
    {
        $config = [
            'general/country/default'          => $baseCc,
            'general/store_information/country_id' => $baseCc,
            'general/store_information/merchant_vat_number' => $this->formatter->formatVatNumber($vatId),
            'tax/classes/shipping_tax_class'   => Tax::TC_PRODUCTS_SHIPPING_STANDARD,
            'tax/calculation/based_on'         => 'shipping',
            'tax/defaults/country'             => $baseCc,
            'shipping/origin/country_id'       => $baseCc,
            'customer/create_account/auto_group_assign' => 0,
            'customer/create_account/vat_frontend_visibility' => 1,
            'customer/address/taxvat_show'     => 0,
            'euvat/mod_info/license_key'       => $key,
            'euvat/mod_info/installation_type' => $installType,
            'euvat/interface_settings/requester_vat_number' => $this->formatter->formatVatNumber($vatId),
            'euvat/vat_settings/domestic_country' => $baseCc
        ];

        if ($withRules) {
            $config['euvat/vat_settings/tax_class_including'] =
                Tax::TC_CUSTOMER_CONSUMER;
            $config['euvat/vat_settings/tax_class_including_business'] =
                Tax::TC_CUSTOMER_BUSINESS_DOMESTIC;
            $config['euvat/vat_settings/tax_class_excluding_business'] =
                Tax::TC_CUSTOMER_BUSINESS_EU;

            $config['euvat/vat_settings/reduced_product_class'] = Tax::TC_PRODUCTS_REDUCED;
            $config['euvat/vat_settings/reduced_shipping_class'] = Tax::TC_PRODUCTS_SHIPPING_REDUCED;
        }

        if ($this->functionsHelper->configHelper->isDebugEnabled()) {
            $this->functionsHelper->logger->debug("Compiled new config settings:");
            $this->functionsHelper->logger->debug(var_export($config, true));
        }

        return $config;
    }

    /**
     * Returns array with data for creating tax classes
     * @return array
     */
    public function getTaxClassesInsertArray($withDigital)
    {
        $insert = [];
        foreach ($this->getNewProductTaxClasses($withDigital) as $key => $class) {
            $insert[] = [
                'class_id'  =>$key,
                'class_name'=>$class,
                'class_type'=> ClassModel::TAX_CLASS_TYPE_PRODUCT
            ];
        }
        foreach ($this->getNewCustomerTaxClasses() as $key => $class) {
            $insert[] = [
                'class_id'=>$key,
                'class_name'=>$class,
                'class_type'=> ClassModel::TAX_CLASS_TYPE_CUSTOMER
            ];
        }
        return $insert;
    }

    /**
     * Required product tax classes
     * @return array
     */
    public function getNewProductTaxClasses($withDigital = false)
    {
        $classes = [];
        $classes[Tax::TC_PRODUCTS_STANDARD] = __('Products with standard VAT rate');
        $classes[Tax::TC_PRODUCTS_REDUCED] = __('Products with reduced VAT rate');
        $classes[Tax::TC_PRODUCTS_SHIPPING_STANDARD] = __('Shipping with standard VAT rate');
        $classes[Tax::TC_PRODUCTS_SHIPPING_REDUCED] = __('Shipping with reduced VAT rate');
        if ($withDigital) {
            $classes[Tax::TC_PRODUCTS_DIGITAL] = __('Digital products and services');
        }
        return $classes;
    }

    /**
     * Required product tax classes
     * @return array
     */
    public function getNewProductTaxClassesAsOptionArray()
    {
        return [
            ['value' => 0, 'label' => __('None')],
            ['value' => Tax::TC_PRODUCTS_STANDARD, 'label' => __('Products with standard VAT rate')],
            ['value' => Tax::TC_PRODUCTS_REDUCED, 'label' => __('Products with reduced VAT rate')],
            ['value' => Tax::TC_PRODUCTS_SHIPPING_STANDARD, 'label' => __('Shipping with standard VAT rate')],
            ['value' => Tax::TC_PRODUCTS_SHIPPING_REDUCED, 'label' => __('Shipping with reduced VAT rate')],
            ['value' => Tax::TC_PRODUCTS_DIGITAL, 'label' => __('Digital products and services')]
        ];
    }

    /**
     * Required customer tax classes
     * @return array
     */
    public function getNewCustomerTaxClasses()
    {
        return [
            Tax::TC_CUSTOMER_CONSUMER => __('Consumers incl. VAT'),
            Tax::TC_CUSTOMER_BUSINESS_DOMESTIC => __('Businesses incl. VAT'),
            Tax::TC_CUSTOMER_BUSINESS_EU => __('Businesses excl. VAT')
        ];
    }

    /**
     * Required customer tax classes
     * @return array
     */
    public function getNewCustomerTaxClassesAsOptionArray()
    {
        return [
            ['value' => Tax::TC_CUSTOMER_CONSUMER, 'label' => __('Consumers incl. VAT')],
            ['value' => Tax::TC_CUSTOMER_BUSINESS_DOMESTIC, 'label' => __('Businesses incl. VAT')],
            ['value' => Tax::TC_CUSTOMER_BUSINESS_EU, 'label' => __('Businesses excl. VAT')]
        ];
    }

    /**
     * Gets the standard VAT rates for all EU countires
     * Source: https://ec.europa.eu/taxation_customs/dds2/taric/taric_consultation.jsp?Lang=en
     * @return array
     */
    public function getStandardRates()
    {
        return $this->ratesCollector->getStandardRates();
    }

    /**
     * Compiles an array with tax rates to be inserted into the database
     *
     * @param string $baseCc
     * @param bool   $withReduced
     * @param null   $reducedRate
     * @param bool   $withNonEu
     * @param bool   $addUk
     *
     * @return array
     */
    public function getTaxRatesInsertArray(
        $baseCc,
        $useMSRate = true,
        $withDigital = false,
        $withReduced = false,
        $reducedRate = null
    ) {
        $standardRates = $this->getStandardRates();
        $standardRate = (float)$standardRates[$baseCc];
        $insertRates = [];

        foreach ($standardRates as $cC => $rate) {

            $insertRates[] = [
                'tax_country_id'=> $cC,
                'tax_postcode' => "*",
                'code' => $cC . __(" standard VAT"),
                'rate' => ($useMSRate || $cC === 'GB') ? (float)$rate : (float)$standardRates[$baseCc]
            ];



            if ($withReduced && $reducedRate != null) {
                $insertRates[] = [
                    'tax_country_id'=> $cC,
                    'tax_postcode' => "*",
                    'code' => $cC . __(" reduced VAT"),
                    'rate' => (float)$reducedRate
                ];
            }

            if ($withDigital) {
                $insertRates[] = [
                    'tax_country_id'=> $cC,
                    'tax_postcode' => "*",
                    'code' => $cC . __(" digital VAT"),
                    'rate' => (float)$rate
                ];
            }

            $insertRates[] = [
                'tax_country_id'=> $cC,
                'tax_postcode' => "*",
                'code' => $cC . __(" VAT zero/exempt"),
                'rate' => 0.0000
            ];
        }

        // Northern Ireland
        $insertRates[] = [
            'tax_country_id'=> 'GB',
            'tax_postcode' => "BT*",
            'code' => __('GB-NI') . __(" standard VAT"),
            'rate' => (float)$standardRate
        ];
        if ($withReduced && $reducedRate != null) {
            $insertRates[] = [
                'tax_country_id'=> 'GB',
                'tax_postcode' => "BT*",
                'code' => __('GB-NI') . __(" reduced VAT"),
                'rate' => (float)$reducedRate
            ];
        }

        return $insertRates;
    }
}