package ibase.webitm.ejb.sys;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.w3c.dom.*;
import com.google.gwt.dom.client.Document;

import ibase.utility.BaseLogger;
import ibase.utility.E12GenericUtility;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.ITMException;

public class SubscriptionRenewalIC extends ValidatorEJB {
 E12GenericUtility genereicutility = new E12GenericUtility();
	
 public String itemChanged(String xmlString, String xmlString1, String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException 
	{
	 
		BaseLogger.log("3", null, null, " inside itemchange domparsing method");

		org.w3c.dom.Document dom = null;
		org.w3c.dom.Document dom1 = null;
		org.w3c.dom.Document dom2 = null;
		String errString = null;
		String valueXmlString = "";
		try
		{
			if (xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genereicutility.parseString(xmlString);
			}
			if (xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = genereicutility.parseString(xmlString1);
			}
			if (xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = genereicutility.parseString(xmlString2);
			}
			
			valueXmlString =  itemChanged(dom, dom1, dom2, objContext, currentColumn, editFlag, xtraParams);
			BaseLogger.log("3", null, null, " ErrString :" + valueXmlString);

		}
		catch (Exception e)
		{
			BaseLogger.log("3", null, null, " Exception :SfaTime :itemChanged(String,String):" + e.getMessage() );

			valueXmlString = genereicutility.createErrorString(e);
		}
		
		BaseLogger.log("3", null, null, "returning from SfaTime itemChanged" );
		return valueXmlString;
	}
 
 public String itemChanged(org.w3c.dom.Document dom, org.w3c.dom.Document dom1, org.w3c.dom.Document dom2, String objContext, String currentColumn,
			String editFlag, String xtraParams) throws RemoteException
	{
		
	 	BaseLogger.log("3", null, null, " Inside itemchange");
		Connection con = null;
		SimpleDateFormat sdf,simpledateformat; 
		int currentFormNo = 0, chrg_amtValues =0;
		String tranId = "" ,newDateString="",user_lic_type ="", enterprise="",getValidUptoString="", valid_upto = "",valid_upto_date= "", sql="",sql3="",sql4="",renewPeriodLowerCase = "",enterprisesql="",validuptostring="", validupto = "", sql2= "", renewPeriod = "",enterpriseLowerCse = "",validuptoDateString="",demoDate = "11-AUG-23";
		List<String> enterprisesqllist = new ArrayList<>();
		List<String> userLicenseTypelist = new ArrayList<>();
		PreparedStatement pstmt = null,pstmt2=null,pstmt3=null,pstmt4=null;
		int chrg_amt, chrg_amtTotal=0,users_count=0;
		ResultSet rs = null, rs2=null,rs3=null,rs4=null ;
		Date validuptoDate = null;
		
		StringBuffer valueXmlString = new StringBuffer();

		try {
			Calendar currentDate = Calendar.getInstance();
			simpledateformat = new SimpleDateFormat(genereicutility.getApplDateFormat());
			String sysDate = simpledateformat.format(currentDate.getTime());	
			Date datee = Calendar.getInstance().getTime() ;
			Date date = new Date();
			con = getConnection();	
			String currentdate = simpledateformat.format(date);
		 	List<Integer> chrg_amountlist = new ArrayList<>();
		 	Date currentDatee = new Date();
			String currntDate = simpledateformat.format(currentDatee);
		 	
		 	
			if (objContext != null && objContext.trim().length() > 0) {
				
				currentFormNo = Integer.parseInt(objContext);
			}
			
			valueXmlString = new StringBuffer("<?xml version=\"1.0\"?><Root><header><editFlag>");
			valueXmlString.append(editFlag).append("</editFlag></header>");
			
			switch(currentFormNo)
			{
			case 1:
				
				valueXmlString.append("<Detail1>");
				if ( currentColumn.trim().equalsIgnoreCase( "itm_default" )) {
			 	
					BaseLogger.log("3", null, null, "inside itm_default");

					tranId = checkNull(genereicutility.getColumnValue("tran_id", dom));

					
				valueXmlString.append("<tran_date><![CDATA[").append(sysDate).append("]]></tran_date>\r\n");
				valueXmlString.append("<tran_id><![CDATA[").append(tranId).append("]]></tran_id>\r\n");

			}

				else if("itm_defaultedit".equalsIgnoreCase(currentColumn.trim()))
				{
					
		
					
				}

			
			else if("enterprise".equalsIgnoreCase(currentColumn.trim()))
			{
				
				
				BaseLogger.log("3", null, null, "  inside enterprise.equalsignorecase(correctcolumn.trim)");

				enterprise = checkNull(genereicutility.getColumnValue("enterprise", dom));
				BaseLogger.log("3", null, null, "  enterprise value"+enterprise);

				
				
				sql = "select enterprise from entp_subs_renew";
				BaseLogger.log("3", null, null, "  sql to get enterprise from entp_subs_renew"+sql);
				pstmt = con.prepareStatement(sql);
				rs = pstmt.executeQuery();
				while(rs.next()) {
					
					enterprisesql =  rs.getString("enterprise") ;
					enterprisesqllist.add(enterprisesql);
					BaseLogger.log("3", null, null, "  enterprisesql"+enterprisesql );

				}
				BaseLogger.log("3", null, null, "  enterprisesqllist outside while "+enterprisesqllist );
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
				
				if(enterprisesqllist.contains(enterprise)) {
					
					BaseLogger.log("3", null, null, "daniel enterprisesql.contains(enterprise)" );

//						sql2 = "select max(valid_upto) from entp_subs_renew where enterprise = '"+enterprise+"' ";
//						BaseLogger.log("3", null, null, "sql for max(validupto) "+sql2 );
//						pstmt2 = con.prepareStatement(sql2);				
//						rs2 = pstmt2.executeQuery();
//							
//							if(rs2.next()) {
//								
//							 validuptoDate =  rs2.getDate("max(valid_upto)");
//							BaseLogger.log("3", null, null, " validupto"+validuptoDate );
//
//							}		
//							rs2.close();
//							rs2 = null;
//							pstmt2.close();
//							pstmt2 = null;
							
					
							validuptoDate = getValidUpto(enterprise);						
							 validuptostring =  simpledateformat.format(validuptoDate);
							//validuptostring =  getValidUpto(enterprise);
							 
							 BaseLogger.log("3", null, null, " validuptostring"+validuptostring );

							renewPeriod = checkNull(genereicutility.getColumnValue("renew_prd",dom));
							BaseLogger.log("3", null, null, " renewperiod"+renewPeriod );
							
							if(renewPeriod. contains("Y")) {
								BaseLogger.log("3", null, null, " inside renewperiod.contains(Y)" );

							int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
							BaseLogger.log("3", null, null, " getMOnthOrYearVAlue returning value "+monthyearvalue );
							
							Date dt =  simpledateformat.parse(validuptostring);
							currentDate.setTime(dt);
							
							currentDate.add(Calendar.YEAR, monthyearvalue);
							
							Date newdate =  currentDate.getTime();
							 newDateString =  simpledateformat.format(newdate);
							BaseLogger.log("3", null, null, " newdatestring "+newDateString );


							BaseLogger.log("3", null, null, " validuptostring inside renewperiod.contains(Y) "+validuptostring );

								
							}
							else if (renewPeriod.contains("M")) {
								BaseLogger.log("3", null, null, " inside renewperiod.contains(M)" );

								int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
								BaseLogger.log("3", null, null, " getMOnthOrYearVAlue returning value "+monthyearvalue );

								
								
								Date dt =  simpledateformat.parse(validuptostring);
								currentDate.setTime(dt);
								BaseLogger.log("3", null, null, " below currendate.settime in else if condition "+newDateString );

								currentDate.add(Calendar.MONTH, monthyearvalue);
								
								Date newdate =  currentDate.getTime();
								 newDateString =  simpledateformat.format(newdate);
								BaseLogger.log("3", null, null, " newdatestring IN ELSE IF  "+newDateString );
							}
							

				}
				
				else {
					
					BaseLogger.log("3", null, null, "inside else condition " );
		        
					BaseLogger.log("3", null, null, " currendate  "+currntDate );
					
					renewPeriod = checkNull(genereicutility.getColumnValue("renew_prd",dom));
					BaseLogger.log("3", null, null, " renewperiod"+renewPeriod );
				
					
					if(renewPeriod. contains("Y")) {
						BaseLogger.log("3", null, null, " inside renewperiod.contains(Y) in else condition" );

						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
						BaseLogger.log("3", null, null, " getMOnthOrYearVAlue returning value "+monthyearvalue );

						
						
						Date dt =  simpledateformat.parse(currntDate);
						currentDate.setTime(dt);
						
						currentDate.add(Calendar.YEAR, monthyearvalue);
						
						Date newdate =  currentDate.getTime();
						 newDateString =  simpledateformat.format(newdate);
						BaseLogger.log("3", null, null, " newdatestring "+newDateString );
						
					}
					
					
					else if(renewPeriod. contains("M")) {
						BaseLogger.log("3", null, null, " inside renewperiod.contains(M) in else condition" );

						
						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
						BaseLogger.log("3", null, null, " getMOnthOrYearVAlue returning value "+monthyearvalue );

						
						
						Date dt =  simpledateformat.parse(currntDate);
						currentDate.setTime(dt);
						BaseLogger.log("3", null, null, " below currendate.settime in else if condition "+newDateString );

						currentDate.add(Calendar.MONTH, monthyearvalue);
						
						Date newdate =  currentDate.getTime();
						 newDateString =  simpledateformat.format(newdate);
						BaseLogger.log("3", null, null, " newdatestring IN ELSE IF  "+newDateString );
						
					}
					
				}

				
				//add method here from 287 to 333
				sql3 = "select * from entp_user_lic_chrgs where enterprise = '"+enterprise+"' ";
				BaseLogger.log("3", null, null, "daniel  sql3"+sql3);
				BaseLogger.log("3", null, null, "daniel  enterprise value from entp_user_lic_chrgs"+enterprise);
				pstmt3 =  con.prepareStatement(sql3);
				rs3 =  pstmt3.executeQuery();
				
				while (rs3.next()) {
					
					user_lic_type =  rs3.getString("user_lic_type");
					userLicenseTypelist.add(user_lic_type);
				}
				rs3.close();		
				rs3 = null;
				pstmt3.close();
				pstmt3 = null;
				

				for (String userlicensevalue : userLicenseTypelist) {
					BaseLogger.log("3", null, null, " daniel items insdie userLicenseTypelist "+userlicensevalue);

					sql4 = "select CHRG_AMT from usertype_lic_chrgs WHERE USER_LIC_TYPE = '"+userlicensevalue+ "' ";
					BaseLogger.log("3", null, null, "daniel  sql4"+sql3);
					pstmt4 = con.prepareStatement(sql4);
					rs4 = pstmt4.executeQuery();
					
					
					while(rs4.next()) {
						
					 	chrg_amtTotal += rs4.getInt("chrg_amt");
						BaseLogger.log("3", null, null, "daniel chrg_amtTotal "+chrg_amtTotal);
						users_count++;
					}
					
				}
				BaseLogger.log("3", null, null, "daniel chrg_amtTotal from users for a enterprise"+chrg_amtTotal);
				BaseLogger.log("3", null, null, "daniel usercount:::  "+users_count);

				
				
				
    
					chrg_amtTotal =  chrg_amtTotal *12;
					//method closes here
					
					
				valueXmlString.append("<valid_upto><![CDATA[").append(newDateString).append("]]></valid_upto>\r\n");;
				valueXmlString.append("<renew_amt><![CDATA[").append(chrg_amtTotal).append("]]></renew_amt>\r\n");;


			}
				
			else if("renew_prd".equalsIgnoreCase(currentColumn.trim()))
			{
				
				BaseLogger.log("3", null, null, "daniel inside renew_prd itmchange code" );

//				enterprise = checkNull(genereicutility.getColumnValue("enterprise",dom));	
//				BaseLogger.log("3", null, null, "daniel enterprise insiderenew_prd .equalsIgnoreCase(currentColumn.trim()"+enterprise );
//
//				getValidUptoString =  getEnterprise(enterprise);
//				BaseLogger.log("3", null, null, "daniel getValidUptoString"+getValidUptoString );
//
//				if(getValidUptoString.trim().length() == 0 || getValidUptoString == null  )
//				{
//					
//					renewPeriod = checkNull(genereicutility.getColumnValue("renew_prd",dom));
//
//					
//					if(renewPeriod. contains("Y")) {
//						
//						
//					//	validuptoDate = getValidUpto(enterprise);
//						valid_upto_date =  simpledateformat.format(currentDatee);
//
//						BaseLogger.log("3", null, null, "daniel inside renewperiod.contains(Y) in renewprd else condition" );
//
//						//chrg_amtTotal = ChargeAmoutValue(enterprise);
//						BaseLogger.log("3", null, null, "daniel chrg_amtotal"+chrg_amtTotal );
//
//						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
//						BaseLogger.log("3", null, null, "daniel getMOnthOrYearVAlue returning value else of renewprd condition "+monthyearvalue );
//
//						Date dt =  simpledateformat.parse(currntDate);
//						BaseLogger.log("3", null, null, "daniel inside renewprd Y currntDate "+currntDate );
//				
//						currentDate.setTime(dt);
//						currentDate.add(Calendar.YEAR, monthyearvalue);
//						Date newdate =  currentDate.getTime();
//						newDateString =  simpledateformat.format(newdate);
//						BaseLogger.log("3", null, null, "daniel newdatestring renew_prd condition"+newDateString );
//						
//						
//					}
//
//					else if(renewPeriod. contains("M")) {
//						BaseLogger.log("3", null, null, "daniel inside renewperiod.contains(M) in renew_prd condition condition" );
//						
//						validuptoDate = getValidUpto(enterprise);
//						valid_upto_date =  simpledateformat.format(currentDatee);
//
//						BaseLogger.log("3", null, null, "daniel chrg_amtotal inside renewPeriod. containsM"+chrg_amtTotal );	
//						
//						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
//						BaseLogger.log("3", null, null, " getMOnthOrYearVAlue returning value renew_prd condition"+monthyearvalue );
//
//						Date dt =  simpledateformat.parse(currntDate);
//						BaseLogger.log("3", null, null, "daniel inside renewprd M currntDate "+currntDate );
//
//						currentDate.setTime(dt);
//						BaseLogger.log("3", null, null, "daniel below currendate.settime in renew_prd condition "+newDateString );
//
//						currentDate.add(Calendar.MONTH, monthyearvalue);
//						
//						Date newdate =  currentDate.getTime();
//						 newDateString =  simpledateformat.format(newdate);
//						BaseLogger.log("3", null, null, "daniel newdatestring IN ELSE IF of renew_prd condition  "+newDateString );
//						
//						
//						
//					}
//					
//					
//				}
	
//				
//				
				
				
				
				
//			if(!enterprisesqllist.contains(enterprise))
//				{ 
//					
				
//					BaseLogger.log("3", null, null, "daniel inside !enterprisesqllist.contains(enterprise)" );
//
//					renewPeriod = checkNull(genereicutility.getColumnValue("renew_prd",dom));
//
//					
//					if(renewPeriod. contains("Y")) {
//						
//						
//						validuptoDate = getValidUpto(enterprise);
//						valid_upto_date =  simpledateformat.format(currentDatee);
//
//						BaseLogger.log("3", null, null, "daniel inside renewperiod.contains(Y) in renewprd else condition" );
//
//						//chrg_amtTotal = ChargeAmoutValue(enterprise);
//						BaseLogger.log("3", null, null, "daniel chrg_amtotal"+chrg_amtTotal );
//
//						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
//						BaseLogger.log("3", null, null, "daniel getMOnthOrYearVAlue returning value else of renewprd condition "+monthyearvalue );
//
//						Date dt =  simpledateformat.parse(currntDate);
//						BaseLogger.log("3", null, null, "daniel inside renewprd Y currntDate "+currntDate );
//				
//						currentDate.setTime(dt);
//						currentDate.add(Calendar.YEAR, monthyearvalue);
//						Date newdate =  currentDate.getTime();
//						newDateString =  simpledateformat.format(newdate);
//						BaseLogger.log("3", null, null, "daniel newdatestring renew_prd condition"+newDateString );
//						
//						
//					}
//
//					else if(renewPeriod. contains("M")) {
//						BaseLogger.log("3", null, null, "daniel inside renewperiod.contains(M) in renew_prd condition condition" );
//						
//						validuptoDate = getValidUpto(enterprise);
//						valid_upto_date =  simpledateformat.format(currentDatee);
//
//						BaseLogger.log("3", null, null, "daniel chrg_amtotal inside renewPeriod. containsM"+chrg_amtTotal );	
//						
//						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
//						BaseLogger.log("3", null, null, " getMOnthOrYearVAlue returning value renew_prd condition"+monthyearvalue );
//
//						Date dt =  simpledateformat.parse(currntDate);
//						BaseLogger.log("3", null, null, "daniel inside renewprd M currntDate "+currntDate );
//
//						currentDate.setTime(dt);
//						BaseLogger.log("3", null, null, "daniel below currendate.settime in renew_prd condition "+newDateString );
//
//						currentDate.add(Calendar.MONTH, monthyearvalue);
//						
//						Date newdate =  currentDate.getTime();
//						 newDateString =  simpledateformat.format(newdate);
//						BaseLogger.log("3", null, null, "daniel newdatestring IN ELSE IF of renew_prd condition  "+newDateString );
//						
//						
//						
//					}
//										
//					valueXmlString.append("<valid_upto><![CDATA[").append(newDateString).append("]]></valid_upto>\r\n");;
//
//					
//				} //till here
				
	
				
				
				
				
				valid_upto = checkNull(genereicutility.getColumnValue("valid_upto",dom));
				BaseLogger.log("3", null, null, "daniel valid_upto inside valid_upto. equalsignorecase inside renew_prd"+valid_upto );

				if(valid_upto.trim().length() == 0)
				{
					
					valueXmlString.append("<valid_upto><![CDATA[").append("").append("]]></valid_upto>\r\n");;

				}
				
	
				else {
					

					renewPeriod = checkNull(genereicutility.getColumnValue("renew_prd",dom));
					BaseLogger.log("3", null, null, "daniel inside else of renew_prd.equalsignorecase  renewPeriod:: "+renewPeriod );
					
					enterprise = checkNull(genereicutility.getColumnValue("enterprise", dom));
					BaseLogger.log("3", null, null, "daniel enterprise value in renew_prd.equalsignorecase:: "+chrg_amtTotal );

					
					if(renewPeriod. contains("Y")) {
						
						
						//validuptoDate = 
						validuptoDate = getValidUpto(enterprise);
						valid_upto_date =  simpledateformat.format(validuptoDate);
						
						
						BaseLogger.log("3", null, null, "daniel inside renewperiod.contains(Y) in renewprd else condition" );

						chrg_amtTotal = ChargeAmoutValue(enterprise);
						BaseLogger.log("3", null, null, "daniel chrg_amtotal"+chrg_amtTotal );

						 if (renewPeriod.equalsIgnoreCase("1Y"))
						{
							
							 
							chrg_amtTotal =  chrg_amtTotal * 12;
							BaseLogger.log("3", null, null, "daniel chrg_amtotal"+chrg_amtTotal );

						}
						
						else if (renewPeriod.equalsIgnoreCase("3Y"))
						{
							
							chrg_amtTotal =  chrg_amtTotal * 36;
						}
						
						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
						BaseLogger.log("3", null, null, "daniel getMOnthOrYearVAlue returning value else of renewprd condition "+monthyearvalue );

						
						Date dt =  simpledateformat.parse(valid_upto_date);
											
						currentDate.setTime(dt);
						currentDate.add(Calendar.YEAR, monthyearvalue);
						Date newdate =  currentDate.getTime();
						newDateString =  simpledateformat.format(newdate);
						BaseLogger.log("3", null, null, "daniel newdatestring renew_prd condition"+newDateString );
						
						
						
					}
					
					else if(renewPeriod. contains("M")) {
						BaseLogger.log("3", null, null, "daniel inside renewperiod.contains(M) in renew_prd condition condition" );
						
						validuptoDate =  getValidUpto(enterprise);
						valid_upto_date =  simpledateformat.format(validuptoDate);
						//valid_upto_date =  getValidUpto(enterprise);
						
						chrg_amtTotal = ChargeAmoutValue(enterprise);
						BaseLogger.log("3", null, null, "daniel chrg_amtotal inside renewPeriod. containsM"+chrg_amtTotal );

						
						if(renewPeriod.equalsIgnoreCase("1M")) {
							
							chrg_amtTotal =  chrg_amtTotal * 1;
							
						}
						
						else if (renewPeriod.equalsIgnoreCase("3M"))
						{
							
							chrg_amtTotal =  chrg_amtTotal * 3;
						}
						
						else if (renewPeriod.equalsIgnoreCase("6M"))
						{
							
							chrg_amtTotal =  chrg_amtTotal * 6;
							BaseLogger.log("3", null, null, "daniel chrg_amtotal inside M"+chrg_amtTotal );

						}
						
						
						int monthyearvalue = getMOnthOrYearVAlue(renewPeriod);
						BaseLogger.log("3", null, null, " getMOnthOrYearVAlue returning value renew_prd condition"+monthyearvalue );

						
							Date dt =  simpledateformat.parse(valid_upto_date);
						currentDate.setTime(dt);
						BaseLogger.log("3", null, null, "daniel below currendate.settime in renew_prd condition "+newDateString );

						currentDate.add(Calendar.MONTH, monthyearvalue);
						
						Date newdate =  currentDate.getTime();
						 newDateString =  simpledateformat.format(newdate);
						BaseLogger.log("3", null, null, "daniel newdatestring IN ELSE IF of renew_prd condition  "+newDateString );
						
						
						
					}

				
				}
				
					
					valueXmlString.append("<renew_amt><![CDATA[").append(chrg_amtTotal).append("]]></renew_amt>\r\n");;
					valueXmlString.append("<valid_upto><![CDATA[").append(newDateString).append("]]></valid_upto>\r\n");;


			}
				
				
				

			valueXmlString.append ("</Detail1>\r\n");
		}
			valueXmlString.append("</Root>\r\n");
	
	}
		
		
		catch(Exception e ) {
		 	BaseLogger.log("3", null, null, "Dan exception e "+e);
			e.printStackTrace();
		}
		
		finally {
			
			try {
				
				if(rs != null) 
				{
					rs.close();
					rs = null;
				}
				
				if(pstmt != null) 
				{
					pstmt.close();
					pstmt = null;

				}
				
				if(con != null)
				{
					
					con.close();
					con = null;
				}
				
				
				
				if(rs2 != null) 
				{
					rs2.close();
					rs2 = null;
				}
				
				if(pstmt2 != null) 
				{
					pstmt2.close();
					pstmt2 = null;

				}
				
				if(rs3 != null) 
				{
					rs3.close();
					rs3 = null;
				}
				
				if(pstmt3 != null) 
				{
					pstmt3.close();
					pstmt3 = null;

				}
				
				if(rs4 != null) 
				{
					rs4.close();
					rs4 = null;
				}
				
				if(pstmt4 != null) 
				{
					pstmt4.close();
					pstmt4 = null;

				}
				
			}
			catch (Exception ex) {	
					
				ex.printStackTrace();
			 	BaseLogger.log("3", null, null, "Dan exception e "+ex);

			}
			
		
			
		}
	 
	 return valueXmlString.toString();
	}
 
 
 public int ChargeAmoutValue (String enterprise) throws RemoteException, ITMException {

	 String sql3 = "",user_lic_type="",sql4 ="";
	 PreparedStatement pstmt3,pstmt4;
	 int chrg_amtTotal=0,users_count=0;
	 ResultSet rs3,rs4;
	 Connection con2 = null ;
	List<String> userLicenseTypelist = new ArrayList<>();
	
	con2 = getConnection();
			 
		sql3 = "select * from entp_user_lic_chrgs where enterprise = '"+enterprise+"' ";
		BaseLogger.log("3", null, null, "daniel  sql3"+sql3);
		BaseLogger.log("3", null, null, "daniel  enterprise value from entp_user_lic_chrgs inside chargeamountvalue method"+enterprise);
		
		try {
			
		pstmt3 =  con2.prepareStatement(sql3);
		rs3 =  pstmt3.executeQuery();
		BaseLogger.log("3", null, null, "daniel below rs3 chargeamount value");

		while (rs3.next()) {
			
			user_lic_type =  rs3.getString("user_lic_type");
			userLicenseTypelist.add(user_lic_type);
		}
		rs3.close();		
		rs3 = null;
		pstmt3.close();
		pstmt3 = null;
		

		for (String userlicensevalue : userLicenseTypelist) {
			BaseLogger.log("3", null, null, " daniel items insdie userLicenseTypelist "+userlicensevalue);

			sql4 = "select CHRG_AMT from usertype_lic_chrgs WHERE USER_LIC_TYPE = '"+userlicensevalue+ "' ";
			BaseLogger.log("3", null, null, "daniel  sql4"+sql3);
			pstmt4 = con2.prepareStatement(sql4);
			rs4 = pstmt4.executeQuery();
			
			
			while(rs4.next()) {
				
			 	chrg_amtTotal += rs4.getInt("chrg_amt");
				BaseLogger.log("3", null, null, "daniel chrg_amtTotal "+chrg_amtTotal);
				users_count++;
			}
			
		}
		BaseLogger.log("3", null, null, "daniel chrg_amtTotal from users for a enterprise"+chrg_amtTotal);
		BaseLogger.log("3", null, null, "daniel usercount:::  "+users_count);
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
		}

	 return chrg_amtTotal;
 }
 
 
 public static int getMOnthOrYearVAlue (String renewPrd)
	{

	 	char renewPrdValue = renewPrd.charAt(0);
		System.out.println("renewPrdValue"+renewPrdValue);
		int value = Integer.parseInt(String.valueOf(renewPrdValue));
		
		
		return value;
	}
 
 public Date getValidUpto(String enterprise) throws SQLException, RemoteException, ITMException
 {
	 String sql2="",validUptoDateString = "";
	 PreparedStatement pstmt2 = null;
	 ResultSet rs2 = null;
	 Connection con;
	 Date validuptoDate = null;
	 con = getConnection();
	 
	 
	 //sql2 = "select max(valid_upto) from entp_subs_renew where enterprise = '"+enterprise+"' ";
	 //new changes
	 sql2 = "select lic_valid_upto from enterprise where enterprise ='"+enterprise+"' ";

		BaseLogger.log("3", null, null, "sql to get lic_valid_upto "+sql2 );
		try {
		pstmt2 = con.prepareStatement(sql2);				
		
			rs2 = pstmt2.executeQuery();
		
			
			if(rs2.next()) {
				
			 //validuptoDate =  rs2.getDate("max(valid_upto)");
			  validuptoDate =  rs2.getDate("lic_valid_upto");  
			 
			  
			  //checkNull(rs2.getDate("lic_valid_upto"));
			BaseLogger.log("3", null, null, " lic_valid_upto"+validuptoDate );

			}	
			
			 // validUptoDateString =   checkNull(validuptoDate.toString());


		}
		 catch (SQLException e) {
				e.printStackTrace();
			}
			rs2.close();
			rs2 = null;
			pstmt2.close();
			pstmt2 = null;
	 
	 return validuptoDate;
 }
 
 

 
 public String getEnterprise(String enterprise) throws SQLException, RemoteException, ITMException
 {
	 
		BaseLogger.log("3", null, null, "inside getenterprise"+enterprise );

	 String sql2="",validUptoDateString = "", enterpriseString = "";
	 PreparedStatement pstmt2 = null;
	 ResultSet rs2 = null;
	 Connection con;
	 Date validuptoDate = null;
	 con = getConnection();
	 
	 
	// sql2 = "select max(valid_upto) from entp_subs_renew where enterprise = '"+enterprise+"' ";
	// sql2 = "select lic_valid_upto from enterprise where enterprise ='"+enterprise+"' ";
	 sql2 = "select enterprise from enterprise where enterprise ='"+enterprise+"' ";

		BaseLogger.log("3", null, null, "sql to get lic_valid_upto "+sql2 );
		try {
			
			pstmt2 = con.prepareStatement(sql2);						
			rs2 = pstmt2.executeQuery();
		
			
			if(rs2.next()) {
				
			 //validuptoDate =  rs2.getDate("max(valid_upto)");
			 // validuptoDate =  rs2.getDate("lic_valid_upto"); 
				enterpriseString =   rs2.getString("enterprise");
			  //checkNull(rs2.getDate("lic_valid_upto"));
			BaseLogger.log("3", null, null, " lic_valid_upto"+validuptoDate );

			}	
			
			  //validUptoDateString =   checkNull(validuptoDate.toString());

			
		}
		 catch (SQLException e) {
				e.printStackTrace();
			}
			rs2.close();
			rs2 = null;
			pstmt2.close();
			pstmt2 = null;
	 
	 return enterpriseString;
 }
 
 
 
 public String checkNull(String inputStr) throws ITMException
	{
		try
		{
			if(inputStr != null && inputStr.trim().length() > 0)
			{
				return inputStr.trim();
			}
			else
			{
				return "";
			}
		}
		catch(Exception e)
		{
			BaseLogger.log("3", getUserInfo(), null,"Exception in checknull--["+e.getMessage()+"]");
			e.printStackTrace();
			throw new ITMException(e);
		}
	}
 
	
}
