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

package Dao;

import DataEntryBean.MicroFileAccountDetBean;
import LoginDb.DbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import oracle.jdbc.OracleTypes;

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


     public String CashDepositOperation(String tellerId, String pacsId,String accNo,Double amt,String txnType , MicroFileAccountDetBean accountDet ) {
        Connection con = null;
        CallableStatement proc = null;
        String message = null;
        String res= null;
        

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

                          proc.setString(1, tellerId);
                          proc.setString(2, pacsId);
                          proc.setString(3, accNo);
                          proc.setDouble(4, amt);
                          proc.setString(5, txnType);
                          proc.registerOutParameter(6, OracleTypes.VARCHAR);
                          proc.registerOutParameter(7, OracleTypes.VARCHAR);



                          proc.executeUpdate();

                          res= proc.getString(7);//success/failure
                          accountDet.setMessage(res);
                         // message = proc.getString(6);//error message from dds procedure
                          if(res.equalsIgnoreCase("SUCCESS"))
                          {
                              accountDet.setTxnRefNo(message.split("#")[1]);
                          }
                          else
                          {
                               System.out.println("Unsuccesfull  CashDeposit/WithdrawOperation for user Id :" + tellerId);
                          }

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

        return message;
    }
 public void  getreciptDetailsFromDB(MicroFileAccountDetBean accountDet)
     {

       Connection con = null;
       StringBuilder accountGetQuery = null;
       ResultSet rs = null;
       PreparedStatement ps= null;

       try{
            con = DbHandler.getDBConnection();
            accountGetQuery = new StringBuilder()
//                    .append(" select (select substr( nvl(r.first_name,' ')||' '||nvl(r.middle_name,' ')||' '||nvl(r.last_name,' '),0,20) from kyc_hdr r where r.cif_no=d.customer_no) cust_name,")
//                    .append("(select substr(nvl(r.address_1,'NA') ,0,16) from kyc_hdr r where r.cif_no=d.customer_no) addrs1,")
//                    .append("(select substr(nvl(r.address_2,'NA') ,0,16) from kyc_hdr r where r.cif_no=d.customer_no) addrs2,")
//                    .append("(select substr(nvl(r.address_3,'NA') ,0,16) from kyc_hdr r where r.cif_no=d.customer_no) addrs3,")//.append(pacsId)
//                    .append("to_char(d.acct_open_dt,'dd-Mon-YYYY') acct_opn_date, ")
//                    .append (" to_char(d.mat_dt,'dd-Mon-YYYY') mat_date, ")
//                    .append("d.avail_bal from dep_account d where d.key_1=?");
//
                    .append(" select (select substr(nvl(r.first_name, ' ') || ' ' || nvl(r.middle_name, ' ') || ' ' || nvl(r.last_name, ' '),0,20) from kyc_hdr r")
                    .append(" where r.cif_no = d.customer_no) cust_name,(select substr(nvl(r.address_1, 'NA'), 0, 16) from kyc_hdr r where r.cif_no = d.customer_no) addrs1,")
                    .append("(select substr(nvl(r.address_2, 'NA'), 0, 16) from kyc_hdr r  where r.cif_no = d.customer_no) addrs2,(select substr(nvl(r.address_3, 'NA'), 0, 16) ")
                    .append(" from kyc_hdr r where r.cif_no = d.customer_no) addrs3,to_char(d.acct_open_dt, 'dd-Mon-YYYY') acct_opn_date, to_char(d.mat_dt, 'dd-Mon-YYYY') mat_date,")//.append(pacsId)
                    .append(" d.avail_bal from dep_account d where d.key_1 = ?  UNION ALL select (select substr(nvl(r.first_name, ' ') || ' ' || nvl(r.middle_name, ' ') || ' ' ||nvl(r.last_name, ' '), ")
                    .append(" 0,20) from kyc_hdr r where r.cif_no = l.customer_no) cust_name,(select substr(nvl(r.address_1, 'NA'), 0, 16) from kyc_hdr r  where r.cif_no = l.customer_no) addrs1,")
                    .append(" (select substr(nvl(r.address_2, 'NA'), 0, 16) from kyc_hdr r  where r.cif_no = l.customer_no) addrs2,(select substr(nvl(r.address_3, 'NA'), 0, 16)  from kyc_hdr r")
                    .append(" where r.cif_no = l.customer_no) addrs3, to_char(l.acct_open_dt, 'dd-Mon-YYYY') acct_opn_date, 'NA' mat_date,l.prin_outst from loan_account l  where l.key_1 = ?");



           ps = con.prepareStatement(accountGetQuery.toString());
           ps.setString(1,accountDet.getAccountCode() );
           ps.setString(2,accountDet.getAccountCode() );
           rs= ps.executeQuery();
           while(rs.next())
           {
                  accountDet.setAccountName(rs.getString("cust_name")==null ?"N/A":rs.getString("cust_name"));
                  accountDet.setAdd1(rs.getString("addrs1")==null ?"N/A":rs.getString("addrs1"));
                  accountDet.setAdd2(rs.getString("addrs2")==null?"N/A":rs.getString("addrs2"));
                  accountDet.setAdd3(rs.getString("addrs3")==null?"N/A":rs.getString("addrs3"));
                  accountDet.setAccntOpenDate(rs.getString("acct_opn_date").equals("")?"N/A":rs.getString("acct_opn_date"));
                  accountDet.setAccntMaturityDate(rs.getString("mat_date").equals("")?"N/A":rs.getString("mat_date"));

           }
       }
       catch(Exception e){
           // e.printStackTrace();
           // System.out.println("Exception occured fetching dept details for account in getreciptDetails :"+ accountDet.getAccountCode());
           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.");
           }
       }
       
     }


 public ArrayList<MicroFileAccountDetBean>  getTransactionForAgent(String tellerId, String pacsId ,MicroFileAccountDetBean accountDet , String from_date , String to_date)
     {

       Connection con = null;
       StringBuilder accountGetQuery = null;
       ResultSet rs = null;
       PreparedStatement ps= null;
       ArrayList<MicroFileAccountDetBean> accountDetlist =  new ArrayList<MicroFileAccountDetBean>();
       MicroFileAccountDetBean accountDetails = null;

       try{
            con = DbHandler.getDBConnection();
            accountGetQuery = new StringBuilder()
//                    .append(" select to_char(rownum) sl_no,n.acct_no acct,(select substr((r.first_name||' '||r.middle_name||' '||r.last_name),0,20) ")
//                    .append(" from kyc_hdr r where r.cif_no=(select d.customer_no from dep_account d where d.key_1=n.acct_no)) cust_name, ")
//                    .append(" (case n.tran_ind when 'CR' then 'Deposit' else 'Withdrawl' end ) typ,abs(n.txn_amt) txnAmt ")
//                    .append(" from dep_txn n where n.acct_no in (select d.key_1 from dep_account d where d.pacs_id=?) ")
//                    .append(" and upper(n.narration) like '%POS MACHINE CASH%' and get_user_role(n.checker_id)='201603000004020'and n.checker_id=? ")
//                    .append(" and n.tran_date =(select system_date from system_date)union all select ' ' sl_no,'Total' acct,('    :') cust_name, ")
//                    .append(" ' ' typ,sum(n.txn_amt)from dep_txn n where n.acct_no in (select d.key_1 from dep_account d where d.pacs_id=?) ")
//                    .append(" and upper(n.narration) like '%POS MACHINE CASH%' and get_user_role(n.checker_id)='201603000004020' ")
//                    .append(" and n.checker_id= ? and n.tran_date =(select system_date from system_date) ");
//
                    .append("  select to_char(rownum) sl_no,  nvl(n.acct_no, 'NA') acct, (select substr((r.first_name || ' ' || r.middle_name || ' ' || r.last_name), 0,20)")
                    .append("  from kyc_hdr r where r.cif_no = (select d.customer_no from dep_account d where d.key_1 = n.acct_no)) cust_name, (case n.tran_ind when 'CR' then ")
                    .append("  'cr' else 'dr'  end) typ, abs(n.txn_amt) txnAmt from dep_txn n where '0'||substr(n.checker_id,0,4)= ? and upper(n.narration) like '%POS MACHINE CASH%' and get_user_role(n.checker_id) = '201603000004020'")
                    .append("  and n.checker_id = ? and  n.tran_date >= to_date(?, 'dd/mm/yyyy') and n.tran_date <= to_date(?, 'dd/mm/yyyy') union all select to_char(rownum) sl_no,nvl(n.acct_no, 'NA') acct,")
                    .append("  (select substr((r.first_name || ' ' || r.middle_name || ' ' || r.last_name), 0, 20) from kyc_hdr r where r.cif_no = (select d.customer_no from dep_account d")
                    .append("  where d.key_1 = n.acct_no)) cust_name,  (case n.tran_ind when 'CR' then 'cr'  else 'dr' end) typ,  abs(n.txn_amt) txnAmt from loan_txn n where '0'||substr(n.checker_id,0,4)= ? ")
                    .append("  and upper(n.narration) like '%FROM POS MACHINE%' and get_user_role(n.checker_id) = '201603000004020' and trim(n.checker_id) = ? and n.tran_date>= to_date(?,'dd/mm/yyyy') and n.tran_date<= to_date(?,'dd/mm/yyyy') ");


           ps = con.prepareStatement(accountGetQuery.toString());
           ps.setString(1,pacsId );
           ps.setString(2,tellerId );
           ps.setString(3,from_date );
           ps.setString(4,to_date );
           ps.setString(5,pacsId );
           ps.setString(6,tellerId );
           ps.setString(7,from_date );
           ps.setString(8,to_date );
           rs= ps.executeQuery();
           while(rs.next())
           {
                  accountDetails = new MicroFileAccountDetBean();
                  accountDetails.setSlNo(rs.getString("SL_NO"));
                  accountDetails.setAccountCode(rs.getString("ACCT"));
                  accountDetails.setAvailBal(rs.getDouble("TXNAMT"));
                  accountDetails.setProdType(rs.getString("TYP"));
                  accountDetlist.add(accountDetails);

           }
       }
       catch(Exception e){
           // e.printStackTrace();
           // System.out.println("Exception occured fetching dept details for account in getTotalTransactionForAgent fro agent :"+ tellerId);
           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 accountDetlist;
     }

  public void getTotalTransactionForAgent(String tellerId, String pacsId ,MicroFileAccountDetBean accountDet , String from_date , String to_date)
     {

       Connection con = null;
       StringBuilder accountGetQuery = null;
       ResultSet rs = null;
       PreparedStatement ps= null;

       try{
            con = DbHandler.getDBConnection();
            accountGetQuery = new StringBuilder()
//                    .append(" select (select to_char(system_date,'dd-Mon-YYYY') from system_date) dt,(select sum(n.txn_amt) ")
//                    .append(" from dep_txn n where n.acct_no in (select d.key_1 from dep_account d where d.pacs_id= ? )")
//                    .append(" and upper(n.narration) like '%POS MACHINE CASH%' and get_user_role(n.checker_id)='201603000004020'")
//                    .append(" and n.checker_id= ? and n.tran_date =(select system_date from system_date) and n.tran_ind='CR') cr_total,(select ")
//                    .append(" sum(n.txn_amt) from dep_txn n where n.acct_no in (select d.key_1 from dep_account d where d.pacs_id=?) and upper(n.narration) like '%POS MACHINE CASH%'")
//                    .append(" and get_user_role(n.checker_id)='201603000004020' and n.checker_id=? and n.tran_date =(select system_date from system_date)")
//                    .append(" and n.tran_ind='DR') dr_total from dual");
            
                    .append("  select t.dt,sum(t.cr_total) cr_total,sum(t.dr_total) dr_total from ( select (select to_char(system_date, 'dd-Mon-YYYY') from system_date) dt,")
                    .append("  nvl((select sum(n.txn_amt) from dep_txn n where '0'||substr(n.checker_id,0,4)= ? and upper(n.narration) like '%POS MACHINE CASH%' and get_user_role(n.checker_id) = '201603000004020'")
                    .append("  and n.checker_id = ? and n.tran_date>= to_date(?,'dd/mm/yyyy') and n.tran_date<= to_date(?,'dd/mm/yyyy') and n.tran_ind = 'CR'),0) cr_total, nvl((select sum(n.txn_amt) from dep_txn n")
                    .append("  where  '0'||substr(n.checker_id,0,4)= ? and upper(n.narration) like '%POS MACHINE CASH%' and get_user_role(n.checker_id) = '201603000004020' and n.checker_id = ? and n.tran_date >= to_date(?, 'dd/mm/yyyy') and n.tran_date <= to_date(?, 'dd/mm/yyyy')")
                    .append("  and n.tran_ind = 'DR'),0) dr_total from dual  union all select (select to_char(system_date, 'dd-Mon-YYYY') from system_date) dt, nvl((select sum(n.txn_amt)  from loan_txn n  where '0'||substr(n.checker_id,0,4)=?")
                    .append("  and upper(n.narration) like '%FROM POS MACHINE%' and get_user_role(n.checker_id) = '201603000004020' and n.checker_id = ? and n.tran_date>= to_date(?,'dd/mm/yyyy') and n.tran_date<= to_date(?,'dd/mm/yyyy')  and n.tran_ind = 'CR'),0) cr_total,")
                    .append("  nvl((select sum(n.txn_amt) from loan_txn n where  '0'||substr(n.checker_id,0,4)= ? and upper(n.narration) like '%FROM POS MACHINE%' and get_user_role(n.checker_id) = '201603000004020' and n.checker_id = ?")
                    .append("  and n.tran_date>= to_date(?,'dd/mm/yyyy') and n.tran_date<= to_date(?,'dd/mm/yyyy')  and n.tran_ind = 'DR'),0) dr_total from dual)t group by t.dt");


           ps = con.prepareStatement(accountGetQuery.toString());
           ps.setString(1,pacsId);
           ps.setString(2,tellerId );
           ps.setString(3,from_date );
           ps.setString(4,to_date );
           ps.setString(5,pacsId );
           ps.setString(6,tellerId );
           ps.setString(7,from_date );
           ps.setString(8,to_date );
           ps.setString(9,pacsId);
           ps.setString(10,tellerId );
           ps.setString(11,from_date );
           ps.setString(12,to_date );
           ps.setString(13,pacsId );
           ps.setString(14,tellerId );
           ps.setString(15,from_date );
           ps.setString(16,to_date );

           rs= ps.executeQuery();
           while(rs.next())
           {
                accountDet.setAccntMaturityDate(rs.getString("dt"));
                accountDet.setCrTotal(rs.getDouble("cr_total"));
                accountDet.setDrTotal(rs.getDouble("dr_total"));
                accountDet.setMessage("SUCCESS");

           }
       }
       catch(Exception e){
           // e.printStackTrace();
           // System.out.println("Exception occured fetching dept details for account in getTotalTransactionForAgent for agent :"+ tellerId);
           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.");
           }
       }

     }

    public String TotalCashDepositOperation(String tellerId, String pacsId, String amnt, String agentId, HttpServletRequest request) {
        Connection con = null;
        CallableStatement proc = null;
        String res = null;
        //String twothouIN = (String) request.getParameter("twothouIN") == "" ? "0" : (request.getParameter("twothouIN"));
        //String twothouOUT = request.getParameter("twothouOUT") == "" ? "0" : (request.getParameter("twothouOUT"));
        //String fivehundredIN = request.getParameter("fivehundredIN") == "" ? "0" : (request.getParameter("fivehundredIN"));
        //String fivehundredOUT = request.getParameter("fivehundredOUT") == "" ? "0" : (request.getParameter("fivehundredOUT"));
        //String hundredIN = request.getParameter("hundredIN") == "" ? "0" : (request.getParameter("hundredIN"));
        //String hundredOUT = request.getParameter("hundredOUT") == "" ? "0" : (request.getParameter("hundredOUT"));
        //String fiftyIN = request.getParameter("fiftyIN") == "" ? "0" : (request.getParameter("fiftyIN"));
        //String fiftyOUT = request.getParameter("fiftyOUT") == "" ? "0" : (request.getParameter("fiftyOUT"));
        //String twentyIN = request.getParameter("twentyIN") == "" ? "0" : (request.getParameter("twentyIN"));
        //String twentyOUT = request.getParameter("twentyOUT") == "" ? "0" : (request.getParameter("twentyOUT"));
        //String tenIN = request.getParameter("tenIN") == "" ? "0" : (request.getParameter("tenIN"));
        //String tenOUT = request.getParameter("tenOUT") == "" ? "0" : (request.getParameter("tenOUT"));
        //String fiveIN = request.getParameter("fiveIN") == "" ? "0" : (request.getParameter("fiveIN"));
        //String fiveOUT = request.getParameter("fiveOUT") == "" ? "0" : (request.getParameter("fiveOUT"));
        //String twoIN = request.getParameter("twoIN") == "" ? "0" : (request.getParameter("twoIN"));
        //String twoOUT = request.getParameter("twoOUT") == "" ? "0" : (request.getParameter("twoOUT"));
        //String oneIN = request.getParameter("oneIN") == "" ? "0" : (request.getParameter("oneIN"));
        //String oneOUT = request.getParameter("oneOUT") == "" ? "0" : (request.getParameter("oneOUT"));
        //String fiftyPaisaIN = request.getParameter("fiftyPaisaIN") == "" ? "0" : (request.getParameter("fiftyPaisaIN"));
        //String fiftyPaisaOUT = request.getParameter("fiftyPaisaOUT") == "" ? "0" : (request.getParameter("fiftyPaisaOUT"));
        //String onePaisaIN = request.getParameter("onePaisaIN") == "" ? "0" : (request.getParameter("onePaisaIN"));
        //String onePaisaOUT = request.getParameter("onePaisaOUT") == "" ? "0" : (request.getParameter("onePaisaOUT"));
        //String twohundredIN = request.getParameter("twohundredIN") == "" ? "0" : (request.getParameter("twohundredIN"));
        //String twohundredOUT = request.getParameter("twohundredOUT") == "" ? "0" : (request.getParameter("twohundredOUT"));


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

            proc.setString(1, tellerId);
            proc.setString(2, pacsId);
            proc.setString(3, agentId);
            proc.setString(4, amnt);
            proc.setString(5, twothouIN);
            proc.setString(6, fivehundredIN);
            proc.setString(7, hundredIN);
            proc.setString(8, fiftyIN);
            proc.setString(9, twentyIN);
            proc.setString(10, tenIN);
            proc.setString(11, fiveIN);
            proc.setString(12, twoIN);
            proc.setString(13, oneIN);
            proc.setString(14, fiftyPaisaIN);
            proc.setString(15, onePaisaIN);
            proc.setString(16, twothouOUT);
            proc.setString(17, fivehundredOUT);
            proc.setString(18, hundredOUT);
            proc.setString(19, fiftyOUT);
            proc.setString(20, twentyOUT);
            proc.setString(21, tenOUT);
            proc.setString(22, fiveOUT);
            proc.setString(23, twoOUT);
            proc.setString(24, oneOUT);
            proc.setString(25, fiftyPaisaOUT);
            proc.setString(26, onePaisaOUT);
            proc.registerOutParameter(27, OracleTypes.VARCHAR);
            proc.setString(28, twohundredIN);
            proc.setString(29, twohundredOUT);

            proc.executeUpdate();

           // res = proc.getString(27);//success/failure

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

}
