package com.fp.frontend.controller.pgeneral.date;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

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 TgeneAccountingDateBranch.
 *
 * @author Andres E. Carpio
 * @version 2.1
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class TgeneAccountingDateBranchController extends AbstractController<TgeneAccountingDateBranch> {

    public TgeneAccountingDateBranchController() throws Exception {
        super(TgeneAccountingDateBranch.class);
    }

    @PostConstruct
    private void postconstruct() {
        this.init();
        // Inicializa autoconsulta
        super.startQuery();

    }

    /**
     * Incializa el controlador, cuando se esta utilizando una pagina que utliza
     * el controlador.
     */
    private void init() {
        try {
            this.recperpage = 10; // Cambiar al # reg a mirar.
            this.lrecord = new ArrayList<>();
            this.beanalias = "ACCOUNTINGDATE";
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    protected void querydatabase() {
        try {
            Request request = this.callerhelper.getRequest();
            DtoQuery dto = super.getDtoQuery(false);
            dto.setOrderby("t.pk"); // En en string van todos los campos de
            // orden ("t.pk,  t.nombre, t.cpais").

            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);

            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) != 0) {
                MessageHelper.setMessageError(resp);
            } else {
                this.record = (TgeneAccountingDateBranch) resp.get(this.beanalias);
            }

        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    public void save() {
        try {
            Request request = this.callerhelper.getRequest();
            DtoSave dtosave = super.getDtoSave();
            if (!dtosave.pendingProcess()) {
                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.postCommit(resp);
                MessageHelper.setMessageInfo(resp);
            } else {
                MessageHelper.setMessageError(resp);
            }

        } catch (Throwable e) {
            MessageHelper.setMessageError(e);
        }
    }

    @Override
    public void postCommit(Response response) throws Exception {
        super.postCommitGeneric(response, this.beanalias);
    }

    /**
     * Entrega la fecha contable de la aplicacin.
     *
     * @return Fecah contable.
     * @throws Exception
     */
    public Date getAccountingDate() throws Exception {
        TgeneAccountingDateBranchController c = new TgeneAccountingDateBranchController();
        c.getMfilters().clear();
        c.getMfilters().put("pk.branchcode", "0");
        c.getMfilters().put("pk.companycode", super.getLoginController().getRequest().getCompany().toString());
        c.querydatabase();
        return c.record.getAccountingdate();
    }

    /**
     * Entrega la fecha contable de la aplicacin.
     *
     * @return Fecah contable.
     * @throws Exception
     */
    public static TgeneAccountingDateBranch find(Integer company) throws Exception {
        TgeneAccountingDateBranchController c = new TgeneAccountingDateBranchController();
        c.getMfilters().clear();
        c.getMfilters().put("pk.branchcode", "0");
        c.getMfilters().put("pk.companycode", company.toString());
        c.querydatabase();
        return c.record;
    }
}
