<?php

ob_start();


     


include './ConexionII.php';
include './GestionDB.php';
session_start();


$Conexion = new Conexion();
$Con = $Conexion->conect();
$DB = new GestionDBFacturas();
$DBII = new GestionDBProducto();
$_SESSION['Numero'] = $DB->Numeropago($Con);

if ($_SESSION['Impresa']== true) {
// Ya se imprimió, cambio de header
unset($_SESSION['Numero']);
unset($_SESSION['Fecha']);
unset($_SESSION['NombreCliente']);
unset($_SESSION['ApellidoCliente']);
unset($_SESSION['Cedula']);
unset($_SESSION['Direccion']);
unset($_SESSION['Telefono']);
unset($_SESSION['Celular']);
unset($_SESSION['Contador']);
unset($_SESSION['Correo']);
unset($_SESSION['Congelador']);
unset($_SESSION['Marca']);
unset($_SESSION['Linea']);
unset($_SESSION['CodigoProducto']);
unset($_SESSION['IdCliente']);
unset($_SESSION['Factura']);
unset($_SESSION['Descuento']);
unset($_SESSION['Plazo']);
unset($_SESSION['Metodo']);
unset($_SESSION['DescuentoAplicado']);
header("Location: ./RegistrarPago.php");
}


if (!isset($_SESSION['Factura'])) {
$_SESSION['Factura'] = $_GET['factura'];
$query = 'SELECT * FROM facturas WHERE numero = "'.$_SESSION['Factura'].'"';
            $result = mysql_query($query, $Con);
$dato = mysql_fetch_array($result);
}else{
$query = 'SELECT * FROM facturas WHERE numero = "'.$_SESSION['Factura'].'"';
            $result = mysql_query($query, $Con);
$dato = mysql_fetch_array($result);
}
                    

if(isset($_SESSION['Usuario'])){
  
  

 
}else{

    //Si no le muestro otra página, puede ser la de Login
    header("Location: /MauricioCorrea/incorrecto.html");
}


?>

<!DOCTYPE html>





<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Pago</title>
                   <link rel="icon" type="image/png" href="imagenes/compra.png" />
          
        <script type="text/javascript" src="Js/jquery.min.js" ></script>
        <link rel="stylesheet" type="text/css" href="estilos.css">
        <link rel="stylesheet" type="text/css" href="fonts.css">
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script src="mysqlwslib.js"></script>
        <script src="Js/jquery-ui.js"></script>
        <script src="menu.js"></script>
<style>
#ui-id-1{
    background-color: white;
    opacity: 0.7;
}
</style>
<script>
            $(function(){
                $("#buscador").autocomplete({
                    source: 'buscadorproductos.php',
                    select: function(event, ui){
$("#codigo").html(
'<br>'+
//'<form id="form" name="form" action="'; echo $_SERVER['PHP_SELF']; echo'" method="post" onsubmit="asigna()">'+
//'<table cellspacing="35"  style="margin: 0 auto;">'+ 
'<tr><td width=150><span class="required"><div>Codigo*</div></span><input type="text" value="'+ ui.item.codigoProducto +'" name="codigo" readonly autofocus="autofocus" required="required" class="required" style="width:150px; height:30px;"></td>'
//'<input type="hidden" name="codigo_1" value="'+ ui.item.codigoProducto +'">'+
//'<input type="submit" name="Grabar" id="Grabar" value="OK" style="width:35px; height:35px;"/></td>'+
//'<input type="hidden" name="var_cont"></tr>'
                        );
$("#producto").html(
'<br>'+
//'<form id="form" name="form" action="'; echo $_SERVER['PHP_SELF']; echo'" method="post" onsubmit="asigna()">'+
//'<table cellspacing="35"  style="margin: 0 auto;">'+ 
'<td width=150><span class="required"><div>Producto*</div></span><input type="text" id="Producto" readonly value="'+ ui.item.value +'" name="producto_1" autofocus="autofocus" min="0" required="required" class="required" style="width:150px; height:30px;"></td>'
//'<input type="hidden" name="codigo_1" value="'+ ui.item.codigoProducto +'">'+
//'<input type="submit" name="Grabar" id="Grabar" value="OK" style="width:35px; height:35px;"/></td>'+
//'<input type="hidden" name="var_cont"></tr>'
                        );
$("#cantidad").html(
'<br>'+
//'<form id="form" name="form" action="'; echo $_SERVER['PHP_SELF']; echo'" method="post" onsubmit="asigna()">'+
//'<table cellspacing="35"  style="margin: 0 auto;">'+ 
'<td width=150 ><span class="required"><div>Cantidad*</div></span><input type="number" id="Cantidad" value="0" name="cantidad_1" autofocus="autofocus" min="0" required="required" class="required" style="width:150px; height:30px;" onChange="multiplicar();"></td>'
//'<input type="hidden" name="codigo_1" value="'+ ui.item.codigoProducto +'">'+
//'<input type="submit" name="Grabar" id="Grabar" value="OK" style="width:35px; height:35px;"/></td>'+
//'<input type="hidden" name="var_cont"></tr>'
                        );
$("#valor").html(
'<br>'+
//'<form id="form" name="form" action="'; echo $_SERVER['PHP_SELF']; echo'" method="post" onsubmit="asigna()">'+
//'<table cellspacing="35"  style="margin: 0 auto;">'+ 
'<td width=180><span class="required"><div>Valor*</div></span><input type="text" readonly value="'+ ui.item.precioProducto +'"  name="valor_1" autofocus="autofocus" min="0" required="required" id="Valor" class="required" style="width:180px; height:30px;"></td>'
//'<input type="hidden" name="codigo_1" value="'+ ui.item.codigoProducto +'">'+
//'<input type="submit" name="Grabar" id="Grabar" value="OK" style="width:35px; height:35px;"/></td>'+
//'<input type="hidden" name="var_cont"></tr>'
                        );
$("#total").html(
'<br>'+
//'<form id="form" name="form" action="'; echo $_SERVER['PHP_SELF']; echo'" method="post" onsubmit="asigna()">'+
//'<table cellspacing="35"  style="margin: 0 auto;">'+ 
'<td width=150><span class="required"><div>Total*</div></span><input type="number" readonly value="0" id="Subtotal" name="subtotal_1" autofocus="autofocus" min="0" required="required" class="required" style="width:150px; height:30px;">'+
'<input type="hidden" name="codigo_1" value="'+ ui.item.codigoProducto +'">'+
'<input type="submit" name="Grabar" id="Grabar" value="OK" style="width:35px; height:35px;"/></td>'+
'<input type="hidden" name="var_cont"></tr>'
                        );
                    }
                });
            });
        </script>
       <script>
function multiplicar() {
m1 = document.getElementById("Cantidad").value;
m2 = document.getElementById("Valor").value;
Subtotal = m1*m2;
document.getElementById("Subtotal").value = Subtotal;

}


function Cambia() {
CodigoJS = document.getElementById("Codigo").value
alert(CodigoJS);
<?php
$Codigo = '<script> document.write(CodigoJS) </script>'; 
$query = 'SELECT * FROM productos WHERE codigo = '.$Codigo.'';
$result = mysql_query($query, $Con);
?>;
document.getElementById("Producto").value = 'Nombre';
document.getElementById("Valor").value = '5000';
}









var cont=1;
function addRowX()  //Esta la funcion que agrega las filas :
{




cont++;
var indiceFila=1;
myNewRow = document.getElementById('tabla').insertRow(-1);
myNewRow.id=indiceFila;




myNewCell=myNewRow.insertCell(-1);
myNewCell.innerHTML='<td><input type="number" id="Codigo'+cont+'" value="0" name="codigo_'+cont+'" autofocus="autofocus" min="0" required="required" class="required" style="width:110px; height:30px;" ></td> <ts><input type="submit" name="Buscar_'+cont+'" id="Buscar_'+cont+'" value="Buscar" style="width:60px; height:40px;" /></td>';

/*myNewCell=myNewRow.insertCell(-1);
myNewCell.innerHTML='<td><input type="number" value="0" name="codigo_'+cont+'" autofocus="autofocus" required="required" class="required" style="width:110px; height:30px;"></td>';

myNewCell=myNewRow.insertCell(-1);
myNewCell.innerHTML='<td><input type="text" readonly value="" name="producto_'+cont+'" autofocus="autofocus" required="required" class="required" style="width:200px; height:30px;"></td>';

myNewCell=myNewRow.insertCell(-1);
myNewCell.innerHTML='<input type="number" id="Cantidad'+cont+'" value="0" name="cantidad_'+cont+'" autofocus="autofocus" min="0" required="required" class="required" style="width:50px; height:30px;" onChange="multiplicar'+cont+'();">';
myNewCell=myNewRow.insertCell(-1);
myNewCell.innerHTML='<input type="text" readonly value="5" name="valor_'+cont+'" autofocus="autofocus" min="0" required="required" id="Valor'+cont+'" class="required" style="width:100px; height:30px;">';
myNewCell=myNewRow.insertCell(-1);
myNewCell.innerHTML='<input type="number" readonly value="0" id="Subtotal'+cont+'" name="subtotal_'+cont+'" autofocus="autofocus" min="0" required="required" class="required" style="width:100px; height:30px;">';

*/
indiceFila++;

}
//////////////Borrar() ///////////
function borrar() {
var table = document.getElementById('tabla');
if(table.rows.length > 3)
    {
    table.deleteRow(table.rows.length -1);
cont--;
    }
}

////////////FUNCION ASIGNA VALOR DE CONT PARA EL FOR DE MOSTRAR DATOS MP-MOD-TT////////
function asigna()
{
valor=document.form.var_cont.value=cont;
}



function ocultar1(){
document.getElementById("1").style.display = 'none';}

function ocultar2(){
document.getElementById("2").style.display = 'none';}

function ocultar3(){
document.getElementById("3").style.display = 'none';}




</script>
        
    </head>
    <body>
        
        <header>
<?php

if(isset($_SESSION['Usuario'])){

echo '<a class="datosusuario icon-exit" href="/MauricioCorrea/Cerrar.php"> - Salir</a>';
echo '<span class="datosusuario icon-users"> - Bienvenido '.$_SESSION['Nombre'].'</span>';


}else{
    header("Location: /MauricioCorrea/incorrecto.html");
}

       





     echo '<div class="menu_bar">
        <a href="#" class="bt-menu"><span class="icon-newspaper"></span>Menu</a>
        </div>
            <nav>
                <ul>
                    <li><a href="inicio.php"><span class="icon-home"> </span>Inicio</a></li>  
                    <li class="submenu">
                    <a href="#"><span class="icon-user-tie"></span>Admin<span class="caret icon-circle-down"></span></a>    
                    <ul class="children">
                        <li><a href="Registro.php" ><span class="icon-user-plus"></span>Crea Vendedor</a></li>
                        <li><a href="ModificaVendedor.php" ><span class="icon-pencil"></span>Modifica Vendedor</a></li>
                        <li><a href="RegistroCliente.php" ><span class="icon-user-plus"></span>Crea Cliente <span class="user-plus"></a></li>
                        <li><a href="RegistroProveedor.php" ><span class="icon-user-plus"></span>Crea Proveedor <span class="user-plus"></a></li>
                       

                    </ul>
                    </li>


                    <li class="submenu">
                     <a href="#"><span class="icon-cart"> </span>Vend<span class="caret icon-circle-down"></span></a>
                      <ul class="children">
                        <li><a href="RegistroCliente.php" ><span class="icon-user-plus"></span>Crea Cliente <span class=""></a></li>
                        <li><a href="RegistroVenta.php" ><span class="icon-cart"></span>Crea Factura <span class=""></a></li>
                        <li><a href="RegistroRemision.php" ><span class="icon-file-text2"></span>Crea Remision <span class=""></a></li>
                        <li><a href="RegistroNota.php" ><span class="icon-files-empty"></span>Crea Nota <span class=""></a></li>
                    </ul>
                    </li>    





                     <li class="submenu">
                    <a href="#.php"><span class="icon-users"> </span>Clientes<span class="caret icon-circle-down"></span></a>    
                   <ul class="children">  
                    
                    <li><a href="RegistraCongelador.php"><span class="icon-credit-card"> </span>Registra Congelador</a></li>  
                    <li><a href="ModificaCliente.php"><span class="icon-user-check"> </span>Modificar Cliente</a></li>  
                    <li><a href="InformesClientes.php"><span class="icon-profile"> </span>Informe Clientes</a></li> 
                    <li><a href="InformesVentas.php"><span class="icon-calculator"> </span>Informe Ventas</a></li> 
                    </ul> 
                     </li>        

                       <li class="submenu">
                    <a href="#"><span class="icon-user-tie"></span>Proveedores<span class="caret icon-circle-down"></span></a>    
                    <ul class="children">
                    <li><a href="RegistroCompra.php" ><span class="icon-plus"></span>Carga Compra <span class="plus"></a></li>
                        <li><a href="ModificaProveedor.php" ><span class="icon-user-plus"></span>Modificar Proveedor</a></li>
                        
                        <li><a href="InformesCompras.php" ><span class="icon-stats-bars2"></span>Informe Compras <span class="plus"></a></li>

                    </ul>
                    </li>



                                        
                    <li class="submenu">
                    <a href="#.php"><span class="icon-droplet"> </span>Prod<span class="caret icon-circle-down"></span></a>    
                   <ul class="children">
                        <li><a href="CrearProducto.php" ><span class="icon-barcode"></span>Crea Producto <span class=""></a></li>
                        <li><a href="ModificarProducto.php" ><span class="icon-qrcode"></span>Modifica Producto <span class=""></a></li>
                        <li><a href="InformesInventarios.php" ><span class="icon-files-empty"></span>Informe Inventario <span class=""></a></li>
                    </ul> 
                     </li>    
                    <li><a href="Cerrar.php"><span class="icon-exit"> </span>Salir</a></li>                    
                </ul>
            </nav>
        </header>
        <section id="TablaLog1">
            
            <p>
         <form id="form" name="form" action="'; echo $_SERVER['PHP_SELF']; echo'" method="get" > 
            <div><p id="Titulo"> Nuevo pago </p></div>
            <br>
        
                
            <table  cellspacing="10"  style="margin: 0 auto;"  >
            <tr>
                <td>
                <span class="required"><div>Tipo</div></span>
                 <input type="text" disabled="disabled" name="tipo" disabled="disabled" value="PAGO" autofocus="autofocus" min="0" required="required" class="required" style="width:140px; height:30px;">
                </td>
                <td>
                <span class="required"><div>Numero</div></span>
                <input type="text" disabled="disabled" name="numero" value=" '.$DB->Numeropago($Con); echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:140px; height:30px;">
                </td>


                <td>
                <span class="required"><div>Fecha</div></span>
                <input type="text" id="dateDefault" disabled="disabled"  value="' .$_SESSION['Fecha']; echo'" name="fecha" autofocus="autofocus" required="required" class="required" style="width:185px; height:30px;">
                </td>
<td> <span class="required"><div>Vendedor</div></span>

 <input type="text" name="vendedor"  disabled="disabled" value="'. $_SESSION['Nombre'] . ' ' .$_SESSION['Apellido']; echo'" autofocus="autofocus" required="required" class="required" style="width:180px; height:30px;">
</td>
                
             </tr>               
             <tr>
             <td>
            <span class="required"><div>Nombre</div></span>
            <input type="text" disabled="disabled" name="nombre" value=" '.$_SESSION['NombreCliente']; echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:140px; height:30px;">
            </td>


            <td>
            <span class="required"><div>Cédula</div></span>
            <input type="text" disabled="disabled" name="cedula" value=" '.$_SESSION['Cedula']; echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:140px; height:30px;">
            </td>       

<td>
            <span class="required"><div>Dirección</div></span>
            <input type="text" disabled="disabled" name="direccion" value=" '.$_SESSION['Direccion'] ; echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:185px; height:30px;">
            </td>
            
            <td>
            <span class="required"><div>Teléfono</div></span>
            <input type="text" disabled="disabled" name="telefono" value=" '.$_SESSION['Telefono']; echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:180px; height:30px;">
            </td>

   <tr>
            
           

           

            <td>
            <span class="required"><div>Factura</div></span>
            <input type="text" disabled="disabled" name="factura" value=" '.$_SESSION['Factura']; echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:140px; height:30px;">
            </td>

            <td>
            <span class="required"><div>Saldo</div></span>
            <input type="text" disabled="disabled" name="saldo" value=" '.$dato['saldo']; echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:140px; height:30px;">
            </td>
<td>
            <span class="required"><div>Vence</div></span>
            <input type="text" disabled="disabled" name="plazo" value=" '.$dato['vence']; echo'" autofocus="autofocus" min="0" required="required" class="required" style="width:185px; height:30px;">
            </td>

<td>
            <span class="required"><div>Pago</div></span>
            <input type="number" name="pago" value="1" autofocus="autofocus" min="0" max="';echo $dato['saldo']; echo '" required="required" class="required" style="width:180px; height:30px;">
            </td>
            </tr>
 </table>


          
<td colspan="6">
<input type="submit" name="GenerarFacturaPDF" id="GenerarFacturaPDF" value="Generar pago" style="width:700px; height:40px;" />
</td>
</form>

<form method="get" name="Cancelar" action="CerrarSesionFactura.php">

<table  cellspacing="10"  style="margin: 0 auto;"  >
<tr>
<td colspan="6">
<input type="submit" name="Cancelar" id="Cancelar" value="Cancelar" style="width:700px; height:40px;"/>
</td>
</tr>
</table>
</form>';


if(isset($_GET['GenerarFacturaPDF'])){
 
if($_GET['pago'] == 0){
print '<script language="JavaScript">'; 
        print 'alert("El valor a pagar no debe ser cero!!!" )'; 
        print '</script>'; 
}else{

//1 - Actualizar el saldo y el estado de la facturas en la tabla facturas
$Nuevosaldo = ($dato['saldo'] - $_GET['pago']);
$_SESSION['NuevoSaldo'] = $Nuevosaldo;
$_SESSION['Pago'] = $_GET['pago'];
$_SESSION['Vence'] = $dato['vence'];

mysql_query("update facturas set saldo ='".$Nuevosaldo."' where id =".$dato['id']);
if($Nuevosaldo == 0){
mysql_query("update facturas set estado ='CANCELADA' where id =".$dato['id']);
}

//2 - Grabar el pago en la tabla pagos
$Vendedor = ($_SESSION['Nombre'] . ' '.$_SESSION['Apellido']);
$DB->ingresarEncabezadopago(($DB->arregloEncabezadopago('PAGO', $_SESSION['Numero'], $_SESSION['Cedula'], $_SESSION['Factura'], $_SESSION['Fecha'], $_GET['pago'], $Vendedor)), $Con);

//3 - Generar formato de pago
header('Location: PagoPDF.php');
}
}



ob_end_flush();

          
       
        ?>
    </body>
</html>