Commit 9559ffec authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

UP : Mise en forme de l'export des activités

parent 32d45e1e
Loading
Loading
Loading
Loading
Loading
+101 −0
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: bouvry
 * Date: 04/10/19
 * Time: 11:49
 */

namespace Oscar\Command;


use Doctrine\ORM\EntityManager;
use Moment\Moment;
use Oscar\Connector\ConnectorActivityJSON;
use Oscar\Entity\Activity;
use Oscar\Entity\Authentification;
use Oscar\Entity\LogActivity;
use Oscar\Entity\OrganizationRole;
use Oscar\Entity\Person;
use Oscar\Entity\Role;
use Oscar\Entity\RoleRepository;
use Oscar\Exception\OscarException;
use Oscar\Formatter\ActivityToJsonFormatter;
use Oscar\Formatter\ConnectorRepportToPlainText;
use Oscar\Service\ConnectorService;
use Oscar\Service\OrganizationService;
use Oscar\Service\OscarConfigurationService;
use Oscar\Service\OscarUserContext;
use Oscar\Service\PersonService;
use Oscar\Service\ProjectGrantService;
use Oscar\Utils\ActivityCSVToObject;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class OscarActivityExportJsonCommand extends OscarCommandAbstract
{
    protected static $defaultName = 'activity:export-json';

    protected function configure()
    {
        $this
            ->setDescription("Synchronisation d'activité de recherche à partir d'un fichier JSON")
            ->addOption('fichier', 'f', InputOption::VALUE_REQUIRED, 'Fichier JSON')
        ;

        $this
            ->addArgument('oscarid', InputArgument::REQUIRED, "N°Oscar");

//        $this->addOption('create-missing-project', 'p', InputOption::VALUE_OPTIONAL,
//            "Créer automatiquement les projets manquants",false);
////
//        $this->addOption('create-missing-person', 'e', InputOption::VALUE_OPTIONAL,
//            "Créer automatiquement les personnes manquantes",false);
//
//        $this->addOption('create-missing-person-role', 'r', InputOption::VALUE_OPTIONAL,
//            "Créer automatiquement les rôles des personnes manquants",false);
//
//        $this->addOption('create-missing-organization', 'o', InputOption::VALUE_OPTIONAL,
//            "Créer automatiquement les organisations manquantes",false);
//
//        $this->addOption('create-missing-organization-role', 'l', InputOption::VALUE_OPTIONAL,
//            "Créer automatiquement les rôles des organisations manquants",false);
//
//        $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);
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->addOutputStyle($output);

        $oscarId = $input->getArgument("oscarid");

        //////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////// SERVICES
        /** @var OscarConfigurationService $oscarConfig */
        $oscarConfig = $this->getServicemanager()->get(OscarConfigurationService::class);

        /** @var ProjectGrantService $projectGrantService */
        $projectGrantService = $this->getServicemanager()->get(ProjectGrantService::class);

        /** @var EntityManager $entityManager */
        $entityManager = $this->getServicemanager()->get(EntityManager::class);

        /** @var Activity $activity */
        $activity = $projectGrantService->getActivityByOscarId($oscarId);

        $formatter = new ActivityToJsonFormatter();

        die(json_encode($formatter->format($activity), JSON_PRETTY_PRINT));

    }
}
 No newline at end of file
+101 −2
Original line number Diff line number Diff line
@@ -5,11 +5,110 @@ namespace Oscar\Formatter;


use Oscar\Entity\Activity;
use Oscar\Entity\ActivityDate;
use Oscar\Entity\ActivityPayment;
use Oscar\Entity\ActivityPerson;
use Oscar\Entity\Organization;

class ActivityToJsonFormatter
{
    public function format(Activity $activity) : array {
        $output = $activity->toArray(true);
        return $output;

        $out = array(
            'id' => $activity->getId(),
            'uid' => $activity->getId(),
            'projectacronym' => $activity->getProject() ? $activity->getProject()->getAcronym() : '',
            'project' => $activity->getProject() ? $activity->getProject()->getLabel() : '',
            'label' => $activity->getLabel(),
            'PFI' => $activity->getCodeEOTP(),
            'dateInit' => $activity->getDateOpened() ? $activity->getDateOpened()->format('Y-m-d') : '',
            'amount' => $activity->getAmount(),
            'numero' => $activity->getCentaureNumConvention(),
            'numOscar' => $activity->getOscarNum(),
            'typeOscar' => $activity->getActivityType() ? (string)$activity->getActivityType() : '',
            'statut' => $activity->getStatusLabel(),
            'dateStart' => $activity->getDateStart() ? $activity->getDateStart()->format('Y-m-d') : '',
            'dateEnd' => $activity->getDateEnd() ? $activity->getDateEnd()->format('Y-m-d') : '',
            'dateSigned' => $activity->getDateSigned() ? $activity->getDateSigned()->format('Y-m-d') : '',
            'dateUpdated' => $activity->getDateUpdated() ? $activity->getDateUpdated()->format('Y-m-d') : '',
            'paymentReceived' => $activity->getTotalPaymentReceived(),
            'paymentProvided' => $activity->getTotalPaymentProvided(),
        );


        $out['persons'] = [];
        /** @var ActivityPerson $personActivity */
        foreach ( $activity->getPersons() as $personActivity ){
            $out['persons'][] = [
                'uid' => $personActivity->getPerson()->getId(),
                'fullname' => $personActivity->getPerson()->getFirstname() . ' ' . $personActivity->getPerson()->getLastname(),
                'firstname' => $personActivity->getPerson()->getFirstname(),
                'lastname' => $personActivity->getPerson()->getLastname(),
                'email' => $personActivity->getPerson()->getEmail()
            ];
        }
        $out['organizations'] = [];
        foreach ( $activity->getOrganizations() as $organizationActivity ){
            /** @var Organization $organization */
            $organization = $organizationActivity->getOrganization();
            $out['organizations'][] = [
                'id' => $organization->getId(),
                'uid' => $organization->getId(),
                'code' => $organization->getCode(),
                'shortname' => $organization->getShortName(),
                'fullname' => $organization->getFullName(),
                'siret' => $organization->getSiret(),
                'type' => $organization->getType(),
                'address' => [
                    'street1' => $organization->getStreet1(),
                    'street2' => $organization->getStreet2(),
                    'street3' => $organization->getStreet3(),
                    'zipcode' => $organization->getZipCode(),
                    'city' => $organization->getCity(),
                    'country' => $organization->getCountry(),
                ]
            ];
        }


        $out['payments'] = [];
        /** @var ActivityPayment $payment */
        foreach ( $activity->getPayments() as $payment ){
            $out['payments'][] = [
                    'id' => $payment->getId(),
                    'datePayment' => $this->formatDate($payment->getDatePayment()),
                    'datePredicted' => $this->formatDate($payment->getDatePredicted()),
                    'amount' => $payment->getAmount(),
                    'rate' => $payment->getRate(),
                    'currency' => $payment->getCurrency() ? $payment->getCurrency()->getLabel() : null,
                    'codeTransaction' => $payment->getCodeTransaction(),
                    'comment' => $payment->getComment(),
                    'status' => $payment->getStatus(),
                    'statusLabel' => $payment->getStatusLabel(),
                    'late' => $payment->isLate()
                ];
        }

        $out['milestones'] = [];
        /** @var ActivityDate $milestone */
        foreach ( $activity->getMilestones() as $milestone ){
            $out['milestones'][] = [
                    'id' => $milestone->getId(),
                    'type' => $milestone->getType()->getLabel(),
                    'type_facet' => $milestone->getType()->getFacet(),
                    'comment' => $milestone->getComment(),
                    'dateStart' => $this->formatDate($milestone->getDateStart()),
                    'finishable' => $milestone->isFinishable(),
                    'finished' => $milestone->isFinishable() ? $milestone->isFinished() : null,
                ];
        }


        return $out;
    }

    protected function formatDate( $date )
    {
        return $date ? $date->format('Y-m-d') : null;
    }
}
 No newline at end of file
+9 −0
Original line number Diff line number Diff line
@@ -194,6 +194,15 @@ class ProjectGrantService implements UseOscarConfigurationService, UseEntityMana
        return $activity;
    }

    public function getActivityByOscarId( $oscarId, $throw = true )
    {
        $activity = $this->getActivityRepository()->findOneBy(['oscarNum'=>$oscarId]);
        if( !$activity && $throw === true ){
            throw new OscarException(sprintf(__("Impossible de charger l'activité '%s'"), $oscarId));
        }
        return $activity;
    }

    /**
     * Retourne la liste des types de documents disponibles pour qualifier les documents dans les activités de
     * recherche.