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


$fin = fopen('praemien.csv','r') or die('Kann pramien.csv nicht oeffnen');
while ($fields = fgetcsv($fin))
{
    // "pkey","praemienid","artikelnr","hmnr","geldwert","anzeige"
    $pkey = $fields[0];
    if ($pkey === 'pkey')
        continue;
    $id = $fields[1];
    $artikelnr = $fields[2];
    $hmnr = $fields[3];
    $geldwert = $fields[4];
    if ($geldwert == 0)
        continue;
    if ($artikelnr == '' && $hmnr == '')
        continue;
    if ($artikelnr == '')
        $where = "WHERE hmnr = $hmnr ";
    else 
    {
        $where = "WHERE artikelnr = $artikelnr ";
        if ($hmnr != '')
            $where .= "AND hmnr = $hmnr ";
    }


    $query = "UPDATE praemienartikel
        SET geldwert = $geldwert "
        .$where;
    print $query . "\n";
    mysql_query($query) or die(mysql_error());
}
fclose($fin);
exit;

  $query = "SELECT * FROM praemien, praemienartikel
    WHERE praemien.id= praemienartikel.praemienid
    AND praemien.status = 'ACTIVE'
    AND praemien.geldpraemie = 0";

  $result = mysql_query($query) or error ('Cannot select ' . $query);
  $first = true;
  while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
      if ($first) {
          foreach (array_keys($row) as $key) {
              print utf8_encode($key) . ';';
          }
          print "\n";
          $first = false;
      }

      foreach (array_keys($row) as $key) {
          $value = str_replace("\r",' ', 
                       str_replace("\n",' ',
                           utf8_encode($row[$key])
                       )
                   );
          $value = str_replace('"','',$value);
          print '"'. $value .'";';
      }
      print "\n";
  }


function repairbeschreibungen() {
    $query = 'SELECT id, name, beschreibung FROM praemien WHERE 1';
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $id = $row['id'];
        $name = $row['name'];
        $beschreibung = $row['beschreibung'];
        $name = html_entity_decode($name,ENT_QUOTES,'ISO-8859-15');
        $beschreibung = html_entity_decode($beschreibung,ENT_QUOTES,'ISO-8859-15');
        $subquery = "UPDATE praemien
                     SET name = '" . _mysqlescapestring($name) . "',
                         beschreibung = '" . _mysqlescapestring($beschreibung) . "'
                     WHERE id = $id";
        mysql_query($subquery) or error("Kann nicht updaten: $subquery");
    }
}
?>

