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_display_form {
36 $main::lxdebug->enter_sub();
39 my $account = $params{account} || {};
40 my $form = $main::form;
41 my $locale = $main::locale;
43 my $charts = SL::Chart->list('link' => 'AP_paid');
44 my $label_sub = sub { join '--', map { $_[0]->{$_} } qw(accno description) };
46 $form->{title} = $account->{id} ? $locale->text('Edit bank account') : $locale->text('Add bank account');
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 });
55 $main::lxdebug->leave_sub();
58 sub bank_account_save {
59 $main::lxdebug->enter_sub();
61 my $form = $main::form;
62 my $locale = $main::locale;
64 my $account = $form->{account} && (ref $form->{account} eq 'HASH') ? $form->{account} : { };
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.'));
70 $main::lxdebug->leave_sub();
74 my $id = SL::BankAccount->save(%{ $account });
76 if ($form->{callback}) {
80 bank_account_edit('id' => $id);
83 $main::lxdebug->leave_sub();
87 sub bank_account_list {
88 $main::lxdebug->enter_sub();
90 my $form = $main::form;
91 my $locale = $main::locale;
93 $form->{title} = $locale->text('List of bank accounts');
95 $form->{sort} ||= 'account_number';
96 $form->{sortdir} = '1' if (!defined $form->{sortdir});
98 $form->{callback} = build_std_url('action=bank_account_list', 'sort', 'sortdir');
100 my $accounts = SL::BankAccount->list('sortorder' => $form->{sort},
101 'sortdir' => $form->{sortdir});
103 my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
105 my $href = build_std_url('action=bank_account_list');
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'), },
115 my @columns = qw(account_number bank bank_code bic iban);
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";
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),
128 $report->set_options_from_form();
129 $locale->set_numberformat_wo_thousands_separator(\%::myconfig) if lc($report->{options}->{output_format}) eq 'csv';
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});
136 my $edit_url = build_std_url('action=bank_account_edit', 'callback');
138 foreach my $account (@{ $accounts }) {
139 my $row = { map { $_ => { 'data' => $account->{$_} } } keys %{ $account } };
141 $row->{account_number}->{link} = $edit_url . '&id=' . E($account->{id});
143 $report->add_data($row);
146 $report->generate_with_headers();
148 $main::lxdebug->leave_sub();
152 my $form = $main::form;
154 foreach my $action (qw(bank_account_save bank_account_delete)) {
155 if ($form->{"action_${action}"}) {
161 $form->error($main::locale->text('No action defined.'));