Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
380 lines
20 KiB
PHP
380 lines
20 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(__DIR__) . '/config/env.php';
|
|
require_once dirname(__DIR__) . '/config/db.php';
|
|
|
|
$db = db();
|
|
|
|
// ─── CONFIG JSON completo (Company 2 y 3 comparten la misma base) ────────────
|
|
|
|
$configJson = [
|
|
'greeting' => '¡Bienvenido a Palmas360! Escribe *menu* para ver las opciones disponibles.',
|
|
'fallback' => 'No entendí. Escribe *menu* para ver las opciones disponibles.',
|
|
'commands' => [
|
|
'menu' => 'show_main_menu',
|
|
'informes' => 'descargar_informes',
|
|
'info' => 'enviar_informacion',
|
|
'inicio' => 'show_main_menu',
|
|
'volver' => 'show_main_menu',
|
|
],
|
|
// ── Menus globales ──────────────────────────────────────────────────────
|
|
'menus' => [
|
|
|
|
// Menú modo trabajo (para per_type=3 tras seleccionar finca)
|
|
'menu_modo_trabajo' => [
|
|
'type' => 'list',
|
|
'header' => '🌴 Palmas360',
|
|
'body' => '¿Qué deseas hacer?',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver opciones',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Opciones',
|
|
'rows' => [
|
|
['id' => 'descargar_informes', 'title' => '📥 Descargar informes', 'description' => 'Ciclos, Producción, Ausentismos'],
|
|
['id' => 'enviar_informacion', 'title' => '📤 Enviar información', 'description' => 'Subir ciclos y datos'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
|
|
// Menú cat2 (descargar informes) — accesible globalmente
|
|
'show_menu_cat2' => [
|
|
'type' => 'list',
|
|
'header' => '📋 Informes',
|
|
'body' => '¿Qué informe deseas?',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver opciones',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Informes',
|
|
'rows' => [
|
|
['id' => 'submenu_produccion', 'title' => '🌿 Producción', 'description' => 'Informes de producción'],
|
|
['id' => 'submenu_ciclos', 'title' => '🔄 Ciclos', 'description' => 'Ciclos de campo'],
|
|
['id' => 'submenu_ausentismos', 'title' => '👥 Ausentismos', 'description' => 'Faltas y novedades'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
|
|
// Submenú producción
|
|
'submenu_produccion' => [
|
|
'type' => 'list',
|
|
'header' => '🌿 Producción',
|
|
'body' => 'Selecciona el informe:',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver opciones',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Producción',
|
|
'rows' => [
|
|
['id' => 'produccion_total', 'title' => '📊 Total del mes', 'description' => 'Resumen y PDF del mes'],
|
|
['id' => 'produccion_lotes', 'title' => '📋 Por lotes (Excel)', 'description' => 'Detalle por lote'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
|
|
// Submenú ciclos
|
|
'submenu_ciclos' => [
|
|
'type' => 'list',
|
|
'header' => '🔄 Ciclos',
|
|
'body' => 'Selecciona el ciclo:',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver ciclos',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Ciclos',
|
|
'rows' => [
|
|
['id' => 'ciclo_cosecha', 'title' => '🌾 Cosecha', 'description' => 'Ciclos de cosecha'],
|
|
['id' => 'ciclo_polinizacion', 'title' => '🌸 Polinización', 'description' => 'Ciclos de polinización'],
|
|
['id' => 'ciclo_censo', 'title' => '📝 Censo', 'description' => 'Ciclos de censo'],
|
|
['id' => 'ciclo_plagas', 'title' => '🐛 Plagas', 'description' => 'Ciclos de plagas'],
|
|
],
|
|
],
|
|
[
|
|
'title' => 'Más ciclos',
|
|
'rows' => [
|
|
['id' => 'ciclo_palm', 'title' => '🌴 Palm', 'description' => 'Ciclos palm'],
|
|
['id' => 'ciclo_tratamiento', 'title' => '💊 Tratamiento', 'description' => 'Ciclos de tratamiento'],
|
|
['id' => 'ciclo_mantenimiento', 'title' => '🔧 Mantenimiento', 'description' => 'Ciclos de mantenimiento'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
|
|
// Submenú ausentismos
|
|
'submenu_ausentismos' => [
|
|
'type' => 'list',
|
|
'header' => '👥 Ausentismos',
|
|
'body' => 'Selecciona el período:',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver opciones',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Período',
|
|
'rows' => [
|
|
['id' => 'ausentismos_hoy', 'title' => '📅 Hoy', 'description' => '¿Quién faltó hoy?'],
|
|
['id' => 'ausentismos_semana', 'title' => '📆 Esta semana', 'description' => '¿Quién faltó esta semana?'],
|
|
['id' => 'ausentismos_mes', 'title' => '📋 Mes (PDF)', 'description' => 'Reporte mensual PDF'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
|
|
// Submenú subir ciclos (para per_type=3)
|
|
'submenu_subir_ciclos' => [
|
|
'type' => 'list',
|
|
'header' => '📤 Enviar información',
|
|
'body' => 'Selecciona qué deseas enviar:',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver opciones',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Opciones',
|
|
'rows' => [
|
|
['id' => 'subir_ciclo_cosecha', 'title' => '🌾 Ciclo cosecha', 'description' => 'Subir ciclo de cosecha'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
|
|
// ── Flows globales ──────────────────────────────────────────────────────
|
|
'flows' => [
|
|
// Producción
|
|
'produccion_total' => ['type' => 'function', 'function' => 'produccion_total_bot'],
|
|
'produccion_lotes' => ['type' => 'function', 'function' => 'api_report', 'params' => [
|
|
'endpoint_key' => 'produccion_lotes_bot_dn',
|
|
'filename' => 'produccion_lotes.xlsx',
|
|
'caption' => 'Producción por lotes del año',
|
|
]],
|
|
|
|
// Ausentismos
|
|
'ausentismos_hoy' => ['type' => 'function', 'function' => 'ausentismos_bot', 'params' => ['periodo' => 'hoy']],
|
|
'ausentismos_semana' => ['type' => 'function', 'function' => 'ausentismos_bot', 'params' => ['periodo' => 'semana']],
|
|
'ausentismos_mes' => ['type' => 'function', 'function' => 'api_report', 'params' => [
|
|
'endpoint_key' => 'ausentismos_bot_pdf_dn',
|
|
'filename' => 'ausentismos_mes.pdf',
|
|
'caption' => 'Ausentismos del mes',
|
|
]],
|
|
|
|
// Ciclos (menus)
|
|
'submenu_produccion' => ['type' => 'menu', 'menu' => 'submenu_produccion'],
|
|
'submenu_ciclos' => ['type' => 'menu', 'menu' => 'submenu_ciclos'],
|
|
'submenu_ausentismos' => ['type' => 'menu', 'menu' => 'submenu_ausentismos'],
|
|
|
|
// Ciclos individuales
|
|
'ciclo_cosecha' => ['type' => 'function', 'function' => 'ciclo_bot', 'params' => ['ciclo' => 'cosecha']],
|
|
'ciclo_polinizacion' => ['type' => 'function', 'function' => 'ciclo_bot', 'params' => ['ciclo' => 'polinizacion']],
|
|
'ciclo_censo' => ['type' => 'function', 'function' => 'ciclo_bot', 'params' => ['ciclo' => 'censo']],
|
|
'ciclo_plagas' => ['type' => 'function', 'function' => 'ciclo_bot', 'params' => ['ciclo' => 'plagas']],
|
|
'ciclo_palm' => ['type' => 'function', 'function' => 'ciclo_bot', 'params' => ['ciclo' => 'palm']],
|
|
'ciclo_tratamiento' => ['type' => 'function', 'function' => 'ciclo_bot', 'params' => ['ciclo' => 'tratamiento']],
|
|
'ciclo_mantenimiento' => ['type' => 'function', 'function' => 'ciclo_bot', 'params' => ['ciclo' => 'mantenimiento']],
|
|
|
|
// Upload
|
|
'enviar_informacion' => ['type' => 'menu', 'menu' => 'submenu_subir_ciclos'],
|
|
'subir_ciclo_cosecha' => ['type' => 'function', 'function' => 'upload_ciclo', 'params' => ['ciclo' => 'cosecha']],
|
|
|
|
// Placeholders globales
|
|
'show_main_menu' => ['type' => 'menu', 'menu' => 'show_menu_cat2'],
|
|
'descargar_informes' => ['type' => 'menu', 'menu' => 'show_menu_cat2'],
|
|
],
|
|
|
|
// ── Per-type config ─────────────────────────────────────────────────────
|
|
'per_type' => [
|
|
|
|
// Cat 1: solo reporta (envía info, no descarga)
|
|
'1' => [
|
|
'greeting_flow' => 'show_menu_cat1',
|
|
'fallback_flow' => 'show_menu_cat1',
|
|
'greeting' => null,
|
|
'fallback' => null,
|
|
'commands' => ['menu' => 'show_menu_cat1'],
|
|
'menus' => [
|
|
'show_menu_cat1' => [
|
|
'type' => 'list',
|
|
'header' => '🌴 Palmas360',
|
|
'body' => '¿Qué deseas hacer?',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver opciones',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Opciones',
|
|
'rows' => [
|
|
['id' => 'subir_ciclo_cosecha', 'title' => '🌾 Subir cosecha', 'description' => 'Enviar ciclo de cosecha'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
'flows' => [
|
|
'show_menu_cat1' => ['type' => 'menu', 'menu' => 'show_menu_cat1'],
|
|
'show_main_menu' => ['type' => 'menu', 'menu' => 'show_menu_cat1'],
|
|
'subir_ciclo_cosecha' => ['type' => 'function', 'function' => 'upload_ciclo', 'params' => ['ciclo' => 'cosecha']],
|
|
],
|
|
],
|
|
|
|
// Cat 2: solo descarga
|
|
'2' => [
|
|
'greeting_flow' => 'ask_finca',
|
|
'fallback_flow' => 'show_menu_cat2',
|
|
'greeting' => null,
|
|
'fallback' => null,
|
|
'commands' => ['menu' => 'show_menu_cat2'],
|
|
'menus' => [
|
|
'show_menu_cat2' => [
|
|
'type' => 'list',
|
|
'header' => '📋 Informes',
|
|
'body' => '¿Qué informe deseas?',
|
|
'footer' => 'Palmas360',
|
|
'button' => 'Ver opciones',
|
|
'sections' => [
|
|
[
|
|
'title' => 'Informes',
|
|
'rows' => [
|
|
['id' => 'submenu_produccion', 'title' => '🌿 Producción', 'description' => 'Informes de producción'],
|
|
['id' => 'submenu_ciclos', 'title' => '🔄 Ciclos', 'description' => 'Ciclos de campo'],
|
|
['id' => 'submenu_ausentismos', 'title' => '👥 Ausentismos', 'description' => 'Faltas y novedades'],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
],
|
|
'flows' => [
|
|
'show_menu_cat2' => ['type' => 'menu', 'menu' => 'show_menu_cat2'],
|
|
'show_main_menu' => ['type' => 'menu', 'menu' => 'show_menu_cat2'],
|
|
'descargar_informes' => ['type' => 'menu', 'menu' => 'show_menu_cat2'],
|
|
],
|
|
],
|
|
|
|
// Cat 3: descarga + envío
|
|
'3' => [
|
|
'greeting_flow' => 'ask_finca',
|
|
'fallback_flow' => 'show_main_menu',
|
|
'greeting' => null,
|
|
'fallback' => null,
|
|
'commands' => ['menu' => 'show_main_menu'],
|
|
'menus' => [],
|
|
'flows' => [
|
|
'show_main_menu' => ['type' => 'menu', 'menu' => 'menu_modo_trabajo'],
|
|
'descargar_informes' => ['type' => 'menu', 'menu' => 'show_menu_cat2'],
|
|
'enviar_informacion' => ['type' => 'menu', 'menu' => 'submenu_subir_ciclos'],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
|
|
// ─── Endpoints ─────────────────────────────────────────────────────────────────
|
|
|
|
$BASE = 'https://app.palmas360.com/rosablanca/php/controller/controller_api_externa.php';
|
|
|
|
$endpoints = [
|
|
['key' => 'produccion_total_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=produccion_total_bot&fecha_desde={fecha_desde}&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'produccion_total_pdf_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=produccion_total_pdf_bot&fecha_desde={fecha_desde}&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'produccion_lotes_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=produccion_lotes_bot&finca_id={finca_id}'],
|
|
['key' => 'ausentismos_bot_texto_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=ausentismos_bot_texto&fecha_desde={fecha_desde}&fecha_hasta={fecha_hasta}'],
|
|
['key' => 'ausentismos_bot_pdf_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=ausentismos_bot_pdf&fecha_desde={fecha_desde}&fecha_hasta={fecha_hasta}'],
|
|
['key' => 'cosecha_top_texto_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=cosecha_top_texto_bot&finca_id={finca_id}'],
|
|
['key' => 'polinizacion_top_texto_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=polinizacion_top_texto_bot&finca_id={finca_id}'],
|
|
['key' => 'censo_top_texto_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=censo_top_texto_bot&finca_id={finca_id}'],
|
|
['key' => 'plagas_top_texto_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=plagas_top_texto_bot&finca_id={finca_id}'],
|
|
['key' => 'palm_top_texto_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=palm_top_texto_bot&finca_id={finca_id}'],
|
|
['key' => 'tratamiento_top_texto_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=tratamiento_top_texto_bot&finca_id={finca_id}'],
|
|
['key' => 'mantenimiento_top_texto_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=mantenimiento_top_texto_bot&grupo=0&finca_id={finca_id}'],
|
|
['key' => 'cosecha_historico_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=cosecha_historico_bot&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'polinizacion_historico_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=polinizacion_historico_bot&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'censo_historico_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=censo_historico_bot&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'plagas_historico_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=plagas_historico_bot&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'palm_historico_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=palm_historico_bot&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'tratamiento_historico_bot_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=tratamiento_historico_bot&fecha_hasta={fecha_hasta}&finca_id={finca_id}'],
|
|
['key' => 'fincas_dn', 'dir' => 'download', 'url' => $BASE . '?peticion=fincas'],
|
|
];
|
|
|
|
// ─── Companies ─────────────────────────────────────────────────────────────────
|
|
|
|
$companies = [
|
|
[
|
|
'id' => 2,
|
|
'name' => 'rosablanca',
|
|
'display_name' => 'Rosa Blanca',
|
|
'phone_number_id' => '1168511569675820',
|
|
'display_phone' => '',
|
|
'api_base_url' => 'https://graph.facebook.com/v22.0',
|
|
'api_key' => 'EAAqbNQQekwUBRm8ZAAfCvXZAsxcM1BTkiHjVJYeOfXYCg5t4qYtBL7ap2IdHGa1iXclZBO47gZBshYZBAlQRuUvgSib2Owh7gPnAWfNlHXZAT61ccElXoihuXuEuy0qLiB95tDhLMWPqoKYuDwAHCU78RQxoi1nzz1OsOY40EK7U2tKZBVuZABQZCJctZBrLK7rRiC7AZDZD',
|
|
'bot_type' => 'normal',
|
|
'requires_approval' => 0,
|
|
'is_active' => 1,
|
|
],
|
|
[
|
|
'id' => 3,
|
|
'name' => 'palmas360_test',
|
|
'display_name' => 'Palmas360 Test',
|
|
'phone_number_id' => '1168511569675820',
|
|
'display_phone' => '',
|
|
'api_base_url' => 'https://graph.facebook.com/v22.0',
|
|
'api_key' => 'EAAqbNQQekwUBRm8ZAAfCvXZAsxcM1BTkiHjVJYeOfXYCg5t4qYtBL7ap2IdHGa1iXclZBO47gZBshYZBAlQRuUvgSib2Owh7gPnAWfNlHXZAT61ccElXoihuXuEuy0qLiB95tDhLMWPqoKYuDwAHCU78RQxoi1nzz1OsOY40EK7U2tKZBVuZABQZCJctZBrLK7rRiC7AZDZD',
|
|
'bot_type' => 'normal',
|
|
'requires_approval' => 0,
|
|
'is_active' => 1,
|
|
],
|
|
];
|
|
|
|
$configJsonStr = json_encode($configJson, JSON_UNESCAPED_UNICODE);
|
|
|
|
// ─── Insert / update companies ────────────────────────────────────────────────
|
|
|
|
$stmtCo = $db->prepare("
|
|
INSERT INTO companies (id, name, display_name, phone_number_id, display_phone, api_base_url, api_key, bot_type, requires_approval, is_active, config_json)
|
|
VALUES (:id, :name, :display_name, :phone_number_id, :display_phone, :api_base_url, :api_key, :bot_type, :requires_approval, :is_active, :config_json)
|
|
ON DUPLICATE KEY UPDATE
|
|
display_name = VALUES(display_name),
|
|
phone_number_id = VALUES(phone_number_id),
|
|
api_base_url = VALUES(api_base_url),
|
|
api_key = VALUES(api_key),
|
|
bot_type = VALUES(bot_type),
|
|
is_active = VALUES(is_active),
|
|
config_json = VALUES(config_json)
|
|
");
|
|
|
|
foreach ($companies as $co) {
|
|
$stmtCo->execute([
|
|
':id' => $co['id'],
|
|
':name' => $co['name'],
|
|
':display_name' => $co['display_name'],
|
|
':phone_number_id' => $co['phone_number_id'],
|
|
':display_phone' => $co['display_phone'],
|
|
':api_base_url' => $co['api_base_url'],
|
|
':api_key' => $co['api_key'],
|
|
':bot_type' => $co['bot_type'],
|
|
':requires_approval'=> $co['requires_approval'],
|
|
':is_active' => $co['is_active'],
|
|
':config_json' => $configJsonStr,
|
|
]);
|
|
echo "✔ Company {$co['id']} ({$co['display_name']}) OK\n";
|
|
}
|
|
|
|
// ─── Insert / update endpoints ────────────────────────────────────────────────
|
|
|
|
$stmtEp = $db->prepare("
|
|
INSERT INTO company_endpoints (company_id, endpoint_key, direction, url)
|
|
VALUES (:company_id, :endpoint_key, :direction, :url)
|
|
ON DUPLICATE KEY UPDATE url = VALUES(url), direction = VALUES(direction)
|
|
");
|
|
|
|
foreach ($companies as $co) {
|
|
foreach ($endpoints as $ep) {
|
|
$stmtEp->execute([
|
|
':company_id' => $co['id'],
|
|
':endpoint_key' => $ep['key'],
|
|
':direction' => $ep['dir'],
|
|
':url' => $ep['url'],
|
|
]);
|
|
}
|
|
echo "✔ " . count($endpoints) . " endpoints OK para company {$co['id']}\n";
|
|
}
|
|
|
|
echo "\n✅ Seed completo.\n";
|