Parcourir la source

implements dolibarr tags synchronization

Olivier Massot il y a 1 an
Parent
commit
1b67b8a095

+ 2 - 0
src/Enum/Organization/LegalEnum.php

@@ -7,6 +7,7 @@ use MyCLabs\Enum\Enum;
 
 /**
  * Statut légal
+ * @method static LOCAL_AUTHORITY()
  * @method static COMMERCIAL_SOCIETY()
  * @method static ASSOCIATION_LAW_1901()
  */
@@ -15,4 +16,5 @@ class LegalEnum extends Enum
     private const LOCAL_AUTHORITY = 'LOCAL_AUTHORITY';
     private const ASSOCIATION_LAW_1901 = 'ASSOCIATION_LAW_1901';
     private const COMMERCIAL_SOCIETY = 'COMMERCIAL_SOCIETY';
+
 }

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

@@ -145,4 +145,26 @@ class DolibarrApiService extends ApiRequestService
             throw $e;
         }
     }
+
+    /**
+     * Get the society tags
+     *
+     * @param int $socId The society ID
+     * @return array<int> The array of tags associated with the society
+     * @throws HttpException|\JsonException if an HTTP error occurs
+     */
+    public function getSocietyTagsIds(int $socId): array {
+        try {
+            return array_map(
+                function ($x) { return (int)$x['id']; },
+                $this->getJsonContent("/thirdparties/$socId/categories")
+            );
+        } catch (HttpException $e) {
+            if ($e->getStatusCode() === 404) {
+                // /!\ The dolibarr API will return a 404 error if no results are found...
+                return [];
+            }
+            throw $e;
+        }
+    }
 }

+ 124 - 5
src/Service/Dolibarr/DolibarrSyncService.php

@@ -12,6 +12,7 @@ use App\Enum\Access\RoleEnum;
 use App\Enum\Core\ContactPointTypeEnum;
 use App\Enum\Network\NetworkEnum;
 use App\Enum\Organization\AddressPostalOrganizationTypeEnum;
+use App\Enum\Organization\LegalEnum;
 use App\Enum\Organization\OrganizationIdsEnum;
 use App\Enum\Organization\SettingsProductEnum;
 use App\Enum\Person\GenderEnum;
@@ -21,6 +22,7 @@ use App\Repository\Organization\OrganizationRepository;
 use App\Service\Core\AddressPostalUtils;
 use App\Service\Rest\Operation\BaseRestOperation;
 use App\Service\Rest\Operation\CreateOperation;
+use App\Service\Rest\Operation\DeleteOperation;
 use App\Service\Rest\Operation\UpdateOperation;
 use App\Service\Utils\ArrayUtils;
 use Exception;
@@ -47,6 +49,22 @@ use Symfony\Contracts\Translation\TranslatorInterface;
  */
 class DolibarrSyncService
 {
+    private const ASSOCIATION_SCHOOL_TAG_ID = 67;
+    private const ASSOCIATION_PREMIUM_TAG_ID = 69;
+    private const LOCAL_AUTH_SCHOOL_TAG_ID = 68;
+    private const LOCAL_AUTH_PREMIUM_TAG_ID = 70;
+
+    private const SYNCHRONIZED_TAGS = [
+        self::ASSOCIATION_SCHOOL_TAG_ID => 'Association School',
+        self::ASSOCIATION_PREMIUM_TAG_ID => 'Association School Premium',
+        self::LOCAL_AUTH_SCHOOL_TAG_ID => 'CT School',
+        self::LOCAL_AUTH_PREMIUM_TAG_ID => 'CT School Premium'
+    ];
+
+    private const ASSO1901_JURIDIC_FORM_ID = 92;
+    private const ASSO_JURIDIC_FORM_ID = 608;
+    private const LOCAL_AUTH_JURIDIC_FORM_ID = 72;
+
     private LoggerInterface $logger;
 
     public function __construct(
@@ -104,6 +122,7 @@ class DolibarrSyncService
         $i = 0; $total = count($dolibarrClientsIndex);
         foreach ($dolibarrClientsIndex as $organizationId => $dolibarrSociety) {
             $dolibarrSociety = $this->sanitizeDolibarrData($dolibarrSociety);
+            $dolibarrSocietyId = (int)$dolibarrSociety['id'];
 
             $organization = $this->organizationRepository->find($organizationId);
             if ($organization === null) {
@@ -193,14 +212,14 @@ class DolibarrSyncService
                 $operations[] = new UpdateOperation(
                     'Update society : ' . $organization->getName() . ' (' . $organization->getId() . ')',
                     'thirdparties',
-                    (int)$dolibarrSociety['id'],
+                    $dolibarrSocietyId,
                     $newSocietyData,
                     $dolibarrSociety
                 );
             }
 
             // ===== Update Contacts =====
-            $dolibarrSocietyContacts = $this->dolibarrApiService->getContacts((int)$dolibarrSociety['id']);
+            $dolibarrSocietyContacts = $this->dolibarrApiService->getContacts($dolibarrSocietyId);
             $contactsProcessed = [];
 
             foreach ($organizationMembers as $accessId => $missions) {
@@ -249,7 +268,7 @@ class DolibarrSyncService
 
                 if ($dolibarrContact === null) {
                     // New contact
-                    $newContactData['socid'] = (int)$dolibarrSociety['id'];
+                    $newContactData['socid'] = $dolibarrSocietyId;
 
                     $operations[] = new CreateOperation(
                         'New contact: ' . $person->getName() . ' ' . $person->getGivenName() . ' (' . $person->getId() . ')',
@@ -271,7 +290,7 @@ class DolibarrSyncService
                             'Update contact: ' . $person->getName() . ' ' . $person->getGivenName() . ' (' . $person->getId() . ')' .
                             ' in ' . $organization->getName() . ' (' . $organization->getId() . ')',
                             'contacts',
-                            (int)$dolibarrContact['id'],
+                            $dolibarrSocietyId,
                             $newContactData,
                             $dolibarrContact
                         );
@@ -294,13 +313,63 @@ class DolibarrSyncService
                         'Disable contact: ' . $contactData['lastname'] . ' ' . $contactData['firstname'] . ' (' . $personId . ')' .
                         ' from  ' . $organization->getName() . ' (' . $organization->getId() . ')',
                         'contacts',
-                        (int)$contactData['id'],
+                        $dolibarrSocietyId,
                         ['statut' => '0'],
                         $contactData
                     );
                 }
             }
 
+            // ===== Update Tags =====
+            $currentTags = $this->dolibarrApiService->getSocietyTagsIds($dolibarrSocietyId);
+            $expectedTags = [];
+
+            // Association and local auth, with school or school premium products
+            if ($organization->getLegalStatus() === LegalEnum::ASSOCIATION_LAW_1901()->getValue()) {
+                if ($product === SettingsProductEnum::SCHOOL()->getValue()) {
+                    $expectedTags[] = self::ASSOCIATION_SCHOOL_TAG_ID;
+                }
+                if ($product === SettingsProductEnum::SCHOOL_PREMIUM()->getValue()) {
+                    $expectedTags[] = self::ASSOCIATION_PREMIUM_TAG_ID;
+                }
+            }
+            if ($organization->getLegalStatus() === LegalEnum::LOCAL_AUTHORITY()->getValue()) {
+                if ($product === SettingsProductEnum::SCHOOL()->getValue()) {
+                    $expectedTags[] = self::LOCAL_AUTH_SCHOOL_TAG_ID;
+                }
+                if ($product === SettingsProductEnum::SCHOOL_PREMIUM()->getValue()) {
+                    $expectedTags[] = self::LOCAL_AUTH_PREMIUM_TAG_ID;
+                }
+            }
+
+            // Remove unexpected tags
+            foreach ($currentTags as $tagId) {
+                if (!array_key_exists($tagId, self::SYNCHRONIZED_TAGS)) {
+                    continue;
+                }
+
+                if (!in_array($tagId, $expectedTags)) {
+                    $operations[] = new DeleteOperation(
+                        "Delete tag: `" . self::SYNCHRONIZED_TAGS[$tagId] .
+                        '` from ' . $organization->getName() . ' (' . $organization->getId() . ')',
+                        "/thirdparties/$dolibarrSocietyId/categories",
+                        $tagId
+                    );
+                }
+            }
+
+            // Add missing tags
+            foreach ($expectedTags as $tagId) {
+                if (!in_array($tagId, $currentTags)) {
+                    $operations[] = new CreateOperation(
+                        "Add tag: `" . self::SYNCHRONIZED_TAGS[$tagId] .
+                        '` to ' . $organization->getName() . ' (' . $organization->getId() . ')',
+                        "/thirdparties/$dolibarrSocietyId/categories/$tagId",
+                        []
+                    );
+                }
+            }
+
             // Next society
             $i++;
             if ($progressionCallback !== null) {
@@ -380,6 +449,10 @@ class DolibarrSyncService
         }
 
         $this->logger->info('Execution ended');
+
+//        $this->logger->info('Post-script updates');
+//        $this->postscript();
+
         $this->logger->info('Done : ' . $done);
         $this->logger->info('Errors : ' . $errors);
         if ($unknown > 0) {
@@ -763,4 +836,50 @@ class DolibarrSyncService
             );
         }
     }
+
+//    protected function postscript(): void
+//    {
+//        $this->updateTags();
+//    }
+//
+//    protected function updateTags(): void
+//    {
+//        $ASSOCIATION_SCHOOL_TAG_ID = 67;
+//        $ASSOCIATION_PREMIUM_TAG_ID = 69;
+//        $LOCAL_AUTH_SCHOOL_TAG_ID = 68;
+//        $LOCAL_AUTH_PREMIUM_TAG_ID = 70;
+//
+//
+//
+//        $sql = "delete from llx_categorie_societe where fk_categorie=$ASSOCIATION_SCHOOL_TAG_ID;";
+//        $sql = "delete from llx_categorie_societe where fk_categorie=$LOCAL_AUTH_SCHOOL_TAG_ID;";
+//        $sql = "delete from llx_categorie_societe where fk_categorie=$ASSOCIATION_PREMIUM_TAG_ID;";
+//        $sql = "delete from llx_categorie_societe where fk_categorie=$LOCAL_AUTH_PREMIUM_TAG_ID;";
+//
+//        $sql = "insert into llx_categorie_societe (fk_categorie, fk_soc)
+//                select $ASSOCIATION_SCHOOL_TAG_ID, s.rowid
+//                from doliprod.llx_societe s
+//                left join llx_societe_extrafields e on s.rowid = e.fk_object
+//                where s.fk_forme_juridique in (92, 608) and s.client=1 and e.`2iopen_software_opentalent` = 'Opentalent School';";
+//
+//        $sql = "insert into llx_categorie_societe (fk_categorie, fk_soc)
+//                select $ASSOCIATION_PREMIUM_TAG_ID, s.rowid
+//                from doliprod.llx_societe s
+//                left join llx_societe_extrafields e on s.rowid = e.fk_object
+//                where s.fk_forme_juridique in (92, 608) and s.client=1 and e.`2iopen_software_opentalent` = 'Opentalent School Premium';";
+//
+//        $sql = "insert into llx_categorie_societe (fk_categorie, fk_soc)
+//                select $LOCAL_AUTH_SCHOOL_TAG_ID, s.rowid
+//                from doliprod.llx_societe s
+//                left join llx_societe_extrafields e on s.rowid = e.fk_object
+//                where s.fk_forme_juridique = 72 and s.client=1 and e.`2iopen_software_opentalent` = 'Opentalent School';";
+//
+//        $sql = "insert into llx_categorie_societe (fk_categorie, fk_soc)
+//                select $LOCAL_AUTH_PREMIUM_TAG_ID, s.rowid
+//                from doliprod.llx_societe s
+//                left join llx_societe_extrafields e on s.rowid = e.fk_object
+//                where s.fk_forme_juridique = 72 and s.client=1 and e.`2iopen_software_opentalent` = 'Opentalent School Premium';";
+//
+//    }
+
 }