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

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

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.kintai.common.action.AttendanceTotalAction;
import jp.mosp.kintai.common.part.KintaiApprovalPart;
import jp.mosp.kintai.common.part.KintaiCommonPart;
import jp.mosp.kintai.common.part.PaidHolidayPart;
import jp.mosp.kintai.dto.MSyukkinboDto;
import jp.mosp.kintai.dto.MYukyuDto;
import jp.mosp.kintai.vo.PaidHolidayCardVo;

/**
 * @author yoshida
 *
 */
public class PaidHolidayCardAction extends AttendanceTotalAction {
	
	// コマンド
	private static final String	CMD_YUKYU_PERSONAL_SHOW	= "K1300";	// 表示
																	
	
	/**
	 * コンストラクタ
	 */
	public PaidHolidayCardAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_YUKYU_PERSONAL_SHOW)) {
			// 表示
			show();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new PaidHolidayCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		// ユーザー情報取得  社員コード摂取
		getInfo(user.getUserId());
	}
	
	@Override
	protected void show() throws Exception {
		PaidHolidayCardVo vo = (PaidHolidayCardVo)getVo();
		vo.setDefaultValues();
		// 対象年月を取得
		Date calcPayment = getCalcPayment();
		// 対象年月の承認・未承認件数作成
		double[] number = makeApprovalYukyuNumber(calcPayment);
		// 対象年月の追加・廃棄予定作成
		makeAddAndDispose(calcPayment, number[0]);
		// VOに情報を設定
		vo.setLblApproved(String.valueOf(number[0]));
		vo.setLblRemainApproved(String.valueOf(number[1]));
		// VOに情報を設定
		vo.setLblAttendanceCalcY(String.valueOf(MospUtility.getYear(calcPayment)));
		vo.setLblAttendanceCalcM(String.valueOf(MospUtility.getMonth(calcPayment)));
	}
	
	/**
	 * 有休承認・未承認件数作成
	 * @param targetDate	対象年月日
	 * @return [0]…承認済み件数、[1]…未承認件数
	 * @throws Exception 	例外処理が発生した場合
	 */
	private double[] makeApprovalYukyuNumber(Date targetDate) throws Exception {
		// 勤怠集計対象月の出勤簿の取得
		getAttendanceList(userBasis.getKCode(), getCalcMonth());
		// 配列初期化
		double[] number = { 0.0, 0.0 };
		// 出勤簿日集計データ
		for (MSyukkinboDto dto : attendanceList) {
			// 有休休暇
			if (KintaiCommonPart.isAllHoliday(dto)) {
				// 承認、未承認数
				if (KintaiApprovalPart.is2ndApproved(dto)) {
					number[0]++;
				} else {
					number[1]++;
				}
			} else if (KintaiCommonPart.isHalfHoliday(dto)) {
				// 半休
				final double halfDays = 0.5;
				// 承認、未承認数
				if (KintaiApprovalPart.is2ndApproved(dto)) {
					number[0] += halfDays;
				} else {
					number[1] += halfDays;
				}
			}
		}
		return number;
	}
	
	/**
	 * 有休追加・廃棄件数作成
	 * @param calcPayment	給与支給日
	 * @param useDays 対象年月の承認件数作成
	 * @throws Exception	例外処理が発生した場合
	 */
	private void makeAddAndDispose(Date calcPayment, double useDays) throws Exception {
		PaidHolidayCardVo vo = (PaidHolidayCardVo)getVo();
		// 前月の有給情報検索
		MYukyuDto formerDto = part().paidHoliday().getYukyuInfo(userBasis.getKCode(),
				DateUtil.addMonth(calcPayment, -1));
		if (!chkExistDto(formerDto)) {
			formerDto = new MYukyuDto();
		}
		// 繰越処理フラグ
		boolean isCarryForward = part().mospcode().getCarriedForwardMonth() == MospUtility.getMonth(calcPayment);
		// 支給日数
		int payment = 0;
		if (isCarryForward) {
			payment = part().paidHoliday().getCarryForwardSupplyDays(userBasis.getKCode());
		}
		// 前月残日数（前年度分）
		double lastRemainDays = formerDto.getTZanZ();
		// 前月残日数（当年度分）
		double remainDays = formerDto.getTZanT();
		// 繰越処理結果取得
		Map<String, Double> map = part().paidHoliday().getOldCarryForwardInfo(lastRemainDays, remainDays, useDays,
				payment, isCarryForward);
		// 破棄日数
		double waste = PaidHolidayPart.getMapWasteDays(map);
		// 来月有休予定数（当年度分）
		double remainThisThis = PaidHolidayPart.getMapRemainDays(map);
		// 来月有休予定数（前年度分）
		double remainThisPrev = PaidHolidayPart.getMapLastRemainDays(map);
		// VOに情報を設定
		vo.setLblThisMGet(String.valueOf(payment));
		vo.setLblThisMDispose(String.valueOf(waste));
		vo.setLblThisMRestLY(String.valueOf(remainThisPrev));
		vo.setLblThisMRestTY(String.valueOf(remainThisThis));
		vo.setLblPaidHolidayLY(String.valueOf(lastRemainDays));
		vo.setLblPaidHolidayTY(String.valueOf(remainDays));
	}
	
}
