Auftrags-Controller: Warnung beim Speichern mit doppelten Artikeln.
[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_dependend_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.init_row_handlers = function() {
186     kivi.run_once_for('.recalc', 'on_change_recalc', function(elt) {
187       $(elt).change(ns.recalc_amounts_and_taxes);
188     });
189
190     kivi.run_once_for('.reformat_number', 'on_change_reformat', function(elt) {
191       $(elt).change(ns.reformat_number);
192     });
193
194     kivi.run_once_for('.unitselect', 'on_change_unit_with_oldval', function(elt) {
195       $(elt).data('oldval', $(elt).val());
196       $(elt).change(ns.unit_change);
197     });
198
199     kivi.run_once_for('.row_entry', 'on_kbd_click_show_hide', function(elt) {
200       $(elt).keydown(function(event) {
201         if(event.keyCode == 40 && event.shiftKey == true) {
202           // shift arrow down
203           event.preventDefault();
204           var row = $(event.target).parents(".row_entry").first();
205           $(row).children().not(':first').show();
206           return false;
207         }
208         if(event.keyCode == 38 && event.shiftKey == true) {
209           // shift arrow up
210           event.preventDefault();
211           var row = $(event.target).parents(".row_entry").first();
212           $(row).children().not(':first').hide();
213           return false;
214         }
215       });
216       $(elt).dblclick(function(event) {
217         event.preventDefault();
218         var row = $(event.target).parents(".row_entry").first();
219         $(row).children().not(':first').toggle();
220         return false;
221       });
222     });
223   };
224
225   ns.redisplay_linetotals = function(data) {
226     $('.row_entry [name="linetotal"]').each(function(idx, elt) {
227       $(elt).html(data[idx]);
228     });
229   };
230
231   ns.renumber_positions = function() {
232     $('.row_entry [name="position"]').each(function(idx, elt) {
233       $(elt).html(idx+1);
234     });
235   };
236
237   ns.reorder_items = function(order_by) {
238     var dir = $('#' + order_by + '_header_id a img').attr("data-sort-dir");
239     $('#row_table_id thead a img').remove();
240
241     var src;
242     if (dir == "1") {
243       dir = "0";
244       src = "image/up.png";
245     } else {
246       dir = "1";
247       src = "image/down.png";
248     };
249
250     $('#' + order_by + '_header_id a').append('<img border=0 data-sort-dir=' + dir + ' src=' + src + ' alt="' + kivi.t8('sort items') + '">');
251
252     var data = $('#order_form').serializeArray();
253     data.push({ name: 'action', value: 'Order/reorder_items' });
254     data.push({ name: 'order_by', value: order_by });
255     data.push({ name: 'sort_dir', value: dir });
256
257     $.post("controller.pl", data, kivi.eval_json_result);
258   };
259
260   ns.redisplay_items = function(data) {
261     var old_rows = $('.row_entry').detach();
262     var new_rows = [];
263     $(data).each(function(idx, elt) {
264       new_rows.push(old_rows[elt.old_pos - 1]);
265     });
266     $(new_rows).appendTo($('#row_table_id'));
267     ns.renumber_positions();
268   };
269
270   ns.add_item = function() {
271     if ($('#add_item_parts_id').val() == '') return;
272     if (!ns.check_cv()) return;
273
274     $('#row_table_id thead a img').remove();
275
276     var data = $('#order_form').serializeArray();
277     data.push({ name: 'action', value: 'Order/add_item' });
278
279     $.post("controller.pl", data, kivi.eval_json_result);
280   };
281
282   ns.show_multi_items_dialog = function() {
283     if (!ns.check_cv()) return;
284
285     $('#row_table_id thead a img').remove();
286
287     kivi.popup_dialog({
288       url: 'controller.pl?action=Order/show_multi_items_dialog',
289       data: { type: $('#type').val(),
290               callback: 'Order/add_multi_items',
291               callback_data_id: 'order_form' },
292       id: 'jq_multi_items_dialog',
293       dialog: {
294         title: kivi.t8('Add multiple items'),
295         width:  800,
296         height: 500
297       }
298     });
299     return true;
300   };
301
302   ns.close_multi_items_dialog = function() {
303     $('#jq_multi_items_dialog').dialog('close');
304   };
305
306   ns.delete_order_item_row = function(clicked) {
307     var row = $(clicked).parents("tbody").first();
308     $(row).remove();
309
310     ns.renumber_positions();
311     ns.recalc_amounts_and_taxes();
312   };
313
314   ns.row_table_scroll_down = function() {
315     $('#row_table_scroll_id').scrollTop($('#row_table_scroll_id')[0].scrollHeight);
316   };
317
318   ns.show_longdescription_dialog = function(clicked) {
319     var row = $(clicked).parents("tbody").first();
320     var position = $(row).find('[name="position"]').html();
321     var partnumber = $(row).find('[name="partnumber"]').html();
322     var description_elt = $(row).find('[name="order.orderitems[].description"]');
323     var description = description_elt.val();
324     var longdescription_elt = $(row).find('[name="order.orderitems[].longdescription"]');
325     var longdescription;
326
327     if (!longdescription_elt.length) {
328       var data = [];
329       data.push({ name: 'action', value: 'Order/get_item_longdescription' });
330       data.push({ name: 'type', value: $('#type').val() });
331       data.push({ name: 'item_id', value: $(row).find('[name="order.orderitems[+].id"]').val() });
332       data.push({ name: 'parts_id', value: $(row).find('[name="order.orderitems[].parts_id"]').val() });
333       $.ajax({
334         url: 'controller.pl',
335         data: data,
336         method: "GET",
337         async: false,
338         dataType: 'text',
339         success: function(val){
340           longdescription = val;
341         }
342       });
343     } else {
344       longdescription = longdescription_elt.val();
345     }
346
347     var params = { runningnumber: position,
348                    partnumber: partnumber,
349                    description: description,
350                    default_longdescription: longdescription,
351                    set_function: function(val){
352                      longdescription_elt.remove();
353                      $('<input type="hidden" name="order.orderitems[].longdescription">').insertAfter(description_elt).val(val);
354                    }
355                  };
356
357     kivi.SalesPurchase.edit_longdescription_with_params(params);
358   };
359
360   ns.price_chooser_item_row = function(clicked) {
361     var row = $(clicked).parents("tbody").first();
362     var item_id_dom = $(row).find('[name="orderitem_ids[+]"]');
363
364     var data = $('#order_form').serializeArray();
365     data.push({ name: 'action', value: 'Order/price_popup' });
366     data.push({ name: 'item_id', value: item_id_dom.val() });
367
368     $.post("controller.pl", data, kivi.eval_json_result);
369   };
370
371   ns.update_price_source = function(item_id, source, descr, price_str, price_editable) {
372     var row = $('#item_' + item_id).parents("tbody").first();
373     var source_elt = $(row).find('[name="order.orderitems[].active_price_source"]');
374     var button_elt = $(row).find('[name="price_chooser_button"]');
375
376     button_elt.val(button_elt.val().replace(/.*\|/, descr + " |"));
377     source_elt.val(source);
378
379     var editable_div_elt = $(row).find('[name="editable_price"]');
380     var not_editable_div_elt = $(row).find('[name="not_editable_price"]');
381     if (price_editable == 1 && source == '') {
382       // editable
383       $(editable_div_elt).show();
384       $(not_editable_div_elt).hide();
385       $(editable_div_elt).find(':input').prop("disabled", false);
386       $(not_editable_div_elt).find(':input').prop("disabled", true);
387     } else {
388       // not editable
389       $(editable_div_elt).hide();
390       $(not_editable_div_elt).show();
391       $(editable_div_elt).find(':input').prop("disabled", true);
392       $(not_editable_div_elt).find(':input').prop("disabled", false);
393     }
394
395     if (price_str) {
396       var price_elt = $(row).find('[name="order.orderitems[].sellprice_as_number"]');
397       var html_elt  = $(row).find('[name="sellprice_text"]');
398       price_elt.val(price_str);
399       html_elt.html(price_str);
400       ns.recalc_amounts_and_taxes();
401     }
402
403     kivi.io.close_dialog();
404   };
405
406   ns.update_discount_source = function(item_id, source, descr, discount_str, price_editable) {
407     var row = $('#item_' + item_id).parents("tbody").first();
408     var source_elt = $(row).find('[name="order.orderitems[].active_discount_source"]');
409     var button_elt = $(row).find('[name="price_chooser_button"]');
410
411     button_elt.val(button_elt.val().replace(/\|.*/, "| " + descr));
412     source_elt.val(source);
413
414     var editable_div_elt = $(row).find('[name="editable_discount"]');
415     var not_editable_div_elt = $(row).find('[name="not_editable_discount"]');
416     if (price_editable == 1 && source == '') {
417       // editable
418       $(editable_div_elt).show();
419       $(not_editable_div_elt).hide();
420       $(editable_div_elt).find(':input').prop("disabled", false);
421       $(not_editable_div_elt).find(':input').prop("disabled", true);
422     } else {
423       // not editable
424       $(editable_div_elt).hide();
425       $(not_editable_div_elt).show();
426       $(editable_div_elt).find(':input').prop("disabled", true);
427       $(not_editable_div_elt).find(':input').prop("disabled", false);
428     }
429
430     if (discount_str) {
431       var discount_elt = $(row).find('[name="order.orderitems[].discount_as_percent"]');
432       var html_elt     = $(row).find('[name="discount_text"]');
433       discount_elt.val(discount_str);
434       html_elt.html(discount_str);
435       ns.recalc_amounts_and_taxes();
436     }
437
438     kivi.io.close_dialog();
439   };
440
441 });
442
443 $(function(){
444   if ($('#type').val() == 'sales_order') {
445     $('#order_customer_id').change(kivi.Order.reload_cv_dependend_selections);
446   } else {
447     $('#order_vendor_id').change(kivi.Order.reload_cv_dependend_selections);
448   }
449
450   if ($('#type').val() == 'sales_order') {
451     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.sellprice, -2)) });
452   } else {
453     $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_sellprice_as_number').val(kivi.format_amount(o.lastcost, -2)) });
454   }
455   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_description').val(o.description) });
456   $('#add_item_parts_id').on('set_item:PartPicker', function(e,o) { $('#add_item_unit').val(o.unit) });
457
458   $('.add_item_input').keydown(function(event) {
459     if(event.keyCode == 13) {
460       event.preventDefault();
461       kivi.Order.add_item();
462       return false;
463     }
464   });
465
466   kivi.Order.init_row_handlers();
467
468   $('#row_table_id').on('sortstop', function(event, ui) {
469     $('#row_table_id thead a img').remove();
470     kivi.Order.renumber_positions();
471   });
472 });