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

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import jp.mosp.common.common.MospException;
import jp.mosp.express.ExpressUtility;
import jp.mosp.payroll.bonus.dto.PmDetailBsDto;
import jp.mosp.payroll.dto.DetailDto;

/**
 * 給与計算関連伝票の元となるクラス。<br><br>
 */
public abstract class PayrollSlipBonus {
	/**
	 * 明細情報を取得する。
	 * @param detailCode 明細コード
	 */
	protected DetailDto getDetailDto(List<?> list, String detailCode) {
		for (Iterator<?> it = list.iterator(); it.hasNext();) {
			DetailDto dto = (DetailDto)it.next();
			if (dto.getDetailCode().equals(detailCode)) {
				return dto;
			}
		}
		return null;
	}
	
	/**
	 * 明細リストを設定する。
	 * @param aryDetail 設定対象明細配列
	 * @return 明細リスト
	 */
	protected void setDetails(List<?> listDetail, String[][] aryDetail) {
		for (Iterator<?> it = listDetail.iterator(); it.hasNext();) {
			DetailDto dto = (DetailDto)it.next();
			for (int i = 0; i < aryDetail.length; i++) {
				if (dto.getDetailCode().equals(aryDetail[i][0])) {
					setDetailAmount(dto, Float.parseFloat(aryDetail[i][2]));
					break;
				}
			}
		}
	}
	
	/**
	 * 明細値を取得する。
	 * @param list 取得対象リスト
	 * @param detailCode 取得対象明細コード
	 * @return 明細値
	 */
	protected float getDetailAmount(List<?> list, String detailCode) {
		for (Iterator<?> it = list.iterator(); it.hasNext();) {
			DetailDto dto = (DetailDto)it.next();
			if (dto.getDetailCode().equals(detailCode)) {
				if (dto instanceof PmDetailBsDto) {
					PmDetailBsDto detailBsDto = (PmDetailBsDto)dto;
					return detailBsDto.getAmount();
				}
			}
		}
		return 0;
	}
	
	/**
	 * 明細配列を取得する。
	 * @param list             取得対象リスト
	 * @param checkDisplayType 0印字区分確認要否
	 * @return 明細配列
	 */
	protected String[][] getAryDetail(List<?> list, boolean checkDisplayType) {
		String[][] aryDetail = new String[list.size()][3];
		for (int i = 0; i < list.size(); i++) {
			DetailDto dto = (DetailDto)list.get(i);
			aryDetail[i][0] = dto.getDetailCode();
			aryDetail[i][1] = dto.getDetailAbbr();
			aryDetail[i][2] = "0";
			if (dto instanceof PmDetailBsDto) {
				aryDetail[i][2] = String.valueOf(((PmDetailBsDto)dto).getAmount());
			}
			if (checkDisplayType && dto.getDisplayType() == PayrollConst.DETAIL_DISP_NONE) {
				if (String.valueOf(aryDetail[i][2]).equals("0")) {
					aryDetail[i][1] = "";
					aryDetail[i][2] = "";
				}
			}
		}
		return aryDetail;
	}
	
	/**
	 * 明細値を設定する。
	 * @param list       明細情報
	 * @param listAmount 明細値リスト
	 */
	protected void setDetailsAmount(List<?> list, List<?> listAmount) {
		for (Iterator<?> it = list.iterator(); it.hasNext();){
			DetailDto dto = (DetailDto)it.next();
			for (Iterator<?> itAmount = listAmount.iterator(); itAmount.hasNext();) {
				DetailDto dtoAmount = (DetailDto)itAmount.next();
				if (dto.getDetailCode().equals(dtoAmount.getDetailCode())) {
					if (dtoAmount instanceof PmDetailBsDto) {
						setDetailAmount(dto, ((PmDetailBsDto)dtoAmount).getAmount());
						break;
					}
				}
			}
		}
	}
	
	/**
	 * 明細値を計算する。
	 * @param listDetail 明細リスト
	 * @param mapParams  パラメータMap
	 * @throws MospException 
	 */
	protected void calcDetails(List<?> listDetail, Map<String, String> mapParams) throws MospException {
		for (Iterator<?> it = listDetail.iterator(); it.hasNext();){
			DetailDto dto = (DetailDto)it.next();
			setDetailAmount(dto, calcExpr(dto.getExpression(), mapParams));
		}
	}
	
	/**
	 * 明細値を計算する。
	 * @param detailDto 明細DTO
	 * @param mapParams パラメータMap
	 * @throws MospException 
	 */
	protected void setDetailAmount(DetailDto detailDto, float amount) {
		if (detailDto instanceof PmDetailBsDto) {
			PmDetailBsDto dto = (PmDetailBsDto)detailDto;
			dto.setAmount((int)amount);
			return;
		}
		
	}
	
	/**
	 * 明細を初期化する。
	 * @param 初期化対象明細リスト
	 */
	protected void initDetails(List<?> list, String kCode, Date calcDate) {
		for (Iterator<?> it = list.iterator(); it.hasNext();){
			initDetail((DetailDto)it.next(), kCode, calcDate);
		}
	}
	
	/**
	 * 明細を初期化する。
	 * @param 初期化対象明細
	 */
	private void initDetail(DetailDto dto, String kCode, Date calcDate) {
		dto.setKCode   (kCode   );
		dto.setCalcDate(calcDate);
		if (dto instanceof PmDetailBsDto) {
			((PmDetailBsDto)dto).setAmount(0);
			return;
		}
	}
	
	/**
	 * 計算式を計算する。<br>
	 * @param expression 計算式
	 * @param mapParams  パラメータ
	 * @return 計算値
	 * @throws MospException 
	 */
	protected float calcExpr(
			String expression, Map<String, String> mapParams
	) throws MospException {
		return ExpressUtility.calculate(
				expression, mapParams, new HashMap<String, String[][]>());
	}
}
