package ibase.webitm.ejb.dis;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.HashMap;

import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;


import ibase.scheduler.utility.interfaces.Schedule;
import ibase.system.config.ConnDriver;
import ibase.webitm.utility.ITMException;

public class SalesReturnAutoConf implements Schedule
{

	public String confirmSalesReturn()
	{
		Connection conn= null;
		ResultSet rs = null; 
		PreparedStatement pStmt = null;
		String sql = "";
		String returnStr= "";
		HashMap <String, String> disparamMap = new HashMap<String, String>(); 
		Double minAmount = 10000d;
		Double maxAmount = 50000d;
		String reasonCode = "RM15";
		String tranId = "";
		String businessObj = "salesreturn_retn";
		String user ="";
		String xtraParams = "";
		try
		{
			user = "SYSTEM";
			xtraParams = "loginCode="+user+"~~loginEmpCode=null~~loginSiteCode=null~~entityCode=null~~profileId=null~~userType=null~~runMode=I~~user_lang=en~~user_country=US~~charEnc=ISO-8859-1~~termId=SYSTEM~~chgTerm=SYSTEM~~detCnt=1~~focusRow=1~~REC_CTR=-1~~saveLevel=0";
			ConnDriver connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			
			sql = "SELECT VAR_NAME,VAR_VALUE FROM DISPARM WHERE VAR_NAME IN ('REAS_CODE_RECALL_RMA','SR_MIN_AMT_APRV1','SR_MAX_AMT_APRV1') ";
			pStmt = conn.prepareStatement(sql);
		    rs = pStmt.executeQuery();
		    while (rs.next())
			{
				disparamMap.put(rs.getString("VAR_NAME"),rs.getString("VAR_VALUE"));
			}
			if(rs != null)
			{
				rs.close();
				rs =null;
			}
			if(pStmt != null)
			{
				pStmt.close();
				pStmt = null;
			}
			
			
			reasonCode = disparamMap.get("REAS_CODE_RECALL_RMA") == null ? reasonCode : disparamMap.get("REAS_CODE_RECALL_RMA"); 
			minAmount = disparamMap.get("SR_MIN_AMT_APRV1") == null ? minAmount :Double.parseDouble(disparamMap.get("SR_MIN_AMT_APRV1"));  
			maxAmount = disparamMap.get("SR_MAX_AMT_APRV1") == null ? maxAmount :Double.parseDouble(disparamMap.get("SR_MAX_AMT_APRV1")); 
					
			sql = " SELECT TRAN_ID FROM SRETURN WHERE WF_STATUS ='S' AND NET_AMT <= ? AND (REAS_CODE != ? OR (REAS_CODE = ?  AND PROCESS_FEE > 0)) AND CONFIRMED != 'Y'";
			
			pStmt = conn.prepareStatement(sql);
			pStmt.setDouble(1, minAmount);
			pStmt.setString(2, reasonCode);
			pStmt.setString(3, reasonCode);
			rs = pStmt.executeQuery();
			while (rs.next())
			{
				tranId = rs.getString("TRAN_ID");
				this.confirmReturn(businessObj, tranId, xtraParams, conn);
			
			}
		
			if(rs != null)
			{
				rs.close();
				rs =null;
			}
			if(pStmt != null)
			{
				pStmt.close();
				pStmt = null;
			}
			
		
		}
		catch(Exception ex)
		{
			System.out.println("Exception in SalesReturnAutoConf"+ ex.getMessage());
			ex.printStackTrace();
		}
		finally
		{
			try
			{
				if(rs != null)
				{
					rs.close();
					rs =null;
				}
				if(pStmt != null)
				{
					pStmt.close();
					pStmt = null;
				}				
				if(conn != null)
				{
					conn.close();
					conn =null;
				}
				
			}
			catch(SQLException sqle)
			{
				sqle.printStackTrace();
			}
		}
		return returnStr;
	}

	private String confirmReturn(String businessObj, String tranIdFr,String xtraParams,Connection conn) throws Exception, ITMException
			{
					String methodName = "";
					String compName = "";
					String retString = "";
					String serviceCode = "";
					String serviceURI = "";
					String actionURI = "";
					String sql = "";
					PreparedStatement pstmt = null;
					ResultSet rs = null;
					try
				{
					 methodName = "gbf_post";
					actionURI = "http://NvoServiceurl.org/" + methodName;

					sql = " SELECT SERVICE_CODE,COMP_NAME FROM SYSTEM_EVENTS WHERE OBJ_NAME = ? AND " +
						  " EVENT_CODE = 'pre_confirm' ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,businessObj);
					rs = pstmt.executeQuery();
					if ( rs.next() )
					{
						serviceCode = rs.getString("SERVICE_CODE");
						compName = rs.getString("COMP_NAME");
					}
					
					if (rs != null)
					{
						rs.close();
						rs = null;
					}
					
					if (pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					
					
					sql = "SELECT SERVICE_URI FROM SYSTEM_EVENT_SERVICES WHERE SERVICE_CODE	= ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1,serviceCode);
					rs = pstmt.executeQuery();
					if ( rs.next() )
					{
						serviceURI = rs.getString("SERVICE_URI");
					}
					
					if (rs != null)
					{
						rs.close();
						rs = null;
					}
					if (pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					
					System.out.println("serviceURI = "+serviceURI+" compName = "+compName);
					Service service = new Service();
					Call call = (Call)service.createCall();
					call.setTargetEndpointAddress(new java.net.URL(serviceURI));
					call.setOperationName( new 	javax.xml.namespace.QName("http://NvoServiceurl.org", methodName ) );
					call.setUseSOAPAction(true);
					call.setSOAPActionURI(actionURI);
					Object[] aobj = new Object[4];

					call.addParameter( new 	javax.xml.namespace.QName("http://NvoServiceurl.org","component_name"), XMLType.XSD_STRING, ParameterMode.IN);
					call.addParameter( new 	javax.xml.namespace.QName("http://NvoServiceurl.org", "tran_id"),XMLType.XSD_STRING, ParameterMode.IN);
					call.addParameter( new 	javax.xml.namespace.QName("http://NvoServiceurl.org", "xtra_params"),XMLType.XSD_STRING, ParameterMode.IN);
					call.addParameter( new 	javax.xml.namespace.QName("http://NvoServiceurl.org", "forced_flag"),XMLType.XSD_STRING, ParameterMode.IN);

					aobj[0] = new String(compName);
					aobj[1] = new String(tranIdFr);
					aobj[2] = new String(xtraParams);
					aobj[3] = new String("Y");
				    call.setReturnType(XMLType.XSD_STRING);
					retString = (String)call.invoke(aobj);
					

				}
				catch (Exception e)
				{
					conn.rollback();
					e.printStackTrace();
					throw new ITMException(e);
				}
					finally
					{
						try
						{
							if(rs != null)
							{
								rs.close();
								rs =null;
							}
							if(pstmt != null)
							{
								pstmt.close();
								pstmt = null;
							}				
							
							
						}
						catch(SQLException sqle)
						{
							sqle.printStackTrace();
						}
					}
				
				return retString;
			}//end of confirmReceipt

	
	@Override
	public String schedule(HashMap arg0) throws Exception {
		// TODO Auto-generated method stub
		confirmSalesReturn();
		return null;
	}

	@Override
	public String schedule(String arg0) throws Exception {
		// TODO Auto-generated method stub
		confirmSalesReturn();
		return null;
	}

	@Override
	public String schedulePriority(String arg0) throws Exception {
		// TODO Auto-generated method stub
		confirmSalesReturn();
		return null;
	}
}
