瀏覽代碼

include last order data to dolibarr account resource

Olivier Massot 8 月之前
父節點
當前提交
781206ce5f

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

@@ -63,6 +63,13 @@ class DolibarrAccount implements ApiResourcesInterface
     #[Groups('dolibarr_get')]
     private ?DolibarrContract $contract = null;
 
+    /**
+     * Last order of the society.
+     * @var DolibarrOrder|null
+     */
+    #[Groups('dolibarr_get')]
+    private ?DolibarrOrder $order = null;
+
     /**
      * Last bills.
      */
@@ -135,6 +142,17 @@ class DolibarrAccount implements ApiResourcesInterface
         return $this;
     }
 
+    public function getOrder(): ?DolibarrOrder
+    {
+        return $this->order;
+    }
+
+    public function setOrder(?DolibarrOrder $order): self
+    {
+        $this->order = $order;
+        return $this;
+    }
+
     public function getBills(): Collection
     {
         return $this->bills;

+ 77 - 0
src/ApiResources/Dolibarr/DolibarrOrder.php

@@ -0,0 +1,77 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\ApiResources\Dolibarr;
+
+use ApiPlatform\Metadata\ApiProperty;
+use ApiPlatform\Metadata\ApiResource;
+use ApiPlatform\Metadata\Get;
+use App\ApiResources\ApiResourcesInterface;
+use Symfony\Component\Serializer\Annotation\Groups;
+
+/**
+ * Order of a society, retrieved from dolibarr.
+ */
+#[ApiResource(
+    operations: [
+        new Get(),
+    ]
+)]
+class DolibarrOrder implements ApiResourcesInterface
+{
+    /**
+     * Id of the dolibarr bill ( = invoice).
+     */
+    #[ApiProperty(identifier: true)]
+    #[Groups('dolibarr_get')]
+    private int $id;
+
+    /**
+     * Bill reference.
+     */
+    #[Groups('dolibarr_get')]
+    private string $ref;
+
+    /**
+     * Date of the bill.
+     */
+    #[Groups('dolibarr_get')]
+    private \DateTime $date;
+
+    public function getId(): int
+    {
+        return $this->id;
+    }
+
+    public function setId(int $id): self
+    {
+        $this->id = $id;
+
+        return $this;
+    }
+
+    public function getRef(): string
+    {
+        return $this->ref;
+    }
+
+    public function setRef(string $ref): self
+    {
+        $this->ref = $ref;
+
+        return $this;
+    }
+
+    public function getDate(): \DateTime
+    {
+        return $this->date;
+    }
+
+    public function setDate(\DateTime $date): self
+    {
+        $this->date = $date;
+
+        return $this;
+    }
+}

+ 22 - 0
src/Service/ApiResourceBuilder/Dolibarr/DolibarrAccountBuilder.php

@@ -8,6 +8,7 @@ use App\ApiResources\Dolibarr\DolibarrAccount;
 use App\ApiResources\Dolibarr\DolibarrBill;
 use App\ApiResources\Dolibarr\DolibarrContract;
 use App\ApiResources\Dolibarr\DolibarrContractLine;
+use App\ApiResources\Dolibarr\DolibarrOrder;
 use App\Service\Dolibarr\DolibarrApiService;
 
 class DolibarrAccountBuilder
@@ -44,6 +45,13 @@ class DolibarrAccountBuilder
             $dolibarrAccount->setContract($contract);
         }
 
+        // get last order (if any)
+        $lastOrderData = $this->dolibarrApiService->getLastOrder($dolibarrAccount->getSocId());
+        if ($lastOrderData !== null) {
+            $order = $this->createDolibarrOrder($lastOrderData);
+            $dolibarrAccount->setOrder($order);
+        }
+
         // get bills
         $billsData = $this->dolibarrApiService->getBills($dolibarrAccount->getSocId());
         foreach ($billsData as $billData) {
@@ -113,6 +121,20 @@ class DolibarrAccountBuilder
                     ->setDateEnd(new \DateTime(date('c', (int) $lineData['date_end'])));
     }
 
+    /**
+     * @param array<mixed> $orderData
+     *
+     * @throws \Exception
+     */
+    public function createDolibarrOrder(array $orderData): DolibarrOrder
+    {
+        $order = new DolibarrOrder();
+
+        return $order->setId((int) $orderData['id'])
+                    ->setRef($orderData['ref'])
+                    ->setDate(new \DateTime(date('c', (int) $orderData['date_validation'])));
+    }
+
     /**
      * @param array<mixed> $billData
      *

+ 24 - 0
src/Service/Dolibarr/DolibarrApiService.php

@@ -96,6 +96,30 @@ class DolibarrApiService extends ApiRequestService
         }
     }
 
+    /**
+     * 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.
      *