- foreach my $open_invoice (@all_open_invoices){
- $open_invoice->{agreement} = 0;
-
- #compare banking arrangements
- my ($bank_code, $account_number);
- $bank_code = $open_invoice->customer->bank_code if $open_invoice->is_sales;
- $account_number = $open_invoice->customer->account_number if $open_invoice->is_sales;
- $bank_code = $open_invoice->vendor->bank_code if ! $open_invoice->is_sales;
- $account_number = $open_invoice->vendor->account_number if ! $open_invoice->is_sales;
- ($bank_code eq $bt->remote_bank_code
- && $account_number eq $bt->remote_account_number) ? ($open_invoice->{agreement} += 2) : ();
-
- my $datediff = $bt->transdate->{utc_rd_days} - $open_invoice->transdate->{utc_rd_days};
- $open_invoice->{datediff} = $datediff;
-
- #compare amount
-# (abs($open_invoice->amount) == abs($bt->amount)) ? ($open_invoice->{agreement} += 2) : ();
-# do we need double abs here?
- (abs(abs($open_invoice->amount) - abs($bt->amount)) < 0.01) ? ($open_invoice->{agreement} += 4) : ();
-
- #search invoice number in purpose
- my $invnumber = $open_invoice->invnumber;
-# possible improvement: match has to have more than 1 character?
- $bt->purpose =~ /\b$invnumber\b/i ? ($open_invoice->{agreement} += 2) : ();
-
- #check sign
- if ( $open_invoice->is_sales && $bt->amount < 0 ) {
- $open_invoice->{agreement} -= 1;
- };
- if ( ! $open_invoice->is_sales && $bt->amount > 0 ) {
- $open_invoice->{agreement} -= 1;
- };
-
- #search customer/vendor number in purpose
- my $cvnumber;
- $cvnumber = $open_invoice->customer->customernumber if $open_invoice->is_sales;
- $cvnumber = $open_invoice->vendor->vendornumber if ! $open_invoice->is_sales;
- $bt->purpose =~ /\b$cvnumber\b/i ? ($open_invoice->{agreement}++) : ();
-
- #compare customer/vendor name and account holder
- my $cvname;
- $cvname = $open_invoice->customer->name if $open_invoice->is_sales;
- $cvname = $open_invoice->vendor->name if ! $open_invoice->is_sales;
- $bt->remote_name =~ /\b$cvname\b/i ? ($open_invoice->{agreement}++) : ();
-
- #Compare transdate of bank_transaction with transdate of invoice
- #Check if words in remote_name appear in cvname
- $open_invoice->{agreement} += &check_string($bt->remote_name,$cvname);
-
- $open_invoice->{agreement} -= 1 if $datediff < -5; # dies hebelt eventuell Vorkasse aus
- $open_invoice->{agreement} += 1 if $datediff < 30; # dies hebelt eventuell Vorkasse aus
-
- # only if we already have a good agreement, let date further change value of agreement.
- # this is so that if there are several open invoices which are all equal (rent jan, rent feb...) the one with the best date match is chose over the others
- # another way around this is to just pre-filter by periods instead of matching everything
- if ( $open_invoice->{agreement} > 5 ) {
- if ( $datediff == 0 ) {
- $open_invoice->{agreement} += 3;
- } elsif ( $datediff > 0 and $datediff <= 14 ) {
- $open_invoice->{agreement} += 2;
- } elsif ( $datediff >14 and $datediff < 35) {
- $open_invoice->{agreement} += 1;
- } elsif ( $datediff >34 and $datediff < 120) {
- $open_invoice->{agreement} += 1;
- } elsif ( $datediff < 0 ) {
- $open_invoice->{agreement} -= 1;
- } else {
- # e.g. datediff > 120
- };
- };