ActionBar: Verwendung bei Briefen
[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" 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     kivi.init_text_editor($edit);
36
37     $('#popup_edit_longdescription_runningnumber').html(params.runningnumber);
38     $('#popup_edit_longdescription_partnumber').html(params.partnumber);
39
40     var description = params.description.replace(/[\n\r]+/, '');
41     if (description.length >= 50)
42       description = description.substring(0, 50) + "…";
43     $('#popup_edit_longdescription_description').html(description);
44
45     kivi.popup_dialog({
46       id:    'edit_longdescription_dialog',
47       dialog: {
48         title: kivi.t8('Enter longdescription'),
49         open:  function() { kivi.focus_ckeditor_when_ready('#popup_edit_longdescription_input'); },
50         close: function() { $('#popup_edit_longdescription_input_container').children().remove(); }
51       }
52     });
53   };
54
55   this.set_longdescription = function() {
56     if ($('#popup_edit_longdescription_input_container').data('setFunction')) {
57       $('#popup_edit_longdescription_input_container').data('setFunction')($('#popup_edit_longdescription_input').val());
58     } else {
59       $('#popup_edit_longdescription_input_container')
60         .data('element')
61         .val( $('#popup_edit_longdescription_input').val() );
62     }
63     $('#edit_longdescription_dialog').dialog('close');
64   };
65
66   this.delivery_order_check_transfer_qty = function() {
67     var all_match = true;
68     var rowcount  = $('input[name=rowcount]').val();
69     for (var i = 1; i < rowcount; i++)
70       if ($('#stock_in_out_qty_matches_' + i).val() != 1)
71         all_match = false;
72
73     if (all_match)
74       return true;
75
76     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?'));
77   };
78
79   this.oe_warn_save_active_periodic_invoice = function() {
80     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?'));
81   };
82
83   this.check_transaction_description = function() {
84     if ($('#transaction_description').val() !== '')
85       return true;
86
87     alert(kivi.t8('A transaction description is required.'));
88     return false;
89   };
90
91   this.check_transport_cost_article_presence = function() {
92     var $form          = $('#form');
93     var wanted_part_id = $form.data('transport-cost-reminder-article-id');
94
95     if (!wanted_part_id)
96       return true;
97
98     var rowcount = $('#rowcount').val() * 1;
99     for (var row = 1; row <= rowcount; row++)
100       if (   (($('#id_'         + row).val() * 1)   === wanted_part_id)
101           && (($('#partnumber_' + row).val() || '') !== ''))
102         return true;
103
104     var description = $form.data('transport-cost-reminder-article-description');
105     return confirm(kivi.t8("The transport cost article '#1' is missing. Do you want to continue anyway?", [ description ]));
106   };
107
108   this.on_submit_checks = function() {
109     var $button = $(this);
110     if (($button.data('check-transfer-qty') == 1) && !kivi.SalesPurchase.delivery_order_check_transfer_qty())
111       return false;
112
113     if (($button.data('warn-save-active-periodic-invoice') == 1) && !kivi.SalesPurchase.oe_warn_save_active_periodic_invoice())
114       return false;
115
116     if (($button.data('require-transaction-description') == 1) && !kivi.SalesPurchase.check_transaction_description())
117       return false;
118
119     return true;
120   };
121
122   this.init_on_submit_checks = function() {
123      $('input[type=submit]').click(kivi.SalesPurchase.on_submit_checks);
124   };
125
126   this.set_duedate_on_reference_date_change = function(reference_field_id) {
127     setTimeout(function() {
128       var data = {
129         action:     'set_duedate',
130         invdate:    $('#' + reference_field_id).val(),
131         duedate:    $('#duedate').val(),
132         payment_id: $('#payment_id').val(),
133       };
134       $.post('is.pl', data, kivi.eval_json_result);
135     });
136   };
137
138   // Functions dialog with entering shipping addresses.
139   this.shipto_addresses = [];
140
141   this.copy_shipto_address = function () {
142     var shipto = this.shipto_addresses[ $('#shipto_to_copy').val() ];
143     for (var key in shipto)
144       $('#' + key).val(shipto[key]);
145   };
146
147   this.clear_shipto_fields = function() {
148     var shipto = this.shipto_addresses[0];
149     for (var key in shipto)
150       $('#' + key).val('');
151     $('#shiptocp_gender').val('m');
152   };
153
154   this.clear_shipto_id_before_submit = function() {
155     var shipto = this.shipto_addresses[0];
156     for (var key in shipto)
157       if ((key != 'shiptocp_gender') && ($('#' + key).val() !== '')) {
158         $('#shipto_id').val('');
159         break;
160       }
161   };
162
163   this.setup_shipto_dialog = function() {
164     var $dlg = $('#shipto_dialog');
165
166     $('#shipto_dialog [name^="shipto"]').each(function(idx, elt) {
167       $dlg.data("original-" + $(elt).prop("name"), $(elt).val());
168     });
169
170     $dlg.data('confirmed', false);
171
172     $('#shiptoname').focus();
173   };
174
175   this.submit_custom_shipto = function() {
176     $('#shipto_id').val('');
177     $('#shipto_dialog').data('confirmed', true);
178     $('#shipto_dialog').dialog('close');
179   };
180
181   this.reset_shipto_fields = function() {
182     var $dlg = $('#shipto_dialog');
183
184     $('#shipto_dialog [name^="shipto"]').each(function(idx, elt) {
185       $(elt).val($dlg.data("original-" + $(elt).prop("name")));
186     });
187   };
188
189   this.finish_shipto_dialog = function() {
190     if (!$('#shipto_dialog').data('confirmed'))
191       kivi.SalesPurchase.reset_shipto_fields();
192
193     $('#shipto_dialog').children().remove().appendTo('#shipto_inputs');
194
195     return true;
196   };
197
198   this.edit_custom_shipto = function() {
199     $('#shipto_inputs').children().remove().appendTo('#shipto_dialog');
200
201     kivi.popup_dialog({
202       id:    'shipto_dialog',
203       dialog: {
204         height: 600,
205         title:  kivi.t8('Edit custom shipto'),
206         open:   kivi.SalesPurchase.setup_shipto_dialog,
207         close:  kivi.SalesPurchase.finish_shipto_dialog,
208       }
209     });
210   };
211
212   this.show_print_options_elements = function(elements, show) {
213     $(elements).each(function(idx, elt) {
214       var $elements = $('#print_options_header_' + elt + ',#print_options_input_' + elt);
215       if (show)
216         $elements.show();
217       else
218         $elements.hide();
219     });
220   };
221
222   this.show_all_print_options_elements = function() {
223     kivi.SalesPurchase.show_print_options_elements([ 'formname', 'language_id', 'format', 'sendmode', 'media', 'printer_id', 'copies', 'groupitems', 'remove_draft' ], true);
224   };
225
226   // Sending records via email.
227   this.check_required_email_fields = function() {
228     var unset = $('#email_form_to,#email_form_subject,#email_form_message').filter(function(idx, elt) {
229       return $(elt).val() === '';
230     });
231
232     if (unset.length === 0)
233       return true;
234
235     alert(kivi.t8("The recipient, subject or body is missing."));
236     $(unset[0]).focus();
237
238     return false;
239   };
240
241   this.send_email = function() {
242     if (!kivi.SalesPurchase.check_required_email_fields())
243       return false;
244
245     $('#send_email_dialog').children().remove().appendTo('#email_inputs');
246     $('#send_email_dialog').dialog('close');
247
248     kivi.submit_form_with_action('#form', 'send_sales_purchase_email');
249
250     return true;
251   };
252
253   this.setup_send_email_dialog = function() {
254     kivi.SalesPurchase.show_all_print_options_elements();
255     kivi.SalesPurchase.show_print_options_elements([ 'sendmode', 'media', 'copies', 'remove_draft' ], false);
256
257     $('#print_options').children().remove().appendTo('#email_form_print_options');
258
259     var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
260     $('#email_form_' + to_focus).focus();
261   };
262
263   this.finish_send_email_dialog = function() {
264     $('#email_form_print_options').children().remove().appendTo('#print_options');
265     return true;
266   };
267
268   this.show_email_dialog = function() {
269     kivi.popup_dialog({
270       id:     'send_email_dialog',
271       url:    'io.pl',
272       load:   kivi.SalesPurchase.setup_send_email_dialog,
273       data:   {
274         action:      'show_sales_purchase_email_dialog',
275         type:        $('#type').val(),
276         formname:    $('#formname').val(),
277         format:      $('#format').val(),
278         media:       'email',
279         ordnumber:   $('#ordnumber').val(),
280         donumber:    $('#donumber').val(),
281         invnumber:   $('#invnumber').val(),
282         quonumber:   $('#quonumber').val(),
283         cp_id:       $('#cp_id').val(),
284         language_id: $('#language_id').val(),
285       },
286       dialog: {
287         height:      600,
288         title:       kivi.t8('Send email'),
289         beforeClose: kivi.SalesPurchase.finish_send_email_dialog
290       }
291     });
292
293     return true;
294   };
295
296   // Printing records.
297   this.setup_print_dialog = function() {
298     kivi.SalesPurchase.show_all_print_options_elements();
299
300     $('#print_options').children().remove().appendTo('#print_dialog_print_options');
301
302     $('#print_dialog_print_button').focus();
303   };
304
305   this.finish_print_dialog = function() {
306     $('#print_dialog_print_options').children().remove().appendTo('#print_options');
307   };
308
309   this.print_record = function() {
310     $('#print_dialog').dialog('close');
311
312     kivi.submit_form_with_action('#form', $('#form').data('print-action'));
313   };
314
315   this.show_print_dialog = function(print_action) {
316     $('#form').data('print-action', print_action || 'print');
317
318     kivi.popup_dialog({
319       id:    'print_dialog',
320       dialog: {
321         height: 600,
322         title:  kivi.t8('Print record'),
323         open:   kivi.SalesPurchase.setup_print_dialog,
324         close:  kivi.SalesPurchase.finish_print_dialog,
325       }
326     });
327   };
328 });