Revision 503fabbf
Von Martin Helmling martin.helmling@octosoft.eu vor mehr als 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 |
|
SL/DB/BankTransaction.pm | ||
---|---|---|
47 | 47 |
return [ @linked_invoices ]; |
48 | 48 |
} |
49 | 49 |
|
50 |
sub is_collective_transaction { |
|
51 |
$_[0]->transaction_code eq "191"; |
|
52 |
} |
|
53 |
|
|
54 |
|
|
50 | 55 |
sub get_agreement_with_invoice { |
51 | 56 |
my ($self, $invoice) = @_; |
52 | 57 |
|
... | ... | |
73 | 78 |
skonto_exact_amount => 5, |
74 | 79 |
wrong_sign => -1, |
75 | 80 |
sepa_export_item => 5, |
81 |
collective_sepa_transaction => 20, |
|
76 | 82 |
); |
77 | 83 |
|
78 | 84 |
my ($agreement,$rule_matches); |
79 | 85 |
|
86 |
if ( $self->is_collective_transaction && $self->{sepa_export_ok}) { |
|
87 |
$agreement += $points{collective_sepa_transaction}; |
|
88 |
$rule_matches .= 'collective_sepa_transaction(' . $points{'collective_sepa_transaction'} . ') '; |
|
89 |
} |
|
90 |
|
|
80 | 91 |
# compare banking arrangements |
81 | 92 |
my ($iban, $bank_code, $account_number); |
82 | 93 |
$bank_code = $invoice->customer->bank_code if $invoice->is_sales; |
... | ... | |
210 | 221 |
}; |
211 | 222 |
|
212 | 223 |
# 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) { |
|
224 |
if ( my $seis = $invoice->{sepa_export_item} ) {
|
|
225 |
if (scalar @$seis == 1) {
|
|
215 | 226 |
my $sei = $seis->[0]; |
216 | 227 |
|
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) { |
|
228 |
if ( abs(abs($self->amount) - abs($sei->amount)) < 0.01 ) { |
|
222 | 229 |
$agreement += $points{sepa_export_item}; |
223 | 230 |
$rule_matches .= 'sepa_export_item(' . $points{'sepa_export_item'} . ') '; |
224 | 231 |
} |
SL/Dev/Payment.pm | ||
---|---|---|
39 | 39 |
$bank_account->save; |
40 | 40 |
} |
41 | 41 |
|
42 |
sub create_sepa_export { |
|
43 |
my (%params) = @_; |
|
44 |
my $sepa_export = SL::DB::SepaExport->new( |
|
45 |
closed => 0, |
|
46 |
employee_id => $params{employee_id} // SL::DB::Manager::Employee->current->id, |
|
47 |
executed => 0, |
|
48 |
vc => 'customer', |
|
49 |
); |
|
50 |
$sepa_export->assign_attributes(%params) if %params; |
|
51 |
$sepa_export->save; |
|
52 |
} |
|
53 |
|
|
54 |
sub create_sepa_export_item { |
|
55 |
my (%params) = @_; |
|
56 |
my $sepa_exportitem = SL::DB::SepaExportItem->new( |
|
57 |
chart_id => delete $params{chart_id} // $::instance_conf->get_ar_paid_accno_id, |
|
58 |
payment_type => 'without_skonto', |
|
59 |
our_bic => 'BANK1234', |
|
60 |
our_iban => 'DE12500105170648489890', |
|
61 |
); |
|
62 |
$sepa_exportitem->assign_attributes(%params) if %params; |
|
63 |
$sepa_exportitem->save; |
|
64 |
} |
|
65 |
|
|
42 | 66 |
sub create_bank_transaction { |
43 | 67 |
my (%params) = @_; |
44 | 68 |
|
t/bank/bank_transactions.t | ||
---|---|---|
1 |
use Test::More tests => 105;
|
|
1 |
use Test::More tests => 130;
|
|
2 | 2 |
|
3 | 3 |
use strict; |
4 | 4 |
|
... | ... | |
41 | 41 |
SL::DB::Manager::Part->delete_all(all => 1); |
42 | 42 |
SL::DB::Manager::Customer->delete_all(all => 1); |
43 | 43 |
SL::DB::Manager::Vendor->delete_all(all => 1); |
44 |
SL::DB::Manager::SepaExportItem->delete_all(all => 1); |
|
45 |
SL::DB::Manager::SepaExport->delete_all(all => 1); |
|
44 | 46 |
SL::DB::Manager::BankAccount->delete_all(all => 1); |
45 | 47 |
SL::DB::Manager::PaymentTerm->delete_all(all => 1); |
46 | 48 |
SL::DB::Manager::Currency->delete_all(where => [ name => 'CUR' ]); |
... | ... | |
79 | 81 |
test_ap_payment_part_transaction(); |
80 | 82 |
test_neg_sales_invoice(); |
81 | 83 |
|
84 |
test_bt_rule1(); |
|
85 |
test_sepa_export(); |
|
86 |
|
|
82 | 87 |
# remove all created data at end of test |
83 | 88 |
clear_up(); |
84 | 89 |
|
... | ... | |
656 | 661 |
is($bt->invoice_amount , '-345.10000', "$testname: bt invoice_amount ok"); |
657 | 662 |
} |
658 | 663 |
|
664 |
sub test_bt_rule1 { |
|
665 |
|
|
666 |
my $testname = 'test_bt_rule1'; |
|
667 |
|
|
668 |
$ar_transaction = test_ar_transaction(invnumber => 'bt_rule1'); |
|
669 |
|
|
670 |
my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction"; |
|
671 |
|
|
672 |
$ar_transaction->load; |
|
673 |
$bt->load; |
|
674 |
is($ar_transaction->paid , '0.00000' , "$testname: not paid"); |
|
675 |
is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned"); |
|
676 |
|
|
677 |
my $bt_controller = SL::Controller::BankTransaction->new; |
|
678 |
$::form->{dont_render_for_test} = 1; |
|
679 |
$::form->{filter}{bank_account} = $bank_account->id; |
|
680 |
my $bt_transactions = $bt_controller->action_list; |
|
681 |
|
|
682 |
is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction"); |
|
683 |
is($bt_transactions->[0]->{agreement}, 20 , "$testname: agreement == 20"); |
|
684 |
my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}}); |
|
685 |
#print "rule_matches='".$match."'\n"; |
|
686 |
is($match, |
|
687 |
"remote_account_number(3) exact_amount(4) own_invnumber_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) ", |
|
688 |
"$testname: rule_matches ok"); |
|
689 |
$bt->invoice_amount($bt->amount); |
|
690 |
$bt->save; |
|
691 |
is($bt->invoice_amount , '119.00000' , "$testname: bt invoice amount now set"); |
|
692 |
}; |
|
693 |
|
|
694 |
sub test_sepa_export { |
|
695 |
|
|
696 |
my $testname = 'test_sepa_export'; |
|
697 |
|
|
698 |
$ar_transaction = test_ar_transaction(invnumber => 'sepa1'); |
|
699 |
|
|
700 |
my $bt = SL::Dev::Payment::create_bank_transaction(record => $ar_transaction) or die "Couldn't create bank_transaction"; |
|
701 |
my $se = SL::Dev::Payment::create_sepa_export(); |
|
702 |
my $sei = SL::Dev::Payment::create_sepa_export_item( |
|
703 |
chart_id => $bank->id, |
|
704 |
ar_id => $ar_transaction->id, |
|
705 |
sepa_export_id => $se->id, |
|
706 |
vc_iban => $customer->iban, |
|
707 |
vc_bic => $customer->bic, |
|
708 |
vc_mandator_id => $customer->mandator_id, |
|
709 |
vc_depositor => $customer->depositor, |
|
710 |
amount => $ar_transaction->amount, |
|
711 |
); |
|
712 |
|
|
713 |
$ar_transaction->load; |
|
714 |
$bt->load; |
|
715 |
$sei->load; |
|
716 |
is($ar_transaction->paid , '0.00000' , "$testname: sepa1 not paid"); |
|
717 |
is($bt->invoice_amount , '0.00000' , "$testname: bt invoice amount was not assigned"); |
|
718 |
is($bt->amount , '119.00000' , "$testname: bt amount ok"); |
|
719 |
is($sei->amount , '119.00000' , "$testname: sepa export amount ok"); |
|
720 |
|
|
721 |
my $bt_controller = SL::Controller::BankTransaction->new; |
|
722 |
$::form->{dont_render_for_test} = 1; |
|
723 |
$::form->{filter}{bank_account} = $bank_account->id; |
|
724 |
my $bt_transactions = $bt_controller->action_list; |
|
725 |
|
|
726 |
is(scalar(@$bt_transactions) , 1 , "$testname: one bank_transaction"); |
|
727 |
is($bt_transactions->[0]->{agreement}, 25 , "$testname: agreement == 25"); |
|
728 |
my $match = join ( ' ',@{$bt_transactions->[0]->{rule_matches}}); |
|
729 |
is($match, |
|
730 |
"remote_account_number(3) exact_amount(4) own_invnumber_in_purpose(5) depositor_matches(2) remote_name(2) payment_within_30_days(1) datebonus0(3) sepa_export_item(5) ", |
|
731 |
"$testname: rule_matches ok"); |
|
732 |
}; |
|
733 |
|
|
734 |
|
|
659 | 735 |
1; |
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