/*
 * 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.kintai.system.action;

import jp.mosp.common.CommonConst;
import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.common.utils.TimeUtil;
import jp.mosp.kintai.common.action.MosPCodeAction;
import jp.mosp.kintai.system.vo.WorkSystemCardVo;

/**
 * @author yoshida
 *
 */
public class WorkSystemCardAction extends MosPCodeAction {
	
	private static final String	CMD_CARD_SHOW	= "S2030";
	private static final String	CMD_CARD_UPDATE	= "S2038";
	

	/**
	 * コンストラクタ
	 */
	public WorkSystemCardAction() {
		super();
	}
	
	public void action() throws Exception {
		// VOの取得
		prepareVo(false);
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_CARD_SHOW)) {
			// 表示
			show();
		} else if (cmd.equals(CMD_CARD_UPDATE)) {
			// 項目更新
			update();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new WorkSystemCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		// SYSTEM権限確認
		confirmSystemAuth();
		WorkSystemCardVo vo = (WorkSystemCardVo)getVo();
		// プルダウンの設定
		vo.aryPltProcess = getProcessArray();
		vo.aryPltCalcMonth = getCalcMonthArray();
		vo.aryPltProvidedMonth = getProvidedMonthArray();
		vo.aryPltHourUnit = StringUtil.getCountArray(1, 2, 1);
		vo.aryPltRoundNumber = getRoundNumberArray();
		vo.aryPltSubHolidayPeriod = getSubHolidayPeriodArray();
		vo.aryPltSubHolidayPriority = getSubHolidayPriorityArray();
	}
	
	@Override
	protected void show() throws Exception {
		WorkSystemCardVo vo = (WorkSystemCardVo)getVo();
		// 勤怠締月
		vo.setPltCalcMonth(findTchange(CommonConst.TID_CALCULATION, CommonConst.TCD_MONTH));
		// 勤怠締日
		vo.setTxtCalcDays(findTchange(CommonConst.TID_CALCULATION, CommonConst.TCD_DAYS));
		// 給与支給月
		vo.setPltProvidedMonth(findTchange(CommonConst.TID_PROVIDED_DATE, CommonConst.TCD_MONTH));
		// 給与支給日
		vo.setTxtProvidedDays(findTchange(CommonConst.TID_PROVIDED_DATE, CommonConst.TCD_DAYS));
		// 入力単位
		vo.setTxtInputUnit(String.valueOf(part().kintai().getInputUnitTimes()));
		// 自動計算
		vo.setPltAutoCalc(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_AUTOMATIC_CALCULATION));
		// 自動計算処理
		vo.setPltCalcProcess(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_CALCULATION_PROCESS));
		// 全休、半休
		String holidayHours = findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_HOLIDAY_HOURS);
		vo.setTxtAllHolidayHour(holidayHours.substring(0, 2));
		vo.setTxtAllHolidayMin(holidayHours.substring(3, 5));
		vo.setTxtHalfHolidayHour(holidayHours.substring(6, 8));
		vo.setTxtHalfHolidayMin(holidayHours.substring(9, 11));
		// 深夜開始時刻、深夜時間
		String lateNightHours = findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_LATE_NIGHT);
		vo.setTxtLateNightStartHour(lateNightHours.substring(0, 2));
		vo.setTxtLateNightStartMin(lateNightHours.substring(3, 5));
		vo.setTxtLateNightTimeHour(lateNightHours.substring(6, 8));
		vo.setTxtLateNightTimeMin(lateNightHours.substring(9, 11));
		// パスワード有効期限
		vo.setTxtPasswordTerm(String.valueOf(getPasswordTerm()));
		// 残業開始前休憩時間
		int beforeOverTimeInterval = part().kintai().getBeforeOverTimeInterval();
		vo.setTxtBeforeOverTimeHour(TimeUtil.convIntegerTimeToStringHour(beforeOverTimeInterval));
		vo.setTxtBeforeOverTimeMin(TimeUtil.convIntegerTimeToStringMinutes(beforeOverTimeInterval));
		// 残業休憩支給単位時間
		int overTimeUnit = part().kintai().getOverTimeUnitTime();
		vo.setTxtOverTimeUnitHour(TimeUtil.convIntegerTimeToStringHour(overTimeUnit));
		vo.setTxtOverTimeUnitMin(TimeUtil.convIntegerTimeToStringMinutes(overTimeUnit));
		// 残業休憩時間
		int overTimeInterval = part().kintai().getOverTimeInterval();
		vo.setTxtOverTimeIntervalHour(TimeUtil.convIntegerTimeToStringHour(overTimeInterval));
		vo.setTxtOverTimeIntervalMin(TimeUtil.convIntegerTimeToStringMinutes(overTimeInterval));
		// 丸め処理
		vo.setPltRoundNumber(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_ROUND_NUMBER));
		// 月60時間時間外機能
		vo.setPltOverTime60Hour(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_OVER_TIME_60_HOUR));
		// 月60時間時間外による代替休暇機能
		vo.setPltSubstituteHoliday(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_HOLIDAY));
		// 月60時間時間外による代休の有効期間
		vo.setPltSubstitutePeriod(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_PERIOD));
		// 代休の消化優先順位
		vo.setPltSubstitutePriority(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_PRIORITY));
		// 代休情報修正
		//vo.setPltSubstituteAmendment(findTchange(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_AMENDMENT));
		vo.setPltSubstituteAmendment(CommonConst.NAM_COMMON_FLAG_ON);
		// 時間単位年休機能
		vo.setPltHourHolidayFunction(findTchange(CommonConst.TID_PAID_HOLIDAY,
				CommonConst.TCD_HOUR_PAID_HOLIDAY_FUNCTION));
		// 単位時間
		vo.setPltHourHolidayUnit(findTchange(CommonConst.TID_PAID_HOLIDAY, CommonConst.TCD_HOUR_PAID_HOLIDAY_UNIT));
	}
	
	@Override
	protected void update() throws Exception {
		WorkSystemCardVo vo = (WorkSystemCardVo)getVo();
		// 項目入力値取得
		vo.setParams(request);
		// パラメータの妥当性確認
		vo.validate();
		// 勤怠締月
		regist(CommonConst.TID_CALCULATION, CommonConst.TCD_MONTH, CommonConst.TNM_MONTH_MINUS, vo.getPltCalcMonth());
		// 勤怠締日
		regist(CommonConst.TID_CALCULATION, CommonConst.TCD_DAYS, CommonConst.TNM_PERIOD_DATE, vo.getTxtCalcDays());
		// 給与支給月
		regist(CommonConst.TID_PROVIDED_DATE, CommonConst.TCD_MONTH, CommonConst.TNM_MONTH_PLUS, vo
			.getPltProvidedMonth());
		// 給与支給日
		regist(CommonConst.TID_PROVIDED_DATE, CommonConst.TCD_DAYS, CommonConst.TNM_PROVIDED_DATE, vo
			.getTxtProvidedDays());
		// 入力単位
		regist(CommonConst.TID_INPUT_UNIT_TIMES, CommonConst.TCD_MINUTES, CommonConst.TNM_AMOUNT_UNIT, vo
			.getTxtInputUnit());
		// 自動計算
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_AUTOMATIC_CALCULATION, CommonConst.TNM_AMOUNT_UNIT, vo
			.getPltAutoCalc());
		// 自動計算処理
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_CALCULATION_PROCESS, "", vo.getPltCalcProcess());
		// 全休、半休	
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_HOLIDAY_HOURS, CommonConst.TNM_AMOUNT_UNIT,
				convStringToTimeCode(vo.getTxtAllHolidayHour(), vo.getTxtAllHolidayMin(), vo.getTxtHalfHolidayHour(),
						vo.getTxtHalfHolidayMin()));
		// 深夜開始時刻、深夜時間
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_LATE_NIGHT, CommonConst.TNM_AMOUNT_UNIT,
				convStringToTimeCode(vo.getTxtLateNightStartHour(), vo.getTxtLateNightStartMin(), vo
					.getTxtLateNightTimeHour(), vo.getTxtLateNightTimeMin()));
		// パスワード有効期限
		regist(CommonConst.TID_PASSWORD_TERM, "", "", vo.getTxtPasswordTerm());
		// 残業開始前休憩時間
		regist(CommonConst.TID_OVER_TIME_INTERVAL, CommonConst.TCD_BEFORE_OVER_TIME_INTERVAL_TIME, "", String
			.valueOf(TimeUtil.convStringTimeToIntegerTime(vo.getTxtBeforeOverTimeHour(), vo.getTxtBeforeOverTimeMin())));
		// 残業休憩支給単位時間
		regist(CommonConst.TID_OVER_TIME_INTERVAL, CommonConst.TCD_OVER_TIME_UNIT_TIME, "", String.valueOf(TimeUtil
			.convStringTimeToIntegerTime(vo.getTxtOverTimeUnitHour(), vo.getTxtOverTimeUnitMin())));
		// 残業休憩時間
		regist(CommonConst.TID_OVER_TIME_INTERVAL, CommonConst.TCD_OVER_TIME_INTERVAL_TIME, "", String.valueOf(TimeUtil
			.convStringTimeToIntegerTime(vo.getTxtOverTimeIntervalHour(), vo.getTxtOverTimeIntervalMin())));
		// 丸め処理
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_ROUND_NUMBER, "", vo.getPltRoundNumber());
		// 月60時間時間外機能
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_OVER_TIME_60_HOUR, "", vo.getPltOverTime60Hour());
		// 月60時間時間外による代替休暇機能
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_HOLIDAY, "", vo.getPltSubstituteHoliday());
		// 代休の有効期間
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_PERIOD, "", vo.getPltSubstitutePeriod());
		// 代休の消化優先順位
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_PRIORITY, "", vo.getPltSubstitutePriority());
		// 代休情報修正
		regist(CommonConst.TID_WORK_SYSTEM, CommonConst.TCD_SUBSTITUTE_AMENDMENT, "", vo.getPltSubstituteAmendment());
		// 時間単位年休機能
		regist(CommonConst.TID_PAID_HOLIDAY, CommonConst.TCD_HOUR_PAID_HOLIDAY_FUNCTION, "", vo
			.getPltHourHolidayFunction());
		// 単位時間
		regist(CommonConst.TID_PAID_HOLIDAY, CommonConst.TCD_HOUR_PAID_HOLIDAY_UNIT, "", vo.getPltHourHolidayUnit());
		// コミット
		commit();
		// メッセージ設定
		setMessage(getMessage(MospConst.MSG_UPDATE, ""));
		// 表示
		show();
	}
	
	/**
	 * @return 勤怠締日対象月配列
	 */
	private String[][] getCalcMonthArray() {
		return new String[][]{ { CommonConst.NUM_CUT_OFF_MONTH_THIS, CommonConst.NAM_CUT_OFF_MONTH_THIS },
			{ CommonConst.NUM_CUT_OFF_MONTH_FORMER, CommonConst.NAM_CUT_OFF_MONTH_FORMER } };
	}
	
	/**
	 * @return 給与支給日対象月配列
	 */
	private String[][] getProvidedMonthArray() {
		return new String[][]{ { CommonConst.NUM_PROVIDED_MONTH_THIS, CommonConst.NAM_PROVIDED_MONTH_THIS },
			{ CommonConst.NUM_PROVIDED_MONTH_FUTURE, CommonConst.NAM_PROVIDED_MONTH_FUTURE } };
	}
	
	/**
	 * @return 自動計算処理配列
	 */
	private String[][] getProcessArray() {
		return StringUtil.getFlagArray(CommonConst.NAM_ATTENDANCE_INPUT, CommonConst.NAM_ATTENDANCE_COMPILE);
	}
	
	/**
	 * @return 丸め処理配列
	 */
	private String[][] getRoundNumberArray() {
		return StringUtil.getFlagArray(CommonConst.NAM_ATTENDANCE_INPUT, CommonConst.NAM_ATTENDANCE_COMPILE);
	}
	
	private String[][] getSubHolidayPeriodArray() {
		return StringUtil.getFlagArray("1ヶ月", "2ヶ月");
	}
	
	private String[][] getSubHolidayPriorityArray() {
		return StringUtil.getFlagArray("平日優先", "所定休日優先");
	}
	
}
