epic-ts
[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 $container = $('#popup_edit_longdescription_input_container');
11     var $edit      = $('<textarea id="popup_edit_longdescription_input" class="texteditor-in-dialog" wrap="soft" style="width: 750px; height: 220px;"></textarea>');
12
13     $container.children().remove();
14     $container.append($edit);
15     $container.data('element', $element);
16
17     $edit.val($element.val());
18
19     kivi.init_text_editor($edit);
20
21     $('#popup_edit_longdescription_runningnumber').html(row);
22     $('#popup_edit_longdescription_partnumber').html($('#partnumber_' + row).val() || '');
23
24     var description = ($('#description_' + row).val() || '').replace(/[\n\r]+/, '');
25     if (description.length >= 50)
26       description = description.substring(0, 50) + "…";
27     $('#popup_edit_longdescription_description').html(description);
28
29     kivi.popup_dialog({
30       id:    'edit_longdescription_dialog',
31       dialog: {
32         title: kivi.t8('Enter longdescription'),
33         open:  function() { kivi.focus_ckeditor_when_ready('#popup_edit_longdescription_input'); },
34         close: function() { $('#popup_edit_longdescription_input_container').children().remove(); }
35       }
36     });
37   };
38
39   this.set_longdescription = function() {
40     $('#popup_edit_longdescription_input_container')
41       .data('element')
42       .val( $('#popup_edit_longdescription_input').val() );
43
44     $('#edit_longdescription_dialog').dialog('close');
45   };
46
47   this.delivery_order_check_transfer_qty = function() {
48     var all_match = true;
49     var rowcount  = $('input[name=rowcount]').val();
50     for (var i = 1; i < rowcount; i++)
51       if ($('#stock_in_out_qty_matches_' + i).val() != 1)
52         all_match = false;
53
54     if (all_match)
55       return true;
56
57     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?'));
58   };
59
60   this.oe_warn_save_active_periodic_invoice = function() {
61     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?'));
62   };
63
64   this.check_transaction_description = function() {
65     if ($('#transaction_description').val() != '')
66       return true;
67
68     alert(kivi.t8('A transaction description is required.'));
69     return false;
70   };
71
72   this.on_submit_checks = function() {
73     var $button = $(this);
74     if (($button.data('check-transfer-qty') == 1) && !kivi.SalesPurchase.delivery_order_check_transfer_qty())
75       return false;
76
77     if (($button.data('warn-save-active-periodic-invoice') == 1) && !kivi.SalesPurchase.oe_warn_save_active_periodic_invoice())
78       return false;
79
80     if (($button.data('require-transaction-description') == 1) && !kivi.SalesPurchase.check_transaction_description())
81       return false;
82
83     return true;
84   };
85
86   this.init_on_submit_checks = function() {
87      $('input[type=submit]').click(kivi.SalesPurchase.on_submit_checks);
88   };
89
90   this.set_duedate_on_reference_date_change = function(reference_field_id) {
91     setTimeout(function() {
92       var data = {
93         action:     'set_duedate',
94         invdate:    $('#' + reference_field_id).val(),
95         duedate:    $('#duedate').val(),
96         payment_id: $('#payment_id').val(),
97       };
98       $.post('is.pl', data, kivi.eval_json_result);
99     });
100   };
101 });