/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2008 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 jp.mosp.common.CommonConst;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;

import jp.mosp.human.dto.MKihonDto;

import jp.mosp.kintai.vo.AttendanceListVo;
import jp.mosp.kintai.vo.AttendanceMenuVo;

import jp.mosp.system.dto.MYakusyokuDto;

public class AttendanceListAction extends AttendanceTotalAction {

	// コマンド
	public	static final String CMD_ATTENDANCE_LIST_SHOW		= "K1010";
	private static final String CMD_ATTENDANCE_LIST_SELECT	= "K1011";
	private static final String CMD_ATTENDANCE_LIST_SEARCH	= "K1012";
	public  static final String CMD_ATTENDANCE_LIST_RE_SEARCH	= "K1013";
	public  static final String CMD_ATTENDANCE_CARD_SELECT  	= "K1014";

	/**
	 * コンストラクタ
	 */
	public AttendanceListAction() {
		super();
	}

	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		AttendanceListVo vo;
		vo = (AttendanceListVo)getStoredVo(CommonConst.VO_ATTENDANCE_LIST);
		if (vo == null) {
			vo = new AttendanceListVo();
		}
		vo.initVo(cfg, msg, cmd, aspUser, user);
		vo.clearMessage();
		vo.clearErrField();
		storeVo(CommonConst.VO_ATTENDANCE_LIST, vo);
		// VO及びフォワード先URLの設定
		setVo(vo);
		setUrl(vo.getRetUrl());
		// DBコネクション取得
		getConnection();
		// ユーザー情報取得
		getInfo(user.getUserId());
		// 表示月
		Date current = util.getSystemDate();
		// 勤怠管理コマンド
		if (getStoredVo(CommonConst.VO_EMPLOYEE_ATTENDANCE_LIST) != null) {
			// 状態遷移確認
			vo.setCmdMenuButton(vo.CMD_MENU_MAMAGE);
			vo.setCmdListButton(EmployeeAttendanceListAction.CMD_EMPLOYEE_LIST_RE_SEARCH);
			vo.setNamListButton(CommonConst.TTL_EMPLOYEE_SELECT);
		} else {
			vo.setCmdMenuButton(vo.CMD_MENU_INPUT);
			vo.setCmdListButton(vo.CMD_MENU_INPUT);
			vo.setNamListButton(CommonConst.NAM_MENU);			
		}
		// プルダウン取得
		vo.aryPltYear  = util.getYearArray(MospUtility.getYear (current), 2, 1);
		vo.aryPltMonth = util.getMonthArray();
		// 勤怠承認機能確認
		if (isAttendanceApprovalFunction()) {
			vo.setMode(AttendanceListVo.MODE_POSSIBLE);
		} else {
			vo.setMode(AttendanceListVo.MODE_IMPOSSIBLE);
		}
		// コマンド毎の処理
		if (cmd.equals(CMD_ATTENDANCE_LIST_SHOW)) {
			// 表示
			// 対象社員設定
			vo.setSelectKCode(mKihon.getKCode());
			// 対象年月日設定
			vo.setPltYear(String.valueOf(MospUtility.getYear(current)));
			vo.setPltMonth(String.valueOf(MospUtility.getMonth(current)));
			/*						
			 *  	2009/01/20	m.yoshida	締め日対応
			 */
			vo.setHidDay(String.valueOf(MospUtility.getDay(current)));
			// 検索
			searchAttendanceList(vo);
		} else if (cmd.equals(CMD_ATTENDANCE_LIST_SELECT)) {
			// 選択
			// 対象社員設定
			vo.setSelectKCode(key1);
			// 対象年月日設定
			vo.setPltYear(String.valueOf(MospUtility.getYear(current)));
			vo.setPltMonth(String.valueOf(MospUtility.getMonth(current)));
			/*						
			 *  	2009/01/20	m.yoshida	締め日対応
			 */
			vo.setHidDay(String.valueOf(MospUtility.getDay(current)));
			// 検索
			searchAttendanceList(vo);
		} else if (cmd.equals(CMD_ATTENDANCE_LIST_SEARCH)) {
			// 対象年月日設定
			vo.setParams(request);
			// 検索
			searchAttendanceList(vo);
		} else if (cmd.equals(CMD_ATTENDANCE_LIST_RE_SEARCH)) {
			// 再検索
			session.removeAttribute(MospConst.ATT_DTO);
			AttendanceMenuVo menuVo = (AttendanceMenuVo)getStoredVo(CommonConst.VO_ATTENDANCE_MENU);
			// 対象社員設定
			vo.setSelectKCode(menuVo.getHidKCode());
			// 対象年月日設定
			vo.setPltYear(menuVo.getHidYear());
			vo.setPltMonth(menuVo.getHidMonth());
			/*						
			 *  	2009/01/20	m.yoshida	締め日対応
			 */
			vo.setHidDay(menuVo.getHidDay());
			// 検索
			searchAttendanceList(vo);
		} else if (cmd.equals(CMD_ATTENDANCE_CARD_SELECT)) {
			// 対象年月日設定
			vo.setParams(request);
			// 検索
			searchAttendance();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}

	/**
	 * 勤怠一覧検索
	 * @param vo AttendanceListVoインスタンス
	 * @throws Exception
	 */
	private void searchAttendanceList(
			AttendanceListVo vo
	) throws Exception {
		// DBコネクション取得
		if (conn == null) {
			getConnection();
		}
		/*						
		 *  	2009/01/20	m.yoshida	締め日対応
		 */
		// 対象期間設定
		Date targetMonth;
		if (vo.getHidDay().length() != 0) {
			targetMonth = util.getDate(vo.getPltYear(), vo.getPltMonth(), vo.getHidDay());
		} else {
			targetMonth = util.getYearMonth(vo.getPltYear(), vo.getPltMonth());
		}
		// 勤怠データ取得
		/*						
		 *  	2009/01/20	m.yoshida	締め日対応
		 */
		getAttendanceListForView(vo.getSelectKCode(), targetMonth);
		// 日集計データ時間設定
		setHours();
		// 日集計データ配列作成
		makeAttendanceArray();
		// 月集計
		totalAttendanceDaily();
		// 人事基本情報の取得
		MKihonDto mKihonDto = getBasisInfo(vo.getSelectKCode());
		// 選択情報の設定
		vo.setSelectKName(mKihonDto.getSimei());
		vo.setSelectSectionName(util.getCodeName(mKihonDto.getSCode(), getSectionArrayAll()));
		// 皆勤手当設定
		// 役職情報取得
		MYakusyokuDto positionDto = getHoldingPositionInfo(vo.getSelectKCode());
		if (chkExistDto(positionDto)) {
			// 皆勤手当が無効の場合、空欄にする
			if (positionDto.getKaikin().equals(CommonConst.NAM_COMMON_FLAG_OFF)) {
				aryAttendanceCompile[17][1] = "";
			}
		}
		// 月集計データとラベルを分離
		String[] aryLabel 	= new String[aryAttendanceCompile.length];
		String[] aryCompile = new String[aryAttendanceCompile.length];
		for (int i = 0; i < aryAttendanceCompile.length; i++) {
			aryLabel[i] = aryAttendanceCompile[i][0];
			aryCompile[i] = aryAttendanceCompile[i][1];
		}
		// VO設定
		vo.setFields(aryAttendance, aryCompile, aryLabel);
	}

}
