/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import DataEntryBean.ChequeDetailsBean;
import LoginDb.DbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author 1004242
 */
public class ChequePostingDao {

    public String PostChequeProc(String tellerId, String pacsId, String accNo, String chqNo, String chqDt, Double chqAmt, String chqBnk) {
        Connection con = null;
        CallableStatement proc = null;
        String res = null;


        try {
            con = DbHandler.getDBConnection();
            proc = con.prepareCall("{ call operations1.POST_CHEQUE(?,?,?,?,?,?,?,?) }");

            proc.setString(1, tellerId);
            proc.setString(2, pacsId);
            proc.setString(3, accNo);
            proc.setString(4, chqNo);
            proc.setString(5, chqDt);
            proc.setDouble(6, chqAmt);
            proc.setString(7, chqBnk);
            proc.registerOutParameter(8, OracleTypes.VARCHAR);


            proc.executeUpdate();

           // res = proc.getString(8);//error message from dds procedure

        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured  in PostChequeProc for user Id :" + tellerId);
           System.out.println("Error occurred during processing.");
           
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return res;
    }

    public ArrayList<ChequeDetailsBean> getChequedetails(String pacsId, String accNo) {

            ArrayList<ChequeDetailsBean> allChequeList = new ArrayList<ChequeDetailsBean>();
        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection con = null;
        StringBuilder chequeGetQuery = null;
        ChequeDetailsBean  chqDet = null;
       
        try {

            con = DbHandler.getDBConnection();
            chequeGetQuery = new StringBuilder()
                            .append(" Select CHQ_NO ,to_char(CHQ_DATE,'dd-mm-yyyy') CHQ_DATE ,CHQ_AMT,CHQ_CLR_BANK,to_char(POST_DATE,'dd-mm-yyyy') POST_DATE,KEY_1 from INWARD_CHQ_DETAILS ")
                            .append(" where key_1 = ? and pacs_id = ? and status='PENDING' ");
            ps = con.prepareStatement(chequeGetQuery.toString());
            ps.setString(1, accNo);
            ps.setString(2, pacsId);
           // rs = ps.executeQuery();
            while (rs.next()) {

                chqDet = new ChequeDetailsBean();
                chqDet.setChqno(rs.getString("CHQ_NO"));
                chqDet.setChqDate(rs.getString("CHQ_DATE"));
                chqDet.setChqAmnt(rs.getDouble("CHQ_AMT"));
                chqDet.setChqBank(rs.getString("CHQ_CLR_BANK"));
                chqDet.setChqPostDate(rs.getString("POST_DATE"));
               
                allChequeList.add(chqDet);
            }
        } catch (Exception e) {
           // e.printStackTrace();
           // System.out.println("Exception occured fetching dept details for pacs :" + pacsId);
           System.out.println("Error occurred during processing.");
           
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }
        return allChequeList;
    }


     public String ProcessChequeProc(String tellerId, String pacsId, String accNo, String chqNo, Double comission, String action , String narration) {
        Connection con = null;
        CallableStatement proc = null;
        String res = null;


        try {
            con = DbHandler.getDBConnection();
            proc = con.prepareCall("{ call operations1.POST_CHEQUE_TXN(?,?,?,?,?,?,?,?) }");

            proc.setString(1, tellerId);
            proc.setString(2, pacsId);
            proc.setString(3, accNo);
            proc.setString(4, chqNo);
            proc.setString(5, action);
            proc.setDouble(6, comission);
            proc.registerOutParameter(7, OracleTypes.VARCHAR);
            proc.setString(8, narration);


            proc.executeUpdate();

           // res = proc.getString(7);//error message from dds procedure

        } catch (Exception e) {
            res= "Error in processsing  cheque No :"+chqNo;
           // e.printStackTrace();
           // System.out.println("Exception occured  in ProcessChequeProc for user Id :" + tellerId);
           System.out.println("Error occurred during processing.");
        } finally {
            try {
               // con.commit();
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
               // e.printStackTrace();
               System.out.println("Error occurred during connection close.");
            }
        }

        return res;
    }
}
