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

import java.sql.SQLException;
import java.util.List;

import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.payroll.base.AdjustmentAction;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.bonus.dto.PhBounsDto;
import jp.mosp.payroll.common.vo.AdjustmentReferVo;
import jp.mosp.payroll.dto.PhPaymentDto;

public class AdjustmentReferAction extends AdjustmentAction {
	
	// コマンド
	private static final String	CMD_ADJUSTMENT_REFER_SHOW	= "P3060";
	

	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// PAYROLL権限確認
		confirmPayrollAuth();
		// VOの準備
		AdjustmentReferVo vo = new AdjustmentReferVo();
		// 名称取得用配列取得
		setNameArray(vo);
		vo.initVo(cfg, msg, cmd, aspUser, user);
		// VO及びフォワード先URLの設定
		setVo(vo);
		setUrl(vo.getRetUrl());
		storeVo(PayrollConst.VO_ADJUSTMENT_LIST, vo);
		
		// コマンド毎の処理
		if (cmd.equals(CMD_ADJUSTMENT_REFER_SHOW)) {
			vo.setParams(request);
			kCode = "";
			if (vo.getLblChangeKCode() != null && !vo.getLblChangeKCode().equals("")) {
				// 入力画面から遷移
				kCode = vo.getLblChangeKCode();
				adjustYear = getAdjustYear();
			} else if (key1 != null && !key1.equals("")) {
				// 一覧から遷移
				kCode = key1;
				vo.setHidAdjustYear(Integer.parseInt(key2));
				adjustYear = Integer.parseInt(key2);
			} else {
				kCode = vo.getHidKCode();
				adjustYear = getAdjustYear();
			}
			startAdjDate = getStartDateOfYear(adjustYear);
			lastDate = getLastDateOfYear(adjustYear);
			
			// 調整年確認
			//confirmAdjustYear(vo.getHidAdjustYear());
			show(vo);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	private void show(AdjustmentReferVo vo) throws Exception {
		// DAO準備
		prepareDao();
		// 対象年月、対象データ設定
		setTargetData();
		// 年末調整データ取得
		setAdjustmentInfo(vo);
		// 人事情報設定
		setHumanInfo(vo);
		// 給与、賞与情報取得
		setPaymentBonusInfo(vo);
		
	}
	
	/**
	 * 名称取得用配列取得
	 * @param vo 設定対象VO
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	private void setNameArray(AdjustmentReferVo vo) throws SQLException, NoSuchFieldException, IllegalAccessException,
			ClassNotFoundException {
		// DBコネクション取得
		getConnection();
		// 取得済確認
		if (vo.aryStation != null) {
			return;
		}
		// 配列取得
		vo.aryStation = getStationArrayAll();
		vo.aryPosition = getPositionArrayAll();
		vo.aryEra = getCodeArray(PayrollConst.TID_JAPANESE);
		vo.aryRelation = getCodeArray(PayrollConst.TID_RELATION);
		// 和暦設定取得
		vo.listConf = getConfList(PayrollConst.CONF_ID_JP_IMP_CAL);
		// 寡婦(夫) 振分用
		vo.aryGender = getCodeArray(PayrollConst.TID_GENDER);
	}
	
	/**
	 * 人事情報設定
	 * @param vo 設定対象VO
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws SQLException 
	 * @throws MospException 
	 */
	private void setHumanInfo(AdjustmentReferVo vo) throws SQLException, IllegalAccessException, NoSuchFieldException,
			MospException {
		
		// 人事基本情報取得
		mKihonDto = mKihonPayrollDao.findForKey(kCode);
		
		if (mKihonDto == null) {
			// 人事基本情報が無い場合
			vo.setLblChangeKCode(kCode);
			String errMessage = MospUtility.getMessage(msg, PayrollConst.MSG_NO_HUMAN_DATA, "");
			addErrMessage(errMessage);
		}
		// 人事管理情報取得
		mKojinDto = mKojinDao.findForKey(kCode);
		// 人事給与情報取得
		cmIncomeTaxDto = cmIncomeTaxDao.findForEmployeeDate(kCode, lastDate);
		cmMidwayEnterDto = cmMidwayEnterDao.findForKey(kCode);
		// 家族情報取得
		// listFamily = cmFamilyDao.findForAdjustment(kCode);
		// VO設定
		vo.setFields(mKihonDto, cmIncomeTaxDto, cmMidwayEnterDto, mKojinDto);
	}
	
	/**
	 * 給与、賞与情報取得
	 * @param vo
	 * @throws Exception 
	 */
	private void setPaymentBonusInfo(AdjustmentReferVo vo) throws Exception {
		// 計算対象給与データ取得
		List<PhPaymentDto> listPaymentData = phPaymentDao.findForConditionDate(kCode, targetBefDate, targetAffDate);
		// 計算対象賞与データ取得
		List<PhBounsDto> listBonusData = phBonusDao.findForConditionDate(kCode, startAdjDate, lastDate);
		// 給与データ確認
		setTargetPaymentData();
		// 対象データ所得税計算(登録なし)
		calcTargetIncomeTax(false);
		// VO設定
		vo.setFields(listPaymentData, listBonusData, pdPayDeductionDao, phPaymentDto, phBonusDto, cdAdjustmentDto);
	}
	
	/**
	 * 年末調整データ取得
	 * @param vo
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	private void setAdjustmentInfo(AdjustmentReferVo vo) throws SQLException, IllegalAccessException,
			NoSuchFieldException {
		cdAdjustmentDto = cdAdjustmentDao.findForKey(kCode, adjustYear);
		// フィールドの設定(年末調整データ)
		vo.setFields(cdAdjustmentDto);
	}
}
