a11fb6bb6bc5e56f6269d4f27a182cf897dd6055
[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(account_number bank_code iban bic)) {
77     bank_account_display_form('account' => $account,
78                               'error'   => $locale->text('You have to fill in at least 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     'account_number' => { 'text' => $locale->text('Account number'), },
119     'bank_code'      => { 'text' => $locale->text('Bank code'), },
120     'bank'           => { 'text' => $locale->text('Bank'), },
121     'bic'            => { 'text' => $locale->text('BIC'), },
122     'iban'           => { 'text' => $locale->text('IBAN'), },
123   );
124
125   my @columns = qw(account_number bank bank_code bic iban);
126
127   foreach my $name (@columns) {
128     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
129     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
130   }
131
132   $report->set_options('raw_bottom_info_text'  => $form->parse_html_template('bankaccounts/bank_account_list_bottom'),
133                        'std_column_visibility' => 1,
134                        'output_format'         => 'HTML',
135                        'title'                 => $form->{title},
136                        'attachment_basename'   => $locale->text('bankaccounts') . strftime('_%Y%m%d', localtime time),
137     );
138   $report->set_options_from_form();
139   $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
140
141   $report->set_columns(%column_defs);
142   $report->set_column_order(@columns);
143   $report->set_export_options('bank_account_list');
144   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
145
146   my $edit_url = build_std_url('action=bank_account_edit', 'callback');
147
148   foreach my $account (@{ $accounts }) {
149     my $row = { map { $_ => { 'data' => $account->{$_} } } keys %{ $account } };
150
151     $row->{account_number}->{link} = $edit_url . '&id=' . E($account->{id});
152
153     $report->add_data($row);
154   }
155
156   $report->generate_with_headers();
157
158   $main::lxdebug->leave_sub();
159 }
160
161 sub dispatcher {
162   my $form = $main::form;
163
164   foreach my $action (qw(bank_account_save bank_account_delete)) {
165     if ($form->{"action_${action}"}) {
166       call_sub($action);
167       return;
168     }
169   }
170
171   $form->error($main::locale->text('No action defined.'));
172 }
173
174 1;