| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Dolibarr;
- use App\Entity\Organization\Organization;
- use App\Service\Rest\ApiRequestService;
- use JetBrains\PhpStorm\Pure;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- /**
- * Service d'appel à l'API dolibarr.
- *
- * @see https://prod-erp.2iopenservice.com/api/index.php/explorer/
- */
- class DolibarrApiService extends ApiRequestService
- {
- /** @noinspection SenselessProxyMethodInspection Method shall be kept to allow dependency injections, even if empty */
- #[Pure]
- public function __construct(HttpClientInterface $dolibarr_client)
- {
- parent::__construct($dolibarr_client);
- }
- /**
- * Get a dolibarr society by its opentalent organization id.
- *
- * @return array<mixed>|null
- *
- * @throws \JsonException
- */
- public function getSociety(int $organizationId): ?array
- {
- // impossible to retrieve a society by its extrafield 2iopen_organization_id (thanks dolibarr), so
- // we need to store the organization id in two fields: 2iopen_organization_id and ref_int :(
- try {
- return $this->getJsonContent(
- 'thirdparties',
- [
- 'limit' => '1',
- 'sqlfilters' => '(ef.2iopen_organization_id:=:'.$organizationId.')',
- ]
- )[0];
- } catch (HttpException $e) {
- if ($e->getStatusCode() === 404) {
- // /!\ The dolibarr API will return a 404 error if no results are found...
- return null;
- }
- throw $e;
- }
- }
- /**
- * Get the first active contract for the given dolibarr society.
- *
- * @return array<mixed>|null
- */
- public function getActiveContract(int $socId): ?array
- {
- try {
- return $this->getJsonContent(
- 'contracts',
- ['limit' => '1', 'sqlfilters' => 'statut:=:1', 'thirdparty_ids' => $socId]
- )[0];
- } catch (HttpException $e) {
- if ($e->getStatusCode() === 404) {
- // /!\ The dolibarr API will return a 404 error if no results are found...
- return null;
- }
- throw $e;
- }
- }
- /**
- * Get a society bills by their society id.
- *
- * @return array<mixed>
- */
- public function getBills(int $socId): array
- {
- try {
- return $this->getJsonContent(
- 'invoices',
- ['sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => 'fk_soc:=:'.$socId]);
- } catch (HttpException $e) {
- if ($e->getStatusCode() === 404) {
- // /!\ The dolibarr API will return a 404 error if no results are found...
- return [];
- }
- throw $e;
- }
- }
- /**
- * Get all the societies which are Opentalent client.
- *
- * @return array<mixed>
- *
- * @throws HttpException
- */
- public function getAllClients(): array
- {
- return $this->getJsonContent(
- 'thirdparties',
- ['limit' => '1000000', 'sqlfilters' => 'client:=:1']
- );
- }
- /**
- * Get the society contacts.
- *
- * @return array<mixed>
- *
- * @throws HttpException
- */
- public function getContacts(int $socId): array
- {
- try {
- return $this->getJsonContent(
- 'contacts',
- ['limit' => 1000, 'thirdparty_ids' => $socId],
- );
- } catch (HttpException $e) {
- if ($e->getStatusCode() === 404) {
- // /!\ The dolibarr API will return a 404 error if no results are found...
- return [];
- }
- throw $e;
- }
- }
- /**
- * Get the society contacts that have a non-null personId.
- *
- * @return array<mixed>
- *
- * @throws HttpException
- */
- public function getActiveOpentalentContacts(int $socId): array
- {
- // On est obligé ici de passer la query en dur, sinon les parenthèses sont encodées,
- // et dolibarr est pas content :(
- try {
- return $this->getJsonContent(
- 'contacts?limit=1000&t.statut=1&thirdparty_ids='.$socId.'&sqlfilters:=:(te.2iopen_person_id%3A%3E%3A0)'
- );
- } catch (HttpException $e) {
- if ($e->getStatusCode() === 404) {
- // /!\ The dolibarr API will return a 404 error if no results are found...
- return [];
- }
- throw $e;
- }
- }
- /**
- * Get the society tags.
- *
- * @param int $socId The society ID
- *
- * @return array<int> The array of tags associated with the society
- *
- * @throws HttpException|\JsonException if an HTTP error occurs
- */
- public function getSocietyTagsIds(int $socId): array
- {
- try {
- return array_map(
- function ($x) { return (int) $x['id']; },
- $this->getJsonContent("/thirdparties/$socId/categories")
- );
- } catch (HttpException $e) {
- if ($e->getStatusCode() === 404) {
- // /!\ The dolibarr API will return a 404 error if no results are found...
- return [];
- }
- throw $e;
- }
- }
- /**
- * Créé une société dans la DB dolibarr, et retourne l'id de celle-ci.
- */
- public function createSociety(Organization $organization, bool $client = false): mixed
- {
- $body = [
- 'name' => $organization->getName(),
- 'client' => $client ? 1 : 2,
- 'code_client' => -1,
- 'import_key' => 'crm',
- 'array_options' => ['options_2iopen_organization_id' => $organization->getId()],
- ];
- /** @var Response $response */
- $response = $this->post('/thirdparties', $body);
- return json_decode($response->getContent(), true);
- }
- /**
- * Delete the organization from Dolibarr.
- *
- * @throws \JsonException
- * @throws TransportExceptionInterface
- */
- public function switchSocietyToProspect(int $organizationId): void
- {
- $socId = $this->getSociety($organizationId)['id'];
- $res = $this->put(
- "thirdparties/$socId",
- ['client' => 2],
- );
- if ($res->getStatusCode() !== 200) {
- throw new HttpException($res->getStatusCode(), 'Error while updating the society in Dolibarr');
- }
- }
- }
|