Reconciliation: Veto, kein invoice_amount nachträglich setzen.
[kivitendo-erp.git] / SL / Controller / Reconciliation.pm
1 package SL::Controller::Reconciliation;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Locale::String;
8 use SL::JSON;
9 use SL::Controller::Helper::ParseFilter;
10 use SL::Helper::Flash;
11
12 use SL::DB::BankTransaction;
13 use SL::DB::Manager::BankAccount;
14 use SL::DB::AccTransaction;
15 use SL::DB::ReconciliationLink;
16 use List::Util qw(sum);
17
18 use Rose::Object::MakeMethods::Generic (
19   'scalar --get_set_init' => [ qw(cleared BANK_ACCOUNTS) ],
20 );
21
22 __PACKAGE__->run_before('check_auth');
23 __PACKAGE__->run_before('_bank_account');
24
25 #
26 # actions
27 #
28
29 sub action_search {
30   my ($self) = @_;
31
32   $self->setup_search_action_bar;
33   $self->render('reconciliation/search');
34 }
35
36 sub action_reconciliation {
37   my ($self) = @_;
38
39   $self->_get_proposals;
40
41   $self->_get_linked_transactions;
42
43   $self->_get_balances;
44
45   $self->setup_reconciliation_action_bar;
46   $self->render('reconciliation/form',
47                 ui_tab => scalar(@{$self->{PROPOSALS}}) > 0?1:0,
48                 title => t8('Reconciliation'));
49 }
50
51 sub action_load_overview {
52   my ($self) = @_;
53
54   $self->_get_proposals;
55
56   $self->_get_linked_transactions;
57
58   $self->_get_balances;
59
60   my $output = $self->render('reconciliation/tabs/overview', { output => 0 });
61   my %result = ( html => $output );
62
63   $self->render(\to_json(\%result), { type => 'json', process => 0 });
64 }
65
66 sub action_filter_overview {
67   my ($self) = @_;
68
69   $self->_get_linked_transactions;
70   $self->_get_balances;
71
72   my $output = $self->render('reconciliation/_linked_transactions', { output => 0 });
73   my %result = ( html               => $output,
74                  absolut_bt_balance => $::form->format_amount(\%::myconfig,      $self->{absolut_bt_balance}, 2),
75                  absolut_bb_balance => $::form->format_amount(\%::myconfig, -1 * $self->{absolut_bb_balance}, 2),
76                  bt_balance         => $::form->format_amount(\%::myconfig,      $self->{bt_balance}, 2),
77                  bb_balance         => $::form->format_amount(\%::myconfig, -1 * $self->{bb_balance}, 2)
78                  );
79
80   $self->render(\to_json(\%result), { type => 'json', process => 0 });
81 }
82
83 sub action_update_reconciliation_table {
84   my ($self) = @_;
85
86   my @errors = $self->_get_elements_and_validate();
87
88   my $output = $self->render('reconciliation/assigning_table', { output => 0 },
89                  bt_sum => $::form->format_amount(\%::myconfig, $self->{bt_sum}, 2),
90                  bb_sum => $::form->format_amount(\%::myconfig, -1 * $self->{bb_sum}, 2),
91                  errors => @errors,
92                  );
93
94   my %result = ( html => $output );
95
96   $self->render(\to_json(\%result), { type => 'json', process => 0 });
97 }
98
99 sub action_reconcile {
100   my ($self) = @_;
101
102   #Check elements
103   my @errors = $self->_get_elements_and_validate;
104
105   if (@errors) {
106     unshift(@errors, (t8('Could not reconcile chosen elements!')));
107     flash('error', @errors);
108     $self->action_reconciliation;
109     return;
110   }
111
112   $self->_reconcile;
113
114   $self->action_reconciliation;
115 }
116
117 sub action_delete_reconciliation {
118   my ($self) = @_;
119
120   my $rec_links = SL::DB::Manager::ReconciliationLink->get_all(where => [ rec_group => $::form->{rec_group} ]);
121
122   foreach my $rec_link (@{ $rec_links }) {
123     my $bank_transaction = SL::DB::Manager::BankTransaction->find_by( id           => $rec_link->bank_transaction_id );
124     my $acc_transaction  = SL::DB::Manager::AccTransaction ->find_by( acc_trans_id => $rec_link->acc_trans_id        );
125
126     $bank_transaction->cleared('0');
127     $acc_transaction->cleared('0');
128
129     $bank_transaction->save;
130     $acc_transaction->save;
131
132     $rec_link->delete;
133   }
134
135   $self->_get_linked_transactions;
136   $self->_get_balances;
137
138   my $output = $self->render('reconciliation/_linked_transactions', { output => 0 });
139   my %result = ( html               => $output,
140                  absolut_bt_balance => $::form->format_amount(\%::myconfig,      $self ->{absolut_bt_balance}, 2),
141                  absolut_bb_balance => $::form->format_amount(\%::myconfig, -1 * $self ->{absolut_bb_balance}, 2),
142                  bt_balance         => $::form->format_amount(\%::myconfig,      $self ->{bt_balance}, 2),
143                  bb_balance         => $::form->format_amount(\%::myconfig, -1 * $self ->{bb_balance}, 2)
144                  );
145
146   $self->render(\to_json(\%result), { type => 'json', process => 0 });
147 }
148
149 sub action_load_proposals {
150   my ($self) = @_;
151
152   $self->_get_proposals;
153
154   my $output = $self->render('reconciliation/tabs/automatic', { output => 0 });
155   my %result = ( html => $output );
156
157   $self->render(\to_json(\%result), { type => 'json', process => 0 });
158 }
159
160 sub action_filter_proposals {
161   my ($self) = @_;
162
163   $self->_get_balances;
164   $self->_get_proposals;
165
166   my $output = $self->render('reconciliation/proposals', { output => 0 });
167   my %result = ( html               => $output,
168                  absolut_bt_balance => $::form->format_amount(\%::myconfig,      $self ->{absolut_bt_balance}, 2),
169                  absolut_bb_balance => $::form->format_amount(\%::myconfig, -1 * $self ->{absolut_bb_balance}, 2),
170                  bt_balance         => $::form->format_amount(\%::myconfig,      $self ->{bt_balance}, 2),
171                  bb_balance         => $::form->format_amount(\%::myconfig, -1 * $self ->{bb_balance}, 2)
172                  );
173
174   $self->render(\to_json(\%result), { type => 'json', process => 0 });
175 }
176
177 sub action_reconcile_proposals {
178   my ($self) = @_;
179
180   my $counter = 0;
181
182   foreach my $bt_id ( @{ $::form->{bt_ids} }) {
183     my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
184     my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
185     $bank_transaction->cleared('1');
186     $bank_transaction->save;
187     foreach my $acc_trans_id (@{ $::form->{proposal_list}->{$bt_id}->{BB} }) {
188       SL::DB::ReconciliationLink->new(
189         rec_group => $rec_group,
190         bank_transaction_id => $bt_id,
191         acc_trans_id => $acc_trans_id
192       )->save;
193       my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $acc_trans_id);
194       $acc_trans->cleared('1');
195       $acc_trans->save;
196     }
197     $counter++;
198   }
199
200   flash('ok', t8('#1 proposal(s) saved.', $counter));
201
202   $self->action_reconciliation;
203 }
204
205 #
206 # filters
207 #
208
209 sub check_auth {
210   $::auth->assert('bank_transaction');
211 }
212
213 sub _bank_account {
214   my ($self) = @_;
215   $self->{bank_account} = SL::DB::Manager::BankAccount->find_by(id => $::form->{filter}->{"local_bank_account_id:number"});
216 }
217
218 #
219 # helpers
220 #
221
222 sub _get_proposals {
223   my ($self) = @_;
224
225   # reconciliation suggestion is based on:
226   # * record_link exists (was paid by bank transaction)
227   # or acc_trans entry exists where
228   # * amount is exactly the same
229   # * date is the same
230   # * IBAN or account number have to match exactly (cv details, no spaces)
231   # * not a gl storno
232   # * there is exactly one match for all conditions
233
234   $self->_filter_to_where;
235
236   my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => [ @{ $self->{bt_where} }, cleared => '0' ]);
237
238   my $check_sum;
239
240   my @proposals;
241
242   foreach my $bt (@{ $bank_transactions }) {
243     $check_sum = $bt->amount;
244     my $proposal;
245     $proposal->{BT} = $bt;
246     $proposal->{BB} = [];
247
248     # first of all check if any of the bank_transactions are already linked (i.e. were paid via bank transactions)
249     my $linked_records = SL::DB::Manager::RecordLink->get_all(where => [ from_table => 'bank_transactions', from_id => $bt->id ]);
250     foreach my $linked_record (@{ $linked_records }) {
251       my $invoice;
252       if ($linked_record->to_table eq 'ar') {
253         $invoice = SL::DB::Manager::Invoice->find_by(id => $linked_record->to_id);
254         #find payments
255         my $payments = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, chart_link => { like => '%AR_paid%' }, transdate => $bt->transdate ]);
256         foreach my $payment (@{ $payments }) {
257           $check_sum += $payment->amount;
258           push @{ $proposal->{BB} }, $payment;
259         }
260       }
261       if ($linked_record->to_table eq 'ap') {
262         $invoice = SL::DB::Manager::PurchaseInvoice->find_by(id => $linked_record->to_id);
263         #find payments
264         my $payments = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, chart_link => { like => '%AP_paid%' }, transdate => $bt->transdate ]);
265         foreach my $payment (@{ $payments }) {
266           $check_sum += $payment->amount;
267           push @{ $proposal->{BB} }, $payment;
268         }
269       }
270     }
271
272     #add proposal if something in acc_trans was found
273     #otherwise try to find another entry in acc_trans and add it
274     # for linked_records we allow a slight difference / imprecision, for acc_trans search we don't
275     if (scalar @{ $proposal->{BB} } and abs($check_sum) <= 0.01 ) {
276       push @proposals, $proposal;
277     } elsif (!scalar @{ $proposal->{BB} }) {
278       # use account_number and iban for matching remote account number
279       # don't suggest gl stornos (ar and ap stornos shouldn't have any payments)
280
281       my @account_number_match = (
282         ( 'ar.customer.iban'           => $bt->remote_account_number ),
283         ( 'ar.customer.account_number' => $bt->remote_account_number ),
284         ( 'ap.vendor.iban'             => $bt->remote_account_number ),
285         ( 'ap.vendor.account_number'   => $bt->remote_account_number ),
286         ( 'gl.storno'                  => '0' ),
287       );
288
289       my $acc_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ @{ $self->{bb_where} },
290                                                                                  amount => -1 * $bt->amount,
291                                                                                  cleared => '0',
292                                                                                  'transdate' => $bt->transdate,
293                                                                                  or => [ @account_number_match ]
294                                                                                ],
295                                                                        with_objects => [ 'ar', 'ap', 'ar.customer', 'ap.vendor', 'gl' ]);
296       if (scalar @{ $acc_transactions } == 1) {
297         push @{ $proposal->{BB} }, @{ $acc_transactions }[0];
298         push @proposals, $proposal;
299       }
300     }
301   }
302
303   $self->{PROPOSALS} = \@proposals;
304 }
305
306 sub _get_elements_and_validate {
307   my ($self) = @_;
308
309   my @errors;
310
311   if ( not defined $::form->{bt_ids} ) {
312     push @errors, t8('No bank account chosen!');
313   }
314
315   if ( not defined $::form->{bb_ids} ) {
316     push @errors, t8('No transaction on chart bank chosen!');
317   }
318
319   if (!@errors) {
320     if (scalar @{ $::form->{bt_ids} } > 1 and scalar @{ $::form->{bb_ids} } > 1) {
321       push @errors, t8('No 1:n or n:1 relation');
322     }
323   }
324
325   my @elements;
326   my ($bt_sum, $bb_sum) = (0,0);
327
328   foreach my $bt_id (@{ $::form->{bt_ids} }) {
329     my $bt = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
330     $bt->{type} = 'BT';
331     $bt_sum += $bt->amount;
332     push @elements, $bt;
333   }
334
335   foreach my $bb_id (@{ $::form->{bb_ids} }) {
336     my $bb = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $bb_id);
337     $bb->{type} = 'BB';
338     $bb->{id} = $bb->acc_trans_id;
339     $bb_sum += $bb->amount;
340     push @elements, $bb;
341   }
342
343   if ($::form->round_amount($bt_sum + $bb_sum, 2) != 0) {
344     push @errors, t8('Out of balance!'), t8('Sum of bank #1 and sum of bookings #2',$bt_sum, $bb_sum);
345   }
346
347   $self->{ELEMENTS} = \@elements;
348   $self->{bt_sum} = $bt_sum;
349   $self->{bb_sum} = $bb_sum;
350
351   return @errors;
352 }
353
354 sub _reconcile {
355   my ($self) = @_;
356
357   # 1. step: set AccTrans and BankTransactions to 'cleared'
358   foreach my $element (@{ $self->{ELEMENTS} }) {
359     $element->cleared('1');
360     # veto either invoice_amount is fully assigned or not! No state tricks in later workflow!
361     # invoice_amount should be a distinct sign, that some bookings were really made from a bank transaction
362     # $element->invoice_amount($element->amount) if $element->isa('SL::DB::BankTransaction');
363     $element->save;
364   }
365
366   # 2. step: insert entry in reconciliation_links
367   my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
368   #There is either a 1:n relation or a n:1 relation
369   if (scalar @{ $::form->{bt_ids} } == 1) {
370     my $bt_id = @{ $::form->{bt_ids} }[0];
371     foreach my $bb_id (@{ $::form->{bb_ids} }) {
372       my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
373                                                      acc_trans_id        => $bb_id,
374                                                      rec_group           => $rec_group);
375       $rec_link->save;
376     }
377   } else {
378     my $bb_id = @{ $::form->{bb_ids} }[0];
379     foreach my $bt_id (@{ $::form->{bt_ids} }) {
380       my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
381                                                      acc_trans_id        => $bb_id,
382                                                      rec_group           => $rec_group);
383       $rec_link->save;
384     }
385   }
386 }
387
388 sub _filter_to_where {
389   my ($self) = @_;
390
391   my %parse_filter = parse_filter($::form->{filter});
392   my %filter = @{ $parse_filter{query} };
393
394   my (@rl_where, @bt_where, @bb_where);
395   @rl_where = ('bank_transaction.local_bank_account_id' => $filter{local_bank_account_id});
396   @bt_where = (local_bank_account_id => $filter{local_bank_account_id});
397   @bb_where = (chart_id              => $self->{bank_account}->chart_id);
398
399   if ($filter{fromdate} and $filter{todate}) {
400
401     push @rl_where, (or => [ and => [ 'acc_trans.transdate'        => $filter{fromdate},
402                                       'acc_trans.transdate'        => $filter{todate}   ],
403                              and => [ 'bank_transaction.transdate' => $filter{fromdate},
404                                       'bank_transaction.transdate' => $filter{todate}   ] ] );
405
406     push @bt_where, (transdate => $filter{todate} );
407     push @bt_where, (transdate => $filter{fromdate} );
408     push @bb_where, (transdate => $filter{todate} );
409     push @bb_where, (transdate => $filter{fromdate} );
410   }
411
412   if ( $self->{bank_account}->reconciliation_starting_date ) {
413     push @bt_where, (transdate => { ge => $self->{bank_account}->reconciliation_starting_date });
414     push @bb_where, (transdate => { ge => $self->{bank_account}->reconciliation_starting_date });
415   }
416
417   # don't try to reconcile opening and closing balance transactions
418   push @bb_where, ('acc_trans.ob_transaction' => 0);
419   push @bb_where, ('acc_trans.cb_transaction' => 0);
420
421   if ($filter{fromdate} and not $filter{todate}) {
422     push @rl_where, (or => [ 'acc_trans.transdate'        => $filter{fromdate},
423                              'bank_transaction.transdate' => $filter{fromdate} ] );
424     push @bt_where, (transdate                    => $filter{fromdate} );
425     push @bb_where, (transdate                    => $filter{fromdate} );
426   }
427
428   if ($filter{todate} and not $filter{fromdate}) {
429     push @rl_where, ( or => [ 'acc_trans.transdate'        => $filter{todate} ,
430                               'bank_transaction.transdate' => $filter{todate} ] );
431     push @bt_where, (transdate                    => $filter{todate} );
432     push @bb_where, (transdate                    => $filter{todate} );
433   }
434
435   if ($filter{cleared}) {
436     $filter{cleared} = $filter{cleared} eq 'FALSE' ? '0' : '1';
437     push @rl_where, ('acc_trans.cleared'        => $filter{cleared} );
438
439     push @bt_where, (cleared                    => $filter{cleared} );
440     push @bb_where, (cleared                    => $filter{cleared} );
441   }
442
443   $self->{rl_where} = \@rl_where;
444   $self->{bt_where} = \@bt_where;
445   $self->{bb_where} = \@bb_where;
446 }
447
448 sub _get_linked_transactions {
449   my ($self) = @_;
450
451   $self->_filter_to_where;
452
453   my (@where, @bt_where, @bb_where);
454   # don't try to reconcile opening and closing balances
455   # instead use an offset in configuration
456
457   @where    = (@{ $self->{rl_where} });
458   @bt_where = (@{ $self->{bt_where} }, cleared => '0');
459   @bb_where = (@{ $self->{bb_where} }, cleared => '0');
460
461   my @rows;
462
463   my $reconciliation_groups = SL::DB::Manager::ReconciliationLink->get_all(distinct => 1,
464                                                                            select => ['rec_group'],
465                                                                            where => \@where,
466                                                                            with_objects => ['bank_transaction', 'acc_trans']);
467
468   my $fromdate = $::locale->parse_date_to_object($::form->{filter}->{fromdate_date__ge});
469   my $todate   = $::locale->parse_date_to_object($::form->{filter}->{todate_date__le});
470
471   foreach my $rec_group (@{ $reconciliation_groups }) {
472     my $linked_transactions = SL::DB::Manager::ReconciliationLink->get_all(where => [rec_group => $rec_group->rec_group], with_objects => ['bank_transaction', 'acc_trans']);
473     my $line;
474     my $first_transaction = shift @{ $linked_transactions };
475     my $first_bt = $first_transaction->bank_transaction;
476     my $first_bb = $first_transaction->acc_trans;
477
478     if (defined $fromdate) {
479       $first_bt->{class} = 'out_of_balance' if ( $first_bt->transdate lt $fromdate );
480       $first_bb->{class} = 'out_of_balance' if ( $first_bb->transdate lt $fromdate );
481     }
482     if (defined $todate) {
483       $first_bt->{class} = 'out_of_balance' if ( $first_bt->transdate gt $todate );
484       $first_bb->{class} = 'out_of_balance' if ( $first_bb->transdate gt $todate );
485     }
486     $line->{BT} = [ $first_bt ];
487     $line->{BB} = [ $first_bb ];
488     $line->{rec_group} = $first_transaction->rec_group;
489     $line->{type} = 'Link';
490
491     #add the rest of transaction of this group
492     my ($previous_bt_id, $previous_acc_trans_id) = ($first_transaction->bank_transaction_id, $first_transaction->acc_trans_id);
493     foreach my $linked_transaction (@{ $linked_transactions }) {
494       my $bank_transaction = $linked_transaction->bank_transaction;
495       my $acc_transaction  = $linked_transaction->acc_trans;
496       if (defined $fromdate) {
497         $bank_transaction->{class} = 'out_of_balance' if ( $bank_transaction->transdate lt $fromdate );
498         $acc_transaction->{class}  = 'out_of_balance' if ( $acc_transaction->transdate  lt $fromdate );
499       }
500       if (defined $todate) {
501         $bank_transaction->{class} = 'out_of_balance' if ( $bank_transaction->transdate gt $todate );
502         $acc_transaction->{class}  = 'out_of_balance' if ( $acc_transaction->transdate  gt $todate );
503       }
504       if ($bank_transaction->id != $previous_bt_id) {
505         push @{ $line->{BT} }, $bank_transaction;
506       }
507       if ($acc_transaction->acc_trans_id != $previous_acc_trans_id) {
508         push @{ $line->{BB} }, $acc_transaction;
509       }
510     }
511     push @rows, $line;
512   }
513
514   # add non-cleared bank transactions
515   my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => \@bt_where);
516   foreach my $bt (@{ $bank_transactions }) {
517     my $line;
518     $line->{BT} = [ $bt ];
519     $line->{type} = 'BT';
520     $line->{id} = $bt->id;
521     push @rows, $line;
522   }
523
524   # add non-cleared bookings on bank
525   my $bookings_on_bank = SL::DB::Manager::AccTransaction->get_all(where => \@bb_where);
526   foreach my $bb (@{ $bookings_on_bank }) {
527     if ($::form->{filter}->{show_stornos} or !$bb->record->storno) {
528       my $line;
529       $line->{BB} = [ $bb ];
530       $line->{type} = 'BB';
531       $line->{id} = $bb->acc_trans_id;
532       push @rows, $line;
533     }
534   }
535
536   #sort lines
537   @rows = sort sort_by_transdate @rows;
538
539   $self->{LINKED_TRANSACTIONS} = \@rows;
540 }
541
542 sub sort_by_transdate {
543   if ($a->{BT} and $b->{BT}) {
544     return $a->{BT}[0]->amount <=> $b->{BT}[0]->amount if $a->{BT}[0]->transdate eq $b->{BT}[0]->transdate;
545     return $a->{BT}[0]->transdate cmp $b->{BT}[0]->transdate;
546   }
547   if ($a->{BT}) {
548     return $a->{BT}[0]->amount <=> (-1 * $b->{BB}[0]->amount) if $a->{BT}[0]->transdate eq $b->{BB}[0]->transdate;
549     return $a->{BT}[0]->transdate cmp $b->{BB}[0]->transdate;
550   }
551   if ($b->{BT}) {
552     return (-1 * $a->{BB}[0]->amount) <=> $b->{BT}[0]->amount if $a->{BB}[0]->transdate eq $b->{BT}[0]->transdate;
553     return $a->{BB}[0]->transdate cmp $b->{BT}[0]->transdate;
554   }
555   return (-1 * $a->{BB}[0]->amount) <=> (-1 * $b->{BB}[0]->amount) if $a->{BB}[0]->transdate eq $b->{BB}[0]->transdate;
556   return $a->{BB}[0]->transdate cmp $b->{BB}[0]->transdate;
557 }
558
559 sub _get_balances {
560   my ($self) = @_;
561
562   $self->_filter_to_where;
563
564   my (@bt_where, @bb_where);
565   @bt_where = @{ $self->{bt_where} };
566   @bb_where = @{ $self->{bb_where} };
567
568   my @all_bt_where = (local_bank_account_id => $self->{bank_account}->id);
569   my @all_bb_where = (chart_id              => $self->{bank_account}->chart_id);
570
571   my ($bt_balance, $bb_balance) = (0,0);
572   my ($absolut_bt_balance, $absolut_bb_balance) = (0,0);
573
574   if ( $self->{bank_account}->reconciliation_starting_date ) {
575     $bt_balance         = $self->{bank_account}->reconciliation_starting_balance;
576     $bb_balance         = $self->{bank_account}->reconciliation_starting_balance * -1;
577     $absolut_bt_balance = $self->{bank_account}->reconciliation_starting_balance;
578     $absolut_bb_balance = $self->{bank_account}->reconciliation_starting_balance * -1;
579
580     push @all_bt_where, ( transdate => { gt => $self->{bank_account}->reconciliation_starting_date });
581     push @all_bb_where, ( transdate => { gt => $self->{bank_account}->reconciliation_starting_date });
582   }
583
584   my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => \@bt_where );
585   my $payments          = SL::DB::Manager::AccTransaction ->get_all(where => \@bb_where );
586
587   # for absolute balance get all bookings until todate
588   my $todate   = $::locale->parse_date_to_object($::form->{filter}->{todate_date__le});
589   my $fromdate = $::locale->parse_date_to_object($::form->{filter}->{fromdate_date__le});
590
591   if ($todate) {
592     push @all_bt_where, (transdate => { le => $todate });
593     push @all_bb_where, (transdate => { le => $todate });
594   }
595
596   my $all_bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => \@all_bt_where);
597   my $all_payments          = SL::DB::Manager::AccTransaction ->get_all(where => \@all_bb_where);
598
599   $bt_balance += sum map { $_->amount } @{ $bank_transactions };
600   $bb_balance += sum map { $_->amount if ($::form->{filter}->{show_stornos} or !$_->record->storno) } @{ $payments };
601
602   $absolut_bt_balance += sum map { $_->amount } @{ $all_bank_transactions };
603   $absolut_bb_balance += sum map { $_->amount } @{ $all_payments };
604
605
606   $self->{bt_balance}         = $bt_balance || 0;
607   $self->{bb_balance}         = $bb_balance || 0;
608   $self->{absolut_bt_balance} = $absolut_bt_balance || 0;
609   $self->{absolut_bb_balance} = $absolut_bb_balance || 0;
610
611   $self->{difference} = $bt_balance + $bb_balance;
612 }
613
614 sub init_cleared {
615   [ { title => t8("all"),       value => ''           },
616     { title => t8("cleared"),   value => 'TRUE'       },
617     { title => t8("uncleared"), value => 'FALSE'      }, ]
618 }
619
620 sub init_BANK_ACCOUNTS {
621   SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
622 }
623
624 sub setup_search_action_bar {
625   my ($self, %params) = @_;
626
627   for my $bar ($::request->layout->get('actionbar')) {
628     $bar->add(
629       action => [
630         t8('Show'),
631         submit    => [ '#search_form', { action => 'Reconciliation/reconciliation' } ],
632         accesskey => 'enter',
633       ],
634     );
635   }
636 }
637
638 sub setup_reconciliation_action_bar {
639   my ($self, %params) = @_;
640
641   for my $bar ($::request->layout->get('actionbar')) {
642     $bar->add(
643       action => [
644         t8('Filter'),
645         call      => [ 'filter_table' ],
646         accesskey => 'enter',
647       ],
648     );
649   }
650 }
651
652 1;