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

add dolibarr services

Olivier Massot 4 éve
szülő
commit
4c376f2789

+ 1 - 2
composer.json

@@ -41,8 +41,7 @@
         "symfony/validator": "5.3.*",
         "symfony/yaml": "5.3.*",
         "vincent/foselastica": "1.1",
-        "webonyx/graphql-php": "^14.3",
-        "guzzlehttp/guzzle": "^7.0"
+        "webonyx/graphql-php": "^14.3"
     },
     "require-dev": {
         "cyclonedx/cyclonedx-php-composer": "^3.4",

+ 3 - 3
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "8f86ef67e9551dacd215fa20b6801858",
+    "content-hash": "a5c7e49963eecc92446ad58b8f5a4754",
     "packages": [
         {
             "name": "api-platform/core",
@@ -8123,10 +8123,10 @@
     "prefer-stable": false,
     "prefer-lowest": false,
     "platform": {
-        "php": ">=7.2.5",
+        "php": ">=8.0",
         "ext-ctype": "*",
         "ext-iconv": "*"
     },
     "platform-dev": [],
-    "plugin-api-version": "2.0.0"
+    "plugin-api-version": "2.1.0"
 }

+ 180 - 0
src/ApiResources/Dolibarr/DolibarrAccount.php

@@ -0,0 +1,180 @@
+<?php
+declare(strict_types=1);
+
+namespace App\ApiResources\Dolibarr;
+
+use ApiPlatform\Core\Annotation\ApiProperty;
+use ApiPlatform\Core\Annotation\ApiResource;
+
+/**
+ * Données de l'organization retournées par l'API Dolibarr
+ *
+ * @ApiResource(
+ *     compositeIdentifier=false,
+ *     itemOperations={
+ *         "get"={
+ *             "method"="GET",
+ *             "path"="/dolibarr/account/{organizationId}"
+ *          }
+ *     }
+ * )
+ */
+class DolibarrAccount
+{
+    /**
+     * @var int
+     * @ApiProperty(identifier=true)
+     */
+    private int $organizationId;
+    /**
+     * Dolibarr societies pk
+     * @var int
+     */
+    private int $socId;
+    /**
+     * Opentalent client ref
+     * @var string
+     */
+    private string $clientNumber;
+    /**
+     * Opentalent product owned
+     * @var string
+     */
+    private string $product;
+    /**
+     * Services currently actives
+     * @var array
+     */
+    private array $services;
+    /**
+     * Sms credit remaining
+     * @var string
+     */
+    private string $smsCredit;
+    /**
+     * Last bills
+     * @var array
+     */
+    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;
+    }
+
+}

+ 130 - 0
src/ApiResources/Dolibarr/DolibarrBill.php

@@ -0,0 +1,130 @@
+<?php
+declare(strict_types=1);
+
+namespace App\ApiResources\Dolibarr;
+
+use ApiPlatform\Core\Annotation\ApiProperty;
+use ApiPlatform\Core\Annotation\ApiResource;
+
+/**
+ * Bill of a society, retrieved from dolibarr
+ *
+ * @ApiResource(
+ *     compositeIdentifier=false,
+ *     collectionOperations={
+ *         "get"={
+ *             "method"="GET",
+ *             "path"="/dolibarr/bills/{socId}"
+ *          }
+ *     }
+ * )
+ */
+class DolibarrBill
+{
+    /**
+     * @var string
+     * @ApiProperty(identifier=true)
+     */
+    private string $billId;
+    /**
+     * Id of the society
+     * @var int
+     */
+    private int $socId;
+    /**
+     * Date of the bill
+     * @var int
+     */
+    private int $date;
+    /**
+     * Amount incl VAT
+     * @var float
+     */
+    private float $amount;
+    /**
+     * Is the bill paid or not
+     * @var bool
+     */
+    private bool $paid;
+
+    /**
+     * @return string
+     */
+    public function getBillId(): string
+    {
+        return $this->billId;
+    }
+
+    /**
+     * @param string $billId
+     */
+    public function setBillId(string $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;
+    }
+
+}

+ 57 - 0
src/DataProvider/Dolibarr/DolibarrAccountDataProvider.php

@@ -0,0 +1,57 @@
+<?php
+declare(strict_types=1);
+
+namespace App\DataProvider\Dolibarr;
+
+use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
+use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
+use App\ApiResources\Dolibarr\DolibarrAccount;
+use App\ApiResources\Dolibarr\DolibarrBill;
+use App\Service\Dolibarr\DolibarrService;
+
+/**
+ * Custom provider pour les DolibarrAccounts récupérés via l'api dolibarr
+ * @package App\DataProvider\Utils
+ */
+final class DolibarrAccountDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
+{
+    private DolibarrService $dolibarrService;
+
+    public function __construct(
+        DolibarrService $dolibarrService,
+    )
+    {
+        $this->dolibarrService = $dolibarrService;
+    }
+
+    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
+    {
+        return DolibarrAccount::class === $resourceClass;
+    }
+
+    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?DolibarrAccount
+    {
+        $dolibarrAccount = new DolibarrAccount();
+
+        $accountData = $this->dolibarrService->getSociety($id);
+        $billsData = $this->dolibarrService->getBills($accountData['id']);
+
+        $dolibarrAccount->setOrganizationId($id);
+        $dolibarrAccount->setSocId($accountData['id']);
+        $dolibarrAccount->setClientNumber($accountData['code_client']);
+        $dolibarrAccount->setProduct($accountData['array_options']['options_2iopen_software_used']);
+        // $dolibarrAccount->setSmsCredit();
+        // $dolibarrAccount->setServices($accountData['code_client']);
+
+        foreach ($billsData as $billData) {
+            $bill = new DolibarrBill();
+            $bill->setBillId($billData['id']);
+            $bill->setSocId($dolibarrAccount->getSocId());
+            $bill->setAmount($billData['total_ttc']);
+            $bill->setDate($billData['date']);
+            $bill->setPaid((bool)$billData['paye']);
+            $dolibarrAccount->addBill($bill);
+        }
+        return $dolibarrAccount;
+    }
+}

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

@@ -0,0 +1,86 @@
+<?php
+
+namespace App\Service\Dolibarr;
+
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
+use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
+use Symfony\Contracts\HttpClient\HttpClientInterface;
+
+class DolibarrService
+{
+    private HttpClientInterface $client;
+
+    function __construct(HttpClientInterface $client)
+    {
+        $this->client = $client;
+    }
+
+    /**
+     * Get a dolibarr society by its opentalent organization id
+     *
+     * @param int $organizationId
+     * @return array
+     */
+    public function getSociety(int $organizationId): array {
+        return $this->request("api/index.php/thirdparties?sqlfilters=ref_int%3D" . $organizationId);
+    }
+
+    /**
+     * Get a society bills by their society id
+     *
+     * @param int $socId
+     * @return array
+     */
+    public function getBills(int $socId): array {
+        return $this->request("invoices?sortfield=t.date&sortorder=DESC&limit=5&thirdparty_ids=" . $socId);
+    }
+
+    /**
+     *
+     * @param $organization
+     * @return array
+     */
+    public function createSociety($organization): array
+    {
+        $body = sprintf(
+            '{"name":"%s","client":"2","code_client":"-1","ref_int":"%s","import_key":"crm"}',
+            $organization->getName(),
+            $organization->getId()
+        );
+
+        return $this->request("api/index.php/thirdparties",'POST', $body);
+    }
+
+    /**
+     * Send an HTTP request to the Dolibarr API,
+     * and return the decoded content of the response's body
+     *
+     * @param string $path
+     * @param string $method
+     * @param string|null $body
+     * @return array
+     * @throws NotFoundHttpException
+     */
+    private function request(string $path, string $method = 'GET', string | null $body = null): array
+    {
+        try {
+            $options = [
+                'headers' => [
+                    'Accept'=> 'application/json',
+                    'DOLAPIKEY' => "Bocc4zC0J186v8J6QCqu7DnoIw4I7mCJ"
+                ]
+            ];
+
+            if ($body !== null) {
+                $options['body'] = $body;
+            }
+
+            $uri = 'api/index.php/' . ltrim($path, '/');
+            $response = $this->client->request($method, $uri, $options);
+            return json_decode($response->getContent(), true);
+        } catch (HttpExceptionInterface | TransportExceptionInterface $e) {
+            throw new NotFoundHttpException('data_not_found', $e, 404);
+        }
+    }
+}

+ 2 - 2
src/Service/Utils/GpsCoordinateUtils.php

@@ -24,7 +24,7 @@ class GpsCoordinateUtils
     }
 
     /**
-     * Renvoi un tableau d'adresse existante correspondant à la recherche d'adresse demandée
+     * Renvoie un tableau d'adresse existante correspondant à la recherche d'adresse demandée
      * @param string|null $street
      * @param string|null $cp
      * @param string|null $city
@@ -99,4 +99,4 @@ class GpsCoordinateUtils
         $addressTransformed['cp'] = key_exists('postcode', $address) ? $address['postcode'] : null;
         return $addressTransformed;
     }
-}
+}