Forráskód Böngészése

completes the dolibarr/account/* route

Olivier Massot 4 éve
szülő
commit
81c8c016cf

+ 5 - 1
.env

@@ -41,4 +41,8 @@ JWT_PASSPHRASE=opentalent
 
 ###> opentalent config folder ###
 OPENTALENT_CONFIG=/config/opentalent
-###< opentalent config folder ###
+###< opentalent config folder ###
+
+###> dolibarr client ###
+DOLIBARR_BASE_URI='https://prod-erp.2iopenservice.com/api/index.php/'
+###< dolibarr client ###

+ 2 - 0
config/packages/framework.yaml

@@ -22,3 +22,5 @@ framework:
                 base_uri: 'https://entreprise.data.gouv.fr/api/sirene/v3/etablissements/'
             openstreetmap:
                 base_uri: 'https://nominatim.openstreetmap.org/'
+            dolibarr_client:
+                base_uri: '%env(DOLIBARR_BASE_URI)%'

+ 7 - 6
src/ApiResources/Dolibarr/DolibarrAccount.php

@@ -9,6 +9,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
 
 /**
  * Données de l'organization retournées par l'API Dolibarr
+ * (aussi nommé 'ThirdParty' ou 'Society' dans dolibarr)
  */
 #[ApiResource(
     itemOperations: [
@@ -48,10 +49,10 @@ class DolibarrAccount
     private string $product = "";
 
     /**
-     * Services currently actives
+     * Contract and services currently active
      */
     #[Groups('dolibarr_get')]
-    private array $services = [];
+    private ?object $contract = null;
 
     /**
      * Sms credit remaining
@@ -105,14 +106,14 @@ class DolibarrAccount
         $this->product = $product;
     }
 
-    public function getServices(): array
+    public function getContract(): ?object
     {
-        return $this->services;
+        return $this->contract;
     }
 
-    public function setServices(array $services): void
+    public function setContract(?object $contract): void
     {
-        $this->services = $services;
+        $this->contract = $contract;
     }
 
     public function getSmsCredit(): string

+ 42 - 14
src/ApiResources/Dolibarr/DolibarrBill.php

@@ -29,7 +29,7 @@ class DolibarrBill
      */
     #[ApiProperty(identifier: true)]
     #[Groups('dolibarr_get')]
-    private int $billId;
+    private int $id;
 
     /**
      * Id of the society
@@ -41,13 +41,19 @@ class DolibarrBill
      * Date of the bill
      */
     #[Groups('dolibarr_get')]
-    private int $date;
+    private \DateTime $date;
 
     /**
-     * Amount incl VAT
+     * Amount (tax excluded)
      */
     #[Groups('dolibarr_get')]
-    private float $amount;
+    private float $taxExcludedAmount;
+
+    /**
+     * Amount (tax included)
+     */
+    #[Groups('dolibarr_get')]
+    private float $taxIncludedAmount;
 
     /**
      * Is the bill paid or not
@@ -55,14 +61,14 @@ class DolibarrBill
     #[Groups('dolibarr_get')]
     private bool $paid;
 
-    public function getBillId(): int
+    public function getId(): int
     {
-        return $this->billId;
+        return $this->id;
     }
 
-    public function setBillId(int $billId): void
+    public function setId(int $id): void
     {
-        $this->billId = $billId;
+        $this->id = $id;
     }
 
     public function getSocId(): int
@@ -75,24 +81,46 @@ class DolibarrBill
         $this->socId = $socId;
     }
 
-    public function getDate(): int
+    public function getDate(): \DateTime
     {
         return $this->date;
     }
 
-    public function setDate(int $date): void
+    public function setDate(\DateTime $date): void
     {
         $this->date = $date;
     }
 
-    public function getAmount(): float
+    /**
+     * @return float
+     */
+    public function getTaxExcludedAmount(): float
+    {
+        return $this->taxExcludedAmount;
+    }
+
+    /**
+     * @param float $taxExcludedAmount
+     */
+    public function setTaxExcludedAmount(float $taxExcludedAmount): void
     {
-        return $this->amount;
+        $this->taxExcludedAmount = $taxExcludedAmount;
     }
 
-    public function setAmount(float $amount): void
+    /**
+     * @return float
+     */
+    public function getTaxIncludedAmount(): float
+    {
+        return $this->taxIncludedAmount;
+    }
+
+    /**
+     * @param float $taxIncludedAmount
+     */
+    public function setTaxIncludedAmount(float $taxIncludedAmount): void
     {
-        $this->amount = $amount;
+        $this->taxIncludedAmount = $taxIncludedAmount;
     }
 
     public function getPaid(): bool

+ 80 - 0
src/ApiResources/Dolibarr/DolibarrContract.php

@@ -0,0 +1,80 @@
+<?php
+declare(strict_types=1);
+
+namespace App\ApiResources\Dolibarr;
+
+use ApiPlatform\Core\Annotation\ApiProperty;
+use ApiPlatform\Core\Annotation\ApiResource;
+use Symfony\Component\Serializer\Annotation\Groups;
+
+/**
+ * Contract of a society, retrieved from dolibarr
+ */
+#[ApiResource(
+    itemOperations: [
+        'get' => [
+            'method' => 'GET',
+            'path' => '/dolibarr/contract/{ref}',
+            'requirements' => ['socId' => '\d+'],
+            'normalization_context' => [
+                'groups' => ['dolibarr_get']
+            ]
+        ],
+    ]
+)]
+class DolibarrContract
+{
+    /**
+     * Reference of the dolibarr contract
+     */
+    #[ApiProperty(identifier: true)]
+    #[Groups('dolibarr_get')]
+    private string $ref;
+
+    /**
+     * Id of the society
+     */
+    #[Groups('dolibarr_get')]
+    private int $socId;
+
+    /**
+     * Lines (services) included in the current contract
+     */
+    #[Groups('dolibarr_get')]
+    private array $lines = [];
+
+    public function getRef(): string
+    {
+        return $this->ref;
+    }
+
+    public function setRef(string $ref): void
+    {
+        $this->ref = $ref;
+    }
+
+    public function getSocId(): int
+    {
+        return $this->socId;
+    }
+
+    public function setSocId(int $socId): void
+    {
+        $this->socId = $socId;
+    }
+
+    public function getLines(): array
+    {
+        return $this->lines;
+    }
+
+    public function setLines(array $lines): void
+    {
+        $this->lines = $lines;
+    }
+
+    public function addLine(object $line): void
+    {
+        $this->lines[] = $line;
+    }
+}

+ 120 - 0
src/ApiResources/Dolibarr/DolibarrContractLine.php

@@ -0,0 +1,120 @@
+<?php
+declare(strict_types=1);
+
+namespace App\ApiResources\Dolibarr;
+
+use ApiPlatform\Core\Annotation\ApiProperty;
+use ApiPlatform\Core\Annotation\ApiResource;
+use Symfony\Component\Serializer\Annotation\Groups;
+
+/**
+ *  Lines (services) included in a society contract, as retrieved from dolibarr
+ */
+#[ApiResource(
+    collectionOperations: [
+        'get' => [
+            'method' => 'GET',
+            'path' => '/dolibarr/contract-lines/{contractId}',
+            'requirements' => ['contractId' => '\d+'],
+            'normalization_context' => [
+                'groups' => ['dolibarr_get']
+            ]
+        ],
+    ]
+)]
+class DolibarrContractLine
+{
+    #[ApiProperty(identifier: true)]
+    #[Groups('dolibarr_get')]
+    private int $id;
+
+    /**
+     * Id of the contract's line
+     */
+    #[Groups('dolibarr_get')]
+    private int $contractId;
+
+    /**
+     * Reference of the contracted service ( = product's reference)
+     */
+    #[Groups('dolibarr_get')]
+    private string $serviceRef;
+
+    /**
+     * Label of the contracted service
+     */
+    #[Groups('dolibarr_get')]
+    private string $serviceLabel;
+
+    /**
+     * Service active from date ...
+     */
+    #[Groups('dolibarr_get')]
+    private \DateTime $dateStart;
+
+    /**
+     * Service active to date ...
+     */
+    #[Groups('dolibarr_get')]
+    private \DateTime $dateEnd;
+
+    public function getId(): int
+    {
+        return $this->id;
+    }
+
+    public function setId(int $id): void
+    {
+        $this->id = $id;
+    }
+
+    public function getContractId(): int
+    {
+        return $this->contractId;
+    }
+
+    public function setContractId(int $contractId): void
+    {
+        $this->contractId = $contractId;
+    }
+
+    public function getServiceRef(): string
+    {
+        return $this->serviceRef;
+    }
+
+    public function setServiceRef(string $serviceRef): void
+    {
+        $this->serviceRef = $serviceRef;
+    }
+
+    public function getServiceLabel(): string
+    {
+        return $this->serviceLabel;
+    }
+
+    public function setServiceLabel(string $serviceLabel): void
+    {
+        $this->serviceLabel = $serviceLabel;
+    }
+
+    public function getDateStart(): \DateTime
+    {
+        return $this->dateStart;
+    }
+
+    public function setDateStart(\DateTime $dateStart): void
+    {
+        $this->dateStart = $dateStart;
+    }
+
+    public function isDateEnd(): \DateTime
+    {
+        return $this->dateEnd;
+    }
+
+    public function setDateEnd(\DateTime $dateEnd): void
+    {
+        $this->dateEnd = $dateEnd;
+    }
+}

+ 43 - 6
src/DataProvider/Dolibarr/DolibarrAccountDataProvider.php

@@ -7,6 +7,8 @@ use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
 use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
 use App\ApiResources\Dolibarr\DolibarrAccount;
 use App\ApiResources\Dolibarr\DolibarrBill;
+use App\ApiResources\Dolibarr\DolibarrContract;
+use App\ApiResources\Dolibarr\DolibarrContractLine;
 use App\Service\Dolibarr\DolibarrService;
 
 /**
@@ -15,6 +17,13 @@ use App\Service\Dolibarr\DolibarrService;
  */
 final class DolibarrAccountDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
 {
+    const PRODUCT_MAPPING = [
+        1 => 'PRODUCT_ARTIST',   # OT Artist
+        2 => 'PRODUCT_ARTIST_PREMIUM',   # OT Artist Premium
+        3 => 'PRODUCT_SCHOOL',   # OT School Standard
+        4 => 'PRODUCT_SCHOOL_PREMIUM',   # OT School Premium
+        5 => 'PRODUCT_MANAGER',   # OT Manager
+    ];
     private DolibarrService $dolibarrService;
 
     public function __construct(
@@ -38,20 +47,48 @@ final class DolibarrAccountDataProvider implements ItemDataProviderInterface, Re
         $dolibarrAccount->setOrganizationId($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']);
+        if ($accountData['array_options']['options_2iopen_software_used']) {
+            $dolibarrAccount->setProduct(
+                self::PRODUCT_MAPPING[(int)$accountData['array_options']['options_2iopen_software_used']]
+            );
+        }
+
+        // Get active contract and services
+        $contractData = $this->dolibarrService->getActiveContract($dolibarrAccount->getSocId());
+        if ($contractData !== null) {
+            $contract = new DolibarrContract();
+            $contract->setRef($contractData['ref']);
+            $contract->setSocId((int)$contractData['socid']);
 
+            foreach ($contractData['lines'] as $lineData) {
+                $line = new DolibarrContractLine();
+                $line->setId((int)$lineData['id']);
+                $line->setContractId((int)$lineData['fk_contrat']);
+                $line->setServiceRef($lineData['product_ref']);
+                $line->setServiceLabel($lineData['product_label']);
+                $line->setDateStart(new \DateTime(date('c', $lineData['date_start'])));
+                $line->setDateEnd(new \DateTime(date('c', $lineData['date_end'])));
+                $contract->addLine($line);
+            }
+
+            $dolibarrAccount->setContract($contract);
+        }
+
+        // get bills
         $billsData = $this->dolibarrService->getBills($dolibarrAccount->getSocId());
         foreach ($billsData as $billData) {
             $bill = new DolibarrBill();
-            $bill->setBillId((int)$billData['id']);
+            $bill->setId((int)$billData['id']);
             $bill->setSocId($dolibarrAccount->getSocId());
-            $bill->setAmount((float)$billData['total_ttc']);
-            $bill->setDate($billData['date']);
+            $bill->setTaxExcludedAmount((float)$billData['total_ht']);
+            $bill->setTaxIncludedAmount((float)$billData['total_ttc']);
+            $bill->setDate(new \DateTime(date('c', $billData['date'])));
             $bill->setPaid((bool)$billData['paye']);
             $dolibarrAccount->addBill($bill);
         }
+
+        // $dolibarrAccount->setSmsCredit();
+
         return $dolibarrAccount;
     }
 }

+ 20 - 4
src/Service/Dolibarr/DolibarrService.php

@@ -7,14 +7,16 @@ use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
 use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 
+/**
+ * Service d'appel à l'API dolibarr
+ */
 class DolibarrService
 {
-    const BASE_URI = 'https://prod-erp.2iopenservice.com/api/index.php/';
     private HttpClientInterface $client;
 
-    function __construct(HttpClientInterface $client)
+    function __construct(HttpClientInterface $dolibarr_client)
     {
-        $this->client = $client;
+        $this->client = $dolibarr_client;
     }
 
     /**
@@ -27,6 +29,20 @@ class DolibarrService
         return $this->request("thirdparties?sqlfilters=" . urlencode("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->request("contracts?limit=1&sqlfilters=statut%3D1&thirdparty_ids%3D" . $socId)[0];
+        } catch (NotFoundHttpException) {
+            return null;
+        }
+    }
+
     /**
      * Get a society bills by their society id
      *
@@ -80,7 +96,7 @@ class DolibarrService
             if ($body !== null) {
                 $options['body'] = $body;
             }
-            $uri = rtrim(self::BASE_URI, '/') . '/' . ltrim($path, '/');
+            $uri = ltrim($path, '/');
             $response = $this->client->request($method, $uri, $options);
             return json_decode($response->getContent(), true);
         } catch (HttpExceptionInterface | TransportExceptionInterface $e) {