Commit 345d6cd4 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

UP : Ajout d'un option sur la commande de synchronisation des activités...

UP : Ajout d'un option sur la commande de synchronisation des activités permettant de baser l'affectation des personnes selon l'email trouvé dans le fichier JSON
parent 3417254b
Loading
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
        "status": 404,
        "persons": {
            "Responsable Scientifique": [
                "Albert Einstein"
                "stephane.bouvry@unicaen.fr"
            ],
            "Ing\u00e9nieur": [
                "NATHALIE HEUZE VOURC'H",
@@ -51,7 +51,7 @@
        "status": "102",
        "persons": {
            "Responsable Scientifique": [
                "Albert Einstein"
                "stephane.bouvry@unicaen.fr"
            ],
            "Ing\u00e9nieur": [
                "JO JO LA PIN",
@@ -113,7 +113,7 @@
        "status": "101",
        "persons": {
            "Responsable Scientifique": [
                "Max Plank"
                "stephane.bouvry@unicaen.fr"
            ],
            "Ing\u00e9nieur": [
                "Niels Bohr",
+4 −1
Original line number Diff line number Diff line
@@ -63,6 +63,9 @@ class OscarActivityImportJsonCommand extends OscarCommandAbstract

        $this->addOption('create-missing-activity-type', 'y', InputOption::VALUE_OPTIONAL,
            "Créer automatiquement les types d'activités manquantes",false);

        $this->addOption('use-person-mail', 'm', InputOption::VALUE_OPTIONAL,
            "Les personnes sont identifiées par leur email",false);
    }

    private function getReadablePath($path)
@@ -127,6 +130,7 @@ class OscarActivityImportJsonCommand extends OscarCommandAbstract
            'create-missing-organization' => $input->getOption('create-missing-organization', false) !== false,
            'create-missing-organization-role' => $input->getOption('create-missing-organization-role', false) !== false,
            'create-missing-activity-type' => $input->getOption('create-missing-activity-type', false) !== false,
            'use-person-mail' => $input->getOption('use-person-mail', false) !== false,
        ];

        $fileExtension = pathinfo($file)['extension'];
@@ -164,7 +168,6 @@ class OscarActivityImportJsonCommand extends OscarCommandAbstract
        } else {
            die("ERROR : Format non pris en charge.");
        }

        $importer = new ConnectorActivityJSON($json, $entityManager,
            $options);
        $repport = $importer->syncAll();
+27 −2
Original line number Diff line number Diff line
@@ -188,6 +188,26 @@ class ConnectorActivityCSVWithConf implements ConnectorInterface
        return $out;
    }

    /**
     * Variante pour l'extraction des personnes
     *
     * @param $value
     * @param $separator
     */
    public function extractArrayStringPersons($value, $separator, $options='fullname')
    {
        $out = [];
        $personDatas = $this->extractArrayString($value, $separator);
        // TODO extraction des données plus élaborée
//        foreach ($personDatas as $personData){
//            if( $options == 'fullname' ){
//
//            }
//        }
        return $personDatas;

    }

    /**
     * @return ConnectorRepport
     * @throws \Doctrine\ORM\NonUniqueResultException
@@ -250,15 +270,20 @@ class ConnectorActivityCSVWithConf implements ConnectorInterface
                        $json['organizations'][$role] = array_merge($json['organizations'][$role], $this->extractArrayString($value, $separator));
                    }
                } else if (preg_match("/persons\.(.*)/", $key, $matches)) {
                    $role = $matches[1];
                    $format = $matches[1];


                    $role = $format;

                    // Création de la clef si besoin
                    if (!array_key_exists($role, $json['persons'])) {
                        $json['persons'][$role] = [];
                    }
                    if (!in_array($value, $json['persons'][$role])) {
                        $json['persons'][$role] = array_merge($json['persons'][$role], $this->extractArrayString($value, $separator));
                        $json['persons'][$role] = array_merge($json['persons'][$role], $this->extractArrayStringPersons($value, $separator));
                    }


                } else if (preg_match("/milestones\.(.*)/", $key, $matches)) {
                    $json['milestones'][] = [
                        "type" => $matches[1],
+76 −47
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ class ConnectorActivityJSON implements ConnectorInterface
                "create-missing-person-role"        => false,
                "create-missing-project"            => false,
                "create-missing-activity-type"      => false,
                "use-person-mail"      => false,
            ];
        } else {
            $this->options = $options;
@@ -168,6 +169,8 @@ class ConnectorActivityJSON implements ConnectorInterface

    protected function getPersonOrCreate( $personDatas, ConnectorRepport $repport ){

        var_dump($personDatas); die();

        $fullname = $personDatas['firstname']. ' ' . $personDatas['lastname'] . ($personDatas['email'] ? '<'.$personDatas['email'].'>' : '');
        try {
            $query = $this->entityManager->getRepository(Person::class)->createQueryBuilder('p')
@@ -405,8 +408,12 @@ class ConnectorActivityJSON implements ConnectorInterface
            // Projet de l'activité
            $projectAcronym = $data->acronym;

            if( $data->project || $data->projectlabel )
                $projectLabel = $data->project ?: $data->projectlabel;
            if( property_exists($data, 'project') ){

            }

            if( @$data->project || @$data->projectlabel )
                $projectLabel = @$data->project ?: @$data->projectlabel;


            $project = $this->getProjectOrCreate( $projectAcronym, $projectLabel, $repport);
@@ -435,14 +442,14 @@ class ConnectorActivityJSON implements ConnectorInterface
            $dateStart = null;
            $dateEnd = null;

            if( $data->currency ){
            if( @$data->currency ){
                $currency = $this->getCurrency($data->currency);
            } else {
                $currency = $defaultCurrency;
            }

            if( $data->currency ){
                $tva = $this->getTva($data->tva);
            if( @$data->currency ){
                $tva = $this->getTva(@$data->tva);
            } else {
                $tva = null;
            }
@@ -462,14 +469,14 @@ class ConnectorActivityJSON implements ConnectorInterface
                ->setAssietteSubventionnable($this->getPropertyObject($data, 'assietteSubventionnable', false))
                ->setCurrency($currency)
                ->setTva($tva)
                ->setCodeEOTP($data->pfi)
                ->setCodeEOTP(@$data->pfi)
                ->setActivityType($type)
                ->setDateSigned($data->datesigned ? new \DateTime($data->datesigned) : null)
                ->setDateOpened($data->datePFI ? new \DateTime($data->datePFI) : null)
                ->setDateSigned(@$data->datesigned ? new \DateTime($data->datesigned) : null)
                ->setDateOpened(@$data->datePFI ? new \DateTime($data->datePFI) : null)
                ->setStatus($status)
                ->setAmount(((double)$data->amount));
                ->setAmount(((double)@$data->amount));

            if( $data->datestart ){
            if( @$data->datestart ){
                try {
                    $dateStart = new \DateTime($data->datestart);
                } catch (\Exception $e ){
@@ -478,7 +485,7 @@ class ConnectorActivityJSON implements ConnectorInterface
                }
            }

            if( $data->dateend ){
            if( @$data->dateend ){
                try {
                    $dateEnd = new \DateTime($data->dateend);
                } catch (\Exception $e ){
@@ -491,39 +498,8 @@ class ConnectorActivityJSON implements ConnectorInterface
                ->setDateStart($dateStart)
                ->setDateEnd($dateEnd);

            $this->entityManager->flush($activity);



            //// TRAITEMENT des ORGANISATIONS
            foreach( $data->organizations as $role=>$organizations ){
                try {

                    $roleObj = $this->getRoleOrganizationOrCreate( $role, $repport );

                    foreach( $organizations as $fullName ){
                        try {
                            $organization = $this->getOrganizationOrCreate($fullName, $repport);

                            if( !$activity->hasOrganization($organization, $roleObj->getLabel()) ){
                                $activityOrganization = new ActivityOrganization();
                                $this->entityManager->persist($activityOrganization);
                                $activityOrganization->setOrganization($organization)
                                    ->setActivity($activity)
                                    ->setRoleObj($roleObj);
                                $this->entityManager->flush($activityOrganization);
                                $repport->addadded(sprintf("L'oganisation '%s' a été ajoutée dans %s avec le rôle '%s'.", $fullName, $activity, $role));
                            }
                        } catch( \Exception $e ){
                            $repport->adderror(sprintf("Impossible d'affecter %s comme %s dans %s : %s.", $fullName, $role, $activity, $e->getMessage()));
                            return $repport;
                        }
                    }
                } catch( \Exception $e ){
                    $repport->adderror($e->getMessage());
                    return $repport;
                }
            }
            $this->entityManager->flush($activity);

//// TRAITEMENT des PERSONNES
            foreach( $data->persons as $role=>$persons ){
@@ -533,10 +509,32 @@ class ConnectorActivityJSON implements ConnectorInterface
                    $roleObj = $this->getRolePersonOrCreate( $role, $repport );



                    foreach( $persons as $fullName ){

                        $person = null;

                        if( $this->options['use-person-mail'] ){

                            try {
                                $person = $this->entityManager->getRepository(Person::class)->findOneBy(['email' => $fullName]);
                                if( $person == null ){
                                    throw new \Exception("Personne n'a été trouvé avec l'email '$fullName'");
                                }
                            } catch (\Exception $e) {
                                $repport->adderror(sprintf("Problème détécté lors de la recherche d'un personne avec l'email '%s' : %s", $fullName, $e->getMessage()));
                            }

                        } else {
                            $datasPerson = (new DataExtractorFullname())->extract($fullName);
                            if( $datasPerson ) {
                                $person = $this->getPersonOrCreate($datasPerson, $repport);
                            } else {
                                $repport->adderror(sprintf("Impossible de traiter la personne '%s'", $fullName));
                            }
                        }

                        if( $person ){
                            if( !$activity->hasPerson($person, $role) ){
                                try {
                                    $personActivity = new ActivityPerson();
@@ -551,9 +549,8 @@ class ConnectorActivityJSON implements ConnectorInterface
                                    $repport->addadded(sprintf("Impossible d'ajouter %s dans %s avec le rôle %s : %s.", $fullName, $activity, $role, $e->getMessage()));
                                }
                            }
                        } else {
                            $repport->adderror(sprintf("Impossible de traiter la personne '%s'", $fullName));
                        }

                    }

                } catch( \Exception $e ){
@@ -562,6 +559,38 @@ class ConnectorActivityJSON implements ConnectorInterface
                }
            }


            //// TRAITEMENT des ORGANISATIONS
            foreach( $data->organizations as $role=>$organizations ){
                try {

                    $roleObj = $this->getRoleOrganizationOrCreate( $role, $repport );

                    foreach( $organizations as $fullName ){
                        try {
                            $organization = $this->getOrganizationOrCreate($fullName, $repport);

                            if( !$activity->hasOrganization($organization, $roleObj->getLabel()) ){
                                $activityOrganization = new ActivityOrganization();
                                $this->entityManager->persist($activityOrganization);
                                $activityOrganization->setOrganization($organization)
                                    ->setActivity($activity)
                                    ->setRoleObj($roleObj);
                                $this->entityManager->flush($activityOrganization);
                                $repport->addadded(sprintf("L'oganisation '%s' a été ajoutée dans %s avec le rôle '%s'.", $fullName, $activity, $role));
                            }
                        } catch( \Exception $e ){
                            $repport->adderror(sprintf("Impossible d'affecter %s comme %s dans %s : %s.", $fullName, $role, $activity, $e->getMessage()));
                            return $repport;
                        }
                    }
                } catch( \Exception $e ){
                    $repport->adderror($e->getMessage());
                    return $repport;
                }
            }


            ///////////////////////////////////////////////////////// MILESTONES
            foreach ( $data->milestones as $milestone ){
                try {