DB/BankTransaction: Kosmetik Fall SEPA-Check
[kivitendo-erp.git] / SL / DB / BankTransaction.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::BankTransaction;
5
6 use strict;
7
8 use SL::DB::MetaSetup::BankTransaction;
9 use SL::DB::Manager::BankTransaction;
10 use SL::DB::Helper::LinkedRecords;
11 use Carp;
12
13 require SL::DB::Invoice;
14 require SL::DB::PurchaseInvoice;
15
16 __PACKAGE__->meta->initialize;
17
18
19 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
20 #__PACKAGE__->meta->make_manager_class;
21
22 sub compare_to {
23   my ($self, $other) = @_;
24
25   return  1 if  $self->transdate && !$other->transdate;
26   return -1 if !$self->transdate &&  $other->transdate;
27
28   my $result = 0;
29   $result    = $self->transdate <=> $other->transdate if $self->transdate;
30   return $result || ($self->id <=> $other->id);
31 }
32
33 sub linked_invoices {
34   my ($self) = @_;
35
36   #my $record_links = $self->linked_records(direction => 'both');
37
38   my @linked_invoices;
39
40   my $record_links = SL::DB::Manager::RecordLink->get_all(where => [ from_table => 'bank_transactions', from_id => $self->id ]);
41
42   foreach my $record_link (@{ $record_links }) {
43     push @linked_invoices, SL::DB::Manager::Invoice->find_by(id => $record_link->to_id)->invnumber         if $record_link->to_table eq 'ar';
44     push @linked_invoices, SL::DB::Manager::PurchaseInvoice->find_by(id => $record_link->to_id)->invnumber if $record_link->to_table eq 'ap';
45   }
46
47   return [ @linked_invoices ];
48 }
49
50 sub get_agreement_with_invoice {
51   my ($self, $invoice) = @_;
52
53   carp "get_agreement_with_invoice needs an invoice object as its first argument"
54     unless ref($invoice) eq 'SL::DB::Invoice' or ref($invoice) eq 'SL::DB::PurchaseInvoice';
55
56   my %points = (
57     cust_vend_name_in_purpose   => 1,
58     cust_vend_number_in_purpose => 1,
59     datebonus0                  => 3,
60     datebonus14                 => 2,
61     datebonus35                 => 1,
62     datebonus120                => 0,
63     datebonus_negative          => -1,
64     depositor_matches           => 2,
65     exact_amount                => 4,
66     exact_open_amount           => 4,
67     invnumber_in_purpose        => 2,
68     own_invnumber_in_purpose    => 5,
69     # overpayment                 => -1, # either other invoice is more likely, or several invoices paid at once
70     payment_before_invoice      => -2,
71     payment_within_30_days      => 1,
72     remote_account_number       => 3,
73     skonto_exact_amount         => 5,
74     wrong_sign                  => -1,
75     sepa_export_item            => 5,
76   );
77
78   my ($agreement,$rule_matches);
79
80   # compare banking arrangements
81   my ($iban, $bank_code, $account_number);
82   $bank_code      = $invoice->customer->bank_code      if $invoice->is_sales;
83   $account_number = $invoice->customer->account_number if $invoice->is_sales;
84   $iban           = $invoice->customer->iban           if $invoice->is_sales;
85   $bank_code      = $invoice->vendor->bank_code        if ! $invoice->is_sales;
86   $iban           = $invoice->vendor->iban             if ! $invoice->is_sales;
87   $account_number = $invoice->vendor->account_number   if ! $invoice->is_sales;
88   if ( $bank_code eq $self->remote_bank_code && $account_number eq $self->remote_account_number ) {
89     $agreement += $points{remote_account_number};
90     $rule_matches .= 'remote_account_number(' . $points{'remote_account_number'} . ') ';
91   };
92   if ( $iban eq $self->remote_account_number ) {
93     $agreement += $points{remote_account_number};
94     $rule_matches .= 'remote_account_number(' . $points{'remote_account_number'} . ') ';
95   };
96
97   my $datediff = $self->transdate->{utc_rd_days} - $invoice->transdate->{utc_rd_days};
98   $invoice->{datediff} = $datediff;
99
100   # compare amount
101   if (abs(abs($invoice->amount) - abs($self->amount)) < 0.01) {
102     $agreement += $points{exact_amount};
103     $rule_matches .= 'exact_amount(' . $points{'exact_amount'} . ') ';
104   };
105
106   # compare open amount, preventing double points when open amount = invoice amount
107   if ( $invoice->amount != $invoice->open_amount && abs(abs($invoice->open_amount) - abs($self->amount)) < 0.01) {
108     $agreement += $points{exact_open_amount};
109     $rule_matches .= 'exact_open_amount(' . $points{'exact_open_amount'} . ') ';
110   };
111
112   if ( $invoice->skonto_date && abs(abs($invoice->amount_less_skonto) - abs($self->amount)) < 0.01) {
113     $agreement += $points{skonto_exact_amount};
114     $rule_matches .= 'skonto_exact_amount(' . $points{'skonto_exact_amount'} . ') ';
115     $invoice->{skonto_type} = 'with_skonto_pt';
116   };
117
118   #search invoice number in purpose
119   my $invnumber = $invoice->invnumber;
120   # invnumber has to have at least 3 characters
121   my $squashed_purpose = $self->purpose;
122   $squashed_purpose =~ s/ //g;
123   if (length($invnumber) > 4 && $squashed_purpose =~ /$invnumber/ && $invoice->is_sales){
124     $agreement += $points{own_invnumber_in_purpose};
125     $rule_matches .= 'own_invnumber_in_purpose(' . $points{'own_invnumber_in_purpose'} . ') ';
126   } elsif (length($invnumber) > 3 && $squashed_purpose =~ /$invnumber/ ) {
127     $agreement += $points{invnumber_in_purpose};
128     $rule_matches .= 'invnumber_in_purpose(' . $points{'invnumber_in_purpose'} . ') ';
129   }
130
131   #check sign
132   if ( $invoice->is_sales && $self->amount < 0 ) {
133     $agreement += $points{wrong_sign};
134     $rule_matches .= 'wrong_sign(' . $points{'wrong_sign'} . ') ';
135   };
136   if ( ! $invoice->is_sales && $self->amount > 0 ) {
137     $agreement += $points{wrong_sign};
138     $rule_matches .= 'wrong_sign(' . $points{'wrong_sign'} . ') ';
139   };
140
141   # search customer/vendor number in purpose
142   my $cvnumber;
143   $cvnumber = $invoice->customer->customernumber if $invoice->is_sales;
144   $cvnumber = $invoice->vendor->vendornumber     if ! $invoice->is_sales;
145   if ( $cvnumber && $self->purpose =~ /\b$cvnumber\b/i ) {
146     $agreement += $points{cust_vend_number_in_purpose};
147     $rule_matches .= 'cust_vend_number_in_purpose(' . $points{'cust_vend_number_in_purpose'} . ') ';
148   }
149
150   # search for customer/vendor name in purpose (may contain GMBH, CO KG, ...)
151   my $cvname;
152   $cvname = $invoice->customer->name if $invoice->is_sales;
153   $cvname = $invoice->vendor->name   if ! $invoice->is_sales;
154   if ( $cvname && $self->purpose =~ /\b\Q$cvname\E\b/i ) {
155     $agreement += $points{cust_vend_name_in_purpose};
156     $rule_matches .= 'cust_vend_name_in_purpose(' . $points{'cust_vend_name_in_purpose'} . ') ';
157   };
158
159   # compare depositorname, don't try to match empty depositors
160   my $depositorname;
161   $depositorname = $invoice->customer->depositor if $invoice->is_sales;
162   $depositorname = $invoice->vendor->depositor   if ! $invoice->is_sales;
163   if ( $depositorname && $self->remote_name =~ /$depositorname/ ) {
164     $agreement += $points{depositor_matches};
165     $rule_matches .= 'depositor_matches(' . $points{'depositor_matches'} . ') ';
166   };
167
168   #Check if words in remote_name appear in cvname
169   my $check_string_points = _check_string($self->remote_name,$cvname);
170   if ( $check_string_points ) {
171     $agreement += $check_string_points;
172     $rule_matches .= 'remote_name(' . $check_string_points . ') ';
173   };
174
175   # transdate prefilter: compare transdate of bank_transaction with transdate of invoice
176   if ( $datediff < -5 ) { # this might conflict with advance payments
177     $agreement += $points{payment_before_invoice};
178     $rule_matches .= 'payment_before_invoice(' . $points{'payment_before_invoice'} . ') ';
179   };
180   if ( $datediff < 30 ) {
181     $agreement += $points{payment_within_30_days};
182     $rule_matches .= 'payment_within_30_days(' . $points{'payment_within_30_days'} . ') ';
183   };
184
185   # only if we already have a good agreement, let date further change value of agreement.
186   # this is so that if there are several plausible open invoices which are all equal
187   # (rent jan, rent feb...) the one with the best date match is chosen over
188   # the others
189
190   # another way around this is to just pre-filter by periods instead of matching everything
191   if ( $agreement > 5 ) {
192     if ( $datediff == 0 ) {
193       $agreement += $points{datebonus0};
194       $rule_matches .= 'datebonus0(' . $points{'datebonus0'} . ') ';
195     } elsif  ( $datediff > 0 and $datediff <= 14 ) {
196       $agreement += $points{datebonus14};
197       $rule_matches .= 'datebonus14(' . $points{'datebonus14'} . ') ';
198     } elsif  ( $datediff >14 and $datediff < 35) {
199       $agreement += $points{datebonus35};
200       $rule_matches .= 'datebonus35(' . $points{'datebonus35'} . ') ';
201     } elsif  ( $datediff >34 and $datediff < 120) {
202       $agreement += $points{datebonus120};
203       $rule_matches .= 'datebonus120(' . $points{'datebonus120'} . ') ';
204     } elsif  ( $datediff < 0 ) {
205       $agreement += $points{datebonus_negative};
206       $rule_matches .= 'datebonus_negative(' . $points{'datebonus_negative'} . ') ';
207     } else {
208   # e.g. datediff > 120
209     };
210   };
211
212 #  # if there is exactly one non-executed sepa_export_item for the invoice
213   if ( my $seis = $invoice->find_sepa_export_items({ executed => 0 }) ) {
214     if (scalar @$seis == 1) {
215       my $sei = $seis->[0];
216
217       # test for amount and id matching only, sepa transfer date and bank
218       # transaction date needn't match
219       my $arap = $invoice->is_sales ? 'ar' : 'ap';
220
221       if (abs($self->amount) == ($sei->amount) && $invoice->id == $sei->arap_id) {
222         $agreement += $points{sepa_export_item};
223         $rule_matches .= 'sepa_export_item(' . $points{'sepa_export_item'} . ') ';
224       }
225     } else {
226       # zero or more than one sepa_export_item, do nothing for this invoice
227       # zero: do nothing, no sepa_export_item exists, no match
228       # more than one: does this ever apply? Currently you can't create sepa
229       # exports for invoices that already have a non-executed sepa_export
230     }
231   }
232
233   return ($agreement,$rule_matches);
234 };
235
236 sub _check_string {
237     my $bankstring = shift;
238     my $namestring = shift;
239     return 0 unless $bankstring and $namestring;
240
241     my @bankwords = grep(/^\w+$/, split(/\b/,$bankstring));
242
243     my $match = 0;
244     foreach my $bankword ( @bankwords ) {
245         # only try to match strings with more than 2 characters
246         next unless length($bankword)>2;
247         if ( $namestring =~ /\b$bankword\b/i ) {
248             $match++;
249         };
250     };
251     return $match;
252 };
253
254 1;
255
256 __END__
257
258 =pod
259
260 =head1 NAME
261
262 SL::DB::BankTransaction
263
264 =head1 FUNCTIONS
265
266 =over 4
267
268 =item C<get_agreement_with_invoice $invoice>
269
270 Using a point system this function checks whether the bank transaction matches
271 an invoices, using a variety of tests, such as
272
273 =over 2
274
275 =item * amount
276
277 =item * amount_less_skonto
278
279 =item * payment date
280
281 =item * invoice number in purpose
282
283 =item * customer or vendor name in purpose
284
285 =item * account number matches account number of customer or vendor
286
287 =back
288
289 The total number of points, and the rules that matched, are returned.
290
291 Example:
292   my $bt      = SL::DB::Manager::BankTransaction->find_by(id => 522);
293   my $invoice = SL::DB::Manager::Invoice->find_by(invnumber => '198');
294   my ($agreement,rule_matches) = $bt->get_agreement_with_invoice($invoice);
295
296 =back
297
298 =head1 AUTHOR
299
300 G. Richardson E<lt>grichardson@kivitendo-premium.de<gt>
301
302 =cut