1 namespace('kivi.SalesPurchase', function(ns) {
 
   2   this.longdescription_dialog_size_percentage = 0;
 
   4   this.edit_longdescription = function(row) {
 
   5     var $element = $('#longdescription_' + row);
 
   7     if (!$element.length) {
 
   8       console.error("kivi.SalesPurchase.edit_longdescription: Element #longdescription_" + row + " not found");
 
  12     var params = { element: $element,
 
  14                    partnumber: $('#partnumber_' + row).val() || '',
 
  15                    description: $('#description_' + row).val() || '',
 
  16                    default_longdescription: $('#longdescription_' + row).val() || ''
 
  18     this.edit_longdescription_with_params(params);
 
  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;
 
  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>');
 
  37     $container.children().remove();
 
  38     $container.append($edit);
 
  41       $container.data('element', params.element);
 
  43     if (params.set_function) {
 
  44       $container.data('setFunction', params.set_function);
 
  47     $edit.val(params.default_longdescription);
 
  49     $('#popup_edit_longdescription_runningnumber').html(params.runningnumber);
 
  50     $('#popup_edit_longdescription_partnumber').html(params.partnumber);
 
  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);
 
  58       id:    'edit_longdescription_dialog',
 
  60         title: kivi.t8('Enter longdescription'),
 
  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(); }
 
  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());
 
  73       $('#popup_edit_longdescription_input_container')
 
  75         .val( $('#popup_edit_longdescription_input').val() );
 
  77     $('#edit_longdescription_dialog').dialog('close');
 
  80   this.delivery_order_check_transfer_qty = function() {
 
  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)
 
  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?'));
 
  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?'));
 
  97   this.check_transaction_description = function() {
 
  98     if ($('#transaction_description').val() !== '')
 
 101     alert(kivi.t8('A transaction description is required.'));
 
 105   this.check_transport_cost_article_presence = function() {
 
 106     var $form          = $('#form');
 
 107     var wanted_part_id = $form.data('transport-cost-reminder-article-id');
 
 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() || '') !== ''))
 
 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 ]));
 
 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())
 
 127     if (($button.data('warn-save-active-periodic-invoice') == 1) && !kivi.SalesPurchase.oe_warn_save_active_periodic_invoice())
 
 130     if (($button.data('require-transaction-description') == 1) && !kivi.SalesPurchase.check_transaction_description())
 
 136   this.init_on_submit_checks = function() {
 
 137      $('input[type=submit]').click(kivi.SalesPurchase.on_submit_checks);
 
 140   this.set_duedate_on_reference_date_change = function(reference_field_id) {
 
 141     setTimeout(function() {
 
 143         action:     'set_duedate',
 
 144         invdate:    $('#' + reference_field_id).val(),
 
 145         duedate:    $('#duedate').val(),
 
 146         payment_id: $('#payment_id').val(),
 
 148       $.post('is.pl', data, kivi.eval_json_result);
 
 152   // Functions dialog with entering shipping addresses.
 
 153   this.shipto_addresses = [];
 
 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]);
 
 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');
 
 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('');
 
 177   this.setup_shipto_dialog = function() {
 
 178     var $dlg = $('#shipto_dialog');
 
 180     $('#shipto_dialog [name^="shipto"]').each(function(idx, elt) {
 
 181       $dlg.data("original-" + $(elt).prop("name"), $(elt).val());
 
 184     $dlg.data('confirmed', false);
 
 186     $('#shiptoname').focus();
 
 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');
 
 196   this.reset_shipto_fields = function() {
 
 197     var $dlg = $('#shipto_dialog');
 
 199     $('#shipto_dialog [name^="shipto"]').each(function(idx, elt) {
 
 200       $(elt).val($dlg.data("original-" + $(elt).prop("name")));
 
 204   this.finish_shipto_dialog = function() {
 
 205     if (!$('#shipto_dialog').data('confirmed'))
 
 206       kivi.SalesPurchase.reset_shipto_fields();
 
 208     $('#shipto_dialog').children().remove().appendTo('#shipto_inputs');
 
 213   this.edit_custom_shipto = function() {
 
 214     $('#shipto_inputs').children().remove().appendTo('#shipto_dialog');
 
 220         title:  kivi.t8('Edit custom shipto'),
 
 221         open:   kivi.SalesPurchase.setup_shipto_dialog,
 
 222         close:  kivi.SalesPurchase.finish_shipto_dialog,
 
 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);
 
 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);
 
 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() === '';
 
 247     if (unset.length === 0)
 
 250     alert(kivi.t8("The recipient, subject or body is missing."));
 
 256   this.send_email = function() {
 
 257     if (!kivi.SalesPurchase.check_required_email_fields())
 
 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.
 
 264     var message = $('#email_form_message').val();
 
 266     $('#send_email_dialog').children().remove().appendTo('#email_inputs');
 
 267     $('#send_email_dialog').dialog('close');
 
 269     $('#email_form_message').val(message);
 
 271     kivi.submit_form_with_action('#form', $('#form').data('send-email-action'));
 
 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);
 
 280     $('#print_options').children().remove().appendTo('#email_form_print_options');
 
 282     kivi.reinit_widgets();
 
 284     var to_focus = $('#email_form_to').val() === '' ? 'to' : 'subject';
 
 285     $('#email_form_' + to_focus).focus();
 
 288   this.finish_send_email_dialog = function() {
 
 289     $('#email_form_print_options').children().remove().appendTo('#print_options');
 
 293   this.show_email_dialog = function(send_action, vc, vc_id_selector) {
 
 294     $('#form').data('send-email-action', send_action || 'send_sales_purchase_email');
 
 296     vc             = vc             || $('#vc').val();
 
 297     vc_id_selector = vc_id_selector || '#' + vc + '_id';
 
 298     var vc_id = $(vc_id_selector).val();
 
 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(),
 
 308       invnumber:    $('#invnumber').val(),
 
 309       language_id:  $('#language_id').val(),
 
 311       ordnumber:    $('#ordnumber').val(),
 
 312       cusordnumber: $('#cusordnumber').val(),
 
 313       rowcount:     $('#rowcount').val(),
 
 314       quonumber:    $('#quonumber').val(),
 
 315       type:         $('#type').val(),
 
 318       project_id:  $('#globalproject_id').val(),
 
 321     $('[name^=id_],[name^=partnumber_]').each(function(idx, elt) {
 
 322       var val = $(elt).val() || '';
 
 324         data[ $(elt).attr('name') ] = val;
 
 328       id:     'send_email_dialog',
 
 330       load:   kivi.SalesPurchase.setup_send_email_dialog,
 
 334         title:       kivi.t8('Send email'),
 
 335         beforeClose: kivi.SalesPurchase.finish_send_email_dialog
 
 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']();
 
 349   this.setup_print_dialog = function() {
 
 350     kivi.SalesPurchase.show_all_print_options_elements();
 
 352     $('#print_options').children().remove().appendTo('#print_dialog_print_options');
 
 354     $('#print_dialog_print_button').focus();
 
 357   this.finish_print_dialog = function() {
 
 358     $('#print_dialog_print_options').children().remove().appendTo('#print_options');
 
 361   this.print_record = function() {
 
 362     $('#print_dialog').dialog('close');
 
 364     var action = $('#form').data('print-action');
 
 365     if (action.match("^js:"))
 
 366       return kivi.run(action.substring(3));
 
 368     kivi.submit_form_with_action('#form', action);
 
 371   this.show_print_dialog = function(print_action) {
 
 372     $('#form').data('print-action', print_action || 'print');
 
 378         title:  kivi.t8('Print record'),
 
 379         open:   kivi.SalesPurchase.setup_print_dialog,
 
 380         close:  kivi.SalesPurchase.finish_print_dialog,