diff --git a/services/NormalBot.php b/services/NormalBot.php index 13ce3be..75cbd86 100644 --- a/services/NormalBot.php +++ b/services/NormalBot.php @@ -458,12 +458,12 @@ class NormalBot $clave = $company['id'] . '|' . $waNumber; if (isset($cache[$clave])) return $cache[$clave]; - $vacio = ['tercero_id' => null, 'modulos' => []]; + $vacio = ['tercero_id' => null, 'modulos' => [], 'es_supervisor' => false]; if ($waNumber === '') return $vacio; try { $stmt = db()->prepare( - "SELECT tercero_id, modulos_json FROM company_phones WHERE company_id = ? AND wa_number = ? LIMIT 1" + "SELECT tercero_id, modulos_json, es_supervisor FROM company_phones WHERE company_id = ? AND wa_number = ? LIMIT 1" ); $stmt->execute([(int)$company['id'], $waNumber]); $row = $stmt->fetch(); @@ -474,7 +474,8 @@ class NormalBot return $cache[$clave] = [ 'tercero_id' => $row['tercero_id'] ?: null, - 'modulos' => json_decode((string)($row['modulos_json'] ?? ''), true) ?: [], + 'modulos' => json_decode((string)($row['modulos_json'] ?? ''), true) ?: [], + 'es_supervisor' => !empty($row['es_supervisor']), ]; } @@ -1063,14 +1064,17 @@ class NormalBot $collected = []; $startIdx = 0; - // from_phone: el dato sale del perfil del numero, no se pregunta. Con un - // tercero vinculado el trabajador reporta lo suyo y el bot no le pide - // que se busque a si mismo; sin vinculo es supervisor y elige. + // from_phone: el dato sale del perfil del numero en vez de preguntarse. + // Un trabajador reporta lo suyo y no se le pide que se busque a si + // mismo; un supervisor reporta por otros, asi que se le pregunta igual + // aunque tenga su propio tercero vinculado. $perfil = self::perfilDelNumero($company, $context['from'] ?? ''); - foreach ($fields as $f) { - $origen = $f['from_phone'] ?? ''; - if ($origen !== '' && !empty($perfil[$origen])) { - $collected[$f['key']] = (string)$perfil[$origen]; + if (empty($perfil['es_supervisor'])) { + foreach ($fields as $f) { + $origen = $f['from_phone'] ?? ''; + if ($origen !== '' && !empty($perfil[$origen])) { + $collected[$f['key']] = (string)$perfil[$origen]; + } } } diff --git a/services/PhoneSync.php b/services/PhoneSync.php index 8f73dfb..a49b9ff 100644 --- a/services/PhoneSync.php +++ b/services/PhoneSync.php @@ -71,13 +71,14 @@ class PhoneSync $db = db(); $upsert = $db->prepare(" - INSERT INTO company_phones (company_id, wa_number, label, permission_type, tercero_id, modulos_json, is_active) - VALUES (?, ?, ?, ?, ?, ?, 1) + INSERT INTO company_phones (company_id, wa_number, label, permission_type, tercero_id, modulos_json, es_supervisor, is_active) + VALUES (?, ?, ?, ?, ?, ?, ?, 1) ON DUPLICATE KEY UPDATE label = VALUES(label), permission_type = VALUES(permission_type), tercero_id = VALUES(tercero_id), modulos_json = VALUES(modulos_json), + es_supervisor = VALUES(es_supervisor), is_active = 1 "); @@ -86,8 +87,8 @@ class PhoneSync $waNumber = trim($n['wa_number'] ?? ''); if ($waNumber === '') continue; - // tercero_id vinculado = perfil trabajador: el bot no le pregunta - // de quién es el registro. Sin él, es supervisor y elige. + // Un supervisor reporta por otros, así que se le pregunta de quién + // es el registro aunque tenga su propio tercero vinculado. $tercero = !empty($n['tercero_id']) ? (int)$n['tercero_id'] : null; $modulos = (array)($n['modulos'] ?? []); @@ -98,6 +99,7 @@ class PhoneSync (int)($n['permiso'] ?? 1), $tercero, $modulos ? json_encode($modulos) : null, + !empty($n['es_supervisor']) ? 1 : 0, ]); $activeNumbers[] = $waNumber; } diff --git a/setup/seed_palmas.php b/setup/seed_palmas.php index 2ae9729..bc7f1d7 100644 --- a/setup/seed_palmas.php +++ b/setup/seed_palmas.php @@ -11,8 +11,9 @@ $db = db(); // despliegue. Es idempotente: si la columna existe, no hace nada. foreach ([ - 'tercero_id' => 'INT NULL', - 'modulos_json' => 'TEXT NULL', + 'tercero_id' => 'INT NULL', + 'modulos_json' => 'TEXT NULL', + 'es_supervisor' => 'TINYINT(1) NOT NULL DEFAULT 0', ] as $columna => $tipo) { $existe = $db->query("SHOW COLUMNS FROM company_phones LIKE '{$columna}'")->fetch(); if (!$existe) {