/*
 * MosP - Mind Open Source Project    http://www.mosp.jp/
 * Copyright (C) MIND Co., Ltd.       http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.payroll.bonus.vo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jp.mosp.common.common.FileUploadUtility;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.payroll.base.PayrollVo;
import jp.mosp.payroll.bonus.BonusConst;
import jp.mosp.payroll.bonus.dto.PdBonusDto;

import org.apache.commons.fileupload.FileItem;

public class BonusCalcVo extends PayrollVo {
	
	private static final long	serialVersionUID		= 3281945937471826907L;
	
	// URL、CSS
	private static final String	RETURN_URL				= "/jsp/payroll/bonus/bonusCalc.jsp";
	private static final String	EXTRA_JS				= "/pub/payroll/bonus/js/bonusCalc.js";
	private static final String	EXTRA_CSS				= "/pub/payroll/bonus/css/bonusCalc.css";
	
	// 画面サブタイトル
	public final String			SUB_TITLE				= BonusConst.NAM_BONUS_CALCULATE;
	
	// フィールド名及び制限
	public static final String	PRM_PLT_YEAR			= "pltYear";
	public static final String	PRM_PLT_MONTH			= "pltMonth";
	public static final String	PRM_PLT_DAY				= "pltDay";
	public static final String	PRM_FIL_PAYMENT			= "filPayment";
	public static final String	PRM_PLT_STATION_CODE	= "pltStationCode";
	
	public static final String	PRM_PLT_DETAIL_YEAR		= "pltDetailYear";
	public static final String	PRM_PLT_DETAIL_MONTH	= "pltDetailMonth";
	public static final String	PRM_PLT_DETAIL_DAY		= "pltDetailDay";
	
	public static final String	PRM_CKB_NEED_CALC		= "ckbNeedCalc";
	
	public static final int		LEN_AMOUNT				= BonusConst.LEN_MONEY_AMOUNT;
	
	public static final String	NAM_CSV_AMOUNT			= "金額項目";
	public static final String	NAM_CALC_DATE			= "計算年月";
	public static final String	NAM_CSV_KEY				= "社員コード";
	
	public static final String	PRM_FIL_UPLOAD_FILE		= "filUploadFile";
	
	// フィールド
	private String				pltYear					= "";
	private String				pltMonth				= "";
	private String				pltDay					= "";
	private String				pltStationCode			= "";
	private String				ckbNeedCalc				= "";
	
	private FileItem			filUploadFile;
	
	private int					idxKCode				= 0;
	private int					idxCalcDate				= 0;
	private String				kCode					= "";
	private Date				calcDate;
	private Date				payday;
	
	// プルダウンリスト
	public String[][]			aryPltYear;
	public String[][]			aryPltMonth;
	public String[][]			aryPltDay;
	
	private String				pltDetailYear;
	private String				pltDetailMonth;
	private String				pltDetailDay;
	
	// 名称取得用リスト
	public String[][]			aryPltStationAll;
	
	// モード(0：新規登録、1:表示)
	private int					mode;
	

	/**
	 * コンストラクタ
	 */
	public BonusCalcVo() {
		super();
		setRetUrl(RETURN_URL);
		setExtraJs(EXTRA_JS);
		setExtraCss(EXTRA_CSS);
	}
	
	/**
	 * サブタイトル取得
	 * @param subTitle サブタイトル
	 * @param cmdMenu  メニューコマンド
	 * @return サブタイトルHTML文字列
	 */
	public String getSubTitle(String subTitle, String cmdMenu) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div class=\"Header\">");
		sb.append("<div class=\"MenuButtonBar\">");
		sb.append("<button type=\"button\" ");
		sb.append("onclick=\"doSubmit(document.form, '");
		sb.append(cmdMenu);
		sb.append("')\">メニューへ</button>");
		sb.append("<button type=\"button\" id=\"btnLogout\" ");
		sb.append("onclick=\"doSubmit(document.form, '");
		sb.append(MospConst.CMD_LOGOUT);
		sb.append("')\">ログアウト</button>");
		sb.append("</div>");
		sb.append("<div class=\"SubTitleBar\">");
		sb.append(subTitle);
		sb.append("</div>");
		sb.append("</div>");
		return sb.toString();
	}
	
	/**
	 * パラメータ設定
	 * @param list リクエストリスト
	 */
	public void setUploadFile(List list) throws Exception {
		pltYear = FileUploadUtility.getMultipartField(list, PRM_PLT_YEAR);
		pltMonth = FileUploadUtility.getMultipartField(list, PRM_PLT_MONTH);
		pltDay = FileUploadUtility.getMultipartField(list, PRM_PLT_DAY);
		pltDetailYear = FileUploadUtility.getMultipartField(list, PRM_PLT_DETAIL_YEAR);
		pltDetailMonth = FileUploadUtility.getMultipartField(list, PRM_PLT_DETAIL_MONTH);
		pltDetailDay = FileUploadUtility.getMultipartField(list, PRM_PLT_DETAIL_DAY);
		pltStationCode = FileUploadUtility.getMultipartField(list, PRM_PLT_STATION_CODE);
		filUploadFile = FileUploadUtility.getMultipartFile(list, PRM_FIL_UPLOAD_FILE);
	}
	
	/**
	 * パラメータ設定
	 * @param request リクエスト
	 */
	public void setYearParams(HttpServletRequest request) {
		pltYear = request.getParameter(PRM_PLT_YEAR);
		pltMonth = request.getParameter(PRM_PLT_MONTH);
		pltDay = request.getParameter(PRM_PLT_DAY);
		pltDetailYear = request.getParameter(PRM_PLT_DETAIL_YEAR);
		pltDetailMonth = request.getParameter(PRM_PLT_DETAIL_MONTH);
		pltDetailDay = request.getParameter(PRM_PLT_DETAIL_DAY);
		pltStationCode = request.getParameter(PRM_PLT_STATION_CODE);
		ckbNeedCalc = request.getParameter(PRM_CKB_NEED_CALC);
	}
	
	/**
	 * ヘッダーNO取得
	 * @throws Exception
	 */
	public String getHeadNo(String code, String[][] aryHeadItem, int idx) throws Exception {
		String retString = "";
		for (String[] element : aryHeadItem) {
			if (code.equals(BonusConst.UP_K_CODE)) {
				setIdxKCode(idx);
			}
			if (code.equals(element[1])) {
				retString = element[0];
				return retString;
			}
		}
		return "";
	}
	
	/**
	 * ヘッダーデータ妥当性チェック
	 * @throws Exception
	 */
	public boolean checkHeadData(String code, String[][] aryHeadItem) throws Exception {
		for (int i = 0; i < aryHeadItem.length; i++) {
			if (code.equals(aryHeadItem[i][1])) {
				return true;
			}
			if (i != aryHeadItem.length - 1) {
				continue;
			}
			return false;
		}
		return true;
	}
	
	/**
	 * 日付の妥当性確認
	 * @throws MospException
	 */
	public boolean checkTargetDate() throws MospException {
		// 妥当性確認開始時処理
		startValidation();
		checkDate(Integer.parseInt(pltYear), Integer.parseInt(pltMonth), Integer.parseInt(pltDay),
				BonusConst.NAM_BONUS_DATE, "");
		// 妥当性確認終了時処理
		endValidation();
		return isValid;
	}
	
	/**
	 * csvキーデータ取得
	 * @throws MospException
	 */
	public void setKeyData(String value, int idx) {
		if (idx == getIdxKCode()) {
			setKCode(value);
		}
		
	}
	
	/**
	 * 計算年月設定
	 * @throws MospException
	 */
	public void setCalcDate() throws MospException {
		Date calDate = getDate(pltYear, pltMonth, String.valueOf(BonusConst.TIME_DEFAULT_DAY));
		setCalcDate(calDate);
	}
	
	/**
	 * 賞与支給日設定
	 * @throws MospException
	 */
	public void setPayday() throws MospException {
		Date payday = getDate(pltYear, pltMonth, pltDay);
		setPayday(payday);
	}
	
	/**
	 * JavaScript設定
	 * @param cls 情報取得クラス
	 * @throws IllegalAccessException
	 */
	@Override
	public String getDirectJs(Class<?> cls) throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDirectJs(cls));
		sb.append("var MODE = ");
		sb.append(mode);
		sb.append(";");
		return sb.toString();
	}
	
	/**
	 * 支給額取得
	 * @param dto
	 */
	public int setAllAllowanceAmount(PdBonusDto dto) {
		int allowanceAmount = dto.getBonusAllowance01() + dto.getBonusAllowance02() + dto.getBonusAllowance03()
				+ dto.getBonusAllowance04() + dto.getBonusAllowance05() + dto.getBonusAllowance06()
				+ dto.getBonusAllowance07() + dto.getBonusAllowance08() + dto.getBonusAllowance09()
				+ dto.getBonusAllowance10() + dto.getBonusAllowance11() + dto.getBonusAllowance12()
				+ dto.getBonusAllowance13() + dto.getBonusAllowance14() + dto.getBonusAllowance15()
				+ dto.getBonusAllowance16() + dto.getBonusAllowance17() + dto.getBonusAllowance18()
				+ dto.getBonusAllowance19() + dto.getBonusAllowance20();
		return allowanceAmount;
	}
	
	/**
	 *
	 * @param dto
	 * @return
	 */
	
	@SuppressWarnings("boxing")
	public int[][] setAryBonusData(PdBonusDto dto) {
		ArrayList<Integer> listItem = new ArrayList<Integer>();
		listItem.add(dto.getBonusAllowance01());
		listItem.add(dto.getBonusAllowance02());
		listItem.add(dto.getBonusAllowance03());
		listItem.add(dto.getBonusAllowance04());
		listItem.add(dto.getBonusAllowance05());
		listItem.add(dto.getBonusAllowance06());
		listItem.add(dto.getBonusAllowance07());
		listItem.add(dto.getBonusAllowance08());
		listItem.add(dto.getBonusAllowance09());
		listItem.add(dto.getBonusAllowance10());
		listItem.add(dto.getBonusAllowance11());
		listItem.add(dto.getBonusAllowance12());
		listItem.add(dto.getBonusAllowance13());
		listItem.add(dto.getBonusAllowance14());
		listItem.add(dto.getBonusAllowance15());
		listItem.add(dto.getBonusAllowance16());
		listItem.add(dto.getBonusAllowance17());
		listItem.add(dto.getBonusAllowance18());
		listItem.add(dto.getBonusAllowance19());
		listItem.add(dto.getBonusAllowance20());
		
		int[][] aryItem = new int[listItem.size()][2];
		for (int i = 0; i < listItem.size(); i++) {
			aryItem[i][0] = i;
			aryItem[i][1] = listItem.get(i);
		}
		return aryItem;
	}
	
	// アクセサメソッド
	public String getPltYear() {
		return pltYear;
	}
	
	public String getPltMonth() {
		return pltMonth;
	}
	
	public String getPltDay() {
		return pltDay;
	}
	
	public FileItem getFilUploadFile() {
		return filUploadFile;
	}
	
	public int getIdxKCode() {
		return idxKCode;
	}
	
	public int getIdxCalcDate() {
		return idxCalcDate;
	}
	
	public String getPltDetailYear() {
		return pltDetailYear;
	}
	
	public String getPltDetailMonth() {
		return pltDetailMonth;
	}
	
	public String getPltDetailDay() {
		return pltDetailDay;
	}
	
	public String getPltStationCode() {
		return pltStationCode;
	}
	
	public String getKCode() {
		return escapeHTML(kCode);
	}
	
	public Date getCalcDate() {
		return getDateField(calcDate);
	}
	
	public Date getPayday() {
		return getDateField(payday);
	}
	
	public String getEscPltStationCode() {
		return escapeHTML(pltStationCode);
	}
	
	public void setPltYear(String pltYear) {
		this.pltYear = pltYear;
	}
	
	public void setPltMonth(String pltMonth) {
		this.pltMonth = pltMonth;
	}
	
	public void setPltDay(String pltDay) {
		this.pltDay = pltDay;
	}
	
	public void setKCode(String kCode) {
		this.kCode = kCode;
	}
	
	public void setCalcDate(Date calcDate) {
		this.calcDate = getDateField(calcDate);
	}
	
	public void setPayday(Date payday) {
		this.payday = getDateField(payday);
	}
	
	public void setIdxKCode(int idxKcode) {
		idxKCode = idxKcode;
	}
	
	public void setIdxCalcDate(int idxCalcDate) {
		this.idxCalcDate = idxCalcDate;
	}
	
	public void setPltStationCode(String pltStationCode) {
		this.pltStationCode = pltStationCode;
	}
	
	public void setPltDetailYear(String pltDetailYear) {
		this.pltDetailYear = pltDetailYear;
	}
	
	public void setPltDetailMonth(String pltDetailMonth) {
		this.pltDetailMonth = pltDetailMonth;
	}
	
	public void setPltDetailDay(String pltDetailDay) {
		this.pltDetailDay = pltDetailDay;
	}
	
	public String getCkbNeedCalc() {
		return getCheckBoxChecked(ckbNeedCalc);
	}
	
	/**
	 * @param mode	セットする mode
	 */
	public void setMode(int mode) {
		this.mode = mode;
	}
	

	/**
	 * チェックボックスパラメータ(チェック時)
	 */
	private static final String	CHECKED	= "on";
	

	/**
	 * チェックボックスパラメータを取得する。
	 * @param value チェックボックスパラメータ値
	 * @return 変換チェックボックスパラメータ値
	 */
	public String getCheckBoxChecked(String value) {
		if (value != null && value.equals(CHECKED)) {
			return "checked=\"checked\"";
		}
		return "";
	}
	
	/**
	 * 法定控除計算要否を取得する。
	 * @return 法定控除計算要否
	 */
	public boolean needCalcLegalDeduction() {
		if (ckbNeedCalc != null && ckbNeedCalc.equals(CHECKED)) {
			return true;
		}
		return false;
	}
	
}
