472 lines
14 KiB
PHP
472 lines
14 KiB
PHP
<?php
|
|
|
|
// Validacion de token
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])
|
|
) {
|
|
$_SESSION['flash_error'] = 'Acción inválida (CSRF)';
|
|
header('Location: index.php?opt=inicio_operador');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Requiero modelos necesarios
|
|
|
|
require_once __DIR__ . '/../models/cajaModel.php';
|
|
require_once __DIR__ . '/../models/productoModel.php';
|
|
|
|
$cajamodel = new cajaModel();
|
|
$productomodel = new productoModel();
|
|
|
|
// Validar sesion
|
|
|
|
if (!isset($_SESSION['user'])) {
|
|
header("Location: index.php?opt=login_operador");
|
|
exit;
|
|
}
|
|
|
|
// Seteo el $opt
|
|
|
|
$opt = $_GET['opt'] ?? null;
|
|
|
|
// Switch de opt para manejo de caja
|
|
|
|
switch ($opt) {
|
|
|
|
// <---------------------------------- MANEJO DE CAJA PARA VENTA ---------------------------------->
|
|
|
|
// AGREGAR PRODUCTO A LA CAJA
|
|
|
|
case 'agregar_producto_caja':
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: index.php?opt=crear_venta');
|
|
exit;
|
|
}
|
|
|
|
$ids = $_POST['ids'] ?? [];
|
|
$cantidades = $_POST['cantidades'] ?? [];
|
|
|
|
if (empty($ids)) {
|
|
$_SESSION['flash_error'] = 'No se seleccionó ningún producto.';
|
|
header('Location: index.php?opt=agregar_producto');
|
|
exit;
|
|
}
|
|
|
|
$agregados = 0;
|
|
$errores = [];
|
|
|
|
// Evitar duplicados
|
|
|
|
$ids = array_unique($ids);
|
|
|
|
foreach ($ids as $idProducto) {
|
|
|
|
$idProducto = (int) $idProducto;
|
|
$cantidad = isset($cantidades[$idProducto]) ? (int) $cantidades[$idProducto] : 1;
|
|
|
|
if ($idProducto <= 0 || $cantidad <= 0) {
|
|
$errores[] = 'Datos inválidos.';
|
|
continue;
|
|
}
|
|
|
|
$producto = $productomodel->obtenerPorIdParaCaja($idProducto);
|
|
|
|
if (!$producto) {
|
|
$errores[] = 'Producto inexistente.';
|
|
continue;
|
|
}
|
|
|
|
if ($cajamodel->agregarProducto($producto, $cantidad)) {
|
|
$agregados++;
|
|
} else {
|
|
$errores[] = $cajamodel->getError();
|
|
}
|
|
}
|
|
|
|
// Condiciones para mostrar mensajes
|
|
|
|
if ($agregados === 1) {
|
|
$_SESSION['flash_success'] = "Se agregó un producto correctamente.";
|
|
} else if ($agregados > 1) {
|
|
$_SESSION['flash_success'] = "Se agregaron {$agregados} productos correctamente.";
|
|
}
|
|
|
|
if (!empty($errores)) {
|
|
$_SESSION['flash_error'] = implode('<br>', array_unique($errores));
|
|
header('Location: index.php?opt=agregar_producto');
|
|
exit;
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_venta');
|
|
exit;
|
|
|
|
// ELIMINAR PRODUCTO DE CAJA
|
|
|
|
case 'eliminar_producto':
|
|
|
|
$idProducto = (int) ($_POST['id_producto'] ?? 0);
|
|
|
|
if ($idProducto <= 0) {
|
|
$_SESSION['flash_error'] = 'Datos inválidos.';
|
|
header('Location: index.php?opt=crear_venta');
|
|
exit;
|
|
}
|
|
|
|
if (!$cajamodel->eliminarProducto($idProducto)) {
|
|
$_SESSION['flash_error'] = $cajamodel->getError() ?? 'No se pudo eliminar el producto.';
|
|
} else {
|
|
$_SESSION['flash_success'] = 'Producto eliminado correctamente.';
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_venta');
|
|
exit;
|
|
|
|
// EDITAR CANTIDAD DE PRODUCTO
|
|
|
|
case 'editar_cantidad':
|
|
|
|
$idProducto = (int) ($_POST['id_producto'] ?? 0);
|
|
$nuevaCantidad = (int) ($_POST['cantidad'] ?? 0);
|
|
|
|
if ($idProducto <= 0 || $nuevaCantidad <= 0) {
|
|
$_SESSION['flash_error'] = 'Datos inválidos.';
|
|
header('Location: index.php?opt=crear_venta');
|
|
exit;
|
|
}
|
|
|
|
if (!$cajamodel->editarCantidad($idProducto, $nuevaCantidad)) {
|
|
$_SESSION['flash_error'] = $cajamodel->getError() ?? 'No se pudo actualizar la cantidad.';
|
|
} else {
|
|
$_SESSION['flash_success'] = 'Cantidad editada correctamente.';
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_venta');
|
|
exit;
|
|
|
|
// VACIAR CAJA DE PRODUCTOS
|
|
|
|
case 'vaciar_caja_venta':
|
|
|
|
if ($cajamodel->vaciarCajaVenta()) {
|
|
$_SESSION['flash_success'] = 'Carrito vaciado.';
|
|
} else {
|
|
$_SESSION['flash_error'] = 'No se pudo vaciar el carrito.';
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_venta');
|
|
exit;
|
|
|
|
// <---------------------------------- MANEJO DE CAJA PARA PEDIDO ---------------------------------->
|
|
|
|
// AGREGAR PRODUCTO A CAJA DE PEDIDO
|
|
|
|
case 'agregar_producto_pedido_caja':
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: index.php?opt=crear_pedido');
|
|
exit;
|
|
}
|
|
|
|
$ids = $_POST['ids'] ?? [];
|
|
$costos = $_POST['costos'] ?? [];
|
|
$cantidades = $_POST['cantidades'] ?? [];
|
|
|
|
if (empty($ids)) {
|
|
$_SESSION['flash_error'] = 'No se seleccionó ningún producto.';
|
|
header('Location: index.php?opt=agregar_producto_pedido');
|
|
exit;
|
|
}
|
|
|
|
$errores = [];
|
|
$agregados = 0;
|
|
|
|
// Evitar duplicados
|
|
|
|
$ids = array_unique($ids);
|
|
|
|
foreach ($ids as $idProducto) {
|
|
|
|
$idProducto = (int) $idProducto;
|
|
$costo = isset($costos[$idProducto]) ? (float) $costos[$idProducto] : 0;
|
|
$cantidad = isset($cantidades[$idProducto]) ? (int) $cantidades[$idProducto] : 1;
|
|
|
|
if ($idProducto <= 0 || $cantidad <= 0 || $costo <= 0) {
|
|
$errores[] = 'Datos inválidos.';
|
|
continue;
|
|
}
|
|
|
|
$producto = $productomodel->obtenerPorIdParaCajaPedido($idProducto);
|
|
|
|
if (!$producto) {
|
|
$errores[] = 'Producto inexistente.';
|
|
continue;
|
|
}
|
|
|
|
if ($cajamodel->agregarProductoPedido($producto, $costo, $cantidad)) {
|
|
$agregados++;
|
|
} else {
|
|
$errores[] = $cajamodel->getError();
|
|
}
|
|
}
|
|
|
|
// Condiciones para manejo de mensajes
|
|
|
|
if ($agregados === 1) {
|
|
$_SESSION['flash_success'] = "Se agregó un producto correctamente.";
|
|
} else if ($agregados > 1) {
|
|
$_SESSION['flash_success'] = "Se agregaron {$agregados} productos correctamente.";
|
|
}
|
|
|
|
if (!empty($errores)) {
|
|
$_SESSION['flash_error'] = implode('<br>', array_unique($errores));
|
|
header('Location: index.php?opt=agregar_producto_pedido');
|
|
exit;
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_pedido');
|
|
exit;
|
|
|
|
// VACIAR CAJA DE PRODUCTOS PARA PEDIDO
|
|
|
|
case 'vaciar_caja_pedido':
|
|
|
|
// Guardo datos en $_SESSION para mantener persistencia
|
|
|
|
$_SESSION['pedido']['cond_iva'] = $_POST['cond_iva'] ?? '';
|
|
$_SESSION['pedido']['cuit'] = $_POST['cuit'] ?? '';
|
|
|
|
if ($cajamodel->vaciarCajaPedido()) {
|
|
$_SESSION['flash_success'] = 'Carrito vaciado.';
|
|
} else {
|
|
$_SESSION['flash_error'] = 'No se pudo vaciar el carrito.';
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_pedido');
|
|
exit;
|
|
|
|
// ELIMINAR PRODUCTO DE PEDIDO
|
|
|
|
case 'eliminar_producto_pedido':
|
|
|
|
// Guardo datos en $_SESSION para mantener persistencia
|
|
|
|
$_SESSION['pedido']['cond_iva'] = $_POST['cond_iva'] ?? '';
|
|
$_SESSION['pedido']['cuit'] = ($_POST['cuit_1'] ?? '') . ($_POST['cuit_2'] ?? '') . ($_POST['cuit_3'] ?? '');
|
|
|
|
$idProducto = (int) ($_POST['id_producto'] ?? 0);
|
|
|
|
if ($idProducto <= 0) {
|
|
$_SESSION['flash_error'] = 'Datos inválidos.';
|
|
header('Location: index.php?opt=crear_pedido');
|
|
exit;
|
|
}
|
|
|
|
if (!$cajamodel->eliminarProductoPedido($idProducto)) {
|
|
$_SESSION['flash_error'] = $cajamodel->getError() ?? 'No se pudo eliminar el producto.';
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_pedido');
|
|
exit;
|
|
|
|
// EDITAR DATOS DE PRODUCTOS PARA PEDIDO
|
|
|
|
case 'editar_datos_producto':
|
|
|
|
// Guardo datos para mantener consistencia
|
|
|
|
$_SESSION['pedido']['cond_iva'] = $_POST['cond_iva'] ?? '';
|
|
$_SESSION['pedido']['cuit'] = ($_POST['cuit_1'] ?? '') . ($_POST['cuit_2'] ?? '') . ($_POST['cuit_3'] ?? '');
|
|
|
|
// Saneamiento de datos
|
|
|
|
$idProducto = (int) ($_POST['id_producto'] ?? 0);
|
|
$nuevaCantidad = $_POST['cantidad'][$idProducto] ?? null;
|
|
$nuevoCosto = $_POST['costo'][$idProducto] ?? null;
|
|
|
|
if ($idProducto <= 0 || $nuevoCosto <= 0 || $nuevaCantidad <= 0) {
|
|
$_SESSION['flash_error'] = 'Datos inválidos.';
|
|
header('Location: index.php?opt=crear_pedido');
|
|
exit;
|
|
}
|
|
|
|
if (!$cajamodel->editarDatosProductoPedido($idProducto, $nuevoCosto, $nuevaCantidad)) {
|
|
$_SESSION['flash_error'] = $cajamodel->getError() ?? 'No se pudieron actualizar los datos.';
|
|
} else {
|
|
$_SESSION['flash_success'] = 'Datos actualizados.';
|
|
}
|
|
|
|
header('Location: index.php?opt=crear_pedido');
|
|
exit;
|
|
|
|
// <---------------------------------- MANEJO DE CAJA PARA SERVICIO ---------------------------------->
|
|
|
|
// AGREGAR INSUMOS PARA SERVICIO EN LA CAJA
|
|
|
|
case 'agregar_insumo_caja':
|
|
|
|
$idServicio = (int) ($_GET['id'] ?? 0);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header('Location: index.php?opt=actualizar_servicio');
|
|
exit;
|
|
}
|
|
|
|
$ids = $_POST['ids'] ?? [];
|
|
$cantidades = $_POST['cantidades'] ?? [];
|
|
|
|
if (empty($ids)) {
|
|
$_SESSION['flash_error'] = 'No se seleccionó ningún insumo.';
|
|
header('Location: index.php?opt=agregar_insumo');
|
|
exit;
|
|
}
|
|
|
|
$agregados = 0;
|
|
$errores = [];
|
|
|
|
// Evitar duplicados
|
|
|
|
$ids = array_unique($ids);
|
|
|
|
foreach ($ids as $idInsumo) {
|
|
|
|
$idInsumo = (int) $idInsumo;
|
|
$cantidad = isset($cantidades[$idInsumo]) ? (int) $cantidades[$idInsumo] : 1;
|
|
|
|
if ($idInsumo <= 0 || $cantidad <= 0) {
|
|
$errores[] = 'Datos inválidos.';
|
|
continue;
|
|
}
|
|
|
|
$insumo = $productomodel->obtenerPorIdParaCajaServicio($idInsumo);
|
|
|
|
if (!$insumo) {
|
|
$errores[] = 'Producto inexistente.';
|
|
continue;
|
|
}
|
|
|
|
if ($cajamodel->agregarInsumo($insumo, $cantidad)) {
|
|
$agregados++;
|
|
} else {
|
|
$errores[] = $cajamodel->getError();
|
|
}
|
|
}
|
|
|
|
// Condiciones para mensajes
|
|
|
|
if ($agregados === 1) {
|
|
$_SESSION['flash_success'] = "Se agregó un insumo correctamente.";
|
|
} else if ($agregados > 1) {
|
|
$_SESSION['flash_success'] = "Se agregaron {$agregados} insumos correctamente.";
|
|
}
|
|
|
|
if (!empty($errores)) {
|
|
$_SESSION['flash_error'] = implode('<br>', array_unique($errores));
|
|
header('Location: index.php?opt=agregar_insumo');
|
|
exit;
|
|
}
|
|
|
|
header('Location: index.php?opt=actualizar_servicio&id=' . $idServicio);
|
|
exit;
|
|
|
|
// VACIAR CAJA DE INSUMOS PARA SERVICIO
|
|
|
|
case 'vaciar_caja_servicio':
|
|
|
|
$idServicio = (int) ($_POST['id'] ?? 0);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$_SESSION['servicio']['detalle_realizado'] = $_POST['detalle_realizado'] ?? '';
|
|
$_SESSION['servicio']['mano_obra'] = (float) $_POST['mano_obra'] ?? 0;
|
|
}
|
|
|
|
$manoObra = $_SESSION['servicio']['mano_obra'] ?? 0;
|
|
|
|
if ($manoObra < 0) {
|
|
$_SESSION['flash_error'] = 'Datos inválidos.';
|
|
header('Location: index.php?opt=actualizar_servicio&id=' . $idServicio);
|
|
exit;
|
|
}
|
|
|
|
if ($cajamodel->vaciarCajaServicio()) {
|
|
$_SESSION['flash_success'] = 'Carrito vaciado.';
|
|
} else {
|
|
$_SESSION['flash_error'] = 'No se pudo vaciar el carrito.';
|
|
}
|
|
|
|
header('Location: index.php?opt=actualizar_servicio&id=' . $idServicio);
|
|
exit;
|
|
|
|
// ELIMINAR INSUMO DE SERVICIO
|
|
|
|
case 'eliminar_insumo':
|
|
|
|
$idServicio = (int) ($_GET['id'] ?? 0);
|
|
$idInsumo = (int) ($_POST['id_producto'] ?? 0);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$_SESSION['servicio']['detalle_realizado'] = $_POST['detalle_realizado'] ?? '';
|
|
$_SESSION['servicio']['mano_obra'] = (float) $_POST['mano_obra'] ?? 0;
|
|
}
|
|
|
|
$manoObra = $_SESSION['servicio']['mano_obra'] ?? 0;
|
|
|
|
// Validaciones
|
|
|
|
if ($idInsumo <= 0 || $manoObra < 0) {
|
|
$_SESSION['flash_error'] = 'Datos inválidos.';
|
|
header('Location: index.php?opt=actualizar_servicio&id=' . $idServicio);
|
|
exit;
|
|
}
|
|
|
|
if (!$cajamodel->eliminarInsumoServicio($idInsumo)) {
|
|
$_SESSION['flash_error'] = $cajamodel->getError() ?? 'No se pudo eliminar el producto.';
|
|
} else {
|
|
$_SESSION['flash_success'] = 'Insumo eliminado correctamente.';
|
|
}
|
|
|
|
header('Location: index.php?opt=actualizar_servicio&id=' . $idServicio);
|
|
exit;
|
|
|
|
// EDITAR CANTIDAD DE INSUMO DE SERVICIO
|
|
|
|
case 'editar_cantidad_insumo':
|
|
|
|
$idServicio = (int) ($_GET['id'] ?? 0);
|
|
$idInsumo = (int) ($_POST['id_producto'] ?? 0);
|
|
$nuevaCantidad = $_POST['cantidad'][$idInsumo] ?? null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$_SESSION['servicio']['detalle_realizado'] = $_POST['detalle_realizado'] ?? '';
|
|
$_SESSION['servicio']['mano_obra'] = (float) $_POST['mano_obra'] ?? 0;
|
|
}
|
|
|
|
$manoObra = $_SESSION['servicio']['mano_obra'] ?? 0;
|
|
|
|
// Validaciones
|
|
|
|
if ($idInsumo <= 0 || $nuevaCantidad <= 0 || $manoObra < 0) {
|
|
$_SESSION['flash_error'] = 'Datos inválidos.';
|
|
header('Location: index.php?opt=actualizar_servicio&id=' . $idServicio);
|
|
exit;
|
|
}
|
|
|
|
if (!$cajamodel->editarCantidadInsumo($idInsumo, $nuevaCantidad)) {
|
|
$_SESSION['flash_error'] = $cajamodel->getError() ?? 'No se pudo actualizar la cantidad.';
|
|
} else {
|
|
$_SESSION['flash_success'] = 'Cantidad editada correctamente.';
|
|
}
|
|
|
|
header('Location: index.php?opt=actualizar_servicio&id=' . $idServicio);
|
|
exit;
|
|
|
|
// DEFAULT (404)
|
|
|
|
default:
|
|
|
|
ob_start();
|
|
header("Location: index.php?opt=404View.php");
|
|
return ob_get_clean();
|
|
} |