Adjust to new framework
[misc/hallinta-metro] / artikel.php
index 2bb5744..d7bcc76 100644 (file)
@@ -1,13 +1,12 @@
 <?php
 
 <?php
 
-$style[] = <<<EOC
+$styles->add("
 div#form_price {
   padding-left: 10px;
   padding-right: 10px;
   font-size: 90%;
   color: #555;
 div#form_price {
   padding-left: 10px;
   padding-right: 10px;
   font-size: 90%;
   color: #555;
-}
-EOC;
+}");
 
 $form = '
 <div id="form_price">
 
 $form = '
 <div id="form_price">
@@ -22,13 +21,70 @@ $form = '
 <label for="form_preis">Preis</label><br>
 <input id="form_preis" name="preis" size="10">
 <div class="buttons" style="margin-top: 15px;">
 <label for="form_preis">Preis</label><br>
 <input id="form_preis" name="preis" size="10">
 <div class="buttons" style="margin-top: 15px;">
-<button onclick="return add_price(this);">Hinzufügen</button>
+<button class="custom" onclick="return add_price(this);">Hinzufügen</button>
 </div>
 </form>
 </div>
 ';
 
 </div>
 </form>
 </div>
 ';
 
-$javascript = <<<EOC
+$jscode = <<<EOC
+var custom_this = false;
+function custom_save_callback(data)
+{
+    if (data.count > 0) {
+       var text = "Diese Artikelnummer ist bereits " + data.count + " mal vorhanden.\\nSoll der Artikel trotzdem gespeichert werden?";
+       if (confirm(text))
+           form_save(custom_this);
+    } else {
+       form_save(custom_this);
+    }
+}
+
+function custom_save(obj)
+{
+    custom_this = obj;
+    var id = document.getElementById('edit_id');
+
+    if (!id.value.length)
+       return custom_insert(obj);
+
+    var artnr = document.getElementById('edit_artnr');
+    if (artnr.value == 0) return form_save(custom_this);
+
+    var parms = 'source=' + source.innerHTML + '&callback=artnr';
+    parms += '&artnr=' + artnr.value;
+    parms += '&artikel=' + id.value;
+    ajax_request('function', parms, custom_save_callback);
+
+    return false;
+}
+
+function custom_insert_callback(data)
+{
+    if (data.count > 0) {
+       var text = "Diese Artikelnummer ist bereits " + data.count + " mal vorhanden.\\nSoll der Artikel trotzdem hinzugefügt werden?";
+       if (confirm(text))
+           form_insert(custom_this);
+    } else {
+       form_insert(custom_this);
+    }
+}
+
+function custom_insert(obj)
+{
+    custom_this = obj;
+    var artnr = document.getElementById('edit_artnr');
+
+    if (artnr.value == 0) return form_insert(custom_this);
+
+    var parms = 'source=' + source.innerHTML + '&callback=artnr';
+    parms += '&artnr=' + artnr.value;
+    ajax_request('function', parms, custom_insert_callback);
+
+    return false;
+}
+
 var price_popup = false;
 function price_popup_open()
 {
 var price_popup = false;
 function price_popup_open()
 {
@@ -104,16 +160,17 @@ function add_price(obj)
 
     return false;
 }
 
     return false;
 }
-
 EOC;
 
 EOC;
 
-$jscode[] = str_replace('CONTENT_FORM', str_replace(array("\n"),
-                                                   array("\\\n"), $form), $javascript);
-
+$jscode = str_replace('CONTENT_FORM', str_replace(array("\n"),
+                                                 array("\\\n"), $form), $jscode);
+$javascript->add($jscode);
+$javascript->onLoad("\$('button_save').onclick = function () {return custom_save(this);};");
+$javascript->onLoad("\$('button_insert').onclick = function () {return custom_insert(this);};");
 
 $buttons = <<<EOC
 <p style="margin-top: 5px; margin-bottom: 4px; text-align: center;">
 
 $buttons = <<<EOC
 <p style="margin-top: 5px; margin-bottom: 4px; text-align: center;">
-<button onclick="return open_popup()">Preis hinzufügen</button>
+<button class="custom" onclick="return open_popup()">Preis hinzufügen</button>
 </p>
 EOC;
 
 </p>
 EOC;
 
@@ -130,6 +187,8 @@ $mask = array(
                              'artnr' => array(
                                            'name' => 'ArtNr',
                                            'width' => 60,
                              'artnr' => array(
                                            'name' => 'ArtNr',
                                            'width' => 60,
+                                           'type' => 'number',
+                                           'specs' => "decPlaces: 0, thouSep: '', filterUI: 't='",
                                            ),
                              'menge' => array(
                                            'name' => 'Menge',
                                            ),
                              'menge' => array(
                                            'name' => 'Menge',
@@ -237,6 +296,10 @@ $mask = array(
                                            'size' => 21,
                                            'null' => true,
                                            ),
                                            'size' => 21,
                                            'null' => true,
                                            ),
+                             'removed' => array(
+                                           'name' => 'Artikel nicht mehr im Programm',
+                                           'type' => 'boolean',
+                                           ),
                              'buttons' => array(
                                            'type' => 'html',
                                            'code' => $buttons,
                              'buttons' => array(
                                            'type' => 'html',
                                            'code' => $buttons,
@@ -247,27 +310,32 @@ $mask = array(
                                'title' => 'Preisverlauf',
                                'rows' => 5,
                                'table' => 'metro_artikel_preis',
                                'title' => 'Preisverlauf',
                                'rows' => 5,
                                'table' => 'metro_artikel_preis',
+                               'table_edit' => 'metro_artikel_preis',
                                'where' => 'artikel = {id} ORDER BY datum DESC',
                                'list' => array(
                                                'id' => array(
                                                              'name' => 'ID',
                                'where' => 'artikel = {id} ORDER BY datum DESC',
                                'list' => array(
                                                'id' => array(
                                                              'name' => 'ID',
+                                                             'edit' => "Writeable:false, EntryType: 'H', Length: 4, isKey: true",
                                                              'visible' => false,
                                                              ),
                                                'datum' => array(
                                                              'name' => 'Datum',
                                                              'width' => 180,
                                                              'visible' => false,
                                                              ),
                                                'datum' => array(
                                                              'name' => 'Datum',
                                                              'width' => 180,
+                                                             'edit' => "EntryType: 'T', isNullable: false, Writeable: true",
                                                              ),
                                                'steuersatz' => array(
                                                              'name' => 'Steuer',
                                                              'width' => 60,
                                                              'type' => 'number',
                                                              'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
                                                              ),
                                                'steuersatz' => array(
                                                              'name' => 'Steuer',
                                                              'width' => 60,
                                                              'type' => 'number',
                                                              'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
+                                                             'edit' => "EntryType: 'T', isNullable: false, Writeable: true",
                                                              ),
                                                'preis' => array(
                                                              'name' => 'netto',
                                                              'width' => 60,
                                                              'type' => 'number',
                                                              'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
                                                              ),
                                                'preis' => array(
                                                              'name' => 'netto',
                                                              'width' => 60,
                                                              'type' => 'number',
                                                              'specs' => "decPlaces: 2, ClassName: 'alignrightpad'",
+                                                             'edit' => "EntryType: 'T', isNullable: false, Writeable: true",
                                                              ),
                                                'brutto' => array(
                                                              'name' => 'Preis',
                                                              ),
                                                'brutto' => array(
                                                              'name' => 'Preis',
@@ -285,6 +353,7 @@ $mask = array(
                                ),
              'callbacks' => array(
                                   'price' => cb_price,
                                ),
              'callbacks' => array(
                                   'price' => cb_price,
+                                  'artnr' => cb_artnr,
                                ),
              );
 
                                ),
              );
 
@@ -311,4 +380,21 @@ function cb_price()
   return true;
 }
 
   return true;
 }
 
+function cb_artnr()
+{
+  global $db;
+
+  if (empty($_POST['artnr']))
+    return array('error' => 'Nicht ausreichend Daten übermittelt');
+
+  $sql = sprintf("SELECT count(*) AS count FROM metro_artikel WHERE artnr = %d", $_POST['artnr']);
+  if (!empty($_POST['artikel']))
+    $sql .= sprintf(" AND id <> %d", $_POST['artikel']);
+
+  $sth = $db->query($sql);
+  $row = $sth->fetch();
+
+  return array('count' => $row['count']);
+}
+
 ?>
 ?>