package kr.co.udapsoft.ebid.buyer.payment.purchase.biz;

/*
 * 대금신청작성 MRR PDF
 */

import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.servlet.http.HttpServletRequest;

import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.config.WAFConfig;
import kr.co.hsnc.common.logger.Logger;
import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.RowSetImpl;
import kr.co.hsnc.common.sql.search.Search;
import kr.co.hsnc.common.sql.search.SearchImpl;
import kr.co.hsnc.common.util.StringUtil;
import kr.co.udapsoft.common.util.SignLineUtil;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.udapsoft.waf.common.util.StringUtil2;


public class PURCreatePDF3 {

	public static BaseFont bfKorean = null;

	private static Color borderColor = Color.BLACK;

	private static float BorderW = 0.1f;

	private static Rectangle border = new Rectangle(0f, 0f);
	private static boolean isGray = false;
	
	public void doBiz(String dept_cd, String dept_name, String vendor_name, String ord_doc_no, String payreq_id, String purContId, String req_date, String currency_gb, int digit, HttpServletRequest request) throws Exception {
		
		String req_date2 = req_date.replaceAll("-", "");
		req_date2 = req_date2.substring(0, 6);
		
		String yyyyMM = (new java.text.SimpleDateFormat ("yyyyMM")).format (new java.util.Date());
		
		Document document = new Document(PageSize.A4, 5f, 5f, 35f, 10f);	// marginLeft, marginRight, marginTop, marginBottom
		
		// 파일 열기
		// /payment/purchase/30672
//		String path = StringUtil.replace(WAFConfig.get("waf.repository.path") + "/"+yyyyMM, "/", File.separator);
		String path = StringUtil.replace(WAFConfig.get("waf.repository.path") + "/payment/purchase/"+payreq_id, "/", File.separator);
		
		int tempFileCnt = 0;
		File fileExist = new File(path);
		File fileList[] = fileExist.listFiles();
		if(fileList != null) {
			for(File tempFile : fileList) {
				if(tempFile.isFile()) {
//				
//					if(tempFile.getName().length() > 11 && tempFile.getName().substring(0, 12).equals(payreq_id+"_"+req_date2)) {
//						tempFileCnt++;
//					}
//					
					if(tempFile.getName().indexOf(ord_doc_no+"_"+req_date2) > -1) {
						tempFileCnt++;
					}
					
					
					
				}
			}
		}
		
		File dir = new File(path);
		if(dir.exists() == false) dir.mkdir();
		
		FileOutputStream file;

		if(tempFileCnt == 0) {
			file = new FileOutputStream(new File(path+"/"+ord_doc_no+"_"+req_date2+".pdf"));
		} else {
			file = new FileOutputStream(new File(path+"/"+ord_doc_no+"_"+req_date2+"_"+tempFileCnt+".pdf"));
		}
		
		
//		FileOutputStream file = new FileOutputStream(new File(path+"/mrr_pdf_test.pdf"));
		
		
		PdfWriter writer = PdfWriter.getInstance(document, file);
		
		try {
			document.open();
			
			//BaseFont bfKorean = BaseFont.createFont("HYGoThic-Medium", "UniKS-UCS2-H", BaseFont.NOT_EMBEDDED);
			BaseFont bfKorean = BaseFont.createFont("c:\\windows\\fonts\\ARIALUNI.TTF", BaseFont.IDENTITY_H , BaseFont.NOT_EMBEDDED);
			
			
			Font FontTitle = new Font(bfKorean, 15, Font.BOLD);
			FontTitle.setColor(borderColor);
			
			Font FontTitle2 = new Font(bfKorean, 8, Font.NORMAL);
			Font FontTitle3 = new Font(bfKorean, 7, Font.NORMAL);
			
			/*
			 * 정보 검색 시작 ---------------------------------------------
			 */

			RowSet listRowSet = getListRowSet(purContId, currency_gb);
			String docId = getDocId(purContId, req_date2);
			RowSet listRowSet2 = SignLineUtil.getSignUser(docId);
			//RowSet listRowSet2 = getListRowSet2(purContId,req_date2);
			
			//2016.6.22 MRR문서생성 수정 
			if (listRowSet.size() < 1) {
				listRowSet = getListRowSet3(payreq_id, currency_gb);
			}
			// 정보 검색 끝 ---------------------------------------------
			
			//String imgstr = request.getRequestURL().toString();
			String imgstr = WAFConfig.get("waf.Corp.Ip").toString();
			imgstr = imgstr.substring(0, imgstr.lastIndexOf("/")) + WAFConfig.get("waf.Corp.LogImg");
			Image image = Image.getInstance(imgstr);
			
			image.scalePercent(20f,  20f);
			image.setAbsolutePosition(10, 750);
			
			document.add(image);
			
			
			
			setBorder(border, true, true, true, true);
			
			
			// 결재 테이블 생성
			makeConfirmTable(document, FontTitle, FontTitle3, yyyyMM, listRowSet2, request);
			// 타이틀 생성
			makeTitleTable(document, FontTitle, FontTitle2, req_date2, request);
			// 갑지 정보 생성
			makeContentTable(document, FontTitle, FontTitle2, request, dept_name, vendor_name, ord_doc_no, listRowSet);
			// sheet 테이블 생성
			if (listRowSet != null && listRowSet.size() > 0) {
				makeContentDetailTable(document, FontTitle, FontTitle2,currency_gb , digit, request, listRowSet);
			}
			document.close();
			
			file.close();
			writer.close();
			
		} catch (DocumentException de) {
			WAFLogger.debug("de.getMessage() = " + de.getMessage());
		} catch (IOException ioe) {
			WAFLogger.debug("ioe.getMessage() = " + ioe.getMessage());
		} catch (Exception e) {
			WAFLogger.debug("[Exception] ... " + e);
		}
	}
	
	
	// 결재 테이블 생성
	private static void makeConfirmTable(Document document, Font FontTitle, Font FontTitle2, String yyyyMM, RowSet listRowSet, HttpServletRequest request) throws Exception {
		Rectangle borderTemp = new Rectangle(60, 0, 120, 70);	// 높이, 넓이
		
		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(0);
		borderTemp.setBorderWidthBottom(0);
		borderTemp.setBorderColor(Color.WHITE);
		
		
//		PdfPTable tbl_Title = new PdfPTable(listRowSet.getColumn(0, "sign_seq").getInt());
		float[] colSize = {5, 45};
		PdfPTable tbl_Title = new PdfPTable(colSize);
		tbl_Title.setWidthPercentage(50);
		tbl_Title.setHorizontalAlignment(2);	// 0 : LEFT, 1 : CENTER , 2: RIGHT
		
		

//		setBorder(borderTemp, false, false, false, false);
		
//		PT_addCell(tbl_Title, "이미지", FontTitle2, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 70f);
		
		
		setBorder(borderTemp, true, true, true, true);
		PT_addCell(tbl_Title, "발\n\n의\n\n부\n\n서", FontTitle2, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 70f);
		
		addImageTable(tbl_Title, FontTitle2, borderTemp, listRowSet);
		
		document.add(tbl_Title);
	}

	// 타이틀 테이블 생성
	private static void makeTitleTable(Document document, Font FontTitle, Font FontTitle2, String req_date, HttpServletRequest request) throws Exception {
		
		req_date = req_date.substring(0, 4) + "-" + req_date.substring(4); 
				
				
		Rectangle borderTemp = new Rectangle(10, 10, 80, 80);
		setBorder(borderTemp, false, false, false, false);
		
		PdfPTable tbl_Title = new PdfPTable(1);
		tbl_Title.setWidthPercentage(100);

		PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 7f);
		PT_addCell(tbl_Title, "MATERIAL RECEIVING REPORT ("+req_date+")", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
		PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
		
		document.add(tbl_Title);
	}
	
	// 갑지정보 컨텐츠 테이블 생성
	private static void makeContentTable(Document document, Font FontTitle, Font FontTitle2, 
			 HttpServletRequest request, String dept_name, String vendor_name, String ord_doc_no, RowSet listRowSet) throws Exception {

		Rectangle borderTemp = new Rectangle(0,0,150,130);
		setBorder(borderTemp, true, true, true, true);
		
		float[] colSize = {15.70f, 67.09f};	
		
		PdfPTable tbl_Title = new PdfPTable(colSize);
		
		tbl_Title.setHorizontalAlignment(0);	// 0 : LEFT, 1 : CENTER , 2: RIGHT
		
		tbl_Title.setWidthPercentage(66.10f);
		
		
		PT_addCell(tbl_Title, "현   장   명", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		PT_addCell(tbl_Title, dept_name, FontTitle2, borderTemp,Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
		
		PT_addCell(tbl_Title, "계약업체명", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		PT_addCell(tbl_Title, vendor_name, FontTitle2, borderTemp,Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
		
		PT_addCell(tbl_Title, "계 약 번 호", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		PT_addCell(tbl_Title, ord_doc_no, FontTitle2, borderTemp,Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
		
		PT_addCell(tbl_Title, "OS&D 여부", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		
		/*if(!listRowSet.getColumn(0, "over_qty").get().equals("")) {
			PT_addCell(tbl_Title, "□ Overage □ Shortage □ Damage", FontTitle2, borderTemp,Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
			
		} else if(!listRowSet.getColumn(0, "short_qty").get().equals("")) {
			PT_addCell(tbl_Title, "□ Overage ■ Shortage □ Damage", FontTitle2, borderTemp,Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
			
		} else {*/
			PT_addCell(tbl_Title, "□ Overage □ Shortage □ Damage", FontTitle2, borderTemp,Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
			
		//}
		
		setBorder(borderTemp, false, false, false, false);
		PT_addCell(tbl_Title, "", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 10f);
		PT_addCell(tbl_Title, "", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 10f);
		
		document.add(tbl_Title);
	}
	// sheet 데이터 정보 테이블 생성
	private static void makeContentDetailTable(Document document, Font FontTitle, Font FontTitle2,String currency_gb, int digit,  HttpServletRequest request, RowSet listRowSet) throws Exception {
		
		Rectangle borderTemp = new Rectangle(0, 0, 50, 50);	// 높이, 넓이
		setBorder(borderTemp, true, true, true, true);
		
		float[] colSize = {5f, 10f, 35f, 7f, 13f, 15f, 15f};
		PdfPTable tbl_Title = new PdfPTable(colSize); // 칸 수, 줄 수
		tbl_Title.setWidthPercentage(100);
		double sumItemAmt = 0;
		double sumExcAmt = 0;
		
		try {
			isGray = true;
			PT_addCell(tbl_Title, "순번", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);

			addSubTable(tbl_Title, FontTitle2, 1, 1);	// 상세내역, 자재명, 규격

			addSubTable2(tbl_Title, FontTitle2, 1, 1);	// 상세내역, 자재명, 규격

			addSubTable3(tbl_Title, FontTitle2, 1, 1);	// 상세내역, 자재명, 규격
			
			PT_addCell(tbl_Title, "입고수량", FontTitle2, borderTemp,Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			
			addSubTable4(tbl_Title, FontTitle2, currency_gb, 2, 2);	// 상세내역, 자재명, 규격
			
			isGray = false;
			
			for (int i = 0; i < listRowSet.size(); i++) {
				sumItemAmt += listRowSet.getRow(i).getDouble("item_amt");
				sumExcAmt += listRowSet.getRow(i).getDouble("exc_amt");
						
				PT_addCell(tbl_Title, String.valueOf(i+1), FontTitle2, borderTemp, Element.ALIGN_CENTER,Element.ALIGN_MIDDLE, 1);
				
				String[] arrMaterial = new String[2];
				arrMaterial[0] = listRowSet.getColumn(i, "material_cd").get();
				arrMaterial[1] = listRowSet.getColumn(i, "req_date").get();
				
				String[] arrMaterial2 = new String[2];
				arrMaterial2[0] = listRowSet.getColumn(i, "material_name").get();
				arrMaterial2[1] = listRowSet.getColumn(i, "material_spec").get();
				
				String[] arrMaterial3 = new String[2];
				arrMaterial3[0] = listRowSet.getColumn(i, "unit_kind_name").get();
				arrMaterial3[1] = listRowSet.getColumn(i, "exchage_rate").get();
				
				addSubTableData(tbl_Title, FontTitle2, arrMaterial, 1,1);

				addSubTableData2(tbl_Title, FontTitle2, arrMaterial2, 1,1);

				addSubTableData3(tbl_Title, FontTitle2, arrMaterial3, 1,1);

				PT_addCell(tbl_Title, StringUtil2.formatCurrency(listRowSet.getRow(i).getDouble("item_qty")), FontTitle2, border, Element.ALIGN_RIGHT,Element.ALIGN_MIDDLE, 1);


				String[] arrMaterial4 = new String[4];
				arrMaterial4[0] = StringUtil2.formatCurrency(listRowSet.getRow(i).getDouble("exc_pri"), digit);
				arrMaterial4[1] = StringUtil2.formatCurrency(listRowSet.getRow(i).getDouble("exc_amt"), digit);
				arrMaterial4[2] = StringUtil2.formatCurrency(listRowSet.getRow(i).getDouble("item_pri"), 0);
				arrMaterial4[3] = StringUtil2.formatCurrency(listRowSet.getRow(i).getDouble("item_amt"), 0);
				
				addSubTableData4(tbl_Title, FontTitle2, arrMaterial4, 2,2);
			}
			isGray = true;
			PT_addCell(tbl_Title, "합 계("+currency_gb+")", FontTitle2, borderTemp, Element.ALIGN_CENTER,Element.ALIGN_MIDDLE, 6, 18f);
			PT_addCell(tbl_Title, StringUtil2.formatCurrency(sumExcAmt,digit), FontTitle2, borderTemp, Element.ALIGN_RIGHT,Element.ALIGN_MIDDLE, 1, 18f);
			PT_addCell(tbl_Title, "합 계(KRW)", FontTitle2, borderTemp, Element.ALIGN_CENTER,Element.ALIGN_MIDDLE, 6, 18f);
			PT_addCell(tbl_Title, StringUtil2.formatCurrency(sumItemAmt,0), FontTitle2, borderTemp, Element.ALIGN_RIGHT,Element.ALIGN_MIDDLE, 1, 18f);
			isGray = false;
			document.add(tbl_Title);
			
		} catch (Exception e) {
			e.printStackTrace();

			StackTraceElement element = e.getStackTrace()[1];

			Logger.err.println(element.getClassName() + " --> "
					+ element.getMethodName() + " : " + e.getMessage());

			Logger.err.println("File  Name  : " + element.getFileName());
			Logger.err.println("Line Number : " + element.getLineNumber());

			Logger.err.printStackTrace((Exception) e);
		}
	}

	
	private static void addSubTable(PdfPTable wrkTable, Font FontTitle, int iCols,  int CS)
			throws Exception {

		PdfPTable tbl_Title = new PdfPTable(iCols);

		Rectangle borderTemp = new Rectangle(0f, 0f);

		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);

		PT_addCell(tbl_Title, "자재코드", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);

		PT_addCell(tbl_Title, "날짜", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);

		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);

		// Cell Border 설정
		cell.cloneNonPositionParameters(border);

		wrkTable.addCell(cell);
	}

	private static void addSubTable2(PdfPTable wrkTable, Font FontTitle, int iCols,  int CS)
			throws Exception {
		
		PdfPTable tbl_Title = new PdfPTable(iCols);
		
		Rectangle borderTemp = new Rectangle(0f, 0f);
		
		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);
		
		PT_addCell(tbl_Title, "자재명", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		
		PT_addCell(tbl_Title, "규격", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		
		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);
		
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);
		
		wrkTable.addCell(cell);
	}

	private static void addSubTable3(PdfPTable wrkTable, Font FontTitle, int iCols,  int CS)
			throws Exception {
		
		PdfPTable tbl_Title = new PdfPTable(iCols);
		
		Rectangle borderTemp = new Rectangle(0f, 0f);
		
		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);
		
		PT_addCell(tbl_Title, "단위", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		
		PT_addCell(tbl_Title, "환율", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		
		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);
		
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);
		
		wrkTable.addCell(cell);
	}

	private static void addSubTable4(PdfPTable wrkTable, Font FontTitle,String currency_gb, int iCols,  int CS)
			throws Exception {
		
		PdfPTable tbl_Title = new PdfPTable(iCols);
		
		Rectangle borderTemp = new Rectangle(0f, 0f);
		
		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);
		
		PT_addCell(tbl_Title, "단가("+currency_gb+")", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		PT_addCell(tbl_Title, "금액("+currency_gb+")", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		
		PT_addCell(tbl_Title, "단가(KRW)", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		PT_addCell(tbl_Title, "금액(KRW)", FontTitle, border, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		
		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);
		
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);
		
		wrkTable.addCell(cell);
	}

	
	private static void addSubTableData(PdfPTable wrkTable, Font FontTitle ,String arrCols[],int iCols, int CS)
			throws Exception {

		PdfPTable tbl_Title = new PdfPTable(iCols);

		
		Rectangle borderTemp = new Rectangle(0f, 0f);

		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);

		for (int i = 0; i < arrCols.length; i++) {
			PT_addCell(tbl_Title, arrCols[i], FontTitle, border,
						Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
		}

		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);

		wrkTable.addCell(cell);
	}

	private static void addSubTableData2(PdfPTable wrkTable, Font FontTitle ,String arrCols[],int iCols, int CS)
			throws Exception {
		
		PdfPTable tbl_Title = new PdfPTable(iCols);
		
		
		Rectangle borderTemp = new Rectangle(0f, 0f);
		
		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);
		
		for (int i = 0; i < arrCols.length; i++) {
			PT_addCell(tbl_Title, arrCols[i], FontTitle, border,
					Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
		}
		
		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);
		
		wrkTable.addCell(cell);
	}

	private static void addSubTableData3(PdfPTable wrkTable, Font FontTitle ,String arrCols[],int iCols, int CS)
			throws Exception {
		
		PdfPTable tbl_Title = new PdfPTable(iCols);
		
		
		Rectangle borderTemp = new Rectangle(0f, 0f);
		
		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);
		
		for (int i = 0; i < arrCols.length; i++) {
			if(i == 1){
				PT_addCell(tbl_Title, arrCols[i], FontTitle, border,
						Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 1, 20f);
			}
			PT_addCell(tbl_Title, arrCols[i], FontTitle, border,
					Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, 1, 20f);
		}
		
		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);
		
		wrkTable.addCell(cell);
	}

	private static void addSubTableData4(PdfPTable wrkTable, Font FontTitle ,String arrCols[],int iCols, int CS)
			throws Exception {
		
		PdfPTable tbl_Title = new PdfPTable(iCols);
		
		float RH = 20f;
		
		Rectangle borderTemp = new Rectangle(0f, 0f);
		
		borderTemp.setBorderWidthLeft(0);
		borderTemp.setBorderWidthRight(0);
		borderTemp.setBorderWidthTop(BorderW);
		borderTemp.setBorderWidthBottom(0);
		
		for (int i = 0; i < arrCols.length; i++) {
			
			PT_addCell(tbl_Title, arrCols[i], FontTitle, border,
					Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 1, RH);
		}
		
		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		cell.setColspan(CS);
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);
		
		wrkTable.addCell(cell);
	}
	
	
	private static void addImageTable(PdfPTable wrkTable, Font FontTitle, Rectangle borderTemp, RowSet listRowSet)
			throws Exception {
		
		PdfPTable tbl_Title = new PdfPTable(5);
		
		if (listRowSet == null || listRowSet.size() == 0 ) {
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "현장소장", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
//			PT_addCell(tbl_Title, "김홍일", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
//			
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
//			PT_addCell(tbl_Title, "2014.12.01", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			
			
		} else if(listRowSet.size() == 1) {
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		} else if(listRowSet.size() == 2) {
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
		} else if(listRowSet.size() == 3) {
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		} else if(listRowSet.size() == 4) {
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(3, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(3, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			
			PT_addCell(tbl_Title, "", FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(3, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		} else if(listRowSet.size() == 5) {
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(3, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(4, "A_POSITION").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(3, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			PT_addCell(tbl_Title, listRowSet.getColumn(4, "APPROVAL_USER").get(), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 40f);
			
			PT_addCell(tbl_Title, listRowSet.getColumn(0, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(1, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(2, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(3, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
			PT_addCell(tbl_Title, listRowSet.getColumn(4, "APPROVAL_DATE").get().substring(0, 10), FontTitle, borderTemp, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 1, 20f);
		} 
		
		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(tbl_Title);
		
		// Cell Border 설정
		cell.cloneNonPositionParameters(border);

		wrkTable.addCell(cell);
	}
	
	// PdfPCell을 리턴하자
	private static PdfPCell PC_addCell(String strText, Font FontTitle,
			Rectangle border, int HA, int VA, int CS) {

		// 추가할 Cell 생성
		PdfPCell cell = new PdfPCell(new Paragraph(strText, FontTitle));

		cell.cloneNonPositionParameters(border);

		// 가로/세로 정렬
		cell.setHorizontalAlignment(HA);
		cell.setVerticalAlignment(VA);

		if (CS > 1)
			cell.setColspan(CS);
		
		if( isGray )
			cell.setGrayFill(0.8f);

		return cell;
	}

	// Cell Border 설정
	private static void setBorder(Rectangle border, boolean blnLEFT,
			boolean blnRIGHT, boolean blnTOP, boolean blnBOTTOM) {
		border.setBorderColor(borderColor);

		if (blnLEFT) {
			border.setBorderWidthLeft(BorderW);
		} else {
			border.setBorderWidthLeft(0);
		}

		if (blnRIGHT) {
			border.setBorderWidthRight(BorderW);
		} else {
			border.setBorderWidthRight(0);
		}

		if (blnTOP) {
			border.setBorderWidthTop(BorderW);
		} else {
			border.setBorderWidthTop(0);
		}

		if (blnBOTTOM) {
			border.setBorderWidthBottom(BorderW);
		} else {
			border.setBorderWidthBottom(0);
		}
		
		
	}

	// PdfPTable에 Cell 을 추가하자
	private static void PT_addCell(PdfPTable table, String strText,
			Font FontTitle, Rectangle border, int HA, int VA, int CS) {

		// 추가할 Cell 생성
		PdfPCell cell = PC_addCell(strText, FontTitle, border, HA, VA, CS);

		table.addCell(cell);
	}

	// PdfPTable에 Cell 을 추가하자(행높이 추가)
	private static void PT_addCell(PdfPTable table, String strText,
			Font FontTitle, Rectangle border, int HA, int VA, int CS, float RH) {

		// 추가할 Cell 생성
		PdfPCell cell = PC_addCell(strText, FontTitle, border, HA, VA, CS);

		// 셀 높이
		if (RH > 0)
			cell.setFixedHeight(RH);

		table.addCell(cell);
	}
	
	private RowSet getListRowSet(String purContId, String currency_gb){

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        StringBuffer sql = new StringBuffer(5000);		// 초기메모리 확보함
        
        try {
//        	sql.append("	SELECT                                                                         		\n");
//            sql.append("	            	(                                                                		\n");
//            sql.append("	                	SELECT                                                       		\n");
//            sql.append("	                    	TO_CHAR(REQ_DATE, 'YYYY-MM-DD')                          		\n");
//            sql.append("	                    FROM PUR1000T                                              		\n");
//            sql.append("	                    WHERE PUR_CONT_ID = ?                                      		\n");
//            sql.append("	                ) AS REQ_DATE                                                  		\n");
//            sql.append("	            	, MATERIAL_CD                                                    		\n");
//            sql.append("	                , F_GET_MATERIAL_NAME(MATERIAL_CD) MATERIAL_NAME               		\n");
//            sql.append("	                , F_GET_MATL_SPEC_NAME(MATERIAL_CD) MATERIAL_SPEC              		\n");
//            sql.append("	                , F_GET_UNIT_KIND_NAME(MATERIAL_CD) UNIT_KIND_NAME             		\n");
//            sql.append("	                , SUM(NVL(ITEM_QTY,0)) ITEM_QTY                                		\n");
//            sql.append("	                , ROUND(SUM(NVL(ITEM_AMT,0)) / SUM(NVL(ITEM_QTY,0)),0) ITEM_PRI		\n");
//            sql.append("	                , SUM(NVL(ITEM_AMT,0)) ITEM_AMT                                		\n");
//            sql.append("	            FROM PMM_PAYREQ_ITEM_NEW_V                                         		\n");
//            sql.append("	            WHERE PUR_CONT_ID = ?                                              		\n");
//            sql.append("	            GROUP BY MATERIAL_CD                                               		\n");
//            sql.append("	            ORDER BY MATERIAL_CD                                               		\n");
        	
        	sql.append("    SELECT    B.STOCK_DATE AS REQ_DATE                                                                                                                                                       	\n");
        	sql.append("                    , MATERIAL_CD                                                                                                                                                            	\n");
        	sql.append("                    , F_GET_MATERIAL_NAME(MATERIAL_CD) MATERIAL_NAME                                                                                                                         	\n");
        	sql.append("                    , F_GET_MATL_SPEC_NAME(MATERIAL_CD) MATERIAL_SPEC                                                                                                                        	\n");
        	sql.append("                    , F_GET_UNIT_KIND_NAME(MATERIAL_CD) UNIT_KIND_NAME                                                                                                                       	\n");
        	sql.append("                    , SUM(NVL(ITEM_QTY,0)) ITEM_QTY                                                                                                                                          	\n");
        	sql.append("                    , ROUND(SUM(NVL(ITEM_AMT,0)) / SUM(NVL(ITEM_QTY,0)),0) ITEM_PRI                                                                                                          	\n");
        	sql.append("                    , SUM(NVL(ITEM_AMT,0)) ITEM_AMT                                                                                                                                          	\n");
        	sql.append("                    , ROUND(SUM(NVL(EXC_AMT,0)) / SUM(NVL(ITEM_QTY,0)),0) EXC_PRI                                                                                                          	\n");
        	sql.append("                    , SUM(NVL(EXC_AMT,0)) EXC_AMT                                                                                                                                          	\n");
        	sql.append("                    , F_GET_EXCHANGE_RATE(B.STOCK_DATE , ?, 'KRW') AS EXCHAGE_RATE                                                                                                       \n");
        	sql.append("                FROM PMM_PAYREQ_ITEM_NEW_V A                                                                                                                                                 	\n");
        	sql.append("                          ,(SELECT * FROM (                                                                                                                                                  	\n");
        	sql.append("                            SELECT MRR_NO AS STOCK_NO , ITEM_NO, TO_CHAR(TO_DATE(SUBSTR(WAREHOUSE_DATE, 0, 8), 'yyyy-mm-dd'),  'yyyy-mm-dd') AS STOCK_DATE                                   	\n");
        	sql.append("                            FROM ETECEPC.FM_WAREHOUSE                                                                                                                                        	\n");
        	sql.append("                            UNION                                                                                                                                                            	\n");
        	sql.append("                            SELECT A.SHIP_NO AS STOCK_NO, C.ITEM_NO AS ITEM_NO, TO_CHAR(TO_DATE(DECODE(SUBSTR(A.SHIP_DATE, 0, 8),NULL, SUBSTR(ARRIVAL, 0, 8), SUBSTR(A.SHIP_DATE, 0, 8)), 'yyyy-mm-dd'), 'yyyy-mm-dd') AS STOCK_DATE  	\n");
        	sql.append("                            FROM ETECEPC.FM_SHIPPING A, ETECEPC.FM_SHIPPING_LIST B, ETECEPC.FM_SHIPPING_DETAIL C                                                                             	\n");
        	sql.append("                            WHERE A.SHIPPING_ID = B.SHIPPING_ID                                                                                                                              	\n");
        	sql.append("                            AND B.SHIPPING_LIST_ID = C.SHIPPING_LIST_ID                                                                                                                      	\n");
        	sql.append("                            UNION                                                                                                                                                            	\n");
        	sql.append("                            SELECT TO_CHAR(A.PAYREQ_ID) STOCK_NO, B.MATERIAL_CD AS ITEM_NO, TO_CHAR(TO_DATE(STOCK_DATE, 'yyyy-mm-dd'), 'yyyy-mm-dd') AS STOCK_DATE                                                	\n");
        	sql.append("                            FROM PUR1100T A, PMM_PAYREQ_ITEM  B                                                                                                                  	\n");
        	sql.append("                            WHERE A.PAYREQ_ID = B.PAYREQ_ID )                                                                                                                                  	\n");
        	sql.append("                         )  B                                                                                                                                                                	\n");
        	sql.append("                WHERE A.STOCK_NO = B.STOCK_NO (+)                                                                                                                                            	\n");
        	sql.append("                AND A.MATERIAL_CD = B.ITEM_NO (+)                                                                                                                                            	\n");
        	sql.append("                AND A.PUR_CONT_ID = ?                                                                                                                                                        	\n");
        	sql.append("                GROUP BY MATERIAL_CD, B.STOCK_DATE                                                                                                                                           	\n");
        	sql.append("                ORDER BY MATERIAL_CD                                                                                                                                                         	\n");

        	
            search.setStatement(sql.toString());
            search.addParameter(currency_gb);
            search.addParameter(purContId);
           // search.addParameter(purContId);
            listRowSet = search.execute();
        }
        catch(Exception e) {
        	WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
         	return null;
             
         } finally {
             if( listRowSet == null ) listRowSet = new RowSetImpl();
         }
         return listRowSet;
     }

	private String getDocId(String purContId, String yyyymm) {
		Search search = new SearchImpl();
		RowSet listRowSet = null;
		StringBuffer sql = new StringBuffer();
		String rtnStr = "";
		try {
			sql.append("SELECT  MAX(ERP_SIGN_DOC_ID) AS ERP_SIGN_DOC_ID	\n");
			sql.append("FROM    PUR1100T								\n");
			sql.append("WHERE   PUR_CONT_ID = ?							\n");
			sql.append("AND     TO_CHAR(REQ_DATE,'YYYYMM') = ?			\n");
			sql.append("AND     ERP_SIGN_DOC_ID IS NOT NULL				\n");
			search.setStatement(sql.toString());
			search.addParameter(purContId);
			search.addParameter(yyyymm);
			listRowSet = search.execute();
			if( listRowSet != null && listRowSet.size() > 0 ) {
				rtnStr = listRowSet.getRow(0).get("ERP_SIGN_DOC_ID");
			}
			return rtnStr;
		}catch(Exception e) {
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
			return "";
		}
	}
	
	private RowSet getListRowSet2(String purContId, String yyyymm){
		
		Search search = new SearchImpl();
		RowSet listRowSet = null;
		StringBuffer sql = new StringBuffer();
		
		try {
			sql.append("	SELECT                                                                                		\n");
			sql.append("	    	A_POSITION                                                                      		\n");
			sql.append("	        , APPROVAL_USER                                                               		\n");
			sql.append("	        , APPROVAL_DATE                                                               		\n");
			sql.append("	    FROM SIGN002T                                                                     		\n");
			sql.append("	    WHERE TO_CHAR(SLIP_NO) IN (                                                                		\n");
			sql.append("	    					SELECT                                                                  		\n");
			sql.append("	                        	SIGN_INTERFACE_ID || SUBSTR(KEY_COL1, 6)                    		\n");
			sql.append("	                        FROM (                                                        		\n");
			sql.append("	                        		SELECT                                                    	\n");
			sql.append("	                                	SIGN_INTERFACE_ID, KEY_COL1                         	\n");
			sql.append("	                                FROM SIGN001T A                                        		\n");
			sql.append("	                                    ,PUR1100T B                                   		    \n");
			sql.append("	                               WHERE A.DOC_ID = B.ERP_SIGN_DOC_ID 			                \n");
			sql.append("	                                 AND B.PUR_CONT_ID = ?                    	                \n");
			sql.append("	                                 AND TO_CHAR(B.REQ_DATE, 'YYYYMM') = '"+yyyymm+"'           \n");
			sql.append("	                        	)                                                           	\n");
			sql.append("	    				)                                                                       \n");
			sql.append("	   ORDER BY SIGN_SEQ                                                                        \n");
			
			search.setStatement(sql.toString());
			search.addParameter(purContId);
			listRowSet = search.execute();
		}
		catch(Exception e) {
			WAFLogger.error("SQL : " + search.getStatement());
			WAFLogger.error(e);
			return null;
			
		} finally {
			if( listRowSet == null ) listRowSet = new RowSetImpl();
		}
		return listRowSet;
	}
	
	//2016.6.22 MRR문서생성 수정 
	private RowSet getListRowSet3(String payreq_id, String currency_gb){

        Search search = new SearchImpl();
        RowSet listRowSet = null;
        StringBuffer sql = new StringBuffer(5000);		// 초기메모리 확보함
        
        try {
 
        	sql.append("    SELECT    B.STOCK_DATE AS REQ_DATE                                                                                                                                                       	\n");
        	sql.append("                    , MATERIAL_CD                                                                                                                                                            	\n");
        	sql.append("                    , F_GET_MATERIAL_NAME(MATERIAL_CD) MATERIAL_NAME                                                                                                                         	\n");
        	sql.append("                    , F_GET_MATL_SPEC_NAME(MATERIAL_CD) MATERIAL_SPEC                                                                                                                        	\n");
        	sql.append("                    , F_GET_UNIT_KIND_NAME(MATERIAL_CD) UNIT_KIND_NAME                                                                                                                       	\n");
        	sql.append("                    , SUM(NVL(ITEM_QTY,0)) ITEM_QTY                                                                                                                                          	\n");
        	sql.append("                    , ROUND(SUM(NVL(ITEM_AMT,0)) / SUM(NVL(ITEM_QTY,0)),0) ITEM_PRI                                                                                                          	\n");
        	sql.append("                    , SUM(NVL(ITEM_AMT,0)) ITEM_AMT                                                                                                                                          	\n");
        	sql.append("                    , SUM(NVL(EXC_AMT,0)) / SUM(NVL(ITEM_QTY,0)) EXC_PRI                                                                                                          	\n");
        	sql.append("                    , SUM(NVL(EXC_AMT,0)) EXC_AMT                                                                                                                                          	\n");
        	sql.append("                    , F_GET_EXCHANGE_RATE(B.STOCK_DATE , ?, 'KRW' ) AS EXCHAGE_RATE                                                                                                       \n");
        	sql.append("                FROM PMM_PAYREQ_ITEM A                                                                                                                                                 	\n");
        	sql.append("                          ,(SELECT * FROM (                                                                                                                                                  	\n");
        	sql.append("                            SELECT MRR_NO AS STOCK_NO , ITEM_NO, TO_CHAR(TO_DATE(SUBSTR(WAREHOUSE_DATE, 0, 8), 'yyyy-mm-dd'),  'yyyy-mm-dd') AS STOCK_DATE                                   	\n");
        	sql.append("                            FROM ETECEPC.FM_WAREHOUSE                                                                                                                                        	\n");
        	sql.append("                            UNION                                                                                                                                                            	\n");
        	sql.append("                            SELECT A.SHIP_NO AS STOCK_NO, C.ITEM_NO AS ITEM_NO, TO_CHAR(TO_DATE(DECODE(SUBSTR(A.SHIP_DATE, 0, 8),NULL, SUBSTR(ARRIVAL, 0, 8), SUBSTR(A.SHIP_DATE, 0, 8)), 'yyyy-mm-dd'), 'yyyy-mm-dd') AS STOCK_DATE  	\n");
        	sql.append("                            FROM ETECEPC.FM_SHIPPING A, ETECEPC.FM_SHIPPING_LIST B, ETECEPC.FM_SHIPPING_DETAIL C                                                                             	\n");
        	sql.append("                            WHERE A.SHIPPING_ID = B.SHIPPING_ID                                                                                                                              	\n");
        	sql.append("                            AND B.SHIPPING_LIST_ID = C.SHIPPING_LIST_ID                                                                                                                      	\n");
        	sql.append("                            UNION                                                                                                                                                            	\n");
        	sql.append("                            SELECT TO_CHAR(A.PAYREQ_ID) STOCK_NO, B.MATERIAL_CD AS ITEM_NO, TO_CHAR(TO_DATE(STOCK_DATE, 'yyyy-mm-dd'), 'yyyy-mm-dd') AS STOCK_DATE                                                	\n");
        	sql.append("                            FROM PUR1100T A,  PMM_PAYREQ_ITEM  B                                                                                                                  	\n");
        	sql.append("                            WHERE A.PAYREQ_ID = B.PAYREQ_ID )                                                                                                                                  	\n");
        	sql.append("                         )  B                                                                                                                                                                	\n");
        	sql.append("                WHERE TO_CHAR(A.PAYREQ_ID) = B.STOCK_NO (+)                                                                                                                                            	\n");
        	sql.append("                AND A.MATERIAL_CD = B.ITEM_NO (+)                                                                                                                                            	\n");
        	sql.append("                AND A.PAYREQ_ID = ?                                                                                                                                                        	\n");
        	sql.append("                GROUP BY MATERIAL_CD, B.STOCK_DATE                                                                                                                                           	\n");
        	sql.append("                ORDER BY MATERIAL_CD                                                                                                                                                         	\n");

        	
            search.setStatement(sql.toString());
            search.addParameter(currency_gb);
            search.addParameter(payreq_id);
           // search.addParameter(purContId);
            listRowSet = search.execute();
        }
        catch(Exception e) {
        	WAFLogger.error("SQL : " + search.getStatement());
            WAFLogger.error(e);
         	return null;
             
         } finally {
             if( listRowSet == null ) listRowSet = new RowSetImpl();
         }
         return listRowSet;
     }
}
