1 namespace('kivi.Order', function(ns) {
 
   2   ns.check_cv = function() {
 
   3     if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation') {
 
   4       if ($('#order_customer_id').val() === '') {
 
   5         alert(kivi.t8('Please select a customer.'));
 
   9       if ($('#order_vendor_id').val() === '') {
 
  10         alert(kivi.t8('Please select a vendor.'));
 
  17   ns.check_duplicate_parts = function(question) {
 
  18     var id_arr = $('[name="order.orderitems[].parts_id"]').map(function() { return this.value; }).get();
 
  20     var i, obj = {}, pos = [];
 
  22     for (i = 0; i < id_arr.length; i++) {
 
  24       if (obj.hasOwnProperty(id)) {
 
  31       question = question || kivi.t8("Do you really want to save?");
 
  32       return confirm(kivi.t8("There are duplicate parts at positions") + "\n"
 
  33                      + pos.join(', ') + "\n"
 
  39   ns.check_valid_reqdate = function() {
 
  40     if ($('#order_reqdate_as_date').val() === '') {
 
  41       alert(kivi.t8('Please select a delivery date.'));
 
  48   ns.save = function(action, warn_on_duplicates, warn_on_reqdate) {
 
  49     if (!ns.check_cv()) return;
 
  50     if (warn_on_duplicates && !ns.check_duplicate_parts()) return;
 
  51     if (warn_on_reqdate    && !ns.check_valid_reqdate())   return;
 
  53     var data = $('#order_form').serializeArray();
 
  54     data.push({ name: 'order.language_id', value: $('#language_id').val() }); // language from print options
 
  55     data.push({ name: 'action', value: 'Order/' + action });
 
  57     $.post("controller.pl", data, kivi.eval_json_result);
 
  60   ns.delete_order = function() {
 
  61     var data = $('#order_form').serializeArray();
 
  62     data.push({ name: 'action', value: 'Order/delete' });
 
  64     $.post("controller.pl", data, kivi.eval_json_result);
 
  67   ns.show_print_options = function(warn_on_duplicates) {
 
  68     if (!ns.check_cv()) return;
 
  69     if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to print?"))) return;
 
  74         title: kivi.t8('Print options'),
 
  81   ns.print = function() {
 
  82     $('#print_options').dialog('close');
 
  84     var data = $('#order_form').serializeArray();
 
  85     data = data.concat($('#print_options_form').serializeArray());
 
  86     data.push({ name: 'order.language_id', value: $('#language_id').val() }); // language from print options
 
  87     data.push({ name: 'action', value: 'Order/print' });
 
  89     $.post("controller.pl", data, kivi.eval_json_result);
 
  92   ns.email = function(warn_on_duplicates) {
 
  93     if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to send by mail?"))) return;
 
  94     if (!ns.check_cv()) return;
 
  96     var data = $('#order_form').serializeArray();
 
  97     data.push({ name: 'action', value: 'Order/show_email_dialog' });
 
  99     $.post("controller.pl", data, kivi.eval_json_result);
 
 104   ns.setup_send_email_dialog = function() {
 
 105     kivi.SalesPurchase.show_all_print_options_elements();
 
 106     kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
 
 108     $('#print_options_form table').first().remove().appendTo('#email_form_print_options');
 
 110     var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
 
 111     $('#email_form_' + to_focus).focus();
 
 114   ns.finish_send_email_dialog = function() {
 
 115     kivi.SalesPurchase.show_all_print_options_elements();
 
 117     $('#email_form_print_options table').first().remove().prependTo('#print_options_form');
 
 121   ns.show_email_dialog = function(html) {
 
 122     var id            = 'send_email_dialog';
 
 123     var dialog_params = {
 
 127       title:  kivi.t8('Send email'),
 
 129       beforeClose: kivi.Order.finish_send_email_dialog,
 
 130       close: function(event, ui) {
 
 131         email_dialog.remove();
 
 135     $('#' + id).remove();
 
 137     email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
 
 138     email_dialog.html(html);
 
 139     email_dialog.dialog(dialog_params);
 
 141     kivi.Order.setup_send_email_dialog();
 
 143     $('.cancel').click(ns.close_email_dialog);
 
 148   ns.send_email = function() {
 
 149     var data = $('#order_form').serializeArray();
 
 150     data = data.concat($('[name^="email_form."]').serializeArray());
 
 151     data = data.concat($('[name^="print_options."]').serializeArray());
 
 152     data.push({ name: 'order.language_id', value: $('#language_id').val() }); // language from print options
 
 153     data.push({ name: 'action', value: 'Order/send_email' });
 
 154     $.post("controller.pl", data, kivi.eval_json_result);
 
 157   ns.close_email_dialog = function() {
 
 158     email_dialog.dialog("close");
 
 161   ns.set_number_in_title = function(elt) {
 
 162     $('#nr_in_title').html($(elt).val());
 
 165   ns.reload_cv_dependant_selections = function() {
 
 166     var data = $('#order_form').serializeArray();
 
 167     data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
 
 169     $.post("controller.pl", data, kivi.eval_json_result);
 
 172   ns.reformat_number = function(event) {
 
 173     $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
 
 176   ns.recalc_amounts_and_taxes = function() {
 
 177     var data = $('#order_form').serializeArray();
 
 178     data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
 
 180     $.post("controller.pl", data, kivi.eval_json_result);
 
 183   ns.unit_change = function(event) {
 
 184     var row           = $(event.target).parents("tbody").first();
 
 185     var item_id_dom   = $(row).find('[name="orderitem_ids[+]"]');
 
 186     var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
 
 187     var select_elt    = $(row).find('[name="order.orderitems[].unit"]');
 
 189     var oldval = $(select_elt).data('oldval');
 
 190     $(select_elt).data('oldval', $(select_elt).val());
 
 192     var data = $('#order_form').serializeArray();
 
 193     data.push({ name: 'action',           value: 'Order/unit_changed'     },
 
 194               { name: 'item_id',          value: item_id_dom.val()        },
 
 195               { name: 'old_unit',         value: oldval                   },
 
 196               { name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
 
 198     $.post("controller.pl", data, kivi.eval_json_result);
 
 201   ns.update_sellprice = function(item_id, price_str) {
 
 202     var row       = $('#item_' + item_id).parents("tbody").first();
 
 203     var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
 
 204     var html_elt  = $(row).find('[name="sellprice_text"]');
 
 205     price_elt.val(price_str);
 
 206     html_elt.html(price_str);
 
 209   ns.load_second_row = function(row) {
 
 210     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
 
 211     var div_elt     = $(row).find('[name="second_row"]');
 
 213     if ($(div_elt).data('loaded') == 1) {
 
 216     var data = $('#order_form').serializeArray();
 
 217     data.push({ name: 'action',     value: 'Order/load_second_rows' },
 
 218               { name: 'item_ids[]', value: item_id_dom.val()        });
 
 220     $.post("controller.pl", data, kivi.eval_json_result);
 
 223   ns.load_all_second_rows = function() {
 
 224     var rows = $('.row_entry').filter(function(idx, elt) {
 
 225       return $(elt).find('[name="second_row"]').data('loaded') != 1;
 
 228     var item_ids = $.map(rows, function(elt) {
 
 229       var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
 
 230       return { name: 'item_ids[]', value: item_id };
 
 233     if (item_ids.length == 0) {
 
 237     var data = $('#order_form').serializeArray();
 
 238     data.push({ name: 'action', value: 'Order/load_second_rows' });
 
 239     data = data.concat(item_ids);
 
 241     $.post("controller.pl", data, kivi.eval_json_result);
 
 244   ns.hide_second_row = function(row) {
 
 245     $(row).children().not(':first').hide();
 
 246     $(row).data('expanded', 0);
 
 247     var elt = $(row).find('.expand');
 
 248     elt.attr('src', "image/expand.svg");
 
 249     elt.attr('alt', kivi.t8('Show details'));
 
 250     elt.attr('title', kivi.t8('Show details'));
 
 253   ns.show_second_row = function(row) {
 
 254     $(row).children().not(':first').show();
 
 255     $(row).data('expanded', 1);
 
 256     var elt = $(row).find('.expand');
 
 257     elt.attr('src', "image/collapse.svg");
 
 258     elt.attr('alt', kivi.t8('Hide details'));
 
 259     elt.attr('title', kivi.t8('Hide details'));
 
 262   ns.toggle_second_row = function(row) {
 
 263     if ($(row).data('expanded') == 1) {
 
 264       ns.hide_second_row(row);
 
 266       ns.show_second_row(row);
 
 270   ns.init_row_handlers = function() {
 
 271     kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
 
 272       $(elt).change(ns.recalc_amounts_and_taxes);
 
 275     kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
 
 276       $(elt).change(ns.reformat_number);
 
 279     kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
 
 280       $(elt).data('oldval', $(elt).val());
 
 281       $(elt).change(ns.unit_change);
 
 284     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
 
 285       $(elt).keydown(function(event) {
 
 287         if (event.keyCode == 40 && event.shiftKey === true) {
 
 289           event.preventDefault();
 
 290           row = $(event.target).parents(".row_entry").first();
 
 291           ns.load_second_row(row);
 
 292           ns.show_second_row(row);
 
 295         if (event.keyCode == 38 && event.shiftKey === true) {
 
 297           event.preventDefault();
 
 298           row = $(event.target).parents(".row_entry").first();
 
 299           ns.hide_second_row(row);
 
 305     kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
 
 306       $(elt).click(function(event) {
 
 307         event.preventDefault();
 
 308         var row = $(event.target).parents(".row_entry").first();
 
 309         ns.load_second_row(row);
 
 310         ns.toggle_second_row(row);
 
 317   ns.redisplay_line_values = function(is_sales, data) {
 
 318     $('.row_entry').each(function(idx, elt) {
 
 319       $(elt).find('[name="linetotal"]').html(data[idx][0]);
 
 320       if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
 
 321         var mt = data[idx][1];
 
 322         var mp = data[idx][2];
 
 324         if (mt[0] === '-') h += ' class="plus0"';
 
 325         h += '>' + mt + '  ' + mp + '%';
 
 327         $(elt).find('[name="linemargin"]').html(h);
 
 332   ns.redisplay_cvpartnumbers = function(data) {
 
 333     $('.row_entry').each(function(idx, elt) {
 
 334       $(elt).find('[name="cvpartnumber"]').html(data[idx][0]);
 
 338   ns.renumber_positions = function() {
 
 339     $('.row_entry [name="position"]').each(function(idx, elt) {
 
 344   ns.reorder_items = function(order_by) {
 
 345     var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
 
 346     $('#row_table_id thead a img').remove();
 
 351       src = "image/up.png";
 
 354       src = "image/down.png";
 
 357     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
 
 359     var data = $('#order_form').serializeArray();
 
 360     data.push({ name: 'action',   value: 'Order/reorder_items' },
 
 361               { name: 'order_by', value: order_by              },
 
 362               { name: 'sort_dir', value: dir                   });
 
 364     $.post("controller.pl", data, kivi.eval_json_result);
 
 367   ns.redisplay_items = function(data) {
 
 368     var old_rows = $('.row_entry').detach();
 
 370     $(data).each(function(idx, elt) {
 
 371       new_rows.push(old_rows[elt.old_pos - 1]);
 
 373     $(new_rows).appendTo($('#row_table_id'));
 
 374     ns.renumber_positions();
 
 377   ns.add_item = function() {
 
 378     if ($('#add_item_parts_id').val() === '') return;
 
 379     if (!ns.check_cv()) return;
 
 381     $('#row_table_id thead a img').remove();
 
 383     var data = $('#order_form').serializeArray();
 
 384     data.push({ name: 'action', value: 'Order/add_item' });
 
 386     $.post("controller.pl", data, kivi.eval_json_result);
 
 389   ns.setup_multi_items_dialog = function() {
 
 390     $('#multi_items_filter_table input, #multi_items_filter_table select').keydown(function(event) {
 
 391       if (event.keyCode == 13) {
 
 392         event.preventDefault();
 
 393         ns.multi_items_dialog_update_result();
 
 398     $('#multi_items_filter_all_substr_multi_ilike').focus();
 
 401   ns.show_multi_items_dialog = function() {
 
 402     if (!ns.check_cv()) return;
 
 404     $('#row_table_id thead a img').remove();
 
 407       url:    'controller.pl?action=Order/show_multi_items_dialog',
 
 408       data:   { type: $('#type').val() },
 
 409       id:     'jq_multi_items_dialog',
 
 410       load:   kivi.Order.setup_multi_items_dialog,
 
 412         title:  kivi.t8('Add multiple items'),
 
 420   ns.close_multi_items_dialog = function() {
 
 421     $('#jq_multi_items_dialog').dialog('close');
 
 424   ns.multi_items_dialog_update_result = function() {
 
 425     var data = $('#multi_items_form').serializeArray();
 
 426     data.push({ name: 'type', value: $('#type').val() });
 
 428       url:     'controller.pl?action=Order/multi_items_update_result',
 
 431       success: function(data) {
 
 432         $('#multi_items_result').html(data);
 
 433         ns.multi_items_dialog_enable_continue();
 
 434         ns.multi_items_result_setup_events();
 
 439   ns.multi_items_dialog_disable_continue = function() {
 
 440     // disable keydown-event and continue button to prevent
 
 441     // impatient users to add parts multiple times
 
 442     $('#multi_items_result input').off("keydown");
 
 443     $('#multi_items_dialog_continue_button').prop('disabled', true);
 
 446   ns.multi_items_dialog_enable_continue = function()  {
 
 447     $('#multi_items_result input').keydown(function(event) {
 
 448       if(event.keyCode == 13) {
 
 449         event.preventDefault();
 
 450         ns.add_multi_items();
 
 454     $('#multi_items_dialog_continue_button').prop('disabled', false);
 
 457   ns.multi_items_result_setup_events = function() {
 
 458     $('#multi_items_all_qty').change(ns.reformat_number);
 
 459     $('#multi_items_all_qty').change(function(event) {
 
 460       $('.multi_items_qty').val($(event.target).val());
 
 462     $('.multi_items_qty').change(ns.reformat_number);
 
 465   ns.add_multi_items = function() {
 
 467     var n_rows = $('.multi_items_qty').length;
 
 468     if (n_rows == 0) return;
 
 471     n_rows = $('.multi_items_qty').filter(function() {
 
 472       return $(this).val().length > 0;
 
 474     if (n_rows == 0) return;
 
 476     ns.multi_items_dialog_disable_continue();
 
 478     var data = $('#order_form').serializeArray();
 
 479     data = data.concat($('#multi_items_form').serializeArray());
 
 480     data.push({ name: 'action', value: 'Order/add_multi_items' });
 
 481     $.post("controller.pl", data, kivi.eval_json_result);
 
 484   ns.set_input_to_one = function(clicked) {
 
 485     if ($(clicked).val() == '') {
 
 486       $(clicked).val(kivi.format_amount(1.00, -2));
 
 491   ns.delete_order_item_row = function(clicked) {
 
 492     var row = $(clicked).parents("tbody").first();
 
 495     ns.renumber_positions();
 
 496     ns.recalc_amounts_and_taxes();
 
 499   ns.row_table_scroll_down = function() {
 
 500     $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
 
 503   ns.show_longdescription_dialog = function(clicked) {
 
 504     var row                 = $(clicked).parents("tbody").first();
 
 505     var position            = $(row).find('[name="position"]').html();
 
 506     var partnumber          = $(row).find('[name="partnumber"]').html();
 
 507     var description_elt     = $(row).find('[name="order.orderitems[].description"]');
 
 508     var description         = description_elt.val();
 
 509     var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
 
 512     if (!longdescription_elt.length) {
 
 514         { name: 'action',   value: 'Order/get_item_longdescription'                          },
 
 515         { name: 'type',     value: $('#type').val()                                          },
 
 516         { name: 'item_id',  value: $(row).find('[name="order.orderitems[+].id"]').val()      },
 
 517         { name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() }
 
 521         url:      'controller.pl',
 
 526         success:  function(val) {
 
 527           longdescription = val;
 
 531       longdescription = longdescription_elt.val();
 
 535       runningnumber:           position,
 
 536       partnumber:              partnumber,
 
 537       description:             description,
 
 538       default_longdescription: longdescription,
 
 539       set_function:            function(val) {
 
 540         longdescription_elt.remove();
 
 541         $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
 
 545     kivi.SalesPurchase.edit_longdescription_with_params(params);
 
 548   ns.price_chooser_item_row = function(clicked) {
 
 549     if (!ns.check_cv()) return;
 
 550     var row         = $(clicked).parents("tbody").first();
 
 551     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
 
 553     var data = $('#order_form').serializeArray();
 
 554     data.push({ name: 'action',  value: 'Order/price_popup' },
 
 555               { name: 'item_id', value: item_id_dom.val()   });
 
 557     $.post("controller.pl", data, kivi.eval_json_result);
 
 560   ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
 
 561     var row        = $('#item_' + item_id).parents("tbody").first();
 
 562     var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
 
 563     var button_elt = $(row).find('[name="price_chooser_button"]');
 
 565     button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
 
 566     source_elt.val(source);
 
 568     var editable_div_elt     = $(row).find('[name="editable_price"]');
 
 569     var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
 
 570     if (price_editable == 1 && source === '') {
 
 572       $(editable_div_elt).show();
 
 573       $(not_editable_div_elt).hide();
 
 574       $(editable_div_elt).find(':input').prop("disabled", false);
 
 575       $(not_editable_div_elt).find(':input').prop("disabled", true);
 
 578       $(editable_div_elt).hide();
 
 579       $(not_editable_div_elt).show();
 
 580       $(editable_div_elt).find(':input').prop("disabled", true);
 
 581       $(not_editable_div_elt).find(':input').prop("disabled", false);
 
 585       var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
 
 586       var html_elt  = $(row).find('[name="sellprice_text"]');
 
 587       price_elt.val(price_str);
 
 588       html_elt.html(price_str);
 
 589       ns.recalc_amounts_and_taxes();
 
 592     kivi.io.close_dialog();
 
 595   ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
 
 596     var row        = $('#item_' + item_id).parents("tbody").first();
 
 597     var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
 
 598     var button_elt = $(row).find('[name="price_chooser_button"]');
 
 600     button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
 
 601     source_elt.val(source);
 
 603     var editable_div_elt     = $(row).find('[name="editable_discount"]');
 
 604     var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
 
 605     if (price_editable == 1 && source === '') {
 
 607       $(editable_div_elt).show();
 
 608       $(not_editable_div_elt).hide();
 
 609       $(editable_div_elt).find(':input').prop("disabled", false);
 
 610       $(not_editable_div_elt).find(':input').prop("disabled", true);
 
 613       $(editable_div_elt).hide();
 
 614       $(not_editable_div_elt).show();
 
 615       $(editable_div_elt).find(':input').prop("disabled", true);
 
 616       $(not_editable_div_elt).find(':input').prop("disabled", false);
 
 620       var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
 
 621       var html_elt     = $(row).find('[name="discount_text"]');
 
 622       discount_elt.val(discount_str);
 
 623       html_elt.html(discount_str);
 
 624       ns.recalc_amounts_and_taxes();
 
 627     kivi.io.close_dialog();
 
 630   ns.show_periodic_invoices_config_dialog = function() {
 
 631     if ($('#type').val() !== 'sales_order') return;
 
 634       url:    'controller.pl?action=Order/show_periodic_invoices_config_dialog',
 
 635       data:   { type:              $('#type').val(),
 
 637                 config:            $('#order_periodic_invoices_config').val(),
 
 638                 customer_id:       $('#order_customer_id').val(),
 
 639                 transdate_as_date: $('#order_transdate_as_date').val(),
 
 640                 language_id:       $('#language_id').val()
 
 642       id:     'jq_periodic_invoices_config_dialog',
 
 643       load:   kivi.reinit_widgets,
 
 645         title:  kivi.t8('Edit the configuration for periodic invoices'),
 
 653   ns.close_periodic_invoices_config_dialog = function() {
 
 654     $('#jq_periodic_invoices_config_dialog').dialog('close');
 
 657   ns.assign_periodic_invoices_config = function() {
 
 658     var data = $('[name="Form"]').serializeArray();
 
 659     data.push({ name: 'type',   value: $('#type').val() },
 
 660               { name: 'action', value: 'Order/assign_periodic_invoices_config' });
 
 661     $.post("controller.pl", data, kivi.eval_json_result);
 
 664   ns.check_save_active_periodic_invoices = function() {
 
 665     var type = $('#type').val();
 
 666     if (type !== 'sales_order') return true;
 
 670       url:      'controller.pl',
 
 671       data:     { action: 'Order/get_has_active_periodic_invoices',
 
 674                   config: $('#order_periodic_invoices_config').val(),
 
 679       success:  function(val) {
 
 685       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?'));
 
 691   ns.show_vc_details_dialog = function() {
 
 692     if (!ns.check_cv()) return;
 
 696     if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
 
 698       vc_id = $('#order_customer_id').val();
 
 699       title = kivi.t8('Customer details');
 
 702       vc_id = $('#order_vendor_id').val();
 
 703       title = kivi.t8('Vendor details');
 
 707       url:    'controller.pl',
 
 708       data:   { action: 'Order/show_customer_vendor_details_dialog',
 
 709                 type  : $('#type').val(),
 
 713       id:     'jq_customer_vendor_details_dialog',
 
 723   ns.update_row_from_master_data = function(clicked) {
 
 724     var row = $(clicked).parents("tbody").first();
 
 725     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
 
 727     var data = $('#order_form').serializeArray();
 
 728     data.push({ name: 'action', value: 'Order/update_row_from_master_data' });
 
 729     data.push({ name: 'item_ids[]', value: item_id_dom.val() });
 
 731     $.post("controller.pl", data, kivi.eval_json_result);
 
 734   ns.update_all_rows_from_master_data = function() {
 
 735     var item_ids = $.map($('.row_entry'), function(elt) {
 
 736       var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
 
 737       return { name: 'item_ids[]', value: item_id };
 
 740     if (item_ids.length == 0) {
 
 744     var data = $('#order_form').serializeArray();
 
 745     data.push({ name: 'action', value: 'Order/update_row_from_master_data' });
 
 746     data = data.concat(item_ids);
 
 748     $.post("controller.pl", data, kivi.eval_json_result);
 
 751   ns.show_calculate_qty_dialog = function(clicked) {
 
 752     var row        = $(clicked).parents("tbody").first();
 
 753     var input_id   = $(row).find('[name="order.orderitems[].qty_as_number"]').attr('id');
 
 754     var formula_id = $(row).find('[name="formula[+]"]').attr('id');
 
 756     calculate_qty_selection_dialog("", input_id, "", formula_id);
 
 763   if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
 
 764     $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
 
 766     $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
 
 769   if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
 
 770     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
 
 772     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
 
 774   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
 
 775   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
 
 777   $('.add_item_input').keydown(function(event) {
 
 778     if (event.keyCode == 13) {
 
 779       event.preventDefault();
 
 780       kivi.Order.add_item();
 
 785   kivi.Order.init_row_handlers();
 
 787   $('#row_table_id').on('sortstop', function(event, ui) {
 
 788     $('#row_table_id thead a img').remove();
 
 789     kivi.Order.renumber_positions();
 
 792   $('#expand_all').on('click', function(event) {
 
 793     event.preventDefault();
 
 794     if ($('#expand_all').data('expanded') == 1) {
 
 795       $('#expand_all').data('expanded', 0);
 
 796       $('#expand_all').attr('src', 'image/expand.svg');
 
 797       $('#expand_all').attr('alt', kivi.t8('Show all details'));
 
 798       $('#expand_all').attr('title', kivi.t8('Show all details'));
 
 799       $('.row_entry').each(function(idx, elt) {
 
 800         kivi.Order.hide_second_row(elt);
 
 803       $('#expand_all').data('expanded', 1);
 
 804       $('#expand_all').attr('src', "image/collapse.svg");
 
 805       $('#expand_all').attr('alt', kivi.t8('Hide all details'));
 
 806       $('#expand_all').attr('title', kivi.t8('Hide all details'));
 
 807       kivi.Order.load_all_second_rows();
 
 808       $('.row_entry').each(function(idx, elt) {
 
 809         kivi.Order.show_second_row(elt);