package jp.mosp.payroll.common.vo;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jp.mosp.common.MosPVo;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dto.MosPConfDto;
import jp.mosp.common.part.DatePart;
import jp.mosp.legal.LegalConst;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.dto.AdjustmentDto;
import jp.mosp.payroll.dto.CdAdjustmentDto;

public class AdjustmentCalcVo extends MosPVo {
	
	private static final long serialVersionUID = 3281945937471826907L;
	
	// URL丄CSS
	private static final String RETURN_URL = "/jsp/payroll/common/adjustmentCalc.jsp";
	private static final String EXTRA_JS   = "/pub/payroll/common/js/adjustmentCalc.js";
	private static final String EXTRA_CSS  = "/pub/payroll/common/css/adjustmentCalc.css";
	
	// 夋柺僒僽僞僀僩儖
	public final String SUB_TITLE = PayrollConst.TTL_ADJUSTMENT_CALC;
	
	// 僷儔儊乕僞柤
	public static final String PRM_TXT_K_CODE          = "txtKCode"         ;
	public static final String PRM_TXT_K_NAME          = "txtKName"         ;
	public static final String PRM_TXT_K_KANA          = "txtKKana"         ;
	public static final String PRM_PLT_RETIREMENT      = "pltRetirement"    ;
	public static final String PRM_PLT_POSITION        = "pltPosition"      ;
	public static final String PRM_PLT_ADJUSTMENT_TYPE = "pltAdjustmentType";
	public static final String PRM_PLT_MIDWAY_TYPE     = "pltMidwayType"    ;
	public static final String PRM_PLT_ADJUST_STATUS   = "pltAdjustStatus"  ;
	public static final String PRM_HID_ADJUST_YEAR     = "hidAdjustYear"    ;
	
	// 僼傿乕儖僪柤媦傃惂尷
	public static final String NAM_K_CODE      = PayrollConst.NAM_M_KIHON_KCODE     ;
	public static final String NAM_K_KANA      = PayrollConst.NAM_K_KANA     ;
	public static final String NAM_ADJUST_YEAR = PayrollConst.NAM_ADJUST_YEAR;
	public static final int    LEN_K_CODE      = PayrollConst.LEN_K_CODE     ;
	public static final int    LEN_K_NAME      = PayrollConst.LEN_K_NAME     ;

	// 僼傿乕儖僪
	private int      hidAdjustYear;
	
	private String[] aryAdjustStatus  ;
	private String[] aryKCode         ;
	private String[] aryKName         ;
	private String[] aryKKana         ;
	private String[] aryEntranceDate  ;
	private String[] aryRetirementDate;
	private String[] aryAdjustmentType;
	private String[] aryTaxGap        ;
	
	private String   txtKCode         ;
	private String   txtKName         ;
	private String   txtKKana         ;
	private String   pltRetirement    ;
	private String   pltPosition      ;
	private String   pltAdjustmentType;
	private String   pltMidwayType    ;
	private String   pltAdjustStatus  ;
	
	// 僾儖僟僂儞儕僗僩
	public String[][] aryPltRetirement    ;
	public String[][] aryPltPosition      ;
	public String[][] aryPltAdjustmentType;
	public String[][] aryPltMidwayType    ;
	public String[][] aryPltAdjustStatus  ;
	
	// 僜乕僩僉乕
	public final String KEY_ADJUST_STATUS    = "KeyAdjustStatus";
	public final String KEY_K_CODE           = "keyKCode";
	public final String KEY_K_NAME           = "keyKName";
	public final String KEY_ENTRANCE         = "keyEntrance";
	public final String KEY_RETIREMENT       = "keyRetirement";
	public final String KEY_ADJUSTMENT_TYPE  = "keyAdjustmentType";
	public final String KEY_TAX_GAP          = "keyTaxGap";
	
	// 柤徧庢摼梡攝楍
	public String[][] aryEra;
	
	// 愝掕僥乕僽儖
	public List<MosPConfDto> listConf;
	
	/**
	 * 僐儞僗僩儔僋僞
	 * @throws MospException 
	 */
	public AdjustmentCalcVo() {
		super();
		setRetUrl(RETURN_URL);
		setExtraJs(EXTRA_JS);
		setExtraCss(EXTRA_CSS);
		hidAdjustYear     = 0 ;
		txtKCode          = "";
		txtKName          = "";
		txtKKana          = "";
		pltRetirement     = "";
		pltPosition       = "";
		pltAdjustmentType = "";
		pltMidwayType     = "";
		pltAdjustStatus   = "";
		aryAdjustStatus   = new String[0];
		aryKCode          = new String[0];
		aryKName          = new String[0];
		aryKKana          = new String[0];
		aryEntranceDate   = new String[0];
		aryRetirementDate = new String[0];
		aryAdjustmentType = new String[0];
		aryTaxGap         = new String[0];
		setFormerKey(KEY_K_CODE);
	}
	
	/**
	 * 僷儔儊乕僞愝掕
	 * @param request 儕僋僄僗僩
	 */
	public void setParams(HttpServletRequest request) {
		hidAdjustYear     = getIntParam(request.getParameter(PRM_HID_ADJUST_YEAR));
		txtKCode          = request.getParameter(PRM_TXT_K_CODE         );
		txtKName          = request.getParameter(PRM_TXT_K_NAME         );
		txtKKana          = request.getParameter(PRM_TXT_K_KANA         );
		pltRetirement     = request.getParameter(PRM_PLT_RETIREMENT     );
		pltPosition       = request.getParameter(PRM_PLT_POSITION       );
		pltAdjustmentType = request.getParameter(PRM_PLT_ADJUSTMENT_TYPE);
		pltMidwayType     = request.getParameter(PRM_PLT_MIDWAY_TYPE    );
		pltAdjustStatus   = request.getParameter(PRM_PLT_ADJUST_STATUS  );
	}
	
	/**
	 * 僼傿乕儖僪弶婜壔
	 * @throws MospException 
	 */
	public void initFields() throws MospException {
		// 攝楍偺弶婜壔
		count = 0;
		aryAdjustStatus   = new String[count];
		aryKCode          = new String[count];
		aryKName          = new String[count];
		aryKKana          = new String[count];
		aryEntranceDate   = new String[count];
		aryRetirementDate = new String[count];
		aryAdjustmentType = new String[count];
		aryTaxGap         = new String[count];
		// 堦棗忣曬弶婜壔
		initListInfo();
	}

	/**
	 * 僼傿乕儖僪愝掕
	 * @param list 幮堳儕僗僩
	 * @throws MospException 
	 */
	public void setFields(List list) throws MospException {
		// 儕僗僩偺愝掕
		this.list = list;
		// 僨乕僞憤悢偺庢摼
		count = list.size();
		// 攝楍偺弶婜壔
		aryAdjustStatus   = new String[count];
		aryKCode          = new String[count];
		aryKName          = new String[count];
		aryKKana          = new String[count];
		aryEntranceDate   = new String[count];
		aryRetirementDate = new String[count];
		aryAdjustmentType = new String[count];
		aryTaxGap         = new String[count];
		// 僨乕僞偺愝掕
		int i = 0;
		Iterator it = list.iterator();
		while (it.hasNext()) {
			AdjustmentDto dto = (AdjustmentDto)it.next();
			aryKCode         [i] = escapeHTML(dto.getKCode());
			aryKName         [i] = escapeHTML(dto.getKName());
			aryKKana         [i] = escapeHTML(dto.getKKana());
//			aryEntranceDate  [i] = escapeHTML(getDateString(listConf, aryEra, dto.getEntranceDate  ()));
//			aryRetirementDate[i] = escapeHTML(getDateString(listConf, aryEra, dto.getRetirementDate()));
			aryEntranceDate  [i] = escapeHTML(getDateString(listConf, aryEra, DatePart.convIntegerDateToDate(dto.getEntranceDate())));
			aryRetirementDate[i] = escapeHTML(getDateString(listConf, aryEra, DatePart.convIntegerDateToDate(dto.getRetirementDate())));
			aryTaxGap        [i] = insertComma(String.valueOf(dto.getTaxGap()));
			aryAdjustStatus  [i] = PayrollConst.ADJU_STATUS_NON_NAME;
			for (int j = 0; j < aryPltAdjustStatus.length; j++) {
				if (aryPltAdjustStatus[j][0].equals(dto.getAdjustStatus())) {
					aryAdjustStatus[i] = escapeHTML(aryPltAdjustStatus[j][1]);
					break;
				}
			}
			aryAdjustmentType[i] = "";
			for (int j = 0; j < LegalConst.ADJUSTMENT_ARRAY.length; j++) {
				if (LegalConst.ADJUSTMENT_ARRAY[j][0].equals(dto.getAdjustmentType())) {
					aryAdjustmentType[i] = escapeHTML(LegalConst.ADJUSTMENT_ARRAY[j][1]);
					break;
				}
			}
			i++;
		}
	}
	
	/**
	 * 僼傿乕儖僪愝掕(DTO偵懳偡傞VO忣曬偺僙僢僩)
	 * @param dto        愝掕懳徾DTO
	 * @param kCode      愝掕幮堳僐乕僪
	 * @param adjustYear 愝掕挷惍擭
	 */
	public void setDtoFields(CdAdjustmentDto dto, String kCode, int adjustYear) {
		dto.setKCode              (kCode      );	// 幮堳僐乕僪
		dto.setAdjustYear         (adjustYear );	// 挷惍擭
		dto.setGeneralLifeIns     (0);	// 堦斒偺惗柦曐尟椏
		dto.setPrivatePension     (0);	// 屄恖擭嬥曐尟椏
		dto.setLifeInsDeduction   (0);	// 惗柦曐尟椏峊彍妟
		dto.setEarthquakeIns      (0);	// 抧恔曐尟椏
		dto.setLongTermDamageIns  (0);	// 媽挿婜懝奞曐尟椏
		dto.setDamageInsDeduction (0);	// 懝奞曐尟椏峊彍妟
		dto.setSocialInsDeduction (0);	// 幮夛曐尟椏峊彍妟
		dto.setMutualAidDeduction (0);	// 彫婯柾婇嬈嫟嵪摍妡嬥峊彍妟
		dto.setSpouseSumIncome    (0);	// 攝嬼幰偺杮擭拞偺崌寁強摼嬥妟偺尒愊妟
		dto.setSpouseSpDeduction  (0);	// 攝嬼幰摿暿峊彍妟
		dto.setHouseLoanDeduction (0);	// 廧戭庁擖嬥摍摿暿峊彍妟
		dto.setAllowanceAdjustment(0);	// 巟媼挷惍妟
		dto.setTaxFreeAdjustment  (0);	// 旕壽惻挷惍妟
		dto.setSocInsAdjustment   (0);	// 幮夛曐尟挷惍妟
		dto.setTaxIncAdjustment   (0);	// 強摼惻挷惍妟
		dto.setWithholdSlipNotes  ("");	// 尮愹挜廂昜揈梫棑
		dto.setPaymentAllowance   (0);	// 媼梌丄庤摉摍嬥妟
		dto.setPaymentIncomeTax   (0);	// 媼梌丄庤摉摍惻妟
		dto.setBonusAllowance     (0);	// 徿梌摍嬥妟
		dto.setBonusIncomeTax     (0);	// 徿梌摍惻妟
		dto.setMidwayAdjustIncome (0);	// 拞搑丄挷惍廂擖嬥妟
		dto.setMidwayAdjustTax    (0);	// 拞搑丄挷惍廂擖惻妟
		dto.setAfterDeductIncome  (0);	// 媼梌強摼峊彍屻偺媼梌摍偺嬥妟
		dto.setPayInsDeduction    (0);	// 幮夛曐尟椏摍媼梌摍偐傜偺峊彍暘
		dto.setDependentsDeduction(0);	// 攝嬼幰峊彍妟丄晑梴峊彍妟丄婎慴峊彍妟媦傃忈奞幰摍偺峊彍妟偺崌寁妟
		dto.setCalcTaxedIncome    (0);	// 嵎堷壽惻媼梌強摼嬥妟
		dto.setCalcIncomeTax      (0);	// 嶼弌擭惻妟
		dto.setAdjustedIncomeTax  (0);	// 擭挷擭惻妟
		dto.setDeductedIncomeTax  (0);	// 擭挷掕棪峊彍妟惻妟
		dto.setFinalIncomeTax     (0);	// 擭惻妟
		dto.setDeadRetirement     (String.valueOf(PayrollConst.CHECKBOX_OFF));
		dto.setSelfDisaster       (String.valueOf(PayrollConst.CHECKBOX_OFF));
		dto.setForeigner          (String.valueOf(PayrollConst.CHECKBOX_OFF));
	}
	
	/**
	 * JavaScript愝掕
	 * @param cls 忣曬庢摼僋儔僗
	 * @throws IllegalAccessException
	 */
	@Override
	public String getDirectJs(Class cls) throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDirectJs(cls));
		return sb.toString();
	}
	
	/**
	 * 僇儞儅憓擖
	 * @param strTarget
	 * @return
	 */
	public String insertComma(String strTarget) {
		if (strTarget == null || strTarget.equals("")) {
			return "";
		}
		return strTarget.replaceAll("(?m)(?<=\\d)(?=(?:\\d{3})+$)",",");
	}
	
	// 傾僋僙僒儊僜僢僪
	public String[] getEscAryAdjustStatus  () { return aryAdjustStatus  .clone(); }
	public String[] getEscAryKCode         () { return aryKCode         .clone(); }
	public String[] getEscAryKName         () { return aryKName         .clone(); }
	public String[] getEscAryKKana         () { return aryKKana         .clone(); }
	public String[] getEscAryEntranceDate  () { return aryEntranceDate  .clone(); }
	public String[] getEscAryRetirementDate() { return aryRetirementDate.clone(); }
	public String[] getEscAryAdjustmentType() { return aryAdjustmentType.clone(); }
	public String[] getEscAryTaxGap        () { return aryTaxGap        .clone(); }
	
	public int   getHidAdjustYear    () { return hidAdjustYear    ; }
	public String getTxtKCode         () { return txtKCode         ; }
	public String getTxtKName         () { return txtKName         ; }
	public String getTxtKKana         () { return txtKKana         ; }
	public String getPltRetirement    () { return pltRetirement    ; }
	public String getPltPosition      () { return pltPosition      ; }
	public String getPltAdjustmentType() { return pltAdjustmentType; }
	public String getPltMidwayType    () { return pltMidwayType    ; }
	public String getPltAdjustStatus  () { return pltAdjustStatus  ; }
	public String getEscTxtKCode() { return escapeHTML(txtKCode); }
	public String getEscTxtKName() { return escapeHTML(txtKName); }
	public String getEscTxtKKana() { return escapeHTML(txtKKana); }
	
	public void setHidAdjustYear    (int    hidAdjustYear    ) { this.hidAdjustYear     = hidAdjustYear    ; }
	public void setPltRetirement    (String pltRetirement    ) { this.pltRetirement     = pltRetirement    ; }
	public void setPltAdjustmentType(String pltAdjustmentType) { this.pltAdjustmentType = pltAdjustmentType; }
	
}
