/*
 * 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 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.NumberUtil;
import jp.mosp.common.utils.TimeUtil;
import jp.mosp.kintai.common.action.MosPWorkflowAction;
import jp.mosp.kintai.common.part.KintaiApprovalPart;
import jp.mosp.workflow.dto.CtOvertimeRequestDto;
import jp.mosp.workflow.vo.OverTimeApprovalCardVo;

/**
 * @author yoshida
 *
 */
public class OverTimeApprovalCardAction extends MosPWorkflowAction {
	
	// コマンド
	/**
	 * K2136
	 */
	public static final String	CMD_CARD_SELECT		= "K2136";
	private static final String	CMD_CARD_APPROVED	= "K2137";
	private static final String	CMD_CARD_RETURNED	= "K2138";
	

	/**
	 * コンストラクタ
	 */
	public OverTimeApprovalCardAction() {
		super();
	}
	
	public void action() throws Exception {
		// VO準備
		prepareVo(false);
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_CARD_SELECT)) {
			// 表示(キーで情報を検索)
			select();
		} else if (cmd.equals(CMD_CARD_APPROVED)) {
			// 承認
			approved();
		} else if (cmd.equals(CMD_CARD_RETURNED)) {
			// 差戻
			returned();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new OverTimeApprovalCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		// ユーザ情報の取得
		getInfo(user.getUserId());
	}
	
	@Override
	protected void select() throws Exception {
		OverTimeApprovalCardVo vo = (OverTimeApprovalCardVo)getVo();
		// データの取得
		CtOvertimeRequestDto dto = getOverTimeRequest(NumberUtil.getIntParam(key2));
		chkExist(dto);
		// VOに情報をセット
		vo.setFields(dto);
		// 承認状況の設定
		setApprovalStatus(dto);
	}
	
	/**
	 * 承認処理
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void approved() throws Exception {
		// DTOの取得
		CtOvertimeRequestDto dto = (CtOvertimeRequestDto)getSessionDto();
		OverTimeApprovalCardVo vo = (OverTimeApprovalCardVo)getVo();
		// 残業申請承認処理
		if (runOverTimeRequestApproved(dto)) {
			// コミット
			commit();
			// メッセージの表示
			setMessage(getMessage(CommonConst.MSG_CODE_WORKFLOW_UPDATE, ""));
		}
		// VOに情報をセット
		vo.setFields(dto);
		// 承認状況の設定
		setApprovalStatus(dto);
	}
	
	/**
	 * 差戻処理
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void returned() throws Exception {
		// DTOの取得
		CtOvertimeRequestDto dto = (CtOvertimeRequestDto)getSessionDto();
		OverTimeApprovalCardVo vo = (OverTimeApprovalCardVo)getVo();
		vo.setParams(request);
		// パラメータの妥当性確認
		vo.validate();
		// 差戻し処理
		if (runWorkflowReturnProcess(dto.getKCode(), dto.getWorkDate(), dto.getWorkflow(),
				CommonConst.NAM_OVER_TIME_REQUEST, vo.getTxtComment())) {
			// コミット
			commit();
			// メッセージの表示
			vo.setMessage(getMessage(CommonConst.MSG_CODE_WORKFLOW_UPDATE, ""));
		}
		// VOに情報をセット
		vo.setFields(dto);
		// 承認状況の設定
		setApprovalStatus(dto);
	}
	
	/**
	 * 承認状況情報の設定
	 * @param dto			対象DTO
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void setApprovalStatus(CtOvertimeRequestDto dto) throws Exception {
		OverTimeApprovalCardVo vo = (OverTimeApprovalCardVo)getVo();
		setLabelBasisInfo(part().human().getBasisInfo(dto.getKCode()));
		String statusCode = toKintaiApprovalStatus(dto.getWorkflow());
		vo.setLblApprovalStatus(getApprovalName(statusCode));
		// モード設定
		vo.setMode(OverTimeApprovalCardVo.MODE_VIEW);
		// 承認者確認
		if (part().kintaiApproval().isApprover(dto.getKCode(), userBasis.getKCode(), statusCode, dto.getWorkDate())) {
			vo.setMode(OverTimeApprovalCardVo.MODE_SELECT);
			if (KintaiApprovalPart.is1stApproved(statusCode)) {
				vo.setMode(OverTimeApprovalCardVo.MODE_APPROVED);
			}
		}
		// 差戻しコメント一覧
		setReturnedComment(dto.getKCode(), dto.getWorkflow());
		// 対象年月の残業時間の合計		
		int overMonthly = part().kintai().getOverTimeForPeriod(dto.getKCode(), getStartDateForList(dto.getWorkDate()),
				getEndDateForList(dto.getWorkDate()));
		// 労使協定情報設定
		setLaborAgreementInfo(dto.getKCode(), dto.getWorkDate());
		vo.setCaution(checkCautionOverTime(overMonthly, 0));
		vo.setWarning(checkWarningOverTime(overMonthly, 0));
		vo.setLblOverTimeMonthly(TimeUtil.convIntegerTimeToJPHours(overMonthly));
		// セッションにDTOをセット
		setSessionDto(dto);
	}
	
}
