package siniestrosServlet;


import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import m05bBD.m05bSiniestros;
import m05bClasses.m05bComun;
import m05bClasses.m05bConexion;
import m05bClasses.m05bConstantes;
import m05bClasses.m05bFileLog;

/**
 * Inserte aquí la descripción del tipo.
 * Fecha de creación: (27/07/04)
 */
public class m05bGrabarSiniestrosServlet extends javax.servlet.http.HttpServlet {m05bFileLog miLog = new m05bFileLog();
/**
 * Process incoming HTTP POST requests
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {

	performTask(request, response);

}
/**
 * Returns the servlet info string.
 */
public String getServletInfo() {

	return super.getServletInfo();

}
/**
 * Initializes the servlet.
 */
public void init() {
	// insert code to initialize the servlet here

}
/**
 * Process incoming requests for information
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */


public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {
       String sql="";
       String sCodigo;
       String Matricula;
       String sFecha;
       String sPolCon;
       String sFecRes;
       String sResponsa;
       String sImporte;
       String sPeritado;
       String sTaller;
       String sComentarios;
       String sDniConGV;
       String sNomConCon;
       String sDniConCon;
       String sMatCon;
       String sNomAsegCon;
       String sDniAsegCon;
       String sCiaAsegCon;
       String sDescTaller="";
       String forFecha="YYYYMMDDHH24MISS";
       String sErrorTaller="0";
       String sErrorDni="0";
       String accion;
       m05bComun comun = new m05bComun();
	   java.sql.Connection conn=null;
	   PreparedStatement pstmt = null;
	   ResultSet rs = null;
	   boolean finalizado = false;
       
       try

	{
		HttpSession session = request.getSession(true);
		Locale idiomaLocal = (Locale)session.getAttribute("Local");
		String idiomaSes = ((idiomaLocal.getLanguage()==null)?"es":idiomaLocal.getLanguage());	
	
        String campoTexto="C";
        String formatoFecha="dd/MM/yyyy";
        if(!idiomaSes.equals("es")){
			campoTexto="E";
			formatoFecha="yyyy/MM/dd";
        }
        m05bFileLog miLog = new m05bFileLog(); 
        m05bSiniestros siniestros = (m05bSiniestros)session.getAttribute("m05bSiniestros");
		sCodigo = request.getParameter("Codigo").trim();
		siniestros.setSin_codigo(sCodigo);
		
        sDniConGV=request.getParameter("Dni").trim();
        if (sDniConGV==null)
        sDniConGV="";
        
        siniestros.setSin_dni(sDniConGV);
        sNomAsegCon=request.getParameter("NomAsegCon").trim().toUpperCase();
        if (sNomAsegCon==null)
        sNomAsegCon="";
        
        siniestros.setSin_nombreacc(sNomAsegCon);
        sDniAsegCon=request.getParameter("DniAsegCon").trim();
        if (sDniAsegCon==null)
        sDniAsegCon="";
        
        siniestros.setSin_dniacc(sDniAsegCon);
        sCiaAsegCon=request.getParameter("CiaAsegCon").trim().toUpperCase();
        if (sCiaAsegCon==null)
        sCiaAsegCon="";
        
        siniestros.setSin_aseguradoracc(sCiaAsegCon);
        sNomConCon=request.getParameter("NomConCon").trim().toUpperCase();
        if (sNomConCon==null)
        sNomConCon="";
        
        siniestros.setSin_nombrecc(sNomConCon);
        sDniConCon=request.getParameter("DniConCon").trim();
        if (sDniConCon==null)
        sDniConCon="";
        
        siniestros.setSin_dnicc(sDniConCon);
        sMatCon=request.getParameter("MatCon").trim().toUpperCase();
        if (sMatCon==null)
        sMatCon="";
        
        siniestros.setSin_matriculacc(sMatCon);
        Matricula=request.getParameter("Matricula").trim();
        if (Matricula==null)
        Matricula="";
        
        siniestros.setSin_matricula(Matricula);
		sFecha=request.getParameter("Fecha").trim();
		siniestros.setSin_fecsiniestro(comun.FechaObj(sFecha,""));
        if (sFecha==null)
        sFecha="";
       
        if (sFecha!=null)
        	sFecha=comun.FechaBD(sFecha,"00:00",campoTexto);
 		
		sComentarios=request.getParameter("Comentarios").trim().toUpperCase();
        if (sComentarios==null)
        sComentarios="";
		
		siniestros.setSin_observacion(sComentarios);
        sTaller=request.getParameter("CodTaller").trim().toUpperCase();
        if (sTaller==null)
        sTaller="";
        
		siniestros.setSin_taller(sTaller);
        sDescTaller=request.getParameter("DescTaller").trim().toUpperCase();
        if (sDescTaller==null)
        sDescTaller="";		
		sPeritado=request.getParameter("Peritado")==null?"N":request.getParameter("Peritado").trim().toUpperCase();
		//System.out.println("Peritado"+sPeritado);
        siniestros.setSin_peritado(sPeritado);
		sImporte=request.getParameter("Importe").trim().toUpperCase();
        if (sImporte==null || sImporte.equals("NULL") || sImporte.equals(""))
        	sImporte="0";
        sImporte=comun.ImportesABasedatos(sImporte);
        siniestros.setSin_importe(sImporte);
        sResponsa=request.getParameter("Responsabilidad").trim().toUpperCase();
		if (sResponsa==null)
        sResponsa="";
        
        siniestros.setSin_responsable(sResponsa);
        sFecRes=request.getParameter("FecResolucion").trim();
		if (sFecRes==null)
        sFecRes="";
        
        if (sFecRes!=null)
        siniestros.setSin_fecresolucion(comun.FechaObj(sFecRes,""));
        sFecRes=comun.FechaBD(sFecRes,"00:00",campoTexto);
        
         sPolCon=request.getParameter("PolContrario").trim().toUpperCase();
		if (sPolCon==null)
        sPolCon="";
        
        siniestros.setSin_polizacc(sPolCon);
        session.setAttribute("m05bSiniestros",siniestros);        
    	accion=request.getParameter("Accion");
 
  		
		try{
		 m05bConexion miConex = new m05bConexion();
		 conn = miConex.getConnection();
		 if (!(sTaller.equals(""))){
		       sql = "SELECT 1  FROM "+ m05bConstantes.TAB_TALLER +
		             " WHERE " + m05bConstantes.COL_30_CODIGO+ "='" + sTaller + "'";
    		  pstmt = conn.prepareStatement(sql);
	    	  rs = pstmt.executeQuery();	 
		      if (!(rs.next())){
		     	 sErrorTaller="1";
		      }
		 }
		 if (sErrorTaller.equals("1")){
		 	 response.sendRedirect("m05bSiniestrosJsp.jsp?Matricula="+Matricula+"&Mensaje="+m05bConstantes.MSG_NOEXISTETALL+"&Accion="+accion+"&Codigo="+sCodigo);
		 	 finalizado = true;
		 }
		 else{
			 sql = "SELECT 1  FROM "+ m05bConstantes.TAB_PERSONAL +
		       " WHERE " + m05bConstantes.COL_21_DNI+ "='" + sDniConGV + "'";
		     pstmt = conn.prepareStatement(sql);
		     rs = pstmt.executeQuery();
		     if (!(rs.next())){ 
			   response.sendRedirect("m05bSiniestrosJsp.jsp?Matricula="+Matricula+"&Mensaje="+m05bConstantes.MSG_NOEXISTEDNI+"&Accion="+accion+"&Codigo="+sCodigo);
			   finalizado = true;
		     }else {					 	
			    if (accion.equals(m05bConstantes.ACC_ACTUALIZAR))  {
			        pstmt = conn.prepareStatement("update " +m05bConstantes.TAB_SINIESTROS+
					" set "+m05bConstantes.COL_29_DNI+" = '"+sDniConGV+
					"' ,"+m05bConstantes.COL_29_PERITADO+" = '"+sPeritado+
					"' ,"+m05bConstantes.COL_29_IMPORTE +" =  " +sImporte+
					" ,"+m05bConstantes.COL_29_OBSERV+" = '" +sComentarios+
					"' ,"+m05bConstantes.COL_29_MATRICULAC+" = '" +sMatCon+
                    "' ,"+m05bConstantes.COL_29_RESPONS+" = '" +sResponsa+
                    "' ,"+m05bConstantes.COL_29_TALLER+" = '"+sTaller+
                    "',	"+m05bConstantes.COL_29_POLIZACC+" = '" +sPolCon+
                    "' ,"+m05bConstantes.COL_29_DNIACC+" = '" +sDniAsegCon+
                    "' ,"+m05bConstantes.COL_29_DNICC+" = '" +sDniConCon+
                    "', "+m05bConstantes.COL_29_FSINIESTRO+" = TO_DATE ('"+sFecha+"','YYYYMMDDHH24MISS') " +
                    " ,	"+m05bConstantes.COL_29_FRESOLU+" = TO_DATE ('"+sFecRes+"','YYYYMMDDHH24MISS') " +
                    " ,	"+m05bConstantes.COL_29_NOMBREC+" =  '" +sNomConCon+
                    "' ,"+m05bConstantes.COL_29_ASEGURCC+" = '" +sCiaAsegCon+
                    "' ,"+m05bConstantes.COL_29_NOMBREASCC+" =  '" +sNomAsegCon+
                    "'  where "+m05bConstantes.COL_29_CODIGO+" = "+sCodigo);
			         miLog.addTraza("BEGO" , "m05bSiniestrosJsp", "grabar:" +"update " +m05bConstantes.TAB_SINIESTROS+
							" set "+m05bConstantes.COL_29_DNI+" = '"+sDniConGV+
							"' ,"+m05bConstantes.COL_29_PERITADO+" = '"+sPeritado+
							"' ,"+m05bConstantes.COL_29_IMPORTE +" =  " +sImporte+
							" ,"+m05bConstantes.COL_29_OBSERV+" = '" +sComentarios+
							"' ,"+m05bConstantes.COL_29_MATRICULAC+" = '" +sMatCon+
		                    "' ,"+m05bConstantes.COL_29_RESPONS+" = '" +sResponsa+
		                    "' ,"+m05bConstantes.COL_29_TALLER+" = '"+sTaller+
		                    "',	"+m05bConstantes.COL_29_POLIZACC+" = '" +sPolCon+
		                    "' ,"+m05bConstantes.COL_29_DNIACC+" = '" +sDniAsegCon+
		                    "' ,"+m05bConstantes.COL_29_DNICC+" = '" +sDniConCon+
		                    "', "+m05bConstantes.COL_29_FSINIESTRO+" = TO_DATE ('"+sFecha+"','YYYYMMDDHH24MISS') " +
		                    " ,	"+m05bConstantes.COL_29_FRESOLU+" = TO_DATE ('"+sFecRes+"','YYYYMMDDHH24MISS') " +
		                    " ,	"+m05bConstantes.COL_29_NOMBREC+" =  '" +sNomConCon+
		                    "' ,"+m05bConstantes.COL_29_ASEGURCC+" = '" +sCiaAsegCon+
		                    "' ,"+m05bConstantes.COL_29_NOMBREASCC+" =  '" +sNomAsegCon+
		                    "'  where "+m05bConstantes.COL_29_CODIGO+" = "+sCodigo , m05bConstantes.LOG_NIVEL_BAJO);						
			}
			
			else if (accion.equals(m05bConstantes.ACC_INSERTAR))  {
				sql=	"insert into " +m05bConstantes.TAB_SINIESTROS+
					" ("+m05bConstantes.COL_29_CODIGO+","+m05bConstantes.COL_29_DNI+","+m05bConstantes.COL_29_MATRICULA+
                    ","+m05bConstantes.COL_29_NOMBREC+","+m05bConstantes.COL_29_DNICC+","+m05bConstantes.COL_29_TALLER+
                    ","+m05bConstantes.COL_29_MATRICULAC+","+m05bConstantes.COL_29_NOMBREASCC+","+m05bConstantes.COL_29_DNIACC+
                    ","+m05bConstantes.COL_29_ASEGURCC+","+m05bConstantes.COL_29_POLIZACC+","+m05bConstantes.COL_29_IMPORTE+","+
                    m05bConstantes.COL_29_FSINIESTRO+","+m05bConstantes.COL_29_FRESOLU+","+m05bConstantes.COL_29_RESPONS+","+
                    m05bConstantes.COL_29_PERITADO+","+m05bConstantes.COL_29_OBSERV+ ")  values " +
					"("+sCodigo+",'"+sDniConGV+"','"+Matricula+"','"+sNomConCon+"','"+sDniConCon+
                    "','"+sTaller+"','"+sMatCon+"','"+sNomAsegCon+"','"+sDniAsegCon+"','"+sCiaAsegCon+"','"+sPolCon+
                    "',"+sImporte+",TO_DATE ('"+sFecha+"','"+forFecha+"'),TO_DATE ('"+sFecRes+"','"+forFecha+"'),'"+sResponsa+"','"+sPeritado+"','"+sComentarios+"' )" ;
				pstmt = conn.prepareStatement(
					"insert into " +m05bConstantes.TAB_SINIESTROS+
					" ("+m05bConstantes.COL_29_CODIGO+","+m05bConstantes.COL_29_DNI+","+m05bConstantes.COL_29_MATRICULA+
                    ","+m05bConstantes.COL_29_NOMBREC+","+m05bConstantes.COL_29_DNICC+","+m05bConstantes.COL_29_TALLER+
                    ","+m05bConstantes.COL_29_MATRICULAC+","+m05bConstantes.COL_29_NOMBREASCC+","+m05bConstantes.COL_29_DNIACC+
                    ","+m05bConstantes.COL_29_ASEGURCC+","+m05bConstantes.COL_29_POLIZACC+","+m05bConstantes.COL_29_IMPORTE+","+
                    m05bConstantes.COL_29_FSINIESTRO+","+m05bConstantes.COL_29_FRESOLU+","+m05bConstantes.COL_29_RESPONS+","+
                    m05bConstantes.COL_29_PERITADO+","+m05bConstantes.COL_29_OBSERV+ ")  values " +
					"("+sCodigo+",'"+sDniConGV+"','"+Matricula+"','"+sNomConCon+"','"+sDniConCon+
                    "','"+sTaller+"','"+sMatCon+"','"+sNomAsegCon+"','"+sDniAsegCon+"','"+sCiaAsegCon+"','"+sPolCon+
                    "',"+sImporte+",TO_DATE ('"+sFecha+"','"+forFecha+"'),TO_DATE ('"+sFecRes+"','"+forFecha+"'),'"+sResponsa+"','"+sPeritado+"','"+sComentarios+"' )" );
                
        						
			}
			else   {

				pstmt = conn.prepareStatement(
					"delete from  " +m05bConstantes.TAB_SINIESTROS+
					" where "+m05bConstantes.COL_29_CODIGO+" =  ? " );
				pstmt.setString(1, sCodigo);
			}
			if(!finalizado){    
				pstmt.executeUpdate();
				response.sendRedirect("m05bVehSiniestroJsp.jsp?Matricula="+Matricula);
			}	
		  }}
		} 	catch (Exception SQLException) {
				miLog.addTraza(SQLException);
				response.sendRedirect("m05bError.jsp");
		} 	finally{
				if (pstmt != null)
					pstmt.close();
				if (conn != null)
					conn.close();
		}


	}
	catch(Throwable theException)
	{
		// uncomment the following line when unexpected exceptions
		// are occuring to aid in debugging the problem.
		miLog.addTraza(theException);
		try {
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
	}
}
}