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