package jp.mosp.payroll.payment.action;

import jp.mosp.common.CommonConst;
import jp.mosp.common.bean.OdsBean;
import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.common.common.PoiUtility;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.payroll.base.Employee;
import jp.mosp.payroll.base.Payment;
import jp.mosp.payroll.base.PayrollAction;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.payment.vo.PaymentReportVo;

/**
 * 給与明細の出力を行う。<br><br>
 * 以下のコマンドを扱う。
 * <ul><li>
 * {@link #CMD_REPORT}
 * </li></ul>
 */
public class PaySlipOutputAction extends PayrollAction {
	
	/**
	 * 給与明細出力コマンド。<br><br>
	 * 条件により、給与明細をExcelで出力する。<br>
	 */
	public static final String	CMD_REPORT	= "P1245";
	

	/**
	 * Actionの初期化を行う。<br><br>
	 * 以下の処理を行う。
	 * <ul><li>
	 * {@link PayrollAction#PayrollAction()}
	 * </li></ul>
	 */
	public PaySlipOutputAction() {
		super();
		setNeedProcSeq(false);
	}
	
	/* (non-Javadoc)
	 * @see jp.mosp.common.common.BaseAction#getSpecificVo()
	 */
	@Override
	protected BaseVo getSpecificVo() {
		return new PaymentReportVo();
	}
	
	/**
	 * アクション。<br>
	 * 詳細は、クラス説明を参照。<br>
	 */
	public void action() throws Exception {
		// VO取得及び設定
		prepareVo();
		if (cmd.equals(CMD_REPORT)) {
			// 給与明細出力
			if (StringUtil.isNotNull(cfg.getProperty(CommonConst.PPT_KINTAI_USE_ODF))) {
				report();
			} else {
				reportXls();
			}
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	/**
	 * 帳票出力処理を行う。<br><br>
	 * @throws Exception 
	 */
	private void report() throws Exception {
		// コネクション取得
		getConnection();
		// 社員情報取得
		Employee employee = getEmployee(key1);
		// 明細情報取得
		Payment payment = getPayment(key1, MospUtility.getDate(key2));
		// 帳票作成
		OdsBean odsBean = new OdsBean(cfg.getProperty(MospConst.PPT_APP_DOCBASE) + PayrollConst.TPL_PAYMENT_SLIP_ODS);
		// ヘッダー項目
		int row = 4;
		int col = 1;
		odsBean.setValue(col, row, employee.getSectionCode()); // 所属コード
		odsBean.setValue(col + 10, row, employee.getKCode()); // 社員コード
		odsBean.setValue(col + 20, row, employee.getKName()); // 社員名
		odsBean.setValue(col + 66, row, payment.getPayDate()); // 支給日
		// 勤怠項目
		row = 6;
		col = 4;
		String[][] aryTime = payment.getTimeArray(true);
		for (int i = 0; i < aryTime.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			odsBean.setValue(col, row, aryTime[i][1]);
			odsBean.setValue(col, row + 1, aryTime[i][2]);
		}
		// 支給項目
		row = 11;
		col = 4;
		String[][] aryAllowance = payment.getAllowanceArray(true);
		for (int i = 0; i < aryAllowance.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			odsBean.setValue(col, row, aryAllowance[i][1]);
			setOdsAmount(col, row + 1, aryAllowance[i][2], odsBean);
		}
		// 控除項目
		row = 22;
		col = 4;
		String[][] aryDeduction = payment.getDeductionArray(true);
		for (int i = 0; i < aryDeduction.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			odsBean.setValue(col, row, aryDeduction[i][1]);
			setOdsAmount(col, row + 1, aryDeduction[i][2], odsBean);
		}
		// 合計項目
		row = 33;
		col = 4;
		String[][] arySum = payment.getSumArray(true);
		for (int i = 0; i < arySum.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			odsBean.setValue(col, row, arySum[i][1]);
			setOdsAmount(col, row + 1, arySum[i][2], odsBean);
		}
		// 出力内容設定
		String fileName = MospUtility.getYear(payment.getCalcMonth()) + PayrollConst.FILE_NAME_SEPARATOR
				+ MospUtility.getMonth(payment.getCalcMonth()) + PayrollConst.FILE_NAME_SEPARATOR + payment.getKCode()
				+ PayrollConst.FILE_NAME_SEPARATOR + PayrollConst.FILE_PAYMENT_SLIP_OO;
		request.setAttribute(MospConst.ATT_FILE_NAME, fileName);
		request.setAttribute(MospConst.ATT_FILE_OBJECT, odsBean.getSpreadSheet());
	}
	
	/**
	 * 帳票出力処理を行う。<br><br>
	 * @throws Exception 
	 */
	private void reportXls() throws Exception {
		// コネクション取得
		getConnection();
		// 社員情報取得
		Employee employee = getEmployee(key1);
		// 明細情報取得
		Payment payment = getPayment(key1, MospUtility.getDate(key2));
		// 帳票作成
		PoiUtility poi = new PoiUtility();
		poi.crtNewDocR(0, cfg.getProperty(MospConst.PPT_APP_DOCBASE) + PayrollConst.TPL_PAYMENT_SLIP_XLS);
		poi.prtItem(4, 1, employee.getSectionCode()); // 所属コード
		poi.prtItem(4, 11, employee.getKCode()); // 社員コード
		poi.prtItem(4, 21, employee.getKName()); // 社員名
		poi.prtItem(4, 67, payment.getPayDate()); // 支給日
		// 勤怠項目
		int row = 6;
		int col = 4;
		String[][] aryTime = payment.getTimeArray(true);
		for (int i = 0; i < aryTime.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			poi.prtItem(row, col, aryTime[i][1]);
			poi.prtItem(row + 1, col, aryTime[i][2]);
		}
		// 支給項目
		row = 11;
		col = 4;
		String[][] aryAllowance = payment.getAllowanceArray(true);
		for (int i = 0; i < aryAllowance.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			poi.prtItem(row, col, aryAllowance[i][1]);
			poi.prtItem(row + 1, col, Integer.parseInt(aryAllowance[i][2]));
		}
		// 控除項目
		row = 22;
		col = 4;
		String[][] aryDeduction = payment.getDeductionArray(true);
		for (int i = 0; i < aryDeduction.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			poi.prtItem(row, col, aryDeduction[i][1]);
			poi.prtItem(row + 1, col, Integer.parseInt(aryDeduction[i][2]));
		}
		// 合計項目
		row = 33;
		col = 4;
		String[][] arySum = payment.getSumArray(true);
		for (int i = 0; i < arySum.length; i++) {
			if (i % 10 == 0 && i != 0) {
				row += 2;
				col = 4;
			} else if (i != 0) {
				col += 9;
			}
			poi.prtItem(row, col, arySum[i][1]);
			poi.prtItem(row + 1, col, Integer.parseInt(arySum[i][2]));
		}
		
		// 出力内容設定
		String fileName = MospUtility.getYear(payment.getCalcMonth()) + PayrollConst.FILE_NAME_SEPARATOR
				+ MospUtility.getMonth(payment.getCalcMonth()) + PayrollConst.FILE_NAME_SEPARATOR + payment.getKCode()
				+ PayrollConst.FILE_NAME_SEPARATOR + PayrollConst.FILE_PAYMENT_SLIP;
		request.setAttribute(MospConst.ATT_FILE_NAME, fileName);
		request.setAttribute(MospConst.ATT_FILE_OBJECT, poi.getWorkbook());
	}

}
