<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once 'shared/conexionBD.php';

mysqli_query($conexion, "SET NAMES utf8");

/*$qry = "UPDATE ITT_ASW_SUDE SET VADI = '226.00' WHERE IDSU = '2023-07' AND TIPO = 'A'";
mysqli_query($conexion, $qry);*/

$totalElec = 0;
$qryTest = "SELECT * FROM ITT_ASW_SUDE WHERE TIPO = 'E' ORDER BY IDSU DESC";
$gstTest = mysqli_query($conexion, $qryTest);
$arrTest = array();
while ($test = mysqli_fetch_assoc($gstTest)) {
  $vadi = array_key_exists($test['IDSU'], $arrTest) ? floatval($arrTest[$test['IDSU']]['AHORRO']) : 0;
  $arrTest[$test['IDSU']] = [
    "FECHA" => $test['IDSU'],
    "AHORRO" => $vadi + floatval($test['VADI']),
  ];
}

$arrElec = array();
$cont = 0;
foreach($arrTest as $key=>$val){
  $arrDate = explode("-", $key);
  $currentMonth = date("m");
  $currentYear = date("Y");

  if($currentYear == $arrDate[0] && $currentMonth == $arrDate[1]) continue;
  if($currentYear == $arrDate[0] && intval($currentMonth) < intval($arrDate[1])) continue;
  if($cont < 12){
    var_dump($cont);
    echo "<br>";
    $arrElec[] = $val;
    $totalElec = $totalElec + floatval($val['AHORRO']);
  }

  $cont++;
}
/*var_dump($total);
echo "<br>";
var_dump($cont);
echo "<br>";*/

/*$valorMes = 5431 + 5 + 3980;
$diasMes = 31;
$valorDia = $valorMes / $diasMes;
$vadi = ceil($valorDia);
for($i = 1; $i <= $diasMes; $i++){
    $qry = "INSERT INTO ITT_ASW_SUDE VALUES ('2022-12', $vadi, $i, 'A')";
    mysqli_query($conexion, $qry);
    var_dump($qry);
    echo "<br>";
}*/

/*$qry = "DELETE FROM ITT_ASW_SUDE WHERE IDSU = '2022-11' AND TIPO = 'A'";
mysqli_query($conexion, $qry);*/