300 lines
9.8 KiB
PHP
300 lines
9.8 KiB
PHP
<?php
|
|
|
|
// Validacion de token
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) { // Recorre todos los caracteres (siempre) y retorna true or false
|
|
$_SESSION['flash_error'] = 'Acción inválida.';
|
|
header('Location: index.php?opt=inicio_operador');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Requiere modelo de proveedor
|
|
|
|
require_once __DIR__ . '/../models/proveedorModel.php';
|
|
$proveedormodel = new proveedorModel;
|
|
|
|
// Validar sesión
|
|
|
|
if (!isset($_SESSION['user'])) {
|
|
header("Location: index.php?opt=login_operador");
|
|
exit;
|
|
}
|
|
|
|
$opt = $_GET['opt'] ?? null;
|
|
|
|
// Manejo de opciones enviadaas por URL
|
|
|
|
switch ($opt) {
|
|
|
|
// LISTAR PROVEEDORES
|
|
|
|
case 'proveedores':
|
|
|
|
// Utilizamos el modelo para obtener la lista completa de proveedores
|
|
|
|
$proveedores = $proveedormodel->listarProveedores();
|
|
|
|
ob_start();
|
|
require __DIR__ . '/../views/proveedores/listaProveedoresView.php';
|
|
return ob_get_clean();
|
|
|
|
// CREAR PROVEEDOR
|
|
|
|
case 'crear_proveedor':
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
|
|
$_SESSION['return_opt'] = $_GET['from'] ?? 'proveedores';
|
|
|
|
ob_start();
|
|
require __DIR__ . '/../views/proveedores/crearProveedorView.php';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
$razon_social = trim($_POST['razon_social'] ?? '');
|
|
$telefono = trim($_POST['telefono'] ?? '');
|
|
$email = trim($_POST['email'] ?? '');
|
|
$condIVA = trim($_POST['cond_iva'] ?? 'consumidor_final');
|
|
|
|
$cuit1 = trim($_POST['cuit_1'] ?? '');
|
|
$cuit2 = trim($_POST['cuit_2'] ?? '');
|
|
$cuit3 = trim($_POST['cuit_3'] ?? '');
|
|
|
|
$cuit = $cuit1 . $cuit2 . $cuit3;
|
|
|
|
$errores = [];
|
|
|
|
// Validaciones
|
|
|
|
if ($razon_social === '') {
|
|
$errores[] = "La razón social no puede estar vacía.";
|
|
} else if (!preg_match("/^[A-Za-zÁÉÍÓÚÑáéíóúñ0-9&',.\-]+(?: [A-Za-zÁÉÍÓÚÑáéíóúñ0-9&',.\-]+)*(?: (S\.A\.|S\.R\.L\.|S\.C\.|S\.C\.P\.|S\.A\.S\.))?$/", $razon_social)) {
|
|
$errores[] = "La razón social contiene caracteres inválidos o la abreviatura legal final no es correcta.";
|
|
} else if (strlen($razon_social) < 2) {
|
|
$errores[] = "La razón social debe tener al menos 2 caracteres.";
|
|
} else if (strlen($razon_social) > 100) {
|
|
$errores[] = "La razón social no puede superar los 100 caracteres.";
|
|
}
|
|
|
|
if ($telefono === '') {
|
|
$errores[] = "El teléfono no puede estar vacío.";
|
|
} else if (!preg_match('/^[0-9]{10}$/', $telefono)) {
|
|
$errores[] = "El teléfono debe tener 10 dígitos.";
|
|
}
|
|
|
|
if ($email === '') {
|
|
$errores[] = "El email no puede estar vacío.";
|
|
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errores[] = "Formato de email inválido.";
|
|
} else if (strlen($email) > 100) {
|
|
$errores[] = "El email no puede superar los 100 caracteres.";
|
|
}
|
|
|
|
$condicionesPermitidas = [
|
|
'consumidor_final',
|
|
'monotributo',
|
|
'responsable_inscripto'
|
|
];
|
|
|
|
if (!in_array($condIVA, $condicionesPermitidas, true)) {
|
|
|
|
$errores[] = 'Condición de IVA inválida.';
|
|
}
|
|
|
|
if ($cuit !== '') {
|
|
|
|
if (!preg_match('/^\d{11}$/', $cuit)) {
|
|
|
|
$errores[] = 'El CUIT debe contener 11 dígitos.';
|
|
}
|
|
} else {
|
|
|
|
$cuit = null;
|
|
}
|
|
|
|
// Guardamos errores para mostrarlos por pantalla
|
|
|
|
if (!empty($errores)) {
|
|
$_SESSION['flash_error'] = implode('<br>', $errores); // Se guarda en $_SESSION el error
|
|
header("Location: index.php?opt=crear_proveedor");
|
|
exit;
|
|
}
|
|
|
|
// Verificación de existencia de proveedor
|
|
|
|
if ($proveedormodel->verificarExistencia([
|
|
'razon_social' => $razon_social,
|
|
'email' => $email,
|
|
'cuit' => $cuit
|
|
])) {
|
|
$_SESSION['flash_error'] = "El proveedor ya existe.";
|
|
|
|
header("Location: index.php?opt=crear_proveedor");
|
|
exit;
|
|
}
|
|
|
|
if ($proveedormodel->crearProveedor([
|
|
'razon_social' => $razon_social,
|
|
'telefono' => $telefono,
|
|
'email' => $email,
|
|
'cuit' => $cuit,
|
|
'cond_iva' => $condIVA
|
|
])) {
|
|
$_SESSION['flash_succes'] = "Proveedor creado correctamente.";
|
|
} else {
|
|
$_SESSION['flash_error'] = "Ocurrió un error al crear el proveedor.";
|
|
}
|
|
|
|
$opt = $_SESSION['return_opt'];
|
|
|
|
header("Location: index.php?opt=$opt");
|
|
exit;
|
|
}
|
|
|
|
// EDITAR PROVEEDOR
|
|
|
|
case 'editar_proveedor':
|
|
|
|
$idEditar = (int) ($_GET['id'] ?? 0);
|
|
|
|
if ($idEditar < 0) {
|
|
header("Location: index.php?opt=proveedores");
|
|
exit;
|
|
};
|
|
|
|
// Busco el cliente en la base de datos mediante el modelo
|
|
|
|
$proveedor = $proveedormodel->obtenerPorId($idEditar);
|
|
|
|
if (!$proveedor) {
|
|
header("Location: index.php?opt=proveedores");
|
|
exit;
|
|
};
|
|
|
|
// Mostrar datos de proveedor
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
ob_start();
|
|
require __DIR__ . '/../views/proveedores/editarProveedorView.php';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
// Caso en donde se modifican los datos
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
$razon_social = trim($_POST['razon_social']) ?? '';
|
|
$telefono = trim($_POST['telefono']) ?? '';
|
|
$email = trim($_POST['email']) ?? '';
|
|
$condIVA = trim($_POST['cond_iva'] ?? 'consumidor_final');
|
|
|
|
$cuit1 = trim($_POST['cuit_1'] ?? '');
|
|
$cuit2 = trim($_POST['cuit_2'] ?? '');
|
|
$cuit3 = trim($_POST['cuit_3'] ?? '');
|
|
|
|
$cuit = $cuit1 . $cuit2 . $cuit3;
|
|
|
|
$errores = [];
|
|
|
|
// Validaciones
|
|
|
|
if ($razon_social === '') {
|
|
$errores[] = "La razón social no puede estar vacía.";
|
|
} else if (!preg_match("/^[A-Za-zÁÉÍÓÚÑáéíóúñ0-9&',.\-]+(?: [A-Za-zÁÉÍÓÚÑáéíóúñ0-9&',.\-]+)*(?: (S\.A\.|S\.R\.L\.|S\.C\.|S\.C\.P\.|S\.A\.S\.))?$/", $razon_social)) {
|
|
$errores[] = "La razón social contiene caracteres inválidos o la abreviatura legal final no es correcta.";
|
|
} else if (strlen($razon_social) < 2) {
|
|
$errores[] = "La razón social debe tener al menos 2 caracteres.";
|
|
} else if (strlen($razon_social) > 100) {
|
|
$errores[] = "La razón social no puede superar los 100 caracteres.";
|
|
}
|
|
|
|
if ($telefono === '') {
|
|
$errores[] = "El teléfono no puede estar vacío.";
|
|
} else if (!preg_match('/^[0-9]{10}$/', $telefono)) {
|
|
$errores[] = "El teléfono debe tener 10 dígitos.";
|
|
}
|
|
|
|
if ($email === '') {
|
|
$errores[] = "El email no puede estar vacío.";
|
|
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errores[] = "Formato de email inválido.";
|
|
} else if (strlen($email) > 100) {
|
|
$errores[] = "El email no puede superar los 100 caracteres.";
|
|
}
|
|
|
|
$condicionesPermitidas = [
|
|
'consumidor_final',
|
|
'monotributo',
|
|
'responsable_inscripto'
|
|
];
|
|
|
|
if (!in_array($condIVA, $condicionesPermitidas, true)) {
|
|
|
|
$errores[] = 'Condición de IVA inválida.';
|
|
}
|
|
|
|
if (($condIVA !== 'consumidor_final') && ($cuit === '')) {
|
|
|
|
$errores[] = 'Debe ingresar el CUIT';
|
|
}
|
|
|
|
if ($cuit !== '') {
|
|
|
|
if (!preg_match('/^\d{11}$/', $cuit)) {
|
|
|
|
$errores[] = 'El CUIT debe contener 11 dígitos.';
|
|
}
|
|
} else {
|
|
|
|
$cuit = null;
|
|
}
|
|
|
|
if (!empty($errores)) {
|
|
$_SESSION['flash_error'] = implode('<br>', $errores); // Guardamos los errores en $_SESSION
|
|
header("Location: index.php?opt=editar_proveedor");
|
|
exit;
|
|
}
|
|
|
|
// Verificación de existencia de proveedor
|
|
|
|
if ($proveedormodel->verificarExistencia([
|
|
'razon_social' => $razon_social,
|
|
'email' => $email,
|
|
'cuit' => $cuit
|
|
], $idEditar)) {
|
|
$_SESSION['flash_error'] = "El proveedor ya existe.";
|
|
|
|
header("Location: index.php?opt=editar_proveedor&id=$idEditar");
|
|
exit;
|
|
}
|
|
|
|
$guardarData = [
|
|
'razon_social' => $razon_social,
|
|
'telefono' => $telefono,
|
|
'email' => $email,
|
|
'cuit' => $cuit,
|
|
'cond_iva' => $condIVA
|
|
];
|
|
|
|
if ($proveedormodel->actualizarProveedor($proveedor['id'], $guardarData)) {
|
|
$_SESSION['flash_success'] = "Datos actualizados correctamente."; // Guardo en $_SESSION el success
|
|
} else {
|
|
$_SESSION['flash_error'] = "No se pudo actualizar el proveedor.";
|
|
}
|
|
|
|
header("Location: index.php?opt=proveedores");
|
|
exit;
|
|
}
|
|
|
|
// DEFAULT (404)
|
|
|
|
default:
|
|
ob_start();
|
|
require __DIR__ . '/../views/404View.php';
|
|
return ob_get_clean();
|
|
} |