<?php
  date_default_timezone_set('America/Costa_Rica');

	include ("../conexion.php");
 

echo $numrec=$_GET["numrec"];
echo $horaactual = date("h:i:s A");

   
    $consulta = "SELECT * FROM web_0000 ";
	$resultado = $mysqli->query($consulta);
	$fila = $resultado->fetch_array();

         $nomcom = $fila["NOMCOM"];
	     $razsoc = $fila["RAZSOC"];
		 $cedjur = $fila["CEDJUR"];
		 $diremp = $fila["DIREMP"];
		 $numtel_empresa = $fila["NUMTEL"];
		 $numofi = $fila["numofi"];
		 $menfac = $fila["menfac"];
         $intmor = $fila["INTMOR"];
		 $tipimp = $fila["TIPIMP"];
		 $ter001 = $fila["TER001"];
		 $ter002 = $fila["TER002"];
		 $ter003 = $fila["TER003"];
		  



   $consulta ="Select * from pagos_clientes where numrec = $numrec";
	  $result = $mysqli->query($consulta);
      $row = $result->fetch_array();


        $fecha  = $row["fecrec"];
		$fecrec = implode("-", array_reverse(explode("-", $fecha)));
		$numfac = $row["numfac"];
		$cdgcli = $row["cdgcli"];
		$dsccli = $row["dsccli"];
        $pagcon = $row["pagcon"];		
		$montot = $row["montot"];
    	$cdgusu = $row["cdgcaj"];
		$vuelto = $row["vuelto"];
		$monint = $row["monint"];
		$monabo = $row["monabo"];
		$nuevosaldo   = $fila["nuevosaldo"];
		


		
	  $usuario = "SELECT * FROM usuarios WHERE codusu='$cdgusu'";
	  $result3 = $mysqli->query($usuario);
      $row3 = $result3->fetch_array();

     $cajero  = $row3["usu"];		

      $cliente ="Select * from web_0301 where cdgcli= $cdgcli";
	  $result2 = $mysqli->query($cliente);
      $row2 = $result2->fetch_array();

      $saldo_cliente = $row2["SALACT"];
	  
	  
	   //////////////////////   MUESTRA EL NUEVO SALDO DEL CLIENTE  /////////////////////////

$consulta4 = "SELECT SALACT, FECVEN FROM web_2101 WHERE cdgcli='$cdgcli' AND salact > 0 ";
if ($resultado4 = $mysqli->query($consulta4)) 
$contador=0;

 while ($fila = $resultado4->fetch_assoc()) {
 

	$hoy=date("y-m-d");
	
       $salact      = $fila["SALACT"];
        $fecven    = $fila["FECVEN"];
		
	

		$segundos=strtotime($hoy) - strtotime($fecven);
		$dias=intval($segundos/60/60/24);
	
		if 	($dias  > 0 ) {
		$taza=$tazint/100;
		$interes=$salact*$taza;
		$saldo=$interes/30;
		$monint=$saldo*$dias;
		$monint=round($monint,2);
		} else {
		$monint=0;
		
		}
			$contador++;
			$total_saldo=$salact + $total_saldo;
		    $total_interes = $monint + $total_interes;
	}
	$salact= $total_saldo + $total_interes;
	$saldo_anterior=round($salact);


 $saldo_anterior  =   $nuevosaldo - $montot   ; 
		
		
		
echo  $ip = $_SERVER["REMOTE_ADDR"];

if ($ip=="192.168.1.13"){
 $puerto="LPT1";



  } else {
  
  echo $puerto="LPT1";



  }  


//if(($handle = @fopen("http://192.168.1.4/", "r")) === FALSE){


if(($handle = @fopen($puerto  , "w")) === FALSE){
  		 echo exec('Impresoras');
	
        die('No se puede Imprimir, Verifique la conexion con la IMPRESORA'); // Revisa la conexion con la impresora
    }
setlocale(LC_TIME, 'spanish');


fwrite($handle,chr(27). chr(64));//reinicio


//Salida: viernes 24 de febrero del 2012


fwrite($handle, chr(27). chr(112). chr(48));//ABRIR EL CAJON


if ($tipimp=="MATRIX"){  //  ************************Si la impresora es TIPO MATRIX ****************************///////////////



fwrite($handle,  chr(27). chr(20). chr(0));//salto de linea VACIO
fwrite($handle,  chr(27). chr(100). chr(0));//salto de linea VACIO
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(32). chr(1));//ESTACIO ENTRE 
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle, chr(27). chr(33). chr(22));//ESTACIO ENTRE LETRAS
//fwrite($handle, chr(27). chr(72). chr(5));//Color Rojo

fwrite($handle, $nomcom);
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS
//fwrite($handle, chr(27) +"3"+ chr(17));//interlienaso
//fwrite($handle, chr(27). chr(72). chr(0));//Color Rojo
fwrite($handle, chr(27). chr(3). chr(17));//salto de linea VACIO
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(33). chr(9));//Tamaño letra 10 
fwrite($handle, $razsoc);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Cedula : ". $cedjur);
//fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
//fwrite($handle, $diremp);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Tel. ".$numtel_empresa);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea

fwrite($handle,"----------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS *revisar *******
fwrite($handle, chr(27). chr(97). chr(2));//derecha

	  $recibo = str_pad("N. Recibo: ".$numrec, 23);
	    $cajrec = str_pad("Cajero: " . $cajero , 40);
fwrite($handle,$recibo);
fwrite($handle,"Fecha: ". $fecha);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle,"Cliente: " . $cdgcli ."-".$dsccli);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//derecha
fwrite($handle, $cajrec);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea

fwrite($handle,"----------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
fwrite($handle,"Factura". "      Monto Pagado"."      Saldo");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"----------------------------------------");
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS *************
fwrite($handle, chr(27). chr(97). chr(0));//IzquierdERRORERR21++a
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha

	 
	$consulta3 = "SELECT * FROM pagos_clientes_recibos_lineas WHERE numrec='$numrec' ORDER BY numlinea ASC";
if ($resultado3 = $mysqli->query($consulta3)) 
$contador=0;

		 while ($fila = $resultado3->fetch_assoc()) {

        $numlinea = $fila["numlinea"];
        $salfac   = $fila["salfac"];
        $numfac   = substr($fila["numfac"],-10);
		$monabo   = $fila["monabo"];
		$totfac   = $fila["totfac"];
		$monint   = $fila["monint"];
		$intfac   = $fila["intfac"];
	

	 
	
$resultado=number_format($salfac,2,".",",");
$saldo_factura=sprintf("%13s",$resultado) ;

$factura = str_pad($numfac, 10); 
$pago=number_format($monabo,2,".",",");
 $abono = str_pad($pago, 15);
   
fwrite($handle, chr(27). chr(97). chr(2));//derecha		
fwrite($handle, $factura ." ".$abono."".$saldo_factura);		
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea								 
fwrite($handle, chr(27). chr(97). chr(1));//centrado

}

fwrite($handle,"----------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
$total_recibo=number_format($montot,2,".",",");
$total_pago=sprintf("%13s",$total_recibo) ;


fwrite($handle, chr(27). chr(33). chr(22));//ESTACIO ENTRE LETRAS
fwrite($handle,"TOTAL:  ");
fwrite($handle,$total_pago);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
fwrite($handle, chr(27). chr(33). chr(9));//Tamaño letra 10 

$monto=number_format($pagcon,2,".",",");
$pagado=sprintf("%16s",$monto) ;
fwrite($handle,"Paga con:  ");
fwrite($handle,$pagado);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
$cambio=number_format($vuelto,2,".",",");
$saldo=sprintf("%16s",$cambio) ;
fwrite($handle,"Vuelto:  ");
fwrite($handle,$saldo);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle,"----------------------------------------");

fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
$saldo_ant=number_format($saldo_anterior,2,".",",");
$saldo_ant=sprintf("%16s",$saldo_ant) ;
fwrite($handle,"Saldo Anterior:   ");
fwrite($handle,$saldo_ant);

fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
$mon_abono=number_format($montot,2,".",",");
$mon_abono=sprintf("%16s",$mon_abono) ;
fwrite($handle,"Monto Abono:   ");
fwrite($handle,$mon_abono);


fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
$saldo_cli=number_format($total_saldo,2,".",",");
$saldo_cliente=sprintf("%16s",$saldo_cli) ;
fwrite($handle,"Saldo Total:   ");
fwrite($handle,$saldo_cliente);

fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, "----------------------------------------");
fwrite($handle, chr(27). chr(100). chr(5));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle,"________________________________________");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Firma y sello del cajero"  );
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,$horaactual);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Gracias Por su pago"  );
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(109). chr(0));//salto de linea
fwrite($handle, chr(29). chr(27). chr(105));//Corta el Papel
//fwrite($handle, chr(29). chr(86). chr(49));//Corta el Papel


}      //**********************************  Cierra tipo de impresora matrix ****************************************//


if ($tipimp=="TERMICA"){  //  ************************Si la impresora es TIPO TERMICA ****************************///////////////

fwrite($handle,chr(27). chr(64));//reinicio


//Salida: viernes 24 de febrero del 2012


fwrite($handle, chr(27). chr(32). chr(1));//ESTACIO ENTRE 
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle, chr(27). chr(33). chr(56));//ESTACIO ENTRE LETRAS
//fwrite($handle, chr(27). chr(72). chr(5));//Color Rojo

fwrite($handle, "  ".$nomcom);

fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS
fwrite($handle, chr(27). chr(33). chr(0));//ESTACIO ENTRE LETRAS
//fwrite($handle, chr(27) +"3"+ chr(17));//interlienaso
//fwrite($handle, chr(27). chr(72). chr(0));//Color Rojo
fwrite($handle, chr(27). chr(3). chr(17));//salto de linea VACIO
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
//fwrite($handle, chr(27). chr(33). chr(9));//Tamaño letra 10 
fwrite($handle,  "    ".$razsoc);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"    Cedula : ". $cedjur);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,  "    ".$diremp);
//fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
//fwrite($handle,"    Tel. ".$numtel_empresa);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//CENTRADO
fwrite($handle,"    --------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle, chr(27). chr(33). chr(8));//ESTACIO ENTRE LETRAS
fwrite($handle, "     RECIBO DE PAGO "."       N. ".$numrec );
fwrite($handle, chr(27). chr(33). chr(0));//ESTACIO ENTRE LETRAS
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS *revisar *******
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
$CAJERO = str_pad("    Cajero: " .$cajero ,27);
fwrite($handle,"    Fecha: ". $fecha."                ".$horaactual);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, chr(27). chr(33). chr(8));//ESTACIO ENTRE LETRAS
  $dsccli = substr($dsccli." ", 0,30);
fwrite($handle,"    Cliente: " . $cdgcli ."-".$dsccli);
fwrite($handle, chr(27). chr(33). chr(0));//ESTACIO ENTRE LETRAS
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $CAJERO);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//CENTRADO
fwrite($handle,"    --------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
fwrite($handle,"    Factura". "      Monto Aplicado"."            Saldo");
fwrite($handle, chr(27). chr(97). chr(1));//CENTRADO
fwrite($handle,"    --------------------------------------------");
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS *************
fwrite($handle, chr(27). chr(97). chr(0));//IzquierdERRORERR21++a
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha

	$consulta3 = "SELECT * FROM pagos_clientes_recibos_lineas WHERE numrec='$numrec' ORDER BY numlinea ASC";
if ($resultado3 = $mysqli->query($consulta3)) 
$contador=0;

		 while ($fila = $resultado3->fetch_assoc()) {

        $numlinea = $fila["numlinea"];
        $salfac   = $fila["salfac"];
        $numfac   = substr($fila["numfac"],-10);
		$monabo   = $fila["monabo"];
		$totfac   = $fila["totfac"];
		$monint   = $fila["monint"];
		$intfac   = $fila["intfac"];


	
$resultado=number_format($salfac,2,".",",");
$saldo_factura=sprintf("%16s",$resultado) ;

 
$pago=number_format($monabo,2,".",",");
 $abono = sprintf("%17s",$pago) ;
   
fwrite($handle, chr(27). chr(97). chr(2));//derecha		
fwrite($handle, $numfac ." ".$abono."".$saldo_factura);		
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea								 
fwrite($handle, chr(27). chr(97). chr(1));//centrado

}

fwrite($handle, chr(27). chr(97). chr(1));//CENTRADO
fwrite($handle,"    --------------------------------------------");
fwrite($handle, chr(27). chr(33). chr(24));//ESTACIO ENTRE LETRAS
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
$total_recibo=number_format($montot,2,".",",");
$total_pago=sprintf("%13s",$total_recibo) ;
fwrite($handle,"    TOTAL:  ");
fwrite($handle,$total_pago);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
fwrite($handle, chr(27). chr(33). chr(0));//ESTACIO ENTRE LETRAS
$monto=number_format($pagcon,2,".",",");
$pagado=sprintf("%13s",$monto) ;
fwrite($handle,"Monto Recibido: ");
fwrite($handle,$pagado);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
$cambio=number_format($vuelto,2,".",",");
$saldo=sprintf("%13s",$cambio) ;
fwrite($handle,"Su Cambio:  ");
fwrite($handle,$saldo);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//CENTRADO
fwrite($handle,"    --------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
fwrite($handle, chr(27). chr(33). chr(8));//ESTACIO ENTRE LETRAS
$saldo_cli=number_format($total_saldo,2,".",",");
$saldo_cliente=sprintf("%13s",$saldo_cli) ;
fwrite($handle,"Nuevo Saldo 11:   ");
fwrite($handle,$saldo_cliente);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(33). chr(0));//ESTACIO ENTRE LETRAS
fwrite($handle, chr(27). chr(97). chr(1));//CENTRADO
fwrite($handle,"    --------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(5));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle,"    ____________________________________________");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"     Firma y sello del cajero"  );
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"     Gracias Por Su Pago"  );
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(29). chr(86). chr(49));//Corta el Papel

}


if ($tipimp=="BEMANTECH"){  //  ************************Si la impresora es TIPO BEMANTECH ****************************///////////////



fwrite($handle,  chr(27). chr(20). chr(0));//salto de linea VACIO
fwrite($handle,  chr(27). chr(100). chr(0));//salto de linea VACIO
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(32). chr(1));//ESTACIO ENTRE 
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle, chr(27). chr(33). chr(22));//ESTACIO ENTRE LETRAS
//fwrite($handle, chr(27). chr(72). chr(5));//Color Rojo

fwrite($handle, $nomcom);
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS
//fwrite($handle, chr(27) +"3"+ chr(17));//interlienaso
//fwrite($handle, chr(27). chr(72). chr(0));//Color Rojo
fwrite($handle, chr(27). chr(3). chr(17));//salto de linea VACIO
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(33). chr(9));//Tamaño letra 10 
fwrite($handle, $razsoc);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Cedula : ". $cedjur);
//fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
//fwrite($handle, $diremp);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Tel. ".$numtel_empresa);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea

fwrite($handle,"--------------------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS *revisar *******
fwrite($handle, chr(27). chr(97). chr(2));//derecha

	  $recibo = str_pad("N. Recibo: ".$numrec, 39);
	    $cajrec = str_pad("Cajero: " . $cajero , 55);
fwrite($handle,$recibo);
fwrite($handle,"Fecha: ". $fecha);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, chr(27). chr(33). chr(22));//ESTACIO ENTRE LETRAS
fwrite($handle,"Cliente: " . $cdgcli ."-".$dsccli);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
fwrite($handle, chr(27). chr(33). chr(9));//Tamaño letra 10 
fwrite($handle, $cajrec);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea

fwrite($handle,"--------------------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
fwrite($handle,"Factura". "              Monto Pagado"."              Saldo");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"--------------------------------------------------------");
fwrite($handle, chr(27). chr(32). chr(0));//ESTACIO ENTRE LETRAS *************
fwrite($handle, chr(27). chr(97). chr(0));//IzquierdERRORERR21++a
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha

	 
	$consulta3 = "SELECT * FROM pagos_clientes_recibos_lineas WHERE numrec='$numrec' ORDER BY numlinea ASC";
if ($resultado3 = $mysqli->query($consulta3)) 
$contador=0;

		 while ($fila = $resultado3->fetch_assoc()) {

        $numlinea = $fila["numlinea"];
        $salfac   = $fila["salfac"];
        $numfac   = substr($fila["numfac"],-10);
		$monabo   = $fila["monabo"];
		$totfac   = $fila["totfac"];
		$monint   = $fila["monint"];
		$intfac   = $fila["intfac"];

	 
	
$resultado=number_format($salfac,2,".",",");
$saldo_factura=sprintf("%19s",$resultado) ;

$factura = str_pad($numfac, 21); 
$pago=number_format($monabo,2,".",",");
 $abono = str_pad($pago, 15);
   
fwrite($handle, chr(27). chr(97). chr(2));//derecha		
fwrite($handle, $factura ." ".$abono."".$saldo_factura);		
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea								 
fwrite($handle, chr(27). chr(97). chr(1));//centrado

}

fwrite($handle,"--------------------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
$total_recibo=number_format($montot,2,".",",");
$total_pago=sprintf("%13s",$total_recibo) ;


fwrite($handle, chr(27). chr(33). chr(22));//ESTACIO ENTRE LETRAS
fwrite($handle,"TOTAL:  ");
fwrite($handle,$total_pago);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
fwrite($handle, chr(27). chr(33). chr(9));//Tamaño letra 10 

$monto=number_format($pagcon,2,".",",");
$pagado=sprintf("%16s",$monto) ;
fwrite($handle,"Paga con:  ");
fwrite($handle,$pagado);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
$cambio=number_format($vuelto,2,".",",");
$saldo=sprintf("%16s",$cambio) ;
fwrite($handle,"Vuelto:  ");
fwrite($handle,$saldo);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle,"--------------------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(2));//Derecha
$saldo_cli=number_format($total_saldo,2,".",",");
$saldo_cliente=sprintf("%16s",$saldo_cli) ;
fwrite($handle,"Saldo Total:   ");
fwrite($handle,$saldo_cliente);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"--------------------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(5));//salto de linea
fwrite($handle, chr(27). chr(97). chr(1));//centrado
fwrite($handle,"________________________________________________________");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Firma y sello del cajero"  );
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,$horaactual);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle,"Gracias Por su pago"  );
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(109). chr(0));//salto de linea
fwrite($handle, chr(29). chr(27). chr(105));//Corta el Papel
//fwrite($handle, chr(29). chr(86). chr(49));//Corta el Papel


}      //**********************************  Cierra tipo de impresora BEMANTECH ****************************************//
		
fclose($handle); // cierra el fichero PRN

$salida = shell_exec('lpr $puerto'); //lpr->puerto impresora, imprimir archivo PRN

echo "<script languaje='javascript' type='text/javascript'>window.close();</script>";
?>

 