-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDolibarrObject.php
More file actions
470 lines (382 loc) · 14.5 KB
/
DolibarrObject.php
File metadata and controls
470 lines (382 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/* Gestion commune pour les objets récupérés depuis Dolibarr */
abstract class DolibarrObject
{
protected $data;
public function __construct($data = null)
{
if (is_object($data)) {
$this->data = $data;
} else {
$this->data = new stdClass();
}
}
/**
* Définit un champ dans la structure de données à envoyer à Dolibarr.
*
* @param string $key Nom du champ
* @param mixed $value Valeur du champ
* @return void
*/
public function set(string $key, $value): void
{
$this->data->$key = $value;
}
// Méthodes get générique pour un champ (retourne la valeur string ou null si pas défini)
public function get(string $key): ?string
{
return $this->data->$key ?? null;
}
// Méthodes get pour les option
public function getOption(string $optionKey): ?string
{
return $this->data->array_options->$optionKey ?? null;
}
// Méthodes "getter" pour chaque champ nommé
public function getId(): ?string
{
return $this->get('id');
}
/**
* Récupère le `socid` associé à la proposition.
*
* @return int|null L'ID du client (socid) ou null si non trouvé.
*/
public function getSocId(): ?string
{
return $this->get('socid');
}
public function getRef(): ?string
{
return $this->get('ref');
}
public function getRefExt(): ?string
{
return $this->get('ref_ext');
}
/**
* Retourne les objets liés exposés par Dolibarr sous linkedObjectsIds.
*
* @return array<string, array<int, string>>
*/
public function getLinkedObjectsIds(): array
{
if (!isset($this->data->linkedObjectsIds)) {
return [];
}
$linkedObjects = $this->data->linkedObjectsIds;
if (is_object($linkedObjects)) {
$linkedObjects = (array) $linkedObjects;
}
if (!is_array($linkedObjects)) {
return [];
}
$normalized = [];
foreach ($linkedObjects as $type => $ids) {
if (is_object($ids)) {
$ids = (array) $ids;
}
if (!is_array($ids)) {
$ids = [$ids];
}
$values = [];
foreach ($ids as $id) {
if ($id === null || $id === '') {
continue;
}
$values[] = (string) $id;
}
if ($values !== []) {
$normalized[(string) $type] = array_values(array_unique($values));
}
}
return $normalized;
}
/**
* Retourne les IDs liés pour un type donné.
*
* @param string $type Type lié, ex: propal, facture, order_supplier.
* @return array<int, string>
*/
public function getLinkedObjectIds(string $type): array
{
$type = trim($type);
if ($type === '') {
return [];
}
$linkedObjects = $this->getLinkedObjectsIds();
if (isset($linkedObjects[$type])) {
return $linkedObjects[$type];
}
$aliases = [
'proposal' => 'propal',
'proposals' => 'propal',
'propal' => 'proposal',
'invoice' => 'facture',
'invoices' => 'facture',
'facture' => 'invoice',
];
$alias = $aliases[strtolower($type)] ?? null;
if ($alias !== null && isset($linkedObjects[$alias])) {
return $linkedObjects[$alias];
}
return [];
}
public function getStatus(): ?string
{
return $this->get('status');
}
public function getProjectId(): ?string
{
return $this->get('fk_project');
}
public static function fetchFromDolibarr($endpoint, $retryCount = 3, $initialDelaySeconds = 10)
{
$apiKey = DOLIBARR_API_KEY;
$url = DOLIBARR_REST_URL . $endpoint;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"DOLAPIKEY: $apiKey"
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_ENCODING, ''); // Accepte toutes les encodages et favorise UTF-8
$attempts = 0;
$response = false;
while ($attempts < $retryCount && !$response) {
$response = curl_exec($ch);
$attempts++;
if (curl_errno($ch)) {
error_log("Tentative $attempts : Erreur cURL : " . curl_error($ch));
$response = false;
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode !== 200) {
error_log("Tentative $attempts : Erreur HTTP : $httpCode pour l'URL : $url");
$response = false;
if ($httpCode == 429) {
sleep($initialDelaySeconds * $attempts);
}
} else {
break;
}
}
if (!$response && $httpCode != 429) {
sleep($initialDelaySeconds);
}
}
curl_close($ch);
if (!$response) {
error_log("Échec de récupération des données après $retryCount tentatives.");
return null;
}
// Nettoyage non destructif uniquement.
// Important: ne pas utiliser stripslashes() ici, car cela casse les
// séquences JSON valides comme \u00e9 ou les guillemets échappés.
$response = trim($response);
$response = preg_replace('/^\xEF\xBB\xBF/', '', $response);
$data = json_decode($response, false, 512, JSON_INVALID_UTF8_SUBSTITUTE);
if (json_last_error() === JSON_ERROR_NONE) {
// Certains retours Dolibarr peuvent être double-encodés:
// json_decode() renvoie alors une chaîne contenant du JSON.
if (is_string($data)) {
$maybeJson = trim($data);
if ($maybeJson !== '' && ($maybeJson[0] === '[' || $maybeJson[0] === '{')) {
$decodedAgain = json_decode($maybeJson, false, 512, JSON_INVALID_UTF8_SUBSTITUTE);
if (json_last_error() === JSON_ERROR_NONE) {
return $decodedAgain;
}
}
}
return $data;
}
error_log('Erreur de décodage JSON : ' . json_last_error_msg());
error_log('Réponse brute Dolibarr (aperçu) : ' . mb_substr($response, 0, 1000));
return null;
}
private static function sendToDolibarr(string $method, string $endpoint, $payload, int $retryCount = 3, int $initialDelaySeconds = 10, array $extraHeaders = []): ?object
{
$apiKey = DOLIBARR_API_KEY;
$url = rtrim(DOLIBARR_REST_URL, '/') . $endpoint;
$jsonPayload = json_encode(is_object($payload) ? (array)$payload : $payload, JSON_UNESCAPED_UNICODE);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); // <-- pour séparer header/body
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
$headers = array_merge([
"DOLAPIKEY: $apiKey",
"Content-Type: application/json",
"Accept: application/json"
], $extraHeaders);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($jsonPayload !== false) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$attempt = 0;
while (true) {
$raw = curl_exec($ch);
$attempt++;
if ($raw === false) {
error_log("Tentative $method $attempt : Erreur cURL : " . curl_error($ch));
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$respHeaders = substr($raw, 0, $headerSize);
$body = substr($raw, $headerSize);
if ($httpCode === 200 || $httpCode === 201) {
// Réponse OK
$data = json_decode($body);
if (json_last_error() === JSON_ERROR_NONE) {
return is_int($data) ? (object)['result' => $data] : $data;
}
error_log("Réponse non-JSON ($method $httpCode) : " . substr($body, 0, 1000));
return null;
}
// Log détaillé sur erreur
$bodyPreview = trim(mb_substr($body ?? '', 0, 1000));
error_log("Erreur HTTP $httpCode $method $url (tentative $attempt). Body: " . ($bodyPreview !== '' ? $bodyPreview : '[vide]'));
// Politique de retry :
// - 429 et 5xx : on retente
// - 4xx (sauf 429) : ne pas retenter (erreur côté client/config)
if ($httpCode == 429 || ($httpCode >= 500 && $httpCode <= 599)) {
if ($attempt < $retryCount) {
sleep($initialDelaySeconds * $attempt);
continue;
}
}
// 4xx non-429 → stop net (ex: 401/403, liste d'IP, droits, payload invalide, etc.)
break;
}
if ($attempt >= $retryCount) {
break;
}
sleep($initialDelaySeconds);
}
// Derniers détails utiles pour debug
$reqHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
if ($reqHeaders) {
error_log("Headers requête envoyés:\n$reqHeaders");
}
curl_close($ch);
error_log("Échec de $method vers Dolibarr après $attempt tentative(s).");
return null;
}
/**
* Effectue une requête POST vers l'API REST de Dolibarr.
*
* @param string $endpoint Endpoint REST (ex: /supplierinvoices)
* @param mixed $payload Données à envoyer (tableau ou objet)
* @param int $retryCount Nombre de tentatives en cas d’échec
* @param int $initialDelaySeconds Temps d’attente entre les tentatives
* @return object|null Réponse JSON décodée sous forme d’objet, ou null en cas d’erreur
*/
public static function postToDolibarr($endpoint, $payload, $retryCount = 3, $initialDelaySeconds = 10): ?object
{
return self::sendToDolibarr('POST', $endpoint, $payload, $retryCount, $initialDelaySeconds);
}
public static function putToDolibarr($endpoint, $payload, $retryCount = 3, $initialDelaySeconds = 10): ?object
{
return self::sendToDolibarr('PUT', $endpoint, $payload, $retryCount, $initialDelaySeconds);
}
public static function deleteToDolibarr($endpoint, $retryCount = 3, $initialDelaySeconds = 10): ?object
{
return self::sendToDolibarr('DELETE', $endpoint, [], $retryCount, $initialDelaySeconds);
}
public static function ping(string $endpoint = '/status'): bool
{
// Appel "léger" : 1 seule tentative, 1s d’attente si besoin.
// On suppose que /status renvoie 200 si OK (Dolibarr REST module).
$data = self::fetchFromDolibarr($endpoint, /* retryCount */ 1, /* initialDelaySeconds */ 1);
// fetchFromDolibarr() retourne null si HTTP≠200 ou JSON invalide
return $data !== null;
}
/**
* Crée un objet dans Dolibarr.
*
* Must be implemented in child classes.
*/
public function createInDolibarr(): ?object
{
return null;
}
public function printData()
{
// Vérifie si $this->data est un objet ou un tableau et le convertit en JSON formaté
$formattedData = json_encode($this->data, JSON_PRETTY_PRINT);
echo "<pre>" . $formattedData . "</pre>";
}
public function getLines(): ?array
{
return $this->data->lines ?? null;
}
/**
* Ajoute une ligne dans le tableau data->lines de l'objet courant.
*
* @param array $line Ligne de facture Dolibarr formatée
* @return void
*/
public function addLine(array $line): void
{
if (!isset($this->data->lines) || !is_array($this->data->lines)) {
$this->data->lines = [];
}
$this->data->lines[] = $line;
}
/**
* Convertit un timestamp UNIX en une date formatée (Y-m-d), avec gestion du fuseau horaire.
*
* @param int|string|null $timestamp Le timestamp UNIX ou une date sous forme de chaîne.
* @param string $timezone Le fuseau horaire (par défaut 'Europe/Paris').
* @return string|null La date formatée ou null si la valeur est invalide.
*/
protected function getFormattedDate($timestamp, $timezone = 'Europe/Paris'): ?string
{
if (empty($timestamp)) {
return null; // Si la date est vide ou nulle
}
try {
// Si c'est un timestamp numérique, on le convertit
if (is_numeric($timestamp)) {
$date = new DateTime("@$timestamp"); // Le '@' indique qu'il s'agit d'un timestamp UNIX
} else {
$date = new DateTime($timestamp); // Cas d'une date au format texte
}
// Ajustement du fuseau horaire
$date->setTimezone(new DateTimeZone($timezone));
// Formatage final
return $date->format('Y-m-d');
} catch (Exception $e) {
error_log('Erreur de conversion de date : ' . $e->getMessage());
return null;
}
}
/**
* Récupère la liste des fichiers associés à un module Dolibarr pour une référence donnée.
*
* @param string $module Le module Dolibarr (ex: 'facture', 'projet', etc.).
* @param string $ref La référence de l'objet Dolibarr (ex: ID de la facture).
*
* @return array Liste des noms de fichiers (filtrés par extension si nécessaire).
*/
public function getFilenames(string $module, string $ref): array
{
// Construction de l'endpoint pour récupérer les documents liés à un module et une référence donnée
$endpoint = "/documents/?modulepart=" . urlencode($module) . "&ref=" . urlencode($ref);
// Appel à l'API Dolibarr pour récupérer les données des documents
$data = self::fetchFromDolibarr($endpoint);
// Initialisation du tableau des noms de fichiers
$filenames = [];
// Vérification de la validité des données reçues
if (is_array($data)) {
foreach ($data as $file) {
if (isset($file->filename)) {
$filenames[] = $file->filename;
}
}
}
return $filenames;
}
}