+ # filter out invoices with less than 1 cent outstanding
+ push @all_open_invoices, map { $_->{is_ar}=1 ; $_ } grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ar_invoices };
+ push @all_open_invoices, map { $_->{is_ar}=0 ; $_ } grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ap_invoices };
+ $main::lxdebug->message(LXDebug->DEBUG2(),"bank_account=".$::form->{filter}{bank_account}." invoices: ".scalar(@{ $all_open_ar_invoices }).
+ " + ".scalar(@{ $all_open_ap_invoices })." non fully paid=".scalar(@all_open_invoices)." transactions=".scalar(@{ $bank_transactions }));
+
+ my @all_sepa_invoices;
+ my @all_non_sepa_invoices;
+ my %sepa_exports;
+ # first collect sepa export items to open invoices
+ foreach my $open_invoice (@all_open_invoices){
+ # my @items = grep { $_->ap_id == $open_invoice->id || $_->ar_id == $open_invoice->id } @{$all_open_sepa_export_items};
+ $open_invoice->{realamount} = $::form->format_amount(\%::myconfig,$open_invoice->amount,2);
+ $open_invoice->{skonto_type} = 'without_skonto';
+ foreach ( @{$all_open_sepa_export_items}) {
+ if ( $_->ap_id == $open_invoice->id || $_->ar_id == $open_invoice->id ) {
+ my $factor = ($_->ar_id == $open_invoice->id?1:-1);
+ $main::lxdebug->message(LXDebug->DEBUG2(),"exitem=".$_->id." for invoice ".$open_invoice->id." factor=".$factor);
+ $open_invoice->{realamount} = $::form->format_amount(\%::myconfig,$open_invoice->amount*$factor,2);
+ $open_invoice->{sepa_export_item} = $_ ;
+ $open_invoice->{skonto_type} = $_->payment_type;
+ $sepa_exports{$_->sepa_export_id} ||= { count => 0, is_ar => 0, amount => 0, proposed => 0, invoices => [], item => $_ };
+ $sepa_exports{$_->sepa_export_id}->{count}++ ;
+ $sepa_exports{$_->sepa_export_id}->{is_ar}++ if $_->ar_id == $open_invoice->id;
+ $sepa_exports{$_->sepa_export_id}->{amount} += $_->amount * $factor;
+ push @{ $sepa_exports{$_->sepa_export_id}->{invoices} }, $open_invoice;
+ #$main::lxdebug->message(LXDebug->DEBUG2(),"amount for export id ".$_->sepa_export_id." = ".
+ # $sepa_exports{$_->sepa_export_id}->{amount}." count = ".
+ # $sepa_exports{$_->sepa_export_id}->{count}." is_ar = ".
+ # $sepa_exports{$_->sepa_export_id}->{is_ar} );
+ push @all_sepa_invoices , $open_invoice;
+ }
+ }
+ push @all_non_sepa_invoices , $open_invoice if ! $open_invoice->{sepa_export_item};
+ }
+
+ # try to match each bank_transaction with each of the possible open invoices
+ # by awarding points
+ my @proposals;