<? include('../functions.php');
/** Exports TOP 100 Prämienbesteller */
$del = '","'; // Delimiter
$year = 2012;
$start = mktime(0,0,0,1,1,$year);
$year = 2012;
$end = mktime(23,59,59,8,31,$year);
#print date('Y-m-d',$start) . "\n";
#print date('Y-m-d',$end) . "\n";
$query = "SELECT userId, count(userId) AS anzahl, abs(sum(wert)) AS summe FROM kontobewegungen
WHERE wert < 0
AND datum >= $start AND datum <= $end
AND beschreibung NOT LIKE '%Punkteverfall%'
group by userId
order by anzahl DESC";
#print $query . "\n";
$result = mysqli_query($dbhandle, $query) or die(mysql_error());
$i=0;
$oldvalue = 0;
while (($row = mysql_fetch_array($result,MYSQL_ASSOC)) && $i++ < 100 )
{
  $userId = $row['userId'];
  $anzahl = $row['anzahl'];
  $summe = $row['summe'];
//  if ($value != $oldvalue) $i++;
  $oldanzahl = $anzahl;
  $user = new user(0,$userId);
  if (! $user->userId)
      $user = new user(0,$userId,1);
  if ($user->userId)
  {
      print '"' . 
          $user->rechtskreis . $del .
          $user->versnummer . $del .
          $user->vorname . $del .
          $user->name . $del .
          $user->strasse . $del .
          $user->plz . $del .
          $user->ort . $del .
          date('Y-m-d',$user->erstellungsdatum) . $del .
          $anzahl . $del .
          $summe . 
          "\"\n";
  }
}
