]> wagnertech.de Git - mfinanz.git/blobdiff - js/kivi.Order.js
Part: Nach einfügen von Zeile den Customer-Vendor-Picker clearen
[mfinanz.git] / js / kivi.Order.js
index 3e6853554354ee0dc62ab6eb10ee92ff681a069e..298a8b51b4a250b81c410e1aaae769e7eeb518e9 100644 (file)
@@ -1,12 +1,12 @@
 namespace('kivi.Order', function(ns) {
   ns.check_cv = function() {
 namespace('kivi.Order', function(ns) {
   ns.check_cv = function() {
-    if ($('#type').val() == 'sales_order') {
-      if ($('#order_customer_id').val() == '') {
+    if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
+      if ($('#order_customer_id').val() === '') {
         alert(kivi.t8('Please select a customer.'));
         return false;
       }
     } else  {
         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;
       }
         alert(kivi.t8('Please select a vendor.'));
         return false;
       }
@@ -14,20 +14,43 @@ namespace('kivi.Order', function(ns) {
     return true;
   };
 
     return true;
   };
 
-  ns.save = function() {
-    if (!ns.check_cv()) return;
+  ns.check_save_duplicate_parts = function() {
+    var id_arr = $('[name="order.orderitems[].parts_id"]').map(function() {return this.value;}).get();
 
 
-    var data = $('#order_form').serializeArray();
-    data.push({ name: 'action', value: 'Order/save' });
+    var i, obj = {}, pos = [];
 
 
-    $.post("controller.pl", data, kivi.eval_json_result);
+    for (i = 0; i < id_arr.length; i++) {
+      var id = id_arr[i];
+      if (obj.hasOwnProperty(id)) {
+        pos.push(i + 1);
+      }
+      obj[id] = 0;
+    }
+
+    if (pos.length > 0) {
+      return confirm(kivi.t8("There are duplicate parts at positions") + "\n"
+                     + pos.join(', ') + "\n"
+                     + kivi.t8("Do you really want to save?"));
+    }
+    return true;
   };
 
   };
 
-  ns.save_and_delivery_order = function() {
+  ns.check_valid_reqdate = function() {
+    if ($('#order_reqdate').val() === '') {
+      alert(kivi.t8('Please select a delivery date.'));
+      return false;
+    } else {
+      return true;
+    }
+  };
+
+  ns.save = function(action, warn_on_duplicates, warn_on_reqdate) {
     if (!ns.check_cv()) return;
     if (!ns.check_cv()) return;
+    if (warn_on_duplicates && !ns.check_save_duplicate_parts()) return;
+    if (warn_on_reqdate    && !ns.check_valid_reqdate()) return;
 
     var data = $('#order_form').serializeArray();
 
     var data = $('#order_form').serializeArray();
-    data.push({ name: 'action', value: 'Order/save_and_delivery_order' });
+    data.push({ name: 'action', value: 'Order/' + action });
 
     $.post("controller.pl", data, kivi.eval_json_result);
   };
 
     $.post("controller.pl", data, kivi.eval_json_result);
   };
@@ -81,16 +104,35 @@ namespace('kivi.Order', function(ns) {
 
   var email_dialog;
 
 
   var email_dialog;
 
+  ns.setup_send_email_dialog = function() {
+    kivi.SalesPurchase.show_all_print_options_elements();
+    kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
+
+    $('#print_options_form table').first().remove().appendTo('#email_form_print_options');
+
+    var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
+    $('#email_form_' + to_focus).focus();
+  };
+
+  ns.finish_send_email_dialog = function() {
+    kivi.SalesPurchase.show_all_print_options_elements();
+
+    $('#email_form_print_options table').first().remove().prependTo('#print_options_form');
+    return true;
+  };
+
   ns.show_email_dialog = function(html) {
   ns.show_email_dialog = function(html) {
-    var id            = 'jqueryui_popup_dialog';
+    var id            = 'send_email_dialog';
     var dialog_params = {
       id:     id,
       width:  800,
     var dialog_params = {
       id:     id,
       width:  800,
-      height: 500,
+      height: 600,
+      title:  kivi.t8('Send email'),
       modal:  true,
       modal:  true,
+      beforeClose: kivi.Order.finish_send_email_dialog,
       close: function(event, ui) {
         email_dialog.remove();
       close: function(event, ui) {
         email_dialog.remove();
-      },
+      }
     };
 
     $('#' + id).remove();
     };
 
     $('#' + id).remove();
@@ -99,6 +141,8 @@ namespace('kivi.Order', function(ns) {
     email_dialog.html(html);
     email_dialog.dialog(dialog_params);
 
     email_dialog.html(html);
     email_dialog.dialog(dialog_params);
 
+    kivi.Order.setup_send_email_dialog();
+
     $('.cancel').click(ns.close_email_dialog);
 
     return true;
     $('.cancel').click(ns.close_email_dialog);
 
     return true;
@@ -106,7 +150,8 @@ namespace('kivi.Order', function(ns) {
 
   ns.send_email = function() {
     var data = $('#order_form').serializeArray();
 
   ns.send_email = function() {
     var data = $('#order_form').serializeArray();
-    data = data.concat($('#email_form').serializeArray());
+    data = data.concat($('[name^="email_form."]').serializeArray());
+    data = data.concat($('[name^="print_options."]').serializeArray());
     data.push({ name: 'action', value: 'Order/send_email' });
     $.post("controller.pl", data, kivi.eval_json_result);
   };
     data.push({ name: 'action', value: 'Order/send_email' });
     $.post("controller.pl", data, kivi.eval_json_result);
   };
@@ -115,7 +160,11 @@ namespace('kivi.Order', function(ns) {
     email_dialog.dialog("close");
   };
 
     email_dialog.dialog("close");
   };
 
-  ns.reload_cv_dependend_selections = function() {
+  ns.set_number_in_title = function(elt) {
+    $('#nr_in_title').html($(elt).val());
+  };
+
+  ns.reload_cv_dependant_selections = function() {
     var data = $('#order_form').serializeArray();
     data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
 
     var data = $('#order_form').serializeArray();
     data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
 
@@ -159,6 +208,67 @@ namespace('kivi.Order', function(ns) {
     html_elt.html(price_str);
   };
 
     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', 0);
+    var elt = $(row).find('.expand');
+    elt.attr('src', "image/expand.svg");
+    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', 1);
+    var elt = $(row).find('.expand');
+    elt.attr('src', "image/collapse.svg");
+    elt.attr('alt', kivi.t8('Hide details'));
+    elt.attr('title', kivi.t8('Hide details'));
+  };
+
+  ns.toggle_second_row = function(row) {
+    if ($(row).data('expanded') == 1) {
+      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);
   ns.init_row_handlers = function() {
     kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
       $(elt).change(ns.recalc_amounts_and_taxes);
@@ -175,33 +285,48 @@ namespace('kivi.Order', function(ns) {
 
     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
       $(elt).keydown(function(event) {
 
     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
       $(elt).keydown(function(event) {
-        if(event.keyCode == 40 && event.shiftKey == true) {
+        if(event.keyCode == 40 && event.shiftKey === true) {
           // shift arrow down
           event.preventDefault();
           var row = $(event.target).parents(".row_entry").first();
           // shift arrow down
           event.preventDefault();
           var row = $(event.target).parents(".row_entry").first();
-          $(row).children().not(':first').show();
+          ns.load_second_row(row);
+          ns.show_second_row(row);
           return false;
         }
           return false;
         }
-        if(event.keyCode == 38 && event.shiftKey == true) {
+        if(event.keyCode == 38 && event.shiftKey === true) {
           // shift arrow up
           event.preventDefault();
           var row = $(event.target).parents(".row_entry").first();
           // shift arrow up
           event.preventDefault();
           var row = $(event.target).parents(".row_entry").first();
-          $(row).children().not(':first').hide();
+          ns.hide_second_row(row);
           return false;
         }
       });
           return false;
         }
       });
-      $(elt).dblclick(function(event) {
+    });
+
+    kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
+      $(elt).click(function(event) {
         event.preventDefault();
         var row = $(event.target).parents(".row_entry").first();
         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;
         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);
+      }
     });
   };
 
     });
   };
 
@@ -222,7 +347,7 @@ namespace('kivi.Order', function(ns) {
     } else {
       dir = "1";
       src = "image/down.png";
     } 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') + '">');
 
 
     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
 
@@ -245,7 +370,7 @@ namespace('kivi.Order', function(ns) {
   };
 
   ns.add_item = function() {
   };
 
   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();
     if (!ns.check_cv()) return;
 
     $('#row_table_id thead a img').remove();
@@ -335,6 +460,7 @@ namespace('kivi.Order', function(ns) {
   };
 
   ns.price_chooser_item_row = function(clicked) {
   };
 
   ns.price_chooser_item_row = function(clicked) {
+    if (!ns.check_cv()) return;
     var row = $(clicked).parents("tbody").first();
     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
 
     var row = $(clicked).parents("tbody").first();
     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
 
@@ -355,7 +481,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"]');
 
     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();
       // editable
       $(editable_div_elt).show();
       $(not_editable_div_elt).hide();
@@ -390,7 +516,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"]');
 
     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();
       // editable
       $(editable_div_elt).show();
       $(not_editable_div_elt).hide();
@@ -415,16 +541,118 @@ namespace('kivi.Order', function(ns) {
     kivi.io.close_dialog();
   };
 
     kivi.io.close_dialog();
   };
 
+  ns.show_periodic_invoices_config_dialog = function() {
+    if ($('#type').val() !== 'sales_order') return;
+
+    kivi.popup_dialog({
+      url: 'controller.pl?action=Order/show_periodic_invoices_config_dialog',
+      data: { type       : $('#type').val(),
+              id         : $('#id').val(),
+              config     : $('#order_periodic_invoices_config').val(),
+              customer_id: $('#order_customer_id').val(),
+              transdate  : $('#order_transdate').val(),
+              language_id: $('#language_id').val()
+            },
+      id: 'jq_periodic_invoices_config_dialog',
+      load: kivi.reinit_widgets,
+      dialog: {
+        title: kivi.t8('Edit the configuration for periodic invoices'),
+        width:  800,
+        height: 650
+      }
+    });
+    return true;
+  };
+
+  ns.close_periodic_invoices_config_dialog = function() {
+    $('#jq_periodic_invoices_config_dialog').dialog('close');
+  };
+
+  ns.assign_periodic_invoices_config = function() {
+    var data = $('[name="Form"]').serializeArray();
+    data.push({ name: 'type',   value: $('#type').val() });
+    data.push({ name: 'action', value: 'Order/assign_periodic_invoices_config' });
+    $.post("controller.pl", data, kivi.eval_json_result);
+  };
+
+  ns.check_save_active_periodic_invoices = function() {
+    var type = $('#type').val();
+    if (type !== 'sales_order') return true;
+
+    var active = false;
+    $.ajax({
+      url: 'controller.pl',
+      data: { action: 'Order/get_has_active_periodic_invoices',
+              type  : type,
+              id    : $('#id').val(),
+              config: $('#order_periodic_invoices_config').val(),
+      },
+      method: "GET",
+      async: false,
+      dataType: 'text',
+      success: function(val){
+        active = val;
+      }
+    });
+
+    if (active == 1) {
+      return confirm(kivi.t8('This sales order has an active configuration for periodic invoices. If you save then all subsequently created invoices will contain those changes as well, but not those that have already been created. Do you want to continue?'));
+    }
+
+    return true;
+  };
+
+  ns.show_vc_details_dialog = function() {
+    if (!ns.check_cv()) return;
+    var vc;
+    var vc_id;
+    var title;
+    if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
+      vc    = 'customer';
+      vc_id = $('#order_customer_id').val();
+      title = kivi.t8('Customer details');
+    } else {
+      vc    = 'vendor';
+      vc_id = $('#order_vendor_id').val();
+      title = kivi.t8('Vendor details');
+    }
+
+    kivi.popup_dialog({
+      url: 'controller.pl',
+      data: { action: 'Order/show_customer_vendor_details_dialog',
+              type  : $('#type').val(),
+              vc    : vc,
+              vc_id : vc_id
+            },
+      id: 'jq_customer_vendor_details_dialog',
+      dialog: {
+        title: title,
+        width:  800,
+        height: 650
+      }
+    });
+    return true;
+  };
+
+  ns.show_calculate_qty_dialog = function(clicked) {
+    var row = $(clicked).parents("tbody").first();
+    var input_id = $(row).find('[name="order.orderitems[].qty_as_number"]').attr('id');
+    var formula_id = $(row).find('[name="formula[+]"]').attr('id');
+
+    calculate_qty_selection_dialog("", input_id, "", formula_id);
+    return true;
+  }
+
 });
 
 $(function(){
 });
 
 $(function(){
-  if ($('#type').val() == 'sales_order') {
-    $('#order_customer_id').change(kivi.Order.reload_cv_dependend_selections);
+  if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
+    $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
   } else {
   } 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') {
+  if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
   } else {
     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
   } else {
     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
@@ -446,4 +674,28 @@ $(function(){
     $('#row_table_id thead a img').remove();
     kivi.Order.renumber_positions();
   });
     $('#row_table_id thead a img').remove();
     kivi.Order.renumber_positions();
   });
+
+  $('#expand_all').on('click', function(event) {
+    event.preventDefault();
+    if ($('#expand_all').data('expanded') == 1) {
+      $('#expand_all').data('expanded', 0);
+      $('#expand_all').attr('src', 'image/expand.svg');
+      $('#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', 1);
+      $('#expand_all').attr('src', "image/collapse.svg");
+      $('#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;
+  });
+
 });
 });