Revision 503fabbf
Von Martin Helmling martin.helmling@octosoft.eu vor fast 7 Jahren hinzugefügt
SL/Controller/BankTransaction.pm | ||
---|---|---|
101 | 101 |
@where |
102 | 102 |
], |
103 | 103 |
); |
104 |
$main::lxdebug->message(LXDebug->DEBUG2(),"count bt=".scalar(@{$bank_transactions}." bank_account=".$bank_account->id." chart=".$bank_account->chart_id)); |
|
105 | 104 |
|
106 | 105 |
# credit notes have a negative amount, treat differently |
107 | 106 |
my $all_open_ar_invoices = SL::DB::Manager::Invoice ->get_all(where => [ or => [ amount => { gt => \'paid' }, |
... | ... | |
115 | 114 |
my $all_open_ap_invoices = SL::DB::Manager::PurchaseInvoice->get_all(where => [amount => { ne => \'paid' }], with_objects => ['vendor' ,'payment_terms']); |
116 | 115 |
my $all_open_sepa_export_items = SL::DB::Manager::SepaExportItem->get_all(where => [chart_id => $bank_account->chart_id , |
117 | 116 |
'sepa_export.executed' => 0, 'sepa_export.closed' => 0 ], with_objects => ['sepa_export']); |
118 |
$main::lxdebug->message(LXDebug->DEBUG2(),"count sepaexport=".scalar(@{$all_open_sepa_export_items})); |
|
119 | 117 |
|
120 | 118 |
my @all_open_invoices; |
121 | 119 |
# filter out invoices with less than 1 cent outstanding |
122 | 120 |
push @all_open_invoices, map { $_->{is_ar}=1 ; $_ } grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ar_invoices }; |
123 | 121 |
push @all_open_invoices, map { $_->{is_ar}=0 ; $_ } grep { abs($_->amount - $_->paid) >= 0.01 } @{ $all_open_ap_invoices }; |
124 |
$main::lxdebug->message(LXDebug->DEBUG2(),"bank_account=".$::form->{filter}{bank_account}." invoices: ".scalar(@{ $all_open_ar_invoices }). |
|
125 |
" + ".scalar(@{ $all_open_ap_invoices })." non fully paid=".scalar(@all_open_invoices)." transactions=".scalar(@{ $bank_transactions })); |
|
126 | 122 |
|
127 |
my @all_sepa_invoices; |
|
128 |
my @all_non_sepa_invoices; |
|
129 | 123 |
my %sepa_exports; |
130 | 124 |
# first collect sepa export items to open invoices |
131 | 125 |
foreach my $open_invoice (@all_open_invoices){ |
132 |
# my @items = grep { $_->ap_id == $open_invoice->id || $_->ar_id == $open_invoice->id } @{$all_open_sepa_export_items}; |
|
133 | 126 |
$open_invoice->{realamount} = $::form->format_amount(\%::myconfig,$open_invoice->amount,2); |
134 | 127 |
$open_invoice->{skonto_type} = 'without_skonto'; |
135 | 128 |
foreach ( @{$all_open_sepa_export_items}) { |
136 | 129 |
if ( $_->ap_id == $open_invoice->id || $_->ar_id == $open_invoice->id ) { |
137 | 130 |
my $factor = ($_->ar_id == $open_invoice->id?1:-1); |
138 |
$main::lxdebug->message(LXDebug->DEBUG2(),"exitem=".$_->id." for invoice ".$open_invoice->id." factor=".$factor);
|
|
131 |
#$main::lxdebug->message(LXDebug->DEBUG2(),"sepa_exitem=".$_->id." for invoice ".$open_invoice->id." factor=".$factor);
|
|
139 | 132 |
$open_invoice->{realamount} = $::form->format_amount(\%::myconfig,$open_invoice->amount*$factor,2); |
140 |
$open_invoice->{sepa_export_item} = $_ ;
|
|
133 |
push @{$open_invoice->{sepa_export_item}} , $_ ;
|
|
141 | 134 |
$open_invoice->{skonto_type} = $_->payment_type; |
142 | 135 |
$sepa_exports{$_->sepa_export_id} ||= { count => 0, is_ar => 0, amount => 0, proposed => 0, invoices => [], item => $_ }; |
143 | 136 |
$sepa_exports{$_->sepa_export_id}->{count}++ ; |
144 | 137 |
$sepa_exports{$_->sepa_export_id}->{is_ar}++ if $_->ar_id == $open_invoice->id; |
145 | 138 |
$sepa_exports{$_->sepa_export_id}->{amount} += $_->amount * $factor; |
146 | 139 |
push @{ $sepa_exports{$_->sepa_export_id}->{invoices} }, $open_invoice; |
147 |
#$main::lxdebug->message(LXDebug->DEBUG2(),"amount for export id ".$_->sepa_export_id." = ". |
|
148 |
# $sepa_exports{$_->sepa_export_id}->{amount}." count = ". |
|
149 |
# $sepa_exports{$_->sepa_export_id}->{count}." is_ar = ". |
|
150 |
# $sepa_exports{$_->sepa_export_id}->{is_ar} ); |
|
151 |
push @all_sepa_invoices , $open_invoice; |
|
152 | 140 |
} |
153 | 141 |
} |
154 |
push @all_non_sepa_invoices , $open_invoice if ! $open_invoice->{sepa_export_item}; |
|
155 | 142 |
} |
156 | 143 |
|
157 | 144 |
# try to match each bank_transaction with each of the possible open invoices |
... | ... | |
162 | 149 |
## 5 Stellen hinter dem Komma auf 2 Stellen reduzieren |
163 | 150 |
$bt->amount($bt->amount*1); |
164 | 151 |
$bt->invoice_amount($bt->invoice_amount*1); |
165 |
$main::lxdebug->message(LXDebug->DEBUG2(),"BT ".$bt->id." amount=".$bt->amount." invoice_amount=".$bt->invoice_amount." remote=". $bt->{remote_name}); |
|
166 | 152 |
|
167 | 153 |
$bt->{proposals} = []; |
168 | 154 |
$bt->{rule_matches} = []; |
169 | 155 |
|
170 | 156 |
$bt->{remote_name} .= $bt->{remote_name_1} if $bt->{remote_name_1}; |
171 | 157 |
|
172 |
if ( $self->is_collective_transaction($bt) ) {
|
|
158 |
if ( $bt->is_collective_transaction ) {
|
|
173 | 159 |
foreach ( keys %sepa_exports) { |
174 |
#$main::lxdebug->message(LXDebug->DEBUG2(),"Exp ID=".$_." compare sum amount ".($sepa_exports{$_}->{amount} *1) ." == ".($bt->amount * 1)); |
|
175 |
if ( $bt->transaction_code eq '191' && abs(($sepa_exports{$_}->{amount} * 1) - ($bt->amount * 1)) < 0.01 ) { |
|
160 |
if ( abs(($sepa_exports{$_}->{amount} * 1) - ($bt->amount * 1)) < 0.01 ) { |
|
176 | 161 |
## jupp |
177 | 162 |
@{$bt->{proposals}} = @{$sepa_exports{$_}->{invoices}}; |
178 |
$bt->{agreement} = 20; |
|
179 |
push(@{$bt->{rule_matches}},'sepa_export_item(20)'); |
|
163 |
$bt->{sepa_export_ok} = 1; |
|
180 | 164 |
$sepa_exports{$_}->{proposed}=1; |
181 |
#$main::lxdebug->message(LXDebug->DEBUG2(),"has ".scalar($bt->{proposals})." invoices"); |
|
182 | 165 |
push(@proposals, $bt); |
183 | 166 |
next; |
184 | 167 |
} |
185 | 168 |
} |
186 |
} |
|
187 |
next unless $bt->{remote_name}; # bank has no name, usually fees, use create invoice to assign |
|
188 |
|
|
189 |
foreach ( @{$all_open_sepa_export_items}) { |
|
190 |
last if scalar (@all_sepa_invoices) == 0; |
|
191 |
foreach my $open_invoice (@all_sepa_invoices){ |
|
192 |
$open_invoice->{agreement} = 0; |
|
193 |
$open_invoice->{rule_matches} =''; |
|
194 |
if ( $_->ap_id == $open_invoice->id || $_->ar_id == $open_invoice->id ) { |
|
195 |
#$main::lxdebug->message(LXDebug->DEBUG2(),"exitem2=".$_->id." for invoice ".$open_invoice->id); |
|
196 |
my $factor = ( $_->ar_id == $open_invoice->id?1:-1); |
|
197 |
$_->amount($_->amount*1); |
|
198 |
#$main::lxdebug->message(LXDebug->DEBUG2(),"remote account '".$bt->{remote_account_number}."' bt_amount=".$bt->amount." factor=".$factor); |
|
199 |
#$main::lxdebug->message(LXDebug->DEBUG2(),"compare with '".$_->vc_iban."' amount=".$_->amount); |
|
200 |
if ( $bt->{remote_account_number} eq $_->vc_iban && abs(abs($_->amount) - abs($bt->amount)) < 0.01 ) { |
|
201 |
my $iban; |
|
202 |
$iban = $open_invoice->customer->iban if $open_invoice->is_sales; |
|
203 |
$iban = $open_invoice->vendor->iban if ! $open_invoice->is_sales; |
|
204 |
if($bt->{remote_account_number} eq $iban && abs(abs($open_invoice->amount) - abs($bt->amount)) < 0.01 ) { |
|
205 |
($open_invoice->{agreement}, $open_invoice->{rule_matches}) = $bt->get_agreement_with_invoice($open_invoice); |
|
206 |
$open_invoice->{agreement} += 5; |
|
207 |
$open_invoice->{rule_matches} .= 'sepa_export_item(5) '; |
|
208 |
$main::lxdebug->message(LXDebug->DEBUG2(),"sepa invoice_id=".$open_invoice->id." agreement=".$open_invoice->{agreement}." rules matches=".$open_invoice->{rule_matches}); |
|
209 |
$open_invoice->{realamount} = $::form->format_amount(\%::myconfig,$open_invoice->amount*$factor,2); |
|
210 |
} |
|
211 |
} |
|
212 |
} |
|
213 |
} |
|
169 |
# colletive transaction has no remotename !! |
|
170 |
} else { |
|
171 |
next unless $bt->{remote_name}; # bank has no name, usually fees, use create invoice to assign |
|
214 | 172 |
} |
215 | 173 |
|
216 | 174 |
# try to match the current $bt to each of the open_invoices, saving the |
... | ... | |
222 | 180 |
# the arrays $bt->{proposals} and $bt->{rule_matches}, and the agreement |
223 | 181 |
# score is stored in $bt->{agreement} |
224 | 182 |
|
225 |
foreach my $open_invoice (@all_non_sepa_invoices, @all_sepa_invoices) {
|
|
183 |
foreach my $open_invoice (@all_open_invoices) {
|
|
226 | 184 |
($open_invoice->{agreement}, $open_invoice->{rule_matches}) = $bt->get_agreement_with_invoice($open_invoice); |
227 | 185 |
$open_invoice->{realamount} = $::form->format_amount(\%::myconfig, |
228 | 186 |
$open_invoice->amount * ($open_invoice->{is_ar} ? 1 : -1), 2); |
... | ... | |
259 | 217 |
: abs(@{ $_->{proposals} }[0]->amount + $_->amount) < 0.01) |
260 | 218 |
} @{ $bank_transactions }; |
261 | 219 |
|
262 |
push ( @proposals, @otherproposals);
|
|
220 |
push @proposals, @otherproposals;
|
|
263 | 221 |
|
264 | 222 |
# sort bank transaction proposals by quality (score) of proposal |
265 | 223 |
$bank_transactions = [ sort { $a->{agreement} <=> $b->{agreement} } @{ $bank_transactions } ] if $::form->{sort_by} eq 'proposal' and $::form->{sort_dir} == 1; |
266 | 224 |
$bank_transactions = [ sort { $b->{agreement} <=> $a->{agreement} } @{ $bank_transactions } ] if $::form->{sort_by} eq 'proposal' and $::form->{sort_dir} == 0; |
267 | 225 |
|
226 |
# for testing with t/bank/banktransaction.t : |
|
227 |
if ( $::form->{dont_render_for_test} ) { |
|
228 |
return $bank_transactions; |
|
229 |
} |
|
230 |
|
|
268 | 231 |
$::request->layout->add_javascripts("kivi.BankTransaction.js"); |
269 | 232 |
$self->render('bank_transactions/list', |
270 | 233 |
title => t8('Bank transactions MT940'), |
... | ... | |
545 | 508 |
|
546 | 509 |
} |
547 | 510 |
|
548 |
sub is_collective_transaction { |
|
549 |
my ($self, $bt) = @_; |
|
550 |
return $bt->transaction_code eq "191"; |
|
551 |
} |
|
552 |
|
|
553 | 511 |
sub save_single_bank_transaction { |
554 | 512 |
my ($self, %params) = @_; |
555 | 513 |
|
Auch abrufbar als: Unified diff
BankTransaction: Überarbeitung von "Kontoauszug verbuchen" , SEPA-Export wieder integriert
Die Punktebewertung findet wieder ausschließlich in "get_agreement_with_bank_transactions" statt,
auch die SEPA-Sammelüberweisung. Diese bekommt dor extra Punkte, da ggf. für bestimmte Rechnungen negative Punkte entstehen.
Auch gibt es dort keine Remote Banknummer etc.
Die Testdatei t/bank/bank_transactions.t wurde um zwei Tests erweitert,
1. ein Test der das Verbuchen ohne SEPA-Export macht,
2. ein Test mit SEPA-Export
fixt #277