Auftrags-Controller: Marge in zweiter Zeile
[kivitendo-erp.git] / js / kivi.Order.js
index d933def..c3673b4 100644 (file)
@@ -1,12 +1,12 @@
 namespace('kivi.Order', function(ns) {
   ns.check_cv = function() {
     if ($('#type').val() == 'sales_order') {
-      if ($('#order_customer_id').val() == '') {
+      if ($('#order_customer_id').val() === '') {
         alert(kivi.t8('Please select a customer.'));
         return false;
       }
     } else  {
-      if ($('#order_vendor_id').val() == '') {
+      if ($('#order_vendor_id').val() === '') {
         alert(kivi.t8('Please select a vendor.'));
         return false;
       }
@@ -138,7 +138,7 @@ namespace('kivi.Order', function(ns) {
     email_dialog.dialog("close");
   };
 
-  ns.reload_cv_dependend_selections = function() {
+  ns.reload_cv_dependant_selections = function() {
     var data = $('#order_form').serializeArray();
     data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
 
@@ -182,6 +182,67 @@ namespace('kivi.Order', function(ns) {
     html_elt.html(price_str);
   };
 
+  ns.load_second_row = function(row) {
+    var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
+    var div_elt = $(row).find('[name="second_row"]');
+
+    if ($(div_elt).data('loaded') == 1) {
+      return;
+    }
+    var data = $('#order_form').serializeArray();
+    data.push({ name: 'action', value: 'Order/load_second_rows' });
+    data.push({ name: 'item_ids[]', value: item_id_dom.val() });
+
+    $.post("controller.pl", data, kivi.eval_json_result);
+  };
+
+  ns.load_all_second_rows = function() {
+    var rows = $('.row_entry').filter(function(idx, elt) {
+      return $(elt).find('[name="second_row"]').data('loaded') != 1;
+    });
+
+    var item_ids = $.map(rows, function(elt) {
+      var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
+      return { name: 'item_ids[]', value: item_id };
+    });
+
+    if (item_ids.length == 0) {
+      return;
+    }
+
+    var data = $('#order_form').serializeArray();
+    data.push({ name: 'action', value: 'Order/load_second_rows' });
+    data = data.concat(item_ids);
+
+    $.post("controller.pl", data, kivi.eval_json_result);
+  };
+
+  ns.hide_second_row = function(row) {
+    $(row).children().not(':first').hide();
+    $(row).data('expanded', false);
+    var elt = $(row).find('.expand');
+    elt.attr('src', "image/expand3.gif");
+    elt.attr('alt', kivi.t8('Show details'));
+    elt.attr('title', kivi.t8('Show details'));
+  };
+
+  ns.show_second_row = function(row) {
+    $(row).children().not(':first').show();
+    $(row).data('expanded', true);
+    var elt = $(row).find('.expand');
+    elt.attr('src', "image/collapse3.gif");
+    elt.attr('alt', kivi.t8('Hide details'));
+    elt.attr('title', kivi.t8('Hide details'));
+  };
+
+  ns.toggle_second_row = function(row) {
+    if ($(row).data('expanded') === true) {
+      ns.hide_second_row(row);
+    } else {
+      ns.show_second_row(row);
+    }
+  };
+
   ns.init_row_handlers = function() {
     kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
       $(elt).change(ns.recalc_amounts_and_taxes);
@@ -198,34 +259,55 @@ namespace('kivi.Order', function(ns) {
 
     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
       $(elt).keydown(function(event) {
-        var row;
-        if(event.keyCode == 40 && event.shiftKey == true) {
+        if(event.keyCode == 40 && event.shiftKey === true) {
           // shift arrow down
           event.preventDefault();
-          row = $(event.target).parents(".row_entry").first();
-          $(row).children().not(':first').show();
+          var row = $(event.target).parents(".row_entry").first();
+          ns.load_second_row(row);
+          ns.show_second_row(row);
           return false;
         }
-        if(event.keyCode == 38 && event.shiftKey == true) {
+        if(event.keyCode == 38 && event.shiftKey === true) {
           // shift arrow up
           event.preventDefault();
-          row = $(event.target).parents(".row_entry").first();
-          $(row).children().not(':first').hide();
+          var row = $(event.target).parents(".row_entry").first();
+          ns.hide_second_row(row);
           return false;
         }
       });
       $(elt).dblclick(function(event) {
         event.preventDefault();
         var row = $(event.target).parents(".row_entry").first();
-        $(row).children().not(':first').toggle();
+        ns.load_second_row(row);
+        ns.toggle_second_row(row);
         return false;
       });
     });
+
+    kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
+      $(elt).click(function(event) {
+        event.preventDefault();
+        var row = $(event.target).parents(".row_entry").first();
+        ns.load_second_row(row);
+        ns.toggle_second_row(row);
+        return false;
+      })
+    });
+
   };
 
-  ns.redisplay_linetotals = function(data) {
-    $('.row_entry [name="linetotal"]').each(function(idx, elt) {
-      $(elt).html(data[idx]);
+  ns.redisplay_line_values = function(is_sales, data) {
+    $('.row_entry').each(function(idx, elt) {
+      $(elt).find('[name="linetotal"]').html(data[idx][0]);
+      if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
+        var mt = data[idx][1];
+        var mp = data[idx][2];
+        var h  = '<span';
+        if (mt[0] === '-') h += ' class="plus0"';
+        h += '>' + mt + '&nbsp;&nbsp;' + mp + '%';
+        h += '</span>';
+        $(elt).find('[name="linemargin"]').html(h);
+      }
     });
   };
 
@@ -246,7 +328,7 @@ namespace('kivi.Order', function(ns) {
     } else {
       dir = "1";
       src = "image/down.png";
-    };
+    }
 
     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
 
@@ -269,7 +351,7 @@ namespace('kivi.Order', function(ns) {
   };
 
   ns.add_item = function() {
-    if ($('#add_item_parts_id').val() == '') return;
+    if ($('#add_item_parts_id').val() === '') return;
     if (!ns.check_cv()) return;
 
     $('#row_table_id thead a img').remove();
@@ -379,7 +461,7 @@ namespace('kivi.Order', function(ns) {
 
     var editable_div_elt = $(row).find('[name="editable_price"]');
     var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
-    if (price_editable == 1 && source == '') {
+    if (price_editable == 1 && source === '') {
       // editable
       $(editable_div_elt).show();
       $(not_editable_div_elt).hide();
@@ -414,7 +496,7 @@ namespace('kivi.Order', function(ns) {
 
     var editable_div_elt = $(row).find('[name="editable_discount"]');
     var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
-    if (price_editable == 1 && source == '') {
+    if (price_editable == 1 && source === '') {
       // editable
       $(editable_div_elt).show();
       $(not_editable_div_elt).hide();
@@ -443,9 +525,9 @@ namespace('kivi.Order', function(ns) {
 
 $(function(){
   if ($('#type').val() == 'sales_order') {
-    $('#order_customer_id').change(kivi.Order.reload_cv_dependend_selections);
+    $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
   } else {
-    $('#order_vendor_id').change(kivi.Order.reload_cv_dependend_selections);
+    $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
   }
 
   if ($('#type').val() == 'sales_order') {
@@ -470,4 +552,27 @@ $(function(){
     $('#row_table_id thead a img').remove();
     kivi.Order.renumber_positions();
   });
+
+  $('#expand_all').on('click', function(event) {
+    event.preventDefault();
+    if ($('#expand_all').data('expanded') === true) {
+      $('#expand_all').data('expanded', false);
+      $('#expand_all').attr('src', 'image/expand3.gif');
+      $('#expand_all').attr('alt', kivi.t8('Show all details'));
+      $('#expand_all').attr('title', kivi.t8('Show all details'));
+      $('.row_entry').each(function(idx, elt) {
+        kivi.Order.hide_second_row(elt);
+      });
+    } else {
+      $('#expand_all').data('expanded', true);
+      $('#expand_all').attr('src', "image/collapse3.gif");
+      $('#expand_all').attr('alt', kivi.t8('Hide all details'));
+      $('#expand_all').attr('title', kivi.t8('Hide all details'));
+      kivi.Order.load_all_second_rows();
+      $('.row_entry').each(function(idx, elt) {
+        kivi.Order.show_second_row(elt);
+      });
+    }
+    return false;
+  });
 });