Ejemplo n.º 1
0
function getTarifs()
{
    switch (func_num_args()) {
        case 2:
            // 2 arguments passés en paramètre
            $tab_prix = func_get_arg(0);
            $date = func_get_arg(1);
            break;
        case 3:
            // 3 arguments passés en paramètre
            $optarif = func_get_arg(0);
            $isousc = func_get_arg(1);
            $tab_prix = getTarifsEDF($optarif, $isousc);
            $date = func_get_arg(2);
            break;
        default:
            $tab_prix = null;
            $date = null;
            break;
    }
    // Pour chaque taxeC
    foreach ($tab_prix["TAXES_C"] as $tkey => $tval) {
        //$cur_prix["TAXES_C"][$tkey] = end($tval);
        krsort($tval);
        reset($tval);
        while (key($tval) > $date) {
            next($tval);
        }
        $cur_prix["TAXES_C"][$tkey] = current($tval);
    }
    // Pour chaque taxeA
    foreach ($tab_prix["TAXES_A"] as $tkey => $tval) {
        //$cur_prix["TAXES_A"][$tkey] = end($tval);
        krsort($tval);
        reset($tval);
        while (key($tval) > $date) {
            next($tval);
        }
        $cur_prix["TAXES_A"][$tkey] = current($tval);
    }
    // Puis les abonnement
    //$cur_prix["ABONNEMENTS"] = end($tab_prix["ABONNEMENTS"]);
    krsort($tab_prix["ABONNEMENTS"]);
    reset($tab_prix["ABONNEMENTS"]);
    while (key($tab_prix["ABONNEMENTS"]) > $date) {
        next($tab_prix["ABONNEMENTS"]);
    }
    $cur_prix["ABONNEMENTS"] = current($tab_prix["ABONNEMENTS"]);
    // Et enfin les tarifs
    //$cur_prix["TARIFS"] = end($tab_prix["TARIFS"]);
    krsort($tab_prix["TARIFS"]);
    reset($tab_prix["TARIFS"]);
    while (key($tab_prix["TARIFS"]) > $date) {
        next($tab_prix["TARIFS"]);
    }
    $cur_prix["TARIFS"] = current($tab_prix["TARIFS"]);
    /*    // Pour chaque taxeC
        foreach($tab_prix["TAXES_C"] as $tkey => $tval) {
            $cur_prix["TAXES_C"][$tkey] = end($tval);
        }
        // Pour chaque taxeA
        foreach($tab_prix["TAXES_A"] as $tkey => $tval) {
            $cur_prix["TAXES_A"][$tkey] = end($tval);
        }
        // Puis les abonnement
        $cur_prix["ABONNEMENTS"] = end($tab_prix["ABONNEMENTS"]);
        // Et enfin les tarifs
        $cur_prix["TARIFS"] = end($tab_prix["TARIFS"]); /* */
    return $cur_prix;
}
Ejemplo n.º 2
0
function history()
{
    global $teleinfo;
    global $config;
    $graphConf = $config["graphiques"]["history"];
    $duree = isset($_GET['duree']) ? $_GET['duree'] : 8;
    $periode = isset($_GET['periode']) ? $_GET['periode'] : "jours";
    //$date = isset($_GET['date'])?$_GET['date']:null;
    $date = isset($_GET['date']) ? $_GET['date'] : Tomorrow(getMaxDate());
    switch ($periode) {
        case "jours":
            // Calcul de la fin de période courante
            $timestampheure = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
            // Timestamp courant, 0h
            $timestampheure += 24 * 3600;
            // Timestamp courant +24h
            // Meilleure date entre celle donnée en paramètre, celle calculée et la dernière date en base
            $date = $date ? min($date, $timestampheure) : $timestampheure;
            // Périodes
            $periodesecondes = $duree * 24 * 3600;
            // Periode en secondes
            $timestampfin = $date;
            // Fin de la période
            $timestampdebut2 = $timestampfin - $periodesecondes;
            // Début de période active
            $timestampdebut = $timestampdebut2 - $periodesecondes;
            // Début de période précédente
            $xlabel = $duree . " jours";
            $dateformatsql = "%a %e";
            $divabonnement = 365;
            break;
        case "semaines":
            // Calcul de la fin de période courante
            $timestampheure = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
            // Timestamp courant, 0h
            $timestampheure += 24 * 3600;
            // Timestamp courant +24h
            // Meilleure date entre celle donnée en paramètre, celle calculée et la dernière date en base
            $date = $date ? min($date, $timestampheure) : $timestampheure;
            // Avance d'un jour tant que celui-ci n'est pas un lundi
            while (date("w", $date) != 1) {
                $date += 24 * 3600;
            }
            // Périodes
            $timestampfin = $date;
            // Fin de la période
            $timestampdebut2 = strtotime(date("Y-m-d", $timestampfin) . " -" . $duree . " week");
            // Début de période active
            $timestampdebut = strtotime(date("Y-m-d", $timestampdebut2) . " -" . $duree . " week");
            // Début de période précédente
            $xlabel = $duree . " semaines";
            $dateformatsql = "sem %v (%x)";
            $divabonnement = 52;
            break;
        case "mois":
            // Calcul de la fin de période courante
            $timestampheure = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
            // Timestamp courant, 0h
            // Meilleure date entre celle donnée en paramètre, celle calculée et la dernière date en base
            $date = $date ? min($date, $timestampheure) : $timestampheure;
            // Avance d'un jour tant qu'on n'est pas le premier du mois
            while (date("d", $date) != 1) {
                $date += 24 * 3600;
            }
            // Périodes
            $timestampfin = $date;
            // Fin de la période
            $timestampdebut2 = mktime(0, 0, 0, date("m", $timestampfin) - $duree, 1, date("Y", $timestampfin));
            // Début de période active
            $timestampdebut = mktime(0, 0, 0, date("m", $timestampdebut2) - $duree, 1, date("Y", $timestampdebut2));
            // Début de période précédente
            $xlabel = $duree . " mois";
            $dateformatsql = "%b (%Y)";
            if ($duree > 6) {
                $dateformatsql = "%b %Y";
            }
            $divabonnement = 12;
            break;
        case "ans":
            // Calcul de la fin de période courante
            $timestampheure = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
            // Timestamp courant, 0h
            // Meilleure date entre celle donnée en paramètre, celle calculée et la dernière date en base
            $date = $date ? min($date, $timestampheure) : $timestampheure;
            $date = mktime(0, 0, 0, 1, 1, date("Y", $date) + 1);
            // Année suivante, 0h
            // Périodes
            $timestampfin = $date;
            // Fin de la période
            $timestampdebut2 = mktime(0, 0, 0, 1, 1, date("Y", $timestampfin) - $duree);
            // Début de période active
            $timestampdebut = mktime(0, 0, 0, 1, 1, date("Y", $timestampdebut2) - $duree);
            // Début de période précédente
            $xlabel = $duree . " an";
            //$xlabel = "l'année ".(date("Y",$timestampdebut2)-$duree)." et ".(date("Y",$timestampfin)-$duree);
            $dateformatsql = "%b %Y";
            $divabonnement = 12;
            break;
        default:
            die("Periode erronée, valeurs possibles: [8jours|8semaines|8mois|1an] !");
            break;
    }
    $tab_optarif = getOPTARIF(true);
    $optarif = $tab_optarif["OPTARIF"];
    $isousc = $tab_optarif["ISOUSC"];
    $query = queryHistory($timestampdebut, $timestampfin, $dateformatsql, $optarif);
    $result = mysql_query($query) or die("<b>Erreur</b> dans la requète <b>" . $query . "</b> : " . mysql_error() . " !<br>");
    $kwhprec = array();
    $kwhprec_detail = array();
    $date_deb = 0;
    // date du 1er enregistrement
    $date_fin = time();
    $row = mysql_fetch_array($result);
    $optarif = $teleinfo["OPTARIF"][$row["OPTARIF"]];
    //$optarifStr = $teleinfo["LIBELLES"]["OPTARIF"][$teleinfo["OPTARIF"][$optarif]];
    $optarifStr = $teleinfo["LIBELLES"]["OPTARIF"][$optarif];
    $ptec = $teleinfo["PTEC"][$row["PTEC"]];
    $ptecStr = $teleinfo["LIBELLES"]["PTEC"][$ptec];
    // On initialise à vide
    // Cas si les périodes sont "nulles", on n'aura pas d'initialisation des tableaux
    foreach ($teleinfo["PERIODES"][$optarif] as $ptec) {
        $kwh[$ptec] = array();
        $kwhp[$ptec] = array();
    }
    $categories = array();
    $timestp = array();
    $timestpp = array();
    // Calcul des consommations
    $row = mysql_data_seek($result, 0);
    // Revient au début (car on a déjà lu un enreg)
    while ($row = mysql_fetch_array($result)) {
        $ts = intval($row["TIMESTAMP"]);
        if ($ts < $timestampdebut2) {
            // Période précédente
            $cumul = null;
            // reset (sinon on cumule à chaque étape de la boucle)
            $timestpp[] = $row["TIMESTAMP"];
            foreach ($teleinfo["PERIODES"][$optarif] as $ptec) {
                // On conserve le détail (qui sera affiché en infobulle)
                $kwhp[$ptec][] = floatval(isset($row[$ptec]) ? $row[$ptec] : 0);
                // On calcule le total consommé (qui sera affiché en courbe)
                $cumul[] = isset($row[$ptec]) ? $row[$ptec] : 0;
            }
            $kwhprec[] = array($row["PERIODE"], array_sum($cumul));
            // php recommande cette syntaxe plutôt que array_push
        } else {
            // Période courante
            if ($date_deb == 0) {
                $date_deb = $row["TIMESTAMP"];
                //$date_deb = strtotime($row["REC_DATE"]);
            }
            // Ajout les éléments actuels à chaque tableau
            $categories[] = $row["PERIODE"];
            $timestp[] = $row["TIMESTAMP"];
            foreach ($teleinfo["PERIODES"][$optarif] as $ptec) {
                $kwh[$ptec][] = floatval(isset($row[$ptec]) ? $row[$ptec] : 0);
            }
        }
    }
    // On vérifie la durée de la période actuelle
    if (count($kwh) < $duree) {
        // pad avec une valeur négative, pour ajouter en début de tableau
        $timestp = array_pad($timestp, -$duree, null);
        $categories = array_pad($categories, -$duree, null);
        foreach ($kwh as &$current) {
            $current = array_pad($current, -$duree, null);
        }
    }
    // On vérifie la durée de la période précédente
    if (count($kwhprec) < count(reset($kwh))) {
        // pad avec une valeur négative, pour ajouter en début de tableau
        $timestpp = array_pad($timestpp, -count(reset($kwh)), null);
        $kwhprec = array_pad($kwhprec, -count(reset($kwh)), null);
        foreach ($kwhp as &$current) {
            $current = array_pad($current, -count(reset($kwh)), null);
        }
    }
    mysql_free_result($result);
    $date_deb_UTC = $date_deb * 1000;
    $datetext = date("d/m G:i", $date_deb);
    // Calcul des coûts
    $tab_prix = getTarifsEDF($optarif, $isousc);
    foreach ($teleinfo["PERIODES"][$optarif] as $ptec) {
        $mnt["KWH"][$ptec] = 0;
        $mntp["KWH"][$ptec] = 0;
    }
    $i = 0;
    $rounds = max(count(reset($kwh)), count(reset($kwhp)));
    while ($i < $rounds) {
        // Puriste, on sépare les traitements des périodes courante et précédente.
        // - Si l'option tarifaire évolue (pas encore pris en charge)
        // - Si les taxes évoluent
        // Période courante
        // On recherche la base tarifaire pour cette période (date)
        $cur_prix = getTarifs($tab_prix, $timestp[$i]);
        foreach ($cur_prix["TAXES_C"] as $tkey => $tval) {
            $mnt["TAXES"][$tkey][$i] = 0;
            // Init à zéro
        }
        $mnt["TOTAL"][$i] = 0;
        $conso = false;
        foreach ($teleinfo["PERIODES"][$optarif] as $ptec) {
            // TaxesC
            foreach ($cur_prix["TAXES_C"] as $tkey => $tval) {
                $mnt["TAXES"][$tkey][$i] += $kwh[$ptec][$i] * $cur_prix["TAXES_C"][$tkey];
                $mnt["TOTAL"][$i] += $kwh[$ptec][$i] * $cur_prix["TAXES_C"][$tkey];
            }
            // Consommation
            $mnt["TARIFS"][$ptec][$i] = $kwh[$ptec][$i] * $cur_prix["TARIFS"][$ptec];
            $mnt["TOTAL"][$i] += $kwh[$ptec][$i] * $cur_prix["TARIFS"][$ptec];
            $mnt["KWH"][$ptec] += $kwh[$ptec][$i];
            $conso = ($conso or $kwh[$ptec][$i] != 0);
        }
        // TaxesA
        foreach ($cur_prix["TAXES_A"] as $tkey => $tval) {
            $mnt["TAXES"][$tkey][$i] = $cur_prix["TAXES_A"][$tkey] / $divabonnement;
            $mnt["TOTAL"][$i] += $cur_prix["TAXES_A"][$tkey] / $divabonnement;
        }
        // Abonnement
        $mnt["ABONNEMENTS"][$i] = $conso * $cur_prix["ABONNEMENTS"][$optarif] / $divabonnement;
        $mnt["TOTAL"][$i] += $conso * $cur_prix["ABONNEMENTS"][$optarif] / $divabonnement;
        // Période précédente
        // On recherche la base tarifaire pour la période précédente
        $prec_prix = getTarifs($tab_prix, $timestpp[$i]);
        foreach ($prec_prix["TAXES_C"] as $tkey => $tval) {
            $mntp["TAXES"][$tkey][$i] = 0;
            // Init à zéro
        }
        $mntp["TOTAL"][$i] = 0;
        $conso = false;
        foreach ($teleinfo["PERIODES"][$optarif] as $ptec) {
            // TaxesC
            foreach ($prec_prix["TAXES_C"] as $tkey => $tval) {
                $mntp["TAXES"][$tkey][$i] += $kwhp[$ptec][$i] * $prec_prix["TAXES_C"][$tkey];
                $mntp["TOTAL"][$i] += $kwhp[$ptec][$i] * $prec_prix["TAXES_C"][$tkey];
            }
            // Consommation
            $mntp["TARIFS"][$ptec][$i] = $kwhp[$ptec][$i] * $prec_prix["TARIFS"][$ptec];
            $mntp["TOTAL"][$i] += $kwhp[$ptec][$i] * $prec_prix["TARIFS"][$ptec];
            $mntp["KWH"][$ptec] += $kwhp[$ptec][$i];
            $conso = ($conso or $kwhp[$ptec][$i] != 0);
        }
        // TaxesA
        foreach ($prec_prix["TAXES_A"] as $tkey => $tval) {
            $mntp["TAXES"][$tkey][$i] = $prec_prix["TAXES_A"][$tkey] / $divabonnement;
            $mntp["TOTAL"][$i] += $prec_prix["TAXES_A"][$tkey] / $divabonnement;
        }
        // Abonnement
        $mntp["ABONNEMENTS"][$i] = $conso * $prec_prix["ABONNEMENTS"][$optarif] / $divabonnement;
        $mntp["TOTAL"][$i] += $conso * $prec_prix["ABONNEMENTS"][$optarif] / $divabonnement;
        $i++;
    }
    // Totaux période courante
    foreach ($mnt["TAXES"] as $tkey => $tval) {
        $total_mnt["TAXES"][$tkey] = array_sum($mnt["TAXES"][$tkey]);
    }
    foreach ($mnt["TARIFS"] as $tkey => $tval) {
        $total_mnt["TARIFS"][$tkey] = array_sum($mnt["TARIFS"][$tkey]);
    }
    $total_mnt["ABONNEMENTS"] = array_sum($mnt["ABONNEMENTS"]);
    $total_mnt["TOTAL"] = array_sum($mnt["TOTAL"]);
    $total_mnt["KWH"] = array_sum($mnt["KWH"]);
    // Totaux période précédente
    foreach ($mntp["TAXES"] as $tkey => $tval) {
        $total_mntp["TAXES"][$tkey] = array_sum($mntp["TAXES"][$tkey]);
    }
    foreach ($mntp["TARIFS"] as $tkey => $tval) {
        $total_mntp["TARIFS"][$tkey] = array_sum($mntp["TARIFS"][$tkey]);
    }
    $total_mntp["ABONNEMENTS"] = array_sum($mntp["ABONNEMENTS"]);
    $total_mntp["TOTAL"] = array_sum($mntp["TOTAL"]);
    $total_mntp["KWH"] = array_sum($mntp["KWH"]);
    // Subtitle pour la période courante
    $subtitle = "";
    if ($total_mnt["TOTAL"] != 0) {
        // A priori, toujours vrai !
        $subtitle = $subtitle . "<b>Coût sur la période</b> " . round($total_mnt["TOTAL"], 2) . " Euro (" . $total_mnt["KWH"] . " KWh)<br />";
        $subtitle = $subtitle . "(Abonnement : " . round($total_mnt["ABONNEMENTS"], 2);
        $subtitle = $subtitle . " + Taxes (" . implode(", ", array_keys($total_mnt["TAXES"])) . ") : " . round(array_sum($total_mnt["TAXES"]), 2);
        foreach ($total_mnt["TARIFS"] as $ptec => $val) {
            if ($total_mnt["TARIFS"][$ptec] != 0) {
                $subtitle = $subtitle . " + " . $ptec . " : " . round($total_mnt["TARIFS"][$ptec], 2);
            }
        }
        $subtitle = $subtitle . ")";
        if (count($total_mnt["TARIFS"]) > 1 && $total_mnt["KWH"] > 0) {
            $subtitle = $subtitle . "<br /><b>Total KWh</b> ";
            $prefix = "";
            foreach ($mnt["KWH"] as $ptec => $val) {
                if ($mnt["KWH"][$ptec] != 0) {
                    $subtitle = $subtitle . $prefix . $ptec . " : " . $mnt["KWH"][$ptec];
                    if ($prefix == "") {
                        $prefix = " + ";
                    }
                }
            }
        }
    }
    // Subtitle pour la période précédente
    if ($total_mntp["TOTAL"] != 0) {
        $subtitle = $subtitle . "<br /><b>Coût sur la période précédente</b> " . round($total_mntp["TOTAL"], 2) . " Euro (" . $total_mntp["KWH"] . " KWh)<br />";
        $subtitle = $subtitle . "(Abonnement : " . round($total_mntp["ABONNEMENTS"], 2);
        $subtitle = $subtitle . " + Taxes (" . implode(", ", array_keys($total_mntp["TAXES"])) . ") : " . round(array_sum($total_mntp["TAXES"]), 2);
        foreach ($total_mntp["TARIFS"] as $ptec => $val) {
            if ($total_mntp["TARIFS"][$ptec] != 0) {
                $subtitle = $subtitle . " + " . $ptec . " : " . round($total_mntp["TARIFS"][$ptec], 2);
            }
        }
        $subtitle = $subtitle . ")";
        if (count($total_mntp["TARIFS"]) > 1 && $total_mntp["KWH"] > 0) {
            $subtitle = $subtitle . "<br /><b>Total KWh</b> ";
            $prefix = "";
            foreach ($mntp["KWH"] as $ptec => $val) {
                if ($mntp["KWH"][$ptec] != 0) {
                    $subtitle = $subtitle . $prefix . $ptec . " : " . $mntp["KWH"][$ptec];
                    if ($prefix == "") {
                        $prefix = " + ";
                    }
                }
            }
        }
    }
    // Conserve les séries nécessaires
    $series = array_intersect_key($teleinfo["LIBELLES"]["PTEC"], array_flip($teleinfo["PERIODES"][$optarif]));
    $history = array('show3D' => $graphConf["show3D"], 'title' => "Consomation sur {$xlabel}", 'subtitle' => $subtitle, 'optarif' => array($optarif => $optarifStr), 'ptec' => array($ptec => $ptecStr), 'duree' => $duree, 'periode' => $periode, 'debut' => $timestampfin * 1000, 'series' => $series, 'prix' => $mnt, 'prix_tot' => $total_mnt, 'PREC_prix' => $mntp, 'PREC_prix_tot' => $total_mntp, 'categories' => $categories, 'PREC_color' => $teleinfo["COULEURS"]["PREC"], 'PREC_name' => 'Période Précédente', 'PREC_data' => $kwhprec, 'PREC_data_detail' => $kwhp, 'PREC_type' => $graphConf["typePrec"]);
    // Ajoute les séries
    foreach ($teleinfo["PERIODES"][$optarif] as $ptec) {
        $history[$ptec . "_color"] = $teleinfo["COULEURS"][$ptec];
        $history[$ptec . "_data"] = $kwh[$ptec];
        $history[$ptec . "_type"] = $graphConf["typeSerie"];
    }
    return $history;
}