Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[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_display_form {
36   $main::lxdebug->enter_sub();
37
38   my %params     = @_;
39   my $account    = $params{account} || {};
40   my $form       = $main::form;
41   my $locale     = $main::locale;
42
43   my $charts     = SL::Chart->list('link' => 'AP_paid');
44   my $label_sub  = sub { join '--', map { $_[0]->{$_} } qw(accno description) };
45
46   $form->{title} = $account->{id} ? $locale->text('Edit bank account') : $locale->text('Add bank account');
47
48   $form->header();
49   print $form->parse_html_template('bankaccounts/bank_account_display_form',
50                                    { 'CHARTS'      => $charts,
51                                      'account'     => $account,
52                                      'chart_label' => $label_sub,
53                                      'params'      => \%params });
54
55   $main::lxdebug->leave_sub();
56 }
57
58 sub bank_account_save {
59   $main::lxdebug->enter_sub();
60
61   my $form    = $main::form;
62   my $locale  = $main::locale;
63
64   my $account = $form->{account} && (ref $form->{account} eq 'HASH') ? $form->{account} : { };
65
66   if (any { !$account->{$_} } qw(account_number bank_code iban bic)) {
67     bank_account_display_form('account' => $account,
68                               'error'   => $locale->text('You have to fill in at least an account number, the bank code, the IBAN and the BIC.'));
69
70     $main::lxdebug->leave_sub();
71     return;
72   }
73
74   my $id = SL::BankAccount->save(%{ $account });
75
76   if ($form->{callback}) {
77     $form->redirect();
78
79   } else {
80     bank_account_edit('id' => $id);
81   }
82
83   $main::lxdebug->leave_sub();
84 }
85
86
87 sub bank_account_list {
88   $main::lxdebug->enter_sub();
89
90   my $form   = $main::form;
91   my $locale = $main::locale;
92
93   $form->{title}     = $locale->text('List of bank accounts');
94
95   $form->{sort}    ||= 'account_number';
96   $form->{sortdir}   = '1' if (!defined $form->{sortdir});
97
98   $form->{callback}  = build_std_url('action=bank_account_list', 'sort', 'sortdir');
99
100   my $accounts       = SL::BankAccount->list('sortorder' => $form->{sort},
101                                              'sortdir'   => $form->{sortdir});
102
103   my $report         = SL::ReportGenerator->new(\%main::myconfig, $form);
104
105   my $href           = build_std_url('action=bank_account_list');
106
107   my %column_defs = (
108     'account_number' => { 'text' => $locale->text('Account number'), },
109     'bank_code'      => { 'text' => $locale->text('Bank code'), },
110     'bank'           => { 'text' => $locale->text('Bank'), },
111     'bic'            => { 'text' => $locale->text('BIC'), },
112     'iban'           => { 'text' => $locale->text('IBAN'), },
113   );
114
115   my @columns = qw(account_number bank bank_code bic iban);
116
117   foreach my $name (@columns) {
118     my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
119     $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
120   }
121
122   $report->set_options('raw_bottom_info_text'  => $form->parse_html_template('bankaccounts/bank_account_list_bottom'),
123                        'std_column_visibility' => 1,
124                        'output_format'         => 'HTML',
125                        'title'                 => $form->{title},
126                        'attachment_basename'   => $locale->text('bankaccounts') . strftime('_%Y%m%d', localtime time),
127     );
128   $report->set_options_from_form();
129   $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
130
131   $report->set_columns(%column_defs);
132   $report->set_column_order(@columns);
133   $report->set_export_options('bank_account_list');
134   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
135
136   my $edit_url = build_std_url('action=bank_account_edit', 'callback');
137
138   foreach my $account (@{ $accounts }) {
139     my $row = { map { $_ => { 'data' => $account->{$_} } } keys %{ $account } };
140
141     $row->{account_number}->{link} = $edit_url . '&id=' . E($account->{id});
142
143     $report->add_data($row);
144   }
145
146   $report->generate_with_headers();
147
148   $main::lxdebug->leave_sub();
149 }
150
151 sub dispatcher {
152   my $form = $main::form;
153
154   foreach my $action (qw(bank_account_save bank_account_delete)) {
155     if ($form->{"action_${action}"}) {
156       call_sub($action);
157       return;
158     }
159   }
160
161   $form->error($main::locale->text('No action defined.'));
162 }
163
164 1;