package com.fp.general.rates.rules.query.code;

import java.util.List;

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.pgeneral.date.TgeneAccountingDateBranch;
import com.fp.persistence.pgeneral.product.rate.TgeneBaseRateDetail;
import com.fp.persistence.pgeneral.product.rate.TgeneBaseRateDetailKey;

public class RatesDetail extends QueryRule {

    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        String currencycode = pQueryRequest.get("currencycode").toString();
        String baseratecatalog = pQueryRequest.get("baseratecatalog").toString();
        String baseratecatalogcode = pQueryRequest.get("baseratecatalogcode").toString();
        Integer company = pQueryRequest.getCompany();

        TgeneAccountingDateBranch d = com.fp.persistence.commondb.db.DataHelper.getInstance().getTgeneAccountingDateBranch(company, 0);

        List<TgeneBaseRateDetail> lratedetail = TgeneBaseRateDetail.findVigents(PersistenceHelper.getEntityManager(), currencycode, baseratecatalog,
                baseratecatalogcode, d.getWorkingdate());

        if (lratedetail.isEmpty()) {
            lratedetail.add(new TgeneBaseRateDetail(new TgeneBaseRateDetailKey(), null, null, null));
        }

        Response response = pQueryRequest.getResponse();
        response.put("TGENEBASERATEDETAIL", lratedetail);

        return pQueryRequest;
    }
}
