Created
March 6, 2020 20:54
-
-
Save parzibyte/924b00c18007a221f708cce077312043 to your computer and use it in GitHub Desktop.
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 | |
public function terminarVenta() | |
{ | |
// Crear una venta | |
$venta = new Venta(); | |
$venta->saveOrFail(); | |
$idVenta = $venta->id; | |
$productos = $this->obtenerProductos(); | |
// Recorrer carrito de compras | |
foreach ($productos as $producto) { | |
// El producto que se vende... | |
$productoVendido = new ProductoVendido(); | |
$productoVendido->fill([ | |
"id_venta" => $idVenta, | |
"descripcion" => $producto->descripcion, | |
"codigo_barras" => $producto->codigo_barras, | |
"precio" => $producto->precio_venta, | |
"cantidad" => $producto->cantidad, | |
]); | |
// Lo guardamos | |
$productoVendido->saveOrFail(); | |
// Y restamos la existencia del original | |
$productoActualizado = Producto::find($producto->id); | |
$productoActualizado->existencia -= $productoVendido->cantidad; | |
$productoActualizado->saveOrFail(); | |
} | |
$this->vaciarProductos(); | |
return redirect() | |
->route("vender.index") | |
->with("mensaje", "Venta terminada"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment