Auftrags-Controller: zweite Zeile (im Moment Cvars) nur bei Bedarf laden.
[kivitendo-erp.git] / js / kivi.Order.js
1 namespace('kivi.Order', function(ns) {
2   ns.check_cv = function() {
3     if ($('#type').val() == 'sales_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_save_duplicate_parts = function() {
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       return confirm(kivi.t8("There are duplicate parts at positions") + "\n"
32                      + pos.join(', ') + "\n"
33                      + kivi.t8("Do you really want to save?"));
34     }
35     return true;
36   };
37
38   ns.save = function(warn_on_duplicates) {
39     if (!ns.check_cv()) return;
40     if (warn_on_duplicates && !ns.check_save_duplicate_parts()) return;
41
42     var data = $('#order_form').serializeArray();
43     data.push({ name: 'action', value: 'Order/save' });
44
45     $.post("controller.pl", data, kivi.eval_json_result);
46   };
47
48   ns.save_and_delivery_order = function(warn_on_duplicates) {
49     if (!ns.check_cv()) return;
50     if (warn_on_duplicates && !ns.check_save_duplicate_parts()) return;
51
52     var data = $('#order_form').serializeArray();
53     data.push({ name: 'action', value: 'Order/save_and_delivery_order' });
54
55     $.post("controller.pl", data, kivi.eval_json_result);
56   };
57
58   ns.delete_order = function() {
59     var data = $('#order_form').serializeArray();
60     data.push({ name: 'action', value: 'Order/delete' });
61
62     $.post("controller.pl", data, kivi.eval_json_result);
63   };
64
65   ns.show_print_options = function() {
66     if (!ns.check_cv()) return;
67
68     kivi.popup_dialog({
69       id: 'print_options',
70       dialog: {
71         title: kivi.t8('Print options'),
72         width:  800,
73         height: 300
74       }
75     });
76   };
77
78   ns.print = function() {
79     $('#print_options').dialog('close');
80
81     var data = $('#order_form').serializeArray();
82     data = data.concat($('#print_options_form').serializeArray());
83     data.push({ name: 'action', value: 'Order/print' });
84
85     $.post("controller.pl", data, kivi.eval_json_result);
86   };
87
88   ns.download_pdf = function(pdf_filename, key) {
89     var data = [];
90     data.push({ name: 'action', value: 'Order/download_pdf' });
91     data.push({ name: 'type', value: $('#type').val() });
92     data.push({ name: 'pdf_filename', value: pdf_filename });
93     data.push({ name: 'key', value: key });
94     $.download("controller.pl", data);
95   };
96
97   ns.email = function() {
98     if (!ns.check_cv()) return;
99     var data = $('#order_form').serializeArray();
100     data.push({ name: 'action', value: 'Order/show_email_dialog' });
101
102     $.post("controller.pl", data, kivi.eval_json_result);
103   };
104
105   var email_dialog;
106
107   ns.show_email_dialog = function(html) {
108     var id            = 'jqueryui_popup_dialog';
109     var dialog_params = {
110       id:     id,
111       width:  800,
112       height: 500,
113       modal:  true,
114       close: function(event, ui) {
115         email_dialog.remove();
116       },
117     };
118
119     $('#' + id).remove();
120
121     email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
122     email_dialog.html(html);
123     email_dialog.dialog(dialog_params);
124
125     $('.cancel').click(ns.close_email_dialog);
126
127     return true;
128   };
129
130   ns.send_email = function() {
131     var data = $('#order_form').serializeArray();
132     data = data.concat($('#email_form').serializeArray());
133     data.push({ name: 'action', value: 'Order/send_email' });
134     $.post("controller.pl", data, kivi.eval_json_result);
135   };
136
137   ns.close_email_dialog = function() {
138     email_dialog.dialog("close");
139   };
140
141   ns.reload_cv_dependant_selections = function() {
142     var data = $('#order_form').serializeArray();
143     data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
144
145     $.post("controller.pl", data, kivi.eval_json_result);
146   };
147
148   ns.reformat_number = function(event) {
149     $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
150   };
151
152   ns.recalc_amounts_and_taxes = function() {
153     var data = $('#order_form').serializeArray();
154     data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
155
156     $.post("controller.pl", data, kivi.eval_json_result);
157   };
158
159   ns.unit_change = function(event) {
160     var row = $(event.target).parents("tbody").first();
161     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
162     var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
163     var select_elt = $(row).find('[name="order.orderitems[].unit"]');
164
165     var oldval = $(select_elt).data('oldval');
166     $(select_elt).data('oldval', $(select_elt).val());
167
168     var data = $('#order_form').serializeArray();
169     data.push({ name: 'action', value: 'Order/unit_changed' });
170     data.push({ name: 'item_id', value: item_id_dom.val() });
171     data.push({ name: 'old_unit', value: oldval });
172     data.push({ name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
173
174     $.post("controller.pl", data, kivi.eval_json_result);
175   };
176
177   ns.update_sellprice = function(item_id, price_str) {
178     var row = $('#item_' + item_id).parents("tbody").first();
179     var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
180     var html_elt  = $(row).find('[name="sellprice_text"]');
181     price_elt.val(price_str);
182     html_elt.html(price_str);
183   };
184
185   ns.load_second_row = function(row) {
186     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
187     var div_elt = $(row).find('[name="second_row"]');
188
189     if ($(div_elt).data('loaded') == 1) {
190       return;
191     }
192     var data = $('#order_form').serializeArray();
193     data.push({ name: 'action', value: 'Order/load_second_row' });
194     data.push({ name: 'item_id', value: item_id_dom.val() });
195
196     $.post("controller.pl", data, kivi.eval_json_result);
197   };
198
199
200   ns.init_row_handlers = function() {
201     kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
202       $(elt).change(ns.recalc_amounts_and_taxes);
203     });
204
205     kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
206       $(elt).change(ns.reformat_number);
207     });
208
209     kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
210       $(elt).data('oldval', $(elt).val());
211       $(elt).change(ns.unit_change);
212     });
213
214     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
215       $(elt).keydown(function(event) {
216         if(event.keyCode == 40 && event.shiftKey === true) {
217           // shift arrow down
218           event.preventDefault();
219           var row = $(event.target).parents(".row_entry").first();
220           ns.load_second_row(row);
221           $(row).children().not(':first').show();
222           return false;
223         }
224         if(event.keyCode == 38 && event.shiftKey === true) {
225           // shift arrow up
226           event.preventDefault();
227           var row = $(event.target).parents(".row_entry").first();
228           $(row).children().not(':first').hide();
229           return false;
230         }
231       });
232       $(elt).dblclick(function(event) {
233         event.preventDefault();
234         var row = $(event.target).parents(".row_entry").first();
235         ns.load_second_row(row);
236         $(row).children().not(':first').toggle();
237         return false;
238       });
239     });
240   };
241
242   ns.redisplay_linetotals = function(data) {
243     $('.row_entry [name="linetotal"]').each(function(idx, elt) {
244       $(elt).html(data[idx]);
245     });
246   };
247
248   ns.renumber_positions = function() {
249     $('.row_entry [name="position"]').each(function(idx, elt) {
250       $(elt).html(idx+1);
251     });
252   };
253
254   ns.reorder_items = function(order_by) {
255     var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
256     $('#row_table_id thead a img').remove();
257
258     var src;
259     if (dir == "1") {
260       dir = "0";
261       src = "image/up.png";
262     } else {
263       dir = "1";
264       src = "image/down.png";
265     }
266
267     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
268
269     var data = $('#order_form').serializeArray();
270     data.push({ name: 'action', value: 'Order/reorder_items' });
271     data.push({ name: 'order_by', value: order_by });
272     data.push({ name: 'sort_dir', value: dir });
273
274     $.post("controller.pl", data, kivi.eval_json_result);
275   };
276
277   ns.redisplay_items = function(data) {
278     var old_rows = $('.row_entry').detach();
279     var new_rows = [];
280     $(data).each(function(idx, elt) {
281       new_rows.push(old_rows[elt.old_pos - 1]);
282     });
283     $(new_rows).appendTo($('#row_table_id'));
284     ns.renumber_positions();
285   };
286
287   ns.add_item = function() {
288     if ($('#add_item_parts_id').val() === '') return;
289     if (!ns.check_cv()) return;
290
291     $('#row_table_id thead a img').remove();
292
293     var data = $('#order_form').serializeArray();
294     data.push({ name: 'action', value: 'Order/add_item' });
295
296     $.post("controller.pl", data, kivi.eval_json_result);
297   };
298
299   ns.show_multi_items_dialog = function() {
300     if (!ns.check_cv()) return;
301
302     $('#row_table_id thead a img').remove();
303
304     kivi.popup_dialog({
305       url: 'controller.pl?action=Order/show_multi_items_dialog',
306       data: { type: $('#type').val(),
307               callback: 'Order/add_multi_items',
308               callback_data_id: 'order_form' },
309       id: 'jq_multi_items_dialog',
310       dialog: {
311         title: kivi.t8('Add multiple items'),
312         width:  800,
313         height: 500
314       }
315     });
316     return true;
317   };
318
319   ns.close_multi_items_dialog = function() {
320     $('#jq_multi_items_dialog').dialog('close');
321   };
322
323   ns.delete_order_item_row = function(clicked) {
324     var row = $(clicked).parents("tbody").first();
325     $(row).remove();
326
327     ns.renumber_positions();
328     ns.recalc_amounts_and_taxes();
329   };
330
331   ns.row_table_scroll_down = function() {
332     $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
333   };
334
335   ns.show_longdescription_dialog = function(clicked) {
336     var row = $(clicked).parents("tbody").first();
337     var position = $(row).find('[name="position"]').html();
338     var partnumber = $(row).find('[name="partnumber"]').html();
339     var description_elt = $(row).find('[name="order.orderitems[].description"]');
340     var description = description_elt.val();
341     var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
342     var longdescription;
343
344     if (!longdescription_elt.length) {
345       var data = [];
346       data.push({ name: 'action', value: 'Order/get_item_longdescription' });
347       data.push({ name: 'type', value: $('#type').val() });
348       data.push({ name: 'item_id', value: $(row).find('[name="order.orderitems[+].id"]').val() });
349       data.push({ name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() });
350       $.ajax({
351         url: 'controller.pl',
352         data: data,
353         method: "GET",
354         async: false,
355         dataType: 'text',
356         success: function(val){
357           longdescription = val;
358         }
359       });
360     } else {
361       longdescription = longdescription_elt.val();
362     }
363
364     var params = { runningnumber: position,
365                    partnumber: partnumber,
366                    description: description,
367                    default_longdescription: longdescription,
368                    set_function: function(val){
369                      longdescription_elt.remove();
370                      $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
371                    }
372                  };
373
374     kivi.SalesPurchase.edit_longdescription_with_params(params);
375   };
376
377   ns.price_chooser_item_row = function(clicked) {
378     var row = $(clicked).parents("tbody").first();
379     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
380
381     var data = $('#order_form').serializeArray();
382     data.push({ name: 'action', value: 'Order/price_popup' });
383     data.push({ name: 'item_id', value: item_id_dom.val() });
384
385     $.post("controller.pl", data, kivi.eval_json_result);
386   };
387
388   ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
389     var row = $('#item_' + item_id).parents("tbody").first();
390     var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
391     var button_elt = $(row).find('[name="price_chooser_button"]');
392
393     button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
394     source_elt.val(source);
395
396     var editable_div_elt = $(row).find('[name="editable_price"]');
397     var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
398     if (price_editable == 1 && source === '') {
399       // editable
400       $(editable_div_elt).show();
401       $(not_editable_div_elt).hide();
402       $(editable_div_elt).find(':input').prop("disabled", false);
403       $(not_editable_div_elt).find(':input').prop("disabled", true);
404     } else {
405       // not editable
406       $(editable_div_elt).hide();
407       $(not_editable_div_elt).show();
408       $(editable_div_elt).find(':input').prop("disabled", true);
409       $(not_editable_div_elt).find(':input').prop("disabled", false);
410     }
411
412     if (price_str) {
413       var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
414       var html_elt  = $(row).find('[name="sellprice_text"]');
415       price_elt.val(price_str);
416       html_elt.html(price_str);
417       ns.recalc_amounts_and_taxes();
418     }
419
420     kivi.io.close_dialog();
421   };
422
423   ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
424     var row = $('#item_' + item_id).parents("tbody").first();
425     var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
426     var button_elt = $(row).find('[name="price_chooser_button"]');
427
428     button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
429     source_elt.val(source);
430
431     var editable_div_elt = $(row).find('[name="editable_discount"]');
432     var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
433     if (price_editable == 1 && source === '') {
434       // editable
435       $(editable_div_elt).show();
436       $(not_editable_div_elt).hide();
437       $(editable_div_elt).find(':input').prop("disabled", false);
438       $(not_editable_div_elt).find(':input').prop("disabled", true);
439     } else {
440       // not editable
441       $(editable_div_elt).hide();
442       $(not_editable_div_elt).show();
443       $(editable_div_elt).find(':input').prop("disabled", true);
444       $(not_editable_div_elt).find(':input').prop("disabled", false);
445     }
446
447     if (discount_str) {
448       var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
449       var html_elt     = $(row).find('[name="discount_text"]');
450       discount_elt.val(discount_str);
451       html_elt.html(discount_str);
452       ns.recalc_amounts_and_taxes();
453     }
454
455     kivi.io.close_dialog();
456   };
457
458 });
459
460 $(function(){
461   if ($('#type').val() == 'sales_order') {
462     $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
463   } else {
464     $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
465   }
466
467   if ($('#type').val() == 'sales_order') {
468     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
469   } else {
470     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
471   }
472   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
473   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
474
475   $('.add_item_input').keydown(function(event) {
476     if(event.keyCode == 13) {
477       event.preventDefault();
478       kivi.Order.add_item();
479       return false;
480     }
481   });
482
483   kivi.Order.init_row_handlers();
484
485   $('#row_table_id').on('sortstop', function(event, ui) {
486     $('#row_table_id thead a img').remove();
487     kivi.Order.renumber_positions();
488   });
489 });