package jp.mosp.payroll.base;

import java.math.BigDecimal;
import java.util.Date;

import jp.mosp.common.common.MospUtility;
import jp.mosp.kintai.common.dto.MKihonDto;
import jp.mosp.payroll.dto.CmSocInsuranceDto;

public abstract class AttendanceTotalAction extends PayrollAction {
	
	// フィールド
	protected String			kCode;
	protected int				year;
	protected int				month;
	protected Date				targetMonth;
	protected Date				startDate;
	protected Date				endDate;
	
	protected MKihonDto			mKihonDto;
	protected CmSocInsuranceDto	cmSocInsuranceDto;
	
	protected String[][]		aryPosition;
	
	double						minority25	= 0.25;
	double						minority50	= 0.50;
	double						minority75	= 0.75;
	
	float						minority25f	= 0.25F;
	float						minority50f	= 0.50F;
	float						minority75f	= 0.75F;
	

	/**
	 * 対象年月設定。<br>
	 * @param targetMonth 設定対象
	 * @throws Exception 
	 */
	protected void setTargetMonth(Date targetMonth) throws Exception {
		this.targetMonth = targetMonth;
		year = MospUtility.getYear(targetMonth);
		month = MospUtility.getMonth(targetMonth);
		startDate = getStartDate(targetMonth);
		endDate = getEndDate(targetMonth);
	}
	
	/**
	 * 丸め処理を行う。<br>
	 * 時間を15分単位で切り上げる。<br>
	 * @param value 対象値(時間)
	 */
	protected float roundTime(float value) {
		// 時間を整数部と小数点部に分割
		int integer = (int)round(value, 0, BigDecimal.ROUND_DOWN);
		float decimal = value - integer;
		// 小数点部切り上げ
		float roundup = 0F;
		if (decimal == 0) {
			roundup = 0F;
		} else if (decimal <= minority25) {
			roundup = minority25f;
		} else if (decimal <= minority50) {
			roundup = minority50f;
		} else if (decimal <= minority75) {
			roundup = minority75f;
		} else {
			roundup = 1;
		}
		// 整数部+小数点切り上げ
		return integer + roundup;
	}
	
	/**
	 * 丸め処理(四捨五入等)
	 * @param value 対象値
	 * @param scale 対象位置(少数点以下：0で整数)
	 * @param mode  方法
	 */
	protected double round(double value, int scale, int mode) {
		BigDecimal bd;
		if (scale >= 0) {
			bd = new BigDecimal(value);
			return bd.setScale(scale, mode).doubleValue();
		}
		int minusScale = scale * -1;
		double dividing = 1;
		for (int i = 0; i < minusScale; i++) {
			dividing *= 10;
		}
		bd = new BigDecimal(value / dividing);
		return bd.setScale(0, mode).doubleValue() * dividing;
	}
	
}
