3 use POSIX qw(strftime);
8 use SL::ReportGenerator;
10 require "bin/mozilla/common.pl";
11 require "bin/mozilla/reportgenerator.pl";
13 sub bank_account_add {
14 $main::lxdebug->enter_sub();
16 bank_account_display_form('account' => {});
18 $main::lxdebug->leave_sub();
21 sub bank_account_edit {
22 $main::lxdebug->enter_sub();
25 my $form = $main::form;
27 my $account = SL::BankAccount->retrieve('id' => $params{id} || $form->{id});
29 bank_account_display_form('account' => $account);
31 $main::lxdebug->leave_sub();
34 sub bank_account_display_form {
35 $main::lxdebug->enter_sub();
38 my $account = $params{account} || {};
39 my $form = $main::form;
40 my $locale = $main::locale;
42 my $charts = SL::Chart->list('link' => 'AP_paid');
43 my $label_sub = sub { join '--', map { $_[0]->{$_} } qw(accno description) };
45 $form->{title} = $account->{id} ? $locale->text('Edit bank account') : $locale->text('Add bank account');
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 });
54 $main::lxdebug->leave_sub();
57 sub bank_account_save {
58 $main::lxdebug->enter_sub();
60 my $form = $main::form;
61 my $locale = $main::locale;
63 my $account = $form->{account} && (ref $form->{account} eq 'HASH') ? $form->{account} : { };
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.'));
69 $main::lxdebug->leave_sub();
73 my $id = SL::BankAccount->save(%{ $account });
75 if ($form->{callback}) {
79 bank_account_edit('id' => $id);
82 $main::lxdebug->leave_sub();
86 sub bank_account_list {
87 $main::lxdebug->enter_sub();
89 my $form = $main::form;
90 my $locale = $main::locale;
92 $form->{title} = $locale->text('List of bank accounts');
94 $form->{sort} ||= 'account_number';
95 $form->{sortdir} = '1' if (!defined $form->{sortdir});
97 $form->{callback} = build_std_url('action=bank_account_list', 'sort', 'sortdir');
99 my $accounts = SL::BankAccount->list('sortorder' => $form->{sort},
100 'sortdir' => $form->{sortdir});
102 my $report = SL::ReportGenerator->new(\%main::myconfig, $form);
104 my $href = build_std_url('action=bank_account_list');
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'), },
114 my @columns = qw(account_number bank bank_code bic iban);
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";
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),
127 $report->set_options_from_form();
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});
134 my $edit_url = build_std_url('action=bank_account_edit', 'callback');
136 foreach my $account (@{ $accounts }) {
137 my $row = { map { $_ => { 'data' => $account->{$_} } } keys %{ $account } };
139 $row->{account_number}->{link} = $edit_url . '&id=' . E($account->{id});
141 $report->add_data($row);
144 $report->generate_with_headers();
146 $main::lxdebug->leave_sub();
150 my $form = $main::form;
152 foreach my $action (qw(bank_account_save bank_account_delete)) {
153 if ($form->{"action_${action}"}) {
159 $form->error($main::locale->text('No action defined.'));