/*
 * 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.StringUtil;
import jp.mosp.common.utils.VariousUtil;
import jp.mosp.kintai.common.action.KintaiCommonAction;
import jp.mosp.kintai.dao.MYukyuDao;
import jp.mosp.kintai.dto.MYukyuDto;
import jp.mosp.kintai.payroll.vo.PaidHolidayOldCardVo;

/**
 * @author yoshida
 *
 */
public class PaidHolidayOldCardAction extends KintaiCommonAction {
	
	// コマンド
	/**
	 * P1118
	 */
	public static final String	CMD_CARD_SELECT	= "P1118";	// 表示
	private static final String	CMD_CARD_UPDATE	= "P1119";	// 更新
															
	
	/**
	 * コンストラクタ
	 */
	public PaidHolidayOldCardAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		prepareVo(false);
		// 
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_CARD_SELECT)) {
			// 表示
			select();
		} else if (cmd.equals(CMD_CARD_UPDATE)) {
			// 更新
			update();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new PaidHolidayOldCardVo();
	}
	
	@Override
	protected void select() throws Exception {
		PaidHolidayOldCardVo vo = (PaidHolidayOldCardVo)getVo();
		// 対象社員設定
		vo.setHidSelectKCode(key1);
		// 対象年月
		vo.setHidSelectYearMonth(key2);
		String[] selectYearMonth = StringUtil.divideByHyphen(vo.getHidSelectYearMonth());
		vo.setHidSelectYear(selectYearMonth[0]);
		vo.setHidSelectMonth(selectYearMonth[1]);
		// 有休情報の表示
		setPaidHolidayInfo();
	}
	
	@Override
	protected void update() throws Exception {
		PaidHolidayOldCardVo vo = (PaidHolidayOldCardVo)getVo();
		vo.setParams(request);
		// パラメータの妥当性確認
		vo.validate();
		int iCalcPayment = DateUtil.convDateToInteger(getCalcPayment(DateUtil.getYearMonth(vo.getHidSelectYear(), vo
			.getHidSelectMonth())));
		// 給与計算対象年月取得
		//String targetDate = DateUtil.convDateToString(getCalcPayment());
		// 登録処理
		// DAO準備
		MYukyuDao dao = part().paidHoliday().getMYukyuDao();
		// DTOの準備
		MYukyuDto dto = new MYukyuDto();
		// 有休情報取得
		MYukyuDto currentDto = dao.findForUpdate(vo.getHidSelectKCode(), iCalcPayment);
		// 有休情報の計算
		calcPaidHoliday();
		if (!chkExistDto(currentDto)) {
			// 有休情報登録
			dto.setKCode(vo.getHidSelectKCode());
			dto.setKyuyoYMD(iCalcPayment);
			dto.setSyutoku(Integer.parseInt(vo.getHidSupply()));
			dto.setHaiki(Double.parseDouble(vo.getHidWaste()));
			dto.setTZanT(Double.parseDouble(vo.getHidThisRemain()));
			dto.setTZanZ(Double.parseDouble(vo.getHidLastRemain()));
			dao.insert(dto);
		} else {
			// 有休情報更新
			dto = currentDto;
			dto.setSyutoku(Integer.parseInt(vo.getHidSupply()));
			dto.setHaiki(Double.parseDouble(vo.getHidWaste()));
			dto.setTZanT(Double.parseDouble(vo.getHidThisRemain()));
			dto.setTZanZ(Double.parseDouble(vo.getHidLastRemain()));
			dao.update(dto);
		}
		// 再取得
		chkUpdate(part().paidHoliday().getYukyuInfo(dto.getKCode(), String.valueOf(dto.getKyuyoYMD())));
		// コミット
		commit();
		// メッセージ設定
		vo.setMessage(getMessage(MospConst.MSG_UPDATE, ""));
		// 有休情報の表示
		setPaidHolidayInfo();
	}
	
	/**
	 * 有休情報の表示
	 * @throws Exception  	例外処理が発生した場合
	 */
	private void setPaidHolidayInfo() throws Exception {
		PaidHolidayOldCardVo vo = (PaidHolidayOldCardVo)getVo();
		vo.setDefaultValues();
		String kCode = vo.getHidSelectKCode();
		// 選択情報の設定
		setLabelBasisInfo(part().human().getBasisInfo(kCode));
		// 対象集計年月
		Date targetMonth = DateUtil.getYearMonth(vo.getHidSelectYear(), vo.getHidSelectMonth());
		vo.setLblSelectYearMonth(DateUtil.convDateToJPYearMonthShort(targetMonth));
		Date calcMonth = getCalcPayment(targetMonth);
		// 対象年月の有給情報を取得
		MYukyuDto currentDto = part().paidHoliday().getYukyuInfo(kCode, calcMonth);
		// 有休情報のチェック
		if (VariousUtil.isNull(currentDto)) {
			// 対象年月の前月分を取得
			currentDto = part().paidHoliday().getYukyuInfo(kCode, DateUtil.addMonth(calcMonth, -1));
		}
		if (VariousUtil.isNull(currentDto)) {
			currentDto = new MYukyuDto();
		}
		// VOに情報を設定
		if (VariousUtil.isNotNull(currentDto)) {
			vo.setFields(currentDto);
		}
		// 有休情報リストの取得
		vo.initListInfo();
		vo.setFields(part().paidHoliday().getMYukyuDao().findForList(kCode, DateUtil.convDateToString(calcMonth)));
		// モード設定
		vo.setMode(PaidHolidayOldCardVo.MODE_UPDATE);
	}
	
	/**
	 * 有休情報計算
	 * @throws Exception  	例外処理が発生した場合
	 */
	private void calcPaidHoliday() throws Exception {
		PaidHolidayOldCardVo vo = (PaidHolidayOldCardVo)getVo();
		// 対象当月保有数前年（変更後）= 当月保有数前年（変更前） + 付与数（前年）- 廃棄数（前年）
		double lastReamin = Double.parseDouble(vo.getHidLastRemain()) + Double.parseDouble(vo.getTxtLastSupply())
				- Double.parseDouble(vo.getTxtLastWaste());
		// 廃棄数が保有数より上回ったら、0に戻す。
		if (lastReamin < 0) {
			double resultLastRemain = Double.parseDouble(vo.getTxtLastWaste()) + lastReamin;
			vo.setTxtLastWaste(String.valueOf(resultLastRemain));
			vo.setHidLastRemain(CommonConst.NAM_COMMON_DOUBLE_INITIAL_VALUE);
		} else {
			vo.setHidLastRemain(String.valueOf(lastReamin));
		}
		// 対象当月保有数当年（変更後）= 当月保有数当年（変更前） + 付与数（前年）- 廃棄数（前年）
		double thisRemain = Double.parseDouble(vo.getHidThisRemain()) + Double.parseDouble(vo.getTxtThisSupply())
				- Double.parseDouble(vo.getTxtThisWaste());
		// 廃棄数が保有数より上回ったら、0に戻す。
		if (thisRemain < 0) {
			double resultThisRemain = Double.parseDouble(vo.getTxtThisWaste()) + thisRemain;
			vo.setTxtThisWaste(String.valueOf(resultThisRemain));
			vo.setHidThisRemain(CommonConst.NAM_COMMON_DOUBLE_INITIAL_VALUE);
		} else {
			vo.setHidThisRemain(String.valueOf(thisRemain));
		}
		// 廃棄数 = 廃棄数（当年）+ 廃棄数（前年）
		double dispose = Double.parseDouble(vo.getHidWaste()) + Double.parseDouble(vo.getTxtThisWaste())
				+ Double.parseDouble(vo.getTxtLastWaste());
		vo.setHidWaste(String.valueOf(dispose));
		// 取得数 = 取得数（当年）+ 取得数（前年）
		int supply = Integer.parseInt(vo.getHidSupply()) + Integer.parseInt(vo.getTxtThisSupply())
				+ Integer.parseInt(vo.getTxtLastSupply());
		vo.setHidSupply(String.valueOf(supply));
	}
	
}
