$endpointGeoDelix = 'https://mobilex.fenixdev.uno/mapas-box/central/api/agregar-entrega-erp.php'; try { do { $idEntregaErp = (int) {ID}; $idReceptor = (int) {idreceptor}; if ($idEntregaErp <= 0) { sc_ajax_message( 'El registro no tiene un ID valido.', 'GeoDelix', 'type=error&button=Y' ); break; } if ($idReceptor <= 0) { sc_ajax_message( 'El comprobante no tiene un receptor valido.', 'GeoDelix', 'type=error&button=Y' ); break; } $sql = " SELECT CONCAT_WS( ' : ', CONCAT_WS( ' - ', NULLIF(TRIM(nombre), ''), NULLIF(TRIM(ncomercial), '') ), NULLIF(TRIM(nfiscal), '') ) AS nombre, COALESCE(celular, '') AS celular, COALESCE(documento, '') AS documento FROM receptores WHERE ID = " . $idReceptor . " LIMIT 1 "; sc_lookup(ds_cliente, $sql); if ({ds_cliente} === false) { sc_ajax_message( 'Error al consultar los datos del receptor.', 'GeoDelix', 'type=error&button=Y' ); break; } if (empty({ds_cliente})) { sc_ajax_message( 'No se encontro el receptor asociado al comprobante.', 'GeoDelix', 'type=error&button=Y' ); break; } $clienteNombre = trim((string) ({ds_cliente}[0][0] ?? '')); $clienteTelefono = trim((string) ({ds_cliente}[0][1] ?? '')); $clienteDocumento = trim((string) ({ds_cliente}[0][2] ?? '')); if ($clienteNombre === '') { sc_ajax_message( 'La entrega no tiene nombre de cliente.', 'GeoDelix', 'type=error&button=Y' ); break; } $serieValor = {serie}; $numeroValor = {numero}; $serie = is_scalar($serieValor) ? trim((string) $serieValor) : ''; $numero = is_scalar($numeroValor) ? trim((string) $numeroValor) : ''; if ($serie !== '' && $numero !== '') { $facturaNumero = $serie . ' - ' . $numero; } elseif ($serie !== '') { $facturaNumero = $serie; } else { $facturaNumero = $numero; } $origenId = 'ERP-' . $idEntregaErp; $payload = [ 'origen' => 'erp', 'origen_id' => $origenId, 'factura_numero' => $facturaNumero, 'cliente_nombre' => $clienteNombre, 'cliente_telefono' => $clienteTelefono, 'cliente_documento' => $clienteDocumento, 'direccion_entrega' => '', 'referencia_entrega' => '', 'latlong' => '', 'horario_entrega' => '', 'prioridad' => 0, 'chofer_id' => 0, 'repartidor_nombre' => '' ]; $jsonPayload = json_encode( $payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); if ($jsonPayload === false) { sc_ajax_message( 'No se pudo generar el envio para GeoDelix.', 'GeoDelix', 'type=error&button=Y' ); break; } $curl = curl_init($endpointGeoDelix); if ($curl === false) { sc_ajax_message( 'No se pudo iniciar la conexion con GeoDelix.', 'GeoDelix', 'type=error&button=Y' ); break; } curl_setopt_array($curl, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $jsonPayload, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json; charset=utf-8', 'Accept: application/json' ] ]); $respuestaRaw = curl_exec($curl); $curlError = curl_error($curl); $curlErrno = curl_errno($curl); $httpCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($curlErrno !== 0 || $respuestaRaw === false) { $mensajeCurl = 'No se pudo conectar con GeoDelix.'; if ($curlError !== '') { $mensajeCurl .= ' ' . $curlError; } sc_ajax_message( $mensajeCurl, 'GeoDelix', 'type=error&button=Y' ); break; } $respuestaGeoDelix = json_decode((string) $respuestaRaw, true); if (!is_array($respuestaGeoDelix)) { sc_ajax_message( 'GeoDelix devolvio una respuesta invalida. HTTP ' . $httpCode . '.', 'GeoDelix', 'type=error&button=Y' ); break; } if ($httpCode < 200 || $httpCode >= 300 || empty($respuestaGeoDelix['ok'])) { $mensajeApi = trim((string) ($respuestaGeoDelix['error'] ?? 'GeoDelix rechazo la entrega.')); sc_ajax_message( $mensajeApi . ' HTTP ' . $httpCode . '.', 'GeoDelix', 'type=error&button=Y' ); break; } $geodelixId = (int) ($respuestaGeoDelix['id'] ?? 0); if (empty($respuestaGeoDelix['created'])) { sc_ajax_message( 'La entrega #' . $geodelixId . ' - ' . $origenId . ' ya existe en GeoDelix.', 'Entrega duplicada', 'type=warning&button=Y' ); break; } sc_ajax_message( 'Entrega #' . $geodelixId . ' - ' . $origenId . ' agregada correctamente en GeoDelix.', 'GeoDelix', 'type=success&button=Y' ); } while (false); } catch (Throwable $errorGeoDelix) { sc_ajax_message( 'No se pudo preparar la entrega: ' . $errorGeoDelix->getMessage(), 'GeoDelix', 'type=error&button=Y' ); }