Handle artnr 4711 as special case items
[misc/hallinta-metro] / utils.php
index a9ae57e..1784acc 100644 (file)
--- a/utils.php
+++ b/utils.php
@@ -5,6 +5,18 @@ define('TEMPLATE', 'ordersatz.tex');
 define('FNAME_VARS', 'ordersatz_vars.tex');
 define('FNAME_ITEMS', 'ordersatz_items.tex');
 define('FAXNUMBER','2002-364');
+define('ERROR_SAFT','joey@finlandia');
+define('ERROR_MAIL','joey@finlandia.infodrom.org');
+
+function is_done($id)
+{
+  global $db;
+
+  $query = sprintf('SELECT count(*) AS count FROM metro_ordersatz WHERE id = %d AND done IS NULL', $id);
+  $sth = $db->query($query);
+  $row = $sth->fetch();
+  return $row['count'] == 0;
+}
 
 function mk_tempdir()
 {
@@ -22,11 +34,30 @@ function rm_tempdir($dir)
   system(sprintf("rm -rf %s", $dir));
 }
 
+function latexspecialchars($string)
+{
+  return preg_replace_callback("/([\^\%~\\\\#\$%&_\{\}])/",
+                              function($matches) {
+                                $map = array(
+                                             "#"=>"\\#",
+                                             "$"=>"\\$",
+                                             "%"=>"\\%",
+                                             "&"=>"\\&",
+                                             "~"=>"\\~{}",
+                                             "_"=>"\\_",
+                                             "^"=>"\\^{}",
+                                             "\\"=>"\\textbackslash",
+                                             "{"=>"\\{",
+                                             "}"=>"\\}",
+                                             );
+                                return $map[$matches[0]];
+                              },
+                              $string);
+}
+
 function latex_encode($text)
 {
-  return str_replace(array("\\",'%'),
-                    array('\\','\%'),
-                    utf8_decode($text));
+  return latexspecialchars(utf8_decode($text));
 }
 
 function ordersatz_archive($id, $title, $filename)
@@ -126,13 +157,13 @@ function ordersatz_write($id, $dir, $send)
   fwrite($f, sprintf('\newcommand{\datum}{%d.%d.%d}'."\n", $d[2], $d[1], $d[0]));
   fclose($f);
 
-  $sql = sprintf("SELECT artnr,gang,metro_vpe.short AS vpname,menge,bezeichnung,hersteller,anzahl " .
+  $sql = sprintf("SELECT artnr,gang,metro_vpe.short AS vpname,menge,bezeichnung,hinweis,hersteller,anzahl " .
                 "FROM metro_ordersatz_pos " .
                 "JOIN metro_artikel ON artikel = metro_artikel.id " .
                 "JOIN metro_abteilung ON abteilung = metro_abteilung.id " .
                 "JOIN metro_vpe ON vpe = metro_vpe.id " .
                 "WHERE ordersatz = %d AND anzahl IS NOT NULL " .
-                "ORDER BY gang,metro_abteilung.name",
+                "ORDER BY sort,gang,metro_abteilung.name,bezeichnung",
                 $id);
 
   $result = query_db($sql);
@@ -143,24 +174,36 @@ function ordersatz_write($id, $dir, $send)
   $f = fopen(FNAME_ITEMS, 'w');
 
   foreach ($result as $row) {
-    $bez = $row['bezeichnung'];
+    $bez = latex_encode($row['bezeichnung']);
     if (strlen($row['hersteller']))
-      $bez .= ' (' . $row['hersteller'] . ')';
+      $bez .= ' (' . latex_encode($row['hersteller']) . ')';
+    if (strlen($row['hinweis']))
+      $bez .= sprintf("\\newline\\textbf{%s}", latex_encode($row['hinweis']));
 
-    fwrite($f, sprintf('\order{%d}{%d}{%s %s}{%s}'."\n",
+    fwrite($f, sprintf('\order{%d}{%s}{%s %s}{%s}{%s}'."\n",
                       $row['anzahl'],
-                      $row['artnr'],
-                      $row['menge'],
-                      latex_encode($bez),
-                      latex_encode($row['vpname'])));
+                      $row['artnr'] ? (int)$row['artnr'] : '',
+                      latex_encode($row['menge']),
+                      $bez,
+                      latex_encode($row['vpname']),
+                      latex_encode($row['gang'])));
   }
 
   fclose($f);
 
-  system("latex ordersatz > ordersatz.out 2>&1 && dvips ordersatz >> ordersatz.out 2>&1 && ps2pdf ordersatz.ps", $ret);
+  system("latex ordersatz > ordersatz.out 2>&1 && dvips ordersatz >> ordersatz.out 2>&1 && pdflatex ordersatz >> ordersatz.out", $ret);
 
-  if ($ret !== 0)
+  if ($ret !== 0) {
+    $fname = sprintf("ordersatz-%d-%d.tar.bz2", $id, time());
+    $cmd = sprintf("tar cfj %s ordersatz.tex ordersatz_vars.tex ordersatz_items.tex ordersatz.out && " .
+                  "mpack -s 'Probleme beim Compilieren des Ordersatzes %d' -d ordersatz.out %s %s && " .
+                  "sendfile -Q -c='Probleme beim Compilieren des Ordersatzes %d' %s %s",
+                  $fname,
+                  $id, $fname, ERROR_MAIL,
+                  $id, $fname, ERROR_SAFT);
+    system($cmd);
     return "Probleme beim Compilieren des Ordersatzes";
+  }
 
   if (!file_exists('ordersatz.ps'))
     return "Postscript-Datei nicht erstellt";
@@ -171,6 +214,9 @@ function ordersatz_write($id, $dir, $send)
     if ($ret !== true)
       return $ret;
 
+    if (empty($satz[0]['email']))
+      $satz[0]['email'] = 'joey@infodrom.org';
+
     $cmd = sprintf("faxspool -f %s %s %s > /dev/null 2>&1",
                   $satz[0]['email'],
                   FAXNUMBER,
@@ -179,12 +225,12 @@ function ordersatz_write($id, $dir, $send)
 
     $message = sprintf("Hallo %s!\n\nDer angehängte Ordersatz wurde gerade an Metro Oldenburg verschickt.\n" .
                       "Eine Bestätigung über den Versand kommt per Mail.\n\n",
-                      $satz[0]['koch']);
+                      utf8_decode($satz[0]['koch']));
 
     mail_attach($satz[0]['email'],
-               'Ordersatz ' . $satz[0]['name'],
+               mb_encode_mimeheader('Ordersatz ' . $satz[0]['name'],'latin1'),
                $satz[0]['email'],
-               'Infodrom ' . $satz[0]['koch'],
+               mb_encode_mimeheader('Infodrom ' . $satz[0]['koch'],'latin1'),
                array($dir . '/ordersatz.pdf'),
                $message);
 
@@ -194,4 +240,4 @@ function ordersatz_write($id, $dir, $send)
   return true;
 }
 
-?>
\ No newline at end of file
+?>