<?php 
//session_start()
 //include("funciones/conexion.php");
 include("seguridad.php");
 include('funciones/funciones2.php');

//foreach($_POST as $campo => $valor){
// echo "- ". $campo ." = ". $valor;
//}

//	$notas_creditos = $_POST['notas_creditos'];	
//	foreach ($Valores as $key => $value) {
//		echo "- ". $key ." = ". $Valores[$key] ;
//	}
//exit();

$vuelve = false;

 $id_empresa     			= 	$_POST['id_empresa'];
 $empresa 					= 	$_POST['empresa'];
 $id_municipio     			=	$_POST['id_municipio'];
 $municipio     			=	$_POST['municipio'];
 $mes   					= 	$_POST['mes'];
 $anio     					= 	$_POST['anio'];
 $total_emp     			=	$_POST['total_emp'];
 $total_remu    			= 	$_POST['total_remu'];
 $cuit    					= 	$_POST['cuit'];
 $usuario  					= 	$_POST['cuit'];
 
 $art_100     				= 	$_POST['art_100'];
 $cant_art_100 				= 	$_POST['cant_art_100'];
 $total_art_101     		=	$_POST['total_art_101'];
 $total_art_101_porciento 	=	$_POST['total_art_101_porciento'];
 $cant_art_101   			= 	$_POST['cant_art_101'];
 $otros_aportes     		= 	$_POST['otros_aportes'];
 $imp_otros_aportes     	=	$_POST['imp_otros_aportes'];
 $banco    					= 	$_POST['tipo_pago']; 

 $total_pagar     			= 	$_POST['art_100']  + $_POST['total_art_101_porciento'] + $_POST['imp_otros_aportes'] ;
$total_pagar2 				= $total_pagar;
 
 $tipo_pago 				= 	$_POST['tipo_pago'];
 $banco_pago     			=	$_POST['banco_pago'];
 $cheque_pago     			=	$_POST['cheque_pago'];
 $lugar_pago				=	$_POST['lugar_pago'];
 
 $total_nc                  =	$_POST['imp_credito'];
 $Valor_NC                  = 0;
 $id_nota_credito	 		=	$_POST['id_nota_credito'];
 $notas_creditos             = $_POST['notas_creditos'];	


 if($total_pagar <= $total_nc){

 	$Valor_NC = $total_pagar;
 	$total_pagar = 0;
 }else{
 	$Valor_NC =$total_nc  ;
 	$total_pagar -= $total_nc ;
 }


 // Tipo de formulario  asi era por defaul se cambia a partir de junio del 2020 
 //para soportar mas de 1 formulario x mes

 // A patir de 25/05/2022 se agrega el manejo de las notas de creditos
 // se modifica la cantidad de ejemplares en Pago Facil y ademas se genera una 
 // boleta la cual se podra emitir con un importe de cero cuando la NC cubra o supere la
 // cuota del mes en cuestion

 $tipo_formulario  = $_POST['tipo_formulario'];

  if ($mes < 1){
  	 print "<script>alert('No tiene mes!!!')</script>"; 
      $vuelve = true;
  }

  if ($vuelve == true){
     echo "<script LANGUAGE='JavaScript'>
	 function redireccionar() { history.back(1); }
	 setTimeout ('redireccionar()', 0);
   </script>";
}

if($Valor_NC == 0){
	if ($total_pagar == 0 or $total_pagar == ""){
		print "<script>alert('No Puede Ser Importe Cero (0)!!!')</script>"; 
      $vuelve = true;
		
	}
}
    if ($art_100  ==0 or $art_100  ==""){
    	print "<script>alert('No Puede Ser Importe Art. 100 en Cero (0) !!!')</script>"; 
      $vuelve = true;
    }


  if ($lugar_pago < 1){
  	print "<script>alert('No Selecciono el Lugar de Pago!!!')</script>"; 
      $vuelve = true;  	
  }

  if ($cant_art_100 == '')
 {
		print "<script>alert('La Cantidad de Art 100 No puede ser Cero (0)!!!')</script>"; 
      $vuelve = true;  
}
if ($cant_art_101 > 0)
{
	if ( $total_art_101_porciento == 0 )
	{
		print "<script>alert('El Importe de Art 101 No puede ser Cero (0)!!!')</script>"; 
      $vuelve = true;  
	}
}





  $_SESSION["s_mes"] = $mes ; 
  $_SESSION["s_anio"] = $anio; 
  $_SESSION["tipo_f"] = $tipo_formulario;

if ($cheque_pago == '')
{
	$cheque_pago ="0";
}
  


if ($cant_art_101 == '')
{
	$cant_art_101 ="0";
}
 

 date_default_timezone_set('America/Argentina/Buenos_Aires');
 $fecha = date("Y-m-d");
 $hora_envio = date("G:i:s");
 
 
 
 if ($anio ==""){
	print "<script>alert('Error en Año No puede ser vacio!!!')</script>"; 
      $vuelve = true;
}else{
	if ($anio > date("Y") ){
		 print "<script>alert('Error en Año es superior al actual!!!')</script>"; 
      	$vuelve = true;
  	}else{
  		if($anio == date("Y")){
  			if($mes > date("m")){
  			print "<script>alert('Error en Mes es superior al actual!!!')</script>"; 
      		$vuelve = true;	
  			}
  		}
	}
}
 
 

 if ($tipo_pago == 1) 
  {
  	if($lugar_pago== 4){
			$bancodepo = "Nacion por Cajero Automarico";
  	}else
  		{if($lugar_pago== 3){
			$bancodepo = "Nacion por Transferencia ";
		}else{	
  			$bancodepo = "Nacion Cta.Cte Nº 40700093/57";
  		}
   }
    $banco_pago    =	"Banco Nacion";
  }
   
 if ($tipo_pago == 2) 
  {
   $bancodepo = "Pago Facil Nº 1561";
   $banco_pago    =	"Pago Facil";   
   $cheque_pago   = 0;
  } 

if ($tipo_pago == 3) 
  {
	if($lugar_pago== 3){
			$bancodepo = "Macro por Trasferencia";
			$banco_pago    =	"Banco Macro";  
  	}else{
  		$bancodepo = "Nacion por Trasferencia";
  		 $banco_pago    =	"Banco Nacion"; 
  	}  
      $cheque_pago   = 0;
  } 
  


 if ($tipo_pago == 4) 
  {
  	if($lugar_pago== 3){
			$bancodepo = "Macro por Cajero Automarico";
			$banco_pago    =	"Banco Macro";  
  	}else{
  		$bancodepo = "Nacion por Cajero Automarico";
  		 $banco_pago    =	"Banco Nacion"; 
  	} 
   $cheque_pago   = 0;
   
  } 





 if ($total_pagar > 0) 
  {
  	$con_letra = valorEnLetras($total_pagar); 
  }else{
  	$con_letra = "Cero Pesos"; 
  }

 if ($tipo_pago == 1) 
  {
   $cheque = "--";					
   $efectivo = "Si";     
  }
 if ($tipo_pago == 2) 
  {
   $cheque = "Si";					
   $efectivo = "--";     
  }
  if ($tipo_pago == 3) 
  {
   $cheque = "--";					
   $efectivo = "--";     
  }

  if ($tipo_pago == 4) 
  {
   $cheque = "--";					
   $efectivo = "--";     
  }
 if ((int)$mes < 10) 
  {
   $mes = "0" . $mes;     
  }


$banco =  $lugar_pago;


// Armamos total_menu
 $total_remu_p = str_replace(',','',$total_remu); 
 $total_remu_x = str_replace('.','',$total_remu_p);  

// Armamos total_art_101
 $total_art_101_p = str_replace(',','',$total_art_101); 
 $total_art_101_x = str_replace('.','',$total_art_101_p);  

// Armamos imp_otros_aportes
 $imp_otros_aportes_p = str_replace(',','',$imp_otros_aportes); 
 $imp_otros_aportes_x = str_replace('.','',$imp_otros_aportes_p);  
if ($imp_otros_aportes_x==""){
	$imp_otros_aportes_x = "0";
	}

// Armamos total_pagar
$porciones = explode(".", $total_pagar);
 //$porciones[0] ; // ENTEROS
 //$porciones[1]; // Decimales

if ( $porciones[1] ==""){
	$porciones[1] = "00"; // Decimales
	}

if (strlen( $porciones[1]) == "1")  {
	$porciones[1] = $porciones[1] ."0"; // Decimales solo 1
	}
$total_pagar_x = $porciones[0] . $porciones[1] ;
  
 //$total_pagar_p = str_replace(',','',$total_pagar); 
 
 //$total_pagar_x = str_replace('.','',$total_pagar_p); 
 
//echo "formateado ". $total_pagar_x;
// Armamos CUIT
 $cuit = str_replace('-','',$cuit); 

// Armamos fecha 
 date_default_timezone_set('America/Argentina/Buenos_Aires');
 $fecha_envio = date("d-m-Y");
 $fecha_envio = str_replace('-','',$fecha_envio); 
 
 
 
 //------------- Valida que n o exista el periodo en un acta  SEPTIEMBRE 2023----------------------------


 
 if ($vuelve == false)
{
   include 'conexion.php';

 
$_existe_acta =  "select * from det_actas where id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " . $anio  ;
$query_Acta = mysqli_query($coneccion ,	$_existe_acta);
$row_Acta = mysqli_fetch_array($query_Acta);
if ($row_Acta >= 1){
      print "<script>alert('No Se puede Generar el Comprobante Ya existe UN ACTA con ese Peiodo!!!')</script>"; 
      $vuelve = true;
                   }
}
 
 //-------------------------------------------------------------------------------------------
 
 
 
 
 

if ($vuelve == true){
echo "<script LANGUAGE='JavaScript'>
	 function redireccionar() { history.back(1); }
	 setTimeout ('redireccionar()', 0);
   </script>";
}



//--------------	COMIENZA A GRABAR REGISTROS ---------------------------


if ($vuelve == false)
{
   include 'conexion.php';



// ---------------------- NOTA DE CREDITO SI TIENE -------------------------
	if ($total_nc <> 0 ){
		$banco_pago = $banco_pago ." + Nota de Credito";
    	if( $total_pagar == 0) {
    		$tipo_cobro = 5;            // cobro total
		}else{
			$tipo_cobro = 6;
		}

    	$_pagi_sql = "select * from det_cobros where id_tipo_formulario = " .$tipo_formulario. " and id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " . $anio . " and tipo_cobro =". $tipo_cobro ;

            
 		$query = mysqli_query($coneccion ,	$_pagi_sql);
		$row = mysqli_fetch_array($query);

		//echo $_pagi_sql;
      	//exit();

 		$activo = "1";
 		
 		if ($row >= 1){
 		
 			$id_cobroNC = $row["id_cobro"];
			$sql = "UPDATE det_cobros SET art_100 = ". $Valor_NC.", total = ". $Valor_NC .", id_banco = '". $banco ."', id_deposito = ". $id_nota_credito .", fecha = '". $fecha ."  WHERE id_cobro = ". $id_cobroNC ;      				
      	
      		//echo "salida 1 ". $sql;
      		//exit();
      		
      		$query_update = mysqli_query($coneccion,$sql);      				
 		}
 		else 		
 		{ 	

           // Aca se agregan los registro que sean necesarios 1 x cada nota de credito Usado
			$saldo = $total_pagar2;			
			$suma = 0;
			//echo count($notas_creditos) . "<br>";
			foreach ($notas_creditos as $id => $value) {
				//echo "Saldo al comienzo:" .$saldo. "<br>";					
				if( $saldo > 0){
					
				$id_nota_credito = $id;
				$Valor_NC = $value;					

				if($Valor_NC >= $saldo and  $saldo > 0){

					$Valor_NC = $saldo;
					$saldo = 0;	

					$sql = "INSERT INTO cobros (id_banco,tipo_cobro,fecha,registros,  importe, id_usuario, activo)".   "VALUES ('$banco', '$tipo_cobro', '$fecha', 1, '$Valor_NC', '9', '$activo')";			               
        	
        			$query_update = mysqli_query($coneccion,$sql);	
        			$_query_sql = "select MAX(id_cobro) AS ultimo_id FROM cobros " ;
       
					$query = mysqli_query($coneccion ,	$_query_sql);
					$row2 = mysqli_fetch_array($query);
					if ($row2 >= 1){
				      		$id_cobroNC = $row2["ultimo_id"];
						}else{
			   		   		$id_cobroNC = 0;
						}

						$sql2 = "INSERT INTO det_cobros (id_cobro,tipo_cobro,id_tipo_formulario, id_empresa,fecha,mes,anio,art_100,art_101,otros,total,id_banco,id_deposito, id_usuario, activo)". "VALUES ('$id_cobroNC', '$tipo_cobro','$tipo_formulario','$id_empresa','$fecha','$mes','$anio','$total_nc','0','0' , '$Valor_NC','$banco', '$id_nota_credito','9', '$activo')";

						$sql3 = "UPDATE notas_creditos SET saldo = saldo - $Valor_NC , usado = true WHERE id_empresa = '$id_empresa'  AND id_nota_credito = '$id_nota_credito'";						

						$query_update2 = mysqli_query($coneccion,$sql2);
						$query_update3 = mysqli_query($coneccion,$sql3);				

					}

					if($Valor_NC < $saldo and  $saldo > 0){
						
						$saldo -= $Valor_NC ;


						$sql = "INSERT INTO cobros (id_banco,tipo_cobro,fecha,registros,  importe, id_usuario, activo)".   "VALUES ('$banco', '$tipo_cobro', '$fecha', 1, '$Valor_NC', '9', '$activo')";
	              
        				$query_update = mysqli_query($coneccion,$sql);	
        				$_query_sql = "select MAX(id_cobro) AS ultimo_id FROM cobros " ;
       
						$query = mysqli_query($coneccion ,	$_query_sql);
						$row2 = mysqli_fetch_array($query);
						if ($row2 >= 1){
				       		$id_cobroNC = $row2["ultimo_id"];
						}else{
			   		   		$id_cobroNC = 0;
						}

						$sql2 = "INSERT INTO det_cobros (id_cobro,tipo_cobro,id_tipo_formulario, id_empresa,fecha,mes,anio,art_100,art_101,otros,total,id_banco,id_deposito, id_usuario, activo)". "VALUES ('$id_cobroNC', '$tipo_cobro','$tipo_formulario','$id_empresa','$fecha','$mes','$anio','$Valor_NC','0','0' , '$Valor_NC','$banco', '$id_nota_credito','9', '$activo')";

						$sql3 = "UPDATE notas_creditos SET saldo = saldo - $Valor_NC , usado = true WHERE id_empresa = '$id_empresa'  AND id_nota_credito = '$id_nota_credito'";
						
						//echo $sql2 . "<br>";
						//echo "Valor NC: ".$Valor_NC . "<br>";		
						//exit();
						//echo "Saldo: ". $saldo. "<br>";						

						$query_update2 = mysqli_query($coneccion,$sql2);
						$query_update3 = mysqli_query($coneccion,$sql3);						

					}
	
				}				

			}
			
 		}

    }

 //echo " Termino de Guardar" . $suma;
 //exit();
	if ($banco == 1) 
		// Armamos código de barra Banco NacION
   {
		$aux = '8570';
		$aux = $aux . $cuit;
		$aux = $aux . $mes;
		$aux = $aux . $anio;
		$aux = $aux . str_pad($total_emp, 4, "0", STR_PAD_LEFT);
		$aux = $aux . str_pad($total_remu_x, 11, "0", STR_PAD_LEFT);
		$aux = $aux . $fecha_envio;
		$aux = $aux . str_pad($cant_art_100, 4, "0", STR_PAD_LEFT);
		$aux = $aux . str_pad($total_art_101_x, 10, "0", STR_PAD_LEFT);
		$aux = $aux . str_pad($imp_otros_aportes_x, 9, "0", STR_PAD_LEFT);
		$aux = $aux . str_pad($total_pagar_x, 11, "0", STR_PAD_LEFT);
			
		// armamos codigo de control 
		
		$dv = (int)substr($aux, 0, 1);
		
		$dv += (int)substr($aux, 1, 1) * 2;
		$dv += (int)substr($aux, 2, 1) * 3;
		$dv += (int)substr($aux, 3, 1) * 4;
		$dv += (int)substr($aux, 4, 1) * 5;
		$dv += (int)substr($aux, 5, 1) * 6;
		$dv += (int)substr($aux, 6, 1) * 7;
		$dv += (int)substr($aux, 7, 1) * 1;
		$dv += (int)substr($aux, 8, 1) * 2;
		$dv += (int)substr($aux, 9, 1) * 3;
		$dv += (int)substr($aux, 10, 1) * 4;
		$dv += (int)substr($aux, 11, 1) * 5;
		$dv += (int)substr($aux, 12, 1) * 6;
		$dv += (int)substr($aux, 13, 1) * 7;
		$dv += (int)substr($aux, 14, 1) * 1;
		$dv += (int)substr($aux, 15, 1) * 2;
		$dv += (int)substr($aux, 16, 1) * 3;
		$dv += (int)substr($aux, 17, 1) * 4;
		$dv += (int)substr($aux, 18, 1) * 5;
		$dv += (int)substr($aux, 19, 1) * 6;
		$dv += (int)substr($aux, 20, 1) * 7;
		$dv += (int)substr($aux, 21, 1) * 1;
		$dv += (int)substr($aux, 22, 1) * 2;
		$dv += (int)substr($aux, 23, 1) * 3;
		$dv += (int)substr($aux, 24, 1) * 4;
		$dv += (int)substr($aux, 25, 1) * 5;
		$dv += (int)substr($aux, 26, 1) * 6;
		$dv += (int)substr($aux, 27, 1) * 7;
		$dv += (int)substr($aux, 28, 1) * 1;
		$dv += (int)substr($aux, 29, 1) * 2;
		$dv += (int)substr($aux, 30, 1) * 3;
		$dv += (int)substr($aux, 31, 1) * 4;
		$dv += (int)substr($aux, 32, 1) * 5;
		$dv += (int)substr($aux, 33, 1) * 6;
		$dv += (int)substr($aux, 34, 1) * 7;
		$dv += (int)substr($aux, 35, 1) * 1;
		$dv += (int)substr($aux, 36, 1) * 2;
		$dv += (int)substr($aux, 37, 1) * 3;
		$dv += (int)substr($aux, 38, 1) * 1;
		$dv += (int)substr($aux, 39, 1) * 2;
		$dv += (int)substr($aux, 40, 1) * 3;
		$dv += (int)substr($aux, 41, 1) * 4;
		$dv += (int)substr($aux, 42, 1) * 5;
		$dv += (int)substr($aux, 43, 1) * 6;
		$dv += (int)substr($aux, 44, 1) * 7;
		$dv += (int)substr($aux, 45, 1) * 1;
		$dv += (int)substr($aux, 46, 1) * 2;
		$dv += (int)substr($aux, 47, 1) * 3;
		$dv += (int)substr($aux, 48, 1) * 4;
		$dv += (int)substr($aux, 49, 1) * 5;
		$dv += (int)substr($aux, 50, 1) * 6;
		$dv += (int)substr($aux, 51, 1) * 7;
		$dv += (int)substr($aux, 52, 1) * 1;
		$dv += (int)substr($aux, 53, 1) * 2;
		$dv += (int)substr($aux, 54, 1) * 3;
		$dv += (int)substr($aux, 55, 1) * 4;
		$dv += (int)substr($aux, 56, 1) * 5;
		$dv += (int)substr($aux, 57, 1) * 6;
		$dv += (int)substr($aux, 58, 1) * 7;
		$dv += (int)substr($aux, 59, 1) * 1;
		$dv += (int)substr($aux, 60, 1) * 2;
		$dv += (int)substr($aux, 61, 1) * 3;
		$dv += (int)substr($aux, 62, 1) * 4;
		$dv += (int)substr($aux, 63, 1) * 5;
		$dv += (int)substr($aux, 64, 1) * 6;
		$dv += (int)substr($aux, 65, 1) * 7;
		$dv += (int)substr($aux, 66, 1) * 1;
		$dv += (int)substr($aux, 67, 1) * 2;
		$dv += (int)substr($aux, 68, 1) * 3;
		$dv += (int)substr($aux, 69, 1) * 4;
		$dv += (int)substr($aux, 70, 1) * 5;
		$dv += (int)substr($aux, 71, 1) * 6;
		$dv += (int)substr($aux, 72, 1) * 7;
		$dv += (int)substr($aux, 73, 1) * 1;
		$dv += (int)substr($aux, 74, 1) * 2;
		$dv += (int)substr($aux, 75, 1) * 3;
		$dv += (int)substr($aux, 76, 1) * 4;
		$dv += (int)substr($aux, 77, 1) * 5;
		
		$res = $dv % 78;
		// $res = $dv / 78 Este estaba mal;
		$res = (int)$res;
		if ((int)$res > 99) {
				$res = "00";     
		}
		if ((int)$res < 10) {
			$res = "0" . $res;     
		}
				
		$aux = $aux . $res;
		// Transfiero datos a variable
		$cod_barra = $aux;
			
		// insertamos los datos en la base
		$estado = "1";
		$activo = "1";
				
		// Armamos la clave
			
						
			//buscar si existe la imagen en el servidor 
			
			//$link2 = @mysql_connect("localhost", "cecposad_form", "lpi74en8b8ed") or die(@mysql_error($link2));
			//@mysql_select_db("cecposad_form", $link2) or die(@mysql_error($link2));
			
			
        $_pagi_sql = "select * from com_formularios where id_tipo_formulario = " .$tipo_formulario. " and id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " . $anio ;


        //var_dump($_pagi_sql);
        //exit();

		//$_pagi_res = mysql_query($_pagi_sql) or die (mysql_error());
 		$query = mysqli_query($coneccion ,	$_pagi_sql);
		$row = mysqli_fetch_array($query);
 
 		if ($row >= 1)				
 			//if (@mysql_affected_rows() >= 1)
		{
      	  $sql = "UPDATE com_formularios SET bancodepo = '". $bancodepo ."', cant_empleados = ". $total_emp .", imp_remuneracion = ". $total_remu .", cant_art_100 = ". $cant_art_100 .", imp_art_100 = ". $art_100 .", cant_art_101 = ". $cant_art_101 .", imp_art_101 = ". $total_art_101_porciento .", otros = '". $otros_aportes ."', imp_otros = ". $imp_otros_aportes .", tipo_pago = ". $tipo_pago .", enletras = '". $con_letra ."', bancopago = '". $banco_pago ."', nro_cheque = ". $cheque_pago .", fecha = '". $fecha ."', cod_barra = '". $aux ."', usuario_actualizacion = '". $usuario ."' WHERE id_empresa = ". $id_empresa . " and mes = " . $mes . " and anio = " .$anio ;

      				
      				//echo $sql;
      				//exit();
      				
      	  $query_update = mysqli_query($coneccion,$sql);
					
					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
			}
 		else 
		{
					
			$sql = "INSERT INTO com_formularios (id_tipo_formulario,  id_empresa, mes, anio, bancodepo, cant_empleados, imp_remuneracion, cant_art_100, imp_art_100, cant_art_101, imp_art_101, otros, imp_otros, tipo_pago, enletras, bancopago, nro_cheque, fecha, cod_barra, estado, activo, usuario_actualizacion)".
			               "VALUES ('$tipo_formulario', '$id_empresa', '$mes', '$anio', '$bancodepo', '$total_emp', '$total_remu', '$cant_art_100', '$art_100', '$cant_art_101', '$total_art_101_porciento', '$otros_aportes', '$imp_otros_aportes', '$tipo_pago', '$con_letra', '$banco_pago', '$cheque_pago', '$fecha', '$aux', '$estado', '$activo', '$usuario')";

			               
			               //echo $sq;
					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
			$query_update = mysqli_query($coneccion,$sql);				      
					
		}
			
		//@mysql_free_result($grabar);
		//@mysql_close($link2);

		mysqli_close($coneccion);
		if($tipo_pago == 3 or $tipo_pago == 4 ) {
		require_once("php/viacajero_nacion.php");
		}else
		{
		require_once("php/comprobante.php");
					// 
					//echo "<script type=\"text/javascript\">      
					//	  window.open('php/comprobante.php', '_blank')    
					//	 </script>";	

					//	 echo "<script LANGUAGE='JavaScript'>
        			//function redireccionar() {window.location='menu.php';}
        			//setTimeout ('redireccionar()', 0);
        			//</script>";				

					//header("Location: php/comprobante.php");
					//require_once("php/comprobante.php");
					
		}
	}			
	

if ($banco == 2) 
	{	
		// Armamos código de barra Pago Facil
		
		// Buscamo el nro en días
		
		$diahoy = date("d");
		$meshoy = date("m");
		$anioahora = (int) date("Y");
		$aniodosdig = (int)substr($anioahora, 2, 2);
		$fecha_inicio = "01/01/".$anioahora;
		$jd = GregorianToJD(01, 01, $anioahora); 
		//$hoy = GregorianToJD($meshoy, $diahoy, $anioahora); // Indicamos el valor de hoy
		$hoy = GregorianToJD(12,30, $anioahora); // Indicamos el valor de hoy
		
		
		$resta_dias = $hoy - $jd; // obtenemos el valor en día que corresponde a hoy 
		if (strlen($resta_dias) < 3) 
			{
				$resta_dias = str_pad($resta_dias, 3, "0", STR_PAD_LEFT);
			}
			
			$aux = '1561';
			$aux = $aux . str_pad($total_pagar_x, 8, "0", STR_PAD_LEFT);

			
			$aux = $aux . $aniodosdig;
			$aux = $aux . $resta_dias;
			// aca se debe agregar el mes de pago 
		if (strlen($mes) < 2) 
				{
				$aux = $aux . "0" ;
			}
			$aux = $aux . $mes;
			$aux = $aux . str_pad($cuit, 12, "0", STR_PAD_LEFT);
			$aux = $aux . '0';
			$aux = $aux . '000000';
			$aux = $aux . '00';
			
			
			
			
		// armamos codigo de control primer tramo
		
			$dv  = (int)substr($aux, 0, 1) * 1;
			$dv += (int)substr($aux, 1, 1) * 3;
			$dv += (int)substr($aux, 2, 1) * 5;
			$dv += (int)substr($aux, 3, 1) * 7;
			$dv += (int)substr($aux, 4, 1) * 9;
			$dv += (int)substr($aux, 5, 1) * 3;
			$dv += (int)substr($aux, 6, 1) * 5;
			$dv += (int)substr($aux, 7, 1) * 7;
			$dv += (int)substr($aux, 8, 1) * 9;
			$dv += (int)substr($aux, 9, 1) * 3;
			$dv += (int)substr($aux, 10, 1) * 5;
			$dv += (int)substr($aux, 11, 1) * 7;
			$dv += (int)substr($aux, 12, 1) * 9;
			$dv += (int)substr($aux, 13, 1) * 3;
			$dv += (int)substr($aux, 14, 1) * 5;
			$dv += (int)substr($aux, 15, 1) * 7;
			$dv += (int)substr($aux, 16, 1) * 9;
			$dv += (int)substr($aux, 17, 1) * 3;
			$dv += (int)substr($aux, 18, 1) * 5;
			$dv += (int)substr($aux, 19, 1) * 7;
			$dv += (int)substr($aux, 20, 1) * 9;
			$dv += (int)substr($aux, 21, 1) * 3;
			$dv += (int)substr($aux, 22, 1) * 5;
			$dv += (int)substr($aux, 23, 1) * 7;
			$dv += (int)substr($aux, 24, 1) * 9;
			$dv += (int)substr($aux, 25, 1) * 3;
			$dv += (int)substr($aux, 26, 1) * 5;
			$dv += (int)substr($aux, 27, 1) * 7;
			$dv += (int)substr($aux, 28, 1) * 9;
			$dv += (int)substr($aux, 29, 1) * 3;
			$dv += (int)substr($aux, 30, 1) * 5;
			$dv += (int)substr($aux, 31, 1) * 7;
			$dv += (int)substr($aux, 32, 1) * 9;
			$dv += (int)substr($aux, 33, 1) * 3;
			$dv += (int)substr($aux, 34, 1) * 5;
			$dv += (int)substr($aux, 35, 1) * 7;
			$dv += (int)substr($aux, 36, 1) * 9;
			$dv += (int)substr($aux, 37, 1) * 3;
			$dv += (int)substr($aux, 38, 1) * 5;
			$dv += (int)substr($aux, 39, 1) * 7;
		
			
			$res1 = $dv / 2;
			$res1 = (int)$res1;
			
			$res2 = $res1 % 10;
			
			//////////////////
			//$resto1 = explode(".", $res2);


			//ECHO $resto1[1];

			$aux = $aux .$res2; //$resto1[1];

			////////////////
	
			
		// armamos codigo de control segundo tramo
		
			$dv  = (int)substr($aux, 0, 1) * 1;
			$dv += (int)substr($aux, 1, 1) * 3;
			$dv += (int)substr($aux, 2, 1) * 5;
			$dv += (int)substr($aux, 3, 1) * 7;
			$dv += (int)substr($aux, 4, 1) * 9;
			$dv += (int)substr($aux, 5, 1) * 3;
			$dv += (int)substr($aux, 6, 1) * 5;
			$dv += (int)substr($aux, 7, 1) * 7;
			$dv += (int)substr($aux, 8, 1) * 9;
			$dv += (int)substr($aux, 9, 1) * 3;
			$dv += (int)substr($aux, 10, 1) * 5;
			$dv += (int)substr($aux, 11, 1) * 7;
			$dv += (int)substr($aux, 12, 1) * 9;
			$dv += (int)substr($aux, 13, 1) * 3;
			$dv += (int)substr($aux, 14, 1) * 5;
			$dv += (int)substr($aux, 15, 1) * 7;
			$dv += (int)substr($aux, 16, 1) * 9;
			$dv += (int)substr($aux, 17, 1) * 3;
			$dv += (int)substr($aux, 18, 1) * 5;
			$dv += (int)substr($aux, 19, 1) * 7;
			$dv += (int)substr($aux, 20, 1) * 9;
			$dv += (int)substr($aux, 21, 1) * 3;
			$dv += (int)substr($aux, 22, 1) * 5;
			$dv += (int)substr($aux, 23, 1) * 7;
			$dv += (int)substr($aux, 24, 1) * 9;
			$dv += (int)substr($aux, 25, 1) * 3;
			$dv += (int)substr($aux, 26, 1) * 5;
			$dv += (int)substr($aux, 27, 1) * 7;
			$dv += (int)substr($aux, 28, 1) * 9;
			$dv += (int)substr($aux, 29, 1) * 3;
			$dv += (int)substr($aux, 30, 1) * 5;
			$dv += (int)substr($aux, 31, 1) * 7;
			$dv += (int)substr($aux, 32, 1) * 9;
			$dv += (int)substr($aux, 33, 1) * 3;
			$dv += (int)substr($aux, 34, 1) * 5;
			$dv += (int)substr($aux, 35, 1) * 7;
			$dv += (int)substr($aux, 36, 1) * 9;
			$dv += (int)substr($aux, 37, 1) * 3;
			$dv += (int)substr($aux, 38, 1) * 5;
			$dv += (int)substr($aux, 39, 1) * 7;
			$dv += (int)substr($aux, 40, 1) * 9;
		
			
			$res3 = $dv / 2;
			$res3 = (int)$res3;
			
			$res4 = $res3 % 10;
			
			//////////////////
			//ECHO "Antes del explode"  . $res4;
			//$resto2 = explode(".", $res4);
			////////////////
	
			//ECHO " Despues del explode" . $resto2[1];

			$aux = $aux .  $res4;
			


			// Transfiero datos a variable
			$cod_barra = $aux;
			
			//echo $aux;
			//exit;
			
			// insertamos los datos en la base
			$estado = "1";
			$activo = "1";
			
			// Armamos la clave
			
			//$clacombi = "1".$id_empresa.$mes.$anio;  Yo no uso esto
			
			//buscar si existe la imagen en el servidor 
			
			//$link2 = @mysql_connect("localhost", "cecposad_form", "lpi74en8b8ed") or die(@mysql_error($link2));
			//@mysql_select_db("cecposad_form", $link2) or die(@mysql_error($link2));
			
 			
			$_pagi_sql = "select * from com_formularios where id_tipo_formulario = " .$tipo_formulario. " and id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " . $anio ;



  			//$_pagi_res = mysql_query($_pagi_sql) or die (mysql_error());
			
			$query = mysqli_query($coneccion ,	$_pagi_sql);
			$row = mysqli_fetch_array($query);


			
 			//if (@mysql_affected_rows() >= 1)
 			if($row >= 1)
				{
      				$sql = 'UPDATE com_formularios SET bancodepo = "'. $bancodepo .'", cant_empleados = "'. $total_emp .'", imp_remuneracion = "'. $total_remu .'", cant_art_100 = "'. $cant_art_100 .'", imp_art_100 = "'. $art_100 .'", cant_art_101 = "'. $cant_art_101 .'", imp_art_101 = "'. $total_art_101_porciento .'", otros = "'. $otros_aportes .'", imp_otros = "'. $imp_otros_aportes .'", tipo_pago = "'. $tipo_pago .'", enletras = "'. $con_letra .'", bancopago = "'. $banco_pago .'", nro_cheque = "'. $cheque_pago .'", fecha = "'. $fecha .'", cod_barra = "'. $aux .'", usuario_actualizacion = "'. $usuario .'" WHERE id_empresa = ' . $id_empresa . ' and mes = ' . $mes . ' and anio = ' .$anio ;

					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
					 $query_update = mysqli_query($coneccion,$sql);
				}
 				else 
				{	
					
					$sql = "INSERT INTO com_formularios (id_tipo_formulario,  id_empresa, mes, anio, bancodepo, cant_empleados, imp_remuneracion, cant_art_100, imp_art_100, cant_art_101, imp_art_101, otros, imp_otros, tipo_pago, enletras, bancopago, nro_cheque, fecha, cod_barra, estado, activo, usuario_actualizacion)".
			               "VALUES ('$tipo_formulario', '$id_empresa', '$mes', '$anio', '$bancodepo', '$total_emp', '$total_remu', '$cant_art_100', '$art_100', '$cant_art_101', '$total_art_101_porciento', '$otros_aportes', '$imp_otros_aportes', '$tipo_pago', '$con_letra', '$banco_pago', '$cheque_pago', '$fecha', '$aux', '$estado', '$activo', '$usuario')";

			        
					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
			        $query_update = mysqli_query($coneccion,$sql);
					
				}
			
			 	//@mysql_free_result($grabar);
			 	//@mysql_close($link2);
				
				mysqli_close($coneccion);
				require_once("php/comprobante.php");				
	}

 //$grab = @mysql_query($sql, $link2) or die(@mysql_error($link2));
 //@mysql_free_result($grab);
 //@mysql_close($link2;



if ($banco == 3) 
	{	
		// Armamos código de barra Pago Facil el mismo lo usamos para deposito via cbu
		
		// Buscamo el nro en días
		
		$diahoy = date("d");
		$meshoy = date("m");
		$anioahora = (int) date("Y");
		$aniodosdig = (int)substr($anioahora, 2, 2);
		$fecha_inicio = "01/01/".$anioahora;
		$jd = GregorianToJD(01, 01, $anioahora); 
		//$hoy = GregorianToJD($meshoy, $diahoy, $anioahora); // Indicamos el valor de hoy
		$hoy = GregorianToJD(12,30, $anioahora); // Indicamos el valor de hoy
		
		
		$resta_dias = $hoy - $jd; // obtenemos el valor en día que corresponde a hoy 
		if (strlen($resta_dias) < 3) 
			{
				$resta_dias = str_pad($resta_dias, 3, "0", STR_PAD_LEFT);
			}
			
			$aux = '0285';
			$aux = $aux . str_pad($total_pagar_x, 8, "0", STR_PAD_LEFT);

			
			$aux = $aux . $aniodosdig;
			$aux = $aux . $resta_dias;
			// aca se debe agregar el mes de pago 
			if (strlen($mes) < 2) 
				{
				$aux = $aux . "0" ;
			}
			$aux = $aux . $mes;
			$aux = $aux . str_pad($cuit, 12, "0", STR_PAD_LEFT);
			$aux = $aux . '0';
			$aux = $aux . '000000';
			$aux = $aux . '00';		
			
			
		// armamos codigo de control primer tramo
		
			$dv  = (int)substr($aux, 0, 1) * 1;
			$dv += (int)substr($aux, 1, 1) * 3;
			$dv += (int)substr($aux, 2, 1) * 5;
			$dv += (int)substr($aux, 3, 1) * 7;
			$dv += (int)substr($aux, 4, 1) * 9;
			$dv += (int)substr($aux, 5, 1) * 3;
			$dv += (int)substr($aux, 6, 1) * 5;
			$dv += (int)substr($aux, 7, 1) * 7;
			$dv += (int)substr($aux, 8, 1) * 9;
			$dv += (int)substr($aux, 9, 1) * 3;
			$dv += (int)substr($aux, 10, 1) * 5;
			$dv += (int)substr($aux, 11, 1) * 7;
			$dv += (int)substr($aux, 12, 1) * 9;
			$dv += (int)substr($aux, 13, 1) * 3;
			$dv += (int)substr($aux, 14, 1) * 5;
			$dv += (int)substr($aux, 15, 1) * 7;
			$dv += (int)substr($aux, 16, 1) * 9;
			$dv += (int)substr($aux, 17, 1) * 3;
			$dv += (int)substr($aux, 18, 1) * 5;
			$dv += (int)substr($aux, 19, 1) * 7;
			$dv += (int)substr($aux, 20, 1) * 9;
			$dv += (int)substr($aux, 21, 1) * 3;
			$dv += (int)substr($aux, 22, 1) * 5;
			$dv += (int)substr($aux, 23, 1) * 7;
			$dv += (int)substr($aux, 24, 1) * 9;
			$dv += (int)substr($aux, 25, 1) * 3;
			$dv += (int)substr($aux, 26, 1) * 5;
			$dv += (int)substr($aux, 27, 1) * 7;
			$dv += (int)substr($aux, 28, 1) * 9;
			$dv += (int)substr($aux, 29, 1) * 3;
			$dv += (int)substr($aux, 30, 1) * 5;
			$dv += (int)substr($aux, 31, 1) * 7;
			$dv += (int)substr($aux, 32, 1) * 9;
			$dv += (int)substr($aux, 33, 1) * 3;
			$dv += (int)substr($aux, 34, 1) * 5;
			$dv += (int)substr($aux, 35, 1) * 7;
			$dv += (int)substr($aux, 36, 1) * 9;
			$dv += (int)substr($aux, 37, 1) * 3;
			$dv += (int)substr($aux, 38, 1) * 5;
			$dv += (int)substr($aux, 39, 1) * 7;
		
			
			$res1 = $dv / 2;
			$res1 = (int)$res1;
			
			$res2 = $res1 % 10;
			
			//////////////////
			//$resto1 = explode(".", $res2);


			//ECHO $resto1[1];

			$aux = $aux .$res2; //$resto1[1];

			////////////////
	
			
		// armamos codigo de control segundo tramo
		
			$dv  = (int)substr($aux, 0, 1) * 1;
			$dv += (int)substr($aux, 1, 1) * 3;
			$dv += (int)substr($aux, 2, 1) * 5;
			$dv += (int)substr($aux, 3, 1) * 7;
			$dv += (int)substr($aux, 4, 1) * 9;
			$dv += (int)substr($aux, 5, 1) * 3;
			$dv += (int)substr($aux, 6, 1) * 5;
			$dv += (int)substr($aux, 7, 1) * 7;
			$dv += (int)substr($aux, 8, 1) * 9;
			$dv += (int)substr($aux, 9, 1) * 3;
			$dv += (int)substr($aux, 10, 1) * 5;
			$dv += (int)substr($aux, 11, 1) * 7;
			$dv += (int)substr($aux, 12, 1) * 9;
			$dv += (int)substr($aux, 13, 1) * 3;
			$dv += (int)substr($aux, 14, 1) * 5;
			$dv += (int)substr($aux, 15, 1) * 7;
			$dv += (int)substr($aux, 16, 1) * 9;
			$dv += (int)substr($aux, 17, 1) * 3;
			$dv += (int)substr($aux, 18, 1) * 5;
			$dv += (int)substr($aux, 19, 1) * 7;
			$dv += (int)substr($aux, 20, 1) * 9;
			$dv += (int)substr($aux, 21, 1) * 3;
			$dv += (int)substr($aux, 22, 1) * 5;
			$dv += (int)substr($aux, 23, 1) * 7;
			$dv += (int)substr($aux, 24, 1) * 9;
			$dv += (int)substr($aux, 25, 1) * 3;
			$dv += (int)substr($aux, 26, 1) * 5;
			$dv += (int)substr($aux, 27, 1) * 7;
			$dv += (int)substr($aux, 28, 1) * 9;
			$dv += (int)substr($aux, 29, 1) * 3;
			$dv += (int)substr($aux, 30, 1) * 5;
			$dv += (int)substr($aux, 31, 1) * 7;
			$dv += (int)substr($aux, 32, 1) * 9;
			$dv += (int)substr($aux, 33, 1) * 3;
			$dv += (int)substr($aux, 34, 1) * 5;
			$dv += (int)substr($aux, 35, 1) * 7;
			$dv += (int)substr($aux, 36, 1) * 9;
			$dv += (int)substr($aux, 37, 1) * 3;
			$dv += (int)substr($aux, 38, 1) * 5;
			$dv += (int)substr($aux, 39, 1) * 7;
			$dv += (int)substr($aux, 40, 1) * 9;
		
			
			$res3 = $dv / 2;
			$res3 = (int)$res3;
			
			$res4 = $res3 % 10;
			
			//////////////////
			//ECHO "Antes del explode"  . $res4;
			//$resto2 = explode(".", $res4);
			////////////////
	
			//ECHO " Despues del explode" . $resto2[1];

			$aux = $aux .  $res4;
			


			// Transfiero datos a variable
			$cod_barra = $aux;
			
			// insertamos los datos en la base
			$estado = "1";
			$activo = "1";
			
						
			//buscar si existe la imagen en el servidor 
			
			//$link2 = @mysql_connect("localhost", "cecposad_form", "lpi74en8b8ed") or die(@mysql_error($link2));

			//$link2 = @mysql_connect("localhost", "root", "rono1303") or die(@mysql_error($link2));


			//@mysql_select_db("cecposad_form", $link2) or die(@mysql_error($link2));
			
 			$_pagi_sql = "select * from com_formularios where id_tipo_formulario = " .$tipo_formulario. " and id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " . $anio ;


	  	  //$_pagi_res = mysql_query($_pagi_sql) or die (mysql_error());	
			
  			$query = mysqli_query($coneccion ,	$_pagi_sql);
			$row = mysqli_fetch_array($query);


			


 			//if (@mysql_affected_rows() >= 1)

 			if($row >= 1)
				{
      				$sql = "UPDATE com_formularios SET bancodepo = '". $bancodepo ."', cant_empleados = ". $total_emp .", imp_remuneracion = ". $total_remu .", cant_art_100 = ". $cant_art_100 .", imp_art_100 = ". $art_100 .", cant_art_101 = ". $cant_art_101 .", imp_art_101 = ". $total_art_101_porciento .", otros = '". $otros_aportes ."', imp_otros = ". $imp_otros_aportes .", tipo_pago = ". $tipo_pago .", enletras = '". $con_letra ."', bancopago = '". $banco_pago ."', nro_cheque = ". $cheque_pago .", fecha = '". $fecha ."', cod_barra = '". $aux ."', usuario_actualizacion = '". $usuario ."' WHERE id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " .$anio ;

					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
					//

					//var_dump($sql);
					//exit();
					
					$query_update = mysqli_query($coneccion,$sql);
				}
 				else 
				{	
					
					$sql = "INSERT INTO com_formularios (id_tipo_formulario,  id_empresa, mes, anio, bancodepo, cant_empleados, imp_remuneracion, cant_art_100, imp_art_100, cant_art_101, imp_art_101, otros, imp_otros, tipo_pago, enletras, bancopago, nro_cheque, fecha, cod_barra, estado, activo, usuario_actualizacion)".
			               "VALUES ('$tipo_formulario', '$id_empresa', '$mes', '$anio', '$bancodepo', '$total_emp', '$total_remu', '$cant_art_100', '$art_100', '$cant_art_101', '$total_art_101_porciento', '$otros_aportes', '$imp_otros_aportes', '$tipo_pago', '$con_letra', '$banco_pago', '$cheque_pago', '$fecha', '$aux', '$estado', '$activo', '$usuario')";

			        
					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
					$query_update = mysqli_query($coneccion,$sql);
					
				}
			
			 	//@mysql_free_result($grabar);
			 	//@mysql_close($link2);
				
				
				//require_once("php/comprobante.php");
				mysqli_close($coneccion);
				//require_once("php/comprobante.php");	
				if ($banco == 1){
					require_once("php/viacajero_nacion.php");
				}else{
				require_once("php/viacajero_macro.php");	
				}	
				

	}

if ($banco == 4) 
	{	
		// Armamos código de barra Pago Facil el mismo lo usamos para deposito via cbu
		
		// Buscamo el nro en días
		
		$diahoy = date("d");
		$meshoy = date("m");
		$anioahora = (int) date("Y");
		$aniodosdig = (int)substr($anioahora, 2, 2);
		$fecha_inicio = "01/01/".$anioahora;
		$jd = GregorianToJD(01, 01, $anioahora); 
		//$hoy = GregorianToJD($meshoy, $diahoy, $anioahora); // Indicamos el valor de hoy
		$hoy = GregorianToJD(12,30, $anioahora); // Indicamos el valor de hoy
		
		
		$resta_dias = $hoy - $jd; // obtenemos el valor en día que corresponde a hoy 
		if (strlen($resta_dias) < 3) 
			{
				$resta_dias = str_pad($resta_dias, 3, "0", STR_PAD_LEFT);
			}
			
			$aux = '1561';
			$aux = $aux . str_pad($total_pagar_x, 8, "0", STR_PAD_LEFT);

			
			$aux = $aux . $aniodosdig;
			$aux = $aux . $resta_dias;
			// aca se debe agregar el mes de pago 
			if (strlen($mes) < 2) 
				{
				$aux = $aux . "0" ;
			}
			$aux = $aux . $mes;
			$aux = $aux . str_pad($cuit, 12, "0", STR_PAD_LEFT);
			$aux = $aux . '0';
			$aux = $aux . '000000';
			$aux = $aux . '00';		
			
			
		// armamos codigo de control primer tramo
		
			$dv  = (int)substr($aux, 0, 1) * 1;
			$dv += (int)substr($aux, 1, 1) * 3;
			$dv += (int)substr($aux, 2, 1) * 5;
			$dv += (int)substr($aux, 3, 1) * 7;
			$dv += (int)substr($aux, 4, 1) * 9;
			$dv += (int)substr($aux, 5, 1) * 3;
			$dv += (int)substr($aux, 6, 1) * 5;
			$dv += (int)substr($aux, 7, 1) * 7;
			$dv += (int)substr($aux, 8, 1) * 9;
			$dv += (int)substr($aux, 9, 1) * 3;
			$dv += (int)substr($aux, 10, 1) * 5;
			$dv += (int)substr($aux, 11, 1) * 7;
			$dv += (int)substr($aux, 12, 1) * 9;
			$dv += (int)substr($aux, 13, 1) * 3;
			$dv += (int)substr($aux, 14, 1) * 5;
			$dv += (int)substr($aux, 15, 1) * 7;
			$dv += (int)substr($aux, 16, 1) * 9;
			$dv += (int)substr($aux, 17, 1) * 3;
			$dv += (int)substr($aux, 18, 1) * 5;
			$dv += (int)substr($aux, 19, 1) * 7;
			$dv += (int)substr($aux, 20, 1) * 9;
			$dv += (int)substr($aux, 21, 1) * 3;
			$dv += (int)substr($aux, 22, 1) * 5;
			$dv += (int)substr($aux, 23, 1) * 7;
			$dv += (int)substr($aux, 24, 1) * 9;
			$dv += (int)substr($aux, 25, 1) * 3;
			$dv += (int)substr($aux, 26, 1) * 5;
			$dv += (int)substr($aux, 27, 1) * 7;
			$dv += (int)substr($aux, 28, 1) * 9;
			$dv += (int)substr($aux, 29, 1) * 3;
			$dv += (int)substr($aux, 30, 1) * 5;
			$dv += (int)substr($aux, 31, 1) * 7;
			$dv += (int)substr($aux, 32, 1) * 9;
			$dv += (int)substr($aux, 33, 1) * 3;
			$dv += (int)substr($aux, 34, 1) * 5;
			$dv += (int)substr($aux, 35, 1) * 7;
			$dv += (int)substr($aux, 36, 1) * 9;
			$dv += (int)substr($aux, 37, 1) * 3;
			$dv += (int)substr($aux, 38, 1) * 5;
			$dv += (int)substr($aux, 39, 1) * 7;
		
			
			$res1 = $dv / 2;
			$res1 = (int)$res1;
			
			$res2 = $res1 % 10;
			
			//////////////////
			//$resto1 = explode(".", $res2);


			//ECHO $resto1[1];

			$aux = $aux .$res2; //$resto1[1];

			////////////////
	
			
		// armamos codigo de control segundo tramo
		
			$dv  = (int)substr($aux, 0, 1) * 1;
			$dv += (int)substr($aux, 1, 1) * 3;
			$dv += (int)substr($aux, 2, 1) * 5;
			$dv += (int)substr($aux, 3, 1) * 7;
			$dv += (int)substr($aux, 4, 1) * 9;
			$dv += (int)substr($aux, 5, 1) * 3;
			$dv += (int)substr($aux, 6, 1) * 5;
			$dv += (int)substr($aux, 7, 1) * 7;
			$dv += (int)substr($aux, 8, 1) * 9;
			$dv += (int)substr($aux, 9, 1) * 3;
			$dv += (int)substr($aux, 10, 1) * 5;
			$dv += (int)substr($aux, 11, 1) * 7;
			$dv += (int)substr($aux, 12, 1) * 9;
			$dv += (int)substr($aux, 13, 1) * 3;
			$dv += (int)substr($aux, 14, 1) * 5;
			$dv += (int)substr($aux, 15, 1) * 7;
			$dv += (int)substr($aux, 16, 1) * 9;
			$dv += (int)substr($aux, 17, 1) * 3;
			$dv += (int)substr($aux, 18, 1) * 5;
			$dv += (int)substr($aux, 19, 1) * 7;
			$dv += (int)substr($aux, 20, 1) * 9;
			$dv += (int)substr($aux, 21, 1) * 3;
			$dv += (int)substr($aux, 22, 1) * 5;
			$dv += (int)substr($aux, 23, 1) * 7;
			$dv += (int)substr($aux, 24, 1) * 9;
			$dv += (int)substr($aux, 25, 1) * 3;
			$dv += (int)substr($aux, 26, 1) * 5;
			$dv += (int)substr($aux, 27, 1) * 7;
			$dv += (int)substr($aux, 28, 1) * 9;
			$dv += (int)substr($aux, 29, 1) * 3;
			$dv += (int)substr($aux, 30, 1) * 5;
			$dv += (int)substr($aux, 31, 1) * 7;
			$dv += (int)substr($aux, 32, 1) * 9;
			$dv += (int)substr($aux, 33, 1) * 3;
			$dv += (int)substr($aux, 34, 1) * 5;
			$dv += (int)substr($aux, 35, 1) * 7;
			$dv += (int)substr($aux, 36, 1) * 9;
			$dv += (int)substr($aux, 37, 1) * 3;
			$dv += (int)substr($aux, 38, 1) * 5;
			$dv += (int)substr($aux, 39, 1) * 7;
			$dv += (int)substr($aux, 40, 1) * 9;
		
			
			$res3 = $dv / 2;
			$res3 = (int)$res3;
			
			$res4 = $res3 % 10;
			
			//////////////////
			//ECHO "Antes del explode"  . $res4;
			//$resto2 = explode(".", $res4);
			////////////////
	
			//ECHO " Despues del explode" . $resto2[1];

			$aux = $aux .  $res4;
			


			// Transfiero datos a variable
			$cod_barra = $aux;
			
			// insertamos los datos en la base
			$estado = "1";
			$activo = "1";
			
						
			//buscar si existe la imagen en el servidor 
			
			//$link2 = @mysql_connect("localhost", "cecposad_form", "lpi74en8b8ed") or die(@mysql_error($link2));

			//$link2 = @mysql_connect("localhost", "root", "rono1303") or die(@mysql_error($link2));


			//@mysql_select_db("cecposad_form", $link2) or die(@mysql_error($link2));
			
 			$_pagi_sql = "select * from com_formularios where id_tipo_formulario = " .$tipo_formulario. " and id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " . $anio ;


	  	  //$_pagi_res = mysql_query($_pagi_sql) or die (mysql_error());	
			
  			$query = mysqli_query($coneccion ,	$_pagi_sql);
			$row = mysqli_fetch_array($query);


			
 			//if (@mysql_affected_rows() >= 1)

 			if($row >= 1)
				{
      				$sql = "UPDATE com_formularios SET bancodepo = '". $bancodepo ."', cant_empleados = ". $total_emp .", imp_remuneracion = ". $total_remu .", cant_art_100 = ". $cant_art_100 .", imp_art_100 = ". $art_100 .", cant_art_101 = ". $cant_art_101 .", imp_art_101 = ". $total_art_101_porciento .", otros = '". $otros_aportes ."', imp_otros = ". $imp_otros_aportes .", tipo_pago = ". $tipo_pago .", enletras = '". $con_letra ."', bancopago = '". $banco_pago ."', nro_cheque = ". $cheque_pago .", fecha = '". $fecha ."', cod_barra = '". $aux ."', usuario_actualizacion = '". $usuario ."' WHERE id_empresa = " . $id_empresa . " and mes = " . $mes . " and anio = " .$anio ;

					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
					//
					$query_update = mysqli_query($coneccion,$sql);
				}
 				else 
				{	
					
					$sql = "INSERT INTO com_formularios (id_tipo_formulario,  id_empresa, mes, anio, bancodepo, cant_empleados, imp_remuneracion, cant_art_100, imp_art_100, cant_art_101, imp_art_101, otros, imp_otros, tipo_pago, enletras, bancopago, nro_cheque, fecha, cod_barra, estado, activo, usuario_actualizacion)".
			               "VALUES ('$tipo_formulario', '$id_empresa', '$mes', '$anio', '$bancodepo', '$total_emp', '$total_remu', '$cant_art_100', '$art_100', '$cant_art_101', '$total_art_101_porciento', '$otros_aportes', '$imp_otros_aportes', '$tipo_pago', '$con_letra', '$banco_pago', '$cheque_pago', '$fecha', '$aux', '$estado', '$activo', '$usuario')";

			        
					//$grabar = @mysql_query($sql, $link2) or die(@mysql_error($link2));
					$query_update = mysqli_query($coneccion,$sql);
					
				}
			
			 	//@mysql_free_result($grabar);
			 	//@mysql_close($link2);
				
				
				//require_once("php/comprobante.php");
				mysqli_close($coneccion);
				//require_once("php/comprobante.php");		
				//require_once("php/viacbu.php");
				if ($banco == 1){
					require_once("php/viacajero_nacion.php");
				}else{
					require_once("php/viacajero_macro.php");	
				}	
						
			
		}

}  

 //$grab = @mysql_query($sql, $link2) or die(@mysql_error($link2));
 //@mysql_free_result($grab);
 //@mysql_close($link2;

?>
