e77b63e1581b82defa258cd743b02ed1703845d1
[kivitendo-erp.git] / js / kivi.Order.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 save?");
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) {
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
70     kivi.popup_dialog({
71       id: 'print_options',
72       dialog: {
73         title: kivi.t8('Print options'),
74         width:  800,
75         height: 300
76       }
77     });
78   };
79
80   ns.print = function() {
81     $('#print_options').dialog('close');
82
83     var data = $('#order_form').serializeArray();
84     data = data.concat($('#print_options_form').serializeArray());
85     data.push({ name: 'action', value: 'Order/print' });
86
87     $.post("controller.pl", data, kivi.eval_json_result);
88   };
89
90   ns.email = function(warn_on_duplicates) {
91     if (warn_on_duplicates && !ns.check_duplicate_parts(kivi.t8("Do you really want to send by mail?"))) return;
92     if (!ns.check_cv()) return;
93
94     var data = $('#order_form').serializeArray();
95     data.push({ name: 'action', value: 'Order/show_email_dialog' });
96
97     $.post("controller.pl", data, kivi.eval_json_result);
98   };
99
100   var email_dialog;
101
102   ns.setup_send_email_dialog = function() {
103     kivi.SalesPurchase.show_all_print_options_elements();
104     kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
105
106     $('#print_options_form table').first().remove().appendTo('#email_form_print_options');
107
108     var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
109     $('#email_form_' + to_focus).focus();
110   };
111
112   ns.finish_send_email_dialog = function() {
113     kivi.SalesPurchase.show_all_print_options_elements();
114
115     $('#email_form_print_options table').first().remove().prependTo('#print_options_form');
116     return true;
117   };
118
119   ns.show_email_dialog = function(html) {
120     var id            = 'send_email_dialog';
121     var dialog_params = {
122       id:     id,
123       width:  800,
124       height: 600,
125       title:  kivi.t8('Send email'),
126       modal:  true,
127       beforeClose: kivi.Order.finish_send_email_dialog,
128       close: function(event, ui) {
129         email_dialog.remove();
130       }
131     };
132
133     $('#' + id).remove();
134
135     email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
136     email_dialog.html(html);
137     email_dialog.dialog(dialog_params);
138
139     kivi.Order.setup_send_email_dialog();
140
141     $('.cancel').click(ns.close_email_dialog);
142
143     return true;
144   };
145
146   ns.send_email = function() {
147     var data = $('#order_form').serializeArray();
148     data = data.concat($('[name^="email_form."]').serializeArray());
149     data = data.concat($('[name^="print_options."]').serializeArray());
150     data.push({ name: 'action', value: 'Order/send_email' });
151     $.post("controller.pl", data, kivi.eval_json_result);
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_dependant_selections = function() {
163     var data = $('#order_form').serializeArray();
164     data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
165
166     $.post("controller.pl", data, kivi.eval_json_result);
167   };
168
169   ns.reformat_number = function(event) {
170     $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
171   };
172
173   ns.recalc_amounts_and_taxes = function() {
174     var data = $('#order_form').serializeArray();
175     data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
176
177     $.post("controller.pl", data, kivi.eval_json_result);
178   };
179
180   ns.unit_change = function(event) {
181     var row           = $(event.target).parents("tbody").first();
182     var item_id_dom   = $(row).find('[name="orderitem_ids[+]"]');
183     var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
184     var select_elt    = $(row).find('[name="order.orderitems[].unit"]');
185
186     var oldval = $(select_elt).data('oldval');
187     $(select_elt).data('oldval', $(select_elt).val());
188
189     var data = $('#order_form').serializeArray();
190     data.push({ name: 'action',           value: 'Order/unit_changed'     },
191               { name: 'item_id',          value: item_id_dom.val()        },
192               { name: 'old_unit',         value: oldval                   },
193               { name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
194
195     $.post("controller.pl", data, kivi.eval_json_result);
196   };
197
198   ns.update_sellprice = function(item_id, price_str) {
199     var row       = $('#item_' + item_id).parents("tbody").first();
200     var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
201     var html_elt  = $(row).find('[name="sellprice_text"]');
202     price_elt.val(price_str);
203     html_elt.html(price_str);
204   };
205
206   ns.load_second_row = function(row) {
207     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
208     var div_elt     = $(row).find('[name="second_row"]');
209
210     if ($(div_elt).data('loaded') == 1) {
211       return;
212     }
213     var data = $('#order_form').serializeArray();
214     data.push({ name: 'action',     value: 'Order/load_second_rows' },
215               { name: 'item_ids[]', value: item_id_dom.val()        });
216
217     $.post("controller.pl", data, kivi.eval_json_result);
218   };
219
220   ns.load_all_second_rows = function() {
221     var rows = $('.row_entry').filter(function(idx, elt) {
222       return $(elt).find('[name="second_row"]').data('loaded') != 1;
223     });
224
225     var item_ids = $.map(rows, function(elt) {
226       var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
227       return { name: 'item_ids[]', value: item_id };
228     });
229
230     if (item_ids.length == 0) {
231       return;
232     }
233
234     var data = $('#order_form').serializeArray();
235     data.push({ name: 'action', value: 'Order/load_second_rows' });
236     data = data.concat(item_ids);
237
238     $.post("controller.pl", data, kivi.eval_json_result);
239   };
240
241   ns.hide_second_row = function(row) {
242     $(row).children().not(':first').hide();
243     $(row).data('expanded', 0);
244     var elt = $(row).find('.expand');
245     elt.attr('src', "image/expand.svg");
246     elt.attr('alt', kivi.t8('Show details'));
247     elt.attr('title', kivi.t8('Show details'));
248   };
249
250   ns.show_second_row = function(row) {
251     $(row).children().not(':first').show();
252     $(row).data('expanded', 1);
253     var elt = $(row).find('.expand');
254     elt.attr('src', "image/collapse.svg");
255     elt.attr('alt', kivi.t8('Hide details'));
256     elt.attr('title', kivi.t8('Hide details'));
257   };
258
259   ns.toggle_second_row = function(row) {
260     if ($(row).data('expanded') == 1) {
261       ns.hide_second_row(row);
262     } else {
263       ns.show_second_row(row);
264     }
265   };
266
267   ns.init_row_handlers = function() {
268     kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
269       $(elt).change(ns.recalc_amounts_and_taxes);
270     });
271
272     kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
273       $(elt).change(ns.reformat_number);
274     });
275
276     kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
277       $(elt).data('oldval', $(elt).val());
278       $(elt).change(ns.unit_change);
279     });
280
281     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
282       $(elt).keydown(function(event) {
283         var row;
284         if (event.keyCode == 40 && event.shiftKey === true) {
285           // shift arrow down
286           event.preventDefault();
287           row = $(event.target).parents(".row_entry").first();
288           ns.load_second_row(row);
289           ns.show_second_row(row);
290           return false;
291         }
292         if (event.keyCode == 38 && event.shiftKey === true) {
293           // shift arrow up
294           event.preventDefault();
295           row = $(event.target).parents(".row_entry").first();
296           ns.hide_second_row(row);
297           return false;
298         }
299       });
300     });
301
302     kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
303       $(elt).click(function(event) {
304         event.preventDefault();
305         var row = $(event.target).parents(".row_entry").first();
306         ns.load_second_row(row);
307         ns.toggle_second_row(row);
308         return false;
309       })
310     });
311
312   };
313
314   ns.redisplay_line_values = function(is_sales, data) {
315     $('.row_entry').each(function(idx, elt) {
316       $(elt).find('[name="linetotal"]').html(data[idx][0]);
317       if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
318         var mt = data[idx][1];
319         var mp = data[idx][2];
320         var h  = '<span';
321         if (mt[0] === '-') h += ' class="plus0"';
322         h += '>' + mt + '&nbsp;&nbsp;' + mp + '%';
323         h += '</span>';
324         $(elt).find('[name="linemargin"]').html(h);
325       }
326     });
327   };
328
329   ns.redisplay_cvpartnumbers = function(data) {
330     $('.row_entry').each(function(idx, elt) {
331       $(elt).find('[name="cvpartnumber"]').html(data[idx][0]);
332     });
333   };
334
335   ns.renumber_positions = function() {
336     $('.row_entry [name="position"]').each(function(idx, elt) {
337       $(elt).html(idx+1);
338     });
339   };
340
341   ns.reorder_items = function(order_by) {
342     var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
343     $('#row_table_id thead a img').remove();
344
345     var src;
346     if (dir == "1") {
347       dir = "0";
348       src = "image/up.png";
349     } else {
350       dir = "1";
351       src = "image/down.png";
352     }
353
354     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
355
356     var data = $('#order_form').serializeArray();
357     data.push({ name: 'action',   value: 'Order/reorder_items' },
358               { name: 'order_by', value: order_by              },
359               { name: 'sort_dir', value: dir                   });
360
361     $.post("controller.pl", data, kivi.eval_json_result);
362   };
363
364   ns.redisplay_items = function(data) {
365     var old_rows = $('.row_entry').detach();
366     var new_rows = [];
367     $(data).each(function(idx, elt) {
368       new_rows.push(old_rows[elt.old_pos - 1]);
369     });
370     $(new_rows).appendTo($('#row_table_id'));
371     ns.renumber_positions();
372   };
373
374   ns.add_item = function() {
375     if ($('#add_item_parts_id').val() === '') return;
376     if (!ns.check_cv()) return;
377
378     $('#row_table_id thead a img').remove();
379
380     var data = $('#order_form').serializeArray();
381     data.push({ name: 'action', value: 'Order/add_item' });
382
383     $.post("controller.pl", data, kivi.eval_json_result);
384   };
385
386   ns.setup_multi_items_dialog = function() {
387     $('#multi_items_filter_table input, #multi_items_filter_table select').keydown(function(event) {
388       if (event.keyCode == 13) {
389         event.preventDefault();
390         ns.multi_items_dialog_update_result();
391         return false;
392       }
393     });
394
395     $('#multi_items_filter_all_substr_multi_ilike').focus();
396   };
397
398   ns.show_multi_items_dialog = function() {
399     if (!ns.check_cv()) return;
400
401     $('#row_table_id thead a img').remove();
402
403     kivi.popup_dialog({
404       url:    'controller.pl?action=Order/show_multi_items_dialog',
405       data:   { type: $('#type').val() },
406       id:     'jq_multi_items_dialog',
407       load:   kivi.Order.setup_multi_items_dialog,
408       dialog: {
409         title:  kivi.t8('Add multiple items'),
410         width:  800,
411         height: 500
412       }
413     });
414     return true;
415   };
416
417   ns.close_multi_items_dialog = function() {
418     $('#jq_multi_items_dialog').dialog('close');
419   };
420
421   ns.multi_items_dialog_update_result = function() {
422     var data = $('#multi_items_form').serializeArray();
423     data.push({ name: 'type', value: $('#type').val() });
424     $.ajax({
425       url:     'controller.pl?action=Order/multi_items_update_result',
426       data:    data,
427       method:  'post',
428       success: function(data) {
429         $('#multi_items_result').html(data);
430         ns.multi_items_dialog_enable_continue();
431         ns.multi_items_result_setup_events();
432       }
433     });
434   };
435
436   ns.multi_items_dialog_disable_continue = function() {
437     // disable keydown-event and continue button to prevent
438     // impatient users to add parts multiple times
439     $('#multi_items_result input').off("keydown");
440     $('#multi_items_dialog_continue_button').prop('disabled', true);
441   };
442
443   ns.multi_items_dialog_enable_continue = function()  {
444     $('#multi_items_result input').keydown(function(event) {
445       if(event.keyCode == 13) {
446         event.preventDefault();
447         ns.add_multi_items();
448         return false;
449       }
450     });
451     $('#multi_items_dialog_continue_button').prop('disabled', false);
452   };
453
454   ns.multi_items_result_setup_events = function() {
455     $('#multi_items_all_qty').change(ns.reformat_number);
456     $('#multi_items_all_qty').change(function(event) {
457       $('.multi_items_qty').val($(event.target).val());
458     });
459     $('.multi_items_qty').change(ns.reformat_number);
460   }
461
462   ns.add_multi_items = function() {
463     // rows at all
464     var n_rows = $('.multi_items_qty').length;
465     if (n_rows == 0) return;
466
467     // filled rows
468     n_rows = $('.multi_items_qty').filter(function() {
469       return $(this).val().length > 0;
470     }).length;
471     if (n_rows == 0) return;
472
473     ns.multi_items_dialog_disable_continue();
474
475     var data = $('#order_form').serializeArray();
476     data = data.concat($('#multi_items_form').serializeArray());
477     data.push({ name: 'action', value: 'Order/add_multi_items' });
478     $.post("controller.pl", data, kivi.eval_json_result);
479   };
480
481   ns.set_input_to_one = function(clicked) {
482     if ($(clicked).val() == '') {
483       $(clicked).val(kivi.format_amount(1.00, -2));
484     }
485     $(clicked).select();
486   };
487
488   ns.delete_order_item_row = function(clicked) {
489     var row = $(clicked).parents("tbody").first();
490     $(row).remove();
491
492     ns.renumber_positions();
493     ns.recalc_amounts_and_taxes();
494   };
495
496   ns.row_table_scroll_down = function() {
497     $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
498   };
499
500   ns.show_longdescription_dialog = function(clicked) {
501     var row                 = $(clicked).parents("tbody").first();
502     var position            = $(row).find('[name="position"]').html();
503     var partnumber          = $(row).find('[name="partnumber"]').html();
504     var description_elt     = $(row).find('[name="order.orderitems[].description"]');
505     var description         = description_elt.val();
506     var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
507     var longdescription;
508
509     if (!longdescription_elt.length) {
510       var data = [
511         { name: 'action',   value: 'Order/get_item_longdescription'                          },
512         { name: 'type',     value: $('#type').val()                                          },
513         { name: 'item_id',  value: $(row).find('[name="order.orderitems[+].id"]').val()      },
514         { name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() }
515       ];
516
517       $.ajax({
518         url:      'controller.pl',
519         data:     data,
520         method:   "GET",
521         async:    false,
522         dataType: 'text',
523         success:  function(val) {
524           longdescription = val;
525         }
526       });
527     } else {
528       longdescription = longdescription_elt.val();
529     }
530
531     var params = {
532       runningnumber:           position,
533       partnumber:              partnumber,
534       description:             description,
535       default_longdescription: longdescription,
536       set_function:            function(val) {
537         longdescription_elt.remove();
538         $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
539       }
540     };
541
542     kivi.SalesPurchase.edit_longdescription_with_params(params);
543   };
544
545   ns.price_chooser_item_row = function(clicked) {
546     if (!ns.check_cv()) return;
547     var row         = $(clicked).parents("tbody").first();
548     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
549
550     var data = $('#order_form').serializeArray();
551     data.push({ name: 'action',  value: 'Order/price_popup' },
552               { name: 'item_id', value: item_id_dom.val()   });
553
554     $.post("controller.pl", data, kivi.eval_json_result);
555   };
556
557   ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
558     var row        = $('#item_' + item_id).parents("tbody").first();
559     var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
560     var button_elt = $(row).find('[name="price_chooser_button"]');
561
562     button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
563     source_elt.val(source);
564
565     var editable_div_elt     = $(row).find('[name="editable_price"]');
566     var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
567     if (price_editable == 1 && source === '') {
568       // editable
569       $(editable_div_elt).show();
570       $(not_editable_div_elt).hide();
571       $(editable_div_elt).find(':input').prop("disabled", false);
572       $(not_editable_div_elt).find(':input').prop("disabled", true);
573     } else {
574       // not editable
575       $(editable_div_elt).hide();
576       $(not_editable_div_elt).show();
577       $(editable_div_elt).find(':input').prop("disabled", true);
578       $(not_editable_div_elt).find(':input').prop("disabled", false);
579     }
580
581     if (price_str) {
582       var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
583       var html_elt  = $(row).find('[name="sellprice_text"]');
584       price_elt.val(price_str);
585       html_elt.html(price_str);
586       ns.recalc_amounts_and_taxes();
587     }
588
589     kivi.io.close_dialog();
590   };
591
592   ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
593     var row        = $('#item_' + item_id).parents("tbody").first();
594     var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
595     var button_elt = $(row).find('[name="price_chooser_button"]');
596
597     button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
598     source_elt.val(source);
599
600     var editable_div_elt     = $(row).find('[name="editable_discount"]');
601     var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
602     if (price_editable == 1 && source === '') {
603       // editable
604       $(editable_div_elt).show();
605       $(not_editable_div_elt).hide();
606       $(editable_div_elt).find(':input').prop("disabled", false);
607       $(not_editable_div_elt).find(':input').prop("disabled", true);
608     } else {
609       // not editable
610       $(editable_div_elt).hide();
611       $(not_editable_div_elt).show();
612       $(editable_div_elt).find(':input').prop("disabled", true);
613       $(not_editable_div_elt).find(':input').prop("disabled", false);
614     }
615
616     if (discount_str) {
617       var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
618       var html_elt     = $(row).find('[name="discount_text"]');
619       discount_elt.val(discount_str);
620       html_elt.html(discount_str);
621       ns.recalc_amounts_and_taxes();
622     }
623
624     kivi.io.close_dialog();
625   };
626
627   ns.show_periodic_invoices_config_dialog = function() {
628     if ($('#type').val() !== 'sales_order') return;
629
630     kivi.popup_dialog({
631       url:    'controller.pl?action=Order/show_periodic_invoices_config_dialog',
632       data:   { type:              $('#type').val(),
633                 id:                $('#id').val(),
634                 config:            $('#order_periodic_invoices_config').val(),
635                 customer_id:       $('#order_customer_id').val(),
636                 transdate_as_date: $('#order_transdate_as_date').val(),
637                 language_id:       $('#language_id').val()
638               },
639       id:     'jq_periodic_invoices_config_dialog',
640       load:   kivi.reinit_widgets,
641       dialog: {
642         title:  kivi.t8('Edit the configuration for periodic invoices'),
643         width:  800,
644         height: 650
645       }
646     });
647     return true;
648   };
649
650   ns.close_periodic_invoices_config_dialog = function() {
651     $('#jq_periodic_invoices_config_dialog').dialog('close');
652   };
653
654   ns.assign_periodic_invoices_config = function() {
655     var data = $('[name="Form"]').serializeArray();
656     data.push({ name: 'type',   value: $('#type').val() },
657               { name: 'action', value: 'Order/assign_periodic_invoices_config' });
658     $.post("controller.pl", data, kivi.eval_json_result);
659   };
660
661   ns.check_save_active_periodic_invoices = function() {
662     var type = $('#type').val();
663     if (type !== 'sales_order') return true;
664
665     var active = false;
666     $.ajax({
667       url:      'controller.pl',
668       data:     { action: 'Order/get_has_active_periodic_invoices',
669                   type  : type,
670                   id    : $('#id').val(),
671                   config: $('#order_periodic_invoices_config').val(),
672                 },
673       method:   "GET",
674       async:    false,
675       dataType: 'text',
676       success:  function(val) {
677         active = val;
678       }
679     });
680
681     if (active == 1) {
682       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?'));
683     }
684
685     return true;
686   };
687
688   ns.show_vc_details_dialog = function() {
689     if (!ns.check_cv()) return;
690     var vc;
691     var vc_id;
692     var title;
693     if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
694       vc    = 'customer';
695       vc_id = $('#order_customer_id').val();
696       title = kivi.t8('Customer details');
697     } else {
698       vc    = 'vendor';
699       vc_id = $('#order_vendor_id').val();
700       title = kivi.t8('Vendor details');
701     }
702
703     kivi.popup_dialog({
704       url:    'controller.pl',
705       data:   { action: 'Order/show_customer_vendor_details_dialog',
706                 type  : $('#type').val(),
707                 vc    : vc,
708                 vc_id : vc_id
709               },
710       id:     'jq_customer_vendor_details_dialog',
711       dialog: {
712         title:  title,
713         width:  800,
714         height: 650
715       }
716     });
717     return true;
718   };
719
720   ns.update_row_from_master_data = function(clicked) {
721     var row = $(clicked).parents("tbody").first();
722     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
723
724     var data = $('#order_form').serializeArray();
725     data.push({ name: 'action', value: 'Order/update_row_from_master_data' });
726     data.push({ name: 'item_ids[]', value: item_id_dom.val() });
727
728     $.post("controller.pl", data, kivi.eval_json_result);
729   };
730
731   ns.update_all_rows_from_master_data = function() {
732     var item_ids = $.map($('.row_entry'), function(elt) {
733       var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
734       return { name: 'item_ids[]', value: item_id };
735     });
736
737     if (item_ids.length == 0) {
738       return;
739     }
740
741     var data = $('#order_form').serializeArray();
742     data.push({ name: 'action', value: 'Order/update_row_from_master_data' });
743     data = data.concat(item_ids);
744
745     $.post("controller.pl", data, kivi.eval_json_result);
746   };
747
748   ns.show_calculate_qty_dialog = function(clicked) {
749     var row        = $(clicked).parents("tbody").first();
750     var input_id   = $(row).find('[name="order.orderitems[].qty_as_number"]').attr('id');
751     var formula_id = $(row).find('[name="formula[+]"]').attr('id');
752
753     calculate_qty_selection_dialog("", input_id, "", formula_id);
754     return true;
755   };
756
757 });
758
759 $(function() {
760   if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
761     $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
762   } else {
763     $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
764   }
765
766   if ($('#type').val() == 'sales_order' || $('#type').val() == 'sales_quotation' ) {
767     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
768   } else {
769     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
770   }
771   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
772   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
773
774   $('.add_item_input').keydown(function(event) {
775     if (event.keyCode == 13) {
776       event.preventDefault();
777       kivi.Order.add_item();
778       return false;
779     }
780   });
781
782   kivi.Order.init_row_handlers();
783
784   $('#row_table_id').on('sortstop', function(event, ui) {
785     $('#row_table_id thead a img').remove();
786     kivi.Order.renumber_positions();
787   });
788
789   $('#expand_all').on('click', function(event) {
790     event.preventDefault();
791     if ($('#expand_all').data('expanded') == 1) {
792       $('#expand_all').data('expanded', 0);
793       $('#expand_all').attr('src', 'image/expand.svg');
794       $('#expand_all').attr('alt', kivi.t8('Show all details'));
795       $('#expand_all').attr('title', kivi.t8('Show all details'));
796       $('.row_entry').each(function(idx, elt) {
797         kivi.Order.hide_second_row(elt);
798       });
799     } else {
800       $('#expand_all').data('expanded', 1);
801       $('#expand_all').attr('src', "image/collapse.svg");
802       $('#expand_all').attr('alt', kivi.t8('Hide all details'));
803       $('#expand_all').attr('title', kivi.t8('Hide all details'));
804       kivi.Order.load_all_second_rows();
805       $('.row_entry').each(function(idx, elt) {
806         kivi.Order.show_second_row(elt);
807       });
808     }
809     return false;
810   });
811
812 });