|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|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 */ public function getBills(int $socId): array { try { return $this->getJsonContent( 'invoices', [ 'sortfield' => 'datef', 'sortorder' => 'DESC', 'limit' => 5, 'sqlfilters' => "(fk_soc:=:$socId) and (fk_statut:!=:0)" ]); } 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 last order of the given society. * * @param int $socId * @return array|null * @throws \JsonException */ public function getLastOrder(int $socId): ?array { try { $results = $this->getJsonContent( 'orders', [ 'sortfield' => 't.date_valid', 'sortorder' => 'DESC', 'limit' => 1, '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 null; } throw $e; } return empty($results) ? null : $results[0]; } /** * Get all the societies which are Opentalent client. * * @return array * * @throws HttpException */ public function getAllClients(): array { return $this->getJsonContent( 'thirdparties', ['limit' => '1000000', 'sqlfilters' => 'client:=:1'] ); } /** * Get the society contacts. * * @return array * * @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 * * @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 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'); } } /** * @return array The resulting document. Ex : * { * "filename": "CO2502-0380.pdf", * "content-type": "application/pdf", * "filesize": 10660, * "content": "JVBERi0xLjcKJeLjz9MKNyAwIG9iago8PCAvV...", * "encoding": "base64" * } * * @throws \JsonException */ public function downloadBillingDocPdf(string $type, string $docRef): array { if (!DolibarrDocTypeEnum::tryFrom($type)) { throw new \InvalidArgumentException(sprintf('Invalid type "%s" provided. Allowed values are: %s', $type, implode(', ', array_map(fn ($t) => $t->value, DolibarrDocTypeEnum::cases())))); } $route = 'documents/download?modulepart='.$type.'&original_file='.urlencode("$docRef/$docRef.pdf"); return $this->getJsonContent($route); } }