Allow importing of new articles
[misc/hallinta-metro] / ordersatz_pos.php
index 83bd4d1..c7002aa 100644 (file)
@@ -73,10 +73,34 @@ function finish()
   return false;
 }
 
+function import_callback(data)
+{
+  if (data.status == true) {
+    info('Ordersatz aktualisiert');
+
+    if (data.count > 0)
+      grid_update(grid);
+
+    confirm(data.count + ' Artikel zum Ordersatz hinzugefügt.');
+  }
+}
+
+function import()
+{
+  var source = document.getElementById('source');
+  if (!source) return false;
+
+  var parms = 'source=' + source.innerHTML + '&callback=import';
+  ajax_request('function', parms, import_callback);
+  return false;
+}
+
 EOC;
 
 $buttons = <<<EOC
 <p style="margin-top: 0px; margin-bottom: 4px; text-align: center;">
+<button onclick="return import()">Artikel importieren</button>
+&nbsp;
 <button onclick="return build_pdf()">PDF erstellen</button>
 &nbsp;
 <button onclick="return finish()">Abschließen</button>
@@ -177,6 +201,7 @@ $mask = array(
                                   'cellsave' => cb_cellsave,
                                   'pdf' => cb_pdf,
                                   'send' => cb_send,
+                                  'import' => cb_import,
                                ),
              'files' => array(
                               'download' => process_download,
@@ -243,4 +268,32 @@ function cb_send()
   return array('status' => true);
 }
 
+function cb_import()
+{
+  global $db;
+
+  $sql = sprintf("SELECT count(*) AS count FROM metro_ordersatz_pos WHERE ordersatz = %d",
+                $_SESSION['ordersatz']);
+  $sth = $db->query($sql);
+  $row = $sth->fetch();
+  $old = $row['count'];
+
+  $sql = sprintf("INSERT INTO metro_ordersatz_pos (ordersatz,artikel,sys_user,sys_edit) " .
+                "SELECT %d,metro_artikel.id,%s,now() FROM metro_artikel " .
+                "LEFT JOIN metro_ordersatz_pos ON artikel = metro_artikel.id AND ordersatz = %d " .
+                "WHERE metro_ordersatz_pos.id IS NULL",
+                $_SESSION['ordersatz'],
+                $db->quote($_SESSION['sys']['login']),
+                $_SESSION['ordersatz']);
+  $db->query($sql);
+
+  $sql = sprintf("SELECT count(*) AS count FROM metro_ordersatz_pos WHERE ordersatz = %d",
+                $_SESSION['ordersatz']);
+  $sth = $db->query($sql);
+  $row = $sth->fetch();
+  $new = $row['count'];
+
+  return array('status' => true, 'count' => $new - $old);
+}
+
 ?>