1 package SL::Controller::BankTransaction;
3 # idee- möglichkeit bankdaten zu übernehmen in stammdaten
4 # erst Kontenabgleich, um alle gl-Einträge wegzuhaben
7 use parent qw(SL::Controller::Base);
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::ReportGenerator;
11 use SL::ReportGenerator;
13 use SL::DB::BankTransaction;
14 use SL::Helper::Flash;
15 use SL::Locale::String;
18 use SL::DB::PurchaseInvoice;
19 use SL::DB::RecordLink;
22 use SL::DB::AccTransaction;
25 use SL::DB::BankAccount;
27 use List::Util qw(max);
29 use Rose::Object::MakeMethods::Generic
31 'scalar --get_set_init' => [ qw(models) ],
34 __PACKAGE__->run_before('check_auth');
44 my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
46 $self->render('bank_transactions/search',
47 BANK_ACCOUNTS => $bank_accounts);
53 $self->make_filter_summary;
54 $self->prepare_report;
56 $self->report_generator_list_objects(report => $self->{report}, objects => $self->models->get);
62 if (!$::form->{filter}{bank_account}) {
63 flash('error', t8('No bank account chosen!'));
68 my $sort_by = $::form->{sort_by} || 'transdate';
69 $sort_by = 'transdate' if $sort_by eq 'proposal';
70 $sort_by .= $::form->{sort_dir} ? ' DESC' : ' ASC';
72 my $fromdate = $::locale->parse_date_to_object($::form->{filter}->{fromdate});
73 my $todate = $::locale->parse_date_to_object($::form->{filter}->{todate});
74 $todate->add( days => 1 ) if $todate;
77 push @where, (transdate => { ge => $fromdate }) if ($fromdate);
78 push @where, (transdate => { lt => $todate }) if ($todate);
79 my $bank_account = SL::DB::Manager::BankAccount->find_by( id => $::form->{filter}{bank_account} );
80 # bank_transactions no younger than starting date,
81 # but OPEN invoices to be matched may be from before
82 if ( $bank_account->reconciliation_starting_date ) {
83 push @where, (transdate => { gt => $bank_account->reconciliation_starting_date });
86 my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => [ amount => {ne => \'invoice_amount'},
87 local_bank_account_id => $::form->{filter}{bank_account},
89 with_objects => [ 'local_bank_account', 'currency' ],
90 sort_by => $sort_by, limit => 10000);
92 my $all_open_ar_invoices = SL::DB::Manager::Invoice->get_all(where => [amount => { gt => \'paid' }], with_objects => 'customer');
93 my $all_open_ap_invoices = SL::DB::Manager::PurchaseInvoice->get_all(where => [amount => { gt => \'paid' }], with_objects => 'vendor');
95 my @all_open_invoices;
96 # filter out invoices with less than 1 cent outstanding
97 push @all_open_invoices, grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ar_invoices };
98 push @all_open_invoices, grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ap_invoices };
100 # try to match each bank_transaction with each of the possible open invoices
103 foreach my $bt (@{ $bank_transactions }) {
104 next unless $bt->{remote_name}; # bank has no name, usually fees, use create invoice to assign
106 $bt->{remote_name} .= $bt->{remote_name_1} if $bt->{remote_name_1};
108 # try to match the current $bt to each of the open_invoices, saving the
109 # results of get_agreement_with_invoice in $open_invoice->{agreement} and
110 # $open_invoice->{rule_matches}.
112 # The values are overwritten each time a new bt is checked, so at the end
113 # of each bt the likely results are filtered and those values are stored in
114 # the arrays $bt->{proposals} and $bt->{rule_matches}, and the agreement
115 # score is stored in $bt->{agreement}
117 foreach my $open_invoice (@all_open_invoices){
118 ($open_invoice->{agreement}, $open_invoice->{rule_matches}) = $bt->get_agreement_with_invoice($open_invoice);
121 $bt->{proposals} = [];
124 my $min_agreement = 3; # suggestions must have at least this score
126 my $max_agreement = max map { $_->{agreement} } @all_open_invoices;
128 # add open_invoices with highest agreement into array $bt->{proposals}
129 if ( $max_agreement >= $min_agreement ) {
130 $bt->{proposals} = [ grep { $_->{agreement} == $max_agreement } @all_open_invoices ];
131 $bt->{agreement} = $max_agreement; #scalar @{ $bt->{proposals} } ? $agreement + 1 : '';
133 # store the rule_matches in a separate array, so they can be displayed in template
134 foreach ( @{ $bt->{proposals} } ) {
135 push(@{$bt->{rule_matches}}, $_->{rule_matches});
141 # separate filter for proposals (second tab, agreement >= 5 and exactly one match)
142 # to qualify as a proposal there has to be
143 # * agreement >= 5 TODO: make threshold configurable in configuration
144 # * there must be only one exact match
145 # * depending on whether sales or purchase the amount has to have the correct sign (so Gutschriften don't work?)
146 my $proposal_threshold = 5;
147 my @proposals = grep { $_->{agreement} >= $proposal_threshold
148 and 1 == scalar @{ $_->{proposals} }
149 and (@{ $_->{proposals} }[0]->is_sales ? abs(@{ $_->{proposals} }[0]->amount - $_->amount) < 0.01 : abs(@{ $_->{proposals} }[0]->amount + $_->amount) < 0.01) } @{ $bank_transactions };
151 # sort bank transaction proposals by quality (score) of proposal
152 $bank_transactions = [ sort { $a->{agreement} <=> $b->{agreement} } @{ $bank_transactions } ] if $::form->{sort_by} eq 'proposal' and $::form->{sort_dir} == 1;
153 $bank_transactions = [ sort { $b->{agreement} <=> $a->{agreement} } @{ $bank_transactions } ] if $::form->{sort_by} eq 'proposal' and $::form->{sort_dir} == 0;
156 $self->render('bank_transactions/list',
157 title => t8('Bank transactions MT940'),
158 BANK_TRANSACTIONS => $bank_transactions,
159 PROPOSALS => \@proposals,
160 bank_account => $bank_account );
163 sub action_assign_invoice {
166 $self->{transaction} = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
168 $self->render('bank_transactions/assign_invoice', { layout => 0 },
169 title => t8('Assign invoice'),);
172 sub action_create_invoice {
174 my %myconfig = %main::myconfig;
176 $self->{transaction} = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
177 my $vendor_of_transaction = SL::DB::Manager::Vendor->find_by(account_number => $self->{transaction}->{remote_account_number});
179 my $drafts = SL::DB::Manager::Draft->get_all(where => [ module => 'ap'] , with_objects => 'employee');
183 foreach my $draft ( @{ $drafts } ) {
184 my $draft_as_object = YAML::Load($draft->form);
185 my $vendor = SL::DB::Manager::Vendor->find_by(id => $draft_as_object->{vendor_id});
186 $draft->{vendor} = $vendor->name;
187 $draft->{vendor_id} = $vendor->id;
188 push @filtered_drafts, $draft;
192 @filtered_drafts = grep { $_->{vendor_id} == $vendor_of_transaction->id } @filtered_drafts if $vendor_of_transaction;
194 my $all_vendors = SL::DB::Manager::Vendor->get_all();
196 $self->render('bank_transactions/create_invoice', { layout => 0 },
197 title => t8('Create invoice'),
198 DRAFTS => \@filtered_drafts,
199 vendor_id => $vendor_of_transaction ? $vendor_of_transaction->id : undef,
200 vendor_name => $vendor_of_transaction ? $vendor_of_transaction->name : undef,
201 ALL_VENDORS => $all_vendors,
202 limit => $myconfig{vclimit},
203 callback => $self->url_for(action => 'list',
204 'filter.bank_account' => $::form->{filter}->{bank_account},
205 'filter.todate' => $::form->{filter}->{todate},
206 'filter.fromdate' => $::form->{filter}->{fromdate}),
210 sub action_ajax_payment_suggestion {
213 # based on a BankTransaction ID and a Invoice or PurchaseInvoice ID passed via $::form,
214 # create an HTML blob to be used by the js function add_invoices in templates/webpages/bank_transactions/list.html
215 # and return encoded as JSON
217 my $bt = SL::DB::Manager::BankTransaction->find_by( id => $::form->{bt_id} );
218 my $invoice = SL::DB::Manager::Invoice->find_by( id => $::form->{prop_id} );
219 $invoice = SL::DB::Manager::PurchaseInvoice->find_by( id => $::form->{prop_id} ) unless $invoice;
221 die unless $bt and $invoice;
223 my @select_options = $invoice->get_payment_select_options_for_bank_transaction($::form->{bt_id});
226 $html .= SL::Presenter->input_tag('invoice_ids.' . $::form->{bt_id} . '[]', $::form->{prop_id} , type => 'hidden');
227 $html .= SL::Presenter->escape( $invoice->invnumber );
228 $html .= SL::Presenter->select_tag('invoice_skontos.' . $::form->{bt_id} . '[]', \@select_options,
229 value_key => 'payment_type',
230 title_key => 'display' ) if @select_options;
231 $html .= '<a href=# onclick="delete_invoice(' . $::form->{bt_id} . ',' . $::form->{prop_id} . ');">x</a>';
232 $html = SL::Presenter->html_tag('div', $html, id => $::form->{bt_id} . '.' . $::form->{prop_id});
234 $self->render(\ SL::JSON::to_json( { 'html' => $html } ), { layout => 0, type => 'json', process => 0 });
237 sub action_filter_drafts {
240 $self->{transaction} = SL::DB::Manager::BankTransaction->find_by(id => $::form->{bt_id});
241 my $vendor_of_transaction = SL::DB::Manager::Vendor->find_by(account_number => $self->{transaction}->{remote_account_number});
243 my $drafts = SL::DB::Manager::Draft->get_all(with_objects => 'employee');
247 foreach my $draft ( @{ $drafts } ) {
248 my $draft_as_object = YAML::Load($draft->form);
249 my $vendor = SL::DB::Manager::Vendor->find_by(id => $draft_as_object->{vendor_id});
250 $draft->{vendor} = $vendor->name;
251 $draft->{vendor_id} = $vendor->id;
252 push @filtered_drafts, $draft;
255 my $vendor_name = $::form->{vendor};
256 my $vendor_id = $::form->{vendor_id};
259 @filtered_drafts = grep { $_->{vendor_id} == $vendor_id } @filtered_drafts if $vendor_id;
260 @filtered_drafts = grep { $_->{vendor} =~ /$vendor_name/i } @filtered_drafts if $vendor_name;
262 my $output = $self->render(
263 'bank_transactions/filter_drafts',
265 DRAFTS => \@filtered_drafts,
268 my %result = ( count => 0, html => $output );
270 $self->render(\to_json(\%result), { type => 'json', process => 0 });
273 sub action_ajax_add_list {
276 my @where_sale = (amount => { ne => \'paid' });
277 my @where_purchase = (amount => { ne => \'paid' });
279 if ($::form->{invnumber}) {
280 push @where_sale, (invnumber => { ilike => '%' . $::form->{invnumber} . '%'});
281 push @where_purchase, (invnumber => { ilike => '%' . $::form->{invnumber} . '%'});
284 if ($::form->{amount}) {
285 push @where_sale, (amount => $::form->parse_amount(\%::myconfig, $::form->{amount}));
286 push @where_purchase, (amount => $::form->parse_amount(\%::myconfig, $::form->{amount}));
289 if ($::form->{vcnumber}) {
290 push @where_sale, ('customer.customernumber' => { ilike => '%' . $::form->{vcnumber} . '%'});
291 push @where_purchase, ('vendor.vendornumber' => { ilike => '%' . $::form->{vcnumber} . '%'});
294 if ($::form->{vcname}) {
295 push @where_sale, ('customer.name' => { ilike => '%' . $::form->{vcname} . '%'});
296 push @where_purchase, ('vendor.name' => { ilike => '%' . $::form->{vcname} . '%'});
299 if ($::form->{transdatefrom}) {
300 my $fromdate = $::locale->parse_date_to_object($::form->{transdatefrom});
301 if ( ref($fromdate) eq 'DateTime' ) {
302 push @where_sale, ('transdate' => { ge => $fromdate});
303 push @where_purchase, ('transdate' => { ge => $fromdate});
307 if ($::form->{transdateto}) {
308 my $todate = $::locale->parse_date_to_object($::form->{transdateto});
309 if ( ref($todate) eq 'DateTime' ) {
310 $todate->add(days => 1);
311 push @where_sale, ('transdate' => { lt => $todate});
312 push @where_purchase, ('transdate' => { lt => $todate});
316 my $all_open_ar_invoices = SL::DB::Manager::Invoice->get_all(where => \@where_sale, with_objects => 'customer');
317 my $all_open_ap_invoices = SL::DB::Manager::PurchaseInvoice->get_all(where => \@where_purchase, with_objects => 'vendor');
319 my @all_open_invoices;
320 # filter out subcent differences from ap invoices
321 push @all_open_invoices, grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ap_invoices };
323 @all_open_invoices = sort { $a->id <=> $b->id } @all_open_invoices;
325 my $output = $self->render(
326 'bank_transactions/add_list',
328 INVOICES => \@all_open_invoices,
331 my %result = ( count => 0, html => $output );
333 $self->render(\to_json(\%result), { type => 'json', process => 0 });
336 sub action_ajax_accept_invoices {
339 my @selected_invoices;
340 foreach my $invoice_id (@{ $::form->{invoice_id} || [] }) {
341 my $invoice_object = SL::DB::Manager::Invoice->find_by(id => $invoice_id);
342 $invoice_object ||= SL::DB::Manager::PurchaseInvoice->find_by(id => $invoice_id);
344 push @selected_invoices, $invoice_object;
347 $self->render('bank_transactions/invoices', { layout => 0 },
348 INVOICES => \@selected_invoices,
349 bt_id => $::form->{bt_id} );
352 sub action_save_invoices {
355 my $invoice_hash = delete $::form->{invoice_ids}; # each key (the bt line with a bt_id) contains an array of invoice_ids
356 my $skonto_hash = delete $::form->{invoice_skontos} || {}; # array containing the payment type, could be empty
358 while ( my ($bt_id, $invoice_ids) = each(%$invoice_hash) ) {
359 my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
360 my $sign = $bank_transaction->amount < 0 ? -1 : 1;
361 my $amount_of_transaction = $sign * $bank_transaction->amount;
364 foreach my $invoice_id (@{ $invoice_ids }) {
365 push @invoices, (SL::DB::Manager::Invoice->find_by(id => $invoice_id) || SL::DB::Manager::PurchaseInvoice->find_by(id => $invoice_id));
367 @invoices = sort { return 1 if ($a->is_sales and $a->amount > 0);
368 return 1 if (!$a->is_sales and $a->amount < 0);
369 return -1; } @invoices if $bank_transaction->amount > 0;
370 @invoices = sort { return -1 if ($a->is_sales and $a->amount > 0);
371 return -1 if (!$a->is_sales and $a->amount < 0);
372 return 1; } @invoices if $bank_transaction->amount < 0;
374 foreach my $invoice (@invoices) {
376 if ( @$skonto_hash{"$bt_id"} ) {
377 $payment_type = shift( @$skonto_hash{"$bt_id"} );
379 $payment_type = 'without_skonto';
381 if ($amount_of_transaction == 0) {
382 flash('warning', $::locale->text('There are invoices which could not be paid by bank transaction #1 (Account number: #2, bank code: #3)!',
383 $bank_transaction->purpose,
384 $bank_transaction->remote_account_number,
385 $bank_transaction->remote_bank_code));
388 #pay invoice or go to the next bank transaction if the amount is not sufficiently high
389 if ($invoice->amount <= $amount_of_transaction) {
390 $invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id,
391 trans_id => $invoice->id,
392 amount => $invoice->amount,
393 payment_type => $payment_type,
394 transdate => $bank_transaction->transdate->to_kivitendo);
395 if ($invoice->is_sales) {
396 $amount_of_transaction -= $sign * $invoice->amount;
397 $bank_transaction->invoice_amount($bank_transaction->invoice_amount + $invoice->amount);
399 $amount_of_transaction += $sign * $invoice->amount if (!$invoice->is_sales);
400 $bank_transaction->invoice_amount($bank_transaction->invoice_amount - $invoice->amount);
403 $invoice->pay_invoice(chart_id => $bank_transaction->local_bank_account->chart_id,
404 trans_id => $invoice->id,
405 amount => $amount_of_transaction,
406 payment_type => $payment_type,
407 transdate => $bank_transaction->transdate->to_kivitendo);
408 $bank_transaction->invoice_amount($bank_transaction->amount) if $invoice->is_sales;
409 $bank_transaction->invoice_amount($bank_transaction->amount) if !$invoice->is_sales;
410 $amount_of_transaction = 0;
413 #Record a link from the bank transaction to the invoice
415 from_table => 'bank_transactions',
417 to_table => $invoice->is_sales ? 'ar' : 'ap',
418 to_id => $invoice->id,
421 my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
423 SL::DB::RecordLink->new(@props)->save if !$existing;
425 $bank_transaction->save;
428 $self->action_list();
431 sub action_save_proposals {
434 foreach my $bt_id (@{ $::form->{proposal_ids} }) {
436 my $bt = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
437 $bt->invoice_amount($bt->amount);
441 my $arap = SL::DB::Manager::Invoice->find_by(id => $::form->{"proposed_invoice_$bt_id"});
442 $arap = SL::DB::Manager::PurchaseInvoice->find_by(id => $::form->{"proposed_invoice_$bt_id"}) if not defined $arap;
443 $arap->pay_invoice(chart_id => $bt->local_bank_account->chart_id,
444 trans_id => $arap->id,
445 amount => $arap->amount,
446 transdate => $bt->transdate->to_kivitendo);
451 from_table => 'bank_transactions',
453 to_table => $arap->is_sales ? 'ar' : 'ap',
457 my $existing = SL::DB::Manager::RecordLink->get_all(where => \@props, limit => 1)->[0];
459 SL::DB::RecordLink->new(@props)->save if !$existing;
462 flash('ok', t8('#1 proposal(s) saved.', scalar @{ $::form->{proposal_ids} }));
464 $self->action_list();
472 $::auth->assert('bank_transaction');
479 sub make_filter_summary {
482 my $filter = $::form->{filter} || {};
486 [ $filter->{"transdate:date::ge"}, $::locale->text('Transdate') . " " . $::locale->text('From Date') ],
487 [ $filter->{"transdate:date::le"}, $::locale->text('Transdate') . " " . $::locale->text('To Date') ],
488 [ $filter->{"valutadate:date::ge"}, $::locale->text('Valutadate') . " " . $::locale->text('From Date') ],
489 [ $filter->{"valutadate:date::le"}, $::locale->text('Valutadate') . " " . $::locale->text('To Date') ],
490 [ $filter->{"amount:number"}, $::locale->text('Amount') ],
491 [ $filter->{"bank_account_id:integer"}, $::locale->text('Local bank account') ],
495 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
498 $self->{filter_summary} = join ', ', @filter_strings;
504 my $callback = $self->models->get_callback;
506 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
507 $self->{report} = $report;
509 my @columns = qw(local_bank_name transdate valudate remote_name remote_account_number remote_bank_code amount invoice_amount invoices currency purpose local_account_number local_bank_code id);
510 my @sortable = qw(local_bank_name transdate valudate remote_name remote_account_number remote_bank_code amount purpose local_account_number local_bank_code);
513 transdate => { sub => sub { $_[0]->transdate_as_date } },
514 valutadate => { sub => sub { $_[0]->valutadate_as_date } },
516 remote_account_number => { },
517 remote_bank_code => { },
518 amount => { sub => sub { $_[0]->amount_as_number },
520 invoice_amount => { sub => sub { $_[0]->invoice_amount_as_number },
522 invoices => { sub => sub { $_[0]->linked_invoices } },
523 currency => { sub => sub { $_[0]->currency->name } },
525 local_account_number => { sub => sub { $_[0]->local_bank_account->account_number } },
526 local_bank_code => { sub => sub { $_[0]->local_bank_account->bank_code } },
527 local_bank_name => { sub => sub { $_[0]->local_bank_account->name } },
531 map { $column_defs{$_}->{text} ||= $::locale->text( $self->models->get_sort_spec->{$_}->{title} ) } keys %column_defs;
533 $report->set_options(
534 std_column_visibility => 1,
535 controller_class => 'BankTransaction',
536 output_format => 'HTML',
537 top_info_text => $::locale->text('Bank transactions'),
538 title => $::locale->text('Bank transactions'),
539 allow_pdf_export => 1,
540 allow_csv_export => 1,
542 $report->set_columns(%column_defs);
543 $report->set_column_order(@columns);
544 $report->set_export_options(qw(list_all filter));
545 $report->set_options_from_form;
546 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
547 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
549 my $bank_accounts = SL::DB::Manager::BankAccount->get_all_sorted();
551 $report->set_options(
552 raw_top_info_text => $self->render('bank_transactions/report_top', { output => 0 }, BANK_ACCOUNTS => $bank_accounts),
553 raw_bottom_info_text => $self->render('bank_transactions/report_bottom', { output => 0 }),
560 SL::Controller::Helper::GetModels->new(
565 dir => 0, # 1 = ASC, 0 = DESC : default sort is newest at top
567 transdate => t8('Transdate'),
568 remote_name => t8('Remote name'),
569 amount => t8('Amount'),
570 invoice_amount => t8('Assigned'),
571 invoices => t8('Linked invoices'),
572 valutadate => t8('Valutadate'),
573 remote_account_number => t8('Remote account number'),
574 remote_bank_code => t8('Remote bank code'),
575 currency => t8('Currency'),
576 purpose => t8('Purpose'),
577 local_account_number => t8('Local account number'),
578 local_bank_code => t8('Local bank code'),
579 local_bank_name => t8('Bank account'),
581 with_objects => [ 'local_bank_account', 'currency' ],