This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(!isset($_POST["total"])) exit; | |
session_start(); | |
$total = $_POST["total"]; | |
include_once "base_de_datos.php"; | |
$ahora = date("Y-m-d H:i:s"); | |
$sentencia = $base_de_datos->prepare("INSERT INTO ventas(fecha, total) VALUES (?, ?);"); | |
$sentencia->execute([$ahora, $total]); | |
$sentencia = $base_de_datos->prepare("SELECT id FROM ventas ORDER BY id DESC LIMIT 1;"); | |
$sentencia->execute(); | |
$resultado = $sentencia->fetch(PDO::FETCH_OBJ); | |
$idVenta = $resultado === false ? 1 : $resultado->id; | |
$base_de_datos->beginTransaction(); | |
$sentencia = $base_de_datos->prepare("INSERT INTO productos_vendidos(id_producto, id_venta, cantidad) VALUES (?, ?, ?);"); | |
$sentenciaExistencia = $base_de_datos->prepare("UPDATE productos SET existencia = existencia - ? WHERE id = ?;"); | |
foreach ($_SESSION["carrito"] as $producto) { | |
$total += $producto->total; | |
$sentencia->execute([$producto->id, $idVenta, $producto->cantidad]); | |
$sentenciaExistencia->execute([$producto->cantidad, $producto->id]); | |
} | |
$base_de_datos->commit(); | |
unset($_SESSION["carrito"]); | |
$_SESSION["carrito"] = []; | |
header("Location: ./vender.php?status=1"); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment