<?php

include('../../functions.php');

$gesammelt = array();
$eingeloest = array();
$verfallen = array();

for ($year = 2009; $year < 2013; $year++)
{
    print $year . ";Gesammelt;Eingeloest;Verfallen\n";
    $users = getUsers($year);
    foreach ($users as $userId => $age)
    {
        $points = getPoints($year, $userId);

        if (isset($gesammelt[$age])) {
            $gesammelt[$age] += $points['gesammelt'];
        } else {
            $gesammelt[$age] = $points['gesammelt'];
        }

        if (isset($eingeloest[$age])) {
            $eingeloest[$age] += $points['eingeloest'];
        } else {
            $eingeloest[$age] = $points['eingeloest'];
        }

        if (isset($verfallen[$age])) {
            $verfallen[$age] += $points['verfallen'];
        } else {
            $verfallen[$age] = $points['verfallen'];
        }

    }
    for ($age = 0; $age < 100; $age++) {
        print "$age;";
        print ($gesammelt[$age] + 0) . ";";
        print ($eingeloest[$age] + 0) . ";";
        print ($verfallen[$age] + 0);
        print "\n";
    }
}


function getUsers($year)
{    
    $users = array();
    $startdatum = mktime(0,0,0,1,1,$year);
    $enddatum = mktime(0,0,0,1,1,$year+1);

    $query = "SELECT users.userId, geburtsdatum
        FROM users, kontobewegungen
        WHERE users.userId = kontobewegungen.userId
        AND datum >= $startdatum
        AND datum < $enddatum
        AND geburtsdatum > '1900-01-01'
        ORDER BY geburtsdatum DESC
        ";
    $result = dbQuery($query)
        or die (dbError());

    while ($row = dbFetchRow($result, MYSQL_ASSOC))
    {
        $userId = $row['userId'];
        $geburtsdatum = $row['geburtsdatum'];
        List($birthyear,$birthmonth,$day) = explode('-',$geburtsdatum);
        $age = $year - $birthyear;
        if ($birthyear <1900 or $birthyear > 2013) {
            print "Error. Geburtsdatum: $geburtsdatum, Alter: $age\n";
        }
        $users[$userId] = $age;
    }
    return $users;
}

function getPoints($year,$userId)
{
    $points = array();

    $startdatum = mktime(0,0,0,1,1,$year);
    $enddatum = mktime(0,0,0,1,1,$year+1);
    $query = "SELECT SUM(wert) AS wert
        FROM kontobewegungen
        WHERE wert < 0
        AND userId = $userId
        AND beschreibung NOT LIKE 'Punkteverfall%'
        AND datum >= $startdatum
        AND datum < $enddatum";
    $result = dbQuery($query) 
        or die (dbError());
    $row = dbFetchRow($result,MYSQL_ASSOC);
    $eingeloest = abs($row['wert']+0);

    $query = "SELECT SUM(wert) AS wert
        FROM kontobewegungen
        WHERE wert > 0
        AND userId = $userId
        AND datum >= $startdatum
        AND datum < $enddatum";
    $result = dbQuery($query) 
        or die (dbError());
    $row = dbFetchRow($result, MYSQL_ASSOC);
    $gesammelt = $row['wert'] + 0;

    $query = "SELECT SUM(wert) AS wert
        FROM kontobewegungen
        WHERE wert < 0
        AND userId = $userId
        AND beschreibung LIKE 'Punkteverfall%'
        AND datum >= $startdatum
        AND datum < $enddatum";
    $result = dbQuery($query) 
        or die (dbError());
    $row = dbFetchRow($result, MYSQL_ASSOC);
    $verfallen = abs($row['wert']+0);
   
    $points = array('gesammelt' => $gesammelt,
            'eingeloest' => $eingeloest,
            'verfallen' => $verfallen);
    return $points;
}

