3 use List::MoreUtils qw(any);
4 use POSIX qw(strftime);
9 use SL::ReportGenerator;
11 require "bin/mozilla/common.pl";
12 require "bin/mozilla/reportgenerator.pl";
14 sub bank_account_add {
15 $main::lxdebug->enter_sub();
17 bank_account_display_form('account' => {});
19 $main::lxdebug->leave_sub();
22 sub bank_account_edit {
23 $main::lxdebug->enter_sub();
26 my $form = $main::form;
28 my $account = SL::BankAccount->retrieve('id' => $params{id} || $form->{id});
30 bank_account_display_form('account' => $account);
32 $main::lxdebug->leave_sub();
35 sub bank_account_delete {
36 $::lxdebug->enter_sub();
38 SL::BankAccount->delete(id => $::form->{account}{id});
40 print $::form->redirect_header('bankaccounts.pl?action=bank_account_list');
42 $::lxdebug->leave_sub();
45 sub bank_account_display_form {
46 $main::lxdebug->enter_sub();
49 my $account = $params{account} || {};
50 my $form = $main::form;
51 my $locale = $main::locale;
53 my $charts = SL::Chart->list('link' => 'AP_paid');
54 my $label_sub = sub { join '--', map { $_[0]->{$_} } qw(accno description) };
56 $form->{title} = $account->{id} ? $locale->text('Edit bank account') : $locale->text('Add bank account');
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 });
65 $main::lxdebug->leave_sub();
68 sub bank_account_save {
69 $main::lxdebug->enter_sub();
71 my $form = $main::form;
72 my $locale = $main::locale;
74 my $account = $form->{account} && (ref $form->{account} eq 'HASH') ? $form->{account} : { };
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.'));
80 $main::lxdebug->leave_sub();
84 my $id = SL::BankAccount->save(%{ $account });
86 if ($form->{callback}) {
90 bank_account_edit('id' => $id);
93 $main::lxdebug->leave_sub();
97 sub bank_account_list {
98 $main::lxdebug->enter_sub();
100 my $form = $main::form;
101 my $locale = $main::locale;
103 $form->{title} = $locale->text('List of bank accounts');
105 $form->{sort} ||= 'account_number';
106 $form->{sortdir} = '1' if (!defined $form->{sortdir});
108 $form->{callback} = build_std_url('action=bank_account_list', 'sort', 'sortdir');
110 my $accounts = SL::BankAccount->list('sortorder' => $form->{sort},
111 'sortdir' => $form->{sortdir});
113 my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
115 my $href = build_std_url('action=bank_account_list');
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'), },
125 my @columns = qw(account_number bank bank_code bic iban);
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";
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),
138 $report->set_options_from_form();
139 $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
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});
146 my $edit_url = build_std_url('action=bank_account_edit', 'callback');
148 foreach my $account (@{ $accounts }) {
149 my $row = { map { $_ => { 'data' => $account->{$_} } } keys %{ $account } };
151 $row->{account_number}->{link} = $edit_url . '&id=' . E($account->{id});
153 $report->add_data($row);
156 $report->generate_with_headers();
158 $main::lxdebug->leave_sub();
162 my $form = $main::form;
164 foreach my $action (qw(bank_account_save bank_account_delete)) {
165 if ($form->{"action_${action}"}) {
171 $form->error($main::locale->text('No action defined.'));