b75621721fefc19ace93a2e7b7ac82b4e47719b3
[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     kivi.popup_dialog({
46       url:    'controller.pl?action=BankTransaction/create_invoice',
47       data:   '&bt_id=' + bank_transaction_id + "&filter.bank_account=" + $('#filter_bank_account').val() + '&filter.fromdate=' + $('#filter_fromdate').val() + '&filter.todate=' + $('#filter_todate').val(),
48       type:   'POST',
49       id:     'create_invoice_window',
50       dialog: { title: kivi.t8('Create invoice') }
51     });
52     return true;
53   };
54
55
56   ns.filter_invoices = function() {
57     var url="controller.pl?action=BankTransaction/ajax_add_list&" + $("#assign_invoice_window form").serialize();
58     $.ajax({
59       url: url,
60       success: function(data) {
61         $("#record_list_filtered_list").html(data.html);
62       }
63     });
64   }
65
66   ns.add_selected_invoices = function() {
67     var bank_transaction_id = $("#assign_invoice_window_form").data("bank-transaction-id");
68     var url                 ="controller.pl?action=BankTransaction/ajax_accept_invoices&bt_id=" + bank_transaction_id + '&' + $("#assign_invoice_window form").serialize();
69
70     $.ajax({
71       url: url,
72       success: function(new_html) {
73         $('#bt_rows_' + bank_transaction_id).append(new_html);
74         $('#assign_invoice_window').dialog('close');
75         ns.update_invoice_amount(bank_transaction_id);
76       }
77     });
78   }
79
80   ns.update_invoice_amount = function(bank_transaction_id) {
81     var $container = $('#invoice_amount_' + bank_transaction_id);
82     var amount     = $container.data('invoice-amount') * 1;
83
84     $('[id^="' + bank_transaction_id + '."]').each(function(idx, elt) {
85       if ($("input[name='skonto_pt." + elt.id + "']").val() == 1) {
86         // skonto payment term
87         amount += $(elt).data('invoice-amount-less-skonto');
88       } else {
89         // normal amount
90         amount += $(elt).data('invoice-amount');
91         //subtract free skonto if checked (no check for number!)
92         if ($("input[name='skonto_pt." + elt.id + "']").val() == 'free_skonto') {
93           amount -= $("input[name='free_skonto_amount." + elt.id + "']").val();
94         }
95       }
96     });
97
98     $container.html(kivi.format_amount(amount, 2));
99   };
100
101   ns.init_list = function(ui_tab) {
102     $('#check_all').checkall('INPUT[name^="proposal_ids"]');
103
104     $('.sort_link').each(function() {
105       var _href = $(this).attr("href");
106       $(this).attr("href", _href + "&filter.fromdate=" + $('#filter_fromdate').val() + "&filter.todate=" + $('#filter_todate').val());
107     });
108
109     $.cookie('jquery_ui_tab_bt_tabs', ui_tab);
110   };
111
112   ns.show_set_all_sources_memos_dialog = function(sources_selector, memos_selector) {
113     var dlg_id = 'set_all_sources_memos_dialog';
114     var $dlg   = $('#' + dlg_id);
115
116     $dlg.data('sources-selector', sources_selector);
117     $dlg.data('memos-selector',   memos_selector);
118
119     $('#set_all_sources').val('');
120     $('#set_all_memos').val('');
121
122     kivi.popup_dialog({
123       id: dlg_id,
124       dialog: {
125         title: kivi.t8('Set all source and memo fields')
126       }
127     });
128   };
129
130   ns.set_all_sources_memos = function(sources_selector, memos_selector) {
131     var $dlg = $('#set_all_sources_memos_dialog');
132
133     ['sources', 'memos'].forEach(function(type) {
134       var value = $('#set_all_' + type).val();
135       if (value !== '')
136         $($dlg.data(type + '-selector')).each(function(idx, input) {
137           $(input).val(value);
138         });
139     });
140
141     $dlg.dialog('close');
142   };
143
144   ns.filter_templates = function() {
145     var url="controller.pl?action=BankTransaction/filter_templates&" + $("#create_invoice_window form").serialize();
146     $.ajax({
147       url: url,
148       success: function(new_data) {
149         $("#templates").html(new_data.error || new_data.html);
150       }
151     });
152   };
153   ns.update_skonto = function(caller, bt_id, prop_id, formatted_amount_with_skonto_pt) {
154
155     if (caller.value === 'free_skonto') {
156       $('#free_skonto_amount_' + bt_id + '_' + prop_id).val("");
157       $('#free_skonto_amount_' + bt_id + '_' + prop_id).prop('disabled', false);
158       $("input[name='skonto_pt." + bt_id + '.' + prop_id + "']").val('free_skonto');
159       $('#free_skonto_amount_' + bt_id + '_' + prop_id).focus();
160     }
161     if (caller.value === 'without_skonto') {
162       $('#free_skonto_amount_' + bt_id + '_' + prop_id).val(kivi.format_amount(0,2));
163       $('#free_skonto_amount_' + bt_id + '_' + prop_id).prop('disabled', true);
164       $("input[name='skonto_pt." + bt_id + '.' + prop_id + "']").val(0);
165     }
166     if (caller.value === 'with_skonto_pt') {
167       $('#free_skonto_amount_' + bt_id + '_' + prop_id).val(formatted_amount_with_skonto_pt);
168       $('#free_skonto_amount_' + bt_id + '_' + prop_id).prop('disabled', true);
169       $("input[name='skonto_pt." + bt_id + '.' + prop_id + "']").val(1);
170     }
171     // recalc assigned amount
172     ns.update_invoice_amount(bt_id);
173   };
174
175 });