/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fp.person.rules.query;

import com.fp.bpm.query.Query;
import com.fp.common.helper.CalculationBase;
import com.fp.dto.Response;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.APPDates;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pcustomer.gene.TgeneActivity;
import com.fp.persistence.pgeneral.date.TgeneAccountingDateBranch;
import com.fp.persistence.pgeneral.gene.TgeneCatalogDetail;
import com.fp.persistence.pgeneral.gene.TgeneCatalogDetailKey;
import com.fp.persistence.pgeneral.gene.TgeneParameters;
import com.fp.persistence.pgeneral.gene.TgeneParametersKey;

/**
 * Clase que retorna la descripcion del sector de la actividad económica
 *
 * @author jarias
 */
public class ActivityEconomic extends QueryRule {

    private String activitycodedesc = "activitycodedesc";
    private String segmentcode = "segmentcode";
    private String segmentcatalogdesc = "segmentcatalogdesc";
    private String expirationdateinfo = "expirationdateinfo";

    /**
     * Metodo que agrega la descripcion de la actividad economica, el sectoy y descripcion del sector asociadas a una
     * persona, cuando una consulta es requerida. Además verifica la validéz de la información del cliente.
     *
     * @param pQueryRequest
     * @return pQueryRequest
     * @throws Exception
     */
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        Query query = new Query();
        query.process(pQueryRequest);
        Object people = (Object) response.get("TCUSTPERSONDETAIL");
        completeSector(people);
        expirationInfo(people, pQueryRequest);
        return pQueryRequest;
    }

    /**
     * Metodo que completa la descripcion de la actividad el sector y la descripción del sector
     *
     * @param detail
     * @throws Exception
     */
    private void completeSector(Object detail) throws Exception {
        if (detail == null) {
            return;
        }
        TcustPersonDetail cusDetail = (TcustPersonDetail) detail;
        if (cusDetail.getActivitycode() != null) {
            TgeneActivity activity = TgeneActivity.find(PersistenceHelper.getEntityManager(), cusDetail.getActivitycode());
            if (activity != null) {
                cusDetail.addAddtionalInfo(activitycodedesc, activity.getDescription());
                cusDetail.addAddtionalInfo(segmentcode, activity.getSegmenttypecatalog());
                TgeneCatalogDetailKey catalogDetailKey = new TgeneCatalogDetailKey(activity.getSegmenttypecatalog(), "SEGMENTTYPE");
                TgeneCatalogDetail detailcatalog = TgeneCatalogDetail.find(PersistenceHelper.getEntityManager(), catalogDetailKey);
                if (detailcatalog != null) {
                    cusDetail.addAddtionalInfo(segmentcatalogdesc, detailcatalog.getDescription());
                } else {
                    cusDetail.addAddtionalInfo(segmentcatalogdesc, null);
                }
            } else {
                cusDetail.addAddtionalInfo(activitycodedesc, null);
                cusDetail.addAddtionalInfo(segmentcode, null);
            }
        } else {
            cusDetail.addAddtionalInfo(activitycodedesc, null);
            cusDetail.addAddtionalInfo(segmentcode, null);
            cusDetail.addAddtionalInfo(segmentcatalogdesc, null);
        }

    }

    /**
     * Método que verifica la validez de la información del cliente segun la ultima actualización
     *
     * @param tcustPerson
     * @param pQueryRequest
     * @throws Exception
     */
    private void expirationInfo(Object tcustPerson, QueryRequest pQueryRequest) throws Exception {
        TcustPersonDetail tcustPersonDetail = (TcustPersonDetail) tcustPerson;
        if (tcustPersonDetail == null) {
            return;
        }
        if (tcustPersonDetail.getPk().getPersoncode() != null) {
            TgeneParametersKey tgeneParametersKey = new TgeneParametersKey("EXPIRATIONDATE", tcustPersonDetail.getCompanycode());
            TgeneParameters tgeneParameters = TgeneParameters.find(PersistenceHelper.getEntityManager(), tgeneParametersKey);
            if (tgeneParameters.getNumbervalue() != null) {
                APPDates fechaVigenteRegistro = new APPDates(tcustPersonDetail.getDatefrom().toString().substring(0, 10), CalculationBase.B365365);
                TgeneAccountingDateBranch db = com.fp.persistence.commondb.db.DataHelper.getInstance().getTgeneAccountingDateBranch(
                        pQueryRequest.getCompany(), 0);
                APPDates fechaActual = new APPDates(db.getWorkingdate(), CalculationBase.B365365);
                if (fechaActual.compareTo(fechaVigenteRegistro) >= 0) {
                    Integer days = fechaActual.substract(fechaVigenteRegistro);
                    if (days > tgeneParameters.getNumbervalue().intValue()) {
                        tcustPersonDetail.addAddtionalInfo(expirationdateinfo, "Y");
                    } else {
                        tcustPersonDetail.addAddtionalInfo(expirationdateinfo, "N");
                    }
                } else {
                        tcustPersonDetail.addAddtionalInfo(expirationdateinfo, "N");
                }
            }
        }
    }
}
