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.
4 package SL::DB::BankTransaction;
8 use SL::DB::MetaSetup::BankTransaction;
9 use SL::DB::Manager::BankTransaction;
10 use SL::DB::Helper::LinkedRecords;
12 require SL::DB::Invoice;
13 require SL::DB::PurchaseInvoice;
15 __PACKAGE__->meta->initialize;
18 # Creates get_all, get_all_count, get_all_iterator, delete_all and update_all.
19 #__PACKAGE__->meta->make_manager_class;
22 my ($self, $other) = @_;
24 return 1 if $self->transdate && !$other->transdate;
25 return -1 if !$self->transdate && $other->transdate;
28 $result = $self->transdate <=> $other->transdate if $self->transdate;
29 return $result || ($self->id <=> $other->id);
35 #my $record_links = $self->linked_records(direction => 'both');
39 my $record_links = SL::DB::Manager::RecordLink->get_all(where => [ from_table => 'bank_transactions', from_id => $self->id ]);
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';
46 return [ @linked_invoices ];
49 sub get_agreement_with_invoice {
50 my ($self, $invoice) = @_;
52 die "first argument is not an invoice object"
53 unless ref($invoice) eq 'SL::DB::Invoice' or ref($invoice) eq 'SL::DB::PurchaseInvoice';
56 cust_vend_name_in_purpose => 1,
57 cust_vend_number_in_purpose => 1,
62 datebonus_negative => -1,
63 depositor_matches => 2,
65 exact_open_amount => 4,
66 invnumber_in_purpose => 2,
67 own_invnumber_in_purpose => 5,
68 # overpayment => -1, # either other invoice is more likely, or several invoices paid at once
69 payment_before_invoice => -2,
70 payment_within_30_days => 1,
71 remote_account_number => 3,
72 skonto_exact_amount => 5,
76 my ($agreement,$rule_matches);
78 # compare banking arrangements
79 my ($iban, $bank_code, $account_number);
80 $bank_code = $invoice->customer->bank_code if $invoice->is_sales;
81 $account_number = $invoice->customer->account_number if $invoice->is_sales;
82 $iban = $invoice->customer->iban if $invoice->is_sales;
83 $bank_code = $invoice->vendor->bank_code if ! $invoice->is_sales;
84 $iban = $invoice->vendor->iban if ! $invoice->is_sales;
85 $account_number = $invoice->vendor->account_number if ! $invoice->is_sales;
86 if ( $bank_code eq $self->remote_bank_code && $account_number eq $self->remote_account_number ) {
87 $agreement += $points{remote_account_number};
88 $rule_matches .= 'remote_account_number(' . $points{'remote_account_number'} . ') ';
90 if ( $iban eq $self->remote_account_number ) {
91 $agreement += $points{remote_account_number};
92 $rule_matches .= 'remote_account_number(' . $points{'remote_account_number'} . ') ';
95 my $datediff = $self->transdate->{utc_rd_days} - $invoice->transdate->{utc_rd_days};
96 $invoice->{datediff} = $datediff;
99 if (abs(abs($invoice->amount) - abs($self->amount)) < 0.01) {
100 $agreement += $points{exact_amount};
101 $rule_matches .= 'exact_amount(' . $points{'exact_amount'} . ') ';
104 # compare open amount, preventing double points when open amount = invoice amount
105 if ( $invoice->amount != $invoice->open_amount && abs(abs($invoice->open_amount) - abs($self->amount)) < 0.01) {
106 $agreement += $points{exact_open_amount};
107 $rule_matches .= 'exact_open_amount(' . $points{'exact_open_amount'} . ') ';
110 if ( $invoice->skonto_date && abs(abs($invoice->amount_less_skonto) - abs($self->amount)) < 0.01) {
111 $agreement += $points{skonto_exact_amount};
112 $rule_matches .= 'skonto_exact_amount(' . $points{'skonto_exact_amount'} . ') ';
115 #search invoice number in purpose
116 my $invnumber = $invoice->invnumber;
117 # invnumbernhas to have at least 3 characters
118 my $squashed_purpose = $self->purpose;
119 $squashed_purpose =~ s/ //g;
120 if (length($invnumber) > 4 && $squashed_purpose =~ /$invnumber/ && $invoice->is_sales){
121 $agreement += $points{own_invnumber_in_purpose};
122 $rule_matches .= 'own_invnumber_in_purpose(' . $points{'own_invnumber_in_purpose'} . ') ';
123 } elsif (length($invnumber) > 3 && $squashed_purpose =~ /$invnumber/ ) {
124 $agreement += $points{invnumber_in_purpose};
125 $rule_matches .= 'invnumber_in_purpose(' . $points{'invnumber_in_purpose'} . ') ';
129 if ( $invoice->is_sales && $self->amount < 0 ) {
130 $agreement += $points{wrong_sign};
131 $rule_matches .= 'wrong_sign(' . $points{'wrong_sign'} . ') ';
133 if ( ! $invoice->is_sales && $self->amount > 0 ) {
134 $agreement += $points{wrong_sign};
135 $rule_matches .= 'wrong_sign(' . $points{'wrong_sign'} . ') ';
138 # search customer/vendor number in purpose
140 $cvnumber = $invoice->customer->customernumber if $invoice->is_sales;
141 $cvnumber = $invoice->vendor->vendornumber if ! $invoice->is_sales;
142 if ( $cvnumber && $self->purpose =~ /\b$cvnumber\b/i ) {
143 $agreement += $points{cust_vend_number_in_purpose};
144 $rule_matches .= 'cust_vend_number_in_purpose(' . $points{'cust_vend_number_in_purpose'} . ') ';
147 # search for customer/vendor name in purpose (may contain GMBH, CO KG, ...)
149 $cvname = $invoice->customer->name if $invoice->is_sales;
150 $cvname = $invoice->vendor->name if ! $invoice->is_sales;
151 if ( $cvname && $self->purpose =~ /\b$cvname\b/i ) {
152 $agreement += $points{cust_vend_name_in_purpose};
153 $rule_matches .= 'cust_vend_name_in_purpose(' . $points{'cust_vend_name_in_purpose'} . ') ';
156 # compare depositorname, don't try to match empty depositors
158 $depositorname = $invoice->customer->depositor if $invoice->is_sales;
159 $depositorname = $invoice->vendor->depositor if ! $invoice->is_sales;
160 if ( $depositorname && $self->remote_name =~ /$depositorname/ ) {
161 $agreement += $points{depositor_matches};
162 $rule_matches .= 'depositor_matches(' . $points{'depositor_matches'} . ') ';
165 #Check if words in remote_name appear in cvname
166 my $check_string_points = _check_string($self->remote_name,$cvname);
167 if ( $check_string_points ) {
168 $agreement += $check_string_points;
169 $rule_matches .= 'remote_name(' . $check_string_points . ') ';
172 # transdate prefilter: compare transdate of bank_transaction with transdate of invoice
173 if ( $datediff < -5 ) { # this might conflict with advance payments
174 $agreement += $points{payment_before_invoice};
175 $rule_matches .= 'payment_before_invoice(' . $points{'payment_before_invoice'} . ') ';
177 if ( $datediff < 30 ) {
178 $agreement += $points{payment_within_30_days};
179 $rule_matches .= 'payment_within_30_days(' . $points{'payment_within_30_days'} . ') ';
182 # only if we already have a good agreement, let date further change value of agreement.
183 # this is so that if there are several plausible open invoices which are all equal
184 # (rent jan, rent feb...) the one with the best date match is chosen over
187 # another way around this is to just pre-filter by periods instead of matching everything
188 if ( $agreement > 5 ) {
189 if ( $datediff == 0 ) {
190 $agreement += $points{datebonus0};
191 $rule_matches .= 'datebonus0(' . $points{'datebonus0'} . ') ';
192 } elsif ( $datediff > 0 and $datediff <= 14 ) {
193 $agreement += $points{datebonus14};
194 $rule_matches .= 'datebonus14(' . $points{'datebonus14'} . ') ';
195 } elsif ( $datediff >14 and $datediff < 35) {
196 $agreement += $points{datebonus35};
197 $rule_matches .= 'datebonus35(' . $points{'datebonus35'} . ') ';
198 } elsif ( $datediff >34 and $datediff < 120) {
199 $agreement += $points{datebonus120};
200 $rule_matches .= 'datebonus120(' . $points{'datebonus120'} . ') ';
201 } elsif ( $datediff < 0 ) {
202 $agreement += $points{datebonus_negative};
203 $rule_matches .= 'datebonus_negative(' . $points{'datebonus_negative'} . ') ';
205 # e.g. datediff > 120
209 return ($agreement,$rule_matches);
213 my $bankstring = shift;
214 my $namestring = shift;
215 return 0 unless $bankstring and $namestring;
217 my @bankwords = grep(/^\w+$/, split(/\b/,$bankstring));
220 foreach my $bankword ( @bankwords ) {
221 # only try to match strings with more than 2 characters
222 next unless length($bankword)>2;
223 if ( $namestring =~ /\b$bankword\b/i ) {
238 SL::DB::BankTransaction
244 =item C<get_agreement_with_invoice $invoice>
246 Using a point system this function checks whether the bank transaction matches
247 an invoices, using a variety of tests, such as
253 =item * amount_less_skonto
257 =item * invoice number in purpose
259 =item * customer or vendor name in purpose
261 =item * account number matches account number of customer or vendor
265 The total number of points, and the rules that matched, are returned.
268 my $bt = SL::DB::Manager::BankTransaction->find_by(id => 522);
269 my $invoice = SL::DB::Manager::Invoice->find_by(invnumber => '198');
270 my ($agreement,rule_matches) = $bt->get_agreement_with_invoice($invoice);
276 G. Richardson E<lt>grichardson@kivitendo-premium.de<gt>