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

import java.io.File;

import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;

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.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 (this.cmd.equals(CMD_REPORT)) {
			// 給与明細出力
			if (cfg.getProperty(PayrollConst.PPT_PAY_USEXLS) == null) {
				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));
		// 帳票作成
		SpreadSheet ss = SpreadSheet.createFromFile(new File(cfg.getProperty(MospConst.PPT_APP_DOCBASE) + "/template/samp_payment_slip.ods"));
		Sheet sheet = ss.getSheet(0);
		// ヘッダー項目
		int row = 4;
		int col = 1;
		sheet.getCellAt(col     , row).setValue(employee.getSectionCode());	// 所属コード
		sheet.getCellAt(col + 10, row).setValue(employee.getKCode      ());	// 社員コード
		sheet.getCellAt(col + 20, row).setValue(employee.getKName      ());	// 社員名
		sheet.getCellAt(col + 66, row).setValue(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;
			}
			sheet.getCellAt(col, row    ).setValue(aryTime[i][1]);
			sheet.getCellAt(col, row + 1).setValue(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;
			}
			sheet.getCellAt(col, row    ).setValue(aryAllowance[i][1]);
			sheet.getCellAt(col, row + 1).setValue(new Integer(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;
			}
			sheet.getCellAt(col, row    ).setValue(aryDeduction[i][1]);
			sheet.getCellAt(col, row + 1).setValue(new Integer(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;
			}
			sheet.getCellAt(col, row    ).setValue(arySum[i][1]);
			sheet.getCellAt(col, row + 1).setValue(new Integer(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_OO;
		request.setAttribute(MospConst.ATT_FILE_NAME, fileName);
		request.setAttribute(MospConst.ATT_FILE_OBJECT, ss);
	}
	
	/**
	 * 帳票出力処理を行う。<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) + "/template/samp_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());
	}

	
}
