/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 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.util.Date;

import jp.mosp.payroll.common.vo.FirmBankingOutputVo;
import jp.mosp.payroll.dto.CmCompanyDto;
import jp.mosp.payroll.dao.CmCompanyDao;
import jp.mosp.payroll.base.PayrollAction;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.dao.PtCurrentDao;
import jp.mosp.payroll.dto.PtCurrentDto;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;

public class FirmBankingOutputAction extends PayrollAction{
	// コマンド
	
	private static final String CMD_FIRM_BANKING_SHOW         = "P9001";
	private static final String CMD_FIRM_BANKING_SELECT      = "U3181";
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// PAYROLL権限確認
		confirmPayrollAuth();
		// VOの取得及び設定
		FirmBankingOutputVo vo = new FirmBankingOutputVo();
		vo = (FirmBankingOutputVo)getStoredVo(vo.getClassName());
		if (vo == null || cmd.equals(CMD_FIRM_BANKING_SHOW)) {
			vo = new FirmBankingOutputVo();
			storeVo(vo.getClassName(), vo);
		}
		// VO及びフォワード先URLの設定
		vo.initVo(cfg, msg, cmd, aspUser, user);
		vo.clearMessage();
		vo.clearErrField();

		
		setVo(vo);
		setUrl(vo.getRetUrl());
		// 計算年月取得
		java.util.Date calcCurrent = getCalcCurrentYearMonth(); 
		// プルダウン及び設定取得
		vo.aryPltCoBankCode   = getCoBankArray                ();
		vo.aryPltOutputDetail = PayrollConst.getOutputDetailArray();
		vo.aryPltCodeType     = PayrollConst.getCodeTypeArray    ();
		vo.aryPltRecordType   = PayrollConst.getRecordTypeArray  ();
		vo.aryPltEndRecord    = PayrollConst.getEndRecordArray   ();
		// コマンド毎の処理
		if (cmd.equals(CMD_FIRM_BANKING_SHOW)) {
			// 年月設定(計算年月設定)
			setYearMonth(vo, calcCurrent);
			vo.setMode(FirmBankingOutputVo.MODE_SHOW);
			show(vo);
		} else if (cmd.equals(CMD_FIRM_BANKING_SELECT)) {
			vo.setMode(FirmBankingOutputVo.MODE_SELECT);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	/**
	 * 表示
	 * @param vo
	 * @throws Exception
	 */
	private void show (FirmBankingOutputVo vo) throws Exception {
		// 会社名取得
		CmCompanyDao cmCompanyDao = new CmCompanyDao();
		cmCompanyDao.initDao(cfg, cmd, aspUser, user, conn);
		CmCompanyDto cmCompanyDto = cmCompanyDao.findAll();
		// 会社情報設定
		if (cmCompanyDto != null) {
			vo.setTxtCoAddress  (cmCompanyDto.getCompanyFbAddress());
			vo.setTxtCompanyName(cmCompanyDto.getCompanyFbKana   ());
		}
		// 遷移前UIにより出力内容を設定(0:給与、1:賞与、2:住民税)
		vo.setFirmBankOutputType(key1);
		vo.setPltOutputDetail(key1);
		// 初期設定
		vo.setPltRecordType(PayrollConst.FB_RECORD_TYPE_NONE);
		vo.setPltEndRecord (PayrollConst.FB_END_RECORD_NONE );
	}
	
	/**
	 * 年月設定
	 * @param vo          設定対象VO
	 * @param calcCurrent 計算月
	 */
	private void setYearMonth(FirmBankingOutputVo vo, Date calcCurrent) {
		// プルダウン取得
		vo.aryPltSpecifyYear  = util.getYearArray(MospUtility.getYear(calcCurrent));
		vo.aryPltSpecifyMonth = getMonthArray();
		vo.aryPltSpecifyDay   = getDayArray();
		vo.aryPltDeadlineYear = util.getYearArray(MospUtility.getYear(calcCurrent));
		vo.aryPltDeadlineMonth= getMonthArray();
		vo.aryPltDeadlineDay  = getDayArray();
		// 計算年月設定
		vo.setPltSpecifyYear (String.valueOf(MospUtility.getYear(calcCurrent )));
		vo.setPltSpecifyMonth(String.valueOf(MospUtility.getMonth(calcCurrent)));
		vo.setPltSpecifyDay  (String.valueOf(MospUtility.getDay(calcCurrent  )));
		// 計算年月設定
		vo.setPltDeadlineYear (String.valueOf(MospUtility.getYear(calcCurrent )));
		vo.setPltDeadlineMonth(String.valueOf(MospUtility.getMonth(calcCurrent)));
		vo.setPltDeadlineDay  (String.valueOf(MospUtility.getDay(calcCurrent  )));
	}
	
	/**
	 * 計算月取得
	 * @return 計算月
	 * @throws Exception
	 */
	private Date getCalcCurrentYearMonth() throws Exception {
		FirmBankingOutputVo vo = (FirmBankingOutputVo)getVo();
		// 対象年月取得
		Date targetMonth = vo.getCalcDate();
		// DAO準備
		PtCurrentDao dao = new PtCurrentDao(cfg, cmd, aspUser, user, conn);
		//dao.initDao(cfg, cmd, aspUser, user, conn);
		PtCurrentDto dto = dao.findForKey(PayrollConst.OFFICE_ALL, PayrollConst.CALC_PAYMENT, targetMonth);
		if (dto != null) {
			return dto.getCalcDate();
		}
		return new Date();
	}

}
