/*
 * 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.Calendar;
import java.util.Date;
import java.util.Iterator;

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.kintai.dto.MScheduleDto;
import jp.mosp.kintai.vo.ScheduleConfirmListVo;

public class ScheduleConfirmListAction extends AttendanceTotalAction {

	// 僐儅儞僪
	private static final String CMD_CONFIRM_LIST_SHOW			= "K1230";
	public  static final String CMD_CONFIRM_LIST_SEARCH		= "K1232";

	/**
	 * 僐儞僗僩儔僋僞
	 */
	public ScheduleConfirmListAction() {
		super();
	}

	/**
	 * 傾僋僔儑儞
	 */
	public void action() throws Exception {
		// VO偺庢摼
		ScheduleConfirmListVo vo;
		vo = (ScheduleConfirmListVo)getStoredVo(CommonConst.VO_SCHEDULE_CONFIRM_LIST);
		if (vo == null) {
			vo = new ScheduleConfirmListVo();
		}
		vo.initVo(cfg, msg, cmd, aspUser, user);
		vo.clearMessage();
		vo.clearErrField();
		storeVo(CommonConst.VO_SCHEDULE_CONFIRM_LIST, vo);
		// VO媦傃僼僅儚乕僪愭URL偺愝掕
		setVo(vo);
		setUrl(vo.getRetUrl());
		// DB僐僱僋僔儑儞庢摼
		getConnection();
		// 儐乕僓乕忣曬庢摼
		getInfo(user.getUserId());
		// 昞帵寧
		Date current = util.getSystemDate();
		// 僾儖僟僂儞庢摼
		vo.aryPltYear  = util.getYearArray(MospUtility.getYear (current), 2, 1);
		vo.aryPltMonth = util.getMonthArray();
		// 懳徾幮堳愝掕
		vo.setSelectKCode(mKihon.getKCode());
		// 僐儅儞僪枅偺張棟
		if (cmd.equals(CMD_CONFIRM_LIST_SHOW)) {
			// 昞帵
			// 懳徾擭寧擔愝掕
			vo.setPltYear(String.valueOf(MospUtility.getYear(current)));
			vo.setPltMonth(String.valueOf(MospUtility.getMonth(current)));
			// 専嶕
			searchScheduleList(vo);	
		} else if (cmd.equals(CMD_CONFIRM_LIST_SEARCH)) {
			// 懳徾擭寧擔愝掕
			vo.setParams(request);
			// 専嶕
			searchScheduleList(vo);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}

	/**
	 * 梊掕堦棗専嶕
	 * @param vo Vo僀儞僗僞儞僗
	 * @throws Exception
	 */
	private void searchScheduleList(
			ScheduleConfirmListVo vo
	) throws Exception {
		// DB僐僱僋僔儑儞庢摼
		if (conn == null) {
			getConnection();
		}
		// 嬑懹僨乕僞庢摼
		//getScheduleList(vo.getSelectKCode(), vo.getPltYear(), vo.getPltMonth());
		/*						
		 *  	2009/01/20	m.yoshida	掲傔擔懳墳
		 */
		//getScheduleList(vo.getSelectKCode(), utility.getYearMonth(vo.getPltYear(), vo.getPltMonth()));
		getScheduleListForView(vo.getSelectKCode(), util.getYearMonth(vo.getPltYear(), vo.getPltMonth()));
		// 擔廤寁僨乕僞帪娫愝掕
		setScheduleHours();
		// 擔廤寁僨乕僞攝楍嶌惉
		makeScheduleArray();
		// 寧廤寁
		totalScheduleDaily();
		// 恖帠婎杮忣曬偺庢摼
		setSelectBasicInfo(vo);
		// 寧廤寁僨乕僞偲儔儀儖傪暘棧
		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);
	}

	/**
	 * 擔廤寁僨乕僞攝楍嶌惉
	 * @throws Exception
	 */
	private void makeScheduleArray() throws Exception {
		// DB僐僱僋僔儑儞庢摼
		if (conn == null) {
			getConnection();
		}
		// 僨乕僞悢庢摼
		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++;
		}
		// 攝楍弶婜壔
		aryAttendance = new String[count][40];
		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] = String.valueOf(MospUtility.getMonth(cal.getTime()))
			+ CommonConst.SEPARATOR_DATE
			+ String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
			// 梛擔
			aryAttendance[i][1] = getDayOfWeek(cal.get(Calendar.DAY_OF_WEEK));
			// 旛峫乮廽嵳擔丄敿媥乯
			StringBuffer remark = new StringBuffer();
			String holidayName = getPublicHolidayName(cal.getTime());
			if (holidayName.length() != 0) {
				remark.append(holidayName);
				remark.append(CommonConst.NAM_COMMON_HALF_KANA_SPACE);
			}
			aryAttendance[i][7] = remark.toString();
			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 kinmu = "";
					if (isAvailableWorkType(dto)) {
						kinmu = workTypeMap.get(dto.getKinmu()).getShiftName();
						// 弌嬑帪崗
						aryAttendance[i][3] = dto.getSyJikoku();
						// 戅嬑帪崗
						aryAttendance[i][4] = dto.getTaJikoku();
						// 嬑柋帪娫
						aryAttendance[i][5] = getHourString(util.convIntegerTimeToDoubleTime(dto.getKinmuJi()));
						// 媥宔帪娫
						aryAttendance[i][6] = getHourString(util.convIntegerTimeToDoubleTime(dto.getKyukeiJi()));
					} else if (isWeekHoliday(dto)) {
						// 廡媥偺応崌
						kinmu = CommonConst.ATTENDANCE_TYPE_WEEK_HOLIDAY;
					} else {
						for (int k = 0; k < aryHolidayReason.length; k++) {
							if (aryHolidayReason[k][0].equals(dto.getKKRiyuu())) {
								kinmu = aryHolidayReason[k][1];
							}
						}
					}
					aryAttendance[i][2] = kinmu;
					// 敿媥
					if (isStartHalfHoliday(dto)) {
						aryAttendance[i][7] += CommonConst.NAM_SHORT_START_ATTENDANCE + CommonConst.NAM_VACATION_LEAVE_HALF;
						aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;
					}
					if (isQuitHalfHoliday(dto)) {
						aryAttendance[i][7] += CommonConst.NAM_SHORT_QUIT_ATTENDANCE + CommonConst.NAM_VACATION_LEAVE_HALF;
						aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;						
					}
					if (isWorkOnHoliday(dto)) {
						aryAttendance[i][7] += CommonConst.NAM_WORK_ON_HOLIDAY;
						aryAttendance[i][7] += CommonConst.NAM_COMMON_HALF_KANA_SPACE;						
					}
					break;
				}
				cal.add(Calendar.DAY_OF_MONTH, 1);
			}
		}
	}

	/**
	 * 寧廤寁
	 * @throws Exception 
	 */
	private void totalScheduleDaily() throws Exception {
		// DB僐僱僋僔儑儞庢摼
		if (conn == null) {
			getConnection();
		}
		// 僨乕僞悢庢摼
		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++;
		}
		// 攝楍偺弶婜壔
		aryAttendanceCompile = new String[18][2];
		// 曄悢愰尵丄曄悢偺弶婜壔
		// 嬑柋帪娫寁
		int totalWork = 0;
		// 媥宔帪娫寁
		int totalInterbal = 0;
		// 弌嬑擔悢寁
		int totalWorkDays = 0;
		// 桳媥擔悢寁
		double totalPaidHoliday = 0;
		// 摿暿媥壣寁
		int totalParticularHoliday = 0;
		// 寚嬑擔悢寁
		int totalAbsence = 0;
		// 彎昦媥壣寁
		int totalMedicalLeave = 0;
		// 戙媥擔悢寁
		int totalCompensationDay =0;
		// 怳懼媥壣寁
		int totalTransfer = 0;
		// 懘懠媥壣寁
		int totalOther = 0;
		// 擔廤寁僨乕僞庢摼
		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 < count; i++) {
				if (scheduleDate.compareTo(cal.getTime()) == 0) {
					// 嬑懹崁栚
					if (isAvailableWorkType(dto)) {
						// 嬑柋帪娫
						totalWork += dto.getKinmuJi();
						// 媥宔帪娫
						totalInterbal += dto.getKyukeiJi();
						// 弌嬑擔悢
						if (dto.getKinmuJi() > 0) {
							totalWorkDays++;
						}
						// 敿媥
						if (isStartHalfHoliday(dto) || isQuitHalfHoliday(dto)) {
							totalPaidHoliday += 0.5;
						}
					} else {
						// 媥壣崁栚
						if (!dto.getKKRiyuu().equals("")) {
							if (dto.getKKRiyuu().equals("0")) {
								// 桳媼媥壣乮m_code, 媥壣棟桼, tcode="0"乯
								totalPaidHoliday++;
							} else if (dto.getKKRiyuu().equals("1")) {
								// 摿暿媥壣乮m_code, 媥壣棟桼, tcode="1"乯
								totalParticularHoliday++;
							} else if (dto.getKKRiyuu().equals("2")) {
								// 寚嬑擔悢乮m_code, 媥壣棟桼, tcode="2"乯
								totalAbsence++;
							} else if (dto.getKKRiyuu().equals("3")) {
								// 彎昦媥壣乮m_code, 媥壣棟桼, tcode="3"乯
								totalMedicalLeave++;
							} else if (dto.getKKRiyuu().equals("4")) {
								// 戙媥擔悢乮m_code, 媥壣棟桼, tcode="4"乯
								totalCompensationDay++;
							} else if (dto.getKKRiyuu().equals("5")) {
								// 怳懼媥壣乮m_code, 媥壣棟桼, tcode="5"乯
								totalTransfer++;
							} else {
								// 懘懠媥壣乮m_code, 媥壣棟桼, tcode="6"傛傝忋乯
								totalOther++;
							}
						}
					}
				}
				cal.add(Calendar.DAY_OF_MONTH, 1);
			}
		}
		// 攝楍
		// 嬑柋帪娫崌寁
		aryAttendanceCompile[0][0] = CommonConst.NAM_WORKING;
		aryAttendanceCompile[0][1] = getHourString(util.convIntegerTimeToDoubleTime(totalWork));
		// 媥宔帪娫崌寁
		aryAttendanceCompile[1][0] = CommonConst.NAM_INTERVAL_TIME;
		aryAttendanceCompile[1][1] = getHourString(util.convIntegerTimeToDoubleTime(totalInterbal));
		// 弌嬑擔悢
		aryAttendanceCompile[2][0] = CommonConst.NAM_PRESENCE;
		aryAttendanceCompile[2][1] = String.valueOf(totalWorkDays);
		// 桳媼媥壣擔悢
		aryAttendanceCompile[3][0] = aryHolidayReason[0][1];
		aryAttendanceCompile[3][1] = String.valueOf(totalPaidHoliday);
		// 摿暿媥壣擔悢
		aryAttendanceCompile[4][0] = aryHolidayReason[1][1];
		aryAttendanceCompile[4][1] = String.valueOf(totalParticularHoliday);
		// 寚嬑媥壣擔悢
		aryAttendanceCompile[5][0] = aryHolidayReason[2][1];
		aryAttendanceCompile[5][1] = String.valueOf(totalAbsence);
		// 彎昦媥壣擔悢
		aryAttendanceCompile[6][0] = aryHolidayReason[3][1];
		aryAttendanceCompile[6][1] = String.valueOf(totalMedicalLeave);
		// 戙媥擔悢
		aryAttendanceCompile[7][0] = aryHolidayReason[4][1];
		aryAttendanceCompile[7][1] = String.valueOf(totalCompensationDay);
		// 怳懼媥壣擔悢
		aryAttendanceCompile[8][0] = aryHolidayReason[5][1];
		aryAttendanceCompile[8][1] = String.valueOf(totalTransfer);
		// 懘懠媥壣擔悢
		aryAttendanceCompile[9][0] = CommonConst.NAM_OTHER_SHORT;
		aryAttendanceCompile[9][1] = String.valueOf(totalOther);
	}

}
