<?php
session_start();
error_reporting(E_ALL ^ E_DEPRECATED);
require('fpdf17/fpdf.php');
require("conexion.php");
$codigos=$_SESSION['codigos'];
$nombres=$_SESSION['nombres'];
$valores=$_SESSION['valores'];
$cantidades=$_SESSION['cantidades'];
$vendedor=$_SESSION['vendedor'];
$almacen=$_SESSION['almacen'];
$Doc=$_SESSION['Doc'];
$Nombre=$_SESSION['Nombre'];
$pago=$_SESSION['pago'];
//echo $codigos."<br>";
//echo $nombres."<br>";
//echo $valores."<br>";
//echo $cantidades."<br>";
//echo $vendedor."<br>";
//echo $almacen."<br>";
ob_start();
$pdf = new FPDF('P', 'mm', array(60,10));
$pdf->AddFont('PAPYRUS','','PAPYRUS.php');
$pdf->AddPage();
$pdf->Image('icono.png',3.5,1,3,3);
$pdf->setXY(0,4.5);
$pdf->SetFont('PAPYRUS','',4);
$pdf->Cell(10,1,'VIRA',0,1,'C');
$pdf->SetFont('Arial','B',2);
$pdf->Cell(0,2,'Joyas con magia para el alma',0,1,'C');
$pdf->SetFont('Arial','B',1.8);
$pdf->setXY(0,7.8);
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,8.5);
$pdf->Cell(10,0.5,' ',0,1,'C');
$pdf->setXY(0,9.2);
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,9.9);
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,10.6);
$pdf->Cell(10,0.5,utf8_decode('Bogotá D.C.'),0,1,'C');
$pdf->setXY(0,11.3);
$pdf->Cell(10,0.5,'',0,1,'C');
$archivo = "contador.txt";
$abre = fopen($archivo, "r");
$total = fread($abre, filesize($archivo));
fclose($abre);
$abre = fopen($archivo, "w");
$total = $total + 1;
$total = sprintf("%04d", $total);
$grabar = fwrite($abre, $total);
fclose($abre);
$pdf->SetFont('Arial','B',1.4);
$pdf->setXY(0,13);
$pdf->Cell(10,0.5,'Factura: N-'.$total,0,1);
$pdf->setXY(0,13.9);
$pdf->Cell(5,0.5,'Fecha: '.date('Y')." / " .date('m')." / " .date('d'),0,1);
$pdf->setXY(5,13.9);
$pdf->Cell(5,0.5,'Hora: '.date('H').":" .date('m'),0,1);
$pdf->setXY(0,14.8);
$pdf->Cell(5,0.5,'Vendedor: '.$vendedor,0,1);
$pdf->setXY(5,14.8);
$pdf->Cell(5,0.5,'Almacen: '.$almacen,0,1);
$pdf->setXY(0,15.7);
$pdf->Cell(5,0.5,'Cliente: ',0,1);
$pdf->setXY(3,15.7);
$pdf->Cell(7,0.5,$Nombre,0,1);
$pdf->setXY(0,16.6);
$pdf->Cell(10,0.5,'-----------------------------------------------------------------------------',0,1);
$pdf->setXY(0,18);
$pdf->Cell(7,0.5,'Cant.: ',0,1,'L');
$pdf->setXY(4,18);
$pdf->Cell(1,0.5,'Descripcion: ',0,1,'C');
$pdf->setXY(8,18);
$pdf->Cell(2,0.5,'Total: ',0,1,'C');
$ids=explode('-',$codigos);
$desc=explode('-',$nombres);
$uni=explode('-',$cantidades);
$vals=explode('-',$valores);
$numproductos=count($ids)-1;
for($x=0;$x<$numproductos;$x++)
{
$pdf->setXY(2,(19+(0.5*$x)));
$pdf->Cell(7,0.5,substr($desc[$x],0,13),0,1,'L');
$pdf->setXY(0,(19+(0.5*$x)));
$pdf->Cell(1,0.5,$uni[$x],0,1,'L');
$pdf->setXY(8,(19+(0.5*$x)));
$pdf->Cell(2,0.5,($uni[$x] * $vals[$x]),0,1,'C');
$grantotal=$uni[$x] * $vals[$x];
$cod=$ids[$x];
$descrip=$desc[$x];
$valor=$vals[$x];
$cant=$uni[$x];
$fecha=date('Y')."-".date('m')."-".date('d');
mysqli_query($mysqli,"INSERT INTO ventas (Codigo,Factura,descripcion,Cantidad,Fecha,Almacen,Vendedor,Doc,Nombre,Valor,pago) VALUES ('$cod','$total','$descrip','$cant','$fecha','$almacen','$vendedor','$Doc','$Nombre','$grantotal','$pago')");
$sql = mysqli_query($mysqli,"SELECT Existencias FROM productos where Codigo='$cod'");
$result = mysqli_fetch_array($sql);
$Nexixtencia=$result['Existencias']-$cant;
mysqli_query($mysqli,"UPDATE productos SET Existencias='$Nexixtencia' where Codigo='$cod'");
}
$pdf->setXY(0,(20+(0.5*$x)));
$pdf->Cell(0,0.5,'------------------------------------------------------------------------------------------------------------------------------',0,1);
$pdf->setXY(0,(21+(0.5*$x)));
$pdf->Cell(8,0.5,'Compra Neta: ',0,1);
$pdf->setXY(8,(21+(0.5*$x)));
$total=0;
$t1=0;
for($x=0;$x<$numproductos;$x++)
{
$total=$total+((($vals[$x]-(($vals[$x]*16)/100)))*$uni[$x]);
$t1=$t1+($vals[$x]*$uni[$x]);
}
$pdf->Cell(2,0.5,$total,0,1,'C');
$pdf->setXY(0,(22+(0.5*$x)));
$pdf->Cell(8,0.5,'IVA. (16%): ',0,1);
$pdf->setXY(8,(22+(0.5*$x)));
$totaliva=(($t1/100)*16);
$pdf->Cell(2,0.5,$totaliva,0,1,'C');
$pdf->setXY(0,(23+(0.5*$x)));
$pdf->Cell(10,0.5,'------------------------------------------------------------------------------------------------------------------------------',0,1);
$pdf->setXY(0,(24+(0.5*$x)));
$pdf->Cell(8,0.5,'TOTAL: ',0,1);
$pdf->setXY(8,(24+(0.5*$x)));
$pdf->Cell(2,0.5,($total+$totaliva),0,1,'C');
$pdf->setXY(0,(25+(0.5*$x)));
$pdf->Cell(10,0.5,'------------------------------------------------------------------------------------------------------------------------------',0,1);
$pdf->setXY(0,(26+(0.5*$x)));
$pdf->Cell(10,0.5,'RESOL: No. 320001223543',0,1,'C');
$pdf->setXY(0,(27+(0.5*$x)));
$pdf->Cell(10,0.5,'del : 02 / 01 / 2015',0,1,'C');
$pdf->setXY(0,(28+(0.5*$x)));
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->setXY(0,(29+(0.5*$x)));
$pdf->Cell(10,0.5,'',0,1,'C');
$pdf->Output();
ob_end_flush();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Documento sin título</title>
</head>
<body>
</body>
</html>