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

import java.util.Date;

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.DateUtil;
import jp.mosp.common.utils.NumberUtil;
import jp.mosp.common.utils.TimeUtil;
import jp.mosp.common.utils.VariousUtil;
import jp.mosp.kintai.common.action.KintaiCommonAction;
import jp.mosp.kintai.payroll.dao.KdSubstituteHolidayDao;
import jp.mosp.kintai.payroll.dto.KdSubstituteHolidayDto;
import jp.mosp.kintai.payroll.dto.KdWorkDto;
import jp.mosp.kintai.payroll.vo.SubHolidayAttitudeCardVo;

/**
 * @author matsusita
 *
 */
public class SubHolidayAttitudeCardAction extends KintaiCommonAction {
	
	// コマンド
	/**
	 * 
	 */
	public static final String	CMD_ATTITUDE_CARD_SHOW		= "K1150";
	private static final String	CMD_ATTITUDE_CARD_DRAFT		= "K1152";
	private static final String	CMD_ATTITUDE_CARD_UPDATE	= "K1153";
	

	/**
	 * コンストラクタ
	 */
	public SubHolidayAttitudeCardAction() {
		super();
	}
	
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_ATTITUDE_CARD_SHOW)) {
			// 表示
			show();
		} else if (cmd.equals(CMD_ATTITUDE_CARD_DRAFT)) {
			// 下書き
			regist(true);
		} else if (cmd.equals(CMD_ATTITUDE_CARD_UPDATE)) {
			regist(false);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new SubHolidayAttitudeCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		SubHolidayAttitudeCardVo vo = (SubHolidayAttitudeCardVo)getVo();
		// ユーザー情報取得
		getInfo(user.getUserId());
		vo.aryPltAttitudeStatus = getAttitudeChoiceArray();
	}
	
	@Override
	protected void show() throws Exception {
		SubHolidayAttitudeCardVo vo = (SubHolidayAttitudeCardVo)getVo();
		// 初期化
		vo.setDefaultValues();
		Date todayDate = DateUtil.getSystemDate();
		// 労使協定情報
		setLaborAgreementInfo(userBasis.getKCode(), todayDate);
		// DTOの準備
		KdSubstituteHolidayDto dto = part().subHoliday().getCloseDate(userBasis.getKCode(), todayDate);
		if (VariousUtil.isNull(dto)) {
			// モード設定
			vo.setMode(SubHolidayAttitudeCardVo.MODE_DISABLED);
			// メッセージ設定
			setMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		} else {
			vo.setFields(dto);
			if (dto.getPaymentAcquireTime() + dto.getPaymentAcquireHoliday() == 0) {
				// 代替休暇取得時間外 + 代替休暇取得休日時間外が0の場合
				vo.setPltAttitudeStatus(CommonConst.CODE_ATTITUDE_CHOICE_PREMIUM);
			} else if (dto.getPaymentQuitTime() == 0) {
				// 代替休暇放棄時間外が0の場合
				vo.setPltAttitudeStatus(CommonConst.CODE_ATTITUDE_CHOICE_SUBSTITUTE);
			} else {
				vo.setPltAttitudeStatus(CommonConst.CODE_ATTITUDE_CHOICE_COMBINATION);
			}
			if (VariousUtil.isNotNull(agreement)) {
				// 平日換算率
				double rate = Double.valueOf(agreement.getSubQuitRate() - agreement.getSubAcquireRate())
						/ CommonConst.NUM_PERCENTAGE;
				vo.setHidRate(NumberUtil.getNumberString(rate, 1, 2));
				// 所定休日換算率
				double holidayRate = Double.valueOf(agreement.getSubQuitRate() - agreement.getSubAcquireHolidayRate())
						/ CommonConst.NUM_PERCENTAGE;
				vo.setHidHolidayRate(NumberUtil.getNumberString(holidayRate, 1, 2));
				KdWorkDto workDto = part().compile().getKdWorkInfo(userBasis.getKCode(), dto.getOccurYear(),
						dto.getOccurMonth());
				if (VariousUtil.isNotNull(workDto)) {
					int convertedOverTime = Double.valueOf(workDto.getMonthlyOverTime() * rate).intValue();
					int convertedWeekHoliday = Double.valueOf(workDto.getMonthlyWeekHoliday() * holidayRate).intValue();
					vo.setHidConvertedMonthlyOverTimeHour(TimeUtil.convIntegerTimeToStringHour(convertedOverTime));
					vo.setHidConvertedMonthlyOverTimeMin(TimeUtil.convIntegerTimeToStringMinutes(convertedOverTime));
					vo
						.setHidConvertedMonthlyWeekHolidayHour(TimeUtil
							.convIntegerTimeToStringHour(convertedWeekHoliday));
					vo.setHidConvertedMonthlyWeekHolidayMin(TimeUtil
						.convIntegerTimeToStringMinutes(convertedWeekHoliday));
				}
			}
			// モード設定
			vo.setMode(SubHolidayAttitudeCardVo.MODE_POSSIBLE);
			// セッションにDTOをセット
			setSessionDto(dto);
		}
	}
	
	private void regist(boolean isDraft) throws Exception {
		// DTOの取得
		KdSubstituteHolidayDto dto = (KdSubstituteHolidayDto)getSessionDto();
		SubHolidayAttitudeCardVo vo = (SubHolidayAttitudeCardVo)getVo();
		vo.setParams(request);
		// パラメータの妥当性確認
		vo.validate();
		// DTOの準備
		vo.setDtoFields(dto);
		// 下書き判断
		if (isDraft) {
			dto.setAttitudeStatus(CommonConst.CODE_ATTITUDE_STATUS_INIT);
		} else {
			// 確認済みへ変更
			dto.setAttitudeStatus(CommonConst.CODE_ATTITUDE_STATUS_FIXED);
			// 全て割増賃金の場合、取得済みへ変更する。
			if (CommonConst.CODE_ATTITUDE_CHOICE_PREMIUM.equals(vo.getPltAttitudeStatus())) {
				dto.setAcquisitionStatus(CommonConst.CODE_ACQUISITION_STATUS_FINISH);
			} else {
				dto.setAcquisitionStatus(CommonConst.CODE_ACQUISITION_STATUS_REMAIN);
			}
		}
		// DAOの準備
		KdSubstituteHolidayDao dao = part().subHoliday().getKdSubstituteHolidayDao();
		// 排他確認
		chkExclusive(dto, dao.findForUpdate(dto.getSubstituteId()));
		// データの更新
		dao.update(dto);
		// DTOの再取得
		KdSubstituteHolidayDto retDto = dao.findForKey(dto.getSubstituteId());
		chkUpdate(retDto);
		// コミット
		commit();
		// モード設定
		vo.setMode(SubHolidayAttitudeCardVo.MODE_POSSIBLE);
		// メッセージ設定
		setMessage(getMessage(MospConst.MSG_UPDATE, ""));
		// セッションにDTOをセット
		setSessionDto(retDto);
	}
	
	private String[][] getAttitudeChoiceArray() {
		return new String[][]{ { CommonConst.CODE_ATTITUDE_CHOICE_PREMIUM, CommonConst.NAM_ATTITUDE_CHOICE_PREMIUM },
			{ CommonConst.CODE_ATTITUDE_CHOICE_COMBINATION, CommonConst.NAM_ATTITUDE_CHOICE_COMBINATION },
			{ CommonConst.CODE_ATTITUDE_CHOICE_SUBSTITUTE, CommonConst.NAM_ATTITUDE_CHOICE_SUBSTITUTE } };
	}
	
}
