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

import java.util.Date;
import java.util.Map;

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.common.MospUtility;
import jp.mosp.common.utils.DateUtil;
import jp.mosp.common.utils.NumberUtil;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.common.utils.TimeUtil;
import jp.mosp.kintai.common.action.MosPWorkflowAction;
import jp.mosp.kintai.common.part.KintaiApprovalPart;
import jp.mosp.kintai.common.part.LaborAgreementPart;
import jp.mosp.kintai.common.part.PaidHolidayPart;
import jp.mosp.workflow.dto.KtHolidayRequestDto;
import jp.mosp.workflow.vo.HolidayApprovalCardVo;

/**
 * @author yoshida
 *
 */
public class HolidayApprovalCardAction extends MosPWorkflowAction {
	
	// コマンド
	/**
	 * 
	 */
	public static final String	CMD_CARD_SELECT		= "K2141";
	private static final String	CMD_CARD_APPROVED	= "K2142";
	private static final String	CMD_CARD_RETURNED	= "K2143";
	

	/**
	 * コンストラクタ
	 */
	public HolidayApprovalCardAction() {
		super();
	}
	
	public void action() throws Exception {
		// VO準備
		prepareVo(false);
		// 初期処理
		init();
		// コマンド毎の処理
		if (CMD_CARD_SELECT.equals(cmd)) {
			// 表示(キーで情報を検索)
			select();
		} else if (CMD_CARD_APPROVED.equals(cmd)) {
			// 承認
			approved();
		} else if (CMD_CARD_RETURNED.equals(cmd)) {
			// 差戻
			returned();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new HolidayApprovalCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		// ユーザ情報の取得
		getInfo(user.getUserId());
	}
	
	@Override
	protected void select() throws Exception {
		// データの取得
		KtHolidayRequestDto dto = holidayDao().findForKey(NumberUtil.getIntParam(key2));
		chkExist(dto);
		// VOに情報をセット
		setFields(dto);
		// 承認状況の設定
		setApprovalStatus(dto);
	}
	
	/**
	 * 承認処理
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void approved() throws Exception {
		// DTOの取得
		KtHolidayRequestDto dto = (KtHolidayRequestDto)getSessionDto();
		// 休暇申請承認処理
		if (runHolidayRequestApproved(dto)) {
			// コミット
			commit();
			// メッセージの表示
			setMessage(getMessage(CommonConst.MSG_CODE_WORKFLOW_UPDATE, ""));
		}
		// VOに情報をセット
		setFields(dto);
		// 承認状況の設定
		setApprovalStatus(dto);
	}
	
	/**
	 * 差戻処理
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void returned() throws Exception {
		// DTOの取得
		KtHolidayRequestDto dto = (KtHolidayRequestDto)getSessionDto();
		HolidayApprovalCardVo vo = (HolidayApprovalCardVo)getVo();
		vo.setParams(request);
		// パラメータの妥当性確認
		vo.validate();
		// 差戻処理
		if (runWorkflowReturnProcess(dto.getKCode(), dto.getHolidayDate(), dto.getWorkflow(),
				CommonConst.NAM_HOLIDAY_REQUEST, vo.getTxtComment())) {
			// コミット
			commit();
			// メッセージの表示
			setMessage(getMessage(CommonConst.MSG_CODE_WORKFLOW_UPDATE, ""));
		}
		// VOに情報をセット
		setFields(dto);
		// 承認状況の設定
		setApprovalStatus(dto);
	}
	
	/**
	 * 承認状況情報の設定
	 * @param dto			対象DTO
	 * @throws Exception  	例外処理が発生した場合
	 */
	private void setApprovalStatus(KtHolidayRequestDto dto) throws Exception {
		HolidayApprovalCardVo vo = (HolidayApprovalCardVo)getVo();
		setLabelBasisInfo(part().human().getBasisInfo(dto.getKCode()));
		String statusCode = toKintaiApprovalStatus(dto.getWorkflow());
		vo.setLblApprovalStatus(getApprovalName(statusCode));
		// モード設定
		vo.setMode(HolidayApprovalCardVo.MODE_VIEW);
		// 承認者確認
		if (part().kintaiApproval().isApprover(dto.getKCode(), userBasis.getKCode(), statusCode, dto.getHolidayDate())) {
			vo.setMode(HolidayApprovalCardVo.MODE_SELECT);
			if (KintaiApprovalPart.is1stApproved(statusCode)) {
				vo.setMode(HolidayApprovalCardVo.MODE_APPROVED);
			}
		}
		// 差戻しコメント一覧
		setReturnedComment(dto.getKCode(), dto.getWorkflow());
		// セッションにDTOをセット
		setSessionDto(dto);
	}
	
	/**
	 * フィールド設定
	 * @param dto 設定対象DTO
	 * @throws Exception   	例外処理が発生した場合
	 */
	public void setFields(KtHolidayRequestDto dto) throws Exception {
		HolidayApprovalCardVo vo = (HolidayApprovalCardVo)getVo();
		vo.setHidVoucher(String.valueOf(dto.getVoucher()));
		Date holidayDate = dto.getHolidayDate();
		vo.setLblHolidayDate(DateUtil.convDateToFormatDateJP(holidayDate));
		StringBuffer sb = new StringBuffer();
		if (dto.getEndTime() - dto.getStartTime() > 0) {
			sb.append(TimeUtil.convIntegerTimeToStringTime(dto.getStartTime()));
			sb.append(CommonConst.STR_TERM);
			sb.append(TimeUtil.convIntegerTimeToStringTime(dto.getEndTime()));
		}
		// 休暇理由名称取得
		vo.setLblHolidayTime(sb.toString());
		vo.setLblReason(part().holidayRequest().getReasonNameWithType(dto, false));
		vo.setLblReqestDate(DateUtil.convDateToFormatDateJP(dto.getInsertDate()));
		vo.setLblCauseComment(dto.getCauseComment());
		// 有休情報対応
		Map<String, Double> map = part().paidHoliday().getAvairablePaidHolidayMap(dto.getKCode(),
				MospUtility.getYear(holidayDate), MospUtility.getMonth(holidayDate));
		vo.setLblTotalRemainDays("");
		vo.setLblTotalRemainHour("");
		vo.setUseHourHoliday(part().mospcode().useHourPaidHolidayFunction());
		sb = new StringBuffer();
		sb.append(PaidHolidayPart.getMapLastRemainDays(map) + PaidHolidayPart.getMapRemainDays(map));
		if (vo.isUseHourHoliday()) {
			// 労使協定情報
			setLaborAgreementInfo(userBasis.getKCode(), holidayDate);
			int prescribedTime = LaborAgreementPart.getPrescribedTime(agreement);
			int prescribedHour = Double.valueOf(Math.ceil(TimeUtil.convMinutesToDoubleHour(prescribedTime))).intValue();
			sb.append(StringUtil.surroundRoundBracket(String.valueOf(PaidHolidayPart.getMapPossible(map)
					/ prescribedHour)));
			vo.setLblTotalRemainHour(String.valueOf(PaidHolidayPart.getMapPossible(map) % prescribedHour));
		}
		vo.setLblTotalRemainDays(sb.toString());
		// 代休情報対応
		double subRemainDays = part().subHoliday().getRemainSubstituteHoliday(dto.getKCode(), holidayDate);
		vo.setLblSubstituteRemain("");
		vo.setLblSubstituteRemain(NumberUtil.getNumberString(subRemainDays, 3, 1));
		vo.setUseSubstitute(part().mospcode().useSubstituteHolidayFunction());
	}
	
}
