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