0

I am adding few custom fields in Tax rate form in admin as below

For this I have overrided below form in di.xml

/app/code/Company/GST/etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

        <preference for="Magento\Tax\Block\Adminhtml\Rate\Form" type="Company\GST\Block\Magento\Tax\Adminhtml\Rate\Form" />

    </config>

Schema file

    <?php

    namespace Company\GST\Setup;

    use Magento\Framework\Setup\InstallSchemaInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;

    class InstallSchema implements InstallSchemaInterface
    {
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $installer = $setup;

            $installer->startSetup();


            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'hsn_title',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => true,
                    'default' => null,
                    'comment' => 'HSN Code'
                ]
            );
            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'igst',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                    'nullable' => false,
                    'LENGTH' =>'12,4',
                    'visible'   => false,
                    'comment' => 'igst'
                ]
            );

            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'cgst',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                    'nullable' => false,
                    'LENGTH' =>'12,4',
                    'visible'   => false,
                    'comment' => 'cgst'
                ]
            );
            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'sgst',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                    'nullable' => false,
                    'LENGTH' =>'12,4',
                    'visible'   => false,
                    'comment' => 'sgst'
                ]
            );
            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'cess',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                    'nullable' => false,
                    'LENGTH' =>'12,4',
                    'visible'   => false,
                    'comment' => 'cess'
                ]
            );
            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'price_slab_type',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    'LENGTH' => 10,
                    'unsigned' => true, 
                    'nullable' => false, 
                    'comment' => 'price slab type'
                ]
            );
            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'price_from',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                    'nullable' => false,
                    'LENGTH' =>'12,4',
                    'visible'   => false,
                    'comment' => 'price_from'
                ]
            );
            $installer->getConnection()->addColumn(
                $setup->getTable('tax_calculation_rate'),
                'price_to',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,
                    'nullable' => false,
                    'LENGTH' =>'12,4',
                    'visible'   => false,
                    'comment' => 'price_to'
                ]
            );


            $installer->endSetup();
        }
    }

This is my form with new fields

    <?php

    namespace Company\GST\Block\Magento\Tax\Adminhtml\Rate;

    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Tax\Controller\RegistryConstants;
    // use Magento\Directory\Model\RegionFactory;


    class Form extends \Magento\Tax\Block\Adminhtml\Rate\Form
    {   

        const FORM_ELEMENT_ID = 'rate-form';

        /**
         * @var null
         */
        protected $_titles = null;

        /**
         * Tax data
         *
         * @var \Magento\Tax\Helper\Data|null
         */
        protected $_taxData = null;

        /**
         * @var \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory
         */
        protected $_fieldsetFactory;

        /**
         * @var \Magento\Directory\Model\Config\Source\Country
         */
        protected $_country;

        /**
         * @var \Magento\Directory\Model\RegionFactory
         */
        protected $_regionFactory;

        /**
         * @var \Magento\Tax\Api\TaxRateRepositoryInterface
         */
        protected $_taxRateRepository;

        /**
         * @var \Magento\Tax\Model\TaxRateCollection
         */
        protected $_taxRateCollection;

        /**
         * @var \Magento\Tax\Model\Calculation\Rate\Converter
         */
        protected $_taxRateConverter;

        /**
         * @param \Magento\Backend\Block\Template\Context $context
         * @param \Magento\Framework\Registry $registry
         * @param \Magento\Framework\Data\FormFactory $formFactory
         * @param \Magento\Directory\Model\RegionFactory $regionFactory
         * @param \Magento\Directory\Model\Config\Source\Country $country
         * @param \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory
         * @param \Magento\Tax\Helper\Data $taxData
         * @param \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository
         * @param \Magento\Tax\Model\TaxRateCollection $taxRateCollection
         * @param \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter
         * @param array $data
         * @SuppressWarnings(PHPMD.ExcessiveParameterList)
         */
        public function __construct(
            \Magento\Backend\Block\Template\Context $context,
            \Magento\Framework\Registry $registry,
            \Magento\Framework\Data\FormFactory $formFactory,
            \Magento\Directory\Model\RegionFactory $regionFactory,
            \Magento\Directory\Model\Config\Source\Country $country,
            \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory,
            \Magento\Tax\Helper\Data $taxData,
            \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
            \Magento\Tax\Model\TaxRateCollection $taxRateCollection,
            \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter,
            array $data = []
        ) {
            $this->_country = $country;
            $this->_fieldsetFactory = $fieldsetFactory;
            $this->_taxData = $taxData;
            $this->_taxRateRepository = $taxRateRepository;
            $this->_regionFactory = $regionFactory;
            $this->_taxRateCollection = $taxRateCollection;
            $this->_taxRateConverter = $taxRateConverter;
            parent::__construct($context, $registry, $formFactory, $data);
        }


        /**
         * @return void
         */
        protected function _construct()
        {
            parent::_construct();
            $this->setDestElementId(self::FORM_ELEMENT_ID);
        }


        public function _prepareForm()
        {
            parent::_prepareForm();


            // $gstEnabled = $this->_gstHelper->enableGstModule();
            $gstEnabled = 1;

            if($gstEnabled){
                $form = $this->getForm();
                $fieldset = $form->addFieldset('new_fieldset',
                        array('legend'=>'GST Tax Rate Information')
                );
                $fieldset->addField('hsn_title', 'text',
                    array(
                        'label' => 'HSN Code',
                        'class' => 'input-text',
                        'name'  => 'hsn_title',
                        'required' => false
                    )
                );

                $fieldset->addField('igst', 'text',
                    array(
                        'label' => 'IGST Rate Percent',
                        'class' => 'validate-not-negative-number',
                        'name'  => 'igst',
                        'required' => false
                    )
                );

                $fieldset->addField('cgst', 'text',
                    array(
                        'label' => 'CGST Rate Percent',
                        'class' => 'validate-not-negative-number',
                        'name'  => 'cgst',
                        'required' => false
                    )
                );

                $fieldset->addField('sgst', 'text',
                    array(
                        'label' => 'SGST Rate Percent',
                        'class' => 'validate-not-negative-number',
                        'name'  => 'sgst',
                        'required' => false
                    )
                );

                $fieldset->addField('cess', 'text',
                    array(
                        'label' => 'CESS Rate Percent',
                        'class' => 'validate-not-negative-number',
                        'name'  => 'cess',
                        'required' => false
                    )
                );

                $price_slab_type = [
                                    ['value' => '1', 'label' => __('MRP')],
                                    ['value' => '2', 'label' => __('Special Price')]
                                ];
                $fieldset->addField(
                    'price_slab_type',
                    'select',
                    ['name' => 'price_slab_type', 'label' => __('Price Slab Type'), 'values' => $price_slab_type]
                );

                $fieldset->addField('price_from', 'text',
                    array(
                        'label' => 'Price From',
                        'class' => 'validate-not-negative-number',
                        'name'  => 'price_from',
                        'required' => false
                    )
                );

                $fieldset->addField('price_to', 'text',
                    array(
                        'label' => 'Price To',
                        'class' => 'validate-not-negative-number',
                        'name'  => 'price_to',
                        'required' => false
                    )
                );

                // $form->setValues($formData);
                $this->setForm($form);
                return $this;
            }

            return false;
        }
    }

and After compilation I get below error -

Errors during compilation:

Company\GST\Block\Magento\Tax\Adminhtml\Rate\Form Incompatible argument type: Required type: \Magento\Directory\Model\RegionFactory. Actual type: array; File: /var/www/html/magento226/app/code/Company/GST/Block/Magento/Tax/Adminhtml/Rate/Form.php

1 Answer 1

0

I have resolved this issue. I have replaced below code and its resolved.

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Data\FormFactory $formFactory,
    \Magento\Directory\Model\RegionFactory $regionFactory,
    \Magento\Directory\Model\Config\Source\Country $country,
    \Magento\Tax\Block\Adminhtml\Rate\Title\FieldsetFactory $fieldsetFactory,
    \Magento\Tax\Helper\Data $taxData,
    \Magento\Tax\Api\TaxRateRepositoryInterface $taxRateRepository,
    \Magento\Tax\Model\TaxRateCollection $taxRateCollection,
    \Magento\Tax\Model\Calculation\Rate\Converter $taxRateConverter,
    array $data = []
    ) {
    parent::__construct($context, $registry, $formFactory, $regionFactory, $country, $fieldsetFactory, $taxData, $taxRateRepository, $taxRateCollection, $taxRateConverter, $data);

}
1
  • We have added a new field but I am not able to save in DB?can u help me
    – Devidas
    Commented Sep 30, 2021 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.