package gastostarjetaServlet;

import java.io.IOException;
import java.sql.PreparedStatement;

import javax.servlet.http.HttpSession;

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

public class m05bGrabarGastosTarjetaServlet extends javax.servlet.http.HttpServlet{

   m05bFileLog miLog = new m05bFileLog(); 
   String persona="";


/**
 * 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="", accion;
       String sCodigo, sTarjeta, sFecha, sHora, sLitros, sCodcpto;
       String sEstcif, sEstncom, sCargo, sImporte, sMatricula;
       String sKilometros, sCentro, sTipo, sServicio, sDpto, sVerifi;
       String sUeconomic;
  	   java.sql.Connection conn=null;
	   PreparedStatement pstmt = null;
       m05bComun comun= new m05bComun();
       
       
       try  {
				HttpSession session = request.getSession(true);
                persona = (String) session.getAttribute("persona");	 
		        m05bGastosTarjeta gastostarjeta = (m05bGastosTarjeta)session.getAttribute("m05bGastosTarjeta");
				sCodigo = request.getParameter("Codigo");
				gastostarjeta.setGastar_control(sCodigo);
				sTarjeta=request.getParameter("Tarjeta");
				gastostarjeta.setGastar_tarjeta(sTarjeta);
				sFecha=request.getParameter("Fecha");
		     	gastostarjeta.setGastar_fecha(sFecha);
		        sHora=request.getParameter("Hora");
		        if ((sFecha != null) && (sHora != null))
		        	sFecha=comun.FechaBD(sFecha,sHora);
		        sLitros=request.getParameter("Litros");
		        if (sLitros==null || sLitros.equals(""))
		        		sLitros="0";
		        gastostarjeta.setGastar_litros(sLitros);
		        sCodcpto=request.getParameter("Concepto");
		        gastostarjeta.setGastar_concepto(sCodcpto);
		        sEstcif=request.getParameter("CIF");
		        gastostarjeta.setGastar_estcif(sEstcif);
		        sCentro=request.getParameter("Centro");
		        gastostarjeta.setGastar_centro(sCentro);
		        sEstncom=request.getParameter("Nombre");
		        gastostarjeta.setGastar_estdesc(sEstncom);
		        sCargo=request.getParameter("Cargo");
		        gastostarjeta.setGastar_cargo(sCargo);
		        sImporte=request.getParameter("Importe");
		        if (sImporte==null || sImporte.equals(""))
		        	sImporte="0";
		        gastostarjeta.setGastar_importe(sImporte);
		        sMatricula=request.getParameter("Matricula");
		        gastostarjeta.setGastar_matricula(sMatricula);
		        sKilometros=request.getParameter("Km");
		        if (sKilometros==null || sKilometros.equals(""))
		        		sKilometros="0";
		        gastostarjeta.setGastar_km(sKilometros);
		        sTipo=request.getParameter("Tipo");
		        gastostarjeta.setGastar_tipo(sTipo);
		        sVerifi=request.getParameter("Verifica")== null?"N":request.getParameter("Verifica");
                miLog.addTraza(persona , "Grabar Vehiculos:  " , "sVerifi: " + sVerifi,m05bConstantes.LOG_NIVEL_BAJO);
		        
		        gastostarjeta.setGastar_verifi(sVerifi);
		        sUeconomic=request.getParameter("Uecono");
		        gastostarjeta.setGastar_ueconomic(sUeconomic);
				accion=request.getParameter("Accion");

				try{
					m05bConexion miConex = new m05bConexion();
					conn = miConex.getConnection();
					if (accion.equals(m05bConstantes.ACC_ACTUALIZAR))  {
							sql=" update	" +m05bConstantes.TAB_OPEBANCOS+
									" set "+m05bConstantes.COL_18_IMPORTE+"	 = " + sImporte+
				                    ", "+m05bConstantes.COL_18_TARJETA+" = '"+sTarjeta+
				                    "', "+m05bConstantes.COL_18_FECHA+" = TO_DATE('"+sFecha+"','YYYYMMDDHH24MISS')"+
				                    ", "+m05bConstantes.COL_18_LITROS+" = "+sLitros+
				                    ", "+m05bConstantes.COL_18_CONCEPTO+" = '"+sCodcpto+
				                    "', "+m05bConstantes.COL_18_ESTCIF+" = '"+sEstcif+
				                    "', "+m05bConstantes.COL_18_ESTAB+" = '"+ sEstncom+
				                    "', "+m05bConstantes.COL_18_CARGO+" = '"+sCargo+
				                    "', "+m05bConstantes.COL_18_KM+" = " +sKilometros+
				                    ", "+m05bConstantes.COL_18_CENTRO+" = '"+sCentro+
				                    "', "+m05bConstantes.COL_18_TIPO+" = '"+ sTipo+
				                    "', "+m05bConstantes.COL_18_VERIFIC+" = '"+sVerifi+
				                    "', "+m05bConstantes.COL_18_UECONO+" = '" +sUeconomic+
									"'  where "+m05bConstantes.COL_18_CONTROL+"	 =  '"+sCodigo+"'";
                miLog.addTraza(persona , "Grabar Vehiculos:  " , "sqlUpdate: " + sql,m05bConstantes.LOG_NIVEL_BAJO);
							
		                pstmt = conn.prepareStatement(sql);
					} else if (accion.equals(m05bConstantes.ACC_INSERTAR))  {
						sql="insert into " +m05bConstantes.TAB_OPEBANCOS+
									"("+m05bConstantes.COL_18_CARGO+","+m05bConstantes.COL_18_CENTRO+","+
				                    m05bConstantes.COL_18_CONCEPTO+","+m05bConstantes.COL_18_CONTROL+","+
				                    m05bConstantes.COL_18_FECHA+","+m05bConstantes.COL_18_ESTCIF+","+
				                    m05bConstantes.COL_18_ESTAB+","+m05bConstantes.COL_18_TIPO+","+
				                    m05bConstantes.COL_18_IMPORTE+","+m05bConstantes.COL_18_MATRICULA+","+
				                    m05bConstantes.COL_18_KM+","+m05bConstantes.COL_18_LITROS+","+
				                    m05bConstantes.COL_18_TARJETA+","+m05bConstantes.COL_18_VERIFIC+","+
				                    m05bConstantes.COL_18_UECONO+") " +" values " +"('"+sCargo+"' ,'"+sCentro+"','"+sCodcpto+"','"+sCodigo+
				                    "' ,TO_DATE('"+sFecha+"','YYYYMMDDHH24MISS') ,'"+sEstcif+"','"+sEstncom+"','"+sTipo+"',"+sImporte+",'"+sMatricula+"',"+
				                    sKilometros+","+sLitros+",'"+sTarjeta+"','"+sVerifi+"','"+sUeconomic+"' )" ;
                miLog.addTraza(persona , "Grabar Vehiculos:  " , "sqlInsert: " + sql,m05bConstantes.LOG_NIVEL_BAJO);
				        
					pstmt = conn.prepareStatement(sql);
						pstmt.executeUpdate();
					  // Actualizar los km y la fecha actuales del vehiculo

			                sql="update	" + m05bConstantes.TAB_VEHICULOS +
									" set " + m05bConstantes.COL_34_KMACTUALES + " = " + sKilometros + 
									" ,  " + m05bConstantes.COL_34_FKMACT + "= TO_DATE ('" + sFecha + "','YYYYMMDDHH24MISS') " +
									" where " + m05bConstantes.COL_34_MATRICULA + " = '" + sMatricula + "' AND TO_CHAR("+
							            m05bConstantes.COL_34_FKMACT+",'YYYY/mm/dd')<='"+sFecha+"' AND "+m05bConstantes.COL_34_KMACTUALES +
							        "<='"+sKilometros+"'";
                miLog.addTraza(persona , "Grabar Vehiculos:  " , "sqlUpdate Vedhiculos: " + sql,m05bConstantes.LOG_NIVEL_BAJO);
							
					} else   {
						sql="delete from " +m05bConstantes.TAB_OPEBANCOS+
									" where "+m05bConstantes.COL_18_CONTROL+" = " +  sCodigo ;
                miLog.addTraza(persona , "Grabar Vehiculos:  " , "sqlDelete Vedhiculos: " + sql,m05bConstantes.LOG_NIVEL_BAJO);
						
					}
					pstmt = conn.prepareStatement(sql);
					pstmt.executeUpdate();
					response.sendRedirect("m05bVehGastosJsp.jsp?Matricula="+sMatricula);

				} 	catch (Exception SQLException) {
						miLog.addTraza(SQLException);
						response.sendRedirect("m05bError.jsp");
				}	finally{
						if (pstmt != null)
							pstmt.close();
						if (conn != null)
							conn.close();
				}
	} catch(Throwable theException) {
		miLog.addTraza(theException);
		try {
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
	}
}
}