| Current Path : /home/ataenra/www/ATA/INTRANET/Admin/Gestion_ACC/ |
| Current File : /home/ataenra/www/ATA/INTRANET/Admin/Gestion_ACC/CSVCC.php |
<?php
require "../../Outils/co.php";
if(isset($_GET['ID'])){
$IDsite = $_GET['ID']; }
$IDpmo = $_GET['IDpmo'];
$list1 = $dbco->prepare("SELECT ID_acc_conso FROM gest_acc_conso_heures WHERE ID_acc_site='$IDsite'");
$list1->execute();
$R1 = $list1->fetch(PDO::FETCH_OBJ);
//Si site déja présent - EFFACER les données de la table conso_heures
if(isset($R1->ID_acc_conso)){
$del=$dbco->prepare("DELETE FROM `gest_acc_conso_bdd` WHERE ID_acc_site='$IDsite'");
$del->execute();
$del=$dbco->prepare("DELETE FROM `gest_acc_conso_heures` WHERE ID_acc_site='$IDsite'");
$del->execute();
}
//Verifier si table existe, supprimer, creer la table sur autre serveur
$requete = "DROP TABLE IF EXISTS `gest_acc_pmo_site_".$IDsite."_heures`";
$dbco1->prepare($requete)->execute();
$C3 = $dbco1->prepare("CREATE TABLE `AccMasny`.`gest_acc_pmo_site_".$IDsite."_heures` (`ID` INT NOT NULL AUTO_INCREMENT ,`jour` VARCHAR(20) NOT NULL , `heure` INT NOT NULL , `conso_puissance` INT NOT NULL , `conso_kwh` FLOAT NOT NULL , `tarif_kwh` FLOAT NOT NULL , `conso_cout` FLOAT NOT NULL , `cc_aci` FLOAT NOT NULL , `cc_aci_kwh` FLOAT NOT NULL , `cout_cc_aci` FLOAT NOT NULL , `ac_aci` FLOAT NOT NULL , `ac_aci_kwh` FLOAT NOT NULL , `cout_ac_aci` FLOAT NOT NULL , `inj_aci` FLOAT NOT NULL , `inj_aci_kwh` FLOAT NOT NULL , `conso_pct` FLOAT NOT NULL , `autoconso_cor` FLOAT NOT NULL , `autoconso_cor_kwh` FLOAT NOT NULL, `autoconso_cor_cout` FLOAT NOT NULL, PRIMARY KEY (`ID`)) ENGINE = InnoDB;");
$C3->execute();
$requete = "DROP TABLE IF EXISTS `gest_acc_pmo_site_".$IDsite."_jours`";
$dbco1->prepare($requete)->execute();
$C3 = $dbco1->prepare("CREATE TABLE `AccMasny`.`gest_acc_pmo_site_".$IDsite."_jours` (`ID` INT NOT NULL AUTO_INCREMENT ,`jour` VARCHAR(20) NOT NULL , `conso_kwh` FLOAT NOT NULL , `conso_cout` FLOAT NOT NULL , `cc_aci_kwh` FLOAT NOT NULL , `cout_cc_aci` FLOAT NOT NULL , `ac_aci_kwh` FLOAT NOT NULL , `cout_ac_aci` FLOAT NOT NULL , `inj_aci_kwh` FLOAT NOT NULL , `autoconso_cor` FLOAT NOT NULL , `autoconso_cor_kwh` FLOAT NOT NULL, `autoconso_cor_cout` FLOAT NOT NULL, PRIMARY KEY (`ID`)) ENGINE = InnoDB;");
$C3->execute();
$requete = "DROP TABLE IF EXISTS `gest_acc_pmo_site_".$IDsite."_mois`";
$dbco1->prepare($requete)->execute();
$C3 = $dbco1->prepare("CREATE TABLE `AccMasny`.`gest_acc_pmo_site_".$IDsite."_mois` (`ID` INT NOT NULL AUTO_INCREMENT ,`mois` INT NOT NULL , `conso_kwh` FLOAT NOT NULL , `conso_cout` FLOAT NOT NULL , `cc_aci_kwh` FLOAT NOT NULL , `cout_cc_aci` FLOAT NOT NULL , `ac_aci_kwh` FLOAT NOT NULL , `cout_ac_aci` FLOAT NOT NULL , `inj_aci_kwh` FLOAT NOT NULL , `autoconso_cor` FLOAT NOT NULL , `autoconso_cor_kwh` FLOAT NOT NULL, `autoconso_cor_cout` FLOAT NOT NULL, PRIMARY KEY (`ID`)) ENGINE = InnoDB;");
$C3->execute();
//Epurer le tableau
$ligne = 0;$c = 1; $jour=array(); $stop=1; $count=0;
if (($handle = fopen("../../../FICHIERS/ACC/$IDpmo/Sites/$IDsite/CC/CC.$IDsite.csv", "r")) !== FALSE) {
$csvj = new SplFileObject('../../../FICHIERS/ACC/'.$IDpmo.'/Sites/'.$IDsite.'/CC/CCN.'.$IDsite.'.csv', 'w');
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$ligne++;
if($ligne > 1){
//RECUPERER DATE
$modifdate = $data[1];
$modifjour = explode("T", $modifdate); //Separer la date et l'heure
$date = $modifjour[0]; //Date du type 0000-00-00
//RECUPERER ANNEE
$modifannee = explode("-", $modifdate); //Separer lannee et mois-jour
$annee = $modifannee[0]; //Date du type 0000
//RECUPERER HEURE
$modifheure = $modifjour[1];
$modifheure2 = str_replace("Z", "", $modifheure);
$heure2 = explode(":", $modifheure2);
$heure1 = $heure2[0];
if($heure1=="00"){$heure=0;}elseif($heure1=="01"){$heure=1;}elseif($heure1=="02"){$heure=2;}elseif($heure1=="03"){$heure=3;}elseif($heure1=="04"){$heure=4;}elseif($heure1=="05"){$heure=5;}elseif($heure1=="06"){$heure=6;}elseif($heure1=="07"){$heure=7;}elseif($heure1=="08"){$heure=8;}elseif($heure1=="09"){$heure=9;}else{
$heure = (int)$heure1;}
//RECUPERER PUISSANCE ET CONSO
$puissance = $data[3]*1000;
$conso = $data[3];
$DBCC[$c][1] = $annee;
$DBCC[$c][2] = $date;
$DBCC[$c][3] = $heure;
$DBCC[$c][4] = (int)$puissance;
$c++;
}
}
//var_dump($DBCC[1]);
foreach($DBCC as $DB){
$valjour[] = $DB[2];
}
foreach ($DBCC as $key => $row)
{
$wek[$key] = $row['2'];
}
array_multisort($wek, SORT_ASC, $DBCC);
$occujour = array_count_values($valjour);
$o = 1;
$pmax = 0;
$lmax=0;
$t=0;
foreach($DBCC as $DB){
if(isset($DBCC[$o])){
$Iteration = ($occujour["$DB[2]"])/24;
$u=0;
$year = $DBCC[$o][1];
if(($o>$t)&&($lmax<8800)&&($year==2024)){
while($u < $Iteration){
$t=$o+$u;
$pmax += $DBCC[$t][4];
$u++;
}
$DBCCF[$o][1] = $DBCC[$o][2];
$DA[$o][1]=$DBCC[$o][2];
$DBCCF[$o][2] = $DBCC[$o][3];
$DA[$o][2]=$DBCC[$o][3];
$MoyPuissance = (int)($pmax/$Iteration);
$DBCCF[$o][3] = $MoyPuissance;
$DA[$o][3]=$MoyPuissance;
$DBCCF[$o][4] = $MoyPuissance/1000;
$DA[$o][4]=$MoyPuissance/1000;
$csvj->fputcsv($DBCCF[$o], ';');
$lmax++;
}
$pmax = 0;
$o++;
}//boucle for
}
}
fclose($handle);
unset($DBCCF);
unset($DB);
unset($DBCC);
//RECUPERER VARIABLES CCJ
if (($ccj = fopen("../../../FICHIERS/ACC/$IDpmo/Sites/$IDsite/CC/CCN.$IDsite.csv", "r")) !== FALSE) {
$i=1;
while ($dat = fgetcsv($ccj, 100, ";")) {
if(isset($dat[0])){
$DA[$i][1] = $dat[0];
$DA[$i][2] = $dat[1];
$DA[$i][3] = $dat[2];
$DA[$i][4] = $dat[3];}
$i++;
}}
fclose($ccj);
//CREER VARIABLE DES JOURS $DBJ
$jour = fopen("../../../FICHIERS/ACC/CP.csv", "r");
$i=1;
while ($data0 = fgetcsv($jour, 100, ";")) {
$DBJ[$i][1] = $data0[0];
$DBJ[$i][2] = $data0[1];
$i++;
}
//DECALAGE DES TABLES AU BESOIN
$i=1;$o=1;
foreach($DBJ as $DB){
$D[$i][1]=$DBJ[$i][1];
$D[$i][2]=$DBJ[$i][2];
if( (!empty($DA[$i][1])) && (isset($DBJ[$i][1])) ){
if(($DA[$o][1]==$D[$i][1])&&($DA[$o][2]==$D[$i][2])){
$D[$i][3]=$DA[$i][3];
$D[$i][4]=$DA[$i][4];
}else{
$o--;
$D[$i][3]=$DA[$o][3];
$D[$i][4]=$DA[$o][4];
}
}else{
$D[$i][3]=0;
$D[$i][4]=0;
}
$i++;
$o++;
}
$csv = new SplFileObject('../../../FICHIERS/ACC/'.$IDpmo.'/Sites/'.$IDsite.'/CC/CCJ.'.$IDsite.'.csv', 'w');
$i=1;
$list2 = $dbco->prepare("SELECT * FROM gest_acc_sites S JOIN gest_acc_pmo_abos A ON S.ID_abo=A.ID_abo WHERE S.ID_acc_site='$IDsite'");
$list2->execute();
$R2 = $list2->fetch(PDO::FETCH_OBJ);
$MODstarthc = explode(":", $R2->abo_hc1_start); //00:00:00 recup
$starthc = intval($MODstarthc[0]); //heure du type 00
$MODstarthp = explode(":", $R2->abo_hc1_stop); //00:00:00 recup
$starthp = intval($MODstarthp[0]); //heure du type 00
echo "HCHP".$starthc." / ".$starthp." <br>";
foreach($D as $DBF){
if(($R2->abo_tempo == 1)&&($R2->abo_creneau == 0)){
$Tarifkwh = ($R2->abo_Thshp+$R2->abo_Tcee+$R2->abo_Tconsohshp);
$D[$i][5]=$Tarifkwh;
$cout = round(($DBF[4]*$Tarifkwh),2); //C5 sans HPHC
$D[$i][6]=$cout;}
if(($R2->abo_creneau == 1)&&($R2->abo_tempo == 4)){
echo "H ".$DBF[2]." / ".$i." <br>";
if($i<2208){
echo "Haute Saison <br>";
if(($DBF[2]>=$starthc)||($DBF[2]<$starthp)){//hc si 10>= 23 ou 10<7
$Tarifkwh = $R2->abo_Thshc+$R2->abo_Tcee+$R2->abo_Tconsohshc;
$cout = round(($DBF[4]*$Tarifkwh), 2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
echo "Heure creuse : ".$cout." <br>";
}else{
$Tarifkwh = ($R2->abo_Thshp+$R2->abo_Tcee+$R2->abo_Tconsohshp);
$cout = round(($DBF[4]*$Tarifkwh), 2);//ici 7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
echo "Heure pleine : ".$cout." <br>";
}
}
if(($i>2208)&&($i<7344)){
echo "Basse Saison <br>";
if(($DBF[2]>=$starthc)||($DBF[2]<$starthp)){//hc si 14>= 2 et 6<7
$Tarifkwh = $R2->abo_Tbshc+$R2->abo_Tcee+$R2->abo_Tconsobshc;
$cout = round(($DBF[4]*$Tarifkwh), 2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
echo "Heure creuse : ".$cout." <br>";
}else{
$Tarifkwh = ($R2->abo_Tbshp+$R2->abo_Tcee+$R2->abo_Tconsobshp);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
echo "Heure pleine : ".$cout." <br>";
}
}
if($i>7344){
echo "Haute Saison <br>";
if(($DBF[2]>=$starthc)||($DBF[2]<$starthp)){//hc si 14>= 2 et 6<7
$Tarifkwh = ($R2->abo_Thshc+$R2->abo_Tcee+$R2->abo_Tconsohshc);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
echo "Heure creuse : ".$cout." <br>";
}else{
$Tarifkwh = ($R2->abo_Thshp+$R2->abo_Tcee+$R2->abo_Tconsohshp);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
echo "Heure pleine : ".$cout." <br>";
}
}
}
if(($R2->abo_creneau == 2)&&($R2->abo_tempo == 4)){
$MODstarthc = explode(":", $R2->abo_hc1_start); //00:00:00 recup
$starthc = intval($MODstarthc[0]); //heure du type 00
$MODstarthp = explode(":", $R2->abo_hc1_stop); //00:00:00 recup
$starthp = intval($MODstarthp[0]); //heure du type 00
$MODstarthc2 = explode(":", $R2->abo_hc2_start); //00:00:00 recup
$starthc2 = intval($MODstarthc2[0]); //heure du type 00
$MODstarthp2 = explode(":", $R2->abo_hc2_stop); //00:00:00 recup
$starthp2 = intval($MODstarthp2[0]); //heure du type 00
if($i<2208){
if(($DBF[2]>=$starthc)&&($DBF[2]<$starthp)){//hc si 5>= 2 et 5<7
$Tarifkwh = ($R2->abo_Thshc+$R2->abo_Tcee+$R2->abo_Tconsohshc);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}elseif(($DBF[2]>=$starthc2)&&($DBF[2]<$starthp2)){
$Tarifkwh = ($R2->abo_Thshc+$R2->abo_Tcee+$R2->abo_Tconsohshc);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}else{
$Tarifkwh = ($R2->abo_Thshp+$R2->abo_Tcee+$R2->abo_Tconsohshp);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}
}
if(($i>2208)&&($i<7344)){
if(($DBF[2]>=$starthc)&&($DBF[2]<$starthp)){//hc si 5>= 2 et 5<7
$Tarifkwh = ($R2->abo_Tbshc+$R2->abo_Tcee+$R2->abo_Tconsobshc);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}elseif(($DBF[2]>=$starthc2)&&($DBF[2]<$starthp2)){
$Tarifkwh = ($R2->abo_Tbshc+$R2->abo_Tcee+$R2->abo_Tconsobshc);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}else{
$Tarifkwh = ($R2->abo_Tbshp+$R2->abo_Tcee+$R2->abo_Tconsobshp);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}
}
if($i>7344){
if(($DBF[2]>=$starthc)&&($DBF[2]<$starthp)){//hc si 5>= 2 et 5<7
$Tarifkwh = ($R2->abo_Thshc+$R2->abo_Tcee+$R2->abo_Tconsohshc);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}elseif(($DBF[2]>=$starthc2)&&($DBF[2]<$starthp2)){
$Tarifkwh = ($R2->abo_Thshc+$R2->abo_Tcee+$R2->abo_Tconsohshc);
$cout = round(($DBF[4]*$Tarifkwh),2);//ici 0-1-2-3-4-5-6 et 23
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}else{
$Tarifkwh = ($R2->abo_Thshp+$R2->abo_Tcee+$R2->abo_Tconsohshp);
$cout = round(($DBF[4]*$Tarifkwh),2);
$D[$i][5]=$Tarifkwh;
$D[$i][6]=$cout;
}
}
}
//echo "Ligne = ".$i." SIte ".$IDsite." / Jour ".$DBF[1]." / heure ".$DBF[2]." / conso ".$DBF[3]."<br>";
//Sur premiere base de données.
$UP3 = $dbco->prepare("INSERT INTO gest_acc_conso_heures (ID_acc_site, conso_jour, conso_heure, conso_puissance, conso_kwh, tarif_kwh, cout_kwh) VALUES (:ID_acc_site, :conso_jour, :conso_heure, :conso_puissance, :conso_kwh, :tarif_kwh, :cout_kwh)");
$UP3->bindparam(':ID_acc_site',$IDsite);
$UP3->bindparam(':conso_jour',$DBF[1]);
$UP3->bindparam(':conso_heure',$DBF[2]);
$UP3->bindparam(':conso_puissance',$DBF[3]);
$UP3->bindparam(':conso_kwh',$DBF[4]);
$UP3->bindparam(':tarif_kwh',$Tarifkwh);
$UP3->bindparam(':cout_kwh',$cout);
$UP3->execute();
//Sur deuxieme base de données.
$UP4 = $dbco1->prepare("INSERT INTO gest_acc_pmo_site_".$IDsite."_heures (jour, heure, conso_puissance, conso_kwh, tarif_kwh, conso_cout, cc_aci, cc_aci_kwh, cout_cc_aci, ac_aci, ac_aci_kwh, cout_ac_aci, inj_aci, inj_aci_kwh, conso_pct, autoconso_cor, autoconso_cor_kwh, autoconso_cor_cout)
VALUES (:jour, :heure, :conso_puissance, :conso_kwh, :tarif_kwh, :conso_cout, :cc_aci, :cc_aci_kwh, :cout_cc_aci, :ac_aci, :ac_aci_kwh, :cout_ac_aci, :inj_aci, :inj_aci_kwh, :conso_pct, :autoconso_cor, :autoconso_cor_kwh, :autoconso_cor_cout)");
$UP4->bindparam(':jour',$DBF[1]);
$UP4->bindparam(':heure',$DBF[2]);
$UP4->bindparam(':conso_puissance',$DBF[3]);
$UP4->bindparam(':conso_kwh',$DBF[4]);
$UP4->bindparam(':tarif_kwh',$Tarifkwh);
$UP4->bindparam(':conso_cout',$cout);
$UP4->bindvalue(':cc_aci','0');
$UP4->bindvalue(':cc_aci_kwh','0');
$UP4->bindvalue(':cout_cc_aci','0');
$UP4->bindvalue(':ac_aci','0');
$UP4->bindvalue(':ac_aci_kwh','0');
$UP4->bindvalue(':cout_ac_aci','0');
$UP4->bindvalue(':inj_aci','0');
$UP4->bindvalue(':inj_aci_kwh','0');
$UP4->bindvalue(':conso_pct','0');
$UP4->bindvalue(':autoconso_cor','0');
$UP4->bindvalue(':autoconso_cor_kwh','0');
$UP4->bindvalue(':autoconso_cor_cout','0');
$UP4->execute();
$csv->fputcsv($D[$i], ';');
$i++;
}
$req1 = $dbco->query("SELECT SUM(Conso_kwh) AS Consos_total FROM gest_acc_conso_heures WHERE ID_acc_site = '$IDsite' AND conso_jour LIKE '2024-%%-%%'");
$totalconsos = $req1->fetch();
$req1->closeCursor();
$UP6 = $dbco->prepare('UPDATE gest_acc_sites SET CC = :CC, Consos = :Consos, Etat = :Etat WHERE ID_acc_site = :ID_acc_site');
$UP6->bindvalue('CC','Oui');
$UP6->bindvalue('Consos',$totalconsos['Consos_total']);
$UP6->bindvalue('Etat','Complet');
$UP6->bindvalue('ID_acc_site',$IDsite);
$UP6->execute();
require('acc_calcul_journalier_CC.php');
require('acc_calcul_mois_CC.php');
header("location:../Gestion_ACC/acc_config_sites.php?IDpmo=$IDpmo");
?>