Browse Source

implements basic dolibarr data provider

Olivier Massot 4 years ago
parent
commit
d16c318e9c

+ 33 - 70
src/ApiResources/Dolibarr/DolibarrAccount.php

@@ -5,173 +5,136 @@ namespace App\ApiResources\Dolibarr;
 
 use ApiPlatform\Core\Annotation\ApiProperty;
 use ApiPlatform\Core\Annotation\ApiResource;
+use Symfony\Component\Serializer\Annotation\Groups;
 
 /**
  * Données de l'organization retournées par l'API Dolibarr
- *
- * @ApiResource(
- *     compositeIdentifier=false,
- *     itemOperations={
- *         "get"={
- *             "method"="GET",
- *             "path"="/dolibarr/account/{organizationId}"
- *          }
- *     }
- * )
  */
+#[ApiResource(
+    itemOperations: [
+        'get' => [
+            'method' => 'GET',
+            'path' => '/dolibarr/account/{organizationId}',
+            'requirements' => ['organizationId' => '\d+'],
+            'normalization_context' => [
+                'groups' => ['dolibarr_get']
+            ],
+        ],
+    ],
+    compositeIdentifier: false,
+)]
 class DolibarrAccount
 {
-    /**
-     * @var int
-     * @ApiProperty(identifier=true)
-     */
+    #[ApiProperty(identifier: true)]
+    #[Groups('dolibarr_get')]
     private int $organizationId;
+
     /**
      * Dolibarr societies pk
-     * @var int
      */
+    #[Groups('dolibarr_get')]
     private int $socId;
+
     /**
      * Opentalent client ref
-     * @var string
      */
-    private string $clientNumber;
+    #[Groups('dolibarr_get')]
+    private string $clientNumber = "";
+
     /**
      * Opentalent product owned
-     * @var string
      */
-    private string $product;
+    #[Groups('dolibarr_get')]
+    private string $product = "";
+
     /**
      * Services currently actives
-     * @var array
      */
-    private array $services;
+    #[Groups('dolibarr_get')]
+    private array $services = [];
+
     /**
      * Sms credit remaining
-     * @var string
      */
-    private string $smsCredit;
+    #[Groups('dolibarr_get')]
+    private string $smsCredit = "";
+
     /**
      * Last bills
-     * @var array
      */
-    private array $bills;
+    #[Groups('dolibarr_get')]
+    private array $bills = [];
 
-    /**
-     * @return int
-     */
     public function getOrganizationId(): int
     {
         return $this->organizationId;
     }
 
-    /**
-     * @param int $organizationId
-     */
     public function setOrganizationId(int $organizationId): void
     {
         $this->organizationId = $organizationId;
     }
 
-    /**
-     * @return int
-     */
     public function getSocId(): int
     {
         return $this->socId;
     }
 
-    /**
-     * @param int $socId
-     */
     public function setSocId(int $socId): void
     {
         $this->socId = $socId;
     }
 
-    /**
-     * @return string
-     */
     public function getClientNumber(): string
     {
         return $this->clientNumber;
     }
 
-    /**
-     * @param string $clientNumber
-     */
     public function setClientNumber(string $clientNumber): void
     {
         $this->clientNumber = $clientNumber;
     }
 
-    /**
-     * @return string
-     */
     public function getProduct(): string
     {
         return $this->product;
     }
 
-    /**
-     * @param string $product
-     */
     public function setProduct(string $product): void
     {
         $this->product = $product;
     }
 
-    /**
-     * @return array
-     */
     public function getServices(): array
     {
         return $this->services;
     }
 
-    /**
-     * @param array $services
-     */
     public function setServices(array $services): void
     {
         $this->services = $services;
     }
 
-    /**
-     * @return string
-     */
     public function getSmsCredit(): string
     {
         return $this->smsCredit;
     }
 
-    /**
-     * @param string $smsCredit
-     */
     public function setSmsCredit(string $smsCredit): void
     {
         $this->smsCredit = $smsCredit;
     }
 
-    /**
-     * @return array
-     */
     public function getBills(): array
     {
         return $this->bills;
     }
 
-    /**
-     * @param array $bills
-     */
     public function setBills(array $bills): void
     {
         $this->bills = $bills;
     }
 
-    /**
-     * @param DolibarrBill $bill
-     */
     public function addBill(DolibarrBill $bill): void
     {
         $this->bills[] = $bill;

+ 27 - 49
src/ApiResources/Dolibarr/DolibarrBill.php

@@ -5,123 +5,101 @@ namespace App\ApiResources\Dolibarr;
 
 use ApiPlatform\Core\Annotation\ApiProperty;
 use ApiPlatform\Core\Annotation\ApiResource;
+use Symfony\Component\Serializer\Annotation\Groups;
 
 /**
  * Bill of a society, retrieved from dolibarr
- *
- * @ApiResource(
- *     compositeIdentifier=false,
- *     collectionOperations={
- *         "get"={
- *             "method"="GET",
- *             "path"="/dolibarr/bills/{socId}"
- *          }
- *     }
- * )
  */
+#[ApiResource(
+    collectionOperations: [
+        'get' => [
+            'method' => 'GET',
+            'path' => '/dolibarr/bills/{socId}',
+            'requirements' => ['socId' => '\d+'],
+            'normalization_context' => [
+                'groups' => ['dolibarr_get']
+            ]
+        ],
+    ]
+)]
 class DolibarrBill
 {
     /**
-     * @var string
-     * @ApiProperty(identifier=true)
+     * Id of the dolibarr bill ( = invoice)
      */
-    private string $billId;
+    #[ApiProperty(identifier: true)]
+    #[Groups('dolibarr_get')]
+    private int $billId;
+
     /**
      * Id of the society
-     * @var int
      */
+    #[Groups('dolibarr_get')]
     private int $socId;
+
     /**
      * Date of the bill
-     * @var int
      */
+    #[Groups('dolibarr_get')]
     private int $date;
+
     /**
      * Amount incl VAT
-     * @var float
      */
+    #[Groups('dolibarr_get')]
     private float $amount;
+
     /**
      * Is the bill paid or not
-     * @var bool
      */
+    #[Groups('dolibarr_get')]
     private bool $paid;
 
-    /**
-     * @return string
-     */
-    public function getBillId(): string
+    public function getBillId(): int
     {
         return $this->billId;
     }
 
-    /**
-     * @param string $billId
-     */
-    public function setBillId(string $billId): void
+    public function setBillId(int $billId): void
     {
         $this->billId = $billId;
     }
 
-    /**
-     * @return int
-     */
     public function getSocId(): int
     {
         return $this->socId;
     }
 
-    /**
-     * @param int $socId
-     */
     public function setSocId(int $socId): void
     {
         $this->socId = $socId;
     }
 
-    /**
-     * @return int
-     */
     public function getDate(): int
     {
         return $this->date;
     }
 
-    /**
-     * @param int $date
-     */
     public function setDate(int $date): void
     {
         $this->date = $date;
     }
 
-    /**
-     * @return float
-     */
     public function getAmount(): float
     {
         return $this->amount;
     }
 
-    /**
-     * @param float $amount
-     */
     public function setAmount(float $amount): void
     {
         $this->amount = $amount;
     }
 
-    /**
-     * @return bool
-     */
     public function getPaid(): bool
     {
         return $this->paid;
     }
 
-    /**
-     * @param bool $paid
-     */
     public function setPaid(bool $paid): void
     {
         $this->paid = $paid;

+ 4 - 4
src/DataProvider/Dolibarr/DolibarrAccountDataProvider.php

@@ -34,20 +34,20 @@ final class DolibarrAccountDataProvider implements ItemDataProviderInterface, Re
         $dolibarrAccount = new DolibarrAccount();
 
         $accountData = $this->dolibarrService->getSociety($id);
-        $billsData = $this->dolibarrService->getBills($accountData['id']);
 
         $dolibarrAccount->setOrganizationId($id);
-        $dolibarrAccount->setSocId($accountData['id']);
+        $dolibarrAccount->setSocId((int)$accountData['id']);
         $dolibarrAccount->setClientNumber($accountData['code_client']);
         $dolibarrAccount->setProduct($accountData['array_options']['options_2iopen_software_used']);
         // $dolibarrAccount->setSmsCredit();
         // $dolibarrAccount->setServices($accountData['code_client']);
 
+        $billsData = $this->dolibarrService->getBills($dolibarrAccount->getSocId());
         foreach ($billsData as $billData) {
             $bill = new DolibarrBill();
-            $bill->setBillId($billData['id']);
+            $bill->setBillId((int)$billData['id']);
             $bill->setSocId($dolibarrAccount->getSocId());
-            $bill->setAmount($billData['total_ttc']);
+            $bill->setAmount((float)$billData['total_ttc']);
             $bill->setDate($billData['date']);
             $bill->setPaid((bool)$billData['paye']);
             $dolibarrAccount->addBill($bill);

+ 10 - 6
src/Service/Dolibarr/DolibarrService.php

@@ -9,6 +9,7 @@ use Symfony\Contracts\HttpClient\HttpClientInterface;
 
 class DolibarrService
 {
+    const BASE_URI = 'https://prod-erp.2iopenservice.com/api/index.php/';
     private HttpClientInterface $client;
 
     function __construct(HttpClientInterface $client)
@@ -23,7 +24,7 @@ class DolibarrService
      * @return array
      */
     public function getSociety(int $organizationId): array {
-        return $this->request("api/index.php/thirdparties?sqlfilters=ref_int%3D" . $organizationId);
+        return $this->request("thirdparties?sqlfilters=" . urlencode("ref_int=" . $organizationId))[0];
     }
 
     /**
@@ -33,7 +34,11 @@ class DolibarrService
      * @return array
      */
     public function getBills(int $socId): array {
-        return $this->request("invoices?sortfield=t.date&sortorder=DESC&limit=5&thirdparty_ids=" . $socId);
+        try {
+            return $this->request("invoices?sortfield=datef&sortorder=DESC&limit=5&thirdparty_ids=" . $socId);
+        } catch (NotFoundHttpException) {
+            return [];
+        }
     }
 
     /**
@@ -62,21 +67,20 @@ class DolibarrService
      * @return array
      * @throws NotFoundHttpException
      */
-    private function request(string $path, string $method = 'GET', string | null $body = null): array
+    private function request(string $path, string $method = 'GET', string $body = ''): array
     {
         try {
             $options = [
                 'headers' => [
                     'Accept'=> 'application/json',
-                    'DOLAPIKEY' => "Bocc4zC0J186v8J6QCqu7DnoIw4I7mCJ"
+                    'DOLAPIKEY' => 'Bocc4zC0J186v8J6QCqu7DnoIw4I7mCJ'
                 ]
             ];
 
             if ($body !== null) {
                 $options['body'] = $body;
             }
-
-            $uri = 'api/index.php/' . ltrim($path, '/');
+            $uri = rtrim(self::BASE_URI, '/') . '/' . ltrim($path, '/');
             $response = $this->client->request($method, $uri, $options);
             return json_decode($response->getContent(), true);
         } catch (HttpExceptionInterface | TransportExceptionInterface $e) {