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