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

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jp.mosp.payroll.dto.CashKindListDto;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.base.PayrollDao;

public class CashKindListDao extends PayrollDao {
	//楍柤
	public static final String COL_K_CODE               = PhPaymentDao.COL_KCODE              ;	// 幮堳僐乕僪
	public static final String COL_CALC_DATE            = PhPaymentDao.COL_CALC_MONTH         ;	// 寁嶼擭寧
	public static final String COL_PAYDAY               = PhPaymentDao.COL_PAY_DATE           ;	// 媼梌巟媼擔
	public static final String COL_K_NAME               = MKihonPayrollDao.COL_SIMEI          ;	// 幮堳巵柤
	public static final String COL_DETAIL_CODE          = DetailMoneyDao.COL_DETAIL_CODE      ;  // 柧嵶僐乕僪
	public static final String COL_AMOUNT               = DetailMoneyDao.COL_AMOUNT           ;  // 柧嵶嬥妟
	public static final String COL_DETAIL_CODE_PS013    = "PS013"                             ; //柧嵶僐乕僪丂
	
	/**
	 * 僐儞僗僩儔僋僞
	 */
	public CashKindListDao() {
		super();
		INSERT_DATE = "";
		INSERT_USER = "";
		UPDATE_DATE = "";
		UPDATE_USER = "";
	}
	
	/**
	 * 専嶕寢壥庢摼(DTO偺List)
	 * @return List 専嶕儕僗僩
	 * @throws SQLException
	 */
	private List<CashKindListDto> mappingAll() throws SQLException {
		List<CashKindListDto> all = new ArrayList<CashKindListDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 専嶕寢壥庢摼(DTO)
	 * @return  CashKindListDto 尰嬥嬥妟儗僐乕僪
	 * @throws SQLException
	 */
	private CashKindListDto mapping() throws SQLException {
		CashKindListDto dto = new CashKindListDto();
		dto.setKCode        (rs.getString(COL_K_CODE            ));	// 幮堳僐乕僪
		dto.setCalcDate     (rs.getDate(COL_CALC_DATE           ));	// 寁嶼擭寧
		dto.setPayday       (rs.getDate(COL_PAYDAY              ));	// 媼梌巟媼擔
		dto.setKName        (rs.getString(COL_K_NAME            ));	// 幮堳巵柤
		dto.setCashAmount   (rs.getInt(COL_AMOUNT               ));	// 尰嬥巟媼妟
		return dto;
	}
	
	/**
	 * 忦審暥偵傛傞尰嬥嬥妟僨乕僞庢摼
	 * @param adjustYear 懳徾擭
	 * @param lastDate   懳徾寧
	 * @return List 専嶕寢壥(DTO偺List)
	 * @throws SQLException
	 */
	public List<CashKindListDto> findForCondition (
			Date   calcDate
	) throws  SQLException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		setCashKindPayment(sb);
		prepareStatement(sb.toString());
		setDateParam(index++, calcDate   );
		sb.append(" ORDER BY ");
		sb.append(	COL_K_CODE              );
		try {
			//幚峴
			executeQuery();
			List<CashKindListDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
	
	/**
	 * 忦審暥偵傛傞尰嬥嬥妟僨乕僞庢摼(媼梌)
	 * @param sb SQL僶僢僼傽
	 * @throws SQLException
	 */
	private void setCashKindPayment (StringBuffer sb) {
		sb.append("SELECT ");
		sb.append(" PHPAY.");
		sb.append(	COL_K_CODE              );
		sb.append(", ");
		sb.append(" PHPAY.");
		sb.append(	COL_CALC_DATE           );
		sb.append(", ");
		sb.append(" PHPAY.");
		sb.append(	COL_PAYDAY              );
		sb.append(", ");
		sb.append(" MKIHON.");
		sb.append(	COL_K_NAME              );
		sb.append(", ");
		sb.append(" PDSUM.");
		sb.append(	COL_AMOUNT     );
		
		sb.append(" FROM ");
		sb.append(PhPaymentDao.TABLE + " PHPAY ");
		sb.append(", ");
		sb.append(MKihonPayrollDao.TABLE + " MKIHON ");
		sb.append(", ");
		sb.append("PD_PAY_SUM" + " PDSUM ");
		
		sb.append(" WHERE ");
		sb.append(" PHPAY.");
		sb.append(  COL_K_CODE           );
		sb.append(" = ");
		sb.append(" MKIHON.");
		sb.append(  COL_K_CODE           );
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(  COL_K_CODE           );
		sb.append(" = ");
		sb.append(" PDSUM.");
		sb.append(  COL_K_CODE           );
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(  COL_CALC_DATE        );
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(" PHPAY.");
		sb.append(  COL_CALC_DATE        );
		sb.append(" = ");
		sb.append(" PDSUM.");
		sb.append(  "CALC_DATE"          );
		sb.append(" AND ");
		sb.append(" PDSUM.");
		sb.append(  COL_DETAIL_CODE      );
		sb.append(" = ");
		sb.append(  "'" + COL_DETAIL_CODE_PS013 + "'"     );
		sb.append(" AND ");
		sb.append(" PDSUM.");
		sb.append(  COL_AMOUNT      );
		sb.append(" <> ");
		sb.append(PayrollConst.AMOUNT_ZERO);
	}

}
