<?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 entregas_clientes_recibos 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"];		
        $retira = $row["retira"];			
		$cdgusu	= $row["cdgusu"];


		

		//$nuevosaldo   = $fila["nuevosaldo"];
		


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

     $cajero  = $row3["usu"];		


echo  $ip = $_SERVER["REMOTE_ADDR"];

if ($ip=="127.0.0.1"){
echo $puerto="LPT1";



  } else {
  
  echo $puerto="LPT2";



  }  


//$handle = fopen("archivo.txt", "a");

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





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. Orden: ".$numrec, 23);
	  $cajrec = str_pad("Cajero: " . $cajero , 40);
	  

$retira1 = str_pad("Retira: " . $retira , 40);
$placa1 = str_pad("Placa: " . $placa , 15);
$entregaen1 = str_pad("Entrega en: " . $entregaen , 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, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $retira1);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $placa1);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $entregaen1);



fwrite($handle,"----------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
//fwrite($handle,"Cant.  ". "  Articulo"."             Precio");
fwrite($handle,"Articulo"."             Cant.Ant "."Cant.Ent "."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 entregas_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"];
        $codfactura   = substr($fila["codfactura"],-10);
		$dscart   = $fila["dscart"];
	    $cdgbar    = $fila["codigo"];
		$cantanterior = $fila["cantanterior"];
		$cantentrega  = $fila["cantentrega"];
		$cantsaldo    = $fila["cantsaldo"];

/*
		$cantanterior = number_format($cantanterior,2,".",",");
		$cantentrega  = number_format($cantentrega,2,".",",");
	    $cantsaldo    = number_format($cantsaldo ,2,".",",");
		*/
				$codigo = str_pad($cdgbar, 13);



		fwrite($handle, chr(27). chr(97). chr(2));//derecha		
   fwrite($handle, $cantanterior ."". $cantentrega ."". $cantsaldo);	
  $acotado = substr($dscart." ", 0, 30);

   fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
   fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
    fwrite($handle,$codigo."-".$acotado);	
   fwrite($handle, chr(27). chr(100). chr(1));//salto de linea

	

}



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);

$retira1 = str_pad("Retira: " . $retira , 40);
$placa1 = str_pad("Placa: " . $placa , 15);
$entregaen1 = str_pad("Entrega en: " . $entregaen , 40);	

fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $retira1);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $placa1);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $entregaen1);


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,"Articulo"."             Cant.Ant "."Cant.Ent "."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 entregas_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"];
        $codfactura   = substr($fila["codfactura"],-10);
		$dscart   = $fila["dscart"];
	    $cdgbar    = $fila["codigo"];
		$cantanterior = $fila["cantanterior"];
		$cantentrega  = $fila["cantentrega"];
		$cantsaldo    = $fila["cantsaldo"];

/*
		$cantanterior = number_format($cantanterior,2,".",",");
		$cantentrega  = number_format($cantentrega,2,".",",");
	    $cantsaldo    = number_format($cantsaldo ,2,".",",");
		*/
				$codigo = str_pad($cdgbar, 13);



		fwrite($handle, chr(27). chr(97). chr(2));//derecha		
   fwrite($handle, $cantanterior ."". $cantentrega ."". $cantsaldo);	
  $acotado = substr($dscart." ", 0, 30);

   fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
   fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
    fwrite($handle,$codigo."-".$acotado);	
   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, 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);

$retira1 = str_pad("Retira: " . $retira , 40);
$placa1 = str_pad("Placa: " . $placa , 15);
$entregaen1 = str_pad("Entrega en: " . $entregaen , 40);	

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(97). chr(0));//izquierda
fwrite($handle, $retira1);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $placa1);
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//izquierda
fwrite($handle, $entregaen1);


fwrite($handle,"--------------------------------------------------------");
fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
fwrite($handle,"Articulo"."             Cant.Ant "."Cant.Ent "."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 entregas_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"];
        $codfactura   = substr($fila["codfactura"],-10);
		$dscart   = $fila["dscart"];
	    $cdgbar    = $fila["codigo"];
		$cantanterior = $fila["cantanterior"];
		$cantentrega  = $fila["cantentrega"];
		$cantsaldo    = $fila["cantsaldo"];

/*
		$cantanterior = number_format($cantanterior,2,".",",");
		$cantentrega  = number_format($cantentrega,2,".",",");
	    $cantsaldo    = number_format($cantsaldo ,2,".",",");
		*/
				$codigo = str_pad($cdgbar, 13);



		fwrite($handle, chr(27). chr(97). chr(2));//derecha		
   fwrite($handle, $cantanterior ."". $cantentrega ."". $cantsaldo);	
  $acotado = substr($dscart." ", 0, 30);

   fwrite($handle, chr(27). chr(100). chr(1));//salto de linea
   fwrite($handle, chr(27). chr(97). chr(0));//Izquierda
    fwrite($handle,$codigo."-".$acotado);	
   fwrite($handle, chr(27). chr(100). chr(1));//salto de linea

	

}
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,"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>";

?>

 