BankTransaction: Kreditorenvorlagen: Vorlage direkt laden, wenn genau 1 Treffer
[kivitendo-erp.git] / js / kivi.BankTransaction.js
1 namespace('kivi.BankTransaction', function(ns) {
2   "use strict";
3
4   ns.assign_invoice = function(bank_transaction_id) {
5     kivi.popup_dialog({
6       url:    'controller.pl?action=BankTransaction/assign_invoice',
7       data:   '&bt_id=' + bank_transaction_id,
8       type:   'POST',
9       id:     'assign_invoice_window',
10       dialog: { title: kivi.t8('Assign invoice') }
11     });
12     return true;
13   };
14
15   ns.add_invoices = function(bank_transaction_id, proposal_id) {
16
17     $.ajax({
18       url: 'controller.pl?action=BankTransaction/ajax_payment_suggestion&bt_id=' + bank_transaction_id  + '&prop_id=' + proposal_id,
19       success: function(data) {
20         $('#assigned_invoices_' + bank_transaction_id + "_" + proposal_id).html(data.html);
21         $('#sources_' + bank_transaction_id + "_" + proposal_id + ',' +
22           '#memos_'   + bank_transaction_id + "_" + proposal_id).show();
23         $('[data-proposal-id=' + proposal_id + ']').hide();
24
25         ns.update_invoice_amount(bank_transaction_id);
26       }
27     });
28   };
29
30   ns.delete_invoice = function(bank_transaction_id, proposal_id) {
31     var $inputs = $('#sources_' + bank_transaction_id + "_" + proposal_id + ',' +
32                     '#memos_'   + bank_transaction_id + "_" + proposal_id);
33
34     $('[data-proposal-id=' + proposal_id + ']').show();
35     $('#assigned_invoices_' + bank_transaction_id + "_" + proposal_id).html('');
36     $('#extra_row_' + bank_transaction_id + '_' + proposal_id).remove();
37
38     $inputs.hide();
39     $inputs.val('');
40
41     ns.update_invoice_amount(bank_transaction_id);
42   };
43
44   ns.create_invoice = function(bank_transaction_id) {
45     $.post('controller.pl?action=BankTransaction/create_invoice',
46            '&bt_id=' + bank_transaction_id + "&filter.bank_account=" + $('#filter_bank_account').val() + '&filter.fromdate=' + $('#filter_fromdate').val() + '&filter.todate=' + $('#filter_todate').val(),
47            kivi.eval_json_result);
48   };
49
50   ns.show_create_invoice_dialog = function(dialog_html) {
51     kivi.popup_dialog({
52       html:    dialog_html,
53       id:     'create_invoice_window',
54       dialog: { title: kivi.t8('Create invoice') }
55     });
56   };
57
58
59   ns.filter_invoices = function() {
60     var url="controller.pl?action=BankTransaction/ajax_add_list&" + $("#assign_invoice_window form").serialize();
61     $.ajax({
62       url: url,
63       success: function(data) {
64         $("#record_list_filtered_list").html(data.html);
65       }
66     });
67   }
68
69   ns.add_selected_invoices = function() {
70     var bank_transaction_id = $("#assign_invoice_window_form").data("bank-transaction-id");
71     var url                 ="controller.pl?action=BankTransaction/ajax_accept_invoices&bt_id=" + bank_transaction_id + '&' + $("#assign_invoice_window form").serialize();
72
73     $.ajax({
74       url: url,
75       success: function(new_html) {
76         $('#bt_rows_' + bank_transaction_id).append(new_html);
77         $('#assign_invoice_window').dialog('close');
78         ns.update_invoice_amount(bank_transaction_id);
79       }
80     });
81   }
82
83   ns.update_invoice_amount = function(bank_transaction_id) {
84     var $container = $('#invoice_amount_' + bank_transaction_id);
85     var amount     = $container.data('invoice-amount') * 1;
86
87     $('[id^="' + bank_transaction_id + '."]').each(function(idx, elt) {
88       if ($("input[name='skonto_pt." + elt.id + "']").val() == 1) {
89         // skonto payment term
90         amount += $(elt).data('invoice-amount-less-skonto');
91       } else {
92         // normal amount
93         amount += $(elt).data('invoice-amount');
94         //subtract free skonto if checked (no check for number!)
95         if ($("input[name='skonto_pt." + elt.id + "']").val() == 'free_skonto') {
96           amount -= $("input[name='free_skonto_amount." + elt.id + "']").val();
97         }
98       }
99     });
100
101     $container.html(kivi.format_amount(amount, 2));
102   };
103
104   ns.init_list = function(ui_tab) {
105     $('#check_all').checkall('INPUT[name^="proposal_ids"]');
106
107     $('.sort_link').each(function() {
108       var _href = $(this).attr("href");
109       $(this).attr("href", _href + "&filter.fromdate=" + $('#filter_fromdate').val() + "&filter.todate=" + $('#filter_todate').val());
110     });
111
112     $.cookie('jquery_ui_tab_bt_tabs', ui_tab);
113   };
114
115   ns.show_set_all_sources_memos_dialog = function(sources_selector, memos_selector) {
116     var dlg_id = 'set_all_sources_memos_dialog';
117     var $dlg   = $('#' + dlg_id);
118
119     $dlg.data('sources-selector', sources_selector);
120     $dlg.data('memos-selector',   memos_selector);
121
122     $('#set_all_sources').val('');
123     $('#set_all_memos').val('');
124
125     kivi.popup_dialog({
126       id: dlg_id,
127       dialog: {
128         title: kivi.t8('Set all source and memo fields')
129       }
130     });
131   };
132
133   ns.set_all_sources_memos = function(sources_selector, memos_selector) {
134     var $dlg = $('#set_all_sources_memos_dialog');
135
136     ['sources', 'memos'].forEach(function(type) {
137       var value = $('#set_all_' + type).val();
138       if (value !== '')
139         $($dlg.data(type + '-selector')).each(function(idx, input) {
140           $(input).val(value);
141         });
142     });
143
144     $dlg.dialog('close');
145   };
146
147   ns.filter_templates = function() {
148     var url="controller.pl?action=BankTransaction/filter_templates&" + $("#create_invoice_window form").serialize();
149     $.ajax({
150       url: url,
151       success: function(new_data) {
152         $("#templates").html(new_data.error || new_data.html);
153       }
154     });
155   };
156   ns.update_skonto = function(caller, bt_id, prop_id, formatted_amount_with_skonto_pt) {
157
158     if (caller.value === 'free_skonto') {
159       $('#free_skonto_amount_' + bt_id + '_' + prop_id).val("");
160       $('#free_skonto_amount_' + bt_id + '_' + prop_id).prop('disabled', false);
161       $("input[name='skonto_pt." + bt_id + '.' + prop_id + "']").val('free_skonto');
162       $('#free_skonto_amount_' + bt_id + '_' + prop_id).focus();
163     }
164     if (caller.value === 'without_skonto') {
165       $('#free_skonto_amount_' + bt_id + '_' + prop_id).val(kivi.format_amount(0,2));
166       $('#free_skonto_amount_' + bt_id + '_' + prop_id).prop('disabled', true);
167       $("input[name='skonto_pt." + bt_id + '.' + prop_id + "']").val(0);
168     }
169     if (caller.value === 'with_skonto_pt') {
170       $('#free_skonto_amount_' + bt_id + '_' + prop_id).val(formatted_amount_with_skonto_pt);
171       $('#free_skonto_amount_' + bt_id + '_' + prop_id).prop('disabled', true);
172       $("input[name='skonto_pt." + bt_id + '.' + prop_id + "']").val(1);
173     }
174     // recalc assigned amount
175     ns.update_invoice_amount(bt_id);
176   };
177
178 });