// ────────────────────────────────────────────────────────────────── // CRUD — Clientes (Administración Comercial → Clientes y Crédito) // // Master: clientes // nombre_cliente, RUT, es_agencia (bool), cliente_padre (FK self) // // Detail: clientes_autoriza_contratos // id_empresa_autoriza, id_empresa_autorizada, activo // // Reglas: // • Todo cliente autoriza implícitamente a sí mismo (fila inmutable). // • Un cliente puede autorizar a otros a usar SU contrato — típicamente // una matriz autoriza a sus filiales (Cencosud → Almacenes París, …) // • La data inicial refleja la estructura conglomeral chilena. // ────────────────────────────────────────────────────────────────── const INITIAL_CLIENTES = [ // ── 01 · CENCOSUD ──────────────────────────────────────────────── { id: "CL1", nombre_cliente: "Cencosud S.A.", rut: "93.834.000-5", es_agencia: false, cliente_padre: null }, { id: "CL2", nombre_cliente: "Almacenes París", rut: "76.575.470-2", es_agencia: false, cliente_padre: "CL1" }, { id: "CL3", nombre_cliente: "Jumbo Supermercados", rut: "81.201.000-K", es_agencia: false, cliente_padre: "CL1" }, { id: "CL4", nombre_cliente: "Santa Isabel", rut: "81.679.000-K", es_agencia: false, cliente_padre: "CL1" }, { id: "CL5", nombre_cliente: "Easy Chile", rut: "96.910.180-3", es_agencia: false, cliente_padre: "CL1" }, { id: "CL6", nombre_cliente: "Cencosud Shopping Centers", rut: "76.433.310-1", es_agencia: false, cliente_padre: "CL1" }, { id: "CL7", nombre_cliente: "Banco Cencosud Scotiabank", rut: "97.023.000-9", es_agencia: false, cliente_padre: "CL1" }, // ── 02 · FALABELLA ──────────────────────────────────────────────── { id: "CL8", nombre_cliente: "S.A.C.I. Falabella", rut: "90.749.000-9", es_agencia: false, cliente_padre: null }, { id: "CL9", nombre_cliente: "Falabella Retail S.A.", rut: "77.261.280-K", es_agencia: false, cliente_padre: "CL8" }, { id: "CL10", nombre_cliente: "Tottus S.A.", rut: "76.134.065-9", es_agencia: false, cliente_padre: "CL8" }, { id: "CL11", nombre_cliente: "Sodimac S.A.", rut: "96.792.430-K", es_agencia: false, cliente_padre: "CL8" }, { id: "CL12", nombre_cliente: "Banco Falabella", rut: "96.509.660-4", es_agencia: false, cliente_padre: "CL8" }, { id: "CL13", nombre_cliente: "Mall Plaza", rut: "76.017.019-4", es_agencia: false, cliente_padre: "CL8" }, // ── 03 · WALMART CHILE ─────────────────────────────────────────── { id: "CL14", nombre_cliente: "Walmart Chile S.A.", rut: "76.042.014-K", es_agencia: false, cliente_padre: null }, { id: "CL15", nombre_cliente: "Líder", rut: "76.124.555-9", es_agencia: false, cliente_padre: "CL14"}, { id: "CL16", nombre_cliente: "Express de Líder", rut: "76.412.860-K", es_agencia: false, cliente_padre: "CL14"}, { id: "CL17", nombre_cliente: "Central Mayorista", rut: "76.882.110-5", es_agencia: false, cliente_padre: "CL14"}, { id: "CL18", nombre_cliente: "aCuenta", rut: "76.998.452-7", es_agencia: false, cliente_padre: "CL14"}, // ── 04 · SMU ────────────────────────────────────────────────────── { id: "CL19", nombre_cliente: "SMU S.A.", rut: "76.012.676-4", es_agencia: false, cliente_padre: null }, { id: "CL20", nombre_cliente: "Unimarc", rut: "78.062.300-9", es_agencia: false, cliente_padre: "CL19"}, { id: "CL21", nombre_cliente: "Alvi", rut: "76.134.055-1", es_agencia: false, cliente_padre: "CL19"}, { id: "CL22", nombre_cliente: "Mayorista 10", rut: "96.665.330-6", es_agencia: false, cliente_padre: "CL19"}, { id: "CL23", nombre_cliente: "OK Market", rut: "76.241.890-3", es_agencia: false, cliente_padre: "CL19"}, // ── 05 · CCU (Quiñenco / Heineken) ─────────────────────────────── { id: "CL24", nombre_cliente: "Compañía Cervecerías Unidas (CCU)", rut: "90.413.000-1", es_agencia: false, cliente_padre: null }, { id: "CL25", nombre_cliente: "Cervecera CCU Chile", rut: "96.989.120-4", es_agencia: false, cliente_padre: "CL24"}, { id: "CL26", nombre_cliente: "Compañía Pisquera de Chile", rut: "96.701.120-0", es_agencia: false, cliente_padre: "CL24"}, { id: "CL27", nombre_cliente: "Viña San Pedro Tarapacá (VSPT)", rut: "91.041.000-8", es_agencia: false, cliente_padre: "CL24"}, { id: "CL28", nombre_cliente: "Aguas CCU-Nestlé", rut: "96.870.090-1", es_agencia: false, cliente_padre: "CL24"}, // ── 06 · CAROZZI ────────────────────────────────────────────────── { id: "CL29", nombre_cliente: "Empresas Carozzi S.A.", rut: "96.591.040-9", es_agencia: false, cliente_padre: null }, { id: "CL30", nombre_cliente: "Costa Chocolates", rut: "76.137.880-K", es_agencia: false, cliente_padre: "CL29"}, { id: "CL31", nombre_cliente: "Tres Montes Lucchetti", rut: "96.886.870-5", es_agencia: false, cliente_padre: "CL29"}, { id: "CL32", nombre_cliente: "McKay", rut: "76.453.998-1", es_agencia: false, cliente_padre: "CL29"}, // ── 07 · QUIÑENCO (Luksic) ─────────────────────────────────────── { id: "CL33", nombre_cliente: "Quiñenco S.A.", rut: "91.705.000-7", es_agencia: false, cliente_padre: null }, { id: "CL34", nombre_cliente: "Banco de Chile", rut: "97.004.000-5", es_agencia: false, cliente_padre: "CL33"}, { id: "CL35", nombre_cliente: "CSAV (Sudamericana de Vapores)", rut: "90.160.000-7", es_agencia: false, cliente_padre: "CL33"}, // ── 08 · EMPRESAS COPEC / ANTARCHILE ───────────────────────────── { id: "CL36", nombre_cliente: "Empresas Copec S.A.", rut: "90.690.000-9", es_agencia: false, cliente_padre: null }, { id: "CL37", nombre_cliente: "Copec Combustibles", rut: "99.520.000-7", es_agencia: false, cliente_padre: "CL36"}, { id: "CL38", nombre_cliente: "Pronto Copec", rut: "76.057.030-3", es_agencia: false, cliente_padre: "CL36"}, { id: "CL39", nombre_cliente: "Arauco", rut: "93.458.000-1", es_agencia: false, cliente_padre: "CL36"}, // ── 09 · COCA-COLA EMBOTELLADORAS ─────────────────────────────── { id: "CL40", nombre_cliente: "Embotelladora Andina", rut: "91.144.000-8", es_agencia: false, cliente_padre: null }, { id: "CL41", nombre_cliente: "Embotelladora Embonor", rut: "93.281.000-K", es_agencia: false, cliente_padre: null }, { id: "CL42", nombre_cliente: "Coca-Cola de Chile S.A.", rut: "82.230.000-2", es_agencia: false, cliente_padre: null }, // ── 10 · OTROS RETAIL & CONSUMO ───────────────────────────────── { id: "CL43", nombre_cliente: "Ripley Corp", rut: "76.018.890-9", es_agencia: false, cliente_padre: null }, { id: "CL44", nombre_cliente: "Hites", rut: "96.836.180-0", es_agencia: false, cliente_padre: null }, { id: "CL45", nombre_cliente: "La Polar", rut: "96.874.030-K", es_agencia: false, cliente_padre: null }, { id: "CL46", nombre_cliente: "ABC Din", rut: "76.108.821-1", es_agencia: false, cliente_padre: null }, // ── 11 · ALIMENTOS / BEBIDAS ──────────────────────────────────── { id: "CL47", nombre_cliente: "Watt's S.A.", rut: "92.535.000-6", es_agencia: false, cliente_padre: null }, { id: "CL48", nombre_cliente: "Soprole", rut: "76.102.405-1", es_agencia: false, cliente_padre: null }, { id: "CL49", nombre_cliente: "Colun", rut: "82.471.500-9", es_agencia: false, cliente_padre: null }, { id: "CL50", nombre_cliente: "Iansa", rut: "92.011.000-2", es_agencia: false, cliente_padre: null }, { id: "CL51", nombre_cliente: "Nestlé Chile", rut: "91.000.000-6", es_agencia: false, cliente_padre: null }, { id: "CL52", nombre_cliente: "Unilever Chile", rut: "96.575.810-0", es_agencia: false, cliente_padre: null }, { id: "CL53", nombre_cliente: "Procter & Gamble Chile", rut: "96.951.230-1", es_agencia: false, cliente_padre: null }, { id: "CL54", nombre_cliente: "Colgate-Palmolive Chile", rut: "92.572.000-8", es_agencia: false, cliente_padre: null }, { id: "CL55", nombre_cliente: "Kimberly-Clark Chile", rut: "78.041.400-0", es_agencia: false, cliente_padre: null }, { id: "CL56", nombre_cliente: "Mondelez Chile", rut: "78.834.310-7", es_agencia: false, cliente_padre: null }, { id: "CL57", nombre_cliente: "L'Oréal Chile", rut: "96.524.090-K", es_agencia: false, cliente_padre: null }, { id: "CL58", nombre_cliente: "Arcos Dorados (McDonald's Chile)", rut: "76.044.183-K", es_agencia: false, cliente_padre: null }, // ── 12 · BANCA ────────────────────────────────────────────────── { id: "CL59", nombre_cliente: "Banco Santander Chile", rut: "97.036.000-K", es_agencia: false, cliente_padre: null }, { id: "CL60", nombre_cliente: "Banco BCI", rut: "97.006.000-6", es_agencia: false, cliente_padre: null }, { id: "CL61", nombre_cliente: "BancoEstado", rut: "97.030.000-7", es_agencia: false, cliente_padre: null }, { id: "CL62", nombre_cliente: "Banco Itaú Chile", rut: "76.645.030-K", es_agencia: false, cliente_padre: null }, { id: "CL63", nombre_cliente: "Scotiabank Chile", rut: "97.018.000-1", es_agencia: false, cliente_padre: null }, { id: "CL64", nombre_cliente: "Banco Security", rut: "97.053.000-2", es_agencia: false, cliente_padre: null }, // ── 13 · TELCOS ───────────────────────────────────────────────── { id: "CL65", nombre_cliente: "Movistar (Telefónica Chile)", rut: "90.635.000-9", es_agencia: false, cliente_padre: null }, { id: "CL66", nombre_cliente: "Entel", rut: "92.580.000-7", es_agencia: false, cliente_padre: null }, { id: "CL67", nombre_cliente: "WOM", rut: "78.921.690-8", es_agencia: false, cliente_padre: null }, { id: "CL68", nombre_cliente: "Claro Chile", rut: "96.799.250-K", es_agencia: false, cliente_padre: null }, { id: "CL69", nombre_cliente: "VTR", rut: "78.703.410-1", es_agencia: false, cliente_padre: null }, // ── 14 · AFP / SALUD / SEGUROS ────────────────────────────────── { id: "CL70", nombre_cliente: "AFP Habitat", rut: "98.000.100-8", es_agencia: false, cliente_padre: null }, { id: "CL71", nombre_cliente: "AFP Provida", rut: "98.000.400-7", es_agencia: false, cliente_padre: null }, { id: "CL72", nombre_cliente: "AFP Capital", rut: "98.000.000-1", es_agencia: false, cliente_padre: null }, { id: "CL73", nombre_cliente: "AFP Cuprum", rut: "98.001.000-7", es_agencia: false, cliente_padre: null }, { id: "CL74", nombre_cliente: "AFP Modelo", rut: "76.241.117-8", es_agencia: false, cliente_padre: null }, { id: "CL75", nombre_cliente: "Banmédica", rut: "96.572.800-7", es_agencia: false, cliente_padre: null }, { id: "CL76", nombre_cliente: "Colmena Golden Cross", rut: "96.555.500-K", es_agencia: false, cliente_padre: null }, { id: "CL77", nombre_cliente: "Cruz Blanca Salud", rut: "96.501.450-0", es_agencia: false, cliente_padre: null }, // ── 15 · AUTOMOTRICES / COMBUSTIBLES ──────────────────────────── { id: "CL78", nombre_cliente: "Derco", rut: "94.141.000-7", es_agencia: false, cliente_padre: null }, { id: "CL79", nombre_cliente: "Indumotora", rut: "92.044.000-5", es_agencia: false, cliente_padre: null }, { id: "CL80", nombre_cliente: "SK Bergé", rut: "76.061.500-1", es_agencia: false, cliente_padre: null }, { id: "CL81", nombre_cliente: "ENEX (Shell Chile)", rut: "76.418.940-4", es_agencia: false, cliente_padre: null }, { id: "CL82", nombre_cliente: "Esmax Distribución", rut: "96.661.690-7", es_agencia: false, cliente_padre: null }, // ── 16 · AEROLÍNEAS / TURISMO ─────────────────────────────────── { id: "CL83", nombre_cliente: "LATAM Airlines", rut: "89.862.200-2", es_agencia: false, cliente_padre: null }, { id: "CL84", nombre_cliente: "Sky Airline", rut: "76.062.262-8", es_agencia: false, cliente_padre: null }, { id: "CL85", nombre_cliente: "JetSmart Airlines", rut: "76.530.890-2", es_agencia: false, cliente_padre: null }, // ── 17 · WPP / GROUPM (Agencias) ─────────────────────────────── { id: "CL86", nombre_cliente: "WPP Chile", rut: "76.005.230-K", es_agencia: true, cliente_padre: null }, { id: "CL87", nombre_cliente: "Mindshare", rut: "76.012.870-8", es_agencia: true, cliente_padre: "CL86"}, { id: "CL88", nombre_cliente: "Wavemaker", rut: "76.892.430-1", es_agencia: true, cliente_padre: "CL86"}, { id: "CL89", nombre_cliente: "EssenceMediacom", rut: "76.943.220-5", es_agencia: true, cliente_padre: "CL86"}, { id: "CL90", nombre_cliente: "Ogilvy Chile", rut: "78.066.510-0", es_agencia: true, cliente_padre: "CL86"}, { id: "CL91", nombre_cliente: "VML Chile", rut: "76.741.080-9", es_agencia: true, cliente_padre: "CL86"}, // ── 18 · PUBLICIS GROUPE (Agencias) ──────────────────────────── { id: "CL92", nombre_cliente: "Publicis Groupe Chile", rut: "76.110.870-K", es_agencia: true, cliente_padre: null }, { id: "CL93", nombre_cliente: "Starcom", rut: "76.225.640-3", es_agencia: true, cliente_padre: "CL92"}, { id: "CL94", nombre_cliente: "Zenith", rut: "76.330.420-7", es_agencia: true, cliente_padre: "CL92"}, { id: "CL95", nombre_cliente: "Spark Foundry", rut: "76.844.760-5", es_agencia: true, cliente_padre: "CL92"}, { id: "CL96", nombre_cliente: "Leo Burnett Chile", rut: "85.213.300-K", es_agencia: true, cliente_padre: "CL92"}, // ── 19 · OMNICOM (Agencias) ───────────────────────────────────── { id: "CL97", nombre_cliente: "Omnicom Media Group Chile", rut: "76.220.910-3", es_agencia: true, cliente_padre: null }, { id: "CL98", nombre_cliente: "OMD", rut: "78.910.480-8", es_agencia: true, cliente_padre: "CL97"}, { id: "CL99", nombre_cliente: "PHD Chile", rut: "76.331.560-8", es_agencia: true, cliente_padre: "CL97"}, { id: "CL100",nombre_cliente: "DDB Chile", rut: "96.811.770-2", es_agencia: true, cliente_padre: "CL97"}, { id: "CL101",nombre_cliente: "BBDO Chile", rut: "96.665.470-1", es_agencia: true, cliente_padre: "CL97"}, { id: "CL102",nombre_cliente: "TBWA\\Chile", rut: "76.488.610-6", es_agencia: true, cliente_padre: "CL97"}, // ── 20 · IPG MEDIABRANDS (Agencias) ──────────────────────────── { id: "CL103",nombre_cliente: "IPG Mediabrands Chile", rut: "76.018.580-2", es_agencia: true, cliente_padre: null }, { id: "CL104",nombre_cliente: "Initiative", rut: "78.890.520-5", es_agencia: true, cliente_padre: "CL103"}, { id: "CL105",nombre_cliente: "UM", rut: "78.890.610-4", es_agencia: true, cliente_padre: "CL103"}, { id: "CL106",nombre_cliente: "McCann Chile", rut: "85.620.700-7", es_agencia: true, cliente_padre: "CL103"}, { id: "CL107",nombre_cliente: "Lowe Porta", rut: "78.422.090-9", es_agencia: true, cliente_padre: "CL103"}, // ── 21 · DENTSU (Agencias) ────────────────────────────────────── { id: "CL108",nombre_cliente: "dentsu Chile", rut: "76.481.040-1", es_agencia: true, cliente_padre: null }, { id: "CL109",nombre_cliente: "Carat", rut: "78.066.730-8", es_agencia: true, cliente_padre: "CL108"}, { id: "CL110",nombre_cliente: "iProspect", rut: "76.560.310-K", es_agencia: true, cliente_padre: "CL108"}, { id: "CL111",nombre_cliente: "dentsu X", rut: "76.910.660-7", es_agencia: true, cliente_padre: "CL108"}, // ── 22 · HAVAS (Agencias) ─────────────────────────────────────── { id: "CL112",nombre_cliente: "Havas Media Chile", rut: "76.330.890-3", es_agencia: true, cliente_padre: null }, { id: "CL113",nombre_cliente: "Havas Creative Chile", rut: "76.330.895-4", es_agencia: true, cliente_padre: "CL112"}, // ── 23 · AGENCIAS INDEPENDIENTES ──────────────────────────────── { id: "CL114",nombre_cliente: "BBK Comunicaciones", rut: "76.211.580-2", es_agencia: true, cliente_padre: null }, { id: "CL115",nombre_cliente: "Inbrax", rut: "76.580.420-7", es_agencia: true, cliente_padre: null }, { id: "CL116",nombre_cliente: "Prolam", rut: "85.620.430-K", es_agencia: true, cliente_padre: null }, { id: "CL117",nombre_cliente: "Promoplan", rut: "78.452.300-2", es_agencia: true, cliente_padre: null }, { id: "CL118",nombre_cliente: "Wolf BCPP", rut: "76.230.100-K", es_agencia: true, cliente_padre: null }, ]; // Derive default autorizaciones: every matriz (padre=null) autoriza a cada hijo. // Self-authorizations are implicit (rendered as a locked row, not stored). const INITIAL_AUTORIZACIONES = (() => { const out = []; let n = 1; for (const c of INITIAL_CLIENTES) { if (c.cliente_padre) { out.push({ id: "AU" + n++, id_empresa_autoriza: c.cliente_padre, id_empresa_autorizada: c.id, activo: true, }); } } return out; })(); // Límites de crédito (CLP). Asignamos a los top clientes / matrices. // Las cifras son referenciales en millones de pesos chilenos. const INITIAL_CREDITOS = [ // Holdings / conglomerados grandes { id_empresa_cliente: "CL1", limite_credito: 1_800_000_000 }, // Cencosud { id_empresa_cliente: "CL8", limite_credito: 1_600_000_000 }, // Falabella { id_empresa_cliente: "CL14", limite_credito: 1_500_000_000 }, // Walmart { id_empresa_cliente: "CL19", limite_credito: 650_000_000 }, // SMU { id_empresa_cliente: "CL24", limite_credito: 1_200_000_000 }, // CCU { id_empresa_cliente: "CL29", limite_credito: 480_000_000 }, // Carozzi { id_empresa_cliente: "CL33", limite_credito: 520_000_000 }, // Quiñenco { id_empresa_cliente: "CL36", limite_credito: 900_000_000 }, // Empresas Copec // Embotelladoras { id_empresa_cliente: "CL40", limite_credito: 720_000_000 }, // Andina { id_empresa_cliente: "CL41", limite_credito: 380_000_000 }, // Embonor { id_empresa_cliente: "CL42", limite_credito: 850_000_000 }, // Coca-Cola Chile // Retail { id_empresa_cliente: "CL43", limite_credito: 320_000_000 }, // Ripley { id_empresa_cliente: "CL44", limite_credito: 140_000_000 }, // Hites { id_empresa_cliente: "CL45", limite_credito: 95_000_000 }, // La Polar { id_empresa_cliente: "CL46", limite_credito: 110_000_000 }, // ABC Din // Consumo { id_empresa_cliente: "CL47", limite_credito: 180_000_000 }, // Watt's { id_empresa_cliente: "CL48", limite_credito: 220_000_000 }, // Soprole { id_empresa_cliente: "CL49", limite_credito: 150_000_000 }, // Colun { id_empresa_cliente: "CL50", limite_credito: 85_000_000 }, // Iansa { id_empresa_cliente: "CL51", limite_credito: 780_000_000 }, // Nestlé { id_empresa_cliente: "CL52", limite_credito: 640_000_000 }, // Unilever { id_empresa_cliente: "CL53", limite_credito: 580_000_000 }, // P&G { id_empresa_cliente: "CL54", limite_credito: 320_000_000 }, // Colgate { id_empresa_cliente: "CL55", limite_credito: 280_000_000 }, // Kimberly { id_empresa_cliente: "CL56", limite_credito: 420_000_000 }, // Mondelez { id_empresa_cliente: "CL57", limite_credito: 240_000_000 }, // L'Oréal { id_empresa_cliente: "CL58", limite_credito: 560_000_000 }, // McDonald's // Banca { id_empresa_cliente: "CL59", limite_credito: 880_000_000 }, // Santander { id_empresa_cliente: "CL60", limite_credito: 620_000_000 }, // BCI { id_empresa_cliente: "CL61", limite_credito: 720_000_000 }, // BancoEstado { id_empresa_cliente: "CL62", limite_credito: 340_000_000 }, // Itaú { id_empresa_cliente: "CL63", limite_credito: 380_000_000 }, // Scotiabank { id_empresa_cliente: "CL64", limite_credito: 180_000_000 }, // Security // Telcos { id_empresa_cliente: "CL65", limite_credito: 920_000_000 }, // Movistar { id_empresa_cliente: "CL66", limite_credito: 880_000_000 }, // Entel { id_empresa_cliente: "CL67", limite_credito: 520_000_000 }, // WOM { id_empresa_cliente: "CL68", limite_credito: 460_000_000 }, // Claro { id_empresa_cliente: "CL69", limite_credito: 240_000_000 }, // VTR // AFP / salud { id_empresa_cliente: "CL70", limite_credito: 220_000_000 }, // Habitat { id_empresa_cliente: "CL71", limite_credito: 180_000_000 }, // Provida { id_empresa_cliente: "CL72", limite_credito: 160_000_000 }, // Capital { id_empresa_cliente: "CL73", limite_credito: 140_000_000 }, // Cuprum { id_empresa_cliente: "CL74", limite_credito: 120_000_000 }, // Modelo { id_empresa_cliente: "CL75", limite_credito: 180_000_000 }, // Banmédica { id_empresa_cliente: "CL76", limite_credito: 90_000_000 }, // Colmena { id_empresa_cliente: "CL77", limite_credito: 110_000_000 }, // Cruz Blanca // Auto / combustibles { id_empresa_cliente: "CL78", limite_credito: 240_000_000 }, // Derco { id_empresa_cliente: "CL79", limite_credito: 180_000_000 }, // Indumotora { id_empresa_cliente: "CL80", limite_credito: 210_000_000 }, // SK Bergé { id_empresa_cliente: "CL81", limite_credito: 320_000_000 }, // ENEX { id_empresa_cliente: "CL82", limite_credito: 180_000_000 }, // Esmax // Aerolíneas { id_empresa_cliente: "CL83", limite_credito: 480_000_000 }, // LATAM { id_empresa_cliente: "CL84", limite_credito: 140_000_000 }, // Sky { id_empresa_cliente: "CL85", limite_credito: 95_000_000 }, // JetSmart // Holdings de agencias (las agencias matrices manejan crédito) { id_empresa_cliente: "CL86", limite_credito: 2_400_000_000 }, // WPP { id_empresa_cliente: "CL92", limite_credito: 2_100_000_000 }, // Publicis { id_empresa_cliente: "CL97", limite_credito: 1_900_000_000 }, // Omnicom { id_empresa_cliente: "CL103", limite_credito: 1_700_000_000 }, // IPG { id_empresa_cliente: "CL108", limite_credito: 1_500_000_000 }, // dentsu { id_empresa_cliente: "CL112", limite_credito: 780_000_000 }, // Havas ]; const LS_CLIENTES = "sc.clientes"; const LS_AUTORIZACIONES = "sc.cliente_autoriza_contrato"; const LS_CREDITOS = "sc.cliente_credito"; // COMPAT: el modelo `cliente` fue reemplazado por `empresa` + `relacion_comercial` // (ver cruds-empresas.jsx). Los ids se preservan (empresa.id == cliente.id), así // que todos los `id_empresa_facturacion` / `id_empresa_cliente` del sistema siguen válidos. // Este hook deriva las filas en la MISMA forma que esperaban los consumidores // ({ id, nombre_cliente, nombre, rut, activo, es_agencia, cliente_padre, es_extranjero }) // leyendo desde el modelo empresa. Es de sólo lectura: las empresas se editan en su // propio mantenedor (Adm Empresas · Catálogos → Empresas). function _empresasComoClientes() { const read = (k) => { try { const r = localStorage.getItem(k); if (r) { const p = JSON.parse(r); if (Array.isArray(p)) return p; } } catch (e) {} return null; }; const emp = read("sc.empresa"); if (emp && emp.length) { const erc = read("sc.empresa_rol_asignado") || []; const rc = read("sc.empresa_rol") || []; const relAgencia = (rc.find(r => /agencia/i.test(r.nombre || "")) || {}).id; const agenciaSet = new Set(erc.filter(x => x.id_empresa_rol === relAgencia).map(x => x.id_empresa)); // El rol «es cliente» de sc.empresa_rol: sin él, toda empresa se ofrecía como // cliente (140), incluidos proveedores y agencias. const relCliente = (rc.find(r => /cliente/i.test(r.nombre || "")) || {}).id; const clienteSet = new Set(erc.filter(x => x.id_empresa_rol === relCliente).map(x => x.id_empresa)); return emp.map(e => ({ id: e.id, nombre_cliente: e.nombre, nombre: e.nombre, rut: e.rut, activo: e.activo !== false, es_agencia: agenciaSet.has(e.id), es_cliente: clienteSet.has(e.id), cliente_padre: e.id_empresa_padre || null, es_extranjero: !!(e.id_pais && e.id_pais !== "CL"), })); } const legacy = read("sc.clientes"); return (legacy && legacy.length) ? legacy : INITIAL_CLIENTES; } function useClientes() { const [rows] = React.useState(_empresasComoClientes); // sólo lectura desde acá; el motor de escritura vive en el mantenedor Empresas. return [rows, () => {}]; } function useAutorizaciones() { const [rows, setRows] = React.useState(() => { try { const raw = localStorage.getItem(LS_AUTORIZACIONES); if (raw) { const p = JSON.parse(raw); if (Array.isArray(p)) return p; } } catch (e) {} return INITIAL_AUTORIZACIONES; }); React.useEffect(() => { try { localStorage.setItem(LS_AUTORIZACIONES, JSON.stringify(rows)); } catch (e) {} }, [rows]); return [rows, setRows]; } function useCreditos() { const [rows, setRows] = React.useState(() => { try { const raw = localStorage.getItem(LS_CREDITOS); if (raw) { const p = JSON.parse(raw); if (Array.isArray(p)) return p; } } catch (e) {} return INITIAL_CREDITOS; }); React.useEffect(() => { try { localStorage.setItem(LS_CREDITOS, JSON.stringify(rows)); } catch (e) {} }, [rows]); return [rows, setRows]; } window.useClientes = useClientes; window.useAutorizaciones = useAutorizaciones; window.useCreditos = useCreditos; window.INITIAL_CLIENTES = INITIAL_CLIENTES; // fuente para sembrar `empresa` (cruds-empresas.jsx)