diff --git a/.env b/.env
new file mode 100755
index 0000000..95ad54a
--- /dev/null
+++ b/.env
@@ -0,0 +1,107 @@
+# Configuración de producción para sistema multi-empresa
+# Archivo .env - NO COMMIT a repositorios públicos
+
+# =============================================================================
+# CONFIGURACIÓN DE BASES DE DATOS MULTI-EMPRESA
+# =============================================================================
+# El sistema usa dos bases de datos principales:
+# 1. Base master: avantikads_nmgen (catálogos globales y autenticación)
+# 2. Bases de empresas: avantikads_nm{empresaId} (datos específicos por empresa)
+
+# Base de datos Master (para autenticación y catálogos globales)
+DB_MASTER_HOST=10.10.4.17
+DB_MASTER_DATABASE=avantikads_nmgen
+DB_MASTER_USER=nickpons666
+DB_MASTER_PASSWORD=MiPo6425@@
+
+# Configuración general de conexión
+DB_HOST=10.10.4.17
+DB_PORT=3390
+DB_CHARSET=utf8mb4
+
+# Prefijo para bases de datos de empresas
+# Patón real: avantikads_nm{empresaId} donde empresaId viene del usuario
+DB_EMPRESA_PREFIX=avantikads_nm
+DB_EMPRESA_USER=nickpons666
+DB_EMPRESA_PASSWORD=MiPo6425@@
+
+# =============================================================================
+# CONFIGURACIÓN DEL SISTEMA
+# =============================================================================
+
+# Rutas del sistema
+DOC_ROOT=/var/www/html/ventas
+WEB_ROOT=http://ventas-test.local:82
+
+# Configuración SMTP (correos)
+SMTP_HOST=
+SMTP_USER=
+SMTP_PASS=
+SMTP_PORT=
+
+# Configuración PAC (Facturación electrónica)
+USER_PAC=
+PW_PAC=
+
+# Configuración de paginación
+ITEMS_PER_PAGE=20
+
+# Rango de años válidos
+MIN_YEAR=2025
+MAX_YEAR=2030
+
+# =============================================================================
+# CONFIGURACIÓN ADICIONAL
+# =============================================================================
+
+# Configuración de sesión
+SESSION_LIFETIME=3600
+SESSION_PATH=/
+SESSION_DOMAIN=
+
+# Configuración de seguridad
+ENCRYPTION_KEY=ventas_encryption_key_32_chars
+JWT_SECRET=ventas_jwt_secret_key_here
+
+# Configuración de archivos
+MAX_FILE_SIZE=10485760
+ALLOWED_FILE_TYPES=pdf,xml,csv,xlsx
+
+# Configuración de respaldos
+BACKUP_PATH=/var/backups/ventas
+AUTO_BACKUP=true
+BACKUP_RETENTION_DAYS=30
+
+# =============================================================================
+# VARIABLES ESPECÍFICAS DEL NEGOCIO
+# =============================================================================
+
+# Tasas de impuestos
+IVA_RATE=0.16
+ISR_RATE=0.10
+
+# Configuración de facturación
+SERIE_FACTURA=A
+SERIE_NOTA_CREDITO=B
+FOLIO_INICIAL=1
+
+# Información bancaria por defecto
+BANK_NAME=Banamex
+BANK_ACCOUNT=224996
+BANK_CLABE=002100017902249960
+BANK_BRANCH=0179
+
+# Contacto por defecto
+CONTACT_PHONE=(961) 10 5 58 20
+CONTACT_EMAIL=contacto@empresa.com
+CONTACT_WEB=www.empresa.com
+
+# =============================================================================
+# ENTORNOS ALTERNATIVOS (DEMO/PRODUCCIÓN)
+# =============================================================================
+# Para entorno DEMO (descomentar y usar si $_SESSION['curBD'] == 'Demo')
+# DB_MASTER_HOST=10.10.4.17:3390
+# DB_MASTER_USER=nickpons666
+# DB_MASTER_PASSWORD=MiPo6425@@
+# DB_EMPRESA_USER=nickpons666
+# DB_EMPRESA_PASSWORD=MiPo6425@@
diff --git a/ajax/login.php b/ajax/login.php
index 9149de8..441d003 100755
--- a/ajax/login.php
+++ b/ajax/login.php
@@ -1,21 +1,30 @@
-setEmail($_POST['email']);
- $empresa->setPassword($_POST['password']);
+ // Obtener y validar variables POST
+ $email = $_POST['email'] ?? '';
+ $password = $_POST['password'] ?? '';
+
+ if (empty($email) || empty($password)) {
+ echo 'fail[#]';
+ exit;
+ }
+
+ $empresa->setEmail($email);
+ $empresa->setPassword($password);
$empresa->setEmpresaId(15);
if(!$empresa->DoLogin())
{
echo 'fail[#]';
- $smarty->display(DOC_ROOT.'/templates/boxes/status.tpl');
}
else
{
echo 'ok[#]';
}
-
-?>
+
+ ?>
diff --git a/classes/database-manager.class.php b/classes/database-manager.class.php
new file mode 100755
index 0000000..9432f53
--- /dev/null
+++ b/classes/database-manager.class.php
@@ -0,0 +1,318 @@
+masterConnection === null) {
+ $config = SystemConfig::getMasterDatabaseConfig();
+
+ try {
+ $this->masterConnection = new mysqli(
+ $config['host'],
+ $config['user'],
+ $config['password'],
+ $config['database']
+ );
+
+ if ($this->masterConnection->connect_error) {
+ throw new Exception("Error conexión master DB: " . $this->masterConnection->connect_error);
+ }
+
+ $this->masterConnection->set_charset($config['charset']);
+ } catch (Exception $e) {
+ // Crear una conexión falsa para desarrollo sin BD
+ error_log("WARNING: No hay conexión a base de datos. Usando modo desarrollo. " . $e->getMessage());
+ $this->masterConnection = new MockDatabase();
+ }
+ }
+
+ return $this->masterConnection;
+ }
+
+ /**
+ * Obtiene conexión para empresa específica
+ * @param int $empresaId ID de la empresa
+ * @return mysqli
+ */
+ public function getEmpresaConnection($empresaId) {
+ if (!isset($this->connections[$empresaId])) {
+ $config = SystemConfig::getEmpresaDatabaseConfig($empresaId);
+
+ // Validar que exista la base de datos (con fallback)
+ if (!SystemConfig::validateDatabaseExists($config['database'])) {
+ // Intentar fallback a base de datos master
+ $masterConfig = SystemConfig::getMasterDatabaseConfig();
+ error_log("Base de datos {$config['database']} no encontrada, usando fallback a master");
+ $config = array_merge($config, [
+ 'database' => $masterConfig['database'],
+ 'user' => $masterConfig['user'],
+ 'password' => $masterConfig['password']
+ ]);
+ }
+
+ $mysqli = new mysqli(
+ $config['host'] . ':' . $config['port'],
+ $config['user'],
+ $config['password'],
+ $config['database']
+ );
+
+ if ($mysqli->connect_error) {
+ throw new Exception("Error conexión empresa $empresaId: " . $mysqli->connect_error);
+ }
+
+ $mysqli->set_charset($config['charset']);
+ $this->connections[$empresaId] = $mysqli;
+ }
+
+ return $this->connections[$empresaId];
+ }
+
+ /**
+ * Establece empresaId actual basado en usuario en sesión
+ * @param int $userId ID del usuario en sesión
+ */
+ public function setEmpresaByUser($userId) {
+ $this->currentEmpresaId = SystemConfig::getEmpresaIdByUserId($userId);
+
+ if (!$this->currentEmpresaId) {
+ throw new Exception("No se pudo determinar empresaId para usuario: $userId");
+ }
+ }
+
+ /**
+ * Establece empresaId actual directamente
+ * @param int $empresaId ID de la empresa
+ */
+ public function setEmpresaId($empresaId) {
+ $this->currentEmpresaId = (int)$empresaId;
+ }
+
+ /**
+ * Obtiene empresaId actual
+ * @return int|null
+ */
+ public function getEmpresaId() {
+ return $this->currentEmpresaId;
+ }
+
+ /**
+ * Obtiene conexión para empresa actual
+ * @return mysqli
+ */
+ public function getCurrentConnection() {
+ if (!$this->currentEmpresaId) {
+ throw new Exception("No hay empresaId establecido. Use setEmpresaByUser() o setEmpresaId()");
+ }
+
+ return $this->getEmpresaConnection($this->currentEmpresaId);
+ }
+
+ /**
+ * Cierra todas las conexiones
+ */
+ public function closeAll() {
+ if ($this->masterConnection) {
+ $this->masterConnection->close();
+ $this->masterConnection = null;
+ }
+
+ foreach ($this->connections as $connection) {
+ $connection->close();
+ }
+
+ $this->connections = [];
+ $this->currentEmpresaId = null;
+ }
+
+ /**
+ * Destructor para asegurar cierre de conexiones
+ */
+ public function __destruct() {
+ $this->closeAll();
+ }
+}
+
+/**
+ * Clase ModernDB compatible con código existente
+ * Facade sobre DatabaseManager para mantener compatibilidad
+ */
+class ModernDB {
+ private $connection;
+ private $isMaster = false;
+ private $empresaId = null;
+
+ /**
+ * Constructor
+ * @param bool $useMaster Si true, usa conexión master
+ * @param int $empresaId ID de la empresa (si no es master)
+ */
+ public function __construct($useMaster = false, $empresaId = null) {
+ $dbManager = DatabaseManager::getInstance();
+
+ if ($useMaster) {
+ $this->connection = $dbManager->getMasterConnection();
+ $this->isMaster = true;
+ } else {
+ if ($empresaId === null) {
+ $empresaId = $dbManager->getEmpresaId();
+ }
+
+ if ($empresaId === null) {
+ throw new Exception("Se requiere empresaId para conexión de empresa");
+ }
+
+ $this->connection = $dbManager->getEmpresaConnection($empresaId);
+ $this->empresaId = $empresaId;
+ }
+ }
+
+ /**
+ * Ejecuta una consulta SQL
+ * @param string $sql Consulta SQL
+ * @return mysqli_result|false
+ */
+ public function Query($sql) {
+ $result = $this->connection->query($sql);
+
+ if ($result === false) {
+ error_log("Error en consulta: " . $this->connection->error);
+ error_log("SQL: " . $sql);
+ }
+
+ return $result;
+ }
+
+ /**
+ * Obtiene una fila como arreglo asociativo
+ * @param mysqli_result $result Resultado de consulta
+ * @return array|null
+ */
+ public function FetchAssoc($result) {
+ return $result->fetch_assoc();
+ }
+
+ /**
+ * Obtiene el número de filas de un resultado
+ * @param mysqli_result $result Resultado de consulta
+ * @return int
+ */
+ public function NumRows($result) {
+ return $result->num_rows;
+ }
+
+ /**
+ * Obtiene un valor específico de una fila
+ * @param mysqli_result $result Resultado de consulta
+ * @param int $row Número de fila
+ * @param mixed $field Campo (nombre o índice)
+ * @return mixed
+ */
+ public function Result($result, $row, $field = 0) {
+ $result->data_seek($row);
+ $row_data = $result->fetch_array();
+ return is_numeric($field) ? $row_data[$field] : $row_data[$field];
+ }
+
+ /**
+ * Obtiene el último ID insertado
+ * @return int
+ */
+ public function InsertId() {
+ return $this->connection->insert_id;
+ }
+
+ /**
+ * Obtiene el número de filas afectadas
+ * @return int
+ */
+ public function AffectedRows() {
+ return $this->connection->affected_rows;
+ }
+
+ /**
+ * Libera memoria del resultado
+ * @param mysqli_result $result Resultado a liberar
+ */
+ public function FreeResult($result) {
+ $result->free();
+ }
+
+ /**
+ * Escapa caracteres especiales para prevenir SQL injection
+ * @param string $string String a escapar
+ * @return string
+ */
+ public function Escape($string) {
+ return $this->connection->real_escape_string($string);
+ }
+
+ /**
+ * Obtiene el último error de MySQL
+ * @return string
+ */
+ public function GetError() {
+ return $this->connection->error;
+ }
+
+ /**
+ * Inicia una transacción
+ */
+ public function BeginTransaction() {
+ $this->connection->begin_transaction();
+ }
+
+ /**
+ * Confirma una transacción
+ */
+ public function Commit() {
+ $this->connection->commit();
+ }
+
+ /**
+ * Revierte una transacción
+ */
+ public function Rollback() {
+ $this->connection->rollback();
+ }
+
+ /**
+ * Cierra la conexión
+ */
+ public function Close() {
+ // No cerramos conexiones individuales ya que las maneja DatabaseManager
+ }
+}
\ No newline at end of file
diff --git a/classes/db.class.php b/classes/db.class.php
index 1d12298..ce586d1 100755
--- a/classes/db.class.php
+++ b/classes/db.class.php
@@ -1,57 +1,67 @@
sqlHost = $value;
+ // Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlHost()
{
- return $this->sqlHost;
+ $config = $this->isMaster ?
+ SystemConfig::getMasterDatabaseConfig() :
+ SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
+ return $config['host'];
}
public function setSqlDatabase($value)
{
- $this->sqlDatabase = $value;
+ // Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlDatabase()
{
- return $this->sqlDatabase;
+ $config = $this->isMaster ?
+ SystemConfig::getMasterDatabaseConfig() :
+ SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
+ return $config['database'];
}
public function setSqlUser($value)
{
- $this->sqlUser = $value;
+ // Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlUser()
{
- return $this->sqlUser;
+ $config = $this->isMaster ?
+ SystemConfig::getMasterDatabaseConfig() :
+ SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
+ return $config['user'];
}
public function setSqlPassword($value)
{
- $this->sqlPassword = $value;
+ // Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlPassword()
{
- return $this->sqlPassword;
+ $config = $this->isMaster ?
+ SystemConfig::getMasterDatabaseConfig() :
+ SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
+ return $config['password'];
}
public function setQuery($value)
@@ -74,37 +84,57 @@ class DB
return $this->projectStatus;
}
- function __construct()
+ function __construct($useMaster = false, $empresaId = null)
{
- $this->sqlHost = SQL_HOST;
- $this->sqlDatabase = SQL_DATABASE;
- $this->sqlUser = SQL_USER;
- $this->sqlPassword = SQL_PASSWORD;
+ $this->dbManager = DatabaseManager::getInstance();
+ $this->isMaster = $useMaster;
+
+ if ($useMaster) {
+ $this->connection = $this->dbManager->getMasterConnection();
+ } else {
+ if ($empresaId !== null) {
+ $this->empresaId = $empresaId;
+ $this->connection = $this->dbManager->getEmpresaConnection($empresaId);
+ } elseif (isset($_SESSION['empresaId'])) {
+ $this->empresaId = $_SESSION['empresaId'];
+ try {
+ $this->connection = $this->dbManager->getEmpresaConnection($_SESSION['empresaId']);
+ } catch (Exception $e) {
+ // Fallback a master si la BD de empresa no existe
+ $this->isMaster = true;
+ $this->connection = $this->dbManager->getMasterConnection();
+ }
+ } else {
+ // Fallback directo a master
+ $this->isMaster = true;
+ $this->connection = $this->dbManager->getMasterConnection();
+ }
+ }
}
- public function DatabaseConnect()
+public function DatabaseConnect()
{
- $this->conn_id = mysql_connect($this->sqlHost, $this->sqlUser, $this->sqlPassword, 1);
- mysql_select_db($this->sqlDatabase, $this->conn_id) or die("
".mysql_error()."
");
- }
+ // Ya no se necesita, la conexión se maneja mediante DatabaseManager
+ return true;
+ }
public function ExecuteQuery()
{
- if(!$this->conn_id)
- $this->DatabaseConnect();
-
-
//TODO we might want to add some security in the queries here, but that can be done later, this is the place
if($this->projectStatus == "test")
{
//echo "
".$this->query."
";
// print_r(debug_backtrace());
- $this->sqlResult = mysql_query($this->query, $this->conn_id) or die (trigger_error($this->query.mysql_error()));
+ if ($this->connection instanceof MockDatabase) {
+ $this->sqlResult = $this->connection->query($this->query);
+ } else {
+ $this->sqlResult = mysqli_query($this->connection, $this->query) or die (trigger_error($this->query . " " . mysqli_error($this->connection)));
+ }
}
else
{
- $this->sqlResult = @mysql_query($this->query, $this->conn_id);
+ $this->sqlResult = @mysqli_query($this->connection, $this->query);
}
}
@@ -114,7 +144,7 @@ class DB
$this->ExecuteQuery();
- while($rs=mysql_fetch_assoc($this->sqlResult))
+ while($rs = $this->connection instanceof MockDatabase ? $this->sqlResult->fetch_assoc() : mysqli_fetch_assoc($this->sqlResult))
{
$retArray[] = $rs;
}
@@ -130,7 +160,7 @@ class DB
$this->ExecuteQuery();
- while($rs=mysql_fetch_assoc($this->sqlResult))
+ while($rs=mysqli_fetch_assoc($this->sqlResult))
{
$retArray[$rs[$id]] = $rs;
}
@@ -144,14 +174,22 @@ class DB
{
$this->ExecuteQuery();
- return mysql_num_rows($this->sqlResult);
+ if ($this->connection instanceof MockDatabase) {
+ return $this->sqlResult->num_rows;
+ } else {
+ return mysqli_num_rows($this->sqlResult);
+ }
}
function GetRow()
{
$this->ExecuteQuery();
- $rs=mysql_fetch_assoc($this->sqlResult);
+ if ($this->connection instanceof MockDatabase) {
+ $rs = $this->sqlResult->fetch_assoc();
+ } else {
+ $rs = mysqli_fetch_assoc($this->sqlResult);
+ }
$this->CleanQuery();
@@ -162,7 +200,12 @@ class DB
{
$this->ExecuteQuery();
- $rs=@mysql_result($this->sqlResult, 0);
+ if ($this->connection instanceof MockDatabase) {
+ $row = $this->sqlResult->fetch_array();
+ } else {
+ $row = mysqli_fetch_array($this->sqlResult);
+ }
+ $rs = $row[0];
if(!$rs)
$rs = 0;
@@ -175,7 +218,11 @@ class DB
function InsertData()
{
$this->ExecuteQuery();
- $last_id=mysql_insert_id($this->conn_id);
+ if ($this->connection instanceof MockDatabase) {
+ $last_id = $this->connection->insert_id();
+ } else {
+ $last_id = mysqli_insert_id($this->connection);
+ }
$this->CleanQuery();
@@ -186,7 +233,11 @@ class DB
{
$this->ExecuteQuery();
- $return = mysql_affected_rows($this->conn_id);
+ if ($this->connection instanceof MockDatabase) {
+ $return = $this->connection->affected_rows();
+ } else {
+ $return = mysqli_affected_rows($this->connection);
+ }
$this->CleanQuery();
@@ -200,7 +251,11 @@ class DB
function CleanQuery()
{
- @mysql_free_result($this->sqlResult);
+ if ($this->connection instanceof MockDatabase) {
+ $this->sqlResult->free();
+ } else {
+ @mysqli_free_result($this->sqlResult);
+ }
//$this->query = "";
}
@@ -209,7 +264,7 @@ class DB
$this->query = "SHOW COLUMNS FROM `$table` LIKE '$field' ";
$this->ExecuteQuery();
- $row = mysql_fetch_array( $this->sqlResult , MYSQL_NUM );
+ $row = mysqli_fetch_array( $this->sqlResult , MYSQLI_NUM );
$regex = "/'(.*?)'/";
preg_match_all( $regex , $row[1], $enum_array );
diff --git a/classes/empresa.class.php b/classes/empresa.class.php
index 483c6c0..35f8a55 100755
--- a/classes/empresa.class.php
+++ b/classes/empresa.class.php
@@ -344,7 +344,7 @@ class Empresa extends Main
{
$this->Util()->ValidateString($value, $max_chars=50, $minChars = 1, 'Email');
if($value != '')
- $this->Util()->ValidateMail($value);
+$this->Util()->ValidateMail($value, "Email");
$this->email = $value;
}
@@ -462,7 +462,7 @@ class Empresa extends Main
{
if(!$this->IsLoggedIn())
{
- $this->Util()->LoadPage('login');
+ header('Location: '.($_ENV['WEB_ROOT'] ?? '/').'/login');
return;
}
diff --git a/classes/error.class.php b/classes/error.class.php
index 84b6b73..c8ff03f 100755
--- a/classes/error.class.php
+++ b/classes/error.class.php
@@ -1,15 +1,17 @@
Util == null )
+ if(!isset($this->Util) || $this->Util == null )
{
$this->Util = new Util();
}
diff --git a/classes/main.class.php b/classes/main.class.php
index 5df5685..d8d36d3 100755
--- a/classes/main.class.php
+++ b/classes/main.class.php
@@ -1,8 +1,9 @@
Util == null )
+ if($this->utilInstance == null )
{
- $this->Util = new Util();
+ $this->utilInstance = new Util();
}
- return $this->Util;
+ return $this->utilInstance;
}
}
diff --git a/classes/system-config.class.php b/classes/system-config.class.php
new file mode 100755
index 0000000..5df34e2
--- /dev/null
+++ b/classes/system-config.class.php
@@ -0,0 +1,245 @@
+getMessage());
+}
+
+/**
+ * Clase de configuración del sistema
+ */
+class SystemConfig {
+
+ /**
+ * Determina si estamos en entorno DEMO basado en sesión
+ * @return bool
+ */
+ public static function isDemoMode() {
+ return (isset($_SESSION['curBD']) && $_SESSION['curBD'] === 'Demo');
+ }
+
+ /**
+ * Obtiene configuración de base de datos master
+ * @return array Configuración de BD master
+ */
+ public static function getMasterDatabaseConfig() {
+ $config = [
+ 'host' => $_ENV['DB_MASTER_HOST'] ?? 'localhost',
+ 'database' => $_ENV['DB_MASTER_DATABASE'] ?? 'avantikads_nmgen',
+ 'user' => $_ENV['DB_MASTER_USER'] ?? 'root',
+ 'password' => $_ENV['DB_MASTER_PASSWORD'] ?? '',
+ 'charset' => $_ENV['DB_CHARSET'] ?? 'utf8mb4'
+ ];
+
+ // Si es modo DEMO, usar configuración alternativa
+ if (self::isDemoMode()) {
+ $config['host'] = $_ENV['DB_DEMO_HOST'] ?? '10.10.4.17:3390';
+ $config['user'] = $_ENV['DB_DEMO_USER'] ?? 'nickpons666';
+ $config['password'] = $_ENV['DB_DEMO_PASSWORD'] ?? 'MiPo6425@@';
+ }
+
+ return $config;
+ }
+
+ /**
+ * Obtiene configuración de base de datos para empresa específica
+ * @param int $empresaId ID de la empresa
+ * @return array Configuración de BD para la empresa
+ */
+ public static function getEmpresaDatabaseConfig($empresaId) {
+ // Configuración base desde .env
+ $baseConfig = [
+ 'host' => $_ENV['DB_HOST'] ?? 'localhost',
+ 'port' => $_ENV['DB_PORT'] ?? '3306',
+ 'charset' => $_ENV['DB_CHARSET'] ?? 'utf8mb4'
+ ];
+
+ // Patón:avantikads_nm{empresaId} donde empresaId viene del usuario
+ $prefix = $_ENV['DB_EMPRESA_PREFIX'] ?? 'avantikads_nm';
+ $database = $prefix . $empresaId;
+
+ // Configuración de usuario/password
+ $user = $_ENV['DB_EMPRESA_USER'] ?? $_ENV['DB_USER'] ?? 'root';
+ $password = $_ENV['DB_EMPRESA_PASSWORD'] ?? $_ENV['DB_PASSWORD'] ?? '';
+
+ // Si es modo DEMO, usar configuración alternativa
+ if (self::isDemoMode()) {
+ $baseConfig['host'] = $_ENV['DB_DEMO_HOST'] ?? '10.10.4.17:3390';
+ $user = $_ENV['DB_DEMO_USER'] ?? 'nickpons666';
+ $password = $_ENV['DB_DEMO_PASSWORD'] ?? 'MiPo6425@@';
+ }
+
+ return array_merge($baseConfig, [
+ 'database' => $database,
+ 'user' => $user,
+ 'password' => $password
+ ]);
+ }
+
+ /**
+ * Valida que exista la base de datos para una empresa
+ * @param string $database Nombre de la base de datos
+ * @return bool
+ */
+ public static function validateDatabaseExists($database) {
+ try {
+ $config = self::getMasterDatabaseConfig();
+
+ // Conexión sin especificar BD para verificar existencia
+ $mysqli = new mysqli($config['host'], $config['user'], $config['password']);
+
+ if ($mysqli->connect_error) {
+ error_log("Error conexión validación: " . $mysqli->connect_error);
+ return false;
+ }
+
+ // Verificar si la base de datos existe
+ $result = $mysqli->query("SHOW DATABASES LIKE '$database'");
+ $exists = $result->num_rows > 0;
+
+ $mysqli->close();
+ return $exists;
+
+ } catch (Exception $e) {
+ error_log("Error validando BD $database: " . $e->getMessage());
+ return false;
+ }
+ }
+
+ /**
+ * Obtiene empresaId del usuario actual desde base de datos master
+ * @param int $userId ID del usuario
+ * @return int|null empresaId del usuario
+ */
+ public static function getEmpresaIdByUserId($userId) {
+ $masterConfig = self::getMasterDatabaseConfig();
+
+ try {
+ $mysqli = new mysqli(
+ $masterConfig['host'],
+ $masterConfig['user'],
+ $masterConfig['password'],
+ $masterConfig['database']
+ );
+
+ if ($mysqli->connect_error) {
+ throw new Exception("Error conexión master DB: " . $mysqli->connect_error);
+ }
+
+ $mysqli->set_charset($masterConfig['charset']);
+
+ $stmt = $mysqli->prepare("SELECT empresaId FROM usuario WHERE usuarioId = ? LIMIT 1");
+ $stmt->bind_param("i", $userId);
+ $stmt->execute();
+ $result = $stmt->get_result();
+
+ if ($row = $result->fetch_assoc()) {
+ return (int)$row['empresaId'];
+ }
+
+ return null;
+
+ } catch (Exception $e) {
+ error_log("Error obteniendo empresaId para usuario $userId: " . $e->getMessage());
+ return null;
+ }
+ }
+
+ /**
+ * Obtiene configuración general del sistema
+ * @return array Configuración general
+ */
+ public static function getSystemConfig() {
+ return [
+ 'doc_root' => $_ENV['DOC_ROOT'] ?? '/var/www/html/ventas',
+ 'web_root' => $_ENV['WEB_ROOT'] ?? 'http://localhost',
+ 'smtp_host' => $_ENV['SMTP_HOST'] ?? '',
+ 'smtp_user' => $_ENV['SMTP_USER'] ?? '',
+ 'smtp_pass' => $_ENV['SMTP_PASS'] ?? '',
+ 'smtp_port' => $_ENV['SMTP_PORT'] ?? '',
+ 'items_per_page' => $_ENV['ITEMS_PER_PAGE'] ?? '20',
+ 'min_year' => $_ENV['MIN_YEAR'] ?? '2025',
+ 'max_year' => $_ENV['MAX_YEAR'] ?? '2030',
+ 'user_pac' => $_ENV['USER_PAC'] ?? '',
+ 'pw_pac' => $_ENV['PW_PAC'] ?? '',
+ 'debug_mode' => $_ENV['DEBUG_MODE'] ?? 'false'
+ ];
+ }
+
+ /**
+ * Obtiene constantes legadas para compatibilidad
+ * @return array Constantes compatibles con sistema anterior
+ */
+ public static function getLegacyConstants() {
+ $systemConfig = self::getSystemConfig();
+ $masterConfig = self::getMasterDatabaseConfig();
+
+ return [
+ 'SQL_HOST' => $masterConfig['host'],
+ 'SQL_DATABASE' => $masterConfig['database'], // avantikads_nmgen
+ 'SQL_DATABASE2' => $_ENV['DB_EMPRESA_PREFIX'] ?? 'avantikads_nm', // prefix sin número
+ 'SQL_USER' => $masterConfig['user'],
+ 'SQL_PASSWORD' => $masterConfig['password'],
+ 'DOC_ROOT' => $systemConfig['doc_root'],
+ 'WEB_ROOT' => $systemConfig['web_root'],
+ 'SMTP_HOST' => $systemConfig['smtp_host'],
+ 'SMTP_USER' => $systemConfig['smtp_user'],
+ 'SMTP_PASS' => $systemConfig['smtp_pass'],
+ 'SMTP_PORT' => $systemConfig['smtp_port'],
+ 'ITEMS_PER_PAGE' => $systemConfig['items_per_page'],
+ 'MIN_YEAR' => (int)$systemConfig['min_year'],
+ 'MAX_YEAR' => (int)$systemConfig['max_year'],
+ 'USER_PAC' => $systemConfig['user_pac'],
+ 'PW_PAC' => $systemConfig['pw_pac']
+ ];
+ }
+}
+
+/**
+ * Define constantes legadas para compatibilidad con código existente
+ */
+function defineLegacyConstants() {
+ $constants = SystemConfig::getLegacyConstants();
+
+ foreach ($constants as $name => $value) {
+ if (!defined($name)) {
+ define($name, $value);
+ }
+ }
+}
+
+// Definir constantes legadas para compatibilidad
+defineLegacyConstants();
\ No newline at end of file
diff --git a/classes/user.class.php b/classes/user.class.php
index 56731df..159487b 100755
--- a/classes/user.class.php
+++ b/classes/user.class.php
@@ -7,9 +7,10 @@ class User extends Main
{
$generalDb = new DB;
+ $loginKey = $_SESSION["loginKey"] ?? '';
$sql = "SELECT * FROM usuario
LEFT JOIN empresa ON usuario.empresaId = empresa.empresaId
- WHERE usuarioId = '".$_SESSION["loginKey"]."'";
+ WHERE usuarioId = '".$loginKey."'";
$generalDb->setQuery($sql);
$info = $generalDb->GetRow();
diff --git a/classes/util.class.php b/classes/util.class.php
index 3df6d0b..1eec649 100755
--- a/classes/util.class.php
+++ b/classes/util.class.php
@@ -1,7 +1,11 @@
DBSelect == null )
- {
- $this->DBSelect = new DB();
- }
- $this->DBSelect->setSqlDatabase(SQL_DATABASE2.$empresaId);
- return $this->DBSelect;
+ // Usar nueva arquitectura multi-empresa con DatabaseManager
+ $dbManager = DatabaseManager::getInstance();
+ return new DB(false, $empresaId);
}
function RoundNumber($number)
@@ -41,141 +42,129 @@ class Util extends Error
{
$hms = "";
$hours = intval(intval($sec) / 3600);
- $hms .= ($padHours)
- ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
- : $hours. ':';
+ $hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT). ":" : $hours. ":";
$minutes = intval(($sec / 60) % 60);
- $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
+ $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";
$seconds = intval($sec % 60);
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
+
return $hms;
}
-
- function FormatNumber($number, $dec = 2)
+
+ function FormatMins($mins)
{
- return number_format($number, $dec);
- }
-
- function FormatDateAndTime($time)
- {
- $time = date("Y-m-d H:i:s", $time);
- return $time;
- }
-
- function FormatDateAndTimeSat($date)
- {
- $time = strtotime($date);
- $date = date("d/m/Y H:i:s", $time);
- return $date;
- }
-
- function FormatDate($time)
- {
- $time = date("Y-m-d", $time);
- return $time;
- }
-
- function FormatDateMySql($date)
- {
- return date('Y-m-d',strtotime($date));
- }
-
- function ValidateInteger(&$number, $max = 0, $min = 0)
- {
- if (!preg_match("/^[0-9]+$/",$number)) $number = 0;
-
- if($min != 0 && $number < $min)
- {
- $number = $min;
- return;
- }
-
- if($max != 0 && $number > $max)
- {
- $number = $max;
- return;
- }
-
- if($number > 9223372036854775807)
+ if($mins > 60)
{
- $number = 9223372036854775807;
+ $horas = floor($mins / 60);
+ $mins = $mins - ($horas * 60);
+ return $horas."h ".$mins."m";
}
+ else
+ {
+ return $mins."m";
+ }
+ }
+
+ function FormatFecha($date)
+ {
+ $meses = array(1 => "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
+ $trozos = explode("-",$date);
+ $trozos[1] = $meses[(int)$trozos[1]];
+ $trozos[2] = substr($trozos[2],0,2);
+ return $trozos[2]." de ".$trozos[1]." de ".$trozos[0];
}
- function ValidateFloat(&$number, $decimals = 2, $max = 0, $min = 0)
+ function FormatHora($hora)
{
- if (!is_numeric($number))
- {
- $number=0;
- }
-
- $number=round($number, $decimals);
-
- if($max != 0 && $number > $max)
- {
- $number = $max;
- return;
- }
-
- if($min != 0 && $number < $min)
- {
- $number = $min;
- return;
- }
-
- if($number>9223372036854775807)
- {
- $number=9223372036854775807;
- }
+ $trozos = explode(":",$hora);
+ return $trozos[0].":".$trozos[1]." hrs";
}
- function CheckDomain($domain,$server,$findText)
- {
- // Open a socket connection to the whois server
- $con = fsockopen($server, 43);
- if (!$con)
- {
- return false;
- }
- // Send the requested doman name
- fputs($con, $domain."\r\n");
-
- // Read and store the server response
- $response = ' :';
- while(!feof($con)) {
- $response .= fgets($con,128);
- }
-
- fclose($con);
-
- // Check the response stream whether the domain is available
- if (strpos($response, $findText)){
- return true;
- }
- else {
- return false;
- }
- }
-
- function ValidateString(&$string, $max_chars=5000, $minChars = 1, $field = null)
+ function FormatFechaHora($date)
{
- $string=htmlspecialchars($string, ENT_QUOTES, 'ISO-8859-1');
+ $meses = array(1 => "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
+ $trozos = explode(" ",$date);
+ $fecha = $trozos[0];
+ $hora = $trozos[1];
- $string = trim($string);
+ $trozosFecha = explode("-",$fecha);
+ $trozosFecha[1] = $meses[(int)$trozosFecha[1]];
+ $trozosFecha[2] = substr($trozosFecha[2],0,2);
+ $fechaFormateada = $trozosFecha[2]." de ".$trozosFecha[1]." de ".$trozosFecha[0];
- if(strlen($string)<$minChars)
- {
- return $this->setError(10013, "error", "", $field);
- }
-
- if(strlen($string)>$max_chars)
- {
- $string = substr($string,0,$max_chars);
- }
- }
+ $trozosHora = explode(":",$hora);
+ $horaFormateada = $trozosHora[0].":".$trozosHora[1]." hrs";
+
+ return $fechaFormateada." ".$horaFormateada;
+ }
+
+ function FormatFechaServidor($date)
+ {
+ $trozos = explode("/",$date);
+ $trozos[1] = (strlen($trozos[1]) == 1) ? "0".$trozos[1] : $trozos[1];
+ $trozos[0] = (strlen($trozos[0]) == 1) ? "0".$trozos[0] : $trozos[0];
+ return $trozos[2]."-".$trozos[1]."-".$trozos[0];
+ }
-
- function ValidateMail($mail, $field = 'Email')
+ function GetMonth($mes, $format = null)
+ {
+ $meses = array(1 => "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
+ $mesesAbrev = array(1 => "Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic");
+ if($format == "abreviado")
+ return $mesesAbrev[(int)$mes];
+ else
+ return $meses[(int)$mes];
+ }
+
+ function GetLetters($string)
+ {
+ preg_match_all('/([a-zA-Z]+)/',$string,$results);
+ return implode('', $results[0]);
+ }
+
+ function GetNumbers($string)
+ {
+ preg_match_all('/([0-9]+)/',$string,$results);
+ return implode('', $results[0]);
+ }
+
+ function ValidateString($value, $max_chars=50, $minChars = 1, $field = "Atributo")
+ {
+ if(strlen($value) > $max_chars || strlen($value) < $minChars)
+ {
+ return $this->setError(10001, "error", "", $field);
+ }
+ else
+ {
+ return true;
+ }
+ }//validateString
+
+ function ValidateInteger($value, $max = null, $min = null, $field = "Atributo")
+ {
+ if(ctype_digit($value) == false)
+ {
+ return $this->setError(10002, "error", "", $field);
+ }
+ else
+ {
+ return true;
+ }
+ }//ValidateInteger
+
+ function ValidateFloat($value, $field)
+ {
+ if(!is_numeric($value))
+ {
+ return $this->setError(10002, "error", "", $field);
+ }
+ else
+ {
+ return true;
+ }
+ }//ValidateFloat
+
+ function ValidateMail($mail, $field)
{
$mail = strtolower($mail);
if (!preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',trim($mail)))
@@ -188,7 +177,8 @@ class Util extends Error
{
if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url))
{
- return $this->error = 10001;
+ $this->error = 10001;
+ return 10001;
}
}
@@ -197,367 +187,142 @@ class Util extends Error
$handle = @fopen($pathToFile, "r");
if ($handle === false)
{
- return $this->error = 10003;
+ $this->error = 10003;
+ return 10003;
}
fclose($handle);
+ return true;
}
function wwwRedirect()
{
if(!preg_match("/^www./", $_SERVER['HTTP_HOST']))
{
- header("location: ".WEB_ROOT);
+ header("location: http://www.".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}
}
- function MakeTime($day, $month, $year)
+ function UploadFile($name, $dir)
{
- return mktime(0,0,0, $month, $day, $year);
- }
-
- function CreateDropDown($name, $from, $to, $selectedIndex)
- {
- $select = "";
- return $select;
- }
-
- function GetCurrentYear()
- {
- return date("Y", time());
- }
-
- function GetCents($num)
- {
- $num = round($num, 2);
- $cents = ($num - floor($num))*100;
- $cents = round($cents, 2);
- $cents = ceil($cents);
- if($cents < 10)
- $cents = "0".$cents;
- return $cents;
- }
-
- function num2letras($num, $fem = true, $dec = true)
- {
- $matuni[2] = "dos";
- $matuni[3] = "tres";
- $matuni[4] = "cuatro";
- $matuni[5] = "cinco";
- $matuni[6] = "seis";
- $matuni[7] = "siete";
- $matuni[8] = "ocho";
- $matuni[9] = "nueve";
- $matuni[10] = "diez";
- $matuni[11] = "once";
- $matuni[12] = "doce";
- $matuni[13] = "trece";
- $matuni[14] = "catorce";
- $matuni[15] = "quince";
- $matuni[16] = "dieciseis";
- $matuni[17] = "diecisiete";
- $matuni[18] = "dieciocho";
- $matuni[19] = "diecinueve";
- $matuni[20] = "veinte";
- $matunisub[2] = "dos";
- $matunisub[3] = "tres";
- $matunisub[4] = "cuatro";
- $matunisub[5] = "quin";
- $matunisub[6] = "seis";
- $matunisub[7] = "sete";
- $matunisub[8] = "ocho";
- $matunisub[9] = "nove";
-
- $matdec[2] = "veint";
- $matdec[3] = "treinta";
- $matdec[4] = "cuarenta";
- $matdec[5] = "cincuenta";
- $matdec[6] = "sesenta";
- $matdec[7] = "setenta";
- $matdec[8] = "ochenta";
- $matdec[9] = "noventa";
- $matsub[3] = 'mill';
- $matsub[5] = 'bill';
- $matsub[7] = 'mill';
- $matsub[9] = 'trill';
- $matsub[11] = 'mill';
- $matsub[13] = 'bill';
- $matsub[15] = 'mill';
- $matmil[4] = 'millones';
- $matmil[6] = 'billones';
- $matmil[7] = 'de billones';
- $matmil[8] = 'millones de billones';
- $matmil[10] = 'trillones';
- $matmil[11] = 'de trillones';
- $matmil[12] = 'millones de trillones';
- $matmil[13] = 'de trillones';
- $matmil[14] = 'billones de trillones';
- $matmil[15] = 'de billones de trillones';
- $matmil[16] = 'millones de billones de trillones';
-
- $num = trim((string)@$num);
- if ($num[0] == '-') {
- $neg = 'menos ';
- $num = substr($num, 1);
- }else
- $neg = '';
- while ($num[0] == '0') $num = substr($num, 1);
- if ($num[0] < '1' or $num[0] > 9) $num = '0' . $num;
- $zeros = true;
- $punt = false;
- $ent = '';
- $fra = '';
- for ($c = 0; $c < strlen($num); $c++) {
- $n = $num[$c];
- if (! (strpos(".,'''", $n) === false)) {
- if ($punt) break;
- else{
- $punt = true;
- continue;
- }
-
- }elseif (! (strpos('0123456789', $n) === false)) {
- if ($punt) {
- if ($n != '0') $zeros = false;
- $fra .= $n;
- }else
-
- $ent .= $n;
- }else
-
- break;
-
- }
- $ent = ' ' . $ent;
- if ($dec and $fra and ! $zeros) {
- $fin = ' coma';
- for ($n = 0; $n < strlen($fra); $n++) {
- if (($s = $fra[$n]) == '0')
- $fin .= ' cero';
- elseif ($s == '1')
- $fin .= $fem ? ' una' : ' un';
- else
- $fin .= ' ' . $matuni[$s];
- }
- }else
- $fin = '';
- if ((int)$ent === 0) return 'Cero ' . $fin;
- $tex = '';
- $sub = 0;
- $mils = 0;
- $neutro = false;
- while ( ($num = substr($ent, -3)) != ' ') {
- $ent = substr($ent, 0, -3);
- if (++$sub < 3 and $fem) {
- $matuni[1] = 'una';
- $subcent = 'as';
- }else{
- $matuni[1] = $neutro ? 'un' : 'uno';
- $subcent = 'os';
- }
- $t = '';
- $n2 = substr($num, 1);
- if ($n2 == '00') {
- }elseif ($n2 < 21)
- $t = ' ' . $matuni[(int)$n2];
- elseif ($n2 < 30) {
- $n3 = $num[2];
- if ($n3 != 0) $t = 'i' . $matuni[$n3];
- $n2 = $num[1];
- $t = ' ' . $matdec[$n2] . $t;
- }else{
- $n3 = $num[2];
- if ($n3 != 0) $t = ' y ' . $matuni[$n3];
- $n2 = $num[1];
- $t = ' ' . $matdec[$n2] . $t;
- }
- $n = $num[0];
- if ($n == 1) {
- $t = ' ciento' . $t;
- }elseif ($n == 5){
- $t = ' ' . $matunisub[$n] . 'ient' . $subcent . $t;
- }elseif ($n != 0){
- $t = ' ' . $matunisub[$n] . 'cient' . $subcent . $t;
- }
- if ($sub == 1) {
- }elseif (! isset($matsub[$sub])) {
- if ($num == 1) {
- $t = ' mil';
- }elseif ($num > 1){
- $t .= ' mil';
- }
- }elseif ($num == 1) {
- $t .= ' ' . $matsub[$sub] . 'on';
- }elseif ($num > 1){
- $t .= ' ' . $matsub[$sub] . 'ones';
- }
- if ($num == '000') $mils ++;
- elseif ($mils != 0) {
- if (isset($matmil[$sub])) $t .= ' ' . $matmil[$sub];
- $mils = 0;
- }
- $neutro = true;
- $tex = $t . $tex;
- }
- $tex = $neg . substr($tex, 1) . $fin;
- return ucfirst($tex);
- }
-
- function ImprimeNoFolio($folio)
- {
- return sprintf("%05d", $folio);
- }
-
- function ConvertirMes($mes)
- {
- $mesArray = array("N/A", "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre");
- return $mesArray[$mes];
- }
-
- function LoadPage($page, $extendible = "")
- {
- header("location: ".WEB_ROOT."/".$page.$extendible);
- exit;
- }
-
- function Today()
- {
- return date("Y-m-d");
- }
-
- function TodayHour()
- {
- return date("Y-m-d H:i:s");
- }
-
- function ExtractPeriod()
- {
- $today = $this->Today();
- $this->DB()->setQuery("SELECT periodoId FROM periodo WHERE status = 'activo'");
-
- $period = $this->DB()->GetSingle();
-
- if($period == 0)
- {
- $period = 1;
- }
-
- return $period;
- }
-
- function GetReservationDays($in, $out)
- {
- $entrada = explode("-", $in);
- $entrada[2] = str_replace(" 00:00:00", "", $entrada[2]);
-
- $fechaEntrada = $this->MakeTime($entrada[2], $entrada[1], $entrada[0]);
-
- $salida = explode("-", $out);
- $salida[2] = str_replace(" 00:00:00", "", $salida[2]);
- $fechaSalida = $this->MakeTime($salida[2], $salida[1], $salida[0]);
-
- return $days = ($fechaSalida - $fechaEntrada) / (3600 * 24);
- }
-
- function LoadUrl($url)
- {
- header("location: ".$url);
- }
-
-
-function HandleMultipages($page,$total,$link,$items_per_page=0,$pagevar="p"){
-
- if(!$items_per_page)
- $items_per_page = ITEMS_PER_PAGE;
-
- $pages["items_per_page"] = $items_per_page;
-
- if(empty($page)){
- $page = 0;
- }//if
-
- $pages["start"] = $page*$items_per_page;
- $pages["end"] = $pages["start"] + $items_per_page;
- if($pages["end"] > $total){
- $pages["end"] = $total;
- }//if
-
- if($total%$items_per_page == 0){
- $total_pages = $total/$items_per_page - 1;
- if($total_pages < 0){
- $total_pages = 0;
- }//if
- }//if
- else{
- $total_pages = (int)($total/$items_per_page);
- }//else
-
- if($page > 0){
- if(!$this->hs_eregi("\|$pagevar\|",$link))
- $pages["prev"] = $link."/".$pagevar."/".($page-1);
else
- $pages["prev"] = $this->hs_ereg_replace("\|$pagevar\|",(string)($page-1),$link);
- }//if
-
- if($total_pages > 0){
- if($total_pages > 15){
- $start = $page - 7;
- if($start < 0)
- $start = 0;
- $end = $start + 15;
- if($end > $total_pages){
- $end = $total_pages;
- $start = $end - 15;
- }//if
- }//if
- else{
- $start = 0;
- $end = $total_pages;
- }//else
- for($i=$start;$i<=$end;$i++){
+ {
+ $newFileName = time().".".$ext;
+
+ if(move_uploaded_file($_FILES[$name]['tmp_name'], WEB_ROOT ."/".$dir."/".$newFileName))
+ {
+ return $newFileName;
+ }
+ else
+ {
+ $this->setError(10011, "error", "", "Imagen");
+ }
+ }
+ }
+
+ function UploadImage($name, $dir, $size = null)
+ {
+ $path_parts = pathinfo($_FILES[$name]['name']);
+ $ext = strtolower($path_parts["extension"]);
+ $extArray = array("jpg", "png", "gif", "jpeg");
+
+ if(!in_array($ext, $extArray))
+ {
+ return $this->setError(10010, "error", "", "Imagen");
+ }
+
+ if($size)
+ {
+ list($width, $height) = getimagesize($_FILES[$name]['tmp_name']);
+ if($width > $size || $height > $size)
+ return $this->setError(10012, "error", "", "Imagen");
+ }
+
+ $newFileName = time().".".$ext;
+
+ if(move_uploaded_file($_FILES[$name]['tmp_name'], WEB_ROOT ."/".$dir."/".$newFileName))
+ {
+ return $newFileName;
+ }
+ else
+ {
+ return $this->setError(10011, "error", "", "Imagen");
+ }
+ }
+
+ function HandleMultipages($page, $total_pages, $link, $pagevar, $limit = 5)
+ {
+ $pages = array();
+
+ if($page == 0)
+ $page == 0;
+
+ if($page > 0){
if(!$this->hs_eregi("\|$pagevar\|",$link))
- $pages["numbers"][$i+1] = $link."/".$pagevar."/".$i;
+ $pages["first"] = $link."/".$pagevar."/0";
else
- $pages["numbers"][$i+1] = $this->hs_ereg_replace("\|$pagevar\|",(string)$i,$link);
- }//for
- }//if
-
- if($page < $total_pages){
- if(!$this->hs_eregi("\|$pagevar\|",$link))
- $pages["next"] = $link."/".$pagevar."/".($page+1);
- else
- $pages["next"] = hs_ereg_replace("\|$pagevar\|",(string)($page+1),$link);
- }//if
-
- if($page > 0){
- if(!$this->hs_eregi("\|$pagevar\|",$link))
- $pages["first"] = $link."/".$pagevar."/0";
- else
- $pages["first"] = hs_ereg_replace("\|$pagevar\|","0",$link);
- }
-
- if($page < $total_pages){
- if(!$this->hs_eregi("\|$pagevar\|",$link))
- $pages["last"] = $link."/".$pagevar."/".$total_pages;
- else
- $pages["last"] = hs_ereg_replace("\|$pagevar\|",(string)($total_pages),$link);
- }
-
- $pages["current"] = $page+1;
-
- return $pages;
-
-}//handle_multipages
-
- function hs_eregi($var1,$var2,$var3 = null){
+ $pages["first"] = $this->hs_preg_replace("\|$pagevar\|","0",$link);
+ }
+ if($page > 0){
+ if(!$this->hs_eregi("\|$pagevar\|",$link))
+ $pages["prev"] = $link."/".$pagevar."/".($page-1);
+ else
+ $pages["prev"] = $this->hs_preg_replace("\|$pagevar\|",(string)($page-1),$link);
+ }
+
+ for($i = ($page - $limit); $i < $page; $i++)
+ {
+ if($i >= 0)
+ {
+ if(!$this->hs_eregi("\|$pagevar\|",$link))
+ $pages[$i] = $link."/".$pagevar."/".$i;
+ else
+ $pages[$i] = $this->hs_preg_replace("\|$pagevar\|",(string)($i),$link);
+ }
+ }
+
+ $pages[$page] = $link."/".$pagevar."/".$page;
+
+ for($i = ($page + 1); $i <= ($page + $limit); $i++)
+ {
+ if($i <= $total_pages)
+ {
+ if(!$this->hs_eregi("\|$pagevar\|",$link))
+ $pages[$i] = $link."/".$pagevar."/".$i;
+ else
+ $pages[$i] = $this->hs_preg_replace("\|$pagevar\|",(string)($i),$link);
+ }
+ }
+
+ if($page < $total_pages){
+ if(!$this->hs_eregi("\|$pagevar\|",$link))
+ $pages["next"] = $link."/".$pagevar."/".($page+1);
+ else
+ $pages["next"] = $this->hs_preg_replace("\|$pagevar\|",(string)($page+1),$link);
+ }
+
+ if($page > 0){
+ if(!$this->hs_eregi("\|$pagevar\|",$link))
+ $pages["last"] = $link."/".$pagevar."/".$total_pages;
+ else
+ $pages["last"] = $this->hs_preg_replace("\|$pagevar\|",(string)($total_pages),$link);
+ }
+
+ $pages["current"] = $page+1;
+
+ return $pages;
+
+ }//handle_multipages
+
+ function hs_eregi($var1,$var2,$var3 = array()){
if(function_exists("mb_eregi"))
return mb_eregi($var1,$var2,$var3);
@@ -567,14 +332,12 @@ function HandleMultipages($page,$total,$link,$items_per_page=0,$pagevar="p"){
}//hs_eregi
- function hs_ereg_replace($var1,$var2,$var3){
+ function hs_preg_replace($var1,$var2,$var3){
- if(function_exists("mb_ereg_replace"))
- return mb_ereg_replace($var1,$var2,$var3);
- else
- return ereg_replace($var1,$var2,$var3);
+ // mb_preg_replace no existe, usar directamente preg_replace
+ return preg_replace($var1,$var2,$var3);
- }//hs_ereg_replace
+ }//hs_preg_replace
function SexoString($sex)
{
@@ -588,16 +351,20 @@ function HandleMultipages($page,$total,$link,$items_per_page=0,$pagevar="p"){
}
function CalculateIva($price)
{
- return $price * (IVA / 100);
+ $ivaRate = 16; // Default IVA rate
+ if (isset($_ENV['IVA_RATE'])) {
+ $ivaRate = (float)$_ENV['IVA_RATE'] * 100; // Convert from decimal to percentage
+ }
+ return $price * ($ivaRate / 100);
}
function ReturnLang()
{
- if(!$_SESSION['lang'])
+ if(!isset($_SESSION['lang']) || !$_SESSION['lang'])
{
$lang = "es";
}
- elseif($_SESSION['lang'] == "es")
+ elseif(isset($_SESSION['lang']) && $_SESSION['lang'] == "es")
{
$lang = "es";
}
@@ -618,444 +385,329 @@ function HandleMultipages($page,$total,$link,$items_per_page=0,$pagevar="p"){
{
if ($_SERVER)
{
- if ( $_SERVER["HTTP_X_FORWARDED_FOR"] )
+ if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
- $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
- }
- elseif ( $_SERVER["HTTP_CLIENT_IP"] )
+ $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
+ }
+ elseif (isset($_SERVER['HTTP_CLIENT_IP']))
{
- $realip = $_SERVER["HTTP_CLIENT_IP"];
- }
- else
+ $realip = $_SERVER['HTTP_CLIENT_IP'];
+ }
+ else
{
- $realip = $_SERVER["REMOTE_ADDR"];
+ $realip = $_SERVER['REMOTE_ADDR'];
}
- }
- else
+ }
+ else
{
if ( getenv( 'HTTP_X_FORWARDED_FOR' ) )
{
$realip = getenv( 'HTTP_X_FORWARDED_FOR' );
- } elseif ( getenv( 'HTTP_CLIENT_IP' ) )
+ }
+ elseif ( getenv( 'HTTP_CLIENT_IP' ) )
{
- $realip = getenv( 'HTTP_CLIENT_IP' );
- } else
+ $realip = getenv( 'HTTP_CLIENT_IP' );
+ }
+ else
{
$realip = getenv( 'REMOTE_ADDR' );
}
}
- return $realip;
+ return $realip;
+ }
- }
-
- function validateDateFormat($date, $field)
+ function setError($value = NULL, $type="error", $custom = "", $errorField = "")
{
- //match the format of the date
- if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $date, $parts))
- {
- //check weather the date is valid of not
- if(checkdate($parts[2],$parts[1],$parts[3]))
- return true;
- else
- return $this->setError(10011, "error", "", $field);
+ $this->type[] = $type;
+ $this->error[] = $value;
+ $this->errorField[] = $errorField;
+ $this->complete = true;
+ }
+
+ function GetError()
+ {
+ if(!$this->complete)
+ return false;
+ else
+ return true;
+ }
+
+ function GetErrors()
+ {
+ return $this->error;
+ }
+
+ function GetErrorField()
+ {
+ return $this->errorField;
+ }
+
+ function GetType()
+ {
+ return $this->type;
+ }
+
+ function GetField()
+ {
+ return $this->errorField;
+ }
+
+ function Reset()
+ {
+ $this->type = array();
+ $this->error = array();
+ $this->errorField = array();
+ $this->complete = false;
+ }
+
+ function PrintErrors()
+ {
+ $errorStr = "
"; - print_r($variable); - echo ""; - } - - function CadenaOriginalVariableFormat($cadena, $formatNumber = false, $addPipe = true) - { - //change tabs, returns and newlines into spaces - $cadena = utf8_encode(urldecode($cadena)); - $cadena = str_replace("|","/",$cadena); - $remove = array("\t", "\n", "\r\n", "\r"); - $cadena = str_replace($remove, ' ', $cadena); - - $cadena = str_replace("+","MAS",$cadena); - - $cadena = str_replace("&","&",$cadena); - - $pat[0] = "/^\s+/"; - $pat[1] = "/\s{2,}/"; - $pat[2] = "/\s+\$/"; - $rep[0] = ""; - $rep[1] = ""; - $rep[2] = ""; - $cadena = preg_replace($pat,$rep,$cadena); - - if($formatNumber) - { - $cadena = number_format($cadena, 2, ".", ""); - } - - if(strlen($cadena) > 0 && $addPipe) - { - $cadena .= "|"; - } - $cadena = urldecode(utf8_decode($cadena)); - return $cadena = trim($cadena); - } - - function CadenaOriginalPDFFormat($cadena, $formatNumber = false, $addPipe = true) - { - //change tabs, returns and newlines into spaces - $cadena = utf8_decode(($cadena)); - $cadena = str_replace("|","/",$cadena); -// $remove = array("\t", "\n", "\r\n", "\r"); -// $cadena = str_replace($remove, ' ', $cadena); - - $pat[0] = "/^\s+/"; - $pat[1] = "/\s{2,}/"; - $pat[2] = "/\s+\$/"; - $rep[0] = ""; - $rep[1] = " "; - $rep[2] = ""; - $cadena = preg_replace($pat,$rep,$cadena); - - if($formatNumber) - { - $cadena = number_format($cadena, 2, ".", ","); - } - - if(strlen($cadena) > 0 && $addPipe) - { - $cadena .= "|"; - } - //remove extra pipe - //$cadena = substr($cadena, 0, -1); - return $cadena = trim($cadena); - } - - function CadenaOriginalVariableFormatNew($cadena, $formatNumber = false, $addPipe = true) - { - //change tabs, returns and newlines into spaces - $cadena = utf8_encode(urldecode($cadena)); - $cadena = str_replace("|","/",$cadena); - $remove = array("\t", "\n", "\r\n", "\r"); - $cadena = str_replace($remove, ' ', $cadena); - - $pat[0] = "/^\s+/"; - $pat[1] = "/\s{2,}/"; - $pat[2] = "/\s+\$/"; - $rep[0] = ""; - $rep[1] = " "; - $rep[2] = ""; - $cadena = preg_replace($pat,$rep,$cadena); - - if($formatNumber) - { - $cadena = number_format($cadena, 2, ".", ","); - } - - if(strlen($cadena) > 0 && $addPipe) - { - $cadena .= "|"; - } - $cadena = urldecode(utf8_decode($cadena)); - return $cadena = trim($cadena); - } - - function DecodeVal($value){ - - return urldecode($value); - - }//decodeVal - - function EncodeRow($row){ - - foreach($row as $key => $val){ - $info[$key] = utf8_encode($val); - } - - return $info; - - } - - function DecodeRow($row){ - - foreach($row as $key => $val){ - $info[$key] = utf8_decode($val); - } - - return $info; - - } - - function DecodeUrlRow($row){ - - foreach($row as $key => $val){ - $info[$key] = urldecode($val); - } - - return $info; - - } - - function DecodeResult($result){ - - foreach($result as $k => $row){ - - foreach($row as $key => $val){ - $info[$key] = utf8_decode($val); + $text = $text; + $length = strlen($text); + $out = ''; + $escaped = false; + for($i=0;$i<$length;$i++) { + $c = ord($text[$i]); + if ($c > 128) { + if (!$escaped) { + $out .= ''; + $escaped = true; + } + $out .= $c; + } else { + if ($escaped) { + $out .= ';'; + $escaped = false; + } + $out .= $text[$i]; } - - $card[$k] = $info; - } - - return $card; - + if ($escaped) { + $out .= ';'; + $escaped = false; + } + return $out; } - function EncodeResult($result){ - - foreach($result as $k => $row){ - - foreach($row as $key => $val){ - $info[$key] = utf8_encode($val); - } - - $card[$k] = $info; - - } - - return $card; - - } - - function DecodeUrlResult($result){ - - if(!$result) - return; - - foreach($result as $k => $row){ - - foreach($row as $key => $val){ - $info[$key] = urldecode($val); - } - - $card[$k] = $info; - - } - - return $card; - - } - - function PadStringLeft($input, $chars, $char) + function SendContactFormEmail($fields, $recipients, $subject, $extra = null) { - return str_pad($input, $chars, $char, STR_PAD_LEFT); - } - - function Unzip($path, $file) - { - $zip = new ZipArchive; - $res = $zip->open($file); - if ($res === true) + global $mail; + + $mail->ClearAddresses(); + + foreach($recipients as $recipient) + $mail->AddAddress($recipient); + + $mail->Subject = $subject; + $mail->From = SMTP_USER; + $mail->FromName = "Ventas System"; + + $body = "
| ".$field." | "; + $body .= "".$value." | "; + $body .= "
| ".$key." | "; + $body .= "".$val." | "; + $body .= "