HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux Bradford-Sitios 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: www-data (33)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/informe-admision/app/Services/ApiOdooService.php
<?php

namespace App\Services;

use App\Models\ApiConnections;
use DOMDocument;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

require_once base_path('app/Libraries/ripcord-master/ripcord.php');

class ApiOdooService
{
    protected $uid;
    public $models;
    protected $credentials;

    public function __construct()
    {
        $credentials = ApiConnections::where('software_id', 1)->first();

        if (empty($credentials)) {
            throw new Exception('Error de configuración de cuenta API', 503);
        }
        if (empty($credentials->endpoint) || empty($credentials->database) || empty($credentials->username) || empty($credentials->api_key)) {
            throw new Exception('Error de configuración de cuenta API', 503);
        }
        if (!$credentials->status) {
            throw new Exception('Cuenta API deshabilitada', 503);
        }
        if($credentials->deleted) {
            throw new Exception('Cuenta API no existe o fue eliminada', 404);
        }
        try {
            $this->models = \ripcord::client($credentials->endpoint . '/xmlrpc/2/object');
            $this->uid = \ripcord::client($credentials->endpoint . '/xmlrpc/2/common')->authenticate($credentials->database, $credentials->username, $credentials->api_key, array());
    
            if (!$this->uid || isset($this->uid['faultCode']) || !is_int($this->uid)) {
                throw new Exception('Error de autenticación en Odoo', 401);
            }
            $this->credentials = $credentials;

        } catch (Exception $e) {
            if ($e->getCode() == 401) {
                throw $e;  // Se lanza el error 401 si es de autenticación
            }
            // Si es otro tipo de error de configuración o problema de conexión, lanzamos un 503
            throw new Exception('Error de configuración de cuenta API', 503);
        }
    }

    // Listar modelos
    public function listModel($model, $fields=[], $domain=[])
    {
        $records = $this->models->execute_kw(
            $this->credentials->database,
            $this->uid,
            $this->credentials->api_key,
            $model,
            'search_read',
            [
                $domain,
            ],
            [
                'fields' => $fields
            ]
        );

        if (isset($records['faultCode'])) {
            throw new Exception($records['faultString'], 503);
        }

        return $records;
    }

    // Listar modelos usando lotes
    public function listModelBatches($model, $fields=[], $domain=[], $batch_size=500)
    {
        $ids = $this->models->execute_kw(
            $this->credentials->database,
            $this->uid,
            $this->credentials->api_key,
            $model,
            'search',
            [
                $domain,
            ]
        );

        if (isset($ids['faultCode'])) {
            throw new Exception($ids['faultString'], 503);
        }
            
        $index = 0;
        $records = [];

        while ($index < count($ids)) {
            $batchIds = array_slice($ids, $index, $batch_size);

            $batchRecords = $this->models->execute_kw(
                $this->credentials->database,
                $this->uid,
                $this->credentials->api_key, 
                $model, 
                'read', 
                array($batchIds),
                [
                    'fields' => $fields
                ]
            );
            // return $batchRecords;
            if (!empty($batchRecords)) {
                $records = array_merge($records, $batchRecords);
            }
            $index += $batch_size;
        }

        return $records;
    }
}