Commit b3b6bfb5 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Gestion des sumif, isblank et concaténations

parent ad0883b1
Loading
Loading
Loading
Loading
+0 −3
Changes for data/formule.sql: 0 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -123,9 +123,6 @@ CREATE OR REPLACE PACKAGE BODY FORMULE_<--NAME--> AS
      vh := ose_formule.volumes_horaires.items(l);
    END IF;
    CASE c



<--CELLS-->
    ELSE
      dbms_output.put_line('La colonne c=' || c || ', l=' || l || ' n''existe pas!');
+114 −4
Changes for module/Application/src/Model/FormuleCalcul.php: 114 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -397,7 +397,16 @@ END FORMULE_" . $this->getName() . ";";
        $s        = $this->getSheet();
        $formules = $this->getFormuleCells();

        $cells = '';
        $cells = $this->indent('-- Colonnes de base', 3);
        foreach ($this->getCellsPos() as $prop => $name) {
            if (str_starts_with($prop, 'vh.')) {
                $cellPlsql = "WHEN '$name' THEN RETURN $prop;";
                $cells     .= $this->indent($cellPlsql, 3);
            }
        }

        $cells .= "\n\n\n";

        foreach ($formules as $name => $cell) {
            $this->currentCellName = $name;

@@ -567,7 +576,7 @@ END FORMULE_" . $this->getName() . ";";

    private function returnPlsql(string $plsql): string
    {
        if (!(str_starts_with($plsql, 'IF') || str_starts_with($plsql, 'RETURN'))) {
        if (!(str_starts_with($plsql, 'IF') || str_starts_with($plsql, 'RETURN') || str_contains($plsql, 'sumIfRow'))) {
            $plsql = 'RETURN ' . $plsql;
        }

@@ -689,6 +698,19 @@ END FORMULE_" . $this->getName() . ";";
                $expr[$i]['exprs'][2] = $else;
            }
        }

        // report des opération faites avant et après les SUMIF à l'intérieur des expressions de résultat du SUMIF
        if ($expr[$i]['name'] === 'SUMIF' && count($expr) > 1) {
            $expr[$i]['valExpr'] = [];
            foreach ($expr as $a => $aitem) {
                if ($a != $i) {
                    $expr[$i]['valExpr'][] = $aitem;
                    unset($expr[$a]);
                } else {
                    $expr[$i]['valExpr'][] = ['type' => 'plsql', 'code' => 'val'];
                }
            }
        }
    }


@@ -813,6 +835,8 @@ END FORMULE_" . $this->getName() . ";";
                }
                if (array_key_exists($term['type'], $methods)) {
                    $plsql .= $this->{$methods[$term['type']]}($expr, $i);
                } elseif ($term['type'] === 'plsql') {
                    $plsql .= $term['code'];
                } else {
                    $plsql .= '[PB TRADUCTION]';
                }
@@ -855,7 +879,16 @@ END FORMULE_" . $this->getName() . ";";
    {
        $term = $expr[$i];

        return ' ' . $term['name'] . ' ';
        $trads = [
            '&' => '||',
        ];

        $op = $term['name'];
        if (isset($trads[$op])) {
            $op = $trads[$op];
        }

        return ' ' . $op . ' ';
    }


@@ -877,7 +910,7 @@ END FORMULE_" . $this->getName() . ";";

        $col = Calc::numberToLetter($col);

        $absName = $term['absName'];
        $absName = $term['absName'] ?? $term['name'];
        if (str_starts_with($absName, '$')) {
            $absName = substr($absName, 1); // on élimine le premier $ lié à la colonne
        }
@@ -944,6 +977,8 @@ END FORMULE_" . $this->getName() . ";";
            'IF'      => 'traductionFunctionIf',
            'AND'     => 'traductionFunctionAnd',
            'OR'      => 'traductionFunctionOr',
            'ISBLANK' => 'traductionFunctionIsBlank',
            'SUMIF'   => 'traductionFunctionSumIf',
        ];

        if (array_key_exists($term['name'], $functions)) {
@@ -1079,6 +1114,81 @@ END FORMULE_" . $this->getName() . ";";



    private function traductionFunctionIsBlank(array &$expr, int $i): string
    {
        $term = $expr[$i];
        $test = $term['exprs'][0];

        if (1 === count($test)) {
            $plsql = $this->traductionExpr($test) . ' IS NULL';
        } elseif (count($test) > 1) {
            $plsql = '(' . $this->traductionExpr($test) . ') IS NULL';
        }

        return $plsql;
    }



    private function traductionFunctionSumIf(array &$expr, int $i): string
    {
        $term = $expr[$i];

//SUMIF([.AD21:.AD$500];[.AD20];[.M21:.$M500])
        $plage   = $term['exprs'][0][0];
        $critere = $term['exprs'][1];
        if (isset($term['exprs'][2][0])) {
            $plageSomme = $term['exprs'][2][0];
        } else {
            $plageSomme = $plage;
        }

        if ($critere[0]['type'] != 'op') { // ajout du =, valeuir par défaut
            $cc      = $critere;
            $critere = [['type' => 'op', 'name' => '=']];
            foreach ($cc as $c) {
                $critere[] = $c;
            }
        }

        var_dump($plage);
        var_dump($critere);
        var_dump($plageSomme);

        $plsql = "val := 0;\n";
        for ($c = 0; $c <= ($plage['colEnd'] - $plage['colBegin']); $c++) {
            $col     = Calc::numberToLetter($plage['colBegin'] + $c);
            $colDest = Calc::numberToLetter($plageSomme['colBegin'] + $c);

            $rowBegin = $plage['rowBegin'] - $this->mainLine;
            if ($rowBegin === 0) {
                $rowBegin = 'l';
            } else {
                $rowBegin = 'l + ' . $rowBegin;
            }

            if ($plage['rowEnd'] >= 500) {
                $rowEnd = 'ose_formule.volumes_horaires.length';
            } else {
                $rowEnd = (string)($plage['rowEnd'] - $this->mainLine);
            }

            $plsql .= "FOR sumIfRow IN $rowBegin .. $rowEnd LOOP\n";
            $plsql .= "  IF cell('" . $col . "',sumIfRow)" . $this->traductionExpr($critere) . " THEN\n";
            $plsql .= "    val := val + cell('" . $colDest . "',sumIfRow);\n";
            $plsql .= "  END IF;\n";
            $plsql .= "END LOOP;\n";
        }

        $plsql .= 'RETURN ' . $this->traductionExpr($term['valExpr']);

        echo '<pre>' . htmlentities($plsql) . '</pre>';

        return $plsql;
    }



    private function indent(string $plsql, int $levels = 1): string
    {