#!/usr/bin/php54
<?php
require_once('../functions.php');

$month = date('m',time());
$year = date('Y',time());
$result = dbQuery("SELECT userId FROM users") or die(dbError());
while ($row = dbFetchRow($result))
{
    $userId = $row['userId'];
    if ($userId > 0)
    {

        $konto = new konto($userId);
        $bewegung = $konto->invalidatePoints("$year-$month-01");
    }
}


exit;

$result = dbQuery("SELECT userId FROM users") or die(dbError());
while ($row = dbFetchRow($result))
{
    $userId = $row['userId'];
    if ($userId > 0)
    {
        verfallen($userId);
    }
}

function verfallen($userId)
{
$konto = new konto($userId);
$firstentry = $konto->getFirstEntryDate();
$startmonth = date('m',$firstentry);
$startyear = date('Y',$firstentry)+2;
$thisyear = date('Y',time());
$thismonth = date('m',time());
for($year = $startyear; $year <= $thisyear;$year++)
{
    if ($year > $startyear) $startmonth = 1;
    for ($month = ($year > $startyear)?1:$startmonth ; $month <= (($year == $thisyear)?$thismonth:12); $month++)
    {
        $bewegung = $konto->invalidatePoints("$year-$month-1");
        if ($bewegung != null)
        {
            print_r($bewegung);
        }
    }
}
}
print_r($konto->calcinvalidatenext());
exit;

$todayyear = date('Y',time());
$todaymonth = date('m',time());
$todayday= date('d',time());
for ($year = 2010; $year <= 2010; $year++) {
  for ($month = 5; $month <= 5; $month++) {
    if ($year == $todayyear && $month > $todaymonth) {
      break;
    }
    $starttimestamp = mktime(0,0,0,$month,1,$year);
    $daysinmonth = date("t", $starttimestamp);
    print "Errechne Verfall für Jahr: $year, Monat: $month\n";
    $query = "SELECT userId FROM users WHERE erstellungsdatum <= $starttimestamp";
    $result = dbQuery($query) or error();
    print "Anzahl der Teilnehmer: " . dbNumRows($result) . "\n";
    $progress = new progress(dbNumRows($result));
    $i=0;
    while ($row = dbFetchRow($result,MYSQL_ASSOC)) {
        $i++;
        $progress->show($i);
        $konto = new konto($row['userId']);
        $konto->invalidatePoints("$year-$month-1");
        if ($konto->getSaldo() < 0) {
          die("UserId: " . $row['userId'] . " Saldo kleiner als 0: " . $konto->getSaldo() . "\n");
        }
    }
  }
}
