|
@@ -1,8 +1,13 @@
|
|
|
<?php
|
|
<?php
|
|
|
|
|
|
|
|
-namespace App\Service\Dolibarr;
|
|
|
|
|
|
|
+namespace App\Service\Dolibarr\DolibarrSync;
|
|
|
|
|
|
|
|
|
|
+use App\Entity\Core\AddressPostal;
|
|
|
|
|
+use App\Entity\Organization\OrganizationAddressPostal;
|
|
|
use App\Repository\Organization\OrganizationRepository;
|
|
use App\Repository\Organization\OrganizationRepository;
|
|
|
|
|
+use App\Service\Dolibarr\DolibarrApiService;
|
|
|
|
|
+use Exception;
|
|
|
|
|
+use HttpException;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Push the data from the Opentalent DB into the Dolibarr DB, trough both applications
|
|
* Push the data from the Opentalent DB into the Dolibarr DB, trough both applications
|
|
@@ -13,18 +18,147 @@ use App\Repository\Organization\OrganizationRepository;
|
|
|
class DolibarrSyncService
|
|
class DolibarrSyncService
|
|
|
{
|
|
{
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
- private OrganizationRepository $organizationRepository
|
|
|
|
|
|
|
+ private OrganizationRepository $organizationRepository,
|
|
|
|
|
+ private DolibarrApiService $dolibarrApiService
|
|
|
) {}
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Performs a scan, comparing data from the Opentalent DB and the data returned
|
|
* Performs a scan, comparing data from the Opentalent DB and the data returned
|
|
|
* by the Dolibarr API
|
|
* by the Dolibarr API
|
|
|
*
|
|
*
|
|
|
- * Returns an array of DolibarrSyncOperation
|
|
|
|
|
|
|
+ * Errors during the scan are recorded in the $this->scanErrors
|
|
|
|
|
+ *
|
|
|
|
|
+ * Returns an array of DolibarrSyncOperations
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return array<DolibarrSyncOperation>
|
|
|
|
|
+ * @throws HttpException
|
|
|
*/
|
|
*/
|
|
|
- public function scan() {
|
|
|
|
|
- foreach ($this->organizationRepository->getAll() as $organization) {
|
|
|
|
|
|
|
+ public function scan(): array {
|
|
|
|
|
+
|
|
|
|
|
+ // Index the dolibarr clients by organization ids
|
|
|
|
|
+ $dolibarrClientsIndex = [];
|
|
|
|
|
+ foreach ($this->dolibarrApiService->getAllClients() as $clientData) {
|
|
|
|
|
+ if (!$clientData["array_options"]["2iopen_organization_id"] > 0) {
|
|
|
|
|
+ $this->logScanError(
|
|
|
|
|
+ "Missing organization id : " . $clientData["name"] . "(" . $clientData["code_client"] .")"
|
|
|
|
|
+ );
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $dolibarrClientsIndex[$clientData["organization_id"]] = $clientData;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Index the dolibarr contacts by person ids
|
|
|
|
|
+ $dolibarrContactsIndex = [];
|
|
|
|
|
+ foreach ($this->dolibarrApiService->getAllOpentalentContacts() as $contactData) {
|
|
|
|
|
+ if (!$contactData["array_options"]["2iopen_person_id"] > 0) {
|
|
|
|
|
+ $this->logScanError(
|
|
|
|
|
+ "Missing person id : " . $contactData["name"] . "(" . $clientData["code_client"] .")"
|
|
|
|
|
+ );
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $dolibarrContactsIndex[$contactData["organization_id"]] = $contactData;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Loop over the Opentalent organizations, and create the operations list
|
|
|
|
|
+ $operations = [];
|
|
|
|
|
+ foreach ($this->organizationRepository->findAll() as $organization) {
|
|
|
|
|
+ if (!isset($dolibarrClientsIndex["organization_id"])) {
|
|
|
|
|
+ // this organization is not a client, probably a federation member
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $dolibarrClient = $dolibarrClientsIndex[$organization->getId()];
|
|
|
|
|
+
|
|
|
|
|
+ // ** Sync contact data of the client
|
|
|
|
|
+ // Postal address
|
|
|
|
|
+ /** @var AddressPostal | null */
|
|
|
|
|
+ $mainAddress = null;
|
|
|
|
|
+ foreach ($organization->getOrganizationAddressPostals() as $postalAddress) {
|
|
|
|
|
+ if ($postalAddress->getType() == 'PRINCIPAL') {
|
|
|
|
|
+ $mainAddress = $postalAddress->getAddress();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($mainAddress !== null) {
|
|
|
|
|
+ $params = [];
|
|
|
|
|
+
|
|
|
|
|
+ $streetAddress = implode('\n', array_filter([
|
|
|
|
|
+ trim($mainAddress->getStreetAddress()),
|
|
|
|
|
+ trim($mainAddress->getStreetAddressSecond()),
|
|
|
|
|
+ trim($mainAddress->getStreetAddressThird())
|
|
|
|
|
+ ]), function ($x) { return $x !== null and strlen($x) > 0; });
|
|
|
|
|
+
|
|
|
|
|
+ if ($streetAddress !== $dolibarrClient['address']) {
|
|
|
|
|
+ $params['address'] = $streetAddress;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($mainAddress->getPostalCode() !== $dolibarrClient['zip']) {
|
|
|
|
|
+ $params['zip'] = $mainAddress->getPostalCode();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($mainAddress->getAddressCity() !== $dolibarrClient['town']) {
|
|
|
|
|
+ $params['town'] = $mainAddress->getAddressCity();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($params) {
|
|
|
|
|
+ $operations[] = new DolibarrSyncOperation(
|
|
|
|
|
+ 'Organization ' . $organization->getId() . ': update address',
|
|
|
|
|
+ 'PUT',
|
|
|
|
|
+ 'thirdparties/' . $dolibarrClient['id'],
|
|
|
|
|
+ $params
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ return $operations;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Execute the operations listed with the DolibarrSyncService::scan method
|
|
|
|
|
+ *
|
|
|
|
|
+ * Returns an array of DolibarrSyncOperations
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param array<DolibarrSyncOperation> $operations
|
|
|
|
|
+ * @return array<DolibarrSyncOperation>
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ public function execute(array $operations): array
|
|
|
|
|
+ {
|
|
|
|
|
+ foreach ($operations as $operation) {
|
|
|
|
|
+ if ($operation->getStatus() !== DolibarrSyncOperation::STATUS_READY) {
|
|
|
|
|
+ // operation has already been treated
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ $operation->execute();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ return $operations;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Scan and execute the sync process
|
|
|
|
|
+ *
|
|
|
|
|
+ * @throws HttpException
|
|
|
|
|
+ * @return array<DolibarrSyncOperation>
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ public function run(): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $operations = $this->scan();
|
|
|
|
|
+
|
|
|
|
|
+ $this->execute($operations);
|
|
|
|
|
+
|
|
|
|
|
+ return $operations;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Log a new scanning error
|
|
|
|
|
+ * @param $errorMsg
|
|
|
|
|
+ */
|
|
|
|
|
+ private function logScanError($errorMsg) {
|
|
|
|
|
+ // TODO : implement
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|