DeliveryOrder: erste Version Lager-Dialog
[kivitendo-erp.git] / js / kivi.DeliveryOrder.js
1 namespace('kivi.DeliveryOrder', function(ns) {
2   ns.check_cv = function() {
3     if ($('#type').val() == 'sales_delivery_order') {
4       if ($('#order_customer_id').val() === '') {
5         alert(kivi.t8('Please select a customer.'));
6         return false;
7       }
8     } else  {
9       if ($('#order_vendor_id').val() === '') {
10         alert(kivi.t8('Please select a vendor.'));
11         return false;
12       }
13     }
14     return true;
15   };
16
17   ns.check_duplicate_parts = function(question) {
18     var id_arr = $('[name="order.orderitems[].parts_id"]').map(function() { return this.value; }).get();
19
20     var i, obj = {}, pos = [];
21
22     for (i = 0; i < id_arr.length; i++) {
23       var id = id_arr[i];
24       if (obj.hasOwnProperty(id)) {
25         pos.push(i + 1);
26       }
27       obj[id] = 0;
28     }
29
30     if (pos.length > 0) {
31       question = question || kivi.t8("Do you really want to continue?");
32       return confirm(kivi.t8("There are duplicate parts at positions") + "\n"
33                      + pos.join(', ') + "\n"
34                      + question);
35     }
36     return true;
37   };
38
39   ns.check_valid_reqdate = function() {
40     if ($('#order_reqdate_as_date').val() === '') {
41       alert(kivi.t8('Please select a delivery date.'));
42       return false;
43     } else {
44       return true;
45     }
46   };
47
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;
52
53     var data = $('#order_form').serializeArray();
54     data.push({ name: 'action', value: 'DeliveryOrder/' + action });
55
56     $.post("controller.pl", data, kivi.eval_json_result);
57   };
58
59   ns.delete_order = function() {
60     var data = $('#order_form').serializeArray();
61     data.push({ name: 'action', value: 'DeliveryOrder/delete' });
62
63     $.post("controller.pl", data, kivi.eval_json_result);
64   };
65
66   ns.show_print_options = function(warn_on_duplicates, warn_on_reqdate) {
67     if (!ns.check_cv()) return;
68     if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to print?"))) return;
69     if (warn_on_reqdate    && !ns.check_valid_reqdate())   return;
70
71     kivi.popup_dialog({
72       id: 'print_options',
73       dialog: {
74         title: kivi.t8('Print options'),
75         width:  800,
76         height: 300
77       }
78     });
79   };
80
81   ns.open_stock_in_out_dialog = function(clicked, in_out) {
82     var $row = $(clicked).parents("tbody").first();
83     var id = $row.find('[name="orderitem_ids[+]"]').val();
84     $row.uniqueId();
85
86     kivi.popup_dialog({
87       id: "stock_in_out",
88       url: "controller.pl?action=DeliveryOrder/stock_in_out_dialog",
89       data: {
90         id:            $("#id").val(),
91         type:          $("#type").val(),
92         parts_id:      $row.find("[name$=parts_id]").val(),
93         unit:          $row.find("[name$=unit]").val(),
94         qty_as_number: $("qty_" + id).val(),
95         stock:         $("stock_" + id).val(),
96         item_id:       id,
97         row:           $row.attr("id"),
98       },
99       dialog: { title: kivi.t8('Transfer stock') }
100     });
101   };
102
103   ns.print = function() {
104     $('#print_options').dialog('close');
105
106     var data = $('#order_form').serializeArray();
107     data = data.concat($('#print_options_form').serializeArray());
108     data.push({ name: 'action', value: 'DeliveryOrder/print' });
109
110     $.post("controller.pl", data, kivi.eval_json_result);
111   };
112
113   var email_dialog;
114
115   ns.setup_send_email_dialog = function() {
116     kivi.SalesPurchase.show_all_print_options_elements();
117     kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
118
119     $('#print_options_form table').first().remove().appendTo('#email_form_print_options');
120
121     var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
122     $('#email_form_' + to_focus).focus();
123   };
124
125   ns.finish_send_email_dialog = function() {
126     kivi.SalesPurchase.show_all_print_options_elements();
127
128     $('#email_form_print_options table').first().remove().prependTo('#print_options_form');
129     return true;
130   };
131
132   ns.show_email_dialog = function(html) {
133     var id            = 'send_email_dialog';
134     var dialog_params = {
135       id:     id,
136       width:  800,
137       height: 600,
138       title:  kivi.t8('Send email'),
139       modal:  true,
140       beforeClose: kivi.DeliveryOrder.finish_send_email_dialog,
141       close: function(event, ui) {
142         email_dialog.remove();
143       }
144     };
145
146     $('#' + id).remove();
147
148     email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
149     email_dialog.html(html);
150     email_dialog.dialog(dialog_params);
151
152     kivi.DeliveryOrder.setup_send_email_dialog();
153
154     $('.cancel').click(ns.close_email_dialog);
155
156     return true;
157   };
158
159   ns.send_email = function() {
160     // push button only once -> slow response from mail server
161     ns.email_dialog_disable_send();
162
163     var data = $('#order_form').serializeArray();
164     data = data.concat($('[name^="email_form."]').serializeArray());
165     data = data.concat($('[name^="print_options."]').serializeArray());
166     data.push({ name: 'action', value: 'DeliveryOrder/send_email' });
167     $.post("controller.pl", data, kivi.eval_json_result);
168   };
169
170   ns.email_dialog_disable_send = function() {
171     // disable mail send event to prevent
172     // impatient users to send multiple times
173     $('#send_email').prop('disabled', true);
174   };
175
176   ns.close_email_dialog = function() {
177     email_dialog.dialog("close");
178   };
179
180   ns.set_number_in_title = function(elt) {
181     $('#nr_in_title').html($(elt).val());
182   };
183
184   ns.reload_cv_dependent_selections = function() {
185     $('#order_shipto_id').val('');
186     var data = $('#order_form').serializeArray();
187     data.push({ name: 'action', value: 'DeliveryOrder/customer_vendor_changed' });
188
189     $.post("controller.pl", data, kivi.eval_json_result);
190   };
191
192   ns.reformat_number = function(event) {
193     $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
194   };
195
196   ns.reformat_number_as_null_number = function(event) {
197     if ($(event.target).val() === '') {
198       return;
199     }
200     ns.reformat_number(event);
201   };
202
203   ns.update_exchangerate = function(event) {
204     if (!ns.check_cv()) {
205       $('#order_currency_id').val($('#old_currency_id').val());
206       return;
207     }
208
209     var rate_input = $('#order_exchangerate_as_null_number');
210     // unset exchangerate if currency changed
211     if ($('#order_currency_id').val() !== $('#old_currency_id').val()) {
212       rate_input.val('');
213     }
214
215     // only set exchangerate if unset
216     if (rate_input.val() !== '') {
217       return;
218     }
219
220     var data = $('#order_form').serializeArray();
221     data.push({ name: 'action', value: 'DeliveryOrder/update_exchangerate' });
222
223     $.ajax({
224       url: 'controller.pl',
225       data: data,
226       method: 'POST',
227       dataType: 'json',
228       success: function(data){
229         if (!data.is_standard) {
230           $('#currency_name').text(data.currency_name);
231           if (data.exchangerate) {
232             rate_input.val(data.exchangerate);
233           } else {
234             rate_input.val('');
235           }
236           $('#exchangerate_settings').show();
237         } else {
238           rate_input.val('');
239           $('#exchangerate_settings').hide();
240         }
241         if ($('#order_currency_id').val() != $('#old_currency_id').val() ||
242             !data.is_standard && data.exchangerate != $('#old_exchangerate').val()) {
243           kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
244         }
245         $('#old_currency_id').val($('#order_currency_id').val());
246         $('#old_exchangerate').val(data.exchangerate);
247       }
248     });
249   };
250
251   ns.exchangerate_changed = function(event) {
252     if (kivi.parse_amount($('#order_exchangerate_as_null_number').val()) != kivi.parse_amount($('#old_exchangerate').val())) {
253       kivi.display_flash('warning', kivi.t8('You have changed the currency or exchange rate. Please check prices.'));
254       $('#old_exchangerate').val($('#order_exchangerate_as_null_number').val());
255     }
256   };
257
258   ns.unit_change = function(event) {
259     var row           = $(event.target).parents("tbody").first();
260     var item_id_dom   = $(row).find('[name="orderitem_ids[+]"]');
261     var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
262     var select_elt    = $(row).find('[name="order.orderitems[].unit"]');
263
264     var oldval = $(select_elt).data('oldval');
265     $(select_elt).data('oldval', $(select_elt).val());
266
267     var data = $('#order_form').serializeArray();
268     data.push({ name: 'action',           value: 'DeliveryOrder/unit_changed'     },
269               { name: 'item_id',          value: item_id_dom.val()        },
270               { name: 'old_unit',         value: oldval                   },
271               { name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
272
273     $.post("controller.pl", data, kivi.eval_json_result);
274   };
275
276   ns.update_sellprice = function(item_id, price_str) {
277     var row       = $('#item_' + item_id).parents("tbody").first();
278     var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
279     var html_elt  = $(row).find('[name="sellprice_text"]');
280     price_elt.val(price_str);
281     html_elt.html(price_str);
282   };
283
284   ns.load_second_row = function(row) {
285     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
286     var div_elt     = $(row).find('[name="second_row"]');
287
288     if ($(div_elt).data('loaded') == 1) {
289       return;
290     }
291     var data = $('#order_form').serializeArray();
292     data.push({ name: 'action',     value: 'DeliveryOrder/load_second_rows' },
293               { name: 'item_ids[]', value: item_id_dom.val()        });
294
295     $.post("controller.pl", data, kivi.eval_json_result);
296   };
297
298   ns.load_all_second_rows = function() {
299     var rows = $('.row_entry').filter(function(idx, elt) {
300       return $(elt).find('[name="second_row"]').data('loaded') != 1;
301     });
302
303     var item_ids = $.map(rows, function(elt) {
304       var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
305       return { name: 'item_ids[]', value: item_id };
306     });
307
308     if (item_ids.length == 0) {
309       return;
310     }
311
312     var data = $('#order_form').serializeArray();
313     data.push({ name: 'action', value: 'DeliveryOrder/load_second_rows' });
314     data = data.concat(item_ids);
315
316     $.post("controller.pl", data, kivi.eval_json_result);
317   };
318
319   ns.hide_second_row = function(row) {
320     $(row).children().not(':first').hide();
321     $(row).data('expanded', 0);
322     var elt = $(row).find('.expand');
323     elt.attr('src', "image/expand.svg");
324     elt.attr('alt', kivi.t8('Show details'));
325     elt.attr('title', kivi.t8('Show details'));
326   };
327
328   ns.show_second_row = function(row) {
329     $(row).children().not(':first').show();
330     $(row).data('expanded', 1);
331     var elt = $(row).find('.expand');
332     elt.attr('src', "image/collapse.svg");
333     elt.attr('alt', kivi.t8('Hide details'));
334     elt.attr('title', kivi.t8('Hide details'));
335   };
336
337   ns.toggle_second_row = function(row) {
338     if ($(row).data('expanded') == 1) {
339       ns.hide_second_row(row);
340     } else {
341       ns.show_second_row(row);
342     }
343   };
344
345   ns.init_row_handlers = function() {
346     kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
347       $(elt).change(ns.reformat_number);
348     });
349
350     kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
351       $(elt).data('oldval', $(elt).val());
352       $(elt).change(ns.unit_change);
353     });
354
355     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
356       $(elt).keydown(function(event) {
357         var row;
358         if (event.keyCode == 40 && event.shiftKey === true) {
359           // shift arrow down
360           event.preventDefault();
361           row = $(event.target).parents(".row_entry").first();
362           ns.load_second_row(row);
363           ns.show_second_row(row);
364           return false;
365         }
366         if (event.keyCode == 38 && event.shiftKey === true) {
367           // shift arrow up
368           event.preventDefault();
369           row = $(event.target).parents(".row_entry").first();
370           ns.hide_second_row(row);
371           return false;
372         }
373       });
374     });
375
376     kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
377       $(elt).click(function(event) {
378         event.preventDefault();
379         var row = $(event.target).parents(".row_entry").first();
380         ns.load_second_row(row);
381         ns.toggle_second_row(row);
382         return false;
383       })
384     });
385
386   };
387
388   ns.redisplay_line_values = function(is_sales, data) {
389     $('.row_entry').each(function(idx, elt) {
390       $(elt).find('[name="linetotal"]').html(data[idx][0]);
391       if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
392         var mt = data[idx][1];
393         var mp = data[idx][2];
394         var h  = '<span';
395         if (mt[0] === '-') h += ' class="plus0"';
396         h += '>' + mt + '&nbsp;&nbsp;' + mp + '%';
397         h += '</span>';
398         $(elt).find('[name="linemargin"]').html(h);
399       }
400     });
401   };
402
403   ns.redisplay_cvpartnumbers = function(data) {
404     $('.row_entry').each(function(idx, elt) {
405       $(elt).find('[name="cvpartnumber"]').html(data[idx][0]);
406     });
407   };
408
409   ns.renumber_positions = function() {
410     $('.row_entry [name="position"]').each(function(idx, elt) {
411       $(elt).html(idx+1);
412     });
413     $('.row_entry').each(function(idx, elt) {
414       $(elt).data("position", idx+1);
415     });
416   };
417
418   ns.reorder_items = function(order_by) {
419     var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
420     $('#row_table_id thead a img').remove();
421
422     var src;
423     if (dir == "1") {
424       dir = "0";
425       src = "image/up.png";
426     } else {
427       dir = "1";
428       src = "image/down.png";
429     }
430
431     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
432
433     var data = $('#order_form').serializeArray();
434     data.push({ name: 'action',   value: 'DeliveryOrder/reorder_items' },
435               { name: 'order_by', value: order_by              },
436               { name: 'sort_dir', value: dir                   });
437
438     $.post("controller.pl", data, kivi.eval_json_result);
439   };
440
441   ns.redisplay_items = function(data) {
442     var old_rows = $('.row_entry').detach();
443     var new_rows = [];
444     $(data).each(function(idx, elt) {
445       new_rows.push(old_rows[elt.old_pos - 1]);
446     });
447     $(new_rows).appendTo($('#row_table_id'));
448     ns.renumber_positions();
449   };
450
451   ns.get_insert_before_item_id = function(wanted_pos) {
452     if (wanted_pos === '') return;
453
454     var insert_before_item_id;
455     // selection by data does not seem to work if data is changed at runtime
456     // var elt = $('.row_entry [data-position="' + wanted_pos + '"]');
457     $('.row_entry').each(function(idx, elt) {
458       if ($(elt).data("position") == wanted_pos) {
459         insert_before_item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
460         return false;
461       }
462     });
463
464     return insert_before_item_id;
465   };
466
467   ns.add_item = function() {
468     if ($('#add_item_parts_id').val() === '') return;
469     if (!ns.check_cv()) return;
470
471     $('#row_table_id thead a img').remove();
472
473     var insert_before_item_id = ns.get_insert_before_item_id($('#add_item_position').val());
474
475     var data = $('#order_form').serializeArray();
476     data.push({ name: 'action', value: 'DeliveryOrder/add_item' },
477               { name: 'insert_before_item_id', value: insert_before_item_id });
478
479     $.post("controller.pl", data, kivi.eval_json_result);
480   };
481
482   ns.open_multi_items_dialog = function() {
483     if (!ns.check_cv()) return;
484
485     var pp = $("#add_item_parts_id").data("part_picker");
486     pp.o.multiple=1;
487     pp.open_dialog();
488   };
489
490   ns.add_multi_items = function(data) {
491     var insert_before_item_id = ns.get_insert_before_item_id($('#multi_items_position').val());
492     data = data.concat($('#order_form').serializeArray());
493     data.push({ name: 'action', value: 'DeliveryOrder/add_multi_items' },
494               { name: 'insert_before_item_id', value: insert_before_item_id });
495     $.post("controller.pl", data, kivi.eval_json_result);
496   };
497
498   ns.delete_order_item_row = function(clicked) {
499     var row = $(clicked).parents("tbody").first();
500     $(row).remove();
501
502     ns.renumber_positions();
503   };
504
505   ns.row_table_scroll_down = function() {
506     $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
507   };
508
509   ns.show_longdescription_dialog = function(clicked) {
510     var row                 = $(clicked).parents("tbody").first();
511     var position            = $(row).find('[name="position"]').html();
512     var partnumber          = $(row).find('[name="partnumber"]').html();
513     var description_elt     = $(row).find('[name="order.orderitems[].description"]');
514     var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
515
516     var params = {
517       runningnumber:           position,
518       partnumber:              partnumber,
519       description:             description_elt.val(),
520       default_longdescription: longdescription_elt.val(),
521       set_function:            function(val) {
522         longdescription_elt.val(val);
523       }
524     };
525
526     kivi.SalesPurchase.edit_longdescription_with_params(params);
527   };
528
529   ns.price_chooser_item_row = function(clicked) {
530     if (!ns.check_cv()) return;
531     var row         = $(clicked).parents("tbody").first();
532     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
533
534     var data = $('#order_form').serializeArray();
535     data.push({ name: 'action',  value: 'DeliveryOrder/price_popup' },
536               { name: 'item_id', value: item_id_dom.val()   });
537
538     $.post("controller.pl", data, kivi.eval_json_result);
539   };
540
541   ns.show_vc_details_dialog = function() {
542     if (!ns.check_cv()) return;
543     var vc;
544     var vc_id;
545     var title;
546     if ($('#order_customer_id').val()) {
547       vc    = 'customer';
548       vc_id = $('#order_customer_id').val();
549       title = kivi.t8('Customer details');
550     } else {
551       vc    = 'vendor';
552       vc_id = $('#order_vendor_id').val();
553       title = kivi.t8('Vendor details');
554     }
555
556     kivi.popup_dialog({
557       url:    'controller.pl',
558       data:   { action: 'DeliveryOrder/show_customer_vendor_details_dialog',
559                 type  : $('#type').val(),
560                 vc    : vc,
561                 vc_id : vc_id
562               },
563       id:     'jq_customer_vendor_details_dialog',
564       dialog: {
565         title:  title,
566         width:  800,
567         height: 650
568       }
569     });
570     return true;
571   };
572
573   ns.update_row_from_master_data = function(clicked) {
574     var row = $(clicked).parents("tbody").first();
575     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
576
577     var data = $('#order_form').serializeArray();
578     data.push({ name: 'action', value: 'DeliveryOrder/update_row_from_master_data' });
579     data.push({ name: 'item_ids[]', value: item_id_dom.val() });
580
581     $.post("controller.pl", data, kivi.eval_json_result);
582   };
583
584   ns.update_all_rows_from_master_data = function() {
585     var item_ids = $.map($('.row_entry'), function(elt) {
586       var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
587       return { name: 'item_ids[]', value: item_id };
588     });
589
590     if (item_ids.length == 0) {
591       return;
592     }
593
594     var data = $('#order_form').serializeArray();
595     data.push({ name: 'action', value: 'DeliveryOrder/update_row_from_master_data' });
596     data = data.concat(item_ids);
597
598     $.post("controller.pl", data, kivi.eval_json_result);
599   };
600
601   ns.show_calculate_qty_dialog = function(clicked) {
602     var row        = $(clicked).parents("tbody").first();
603     var input_id   = $(row).find('[name="order.orderitems[].qty_as_number"]').attr('id');
604     var formula_id = $(row).find('[name="formula[+]"]').attr('id');
605
606     calculate_qty_selection_dialog("", input_id, "", formula_id);
607     return true;
608   };
609
610   ns.edit_custom_shipto = function() {
611     if (!ns.check_cv()) return;
612
613     kivi.SalesPurchase.edit_custom_shipto();
614   };
615
616   ns.purchase_order_check_for_direct_delivery = function() {
617     if ($('#type').val() != 'sales_order') {
618       kivi.submit_form_with_action($('#order_form'), 'DeliveryOrder/purchase_order');
619     }
620
621     var empty = true;
622     var shipto;
623     if ($('#order_shipto_id').val() !== '') {
624       empty = false;
625       shipto = $('#order_shipto_id option:selected').text();
626     } else {
627       $('#shipto_inputs [id^="shipto"]').each(function(idx, elt) {
628         if (!empty)                                     return true;
629         if (/^shipto_to_copy/.test($(elt).prop('id')))  return true;
630         if (/^shiptocp_gender/.test($(elt).prop('id'))) return true;
631         if (/^shiptocvar_/.test($(elt).prop('id')))     return true;
632         if ($(elt).val() !== '') {
633           empty = false;
634           return false;
635         }
636       });
637       var shipto_elements = [];
638       $([$('#shiptoname').val(), $('#shiptostreet').val(), $('#shiptozipcode').val(), $('#shiptocity').val()]).each(function(idx, elt) {
639         if (elt !== '') shipto_elements.push(elt);
640       });
641       shipto = shipto_elements.join('; ');
642     }
643
644     var use_it = false;
645     if (!empty) {
646       ns.direct_delivery_dialog(shipto);
647     } else {
648       kivi.submit_form_with_action($('#order_form'), 'DeliveryOrder/purchase_order');
649     }
650   };
651
652   ns.direct_delivery_callback = function(accepted) {
653     $('#direct-delivery-dialog').dialog('close');
654
655     if (accepted) {
656       $('<input type="hidden" name="use_shipto">').appendTo('#order_form').val('1');
657     }
658
659     kivi.submit_form_with_action($('#order_form'), 'DeliveryOrder/purchase_order');
660   };
661
662   ns.direct_delivery_dialog = function(shipto) {
663     $('#direct-delivery-dialog').remove();
664
665     var text1 = kivi.t8('You have entered or selected the following shipping address for this customer:');
666     var text2 = kivi.t8('Do you want to carry this shipping address over to the new purchase order so that the vendor can deliver the goods directly to your customer?');
667     var html  = '<div id="direct-delivery-dialog"><p>' + text1 + '</p><p>' + shipto + '</p><p>' + text2 + '</p>';
668     html      = html + '<hr><p>';
669     html      = html + '<input type="button" value="' + kivi.t8('Yes') + '" size="30" onclick="kivi.DeliveryOrder.direct_delivery_callback(true)">';
670     html      = html + '&nbsp;';
671     html      = html + '<input type="button" value="' + kivi.t8('No')  + '" size="30" onclick="kivi.DeliveryOrder.direct_delivery_callback(false)">';
672     html      = html + '</p></div>';
673     $(html).hide().appendTo('#order_form');
674
675     kivi.popup_dialog({id: 'direct-delivery-dialog',
676                        dialog: {title:  kivi.t8('Carry over shipping address'),
677                                 height: 300,
678                                 width:  500 }});
679   };
680
681   ns.follow_up_window = function() {
682     var id   = $('#id').val();
683     var type = $('#type').val();
684
685     var number_info = '';
686     if ($('#type').val() == 'sales_order' || $('#type').val() == 'purchase_order') {
687       number_info = $('#order_ordnumber').val();
688     } else if ($('#type').val() == 'sales_quotation' || $('#type').val() == 'request_quotation') {
689       number_info = $('#order_quonumber').val();
690     }
691
692     var name_info = '';
693     if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation') {
694       name_info = $('#order_customer_id_name').val();
695     } else if ($('#type').val() == 'purchase_order' || $('#type').val() == 'request_quotation') {
696       name_info = $('#order_vendor_id_name').val();
697     }
698
699     var info = '';
700     if (number_info !== '') { info += ' (' + number_info + ')' }
701     if (name_info   !== '') { info += ' (' + name_info + ')' }
702
703     if (!$('#follow_up_rowcount').lenght) {
704       $('<input type="hidden" name="follow_up_rowcount"        id="follow_up_rowcount">').appendTo('#order_form');
705       $('<input type="hidden" name="follow_up_trans_id_1"      id="follow_up_trans_id_1">').appendTo('#order_form');
706       $('<input type="hidden" name="follow_up_trans_type_1"    id="follow_up_trans_type_1">').appendTo('#order_form');
707       $('<input type="hidden" name="follow_up_trans_info_1"    id="follow_up_trans_info_1">').appendTo('#order_form');
708       $('<input type="hidden" name="follow_up_trans_subject_1" id="follow_up_trans_subject_1">').appendTo('#order_form');
709     }
710     $('#follow_up_rowcount').val(1);
711     $('#follow_up_trans_id_1').val(id);
712     $('#follow_up_trans_type_1').val(type);
713     $('#follow_up_trans_info_1').val(info);
714     $('#follow_up_trans_subject_1').val($('#order_transaction_description').val());
715
716     follow_up_window();
717   };
718
719   ns.create_part = function() {
720     var data = $('#order_form').serializeArray();
721     data.push({ name: 'action', value: 'DeliveryOrder/create_part' });
722
723     $.post("controller.pl", data, kivi.eval_json_result);
724   };
725
726 });
727
728 $(function() {
729   $('#order_customer_id').change(kivi.DeliveryOrder.reload_cv_dependent_selections);
730   $('#order_vendor_id').change(kivi.DeliveryOrder.reload_cv_dependent_selections);
731
732   $('#order_currency_id').change(kivi.DeliveryOrder.update_exchangerate);
733   $('#order_transdate_as_date').change(kivi.DeliveryOrder.update_exchangerate);
734   $('#order_exchangerate_as_null_number').change(kivi.DeliveryOrder.exchangerate_changed);
735
736   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
737   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
738
739   $('.add_item_input').keydown(function(event) {
740     if (event.keyCode == 13) {
741       event.preventDefault();
742       kivi.DeliveryOrder.add_item();
743       return false;
744     }
745   });
746
747   kivi.DeliveryOrder.init_row_handlers();
748
749   $('#row_table_id').on('sortstop', function(event, ui) {
750     $('#row_table_id thead a img').remove();
751     kivi.DeliveryOrder.renumber_positions();
752   });
753
754   $('#expand_all').on('click', function(event) {
755     event.preventDefault();
756     if ($('#expand_all').data('expanded') == 1) {
757       $('#expand_all').data('expanded', 0);
758       $('#expand_all').attr('src', 'image/expand.svg');
759       $('#expand_all').attr('alt', kivi.t8('Show all details'));
760       $('#expand_all').attr('title', kivi.t8('Show all details'));
761       $('.row_entry').each(function(idx, elt) {
762         kivi.DeliveryOrder.hide_second_row(elt);
763       });
764     } else {
765       $('#expand_all').data('expanded', 1);
766       $('#expand_all').attr('src', "image/collapse.svg");
767       $('#expand_all').attr('alt', kivi.t8('Hide all details'));
768       $('#expand_all').attr('title', kivi.t8('Hide all details'));
769       kivi.DeliveryOrder.load_all_second_rows();
770       $('.row_entry').each(function(idx, elt) {
771         kivi.DeliveryOrder.show_second_row(elt);
772       });
773     }
774     return false;
775   });
776
777   $('.reformat_number_as_null_number').change(kivi.DeliveryOrder.reformat_number_as_null_number);
778
779 });