Jump to content

Update the VAT value in the configuration screen of the module.


Recommended Posts

hi, I am designing a module to calculate the VAT between two dates. I already have everything working, it already calculates the VAT perfectly, but now I want it to be displayed on the configuration screen of the module when clicking on save settings. It's the only thing I'm missing. Right now if I want the VAT to be shown I have to exit and re-enter the module configuration screen. Attached the complete code of the module. Right now where I try to update the VAT field is in the postProcess() function, with this line:Configuration::updateValue($this->name . '_iva', $ivaAcumulado);

<?php


if (!defined('_PS_VERSION_')) {
    exit;
}

class moduloResumenIva extends Module {

    protected $config_form = false;

    public function __construct() {
        $this->name = 'moduloResumenIva';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'luilli';
        $this->need_instance = 0;

        /**
         * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
         */
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('moduloResumenIva');
        $this->description = $this->l('mi nuevo modulo mi nuevo modulomi nuevo modulomi nuevo modulomi nuevo modulo');

        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
    }

    /**
     * Don't forget to create update methods if needed:
     * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
     */
    public function install() {
        Configuration::updateValue('MIMODULOMISMADB_LIVE_MODE', false);

        return parent::install() &&
                $this->registerHook('header') &&
                $this->registerHook('actionPaymentConfirmation') &&
                $this->registerHook('backOfficeHeader');
    }

    public function uninstall() {
        Configuration::deleteByName('MIMODULOMISMADB_LIVE_MODE');

        return parent::uninstall();
    }

    /**
     * Load the configuration form
     */
    public function getContent() {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool) Tools::isSubmit('submitButton')) == true) {
            $this->postProcess();
        }

        $this->context->smarty->assign('module_dir', $this->_path);

        $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');

        return $output . $this->renderForm();
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    protected function renderForm() {

        $values = array();
        $this->fields_form = array();
        $this->context->controller->addJqueryUI('ui.datepicker');
        $defaultDate = date('Y-m-d');
        if (!Configuration::get($this->name . 'my_date_desde')) {
            $values['my_date_desde'] = Tools::getValue('my_date_desde', $defaultDate);
        } else {
            $values['my_date_desde'] = Tools::getValue('my_date_desde', Configuration::get($this->name . '_my_date_desde'));
        }
        if (!Configuration::get($this->name . 'my_date_hasta')) {
            $values['my_date_hasta'] = Tools::getValue('my_date_hasta', $defaultDate);
        } else {
            $values['my_date_hasta'] = Tools::getValue('my_date_hasta', Configuration::get($this->name . '_my_date_hasta'));
        }
        $values['iva'] = Tools::getValue('iva', Configuration::get($this->name . '_iva'));
        
        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
        $helper->default_form_language = $lang->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
        $helper->identifier = $this->identifier;
        $helper->submit_action = 'Submit' . $this->name;
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->tpl_vars = array(
            'fields_value' => $values,
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );
        return $helper->generateForm(array($this->getConfigForm()));

    }

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm() {

        return array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'date',
                        'label' => $this->l('Desde'),
                        'name' => 'my_date_desde',
                        'required' => true,
                    ),
                    array(
                        'type' => 'date',
                        'label' => $this->l('Hasta'),
                        'name' => 'my_date_hasta',
                        'required' => true,
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Iva'),
                        'name' => 'iva',
                    )
                ),
                
                'submit' => array(
                    'title' => $this->l('Save settings'),
                    'class' => 'button btn btn-default pull-right',
                    'name' => 'submitButton',
                )
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues() {
        return array(
            'CAMPOID' => Configuration::get('CAMPOID', null),
            'MIMODULOMISMADB_ACCOUNT_USUARIO' => Configuration::get('MIMODULOMISMADB_ACCOUNT_USUARIO', null),
            'MIMODULOMISMADB_ACCOUNT_PASSWORD' => Configuration::get('MIMODULOMISMADB_ACCOUNT_PASSWORD', null),
            'my_date_desde' => Configuration::get('my_date_desde', null),
            'my_date_hasta' => Configuration::get('my_date_hasta', null),
            'iva' => Configuration::get('iva', null),
        );
    }

    /**
     * Save form data.
     */
    protected function postProcess() {

        if (Tools::isSubmit('Submit' . $this->name)) {
            if (Tools::getValue('my_date_desde')) {
                Configuration::updateValue($this->name . '_my_date_desde', Tools::getValue('my_date_desde'));
            }
            if (Tools::getValue('my_date_hasta')) {
                Configuration::updateValue($this->name . '_my_date_hasta', Tools::getValue('my_date_hasta'));
            }
            $fechaDesde = Configuration::get($this->name . '_my_date_desde', null) . " 00:00:00";
            $fechaHasta = Configuration::get($this->name . '_my_date_hasta', null) . " 00:00:00";
            $db = \Db::getInstance();
            $sql = "select * from orders where date_add BETWEEN '" . $fechaDesde . "' AND '" . $fechaHasta . "'";
            $ivaAcumulado = 0;
            $result = $db->executeS($sql);
            foreach ($result as $row) {
                $ivaAcumulado += $row["total_paid_tax_incl"] - $row["total_paid_tax_excl"];
            }
            Configuration::updateValue($this->name . '_iva', $ivaAcumulado);
        }
    }

    /**
     * Add the CSS & JavaScript files you want to be loaded in the BO.
     */
    public function hookBackOfficeHeader() {
        if (Tools::getValue('module_name') == $this->name) {
            $this->context->controller->addJS($this->_path . 'views/js/back.js');
            $this->context->controller->addCSS($this->_path . 'views/css/back.css');
        }
    }

    /**
     * Add the CSS & JavaScript files you want to be added on the FO.
     */
    public function hookHeader() {
        //mail("[email protected]", "hola", "viva el vino");
        $this->context->controller->addJS($this->_path . '/views/js/front.js');
        $this->context->controller->addCSS($this->_path . '/views/css/front.css');
        
    }

}

an here's an screenshot of the configuration screen:

 

Captura desde 2023-01-13 22-55-10.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...