Bankkonten einen Namen geben
[kivitendo-erp.git] / bin / mozilla / bankaccounts.pl
1 use strict;
2
3 use List::MoreUtils qw(any);
4 use POSIX qw(strftime);
5
6 use SL::BankAccount;
7 use SL::Chart;
8 use SL::Form;
9 use SL::ReportGenerator;
10
11 require "bin/mozilla/common.pl";
12 require "bin/mozilla/reportgenerator.pl";
13
14 sub bank_account_add {
15   $main::lxdebug->enter_sub();
16
17   bank_account_display_form('account' => {});
18
19   $main::lxdebug->leave_sub();
20 }
21
22 sub bank_account_edit {
23   $main::lxdebug->enter_sub();
24
25   my %params  = @_;
26   my $form    = $main::form;
27
28   my $account = SL::BankAccount->retrieve('id' => $params{id} || $form->{id});
29
30   bank_account_display_form('account' => $account);
31
32   $main::lxdebug->leave_sub();
33 }
34
35 sub bank_account_delete {
36   $::lxdebug->enter_sub();
37
38   SL::BankAccount->delete(id => $::form->{account}{id});
39
40   print $::form->redirect_header('bankaccounts.pl?action=bank_account_list');
41
42   $::lxdebug->leave_sub();
43 }
44
45 sub bank_account_display_form {
46   $main::lxdebug->enter_sub();
47
48   my %params     = @_;
49   my $account    = $params{account} || {};
50   my $form       = $main::form;
51   my $locale     = $main::locale;
52
53   my $charts     = SL::Chart->list('link' => 'AP_paid');
54   my $label_sub  = sub { join '--', map { $_[0]->{$_} } qw(accno description) };
55
56   $form->{title} = $account->{id} ? $locale->text('Edit bank account') : $locale->text('Add bank account');
57
58   $form->header();
59   print $form->parse_html_template('bankaccounts/bank_account_display_form',
60                                    { 'CHARTS'      => $charts,
61                                      'account'     => $account,
62                                      'chart_label' => $label_sub,
63                                      'params'      => \%params });
64
65   $main::lxdebug->leave_sub();
66 }
67
68 sub bank_account_save {
69   $main::lxdebug->enter_sub();
70
71   my $form    = $main::form;
72   my $locale  = $main::locale;
73
74   my $account = $form->{account} && (ref $form->{account} eq 'HASH') ? $form->{account} : { };
75
76   if (any { !$account->{$_} } qw(name account_number bank_code iban bic)) {
77     bank_account_display_form('account' => $account,
78                               'error'   => $locale->text('You have to fill in at least a name, an account number, the bank code, the IBAN and the BIC.'));
79
80     $main::lxdebug->leave_sub();
81     return;
82   }
83
84   my $id = SL::BankAccount->save(%{ $account });
85
86   if ($form->{callback}) {
87     $form->redirect();
88
89   } else {
90     bank_account_edit('id' => $id);
91   }
92
93   $main::lxdebug->leave_sub();
94 }
95
96
97 sub bank_account_list {
98   $main::lxdebug->enter_sub();
99
100   my $form   = $main::form;
101   my $locale = $main::locale;
102
103   $form->{title}     = $locale->text('List of bank accounts');
104
105   $form->{sort}    ||= 'account_number';
106   $form->{sortdir}   = '1' if (!defined $form->{sortdir});
107
108   $form->{callback}  = build_std_url('action=bank_account_list', 'sort', 'sortdir');
109
110   my $accounts       = SL::BankAccount->list('sortorder' => $form->{sort},
111                                              'sortdir'   => $form->{sortdir});
112
113   my $report         = SL::ReportGenerator->new(\%main::myconfig, $form);
114
115   my $href           = build_std_url('action=bank_account_list');
116
117   my %column_defs = (
118     'name'           => { 'text' => $locale->text('Name'), },
119     'account_number' => { 'text' => $locale->text('Account number'), },
120     'bank_code'      => { 'text' => $locale->text('Bank code'), },
121     'bank'           => { 'text' => $locale->text('Bank'), },
122     'bic'            => { 'text' => $locale->text('BIC'), },
123     'iban'           => { 'text' => $locale->text('IBAN'), },
124   );
125
126   my @columns = qw(name account_number bank bank_code bic iban);
127
128   foreach my $name (@columns) {
129     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
130     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
131   }
132
133   $report->set_options('raw_bottom_info_text'  => $form->parse_html_template('bankaccounts/bank_account_list_bottom'),
134                        'std_column_visibility' => 1,
135                        'output_format'         => 'HTML',
136                        'title'                 => $form->{title},
137                        'attachment_basename'   => $locale->text('bankaccounts') . strftime('_%Y%m%d', localtime time),
138     );
139   $report->set_options_from_form();
140   $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
141
142   $report->set_columns(%column_defs);
143   $report->set_column_order(@columns);
144   $report->set_export_options('bank_account_list');
145   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
146
147   my $edit_url = build_std_url('action=bank_account_edit', 'callback');
148
149   foreach my $account (@{ $accounts }) {
150     my $row = { map { $_ => { 'data' => $account->{$_} } } keys %{ $account } };
151
152     $row->{account_number}->{link} = $edit_url . '&id=' . E($account->{id});
153
154     $report->add_data($row);
155   }
156
157   $report->generate_with_headers();
158
159   $main::lxdebug->leave_sub();
160 }
161
162 sub dispatcher {
163   my $form = $main::form;
164
165   foreach my $action (qw(bank_account_save bank_account_delete)) {
166     if ($form->{"action_${action}"}) {
167       call_sub($action);
168       return;
169     }
170   }
171
172   $form->error($main::locale->text('No action defined.'));
173 }
174
175 1;