Langtext-Dialog: Größe prozentual zum Hauptfenster einstellbar pro Benutzer
[kivitendo-erp.git] / js / kivi.SalesPurchase.js
1 namespace('kivi.SalesPurchase', function(ns) {
2   this.longdescription_dialog_size_percentage = 0;
3
4   this.edit_longdescription = function(row) {
5     var $element = $('#longdescription_' + row);
6
7     if (!$element.length) {
8       console.error("kivi.SalesPurchase.edit_longdescription: Element #longdescription_" + row + " not found");
9       return;
10     }
11
12     var params = { element: $element,
13                    runningnumber: row,
14                    partnumber: $('#partnumber_' + row).val() || '',
15                    description: $('#description_' + row).val() || '',
16                    default_longdescription: $('#longdescription_' + row).val() || ''
17                  };
18     this.edit_longdescription_with_params(params);
19   };
20
21   this.edit_longdescription_with_params = function(params) {
22     var dialog_width    = 800;
23     var dialog_height   = 500;
24     var textarea_width  = 750;
25     var textarea_height = 220;
26     if (this.longdescription_dialog_size_percentage != 0) {
27       dialog_width    = Math.ceil(window.innerWidth  * this.longdescription_dialog_size_percentage/100);
28       dialog_height   = Math.ceil(window.innerHeight * this.longdescription_dialog_size_percentage/100);
29       textarea_width  = Math.ceil(dialog_width * 95/100);
30       textarea_height = dialog_height - 220;
31       if (textarea_height <= 0) textarea_height = 220;
32     }
33
34     var $container = $('#popup_edit_longdescription_input_container');
35     var $edit      = $('<textarea id="popup_edit_longdescription_input" class="texteditor-in-dialog texteditor-space-for-toolbar" wrap="soft" style="width: ' + textarea_width + 'px; height: ' + textarea_height + 'px;"></textarea>');
36
37     $container.children().remove();
38     $container.append($edit);
39
40     if (params.element) {
41       $container.data('element', params.element);
42     }
43     if (params.set_function) {
44       $container.data('setFunction', params.set_function);
45     }
46
47     $edit.val(params.default_longdescription);
48
49     $('#popup_edit_longdescription_runningnumber').html(params.runningnumber);
50     $('#popup_edit_longdescription_partnumber').html(params.partnumber);
51
52     var description = params.description.replace(/[\n\r]+/, '');
53     if (description.length >= 50)
54       description = description.substring(0, 50) + "…";
55     $('#popup_edit_longdescription_description').html(description);
56
57     kivi.popup_dialog({
58       id:    'edit_longdescription_dialog',
59       dialog: {
60         title: kivi.t8('Enter longdescription'),
61         width:  dialog_width,
62         height: dialog_height,
63         open:  function() { kivi.focus_ckeditor_when_ready('#popup_edit_longdescription_input'); },
64         close: function() { $('#popup_edit_longdescription_input_container').children().remove(); }
65       }
66     });
67   };
68
69   this.set_longdescription = function() {
70     if ($('#popup_edit_longdescription_input_container').data('setFunction')) {
71       $('#popup_edit_longdescription_input_container').data('setFunction')($('#popup_edit_longdescription_input').val());
72     } else {
73       $('#popup_edit_longdescription_input_container')
74         .data('element')
75         .val( $('#popup_edit_longdescription_input').val() );
76     }
77     $('#edit_longdescription_dialog').dialog('close');
78   };
79
80   this.delivery_order_check_transfer_qty = function() {
81     var all_match = true;
82     var rowcount  = $('input[name=rowcount]').val();
83     for (var i = 1; i < rowcount; i++)
84       if ($('#stock_in_out_qty_matches_' + i).val() != 1)
85         all_match = false;
86
87     if (all_match)
88       return true;
89
90     return confirm(kivi.t8('There are still transfers not matching the qty of the delivery order. Stock operations can not be changed later. Do you really want to proceed?'));
91   };
92
93   this.oe_warn_save_active_periodic_invoice = function() {
94     return confirm(kivi.t8('This sales order has an active configuration for periodic invoices. If you save then all subsequently created invoices will contain those changes as well, but not those that have already been created. Do you want to continue?'));
95   };
96
97   this.check_transaction_description = function() {
98     if ($('#transaction_description').val() !== '')
99       return true;
100
101     alert(kivi.t8('A transaction description is required.'));
102     return false;
103   };
104
105   this.check_transport_cost_article_presence = function() {
106     var $form          = $('#form');
107     var wanted_part_id = $form.data('transport-cost-reminder-article-id');
108
109     if (!wanted_part_id)
110       return true;
111
112     var rowcount = $('#rowcount').val() * 1;
113     for (var row = 1; row <= rowcount; row++)
114       if (   (($('#id_'         + row).val() * 1)   === wanted_part_id)
115           && (($('#partnumber_' + row).val() || '') !== ''))
116         return true;
117
118     var description = $form.data('transport-cost-reminder-article-description');
119     return confirm(kivi.t8("The transport cost article '#1' is missing. Do you want to continue anyway?", [ description ]));
120   };
121
122   this.on_submit_checks = function() {
123     var $button = $(this);
124     if (($button.data('check-transfer-qty') == 1) && !kivi.SalesPurchase.delivery_order_check_transfer_qty())
125       return false;
126
127     if (($button.data('warn-save-active-periodic-invoice') == 1) && !kivi.SalesPurchase.oe_warn_save_active_periodic_invoice())
128       return false;
129
130     if (($button.data('require-transaction-description') == 1) && !kivi.SalesPurchase.check_transaction_description())
131       return false;
132
133     return true;
134   };
135
136   this.init_on_submit_checks = function() {
137      $('input[type=submit]').click(kivi.SalesPurchase.on_submit_checks);
138   };
139
140   this.set_duedate_on_reference_date_change = function(reference_field_id) {
141     setTimeout(function() {
142       var data = {
143         action:     'set_duedate',
144         invdate:    $('#' + reference_field_id).val(),
145         duedate:    $('#duedate').val(),
146         payment_id: $('#payment_id').val(),
147       };
148       $.post('is.pl', data, kivi.eval_json_result);
149     });
150   };
151
152   // Functions dialog with entering shipping addresses.
153   this.shipto_addresses = [];
154
155   this.copy_shipto_address = function () {
156     var shipto = this.shipto_addresses[ $('#shipto_to_copy').val() ];
157     for (var key in shipto)
158       $('#' + key).val(shipto[key]);
159   };
160
161   this.clear_shipto_fields = function() {
162     var shipto = this.shipto_addresses[0];
163     for (var key in shipto)
164       $('#' + key).val('');
165     $('#shiptocp_gender').val('m');
166   };
167
168   this.clear_shipto_id_before_submit = function() {
169     var shipto = this.shipto_addresses[0];
170     for (var key in shipto)
171       if ((key != 'shiptocp_gender') && ($('#' + key).val() !== '')) {
172         $('#shipto_id').val('');
173         break;
174       }
175   };
176
177   this.setup_shipto_dialog = function() {
178     var $dlg = $('#shipto_dialog');
179
180     $('#shipto_dialog [name^="shipto"]').each(function(idx, elt) {
181       $dlg.data("original-" + $(elt).prop("name"), $(elt).val());
182     });
183
184     $dlg.data('confirmed', false);
185
186     $('#shiptoname').focus();
187   };
188
189   this.submit_custom_shipto = function(id_selector) {
190     id_selector = id_selector || '#shipto_id';
191     $(id_selector).val('');
192     $('#shipto_dialog').data('confirmed', true);
193     $('#shipto_dialog').dialog('close');
194   };
195
196   this.reset_shipto_fields = function() {
197     var $dlg = $('#shipto_dialog');
198
199     $('#shipto_dialog [name^="shipto"]').each(function(idx, elt) {
200       $(elt).val($dlg.data("original-" + $(elt).prop("name")));
201     });
202   };
203
204   this.finish_shipto_dialog = function() {
205     if (!$('#shipto_dialog').data('confirmed'))
206       kivi.SalesPurchase.reset_shipto_fields();
207
208     $('#shipto_dialog').children().remove().appendTo('#shipto_inputs');
209
210     return true;
211   };
212
213   this.edit_custom_shipto = function() {
214     $('#shipto_inputs').children().remove().appendTo('#shipto_dialog');
215
216     kivi.popup_dialog({
217       id:    'shipto_dialog',
218       dialog: {
219         height: 600,
220         title:  kivi.t8('Edit custom shipto'),
221         open:   kivi.SalesPurchase.setup_shipto_dialog,
222         close:  kivi.SalesPurchase.finish_shipto_dialog,
223       }
224     });
225   };
226
227   this.show_print_options_elements = function(elements, show) {
228     $(elements).each(function(idx, elt) {
229       var $elements = $('#print_options_header_' + elt + ',#print_options_input_' + elt);
230       if (show)
231         $elements.show();
232       else
233         $elements.hide();
234     });
235   };
236
237   this.show_all_print_options_elements = function() {
238     kivi.SalesPurchase.show_print_options_elements([ 'formname', 'language_id', 'format', 'sendmode', 'media', 'printer_id', 'copies', 'groupitems', 'remove_draft' ], true);
239   };
240
241   // Sending records via email.
242   this.check_required_email_fields = function() {
243     var unset = $('#email_form_to,#email_form_subject,#email_form_message').filter(function(idx, elt) {
244       return $(elt).val() === '';
245     });
246
247     if (unset.length === 0)
248       return true;
249
250     alert(kivi.t8("The recipient, subject or body is missing."));
251     $(unset[0]).focus();
252
253     return false;
254   };
255
256   this.send_email = function() {
257     if (!kivi.SalesPurchase.check_required_email_fields())
258       return false;
259
260     // ckeditor gets de-initialized when removing the children from
261     // the DOM. Therefore we have to manually preserve its content
262     // over the children's relocation.
263
264     var message = $('#email_form_message').val();
265
266     $('#send_email_dialog').children().remove().appendTo('#email_inputs');
267     $('#send_email_dialog').dialog('close');
268
269     $('#email_form_message').val(message);
270
271     kivi.submit_form_with_action('#form', $('#form').data('send-email-action'));
272
273     return true;
274   };
275
276   this.setup_send_email_dialog = function() {
277     kivi.SalesPurchase.show_all_print_options_elements();
278     kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
279
280     $('#print_options').children().remove().appendTo('#email_form_print_options');
281
282     kivi.reinit_widgets();
283
284     var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
285     $('#email_form_' + to_focus).focus();
286   };
287
288   this.finish_send_email_dialog = function() {
289     $('#email_form_print_options').children().remove().appendTo('#print_options');
290     return true;
291   };
292
293   this.show_email_dialog = function(send_action, vc, vc_id_selector) {
294     $('#form').data('send-email-action', send_action || 'send_sales_purchase_email');
295
296     vc             = vc             || $('#vc').val();
297     vc_id_selector = vc_id_selector || '#' + vc + '_id';
298     var vc_id = $(vc_id_selector).val();
299
300     var data = {
301       action:       'show_sales_purchase_email_dialog',
302       cp_id:        $('#cp_id').val(),
303       direct_debit: $('#direct_debit').prop('checked') ? 1 : 0,
304       donumber:     $('#donumber').val(),
305       format:       $('#format').val(),
306       formname:     $('#formname').val(),
307       id:           $('#id').val(),
308       invnumber:    $('#invnumber').val(),
309       language_id:  $('#language_id').val(),
310       media:        'email',
311       ordnumber:    $('#ordnumber').val(),
312       cusordnumber: $('#cusordnumber').val(),
313       rowcount:     $('#rowcount').val(),
314       quonumber:    $('#quonumber').val(),
315       type:         $('#type').val(),
316       vc:           vc,
317       vc_id:        vc_id,
318       project_id:  $('#globalproject_id').val(),
319     };
320
321     $('[name^=id_],[name^=partnumber_]').each(function(idx, elt) {
322       var val = $(elt).val() || '';
323       if (val !== '')
324         data[ $(elt).attr('name') ] = val;
325     });
326
327     kivi.popup_dialog({
328       id:     'send_email_dialog',
329       url:    'io.pl',
330       load:   kivi.SalesPurchase.setup_send_email_dialog,
331       data:   data,
332       dialog: {
333         height:      600,
334         title:       kivi.t8('Send email'),
335         beforeClose: kivi.SalesPurchase.finish_send_email_dialog
336       }
337     });
338
339     return true;
340   };
341
342   this.activate_send_email_actions_regarding_printout = function() {
343     var selected = $('#email_form_attachment_policy').val();
344     $('#email_form_attachment_filename').parents('tr')[selected !== 'no_file' ? 'show' : 'hide']();
345     $('#email_form_print_options')[selected !== 'no_file' ? 'show' : 'hide']();
346   };
347
348   // Printing records.
349   this.setup_print_dialog = function() {
350     kivi.SalesPurchase.show_all_print_options_elements();
351
352     $('#print_options').children().remove().appendTo('#print_dialog_print_options');
353
354     $('#print_dialog_print_button').focus();
355   };
356
357   this.finish_print_dialog = function() {
358     $('#print_dialog_print_options').children().remove().appendTo('#print_options');
359   };
360
361   this.print_record = function() {
362     $('#print_dialog').dialog('close');
363
364     var action = $('#form').data('print-action');
365     if (action.match("^js:"))
366       return kivi.run(action.substring(3));
367
368     kivi.submit_form_with_action('#form', action);
369   };
370
371   this.show_print_dialog = function(print_action) {
372     $('#form').data('print-action', print_action || 'print');
373
374     kivi.popup_dialog({
375       id:    'print_dialog',
376       dialog: {
377         height: 600,
378         title:  kivi.t8('Print record'),
379         open:   kivi.SalesPurchase.setup_print_dialog,
380         close:  kivi.SalesPurchase.finish_print_dialog,
381       }
382     });
383   };
384 });