Browse Source

add billing contact to dolibarr sync

Olivier Massot 1 year ago
parent
commit
a37af55d98
1 changed files with 68 additions and 0 deletions
  1. 68 0
      src/Service/Dolibarr/DolibarrSyncService.php

+ 68 - 0
src/Service/Dolibarr/DolibarrSyncService.php

@@ -303,6 +303,7 @@ class DolibarrSyncService
                 }
             }
 
+            // Disable non existing contacts
             foreach ($dolibarrSocietyContacts as $contactData) {
                 if (empty($contactData['array_options']['options_2iopen_person_id'])) {
                     continue;
@@ -325,6 +326,48 @@ class DolibarrSyncService
                 }
             }
 
+            // Update Billing service contact (if existing)
+            foreach ($dolibarrSocietyContacts as $contactData) {
+                if (
+                    strtolower($contactData['lastname']) === 'service facturation' &&
+                    empty($contactData['name']) &&
+                    empty($contactData['array_options']['options_2iopen_person_id'])
+                ) {
+                    $billingAddress = $this->getOrganizationBillingPostalAddress($organization);
+                    $newContactData = $contactData;
+
+                    if ($billingAddress) {
+                        $newContactData['address'] = $this->addressPostalUtils->getFullStreetAddress($billingAddress);
+                        $newContactData['zip'] = $mainAddress->getPostalCode();
+                        $newContactData['town'] = $mainAddress->getAddressCity();
+                    } else {
+                        $newContactData['address'] = null;
+                        $newContactData['zip'] = null;
+                        $newContactData['town'] = null;
+                    }
+
+                    // Only update the fields that are different (it's important to let it non-recursive, the subarray have to be passed entirely)
+                    $newContactData = $this->arrayUtils->getChanges(
+                        $contactData,
+                        $newContactData,
+                        false,
+                        static function ($v1, $v2) { return ($v1 ?? '') === ($v2 ?? ''); }
+                    );
+
+                    // add an update operation if some data has to be updated
+                    if (!empty($newContactData)) {
+                        $operations[] = new UpdateOperation(
+                            'Update contact: Service facturation'.
+                            ' in '.$organization->getName().' ('.$organization->getId().')',
+                            'contacts',
+                            (int) $contactData['id'],
+                            $newContactData,
+                            $contactData
+                        );
+                    }
+                }
+            }
+
             // ===== Update Tags =====
             $currentTags = $this->dolibarrApiService->getSocietyTagsIds($dolibarrSocietyId);
             $expectedTags = $this->getExpectedTagsFor($organization);
@@ -616,6 +659,31 @@ class DolibarrSyncService
         return null;
     }
 
+    /**
+     * Retrieve the postal address of the organization.
+     */
+    protected function getOrganizationBillingPostalAddress(Organization $organization): ?AddressPostal
+    {
+        $addressPriorities = [
+            AddressPostalOrganizationTypeEnum::ADDRESS_BILL,
+            AddressPostalOrganizationTypeEnum::ADDRESS_CONTACT,
+            AddressPostalOrganizationTypeEnum::ADDRESS_HEAD_OFFICE,
+            AddressPostalOrganizationTypeEnum::ADDRESS_OTHER,
+        ];
+
+        $organizationAddressPostal = $organization->getOrganizationAddressPostals();
+
+        foreach ($addressPriorities as $addressType) {
+            foreach ($organizationAddressPostal as $postalAddress) {
+                if ($postalAddress->getType() === $addressType) {
+                    return $postalAddress->getAddressPostal();
+                }
+            }
+        }
+
+        return null;
+    }
+
     /**
      * Retrieve the phone for the organization.
      */