/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.fp.frontend.controller.armas.solicitud;

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.commondb.PersistenceHelper;
import com.fp.persistence.parmas.soli.TarmInvoiceHistory;
import com.fp.persistence.pgeneral.gene.TgeneParameters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.Query;

/**
 *
 * @author developer2
 */
@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class InvoiceHistoryController extends AbstractController<TarmInvoiceHistory>{

    public InvoiceHistoryController() throws Exception {
        super(TarmInvoiceHistory.class);
    }
    @PostConstruct
    private void postconstruct() {
        this.init();
        // Inicializa autoconsulta
        super.startQuery();
        //
        //super.addFilter("pk.companycode", super.getLoginController().getRequest().getCompany().toString());

    }
    private void init() {
        try {
            this.recperpage = 10; // Cambiar al # reg a mirar.
            this.lrecord = new ArrayList<>();
            this.beanalias = "TARMINVOICEHISTORY";
        } catch (Exception e) {
            MessageHelper.setMessageError(e);
        }
    }
    
    public void update(TarmInvoiceHistory bean) throws Exception {
            this.record = bean;
            super.update();
       
    }
    
    
    
    @Override
    public void save() {
        try {
            Request request = this.callerhelper.getRequest();
            DtoSave dtosave = super.getDtoSave();
            dtosave.setReturnpk(true);
            /**
            if (!dtosave.pendingProcess()) {
                System.out.println("Pending Process "+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);
            
            System.out.println("Response execution: "+resp);       
            if (resp.getResponseCode().compareTo(Response.RESPONSE_OK) == 0) {
                this.postCommit(resp);
                MessageHelper.setMessageInfo(resp);

                System.out.println("Deberia haber guardado");
            } else {
                MessageHelper.setMessageError(resp);
                throw new Exception("Error al guardar " + resp);
            }

        } catch (Throwable e) {
            System.out.println("Error guardar Auditoria XML: " +e.toString());

            MessageHelper.setMessageError(e);
        }
    }
    
    @Override
    public void postCommit(Response response) throws Exception {
        super.postCommitGeneric(response, this.beanalias);
    }
    
    @Override
    protected void querydatabase() {
        try {
            Request request = this.callerhelper.getRequest();

            DtoQuery dto = super.getDtoQuery(true);
            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) {
                this.lrecord = new ArrayList<TarmInvoiceHistory>();
                MessageHelper.setMessageError(resp);
            } else {
                this.lrecord = (List<TarmInvoiceHistory>) resp.get(this.beanalias);
                super.postQuery(this.lrecord);
            }

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