Commit 4a4114b5 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

Multirequête (en cours)

parent cfbb5baf
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ SAPSR3.DD07V.DDTEXT AS DESIGNATION,
  SAPSR3.FMIOI.TRBTR AS MONTANT,
  NVL(substr(SAPSR3.FMIOI.OBJNRZ, '12'), SAPSR3.FMIOI.PRCTR) AS CENTREFINANCIER,
  SAPSR3.FMIOI.SGTXT AS TEXTEFACTURE,
  '' AS NUMSIFAC,
  SAPSR3.FMIOI.STUNR AS NUMSIFAC,
  '' AS pieceref,
  -- Les dates sont "à confirmer"
  sapsr3.fmioi.budat AS DATEPIECE,
+6 −2
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@ class OscarSpentSyncCommand extends OscarCommandAbstract
        $this
            ->setDescription("Permet d'obtenir les dépenses")
            ->setHelp("")
            ->addArgument('pfi', InputArgument::OPTIONAL, "PFI à synchroniser");
            ->addArgument('pfi', InputArgument::OPTIONAL, "PFI à synchroniser")
            ->addOption("query", "r", InputArgument::OPTIONAL)
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
@@ -43,6 +45,8 @@ class OscarSpentSyncCommand extends OscarCommandAbstract

        $io->title("Synchronisation des dépenses");

        $query = $input->getOption("query", null);

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

@@ -80,7 +84,7 @@ class OscarSpentSyncCommand extends OscarCommandAbstract
                    [$this->getServicemanager()->get(SpentService::class), $conf['params']]
                );

                $result = $instance->sync($pfi, true);
                $result = $instance->sync($pfi, true, $query);
                $io->write($result);
                return Command::SUCCESS;
            }
+97 −39
Original line number Diff line number Diff line
@@ -73,6 +73,9 @@ class ConnectorSpentSifacOCI
                throw new OscarException("La requête $queryTagName n'existe pas");
            }
        }



        $datas = $this->query($pfi, $querySend);
        return $datas;
    }
@@ -81,6 +84,7 @@ class ConnectorSpentSifacOCI
        try {
            $c = $this->getConnection();
            $query = sprintf($query, $pfi);
            $this->logDebug($query);
            if ($c) {
                $stid = oci_parse($c, $query);
                if (!$stid) {
@@ -114,69 +118,123 @@ class ConnectorSpentSifacOCI
     * @return string
     * @throws OscarException
     */
    public function sync($pfi, bool $optimized = false) :string
    public function sync($pfi, bool $optimized = false, ?string $query=null) :string
    {
        $this->logInfo("> call sync('$pfi')'" . ($optimized ? ' [optimized]' : ''));
        $this->logInfo("> call sync('$pfi')'"
                       . ($optimized ? ' [optimized]' : '')
                       . ' ['.($query ?? ' all') . ']');
        $queryTags = [];
        if( $query == self::QUERY_DEFAULT || $query === null ) {
            $queryTags[] = self::QUERY_DEFAULT;
        }
        if( $query === null ){
            foreach ($this->getQueries() as $key=>$query) {
                $queryTags[] = $key;
            }
        } else {
            $queryTags[] = $query;
        }

        try {
            $c = $this->getConnection();
            $exists = $this->spentService->getSpentsSyncIdByPFI($pfi);
            $query = sprintf($this->sifacAccess['spent_query'], $pfi);
            if( $optimized ){
                $this->logDebug("Optimisation : récupération du dernier syncId");
                $lastId = $this->spentService->getLastSyncId($pfi);
                if( $lastId ){
                    $query .= sprintf($this->sifacAccess['optimize_clause'], $lastId);
                }
            }

            $this->logDebug("Query : '$query'");

            if ($c) {
                $stid = oci_parse($c, $query);
                if (!$stid) {
                    throw new OscarException("ORACLE - PARSE ERROR : " . oci_error());
                }
                if (!oci_execute($stid)) {
                    throw new OscarException("ORACLE - QUERY ERROR : " . oci_error());
        } catch (\Exception $e) {
            throw new OscarException($e->getMessage());
        }
                $nbr = 0;
                $nbrAdd = 0;
                $nbrExist = count($exists);

                while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) {
                    $nbr++;
                    if (!in_array($row['IDSYNC'], $exists)) {
        try {
                            $this->spentService->addSpentLine($row);
                            $nbrAdd++;
            $added = 0;
            foreach ($queryTags as $queryTag) {
                $this->logDebug("process '$queryTag'");
                $datas = $this->data($pfi, $queryTag);
                $this->logDebug("data from source : " . count($datas));

                foreach ($datas as $data) {
                    if (!in_array($data['IDSYNC'], $exists)) {
                        $this->logDebug("perform row data " . $data['IDSYNC']);
                        try {
                            $this->spentService->addSpentLine($data);
                            $exists[] = $data['IDSYNC'];
                            $added++;

                        } catch (\Exception $e) {
                            throw new OscarException(
                                "ERROR - Enregistrement d'une ligne de dépense impossible pour " . $row['IDSYNC'] . " : " . $e->getMessage()
                            );
                        }
                    } else {
                        $this->logDebug("perform row data " . $data['IDSYNC']);
                    }
                }
            }

            $message = sprintf(
                    "%s déjà synchronisé(s), %s entrée(s) dans SIFAC, %s résultat(s) ajouté(s) dans Oscar pour %s.",
                    $nbrExist,
                    $nbr,
                    $nbrAdd,
                    "Données synchronisées, %s ajoutée(s) pour %s.",
                    $added,
                    $pfi
                );

                $this->logInfo($message);

                return $message;
            }
            else {
                throw new OscarException("Erreur de connection ORACLE");
            }
        } catch (\Exception $e) {
            $this->logError($e->getMessage());
            throw $e;
            throw new OscarException($e->getMessage());
        }
            $query = sprintf($this->sifacAccess['spent_query'], $pfi);
//            if( $optimized ){
//                $this->logDebug("Optimisation : récupération du dernier syncId");
//                $lastId = $this->spentService->getLastSyncId($pfi);
//                if( $lastId ){
//                    $query .= sprintf($this->sifacAccess['optimize_clause'], $lastId);
//                }
//            }

//            $this->logDebug("Query : '$query'");
//
//            if ($c) {
//                $stid = oci_parse($c, $query);
//                if (!$stid) {
//                    throw new OscarException("ORACLE - PARSE ERROR : " . oci_error());
//                }
//                if (!oci_execute($stid)) {
//                    throw new OscarException("ORACLE - QUERY ERROR : " . oci_error());
//                }
//                $nbr = 0;
//                $nbrAdd = 0;
//                $nbrExist = count($exists);
//
//                while ($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) {
//                    $nbr++;
//                    if (!in_array($row['IDSYNC'], $exists)) {
//                        try {
//                            $this->spentService->addSpentLine($row);
//                            $nbrAdd++;
//                        } catch (\Exception $e) {
//                            throw new OscarException(
//                                "ERROR - Enregistrement d'une ligne de dépense impossible pour " . $row['IDSYNC'] . " : " . $e->getMessage()
//                            );
//                        }
//                    }
//                }
//
//                $message = sprintf(
//                    "%s déjà synchronisé(s), %s entrée(s) dans SIFAC, %s résultat(s) ajouté(s) dans Oscar pour %s.",
//                    $nbrExist,
//                    $nbr,
//                    $nbrAdd,
//                    $pfi
//                );
//
//                $this->logInfo($message);
//
//                return $message;
//            }
//            else {
//                throw new OscarException("Erreur de connection ORACLE");
//            }
//        } catch (\Exception $e) {
//            $this->logError($e->getMessage());
//            throw $e;
//        }
    }

    /**