Auftrags-Controller: E-Mail-Dialog aus common verwenden.
[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       amount += $(elt).data('invoice-amount');
86     });
87
88     $container.html(kivi.format_amount(amount, 2));
89   };
90
91   ns.init_list = function(ui_tab) {
92     $('#check_all').checkall('INPUT[name^="proposal_ids"]');
93
94     $('.sort_link').each(function() {
95       var _href = $(this).attr("href");
96       $(this).attr("href", _href + "&filter.fromdate=" + $('#filter_fromdate').val() + "&filter.todate=" + $('#filter_todate').val());
97     });
98
99     $.cookie('jquery_ui_tab_bt_tabs', ui_tab);
100   };
101
102   ns.show_set_all_sources_memos_dialog = function(sources_selector, memos_selector) {
103     var dlg_id = 'set_all_sources_memos_dialog';
104     var $dlg   = $('#' + dlg_id);
105
106     $dlg.data('sources-selector', sources_selector);
107     $dlg.data('memos-selector',   memos_selector);
108
109     $('#set_all_sources').val('');
110     $('#set_all_memos').val('');
111
112     kivi.popup_dialog({
113       id: dlg_id,
114       dialog: {
115         title: kivi.t8('Set all source and memo fields')
116       }
117     });
118   };
119
120   ns.set_all_sources_memos = function(sources_selector, memos_selector) {
121     var $dlg = $('#set_all_sources_memos_dialog');
122
123     ['sources', 'memos'].forEach(function(type) {
124       var value = $('#set_all_' + type).val();
125       if (value !== '')
126         $($dlg.data(type + '-selector')).each(function(idx, input) {
127           $(input).val(value);
128         });
129     });
130
131     $dlg.dialog('close');
132   };
133
134   ns.filter_templates = function() {
135     var url="controller.pl?action=BankTransaction/filter_templates&" + $("#create_invoice_window form").serialize();
136     $.ajax({
137       url: url,
138       success: function(new_data) {
139         $("#templates").html(new_data.error || new_data.html);
140       }
141     });
142   };
143 });