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