package wenrgise.hrms.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.LOVBean;
import wenrgise.common.ejb.business.HrmCommonBO;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseMessageKeyException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.MessageKey;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.vo.LovQueryVO;
import wenrgise.common.vo.LovVO;
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.HrmLvCreditDtlBean;
import wenrgise.hrms.bean.HrmLvCreditHdrBean;

public class HrmLvCreditBO extends HrmBaseBO {
  private int iThisMonth;
  
  private int iThisYear;
  
  public HrmLvCreditBO() {
    Calendar cal = Calendar.getInstance();
    this.iThisMonth = cal.get(2) + 1;
    this.iThisYear = cal.get(1);
    cal = null;
  }
  
  public HrmLvCreditBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public String creditLvRegion(HrmLvCreditHdrBean oHrmLvCreditHdrBean, Timestamp oWhenPicked, ArrayList oDetailBeanArray, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstEmpList = null;
    ArrayList arylstErrorList = new ArrayList();
    String sRegionId = oHrmLvCreditHdrBean.getHdrRegionId();
    checkCreditMonth(oHrmLvCreditHdrBean, arylstErrorList);
    creditLvRegion(sRegionId, arylstErrorList);
    reportError(arylstErrorList);
    return null;
  }
  
  public String creditLv(HrmLvCreditHdrBean oHrmLvCreditHdrBean, Timestamp oWhenPicked, ArrayList oDetailBeanArray, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstEmpList = null;
    ArrayList arylstErrorList = new ArrayList();
    String sEmpId = oHrmLvCreditHdrBean.getEmployeeNoId();
    String sLeaveId = oHrmLvCreditHdrBean.getLeaveTypeId();
    String sNoOfDay = oHrmLvCreditHdrBean.getTxtNoOfDays();
    creditLv(sEmpId, sLeaveId, sNoOfDay, arylstErrorList);
    reportError(arylstErrorList);
    return null;
  }
  
  public void checkCreditMonth(HrmLvCreditHdrBean oHrmLvCreditHdrBean, ArrayList arylstErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstEmpList = null;
    String sRegionId = oHrmLvCreditHdrBean.getHdrRegionId();
    ArrayList arylstLvList = getLvList(sRegionId);
    Iterator itrLvList = arylstLvList.iterator();
    while (itrLvList.hasNext()) {
      HrmLvCreditDtlBean oLvBean = itrLvList.next();
      String sLvAttrId = oLvBean.getDetailId();
      String sLeaveName = oLvBean.getName();
      int iCreditFreq = Integer.parseInt(oLvBean.getTxtCreditFrequency());
      int iNextMonthOffset = 12 / iCreditFreq;
      String sCurrentCreditMonth = oLvBean.getNextCreditYYMM();
      String sCalBeginMonth = oLvBean.getCalBeginMonth();
      String sLastCreditDay = getFirstDayOfMonth(sCurrentCreditMonth);
      HrmCommonBO oBo = new HrmCommonBO();
      sLastCreditDay = EnrgiseUtil.convertToString(oBo.getPreviousDay(sLastCreditDay, 1));
      String sNextCreditMonth = getNextCreditMonth(sCurrentCreditMonth, this.iThisYear, this.iThisMonth, iNextMonthOffset);
      if (sNextCreditMonth == null) {
        MessageKey oMessageKey = new MessageKey("hrm.HrmLvCredit.leave");
        ArrayList oParams = new ArrayList();
        oParams.add(sLeaveName);
        arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmLvCredit.failure", oParams, "E"));
      } 
    } 
  }
  
  private void creditLvRegion(String sRegionId, ArrayList arylstErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    String sLv = new String();
    String sLv1 = new String();
    String sLv2 = new String();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sRegionId));
    arylstParam.add(new DBObject(2, 1, 12, "100533"));
    arylstParam.add(new DBObject(3, 1, 12, "100200500000054"));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 12));
    arylstParam.add(new DBObject(7, 2, 12));
    arylstParam.add(new DBObject(8, 2, 12));
    arylstParam.add(new DBObject(9, 2, 12));
    arylstParam.add(new DBObject(10, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMLVCREDIT.procLvCredit(?,?,?,?,?,?,?,?,?,?)");
    DBObject oErrorCode = arylstOutArray.get(1);
    DBObject oErrorCode1 = arylstOutArray.get(2);
    DBObject oErrorCode2 = arylstOutArray.get(3);
    sLv = (String)oErrorCode.getObject();
    sLv1 = (String)oErrorCode1.getObject();
    sLv2 = (String)oErrorCode2.getObject();
    if (EnrgiseUtil.checkString(sLv)) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmLvCredit");
      ArrayList oParams = new ArrayList();
      oParams.add(sLv);
      arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmLvCredit.success", oParams, "E"));
    } 
    if (EnrgiseUtil.checkString(sLv1)) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmLvCredit");
      ArrayList oParams = new ArrayList();
      oParams.add(sLv1);
      arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmLvCredit.success", oParams, "E"));
    } 
    if (EnrgiseUtil.checkString(sLv2)) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmLvCredit");
      ArrayList oParams = new ArrayList();
      oParams.add(sLv2);
      arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmLvCredit.success", oParams, "E"));
    } 
  }
  
  private void creditLv(String sEmpId, String sLeaveId, String sNoOfDay, ArrayList arylstErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    String sLv = new String();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sEmpId));
    arylstParam.add(new DBObject(2, 1, 12, sLeaveId));
    arylstParam.add(new DBObject(3, 1, 12, sNoOfDay));
    arylstParam.add(new DBObject(4, 1, 12, "1"));
    arylstParam.add(new DBObject(5, 1, 12, "1"));
    arylstParam.add(new DBObject(6, 2, 12));
    arylstParam.add(new DBObject(7, 2, 12));
    arylstParam.add(new DBObject(8, 2, 12));
    arylstParam.add(new DBObject(9, 2, 12));
    arylstParam.add(new DBObject(10, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMLVCREDIT.procLvCredit(?,?,?,?,?,?,?,?,?,?)");
    DBObject oErrorCode = arylstOutArray.get(1);
    sLv = (String)oErrorCode.getObject();
    if (EnrgiseUtil.checkString(sLv)) {
      MessageKey oMessageKey = new MessageKey("hrm.HrmLvCredit");
      ArrayList oParams = new ArrayList();
      oParams.add(sLv);
      arylstErrorList.add(new EnrgiseMessageKeyException("hrm.HrmLvCredit.success", oParams, "E"));
    } 
  }
  
  private ArrayList getLvList(String sRegionId) throws EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    String sCalId = null;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sRegionId));
    arylstParam.add(new DBObject(2, 2, -10));
    arylstParam.add(new DBObject(3, 2, 12));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "HRMLVCREDIT.proc_GetLvList(?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    QueryValue oValue = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstList.iterator();
    HrmLvCreditDtlBean oCreditBean = null;
    ArrayList arylstLvList = new ArrayList();
    while (itrBean.hasNext()) {
      oRow = itrBean.next();
      oCreditBean = new HrmLvCreditDtlBean();
      oCreditBean.setDetailId(oRow.get("ID").getString());
      oCreditBean.setLvAttrId(oRow.get("ID").getString());
      oCreditBean.setName(oRow.get("NAME").getString());
      oCreditBean.setNextCreditYYMM(oRow.get("next_credit_yymm").getString());
      oCreditBean.setTxtCreditFrequency(oRow.get("credit_frequency").getString());
      oCreditBean.setCalBeginMonth(oRow.get("begin_month").getString());
      arylstLvList.add(oCreditBean);
    } 
    return arylstLvList;
  }
  
  public String getNextCreditMonth(String sCurrentCreditMonth, int iThisYear, int iThisMonth, int iNextMonthOffset) throws EnrgiseSystemException {
    String sYYYYMM = null;
    String sYYYY = sCurrentCreditMonth.substring(0, 4);
    String sMM = sCurrentCreditMonth.substring(4);
    int iYYYY = Integer.parseInt(sYYYY);
    int iMM = Integer.parseInt(sMM);
    if (iYYYY != iThisYear)
      return null; 
    if (iMM == iThisMonth) {
      iMM += iNextMonthOffset;
      if (iMM > 12) {
        iYYYY++;
        iMM %= 12;
      } 
      sMM = Integer.toString(iMM);
      if (sMM.length() == 1)
        sMM = String.valueOf("0").concat(String.valueOf(sMM)); 
      sYYYYMM = String.valueOf(String.valueOf(iYYYY)).concat(String.valueOf(sMM));
    } 
    return sYYYYMM;
  }
  
  private String getFirstDayOfMonth(String sYYYYMM) {
    int iDay = 1;
    String sYYYY = sYYYYMM.substring(0, 4);
    String sMM = sYYYYMM.substring(4);
    int iMonth = Integer.parseInt(sMM);
    int iYear = Integer.parseInt(sYYYY);
    iMonth--;
    GregorianCalendar c = new GregorianCalendar(iYear, iMonth, iDay);
    Date dFirstDay = c.getTime();
    String sFirstDay = EnrgiseUtil.convertToString(dFirstDay);
    return sFirstDay;
  }
  
  private String getLastDayOfMonth(String sYYYYMM) throws EnrgiseSystemException {
    int iDay = 1;
    String sYYYY = sYYYYMM.substring(0, 4);
    String sMM = sYYYYMM.substring(4);
    int iMonth = Integer.parseInt(sMM);
    int iYear = Integer.parseInt(sYYYY);
    iMonth--;
    GregorianCalendar gcCal = new GregorianCalendar(iYear, iMonth, iDay);
    Date dFirstDay = gcCal.getTime();
    HrmCommonBO oBo = new HrmCommonBO();
    dFirstDay = oBo.getPreviousDay(EnrgiseUtil.convertToString(dFirstDay), 1);
    String sFirstDay = EnrgiseUtil.convertToString(dFirstDay);
    return sFirstDay;
  }
  
  private String getCalStartDate(String sCalBeginMonth) throws EnrgiseSystemException {
    HrmCommonBO oBO = new HrmCommonBO();
    String sYYYY = String.valueOf(this.iThisYear);
    String sMM = sCalBeginMonth;
    if (sCalBeginMonth.length() == 1)
      sMM = String.valueOf("0").concat(String.valueOf(sMM)); 
    String sYYYYMM = String.valueOf(sYYYY).concat(String.valueOf(sMM));
    String sCalStartDate = getFirstDayOfMonth(sYYYYMM);
    return sCalStartDate;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "hrm_leave_mst";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String sScreenMode) throws EnrgiseSystemException {
    return new String();
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return new String();
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  public void additionalTimestampValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {}
  
  public LovVO getHrmLvCreditRegionLOVdata(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("ID");
    arylstHeaderList.add("hrm.HrmLeaveLedger.employeeNo");
    arylstHeaderList.add("hrm.HrmLeaveLedger.employeeName");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuerySearch2 = oLovQueryVO.getSearchField2(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParam.add(new DBObject(2, 1, 12, sQuerySearch2));
    arylstParam.add(new DBObject(3, 2, -10));
    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, "HRMLVCREDIT.getEmpCode(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("EMP_NO").getString());
      oLOVBean.setDetailField3(oRow.get("EMPNAME").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
}
