DolibarrApiService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  26. * @throws HttpException
  27. */
  28. public function getSociety(int $organizationId): array {
  29. // impossible to retrieve a society by its extrafield 2iopen_organization_id (thanks dolibarr), so
  30. // we need to store the organization id in two fields: 2iopen_organization_id and ref_int :(
  31. return $this->getJsonContent("thirdparties" , [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])[0];
  32. }
  33. /**
  34. * Get the first active contract for the given dolibarr society
  35. *
  36. * @param int $socId
  37. * @return array|null
  38. */
  39. public function getActiveContract(int $socId): ?array {
  40. try {
  41. return $this->getJsonContent(
  42. "contracts",
  43. ["limit" => "1", "sqlfilters" => "statut=1", "thirdparty_ids" => $socId]
  44. )[0];
  45. } catch (HttpException $e) {
  46. if ($e->getStatusCode() === 404) {
  47. // /!\ The dolibarr API will return a 404 error if no results are found...
  48. return null;
  49. }
  50. throw $e;
  51. }
  52. }
  53. /**
  54. * Get a society bills by their society id
  55. *
  56. * @param int $socId
  57. * @return array
  58. */
  59. public function getBills(int $socId): array {
  60. try {
  61. return $this->getJsonContent(
  62. "invoices",
  63. ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId]);
  64. } catch (HttpException $e) {
  65. if ($e->getStatusCode() === 404) {
  66. // /!\ The dolibarr API will return a 404 error if no results are found...
  67. return [];
  68. }
  69. throw $e;
  70. }
  71. }
  72. /**
  73. * Get all the societies which are Opentalent client
  74. * @throws HttpException
  75. */
  76. public function getAllClients(): array
  77. {
  78. return $this->getJsonContent(
  79. "thirdparties",
  80. ["limit" => "1000000", "sqlfilters" => "client=1"]
  81. );
  82. }
  83. /**
  84. * Get the society contacts
  85. *
  86. * @throws HttpException
  87. */
  88. public function getContacts(int $socId): array
  89. {
  90. try {
  91. return $this->getJsonContent(
  92. "contacts",
  93. ['limit' => 1000, 'thirdparty_ids' => $socId],
  94. );
  95. } catch (HttpException $e) {
  96. if ($e->getStatusCode() === 404) {
  97. // /!\ The dolibarr API will return a 404 error if no results are found...
  98. return [];
  99. }
  100. throw $e;
  101. }
  102. }
  103. /**
  104. * Get the society contacts that have a non-null personId
  105. *
  106. * @throws HttpException
  107. */
  108. public function getActiveOpentalentContacts(int $socId): array
  109. {
  110. // On est obligé ici de passer la query en dur, sinon les parenthèses sont encodées,
  111. // et dolibarr est pas content :(
  112. try {
  113. return $this->getJsonContent(
  114. "contacts?limit=1000&t.statut=1&thirdparty_ids=" . $socId . "&sqlfilters=(te.2iopen_person_id%3A%3E%3A0)"
  115. );
  116. } catch (HttpException $e) {
  117. if ($e->getStatusCode() === 404) {
  118. // /!\ The dolibarr API will return a 404 error if no results are found...
  119. return [];
  120. }
  121. throw $e;
  122. }
  123. }
  124. }