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

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

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.common.MospUtility;
import jp.mosp.common.utils.DateUtil;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.common.utils.TimeUtil;
import jp.mosp.kintai.common.action.AttendanceTotalAction;
import jp.mosp.kintai.common.part.KintaiCommonPart;
import jp.mosp.kintai.payroll.dto.KintaiCompileDto;
import jp.mosp.kintai.schedule.dto.MScheduleDto;
import jp.mosp.kintai.schedule.vo.ScheduleConfirmListVo;

/**
 * @author yoshida
 *
 */
public class ScheduleConfirmListAction extends AttendanceTotalAction {
	
	// コマンド
	private static final String	CMD_CONFIRM_LIST_SHOW	= "K1230";
	private static final String	CMD_CONFIRM_LIST_SELECT	= "K1231";
	/**
	 * 
	 */
	public static final String	CMD_CONFIRM_LIST_SEARCH	= "K1232";
	

	/**
	 * コンストラクタ
	 */
	public ScheduleConfirmListAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		prepareVo(false);
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_CONFIRM_LIST_SHOW)) {
			// 表示
			show();
		} else if (cmd.equals(CMD_CONFIRM_LIST_SELECT)) {
			// 表示
			select();
		} else if (cmd.equals(CMD_CONFIRM_LIST_SEARCH)) {
			// 検索
			search();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new ScheduleConfirmListVo();
	}
	
	@Override
	protected void init() throws Exception {
		ScheduleConfirmListVo vo = (ScheduleConfirmListVo)getVo();
		// プルダウン取得
		vo.aryPltYear = StringUtil.getYearArray(MospUtility.getYear(DateUtil.getSystemDate()), 3, 1);
		vo.aryPltMonth = StringUtil.getMonthArray();
		// ユーザー情報取得
		getInfo(user.getUserId());
		// メニューボタン
		setScheduleMenuButton();
	}
	
	@Override
	protected void show() throws Exception {
		ScheduleConfirmListVo vo = (ScheduleConfirmListVo)getVo();
		// 対象社員設定
		vo.setSelectKCode(userBasis.getKCode());
		// 表示月
		Date current = DateUtil.getSystemDate();
		// 対象年月日設定
		vo.setPltYear(String.valueOf(MospUtility.getYear(current)));
		vo.setPltMonth(String.valueOf(MospUtility.getMonth(current)));
		// 検索
		searchScheduleList();
	}
	
	@Override
	protected void select() throws Exception {
		ScheduleConfirmListVo vo = (ScheduleConfirmListVo)getVo();
		// 対象社員設定
		vo.setSelectKCode(key1);
		// 表示月
		Date current = DateUtil.getSystemDate();
		// 対象年月日設定
		vo.setPltYear(String.valueOf(MospUtility.getYear(current)));
		vo.setPltMonth(String.valueOf(MospUtility.getMonth(current)));
		// 検索
		searchScheduleList();
	}
	
	@Override
	protected void search() throws Exception {
		ScheduleConfirmListVo vo = (ScheduleConfirmListVo)getVo();
		// 対象年月日設定
		vo.setParams(request);
		// 検索
		searchScheduleList();
	}
	
	/**
	 * 予定一覧検索
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void searchScheduleList() throws Exception {
		ScheduleConfirmListVo vo = (ScheduleConfirmListVo)getVo();
		// 勤怠データ取得
		getScheduleListForView(vo.getSelectKCode(), DateUtil.getYearMonth(vo.getPltYear(), vo.getPltMonth()));
		// 日集計データ時間設定
		setScheduleHours(scheduleList);
		// 日集計データ配列作成
		makeScheduleArray(vo.getSelectKCode(), scheduleList);
		// 月集計
		totalScheduleDaily(vo.getSelectKCode(), scheduleList);
		// 選択情報の設定
		setLabelBasisInfo(vo.getSelectKCode());
		// 月集計データとラベルを分離
		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);
	}
	
	/**
	 * 日集計データ配列作成
	 * @param kCode  対象社員コード
	 * @param scheduleList  予定簿情報リスト
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void makeScheduleArray(String kCode, List<MScheduleDto> scheduleList) throws Exception {
		// データ数取得
		Calendar cal = Calendar.getInstance();
		cal.setTime(MospUtility.getDate(startDate));
		int count = 0;
		while (MospUtility.getDate(endDate).compareTo(cal.getTime()) >= 0) {
			cal.add(Calendar.DAY_OF_MONTH, 1);
			count++;
		}
		// 配列初期化
		final int size = 40;
		aryAttendance = new String[count][size];
		for (int i = 0; i < aryAttendance.length; i++) {
			for (int j = 0; j < aryAttendance[i].length; j++) {
				aryAttendance[i][j] = "";
			}
		}
		// 日付設定
		cal.setTime(MospUtility.getDate(startDate));
		for (int i = 0; i < aryAttendance.length; i++) {
			// 日付
			aryAttendance[i][0] = DateUtil.convDateToMonthDayShort(cal.getTime());
			// 曜日
			aryAttendance[i][1] = getDayOfWeek(cal.get(Calendar.DAY_OF_WEEK));
			aryAttendance[i][8] = getDayOfWeekColorClass(cal.getTime());
			// 備考（祝祭日、半休）
			StringBuffer remark = new StringBuffer();
			String holidayName = getPublicHolidayName(cal.getTime());
			if (StringUtil.isNotNull(holidayName)) {
				remark.append(holidayName);
				remark.append(CommonConst.NAM_COMMON_HALF_KANA_SPACE);
			}
			aryAttendance[i][7] = remark.toString();
			// 残業申請
			if (part().overtime().isOverTimeRequest(kCode, cal.getTime())) {
				aryAttendance[i][7] += CommonConst.NAM_OVERTIME;
				aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;
			}
			// 休暇申請
			if (part().holidayRequest().existHolidayRequest(kCode, cal.getTime())) {
				aryAttendance[i][7] += CommonConst.NAM_VACATION_LEAVE;
				aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;
			}
			cal.add(Calendar.DAY_OF_MONTH, 1);
		}
		// データの設定
		Iterator<MScheduleDto> it = scheduleList.iterator();
		while (it.hasNext()) {
			cal.setTime(MospUtility.getDate(startDate));
			MScheduleDto dto = it.next();
			Date scheduleDate = MospUtility.getDate(dto.getHizuke());
			for (int i = 0; i < aryAttendance.length; i++) {
				if (scheduleDate.compareTo(cal.getTime()) == 0) {
					// 勤務形態
					String kinmuName = "";
					if (KintaiCommonPart.isAvailableWorkType(dto)) {
						kinmuName = part().worktype().getWorkTypeName(dto.getKinmu());
						// 出勤時刻
						aryAttendance[i][3] = dto.getSyJikoku();
						// 退勤時刻
						aryAttendance[i][4] = dto.getTaJikoku();
						// 勤務時間
						aryAttendance[i][5] = getHourString(TimeUtil.convIntegerTimeToDoubleTime(dto.getKinmuJi()));
						// 休憩時間
						aryAttendance[i][6] = getHourString(TimeUtil.convIntegerTimeToDoubleTime(dto.getKyukeiJi()));
					} else if (KintaiCommonPart.isWeekHoliday(dto) || KintaiCommonPart.isOfficialHoliday(dto)) {
						// 週休の場合
						//kinmu = CommonConst.ATTENDANCE_TYPE_WEEK_HOLIDAY;
						// 週休→所定休日表示
						kinmuName = part().worktype().getWorkTypeName(dto.getKinmu());
					} else {
						// 休暇理由
						kinmuName = part().mospcode().getHolidayName(dto.getKKRiyuu());
					}
					aryAttendance[i][2] = kinmuName;
					// 半休
					if (KintaiCommonPart.isStartHalfHoliday(dto)) {
						aryAttendance[i][7] += CommonConst.NAM_SHORT_START_ATTENDANCE;
						aryAttendance[i][7] += CommonConst.NAM_VACATION_LEAVE_HALF;
						aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;
					}
					if (KintaiCommonPart.isQuitHalfHoliday(dto)) {
						aryAttendance[i][7] += CommonConst.NAM_SHORT_QUIT_ATTENDANCE;
						aryAttendance[i][7] += CommonConst.NAM_VACATION_LEAVE_HALF;
						aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;
					}
					if (KintaiCommonPart.isWorkOnHoliday(dto)) {
						aryAttendance[i][7] += CommonConst.NAM_WORK_ON_HOLIDAY;
						aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;
					}
					if (KintaiCommonPart.isOverTimeSubHolidayHalf(dto)) {
						aryAttendance[i][7] += CommonConst.NAM_SUB_HOLIDAY_HALF;
						aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;
					}
					break;
				}
				cal.add(Calendar.DAY_OF_MONTH, 1);
			}
		}
	}
	
	/**
	 * 月集計
	 * @param kCode	対象社員コード
	 * @param scheduleList  予定簿情報リスト
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void totalScheduleDaily(String kCode, List<MScheduleDto> scheduleList) throws Exception {
		KintaiCompileDto dto = compile().getCompiledInfo(kCode, scheduleList, false);
		// 配列の初期化
		aryAttendanceCompile = new String[10][2];
		// 配列
		// 勤務時間合計
		aryAttendanceCompile[0][0] = CommonConst.NAM_WORKING;
		aryAttendanceCompile[0][1] = getHourString(TimeUtil.convIntegerTimeToDoubleTime(dto.getTotalWork()));
		// 休憩時間合計
		aryAttendanceCompile[1][0] = CommonConst.NAM_INTERVAL_TIME;
		aryAttendanceCompile[1][1] = getHourString(TimeUtil.convIntegerTimeToDoubleTime(dto.getTotalInterbal()));
		// 出勤日数
		aryAttendanceCompile[2][0] = CommonConst.NAM_PRESENCE;
		aryAttendanceCompile[2][1] = String.valueOf(dto.getTotalWorkDays());
		// 有給休暇日数
		aryAttendanceCompile[3][0] = aryHolidayReason[0][1];
		aryAttendanceCompile[3][1] = String.valueOf(dto.getTotalPaidHoliday());
		// 特別休暇日数
		aryAttendanceCompile[4][0] = aryHolidayReason[1][1];
		aryAttendanceCompile[4][1] = String.valueOf(dto.getTotalParticularHoliday());
		// 欠勤休暇日数
		aryAttendanceCompile[5][0] = aryHolidayReason[2][1];
		aryAttendanceCompile[5][1] = String.valueOf(dto.getTotalAbsence());
		// 傷病休暇日数
		aryAttendanceCompile[6][0] = aryHolidayReason[3][1];
		aryAttendanceCompile[6][1] = String.valueOf(dto.getTotalMedicalLeave());
		// 代休日数
		aryAttendanceCompile[7][0] = aryHolidayReason[4][1];
		aryAttendanceCompile[7][1] = String.valueOf(dto.getTotalCompensationDay());
		// 振替休暇日数
		aryAttendanceCompile[8][0] = aryHolidayReason[5][1];
		aryAttendanceCompile[8][1] = String.valueOf(dto.getTotalTransfer());
		// 其他休暇日数
		aryAttendanceCompile[9][0] = CommonConst.NAM_OTHER_SHORT;
		aryAttendanceCompile[9][1] = String.valueOf(dto.getTotalOther());
	}
	
}
