فهرست منبع

Merge branch 'hotfix/remove_old_ref_service'

Olivier Massot 3 سال پیش
والد
کامیت
e1c58e5aaf

+ 4 - 4
src/Service/Dolibarr/DolibarrAccountCreator.php

@@ -19,18 +19,18 @@ class DolibarrAccountCreator
     ];
 
     public function __construct(
-        private DolibarrService $dolibarrService,
+        private DolibarrApiService $dolibarrApiService,
     )
     {}
 
     public function getDolibarrAccount(int $id): DolibarrAccount {
 
         // Get dolibarr account (society)
-        $accountData = $this->dolibarrService->getSociety($id);
+        $accountData = $this->dolibarrApiService->getSociety($id);
         $dolibarrAccount = $this->createDolibarrAccount($id, $accountData);
 
         // Get active contract and services
-        $contractData = $this->dolibarrService->getActiveContract($dolibarrAccount->getSocId());
+        $contractData = $this->dolibarrApiService->getActiveContract($dolibarrAccount->getSocId());
 
         if ($contractData !== null) {
             $contract = $this->createDolibarrContract($contractData);
@@ -38,7 +38,7 @@ class DolibarrAccountCreator
         }
 
         // get bills
-        $billsData = $this->dolibarrService->getBills($dolibarrAccount->getSocId());
+        $billsData = $this->dolibarrApiService->getBills($dolibarrAccount->getSocId());
         foreach ($billsData as $billData) {
             $bill = $this->createDolibarrBill($billData);
             $dolibarrAccount->addBill($bill);

+ 3 - 1
src/Service/Dolibarr/DolibarrApiService.php

@@ -30,6 +30,8 @@ class DolibarrApiService extends ApiRequestService
      * @throws HttpException
      */
     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 :(
         return $this->getJsonContent("thirdparties" , [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])[0];
     }
 
@@ -48,7 +50,7 @@ class DolibarrApiService extends ApiRequestService
         } catch (HttpException $e) {
             if ($e->getStatusCode() === 404) {
                 // /!\ The dolibarr API will return a 404 error if no results are found...
-                return [];
+                return null;
             }
             throw $e;
         }

+ 0 - 84
src/Service/Dolibarr/DolibarrService.php

@@ -1,84 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace App\Service\Dolibarr;
-
-use App\Service\ApiRequestService;
-use JetBrains\PhpStorm\Pure;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
-use Symfony\Contracts\HttpClient\HttpClientInterface;
-
-/**
- * Service d'appel à l'API dolibarr
- *
- * @see https://prod-erp.2iopenservice.com/api/index.php/explorer/
- */
-class DolibarrService extends ApiRequestService
-{
-    #[Pure]
-    function __construct(HttpClientInterface $dolibarr_client)
-    {
-        parent::__construct($dolibarr_client);
-    }
-
-    /**
-     * Get a dolibarr society by its opentalent organization id
-     *
-     * @param int $organizationId
-     * @return array
-     */
-    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 :(
-        return $this->getJsonContent("thirdparties" , ["limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])[0];
-    }
-
-    /**
-     * Get the first active contract for the given dolibarr society
-     *
-     * @param int $socId
-     * @return array|null
-     */
-    public function getActiveContract(int $socId): ?array {
-        try {
-            return $this->getJsonContent(
-                "contracts",
-                ["limit" => "1", "sqlfilters" => "statut=1", "thirdparty_ids=" . $socId => null]
-            )[0];
-        } catch (NotFoundHttpException) {
-            return null;
-        }
-    }
-
-    /**
-     * Get a society bills by their society id
-     *
-     * @param int $socId
-     * @return array
-     */
-    public function getBills(int $socId): array {
-        try {
-            return $this->getJsonContent(
-                "invoices",
-                ["sortfield" => "datef", "sortorder" => "DESC", "limit" => 5, "sqlfilters" => "fk_soc=" . $socId]);
-        } catch (NotFoundHttpException) {
-            return [];
-        }
-    }
-
-    /**
-     *
-     * @param $organization
-     * @return void
-     */
-    public function createSociety($organization): void
-    {
-        $body = sprintf(
-            '{"name":"%s","client":"2","code_client":"-1","ref_int":"%s","import_key":"crm"}',
-            $organization->getName(),
-            $organization->getId()
-        );
-
-        $this->request('POST', "api/index.php/thirdparties", [], ['body' => $body]);
-    }
-}