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

session_start();

 $usu=$_SESSION['username'];
 

		include('../conexion.php'); 
		if($_SESSION['tipo_usu']=='a' or $_SESSION['tipo_usu']=='s'  or $_SESSION['tipo_usu']=='c'  or $_SESSION['tipo_usu']=='u'){
		}else{
			header('location:acceso_denegado.php');
		}



$consulta = "SELECT * FROM usuarios WHERE usu='$usu'";
$resultado = $mysqli->query($consulta);
$row = $resultado->fetch_array();
$nomb = $row["usu"];
$cod  = $row["codusu"];
		
$accion=$_POST["accion"];
if (!isset($accion)) { $accion=$_GET["accion"]; }


$cdgpro=$_POST["cdgpro"];
$dscpro=strtoupper($_POST["dscpro"]);
$fecfac=$_POST["fecfac"];
$fecfac= implode("-", array_reverse(explode("-", $fecfac))); 
$fecpro=$_POST["fecpro"];
$fecpro= implode("-", array_reverse(explode("-", $fecpro))); 
$cdgtipven=$_POST["combotipoventa"];
$cdgtipcxp=$_POST["combotipocxp"];
$numfac=$_POST["numfac"];
$subexe=$_POST["subexe"];
$subexe = str_replace (",","",$subexe);
$subgra=$_POST["subgra"];
$subgra = str_replace (",","",$subgra);
$subfac=$subexe+$subgra;
$desfactot=$_POST["desfactot"];
$desfactot = str_replace (",","",$desfactot);
$importe= $subfac -  $desfactot;
$impfac=$_POST["impfac"];
$impfac = str_replace (",","",$impfac);
$flefac=$_POST["flefac"];
$flefac = str_replace (",","",$flefac);
$otrfac=$_POST["otrfac"];
$otrfac = str_replace (",","",$otrfac);
$totfac=$_POST["totfac"];

$cob_iv = $_POST["cob_iv"];
$tarifa = $_POST["tarifa"];
$ivporc = $_POST["ivporc"];


$detpro="CP";
$cdgusu=$cod;
	
$minimo=0;

if (!isset($_POST["obsfac"])) { $obsfac=""; } else  {  $obsfac=strtoupper($_POST["obsfac"]); }   

	//************************************************ Seleción del plazo del cliente *********************************



	

 if ($cdgtipven == 2) {
		
	   $query_plazo = "SELECT * FROM proveedores WHERE cdgpro='$cdgpro'";
       $resultado = $mysqli->query($query_plazo);
       $row = $resultado->fetch_array();
       $pladia = $row["cdgpla"];
	   $dscpro = $row["dscpro"];
	   $cedpro = $row["cedpro"];

// calculo de la fecha de vencimiento **

		$vencimiento= "+".$pladia." day" ;
		
		$fecven = strtotime ( $vencimiento , strtotime ( $fecfac ) ) ;
		$fecven = date ( 'Y-m-d' , $fecven );
	
   } else {
   
	   $cdgtipven = 1;
	   $pladia = "0";
	   $query_plazo = "SELECT * FROM proveedores WHERE cdgpro='$cdgpro'";
       $resultado = $mysqli->query($query_plazo);
       $row = $resultado->fetch_array();

	   
	    if ($cdgpro == 9999) {
	   
	     } else {
		 
	   $dscpro = $row["dscpro"];
	   $cedpro = $row["cedpro"];
	   $fecven = " ";
   		
		}
    }

 ////////////////////////////////////////////////////   insert de la FACTURA DE COMPRA  /////////////////////////////////

if ($accion=="alta") {
    if ($cdgtipven==2){

	
	//************************************************ Insert 2201 CxC y proveedores  saldo actual*******************************
			

		$query_operacion1="INSERT INTO web_2201 (codfactura, fecfac, fecpro, cdgpro, dscpro, borrado, estado , cdgtipcxp, cdgtipven, cdgusu, pladia, fecven, numfac, subexe, subgra, subfac, desfactot, impfac, flefac, otrfac, totfac, salact, detpro,obsfac ) VALUES ('',  '$fecfac','$fecpro', '$cdgpro', '$dscpro',  '0', '1' , '$cdgtipcxp', '$cdgtipven','$cdgusu','$pladia','$fecven','$numfac','$subexe','$subgra','$subfac', '$desfactot','$impfac','$flefac','$otrfac','$totfac' ,'$totfac','$detpro','$obsfac')";	
		
	 $query_operacion1;
	$mysqli->query($query_operacion1);


	$consulta = "SELECT SUM(salact) AS saldo FROM web_2201 WHERE cdgpro='$cdgpro' AND salact > 0 ";
    $resultado = $mysqli->query($consulta);
    $row = $resultado->fetch_array();
    $salact = $row["saldo"];


	$nuevo_saldo="UPDATE proveedores SET  salact='$salact' WHERE cdgpro='$cdgpro'";
	$mysqli->query($nuevo_saldo);

      	} else  {
		
		
		
		$query_operacion1="INSERT INTO web_2201 (codfactura, fecfac, fecpro, cdgpro, dscpro, borrado, estado , cdgtipcxp, cdgtipven, cdgusu, pladia, fecven, numfac,  subexe, subgra, subfac, desfactot, impfac, flefac, otrfac, totfac, salact, detpro,obsfac ) VALUES ('',  '$fecfac','$fecpro', '$cdgpro', '$dscpro',  '0', '1' , '$cdgtipcxp', '$cdgtipven', '$cdgusu','$pladia','$fecven','$numfac','$subexe','$subgra','$subfac', '$desfactot','$impfac','$flefac','$otrfac','$totfac' ,'0','$detpro','$obsfac')";	
		
	 $query_operacion1;
	$mysqli->query($query_operacion1);


		
		} 
		
		
	$query_operacion="INSERT INTO facturas_compras (codfactura, fecfac, fecpro, cdgpro, dscpro, borrado, estado, cdgtipven, cdgtipcxp, cdgusu, pladia, fecven, numfac,subexe, subgra, subfac, desfactot, impfac, flefac, otrfac, totfac ) VALUES ('',  '$fecfac','$fecpro', '$cdgpro', '$dscpro',  '0', '1', '$cdgtipven', '$cdgtipcxp', '$cdgusu','$pladia','$fecven','$numfac','$subexe','$subgra','$subfac', '$desfactot','$impfac','$flefac','$otrfac','$totfac'  )";				
	$mysqli->query($query_operacion);
	$codfacturacompras= $mysqli-> insert_id ;	
		
		
	$sel_insertar="INSERT INTO factulinea_compra (codfactura,numlinea,codigo,dscart,cdgfam,cdglin,canart,cosart, utiart1, utiart2, PRE001, PRE002,importe,desfac,despro,impart, fleart, implin,deslin, flelin, borrado, PREANT, modpre, cdgimp ,cdgtar,tarimp) VALUES ('$codfacturacompras','1','100','ARTICULO COMODIN','001','001','1','0', '0', '0','$totfac', '0','$importe','0','0','0', '0','$impfac','$desfactot' ,'$flefac','0' ,'0','1','$cob_iv' ,'$tarifa' ,'$ivporc')";
	$mysqli->query($sel_insertar);	
		
		
		} 


if ($accion=="modificar") {

$cdgpro=$_POST["cdgpro"];
$dscpro=strtoupper($_POST["dscpro"]);
$fecfac=$_POST["fecfac"];
$fecfac= implode("-", array_reverse(explode("-", $fecfac))); 
$fecpro=$_POST["fecpro"];
$fecpro= implode("-", array_reverse(explode("-", $fecpro))); 
$cdgtipven=$_POST["combotipoventa"];
$cdgtipcxp=$_POST["combotipocxp"];
$numfac=$_POST["numfac"];
$subexe=$_POST["subexe"];
$subexe = str_replace (",","",$subexe);
$subgra=$_POST["subgra"];
$subgra = str_replace (",","",$subgra);
$subfac=$subexe+$subgra;
$desfactot=$_POST["desfactot"];
$desfactot = str_replace (",","",$desfactot);
$impfac=$_POST["impfac"];
$impfac = str_replace (",","",$impfac);
$flefac=$_POST["flefac"];
$flefac = str_replace (",","",$flefac);
$otrfac=$_POST["otrfac"];
$otrfac= str_replace (",","",$otrfac);
$totfac=$_POST["totfac"];
$totfac = str_replace (",","",$totfac);
$detpro="CP";
$cdgusu=$cod;
$cob_iv = $_POST["cob_iv"];
$tarifa = $_POST["tarifa"];
$ivporc = $_POST["ivporc"];	

if (!isset($_POST["obsfac"])) { $obsfac=""; } else  {  $obsfac=strtoupper($_POST["obsfac"]); }   

$sel="SELECT * FROM facturas_compras WHERE  numfac='$numfac' AND cdgpro='$cdgpro'  "; 
		$resultado = $mysqli->query($sel);
		$row = $resultado->fetch_array();
		
		$codfactura   = $row["codfactura"];


$consulta2 = "DELETE FROM web_2201 WHERE  numfac='$numfac' AND cdgpro='$cdgpro' ";
$mysqli->query($consulta2);

$consulta3 = "DELETE FROM facturas_compras WHERE  numfac='$numfac' AND cdgpro='$cdgpro' ";
$mysqli->query($consulta3);

$consulta4 = "DELETE FROM factulinea_compra WHERE  codfactura='$codfactura'  ";
$mysqli->query($consulta4);



    if ($cdgtipven==2){

	//************************************************ Insert 2201 CxC y proveedores  saldo actual*******************************
			

		$query_operacion1="INSERT INTO web_2201 (codfactura, fecfac, fecpro, cdgpro, dscpro, borrado, estado , cdgtipcxp, cdgtipven, cdgusu, pladia, fecven, numfac, subexe, subgra, subfac, desfactot, impfac, flefac, otrfac, totfac, salact, detpro,obsfac) VALUES ('',  '$fecfac','$fecpro', '$cdgpro', '$dscpro',  '0', '1' , '$cdgtipcxp', '$cdgtipven','$cdgusu','$pladia','$fecven','$numfac','$subexe','$subgra','$subfac', '$desfactot','$impfac','$flefac','$otrfac','$totfac' ,'$totfac','$detpro','$obsfac')";	
	$mysqli->query($query_operacion1);


	$consulta = "SELECT SUM(salact) AS saldo FROM web_2201 WHERE cdgpro='$cdgpro' AND salact > 0 ";
    $resultado = $mysqli->query($consulta);
    $row = $resultado->fetch_array();
    $salact = $row["saldo"];


	$nuevo_saldo="UPDATE proveedores SET  salact='$salact' WHERE cdgpro='$cdgpro'";
	$mysqli->query($nuevo_saldo);

      	} else  {
		
		
		
		$query_operacion1="INSERT INTO web_2201 (codfactura, fecfac, fecpro, cdgpro, dscpro, borrado, estado , cdgtipcxp, cdgtipven, cdgusu, pladia, fecven, numfac,  subexe, subgra, subfac, desfactot, impfac, flefac, otrfac, totfac, salact, detpro,obsfac ) VALUES ('',  '$fecfac','$fecpro', '$cdgpro', '$dscpro',  '0', '1' , '$cdgtipcxp', '$cdgtipven', '$cdgusu','$pladia','$fecven','$numfac','$subexe','$subgra','$subfac', '$desfactot','$impfac','$flefac','$otrfac','$totfac' ,'0','$detpro','$obsfac')";	
$mysqli->query($query_operacion1);
		
	$consulta = "SELECT SUM(salact) AS saldo FROM web_2201 WHERE cdgpro='$cdgpro' AND salact > 0 ";
    $resultado = $mysqli->query($consulta);
    $row = $resultado->fetch_array();
    $salact = $row["saldo"];


	$nuevo_saldo="UPDATE proveedores SET  salact='$salact' WHERE cdgpro='$cdgpro'";
	$mysqli->query($nuevo_saldo);


		
		}
		
		$query_operacion="INSERT INTO facturas_compras (codfactura, fecfac, fecpro, cdgpro, dscpro, borrado, estado, cdgtipven, cdgtipcxp, cdgusu, pladia, fecven, numfac,subexe, subgra, subfac, desfactot, impfac, flefac, otrfac, totfac ) VALUES ('',  '$fecfac','$fecpro', '$cdgpro', '$dscpro',  '0', '1', '$cdgtipven', '$cdgtipcxp', '$cdgusu','$pladia','$fecven','$numfac','$subexe','$subgra','$subfac', '$desfactot','$impfac','$flefac','$otrfac','$totfac'  )";				
	$mysqli->query($query_operacion);
	$codfacturacompras= $mysqli-> insert_id ;	
		
		
	$sel_insertar="INSERT INTO factulinea_compra (codfactura,numlinea,codigo,dscart,cdgfam,cdglin,canart,cosart, utiart1, utiart2, PRE001, PRE002,importe,desfac,despro,impart, fleart, implin,deslin, flelin, borrado, PREANT, modpre, cdgimp ,cdgtar,tarimp) VALUES ('$codfacturacompras','1','100','ARTICULO COMODIN','001','001','1','0', '0', '0','$totfac', '0','$subfac','0','0','0', '0','$impfac','$desfactot' ,'$flefac','0' ,'0','1','$cob_iv' ,'$tarifa' ,'$ivporc')";
	$mysqli->query($sel_insertar);	
		
		 
		} 


?>	
	<html>
	<head>
		<title>Factura Nula</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
		<link href="../calendario/calendar-blue.css" rel="stylesheet" type="text/css">
		<script type="text/javascript" src="../funciones/validar.js"></script>
		<script type="text/JavaScript" language="javascript" src="../calendario/calendar.js"></script>
		<script type="text/JavaScript" language="javascript" src="../calendario/lang/calendar-sp.js"></script>
		<script type="text/JavaScript" language="javascript" src="../calendario/calendar-setup.js"></script>
		<script language="javascript">
		var cursor;
		if (document.all) {
		// Está utilizando EXPLORER
		cursor='hand';
		} else {
		// Está utilizando MOZILLA/NETSCAPE
		cursor='pointer';
		}
		
	
	    function cancelar() {
			location.href="index.php";
		}
		
				function nobackbutton(){	
   window.location.hash="no-back-button";	
   window.location.hash="Again-No-back-button" //chrome	
   window.onhashchange=function(){window.location.hash="no-back-button";}	
}


		</script>



</head>
	<body onLoad="nobackbutton();">
		<div id="">
			<div id="zonaContenido">
         	<div class="contenidoFactura">
			   <div align="center">
				<div id="tituloForm" class="header">
               Operacion completada satisfactoriamente
			   </div>
			    <br> </br>
				<table width="100%" border="0">
  <tr>
    <td colspan="4">&nbsp;</td>
    </tr>
</table>

				
			    
			  </div>
				<div id="botonBusquedaAWR">					
				
				<div id="" align="center">
					<img src="../img/Symbol-Check_128x128-32.png" width="128" height="128" onClick="cancelar()" border="0" onMouseOver="style.cursor=cursor" title="Salir">
			
			  </div>
				    
			   </div>
			  </div>
		  </div>
		</div>
	</body>
</html>



