package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.UserInfo;
import wenrgise.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.helper.QueryValue;
import wenrgise.ejb.common.utility.DBUtilitiesBean;
import wenrgise.hrms.bean.HrmAdvSettleHeadsDtlBean;
import wenrgise.hrms.bean.HrmAdvSettleHeadsHdrBean;
import wenrgise.hrms.vo.HrmAdvSettleHeadsQVO;

public class HrmAdvSettleHeadsBO extends HrmBaseBO {
  public HrmAdvSettleHeadsBO() {}
  
  public HrmAdvSettleHeadsBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrAdvSettleHeadsDetailMetaInfo(HrmAdvSettleHeadsQVO oHrmAdvSettleHeadsQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    if (EnrgiseUtil.checkString(oHrmAdvSettleHeadsQVO.getHeaderPrimaryKey())) {
      String sPrimaryKey = oHrmAdvSettleHeadsQVO.getHeaderPrimaryKey();
      oHrmAdvSettleHeadsQVO.setSettleId(sPrimaryKey);
    } 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, oHrmAdvSettleHeadsQVO.getSettleId()));
    arylstParam.add(new DBObject(2, 2, -5));
    arylstParam.add(new DBObject(3, 2, 93));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMADVSETTLEHEADS.proc_HrEmpAdvSettleDtlCount(?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTotalRecord = arylstOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    DBObject oTimeObject = arylstOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrAdvSettleHeadsDetail(HrmAdvSettleHeadsQVO oHrmAdvSettleHeadsQVO, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    if (EnrgiseUtil.checkString(oHrmAdvSettleHeadsQVO.getHeaderPrimaryKey())) {
      String sPrimaryKey = oHrmAdvSettleHeadsQVO.getHeaderPrimaryKey();
      oHrmAdvSettleHeadsQVO.setSettleId(sPrimaryKey);
    } 
    QueryRow oRow = null;
    QueryValue oValue = null;
    ArrayList arylstList = null;
    Iterator itrBean = null;
    ArrayList arylstSettleHeads = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, -5, new Long(lDetailFirstPosition)));
    arylstParam.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    arylstParam.add(new DBObject(3, 1, 12, oHrmAdvSettleHeadsQVO.getSettleId()));
    arylstParam.add(new DBObject(4, 2, -10));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 12));
    arylstParam.add(new DBObject(7, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMADVSETTLEHEADS.proc_HrEmpAdvSettleDtl(?,?,?,?,?,?,?)");
    arylstList = (ArrayList)((DBObject)arylstOutArray.get(0)).getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    count = 0;
    itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      if (count == 0)
        arylstSettleHeads = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmAdvSettleHeadsDtlBean oHrmAdvSettleHeadsDtlBean = new HrmAdvSettleHeadsDtlBean();
      oHrmAdvSettleHeadsDtlBean.setDetailId(oRow.get("ID").getString());
      oHrmAdvSettleHeadsDtlBean.setTxtHeadName(oRow.get("HEAD_NAME").getString());
      oHrmAdvSettleHeadsDtlBean.setTxtMaxLimit(oRow.get("MAX_LIMIT").getString());
      oHrmAdvSettleHeadsDtlBean.setTxtAmount(oRow.get("SETL_AMOUNT").getString());
      oHrmAdvSettleHeadsDtlBean.setTxtSettleHeadId(oRow.get("SETL_ID").getString());
      oHrmAdvSettleHeadsDtlBean.setTxtAdvancesId(oRow.get("ADVNC_ID").getString());
      oHrmAdvSettleHeadsDtlBean.setTxtSettleStatus(oRow.get("SETL_APPROVED_FLAG").getString());
      arylstSettleHeads.add(oHrmAdvSettleHeadsDtlBean);
    } 
    return arylstSettleHeads;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_EMP_SETL_HEAD";
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  private void checkMandatoryDtls(HrmAdvSettleHeadsDtlBean oHrmAdvSettleHeadsDtlBean, int rowCount, ArrayList oErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String param1) throws EnrgiseSystemException {
    return "saveHeaderImpl toBeDone";
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void saveDetailImpl(String param1, String param2, ArrayList param3) {}
  
  public void saveDetailImpl(BaseHeaderBean oBaseHeaderBean, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("HrmAdvSettlementHeads"))
      saveHrmAdvSettleHdsDetail(oBaseHeaderBean, oDetailBeanArray); 
  }
  
  public String saveHrmAdvSettleHdsDetail(BaseHeaderBean oBaseHeaderBean, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean oBean1 = null;
    DBUtilitiesBean oBean2 = null;
    DBUtilitiesBean oBean3 = null;
    HrmAdvSettleHeadsHdrBean oHrmAdvSettleHeadsHdrBean = (HrmAdvSettleHeadsHdrBean)oBaseHeaderBean;
    Iterator itrBean = arylstDetailBeanArray.iterator();
    while (itrBean.hasNext()) {
      HrmAdvSettleHeadsDtlBean oHrmAdvSettleHeadsDtlBean = itrBean.next();
      if (oHrmAdvSettleHeadsDtlBean.getDetailId().equals("")) {
        if (!bInsert) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("HRMADVSETTLEHEADS.proc_UpsertHrAdvSetHdsDtl(?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "N"));
        arylstParameters.add(new DBObject(2, 1, 12, null));
        arylstParameters.add(new DBObject(3, 1, 12, oHrmAdvSettleHeadsDtlBean.getTxtAdvancesId()));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmAdvSettleHeadsDtlBean.getTxtSettleHeadId()));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmAdvSettleHeadsDtlBean.getTxtAmount()));
        arylstParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oBean1.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmAdvSettleHeadsDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean2 = new DBUtilitiesBean();
          oBean2.createBatch("HRMADVSETTLEHEADS.proc_UpsertHrAdvSetHdsDtl(?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, "U"));
        arylstParameters.add(new DBObject(2, 1, 12, oHrmAdvSettleHeadsDtlBean.getDetailId()));
        arylstParameters.add(new DBObject(3, 1, 12, oHrmAdvSettleHeadsDtlBean.getTxtAdvancesId()));
        arylstParameters.add(new DBObject(4, 1, 12, oHrmAdvSettleHeadsDtlBean.getTxtSettleHeadId()));
        arylstParameters.add(new DBObject(5, 1, 12, oHrmAdvSettleHeadsDtlBean.getTxtAmount()));
        arylstParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserTypeId()));
        arylstParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oBean2.addToBatch(arylstParameters);
        continue;
      } 
      if (oHrmAdvSettleHeadsDtlBean.getStatus().equals("D")) {
        if (!bDelete);
        arylstParameters = new ArrayList();
        arylstParameters.add(new DBObject(1, 1, 12, oHrmAdvSettleHeadsDtlBean.getDetailId()));
        oBean3.addToBatch(arylstParameters);
      } 
    } 
    if (bInsert)
      oBean1.executeBatch(); 
    if (bUpdate)
      oBean2.executeBatch(); 
    if (bDelete)
      oBean3.executeBatch(); 
    return oHrmAdvSettleHeadsHdrBean.getSettleId();
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    HrmAdvSettleHeadsHdrBean oHrmAdvSettleHeadsHdrBean = (HrmAdvSettleHeadsHdrBean)oBaseHeaderBean;
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D")); 
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("HrmAdvSettlementHeads"))
        checkSettleAmount(arylstDetailBeanArray);  
    reportError(arylstErrorList);
  }
  
  private void checkSettleAmount(ArrayList arylstDetailBeanArray) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    Iterator itrBean1 = arylstDetailBeanArray.iterator();
    int iCount = 1;
    while (itrBean1.hasNext()) {
      HrmAdvSettleHeadsDtlBean oHrmAdvSettleHeadsDtlBean = itrBean1.next();
      if (!oHrmAdvSettleHeadsDtlBean.getStatus().equalsIgnoreCase("D")) {
        String sMaxLimit = oHrmAdvSettleHeadsDtlBean.getTxtMaxLimit();
        System.out.println(sMaxLimit);
        String sAmount = oHrmAdvSettleHeadsDtlBean.getTxtAmount();
        System.out.println(sAmount);
        int iMaxLimit = Integer.parseInt(sMaxLimit);
        System.out.println(iMaxLimit);
        int iAmount = Integer.parseInt(sAmount);
        System.out.println(iAmount);
        if (iMaxLimit < iAmount)
          throw new EnrgiseApplicationException("wenrgise.hrms.settleHeads.amount", "M"); 
      } 
    } 
  }
}
