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