<?php

class ControllerPaymentGeralConfigs extends Controller
{
    public function index()
    {
        $this->document->addScript(array(
            'public/js/payment/payment_configs.js'
        ));

        $this->document->addTypesForJs(array(
            Type_ConfigParcelamento::getTypeForJs()
        ));

        $this->load->model('setting/setting');
        $this->load->helper('ftr_geral');
        $this->load->type('tipocomum');

        $this->data['Type_ConfigParcelamento'] = $this->load->typeForTwig('Type_ConfigParcelamento');
        $this->data['Type_CalculoParcelamento'] = $this->load->typeForTwig('Type_CalculoParcelamento');
        $this->data['Type_Status'] = $this->load->typeForTwig('Type_Status');

        $this->data['ftr_geral'] = new ftr_geral();
    }

    public function parcelamentoValidate($modulo)
    {
        $this->load->helper('ftr_geral');
        $this->load->language('payment/geralconfigs');
        $error = array();
        $configs = isset($this->request->post[$modulo . '_parcelamento_configs']) ? $this->request->post[$modulo . '_parcelamento_configs'] : array();
        $totalMinimoParcela = isset($this->request->post[$modulo . '_parcela_minimo']) ? $this->request->post[$modulo . '_parcela_minimo'] : 0;

        foreach ($configs as $key => $config) {
            if (($config['tipo_calculo'] <> Type_CalculoParcelamento::SemJuros) && ($config['percentual_calculo'] <= 0)) {
                $error['parcela_config_' . $key] = $this->language->get('error_percentual_parcela');
            }

            if ((ftr_geral::toFloat($totalMinimoParcela) > 0) && (ftr_geral::toFloat($config['val_minimo']) > 0) && (ftr_geral::toFloat($config['val_minimo']) < (((float)$totalMinimoParcela) * $key))) {
                $error['parcela_config_' . $key] = $this->language->get('error_total_minimo_parcela');
            }
        }

        return $error;
    }


    public function removeMask($modulo)
    {
        extract($this->request->post);
        if (isset($this->request->post[$modulo . '_parcelamento_configs'])) {
            foreach ($this->request->post[$modulo . '_parcelamento_configs'] as $key => $value) {
                $this->request->post[$modulo . '_parcelamento_configs'][$key]['val_minimo'] = ftr_geral::removeMaskToDouble($value['val_minimo']);
                $this->request->post[$modulo . '_parcelamento_configs'][$key]['percentual_calculo'] = ftr_geral::removeMaskToDouble($value['percentual_calculo']);
            }
        }
        if (isset($this->request->post[$modulo . '_parcela_juros'])) {
            $this->request->post[$modulo . '_parcela_juros'] = ftr_geral::removeMaskToDouble($this->request->post[$modulo . '_parcela_juros']);
        }
        if (isset($this->request->post[$modulo . '_parcela_desconto'])) {
            $this->request->post[$modulo . '_parcela_desconto'] = ftr_geral::removeMaskToDouble($this->request->post[$modulo . '_parcela_desconto']);
        }

    }

    public function getParcelamentoAvancado()
    {
        $this->data = array_merge($this->data, $this->load->language('payment/geralconfigs'));
        $this->data['Type_CalculoParcelamento'] = $this->load->typeForTwig('Type_CalculoParcelamento');
        $this->data['ftr_geral'] = new ftr_geral();

        $this->data['modulo'] = $this->request->get['modulo'];

        if ($this->config->get($this->data['modulo'] . '_parcelamento_configs') && $this->request->get['loadmodule'] == 'true') {
            $this->data['parcelas'] = $this->config->get($this->data['modulo'] . '_parcelamento_configs');
        } else {
            for ($idx = 1; $idx <= $this->request->get['qtd_parcelas']; $idx++) {
                $this->data['parcelas'][$idx] = array(
                    'num_parcela' => $idx,
                    'tipo_calculo' => Type_CalculoParcelamento::SemJuros,
                    'val_minimo' => 0,
                    'percentual_calculo' => 0
                );
            }
        }

        $this->data['qtd_parcelas'] = $this->request->get['qtd_parcelas'];
        $this->response->setOutput($this->render('payment/parcelamento_configs'));
    }


}

?>
