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

import java.util.Date;

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.StringUtil;
import jp.mosp.common.utils.VariousUtil;
import jp.mosp.kintai.common.action.KintaiCommonAction;
import jp.mosp.payroll.dao.MKintaiKeisanDao;
import jp.mosp.payroll.dto.MKintaiKeisanDto;
import jp.mosp.payroll.vo.AttendanceCalculateStartCardVo;

/**
 * @author yoshida
 *
 */
public class AttendanceCalculateStartCardAction extends KintaiCommonAction {
	
	// コマンド
	private static final String	CMD_START_CARD_SHOW		= "P1020";
	private static final String	CMD_START_CARD_UPDATE	= "P1021";
	

	/**
	 * コンストラクタ
	 */
	public AttendanceCalculateStartCardAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		prepareVo(false);
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_START_CARD_SHOW)) {
			// 表示
			show();
		} else if (cmd.equals(CMD_START_CARD_UPDATE)) {
			// 更新
			update();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new AttendanceCalculateStartCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// PAYROLL権限確認
		confirmPayrollAuth();
		AttendanceCalculateStartCardVo vo = (AttendanceCalculateStartCardVo)getVo();
		// 年度リスト取得
		vo.aryPltKTnen = StringUtil.getYearArray(MospUtility.getYear(getCalcMonth()), 1, 1);
		// 月リスト取得
		vo.aryPltKTtuki = StringUtil.getMonthArray();
	}
	
	@Override
	protected void show() throws Exception {
		AttendanceCalculateStartCardVo vo = (AttendanceCalculateStartCardVo)getVo();
		vo.setDefaultValues();
		MKintaiKeisanDto dto = part().calculate().findForCalc();
		if (VariousUtil.isNull(dto)) {
			dto = new MKintaiKeisanDto();
			Date calcMonth = getCalcAttendanceMonth(DateUtil.getSystemDate());
			dto.setKTNen(MospUtility.getYear(calcMonth));
			dto.setKTTuki(MospUtility.getMonth(calcMonth));
		}
		// 計算対象年月をセット
		vo.setFields(dto);
		// セッションにDTOをセット
		setSessionDto(dto);
	}
	
	@Override
	protected void update() throws Exception {
		// DTOの取得
		MKintaiKeisanDto dto = (MKintaiKeisanDto)getSessionDto();
		AttendanceCalculateStartCardVo vo = (AttendanceCalculateStartCardVo)getVo();
		vo.setParams(request);
		// DAOの準備
		MKintaiKeisanDao dao = part().calculate().getKeisanDao();
		// 更新前データをＧＥＴ
		MKintaiKeisanDto formerDto = dao.findForUpdate();
		// 入力日付
		Date change = DateUtil.getFirstDateOfMonth(vo.getPltKTnen(), vo.getPltKTtuki());
		// 更新する前にdtoが存在するかどうかをチェック
		if (VariousUtil.isNotNull(formerDto)) {
			// 集計日付
			Date current = DateUtil.getFirstDateOfMonth(formerDto.getKTNen(), formerDto.getKTTuki());
			// パラメータの妥当性確認
			vo.validate(formerDto, current, change);
			// 削除
			dao.delete(formerDto);
		}
		// 変更値をDTOにセット
		vo.setDtoFields(dto);
		// 給与計算日をセット
		dto.setKSNen(MospUtility.getYear(getCalcPayment(change)));
		dto.setKSTuki(MospUtility.getMonth(getCalcPayment(change)));
		dto.setKShi(MospUtility.getDay(getCalcPayment(change)));
		// 登録	
		dao.insert(dto);
		// DTOの再取得
		MKintaiKeisanDto retDto = dao.findForKey(dto.getKTNen());
		chkUpdate(retDto);
		vo.setFields(retDto);
		// メッセージ設定
		vo.setMessage(getMessage(MospConst.MSG_UPDATE, ""));
		// コミット
		commit();
		// セッションにDTOをセット
		setSessionDto(retDto);
	}
	
}
