Auftrags-Controller: Workflow -> Rechnung (Speichern und Rechnung erfassen)
[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.save_and_invoice = function(warn_on_duplicates) {
59     if (!ns.check_cv()) return;
60     if (warn_on_duplicates && !ns.check_save_duplicate_parts()) return;
61
62     var data = $('#order_form').serializeArray();
63     data.push({ name: 'action', value: 'Order/save_and_invoice' });
64
65     $.post("controller.pl", data, kivi.eval_json_result);
66   };
67
68   ns.delete_order = function() {
69     var data = $('#order_form').serializeArray();
70     data.push({ name: 'action', value: 'Order/delete' });
71
72     $.post("controller.pl", data, kivi.eval_json_result);
73   };
74
75   ns.show_print_options = function() {
76     if (!ns.check_cv()) return;
77
78     kivi.popup_dialog({
79       id: 'print_options',
80       dialog: {
81         title: kivi.t8('Print options'),
82         width:  800,
83         height: 300
84       }
85     });
86   };
87
88   ns.print = function() {
89     $('#print_options').dialog('close');
90
91     var data = $('#order_form').serializeArray();
92     data = data.concat($('#print_options_form').serializeArray());
93     data.push({ name: 'action', value: 'Order/print' });
94
95     $.post("controller.pl", data, kivi.eval_json_result);
96   };
97
98   ns.download_pdf = function(pdf_filename, key) {
99     var data = [];
100     data.push({ name: 'action', value: 'Order/download_pdf' });
101     data.push({ name: 'type', value: $('#type').val() });
102     data.push({ name: 'pdf_filename', value: pdf_filename });
103     data.push({ name: 'key', value: key });
104     $.download("controller.pl", data);
105   };
106
107   ns.email = function() {
108     if (!ns.check_cv()) return;
109     var data = $('#order_form').serializeArray();
110     data.push({ name: 'action', value: 'Order/show_email_dialog' });
111
112     $.post("controller.pl", data, kivi.eval_json_result);
113   };
114
115   var email_dialog;
116
117   ns.show_email_dialog = function(html) {
118     var id            = 'jqueryui_popup_dialog';
119     var dialog_params = {
120       id:     id,
121       width:  800,
122       height: 500,
123       modal:  true,
124       close: function(event, ui) {
125         email_dialog.remove();
126       },
127     };
128
129     $('#' + id).remove();
130
131     email_dialog = $('<div style="display:none" id="' + id + '"></div>').appendTo('body');
132     email_dialog.html(html);
133     email_dialog.dialog(dialog_params);
134
135     $('.cancel').click(ns.close_email_dialog);
136
137     return true;
138   };
139
140   ns.send_email = function() {
141     var data = $('#order_form').serializeArray();
142     data = data.concat($('#email_form').serializeArray());
143     data.push({ name: 'action', value: 'Order/send_email' });
144     $.post("controller.pl", data, kivi.eval_json_result);
145   };
146
147   ns.close_email_dialog = function() {
148     email_dialog.dialog("close");
149   };
150
151   ns.reload_cv_dependant_selections = function() {
152     var data = $('#order_form').serializeArray();
153     data.push({ name: 'action', value: 'Order/customer_vendor_changed' });
154
155     $.post("controller.pl", data, kivi.eval_json_result);
156   };
157
158   ns.reformat_number = function(event) {
159     $(event.target).val(kivi.format_amount(kivi.parse_amount($(event.target).val()), -2));
160   };
161
162   ns.recalc_amounts_and_taxes = function() {
163     var data = $('#order_form').serializeArray();
164     data.push({ name: 'action', value: 'Order/recalc_amounts_and_taxes' });
165
166     $.post("controller.pl", data, kivi.eval_json_result);
167   };
168
169   ns.unit_change = function(event) {
170     var row = $(event.target).parents("tbody").first();
171     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
172     var sellprice_dom = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
173     var select_elt = $(row).find('[name="order.orderitems[].unit"]');
174
175     var oldval = $(select_elt).data('oldval');
176     $(select_elt).data('oldval', $(select_elt).val());
177
178     var data = $('#order_form').serializeArray();
179     data.push({ name: 'action', value: 'Order/unit_changed' });
180     data.push({ name: 'item_id', value: item_id_dom.val() });
181     data.push({ name: 'old_unit', value: oldval });
182     data.push({ name: 'sellprice_dom_id', value: sellprice_dom.attr('id') });
183
184     $.post("controller.pl", data, kivi.eval_json_result);
185   };
186
187   ns.update_sellprice = function(item_id, price_str) {
188     var row = $('#item_' + item_id).parents("tbody").first();
189     var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
190     var html_elt  = $(row).find('[name="sellprice_text"]');
191     price_elt.val(price_str);
192     html_elt.html(price_str);
193   };
194
195   ns.load_second_row = function(row) {
196     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
197     var div_elt = $(row).find('[name="second_row"]');
198
199     if ($(div_elt).data('loaded') == 1) {
200       return;
201     }
202     var data = $('#order_form').serializeArray();
203     data.push({ name: 'action', value: 'Order/load_second_rows' });
204     data.push({ name: 'item_ids[]', value: item_id_dom.val() });
205
206     $.post("controller.pl", data, kivi.eval_json_result);
207   };
208
209   ns.load_all_second_rows = function() {
210     var rows = $('.row_entry').filter(function(idx, elt) {
211       return $(elt).find('[name="second_row"]').data('loaded') != 1;
212     });
213
214     var item_ids = $.map(rows, function(elt) {
215       var item_id = $(elt).find('[name="orderitem_ids[+]"]').val();
216       return { name: 'item_ids[]', value: item_id };
217     });
218
219     if (item_ids.length == 0) {
220       return;
221     }
222
223     var data = $('#order_form').serializeArray();
224     data.push({ name: 'action', value: 'Order/load_second_rows' });
225     data = data.concat(item_ids);
226
227     $.post("controller.pl", data, kivi.eval_json_result);
228   };
229
230   ns.hide_second_row = function(row) {
231     $(row).children().not(':first').hide();
232     $(row).data('expanded', 0);
233     var elt = $(row).find('.expand');
234     elt.attr('src', "image/expand.svg");
235     elt.attr('alt', kivi.t8('Show details'));
236     elt.attr('title', kivi.t8('Show details'));
237   };
238
239   ns.show_second_row = function(row) {
240     $(row).children().not(':first').show();
241     $(row).data('expanded', 1);
242     var elt = $(row).find('.expand');
243     elt.attr('src', "image/collapse.svg");
244     elt.attr('alt', kivi.t8('Hide details'));
245     elt.attr('title', kivi.t8('Hide details'));
246   };
247
248   ns.toggle_second_row = function(row) {
249     if ($(row).data('expanded') == 1) {
250       ns.hide_second_row(row);
251     } else {
252       ns.show_second_row(row);
253     }
254   };
255
256   ns.init_row_handlers = function() {
257     kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
258       $(elt).change(ns.recalc_amounts_and_taxes);
259     });
260
261     kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
262       $(elt).change(ns.reformat_number);
263     });
264
265     kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
266       $(elt).data('oldval', $(elt).val());
267       $(elt).change(ns.unit_change);
268     });
269
270     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
271       $(elt).keydown(function(event) {
272         if(event.keyCode == 40 && event.shiftKey === true) {
273           // shift arrow down
274           event.preventDefault();
275           var row = $(event.target).parents(".row_entry").first();
276           ns.load_second_row(row);
277           ns.show_second_row(row);
278           return false;
279         }
280         if(event.keyCode == 38 && event.shiftKey === true) {
281           // shift arrow up
282           event.preventDefault();
283           var row = $(event.target).parents(".row_entry").first();
284           ns.hide_second_row(row);
285           return false;
286         }
287       });
288     });
289
290     kivi.run_once_for('.expand', 'expand_second_row', function(elt) {
291       $(elt).click(function(event) {
292         event.preventDefault();
293         var row = $(event.target).parents(".row_entry").first();
294         ns.load_second_row(row);
295         ns.toggle_second_row(row);
296         return false;
297       })
298     });
299
300   };
301
302   ns.redisplay_line_values = function(is_sales, data) {
303     $('.row_entry').each(function(idx, elt) {
304       $(elt).find('[name="linetotal"]').html(data[idx][0]);
305       if (is_sales && $(elt).find('[name="second_row"]').data('loaded') == 1) {
306         var mt = data[idx][1];
307         var mp = data[idx][2];
308         var h  = '<span';
309         if (mt[0] === '-') h += ' class="plus0"';
310         h += '>' + mt + '&nbsp;&nbsp;' + mp + '%';
311         h += '</span>';
312         $(elt).find('[name="linemargin"]').html(h);
313       }
314     });
315   };
316
317   ns.renumber_positions = function() {
318     $('.row_entry [name="position"]').each(function(idx, elt) {
319       $(elt).html(idx+1);
320     });
321   };
322
323   ns.reorder_items = function(order_by) {
324     var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
325     $('#row_table_id thead a img').remove();
326
327     var src;
328     if (dir == "1") {
329       dir = "0";
330       src = "image/up.png";
331     } else {
332       dir = "1";
333       src = "image/down.png";
334     }
335
336     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
337
338     var data = $('#order_form').serializeArray();
339     data.push({ name: 'action', value: 'Order/reorder_items' });
340     data.push({ name: 'order_by', value: order_by });
341     data.push({ name: 'sort_dir', value: dir });
342
343     $.post("controller.pl", data, kivi.eval_json_result);
344   };
345
346   ns.redisplay_items = function(data) {
347     var old_rows = $('.row_entry').detach();
348     var new_rows = [];
349     $(data).each(function(idx, elt) {
350       new_rows.push(old_rows[elt.old_pos - 1]);
351     });
352     $(new_rows).appendTo($('#row_table_id'));
353     ns.renumber_positions();
354   };
355
356   ns.add_item = function() {
357     if ($('#add_item_parts_id').val() === '') return;
358     if (!ns.check_cv()) return;
359
360     $('#row_table_id thead a img').remove();
361
362     var data = $('#order_form').serializeArray();
363     data.push({ name: 'action', value: 'Order/add_item' });
364
365     $.post("controller.pl", data, kivi.eval_json_result);
366   };
367
368   ns.show_multi_items_dialog = function() {
369     if (!ns.check_cv()) return;
370
371     $('#row_table_id thead a img').remove();
372
373     kivi.popup_dialog({
374       url: 'controller.pl?action=Order/show_multi_items_dialog',
375       data: { type: $('#type').val(),
376               callback: 'Order/add_multi_items',
377               callback_data_id: 'order_form' },
378       id: 'jq_multi_items_dialog',
379       dialog: {
380         title: kivi.t8('Add multiple items'),
381         width:  800,
382         height: 500
383       }
384     });
385     return true;
386   };
387
388   ns.close_multi_items_dialog = function() {
389     $('#jq_multi_items_dialog').dialog('close');
390   };
391
392   ns.delete_order_item_row = function(clicked) {
393     var row = $(clicked).parents("tbody").first();
394     $(row).remove();
395
396     ns.renumber_positions();
397     ns.recalc_amounts_and_taxes();
398   };
399
400   ns.row_table_scroll_down = function() {
401     $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
402   };
403
404   ns.show_longdescription_dialog = function(clicked) {
405     var row = $(clicked).parents("tbody").first();
406     var position = $(row).find('[name="position"]').html();
407     var partnumber = $(row).find('[name="partnumber"]').html();
408     var description_elt = $(row).find('[name="order.orderitems[].description"]');
409     var description = description_elt.val();
410     var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
411     var longdescription;
412
413     if (!longdescription_elt.length) {
414       var data = [];
415       data.push({ name: 'action', value: 'Order/get_item_longdescription' });
416       data.push({ name: 'type', value: $('#type').val() });
417       data.push({ name: 'item_id', value: $(row).find('[name="order.orderitems[+].id"]').val() });
418       data.push({ name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() });
419       $.ajax({
420         url: 'controller.pl',
421         data: data,
422         method: "GET",
423         async: false,
424         dataType: 'text',
425         success: function(val){
426           longdescription = val;
427         }
428       });
429     } else {
430       longdescription = longdescription_elt.val();
431     }
432
433     var params = { runningnumber: position,
434                    partnumber: partnumber,
435                    description: description,
436                    default_longdescription: longdescription,
437                    set_function: function(val){
438                      longdescription_elt.remove();
439                      $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
440                    }
441                  };
442
443     kivi.SalesPurchase.edit_longdescription_with_params(params);
444   };
445
446   ns.price_chooser_item_row = function(clicked) {
447     var row = $(clicked).parents("tbody").first();
448     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
449
450     var data = $('#order_form').serializeArray();
451     data.push({ name: 'action', value: 'Order/price_popup' });
452     data.push({ name: 'item_id', value: item_id_dom.val() });
453
454     $.post("controller.pl", data, kivi.eval_json_result);
455   };
456
457   ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
458     var row = $('#item_' + item_id).parents("tbody").first();
459     var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
460     var button_elt = $(row).find('[name="price_chooser_button"]');
461
462     button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
463     source_elt.val(source);
464
465     var editable_div_elt = $(row).find('[name="editable_price"]');
466     var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
467     if (price_editable == 1 && source === '') {
468       // editable
469       $(editable_div_elt).show();
470       $(not_editable_div_elt).hide();
471       $(editable_div_elt).find(':input').prop("disabled", false);
472       $(not_editable_div_elt).find(':input').prop("disabled", true);
473     } else {
474       // not editable
475       $(editable_div_elt).hide();
476       $(not_editable_div_elt).show();
477       $(editable_div_elt).find(':input').prop("disabled", true);
478       $(not_editable_div_elt).find(':input').prop("disabled", false);
479     }
480
481     if (price_str) {
482       var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
483       var html_elt  = $(row).find('[name="sellprice_text"]');
484       price_elt.val(price_str);
485       html_elt.html(price_str);
486       ns.recalc_amounts_and_taxes();
487     }
488
489     kivi.io.close_dialog();
490   };
491
492   ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
493     var row = $('#item_' + item_id).parents("tbody").first();
494     var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
495     var button_elt = $(row).find('[name="price_chooser_button"]');
496
497     button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
498     source_elt.val(source);
499
500     var editable_div_elt = $(row).find('[name="editable_discount"]');
501     var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
502     if (price_editable == 1 && source === '') {
503       // editable
504       $(editable_div_elt).show();
505       $(not_editable_div_elt).hide();
506       $(editable_div_elt).find(':input').prop("disabled", false);
507       $(not_editable_div_elt).find(':input').prop("disabled", true);
508     } else {
509       // not editable
510       $(editable_div_elt).hide();
511       $(not_editable_div_elt).show();
512       $(editable_div_elt).find(':input').prop("disabled", true);
513       $(not_editable_div_elt).find(':input').prop("disabled", false);
514     }
515
516     if (discount_str) {
517       var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
518       var html_elt     = $(row).find('[name="discount_text"]');
519       discount_elt.val(discount_str);
520       html_elt.html(discount_str);
521       ns.recalc_amounts_and_taxes();
522     }
523
524     kivi.io.close_dialog();
525   };
526
527 });
528
529 $(function(){
530   if ($('#type').val() == 'sales_order') {
531     $('#order_customer_id').change(kivi.Order.reload_cv_dependant_selections);
532   } else {
533     $('#order_vendor_id').change(kivi.Order.reload_cv_dependant_selections);
534   }
535
536   if ($('#type').val() == 'sales_order') {
537     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
538   } else {
539     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
540   }
541   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
542   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
543
544   $('.add_item_input').keydown(function(event) {
545     if(event.keyCode == 13) {
546       event.preventDefault();
547       kivi.Order.add_item();
548       return false;
549     }
550   });
551
552   kivi.Order.init_row_handlers();
553
554   $('#row_table_id').on('sortstop', function(event, ui) {
555     $('#row_table_id thead a img').remove();
556     kivi.Order.renumber_positions();
557   });
558
559   $('#expand_all').on('click', function(event) {
560     event.preventDefault();
561     if ($('#expand_all').data('expanded') == 1) {
562       $('#expand_all').data('expanded', 0);
563       $('#expand_all').attr('src', 'image/expand.svg');
564       $('#expand_all').attr('alt', kivi.t8('Show all details'));
565       $('#expand_all').attr('title', kivi.t8('Show all details'));
566       $('.row_entry').each(function(idx, elt) {
567         kivi.Order.hide_second_row(elt);
568       });
569     } else {
570       $('#expand_all').data('expanded', 1);
571       $('#expand_all').attr('src', "image/collapse.svg");
572       $('#expand_all').attr('alt', kivi.t8('Hide all details'));
573       $('#expand_all').attr('title', kivi.t8('Hide all details'));
574       kivi.Order.load_all_second_rows();
575       $('.row_entry').each(function(idx, elt) {
576         kivi.Order.show_second_row(elt);
577       });
578     }
579     return false;
580   });
581
582 });