Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 13, 2018 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/f5ba44d97d7929edf9fe6fb1ac9c0cd3 to your computer and use it in GitHub Desktop.
Save parzibyte/f5ba44d97d7929edf9fe6fb1ac9c0cd3 to your computer and use it in GitHub Desktop.
<?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