// author -- Jiten - 02/05/07
package ibase.webitm.ejb.fin;

import java.sql.*;
import java.util.HashMap;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.ITMException;

//nvo_business_object_fin_inv_acct
public class FinCommonInvAcct {
    
    //Finction migrated for nvo_business_object_fin_inv_acct - gbf_rbf_ost()
    public String rbfpost(String tranId,String tranSer,Connection conn) throws ITMException{
        Statement stmt = null,stmt1 = null;        
        ResultSet rs = null,rs1 = null;
        String errString = "",invOnLine = "";
        String sql = "",itemSer = "",tempAcctcodeCr = "";
        java.sql.Date rcpTranDate = null;
        String siteCodeHdr = "",finEntityHdr = "",currCodeHdr = "";
        Timestamp tranDateHdr = null;
        double exchRateHdr = 1;
        String rcpItemCode = "",rcpLocCode = "",rcpLotNo = "",rcpLotSl = "";
        double rcpAmount = 0,rcpInvValue = 0,rcpGrossInvValue = 0;
        String issItemCode = "",issLocCode = "",issLotNo = "",issLotSl = "";
        double issAmount = 0,issGrossAmount = 0;
        String acctCodeCr = "",acctCodeDr = "",cctrCodeCr = "",cctrCodeDr = "";
        double convGl = 0;
        String acctCodeConvGl = "",cctrCodeConvGl = "";
        FinCommon finCommon = null;
        ITMDBAccessEJB  itmDBAccessEJB = null;
        HashMap glTrace = null;
        try{
            System.out.println("///////////////////Inside Rbf Post/////////////////////");
            stmt = conn.createStatement();
            stmt1 = conn.createStatement();
            glTrace = new HashMap();
            itmDBAccessEJB = new ITMDBAccessEJB();
            finCommon = new FinCommon();
            invOnLine = finCommon.getFinparams("999999","INV_ACCT_RB",conn);
            if(invOnLine == null || invOnLine.equalsIgnoreCase("NULLFOUND")){
                errString = itmDBAccessEJB.getErrorString("","VTFINPARM","","",conn);
                return errString;
            }
            System.out.println("invOnLine :: "+invOnLine);
            invOnLine = invOnLine.substring(0,1);
            if("Y_N".indexOf(invOnLine) == -1){
                errString = itmDBAccessEJB.getErrorString("","VTFINPARM1","","",conn);
                return errString;
            }
            if(!invOnLine.equalsIgnoreCase("Y")){
                return "";
            }
            sql = "select tran_date,site_code,acct_code__conv_gl,cctr_code__conv_gl from rcp_backflush where tran_id = '"+tranId+"'";
            System.out.println("SQL :"+sql);
            rs1 = stmt1.executeQuery(sql);
            if(rs1.next()){
                tranDateHdr = rs1.getTimestamp("tran_date");
                siteCodeHdr = rs1.getString("site_code");
                acctCodeConvGl = rs1.getString("acct_code__conv_gl");
                cctrCodeConvGl = rs1.getString("cctr_code__conv_gl");
            }
            rs1.close();
            
            sql = "select fin_entity from site where site_code = '"+siteCodeHdr+"'";
            System.out.println("SQL :"+sql);
            rs1 = stmt1.executeQuery(sql);
            if(rs1.next()){
                finEntityHdr = rs1.getString("fin_entity");
            }
            rs1.close();
            
            sql = "select curr_code from finent where fin_entity = '"+finEntityHdr+"'";
            System.out.println("SQL :"+sql);
            rs1 = stmt1.executeQuery(sql);
            if(rs1.next()){
                currCodeHdr = rs1.getString("curr_code");
            }
            rs1.close();
            
            
            sql = "select item_code, loc_code, lot_no, lot_sl,amount " +
                    "from rcp_backflush_iss where tran_id = '"+tranId+"'";
            System.out.println("SQL :"+sql);
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                issItemCode = rs.getString("item_code");
                issLocCode = rs.getString("loc_code");
                issLotNo = rs.getString("lot_no");
                issLotSl = rs.getString("lot_sl");
                issAmount = rs.getDouble("amount");
                
                if(issAmount > 0){
                    sql = "select acct_code__inv, cctr_code__inv from stock " +
                            "where item_code = '"+issItemCode+"' and " +
                            "site_code = '"+siteCodeHdr+"' and " +
                            "loc_code = '"+issLocCode+"' and " +
                            "lot_no = '"+issLotNo+"' and " +
                            "lot_sl = '"+issLotSl+"' ";
                    System.out.println("SQL :"+sql);
                    rs1 = stmt1.executeQuery(sql);
                    if(rs1.next()){
                        acctCodeCr = rs1.getString("acct_code__inv");
                        cctrCodeCr = rs1.getString("cctr_code__inv");
                    }else{
                        errString = itmDBAccessEJB.getErrorString("","VTACCTINV","","",conn);
                        return errString;
                    }
                    rs1.close();                    
                }else{
                    sql = "select item_ser from item where item_code = '"+issItemCode+"'";
                    System.out.println("SQL :"+sql);
                    rs1 = stmt1.executeQuery(sql);
                    if(rs1.next()){
                        itemSer = rs1.getString("item_ser");
                    }
                    rs1.close();
                    tempAcctcodeCr = finCommon.getFromAcctDetr(issItemCode,itemSer,"IN",conn);
                    acctCodeCr = tempAcctcodeCr.substring(0,tempAcctcodeCr.indexOf(","));
                    cctrCodeCr = tempAcctcodeCr.substring(tempAcctcodeCr.indexOf(",")+1);
                }
                glTrace.put("tran_date",tranDateHdr);
                glTrace.put("eff_date",tranDateHdr);
                glTrace.put("fin_entity",finEntityHdr);
                glTrace.put("site_code",siteCodeHdr);
                glTrace.put("sundry_type","O");
                glTrace.put("sundry_code","");
                glTrace.put("acct_code",acctCodeCr);
                glTrace.put("cctr_code",cctrCodeCr);
                glTrace.put("emp_code","");
                glTrace.put("anal_code","");
                glTrace.put("curr_code",currCodeHdr);
                glTrace.put("exch_rate",Double.toString(exchRateHdr));
                if(issAmount > 0){
                    glTrace.put("dr_amt","0");
                    glTrace.put("cr_amt",Double.toString(issAmount));
                }else{
                    glTrace.put("dr_amt",Double.toString( 0 - issAmount));
                    glTrace.put("cr_amt","0");
                }
                glTrace.put("ref_type","I");
                glTrace.put("ref_ser",tranSer);
                glTrace.put("ref_id",tranId);
                glTrace.put("remarks","");
                
                errString = finCommon.glTraceUpdate(glTrace,conn);
                if(errString != null && errString.trim().length() > 0){
                    return errString;
                }
                issGrossAmount = issGrossAmount + issAmount;
                glTrace.clear();
            }
            rs.close();
            //amount field should be same as inv_value in rcp_backflush_rcpt
            sql = "select item_code,loc_code,lot_no,lot_sl,amount,inv_value from rcp_backflush_rcpt " +
                    "where tran_id = '"+tranId+"'";
            System.out.println("SQL :"+sql);
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                rcpItemCode = rs.getString("item_code");
                rcpLocCode = rs.getString("loc_code");
                rcpLotNo = rs.getString("lot_no");
                rcpLotSl = rs.getString("lot_sl");
                rcpAmount = rs.getDouble("amount");
                rcpInvValue = rs.getDouble("inv_value");
                
                if(rcpAmount >= 0){
                    tempAcctcodeCr = finCommon.getFromAcctDetr(rcpItemCode,itemSer,"IN",conn);
                    acctCodeDr = tempAcctcodeCr.substring(0,tempAcctcodeCr.indexOf(","));
                    cctrCodeDr = tempAcctcodeCr.substring(tempAcctcodeCr.indexOf(",")+1);                    
                }else{
                    sql = "select acct_code__inv, cctr_code__inv from stock " +
                            "where item_code = '"+rcpItemCode+"' " +
                            "and site_code = '"+siteCodeHdr+"' " +
                            "and loc_code = '"+rcpLocCode+"' " +
                            "and lot_no = '"+rcpLotNo+"' " +
                            "and lot_sl = '"+rcpLotSl+"'";
                    System.out.println("SQL :"+sql);
                    rs1 = stmt1.executeQuery(sql);
                    if(rs1.next()){
                        acctCodeDr = rs1.getString("acct_code__inv");
                        cctrCodeDr = rs1.getString("cctr_code__inv");
                    }
                    rs1.close();
                }
                if(acctCodeDr == null || acctCodeDr.trim().length() == 0){
                    errString = itmDBAccessEJB.getErrorString("","VTACCTIN","","",conn);
                    return errString;
                }
                
                glTrace.put("tran_date",tranDateHdr);
                glTrace.put("eff_date",tranDateHdr);
                glTrace.put("fin_entity",finEntityHdr);
                glTrace.put("site_code",siteCodeHdr);
                glTrace.put("sundry_type","O");
                glTrace.put("sundry_code","");
                glTrace.put("acct_code",acctCodeDr);
                glTrace.put("cctr_code",cctrCodeDr);
                glTrace.put("emp_code","");
                glTrace.put("anal_code","");
                glTrace.put("curr_code",currCodeHdr);
                glTrace.put("exch_rate",Double.toString(exchRateHdr));
                if(rcpInvValue > 0){
                    glTrace.put("dr_amt",Double.toString(rcpInvValue));
                    glTrace.put("cr_amt","0");
                }else{
                    glTrace.put("dr_amt","0");
                    glTrace.put("cr_amt",Double.toString(0 - rcpInvValue));
                }
                glTrace.put("ref_type","I");
                glTrace.put("ref_ser",tranSer);
                glTrace.put("ref_id",tranId);
                glTrace.put("remarks","");
                
                errString = finCommon.glTraceUpdate(glTrace,conn);
                if(errString != null && errString.trim().length() > 0){
                    return errString;
                }
                rcpGrossInvValue = rcpGrossInvValue + rcpInvValue;
                //glTrace.clear();
            }
            convGl = issGrossAmount - rcpGrossInvValue;
            System.out.println("ConvGl (issGrossAmount - rcpGrossInvValue): "+convGl);
            if(convGl != 0){
                if(acctCodeConvGl == null || acctCodeConvGl.trim().length() == 0){
                    errString = itmDBAccessEJB.getErrorString("","VTACCTIN","","",conn);
                    return errString;
                }
                if(cctrCodeConvGl == null || cctrCodeConvGl.trim().length() == 0){
                    cctrCodeConvGl = " ";
                }
                glTrace.put("acct_code",acctCodeConvGl);
                glTrace.put("cctr_code",cctrCodeConvGl);
                if(convGl > 0){
                    glTrace.put("dr_amt",Double.toString(convGl));
                    glTrace.put("cr_amt","0");
                }else{
                    glTrace.put("dr_amt","0");
                    glTrace.put("cr_amt",Double.toString(0 - convGl));
                }
                errString = finCommon.glTraceUpdate(glTrace,conn);
                if(errString != null && errString.trim().length() > 0){
                    return errString;
                }
            }
            errString = finCommon.checkGlTranDrCr(tranSer,tranId,conn);
        }catch(Exception e){
            System.out.println("Exception e"+e);
            e.printStackTrace();
            throw new ITMException(e);
        }finally{
            try{
                glTrace.clear();
                glTrace = null;
                if(rs != null){
                    rs.close();
                    rs = null;
                }
                if(rs1 != null){
                    rs1.close();
                    rs1 = null;
                }
                if(stmt != null){
                    stmt.close();
                    stmt = null;
                }
                if(stmt1 != null){
                    stmt1.close();
                    stmt1 = null;
                }
            }catch(Exception e){
                System.out.println("Exception in Finally : "+e);
            }
        }
        System.out.println("Returning String From Rbfpost :: "+errString);
        return errString;
    }
}
