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