DolibarrApiService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Dolibarr;
  4. use App\Service\Rest\ApiRequestService;
  5. use JetBrains\PhpStorm\Pure;
  6. use Symfony\Component\HttpKernel\Exception\HttpException;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. /**
  9. * Service d'appel à l'API dolibarr
  10. *
  11. * @see https://prod-erp.2iopenservice.com/api/index.php/explorer/
  12. */
  13. class DolibarrApiService extends ApiRequestService
  14. {
  15. /** @noinspection SenselessProxyMethodInspection Method shall be kept to allow dependency injections, even if empty */
  16. #[Pure]
  17. public function __construct(HttpClientInterface $dolibarr_client)
  18. {
  19. parent::__construct($dolibarr_client);
  20. }
  21. /**
  22. * Get a dolibarr society by its opentalent organization id
  23. *
  24. * @param int $organizationId
  25. * @return array<mixed>|null
  26. * @throws \JsonException
  27. */
  28. public function getSociety(int $organizationId): ?array
  29. {
  30. // impossible to retrieve a society by its extrafield 2iopen_organization_id (thanks dolibarr), so
  31. // we need to store the organization id in two fields: 2iopen_organization_id and ref_int :(
  32. try {
  33. return $this->getJsonContent("thirdparties" , [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])[0];
  34. } catch (HttpException $e) {
  35. if ($e->getStatusCode() === 404) {
  36. // /!\ The dolibarr API will return a 404 error if no results are found...
  37. return null;
  38. }
  39. throw $e;
  40. }
  41. }
  42. /**
  43. * Get the first active contract for the given dolibarr society
  44. *
  45. * @param int $socId
  46. * @return array<mixed>|null
  47. */
  48. public function getActiveContract(int $socId): ?array
  49. {
  50. try {
  51. return $this->getJsonContent(
  52. "contracts",
  53. ["limit" => "1", "sqlfilters" => "statut=1", "thirdparty_ids" => $socId]
  54. )[0];
  55. } catch (HttpException $e) {
  56. if ($e->getStatusCode() === 404) {
  57. // /!\ The dolibarr API will return a 404 error if no results are found...
  58. return null;
  59. }
  60. throw $e;
  61. }
  62. }
  63. /**
  64. * Get a society bills by their society id
  65. *
  66. * @param int $socId
  67. * @return array<mixed>
  68. */
  69. public function getBills(int $socId): array
  70. {
  71. try {
  72. return $this->getJsonContent(
  73. "invoices",
  74. ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId]);
  75. } catch (HttpException $e) {
  76. if ($e->getStatusCode() === 404) {
  77. // /!\ The dolibarr API will return a 404 error if no results are found...
  78. return [];
  79. }
  80. throw $e;
  81. }
  82. }
  83. /**
  84. * Get all the societies which are Opentalent client
  85. *
  86. * @return array<mixed>
  87. * @throws HttpException
  88. */
  89. public function getAllClients(): array
  90. {
  91. return $this->getJsonContent(
  92. "thirdparties",
  93. ["limit" => "1000000", "sqlfilters" => "client=1"]
  94. );
  95. }
  96. /**
  97. * Get the society contacts
  98. *
  99. * @return array<mixed>
  100. * @throws HttpException
  101. */
  102. public function getContacts(int $socId): array
  103. {
  104. try {
  105. return $this->getJsonContent(
  106. "contacts",
  107. ['limit' => 1000, 'thirdparty_ids' => $socId],
  108. );
  109. } catch (HttpException $e) {
  110. if ($e->getStatusCode() === 404) {
  111. // /!\ The dolibarr API will return a 404 error if no results are found...
  112. return [];
  113. }
  114. throw $e;
  115. }
  116. }
  117. /**
  118. * Get the society contacts that have a non-null personId
  119. *
  120. * @return array<mixed>
  121. * @throws HttpException
  122. */
  123. public function getActiveOpentalentContacts(int $socId): array
  124. {
  125. // On est obligé ici de passer la query en dur, sinon les parenthèses sont encodées,
  126. // et dolibarr est pas content :(
  127. try {
  128. return $this->getJsonContent(
  129. "contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)"
  130. );
  131. } catch (HttpException $e) {
  132. if ($e->getStatusCode() === 404) {
  133. // /!\ The dolibarr API will return a 404 error if no results are found...
  134. return [];
  135. }
  136. throw $e;
  137. }
  138. }
  139. /**
  140. * Get the society tags
  141. *
  142. * @param int $socId The society ID
  143. * @return array<int> The array of tags associated with the society
  144. * @throws HttpException|\JsonException if an HTTP error occurs
  145. */
  146. public function getSocietyTagsIds(int $socId): array {
  147. try {
  148. return array_map(
  149. function ($x) { return (int)$x['id']; },
  150. $this->getJsonContent("/thirdparties/$socId/categories")
  151. );
  152. } catch (HttpException $e) {
  153. if ($e->getStatusCode() === 404) {
  154. // /!\ The dolibarr API will return a 404 error if no results are found...
  155. return [];
  156. }
  157. throw $e;
  158. }
  159. }
  160. }