package com.fp.frontend.controller.accounting;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.fp.dto.Request;
import com.fp.dto.Response;
import com.fp.dto.query.DtoQuery;
import com.fp.dto.save.DtoSave;
import com.fp.frontend.controller.AbstractController;
import com.fp.frontend.helper.MessageHelper;
import com.fp.persistence.pgeneral.date.TgeneAccountingDateBranch;

/**
 * Clase controladora del bean TgeneCatalog.
 *
 * @author Jorge Vaca.
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class ChangeAccoDateControllerController extends AbstractController<TgeneAccountingDateBranch> {

    private Map<String, String> nextDateMap;

    /**
     * Constructor
     *
     * @throws Exception
     */
    public ChangeAccoDateControllerController() throws Exception {
        super(TgeneAccountingDateBranch.class);
    }

    /**
     * Metodo invocado despues de instanciar el controlador
     */
    @PostConstruct
    private void postconstruct() {
        this.init();
        // Inicializa autoconsulta
        super.startQuery();
    }

    /**
     * Incializa variables del controlador, cuando se esta utilizando una pagina
     * que utliza el controlador.
     */
    private void init() {
        try {
            this.beanalias = "TGENEACCOUNTINGDATEBRANCH";
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

    /* (non-Javadoc)
     * @see com.fp.frontend.controller.AbstractController#querydatabase()
     */
    @Override
    protected void querydatabase() {
        try {
            Request request = this.callerhelper.getRequest();
            Map<String, String> mcriterios = new HashMap<String, String>();
            mcriterios.put("pk.branchcode", "0");
            mcriterios.put("pk.companycode", request.getCompany().toString());

            DtoQuery dto = super.getDtoQuery(false);
            dto.addCriterios(mcriterios);

            HashMap<String, DtoQuery> mtables = new HashMap<String, DtoQuery>();
            mtables.put(this.beanalias, dto); // permite adicionar mas de una tabla.
            request.setQueryTables(mtables);
            Response resp = this.callerhelper.executeQuery(request);
            this.nextDateMap = new HashMap<String, String>();
            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                this.record = new TgeneAccountingDateBranch();
                MessageHelper.setMessageError(resp);
            } else {
                this.record = (TgeneAccountingDateBranch) resp.get(this.beanalias);
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    /* (non-Javadoc)
     * @see com.fp.frontend.controller.AbstractController#save()
     */
    @Override
    public void save() {
        try {
            Request request = this.callerhelper.getRequest();
            this.update();

            DtoSave dtosave = super.getDtoSave(true);
            if (!this.nextDateMap.isEmpty()) {
                MessageHelper.setMessageError("msg_accodate_already_generated");
                return;
            }
            HashMap<String, DtoSave> msave = new HashMap<String, DtoSave>();
            msave.put(this.beanalias, dtosave); // adicionar metadata de mantenimiento para cada tabla.
            request.setSaveTables(msave);
            Response resp = this.callerhelper.executeSave(request);
            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) == 0) {
                this.nextDateMap = (Map<String, String>) resp.get("NEXTDATE");
                this.postCommit(resp);
                MessageHelper.setMessageInfo(resp);
            } else {
                MessageHelper.setMessageError(resp);
            }
        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    /**
     * @return Retorna el atributo nextDateMap
     */
    public Map<String, String> getNextDateMap() {
        return this.nextDateMap;
    }

    /**
     * @param nextDateMap Parametro a fijar en el atributo nextDateMap
     */
    public void setNextDateMap(Map<String, String> nextDateMap) {
        this.nextDateMap = nextDateMap;
    }

}
