<?php
$conex = mysql_connect("localhost","sisventas","sisventas")or die("no se pudo realizar la conexion");
mysql_select_db("sisventas",$conex)or die("ERROR con la base de datos");
session_start();
if(!$_SESSION)
{
echo '<script language = javascript>
alert("usuario no autenticado")
self.location = "index.php"
</script>';
}
$person_id = $_SESSION['id_Usuarios'];
$hora= date("g:i:s-a");
$fecha = date('Y-m-j');
$nuevafecha = strtotime ( '+2 day' , strtotime ( $fecha ) ) ;
$nuevafecha = date ( 'Y-m-j' , $nuevafecha );
include "conexion.php";
$arreglo=$_SESSION['datos'];
$numeroventa=0;
$re=mysql_query("select * from compras order by numeroventa DESC limit 1") or die(mysql_error());
while ( $f=mysql_fetch_array($re)) {
$numeroventa=$f['numeroventa'];
}
if($numeroventa==0){
$numeroventa=1;
}else{
$numeroventa=$numeroventa+1;
}
$arreglo=$_SESSION['datos'];
for($i=0;$i<count($arreglo);$i++){
$re=mysql_query("select cantidad from productos where id_producto=".$arreglo[$i]['Id'])or die(mysql_error());
while ($f=mysql_fetch_array($re)) {
if($arreglo[$i]['Cantidad'] > $f['cantidad']){
echo '<script language = javascript>
alert("cantidad excede el stock")
self.location = "stock.php"
</script>';
}else{
$x=($f['cantidad']-$arreglo[$i]['Cantidad']);
mysql_query("update productos set cantidad=".$x." where id_Producto=".$arreglo[$i]['Id'])or die(mysql_error());
}
}
}
for($i=0; $i<count($arreglo);$i++){
mysql_query("insert into pedidos (usuario,id_Producto,fecha,hora,numeroventa,nombre,imagen,precio,nueva_Fecha,cantidad,subtotal) values(
".$person_id.",
'".$arreglo[$i]['Id']."',
'".$fecha."',
'".$hora."',
".$numeroventa.",
'".$arreglo[$i]['Nombre']."',
'".$arreglo[$i]['Imagen']."',
'".$arreglo[$i]['Precio']."',
'".$nuevafecha."',
'".$arreglo[$i]['Cantidad']."',
'".($arreglo[$i]['Precio']*$arreglo[$i]['Cantidad'])."'
)")or die(mysql_error());
}
$total=0;
$tabla='<table border="1">
<tr>
<th>Nombre</th>
<th>Cantidad</th>
<th>Precio</th>
<th>Subtotal</th>
</tr>';
for($i=0;$i<count($arreglo);$i++){
$tabla=$tabla.'<tr>
<td>'.$arreglo[$i]['Nombre'].'</td>
<td>'.$arreglo[$i]['Cantidad'].'</td>
<td>'.$arreglo[$i]['Precio'].'</td>
<td>'.$arreglo[$i]['Cantidad']*$arreglo[$i]['Precio'].'</td>
</tr>
';
$total=$total+($arreglo[$i]['Cantidad']*$arreglo[$i]['Precio']);
}
$tabla=$tabla.'</table>';
echo $tabla;
?>