Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[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   # reconcile transaction safe
183   SL::DB->client->with_transaction(sub {
184     foreach my $bt_id ( @{ $::form->{bt_ids} }) {
185       my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
186       my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
187       $bank_transaction->cleared('1');
188       $bank_transaction->save;
189       foreach my $acc_trans_id (@{ $::form->{proposal_list}->{$bt_id}->{BB} }) {
190         SL::DB::ReconciliationLink->new(
191           rec_group => $rec_group,
192           bank_transaction_id => $bt_id,
193           acc_trans_id => $acc_trans_id
194         )->save;
195         my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $acc_trans_id);
196         $acc_trans->cleared('1');
197         $acc_trans->save;
198       }
199       $counter++;
200     }
201
202   1;
203   }) or die t8('Unable to reconcile, database transaction failure');
204
205   flash('ok', t8('#1 proposal(s) saved.', $counter));
206
207   $self->action_reconciliation;
208 }
209
210 #
211 # filters
212 #
213
214 sub check_auth {
215   $::auth->assert('bank_transaction');
216 }
217
218 sub _bank_account {
219   my ($self) = @_;
220   $self->{bank_account} = SL::DB::Manager::BankAccount->find_by(id => $::form->{filter}->{"local_bank_account_id:number"});
221 }
222
223 #
224 # helpers
225 #
226
227 sub _get_proposals {
228   my ($self) = @_;
229
230   # reconciliation suggestion is based on:
231   # * record_link exists (was paid by bank transaction)
232   # or acc_trans entry exists where
233   # * amount is exactly the same
234   # * date is the same
235   # * IBAN or account number have to match exactly (cv details, no spaces)
236   # * not a gl storno
237   # * there is exactly one match for all conditions
238
239   $self->_filter_to_where;
240
241   my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => [ @{ $self->{bt_where} }, cleared => '0' ]);
242
243   my $check_sum;
244
245   my @proposals;
246
247   foreach my $bt (@{ $bank_transactions }) {
248     $check_sum = $bt->amount;
249     my $proposal;
250     $proposal->{BT} = $bt;
251     $proposal->{BB} = [];
252
253     # first of all check if any of the bank_transactions are already linked (i.e. were paid via bank transactions)
254     my $linked_records = SL::DB::Manager::RecordLink->get_all(where => [ from_table => 'bank_transactions', from_id => $bt->id ]);
255     foreach my $linked_record (@{ $linked_records }) {
256       my $invoice;
257       if ($linked_record->to_table eq 'ar') {
258         $invoice = SL::DB::Manager::Invoice->find_by(id => $linked_record->to_id);
259         #find payments
260         my $payments = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, chart_link => { like => '%AR_paid%' }, transdate => $bt->transdate ]);
261         foreach my $payment (@{ $payments }) {
262           $check_sum += $payment->amount;
263           push @{ $proposal->{BB} }, $payment;
264         }
265       }
266       if ($linked_record->to_table eq 'ap') {
267         $invoice = SL::DB::Manager::PurchaseInvoice->find_by(id => $linked_record->to_id);
268         #find payments
269         my $payments = SL::DB::Manager::AccTransaction->get_all(where => [ trans_id => $invoice->id, chart_link => { like => '%AP_paid%' }, transdate => $bt->transdate ]);
270         foreach my $payment (@{ $payments }) {
271           $check_sum += $payment->amount;
272           push @{ $proposal->{BB} }, $payment;
273         }
274       }
275     }
276
277     #add proposal if something in acc_trans was found
278     #otherwise try to find another entry in acc_trans and add it
279     # for linked_records we allow a slight difference / imprecision, for acc_trans search we don't
280     if (scalar @{ $proposal->{BB} } and abs($check_sum) <= 0.01 ) {
281       push @proposals, $proposal;
282     } elsif (!scalar @{ $proposal->{BB} }) {
283       # use account_number and iban for matching remote account number
284       # don't suggest gl stornos (ar and ap stornos shouldn't have any payments)
285
286       my @account_number_match = (
287         ( 'ar.customer.iban'           => $bt->remote_account_number ),
288         ( 'ar.customer.account_number' => $bt->remote_account_number ),
289         ( 'ap.vendor.iban'             => $bt->remote_account_number ),
290         ( 'ap.vendor.account_number'   => $bt->remote_account_number ),
291         ( 'gl.storno'                  => '0' ),
292       );
293
294       my $acc_transactions = SL::DB::Manager::AccTransaction->get_all(where => [ @{ $self->{bb_where} },
295                                                                                  amount => -1 * $bt->amount,
296                                                                                  cleared => '0',
297                                                                                  'transdate' => $bt->transdate,
298                                                                                  or => [ @account_number_match ]
299                                                                                ],
300                                                                        with_objects => [ 'ar', 'ap', 'ar.customer', 'ap.vendor', 'gl' ]);
301       if (scalar @{ $acc_transactions } == 1) {
302         push @{ $proposal->{BB} }, @{ $acc_transactions }[0];
303         push @proposals, $proposal;
304       }
305     }
306   }
307
308   $self->{PROPOSALS} = \@proposals;
309 }
310
311 sub _get_elements_and_validate {
312   my ($self) = @_;
313
314   my @errors;
315
316   if ( not defined $::form->{bt_ids} ) {
317     push @errors, t8('No bank account chosen!');
318   }
319
320   if ( not defined $::form->{bb_ids} ) {
321     push @errors, t8('No transaction on chart bank chosen!');
322   }
323
324   if (!@errors) {
325     if (scalar @{ $::form->{bt_ids} } > 1 and scalar @{ $::form->{bb_ids} } > 1) {
326       push @errors, t8('No 1:n or n:1 relation');
327     }
328   }
329
330   my @elements;
331   my ($bt_sum, $bb_sum) = (0,0);
332
333   foreach my $bt_id (@{ $::form->{bt_ids} }) {
334     my $bt = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
335     $bt->{type} = 'BT';
336     $bt_sum += $bt->amount;
337     push @elements, $bt;
338   }
339
340   foreach my $bb_id (@{ $::form->{bb_ids} }) {
341     my $bb = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $bb_id);
342     $bb->{type} = 'BB';
343     $bb->{id} = $bb->acc_trans_id;
344     $bb_sum += $bb->amount;
345     push @elements, $bb;
346   }
347
348   if ($::form->round_amount($bt_sum + $bb_sum, 2) != 0) {
349     push @errors, t8('Out of balance!'), t8('Sum of bank #1 and sum of bookings #2',$bt_sum, $bb_sum);
350   }
351
352   $self->{ELEMENTS} = \@elements;
353   $self->{bt_sum} = $bt_sum;
354   $self->{bb_sum} = $bb_sum;
355
356   return @errors;
357 }
358
359 sub _reconcile {
360   my ($self) = @_;
361
362   # reconcile transaction safe
363   SL::DB->client->with_transaction(sub {
364
365     # 1. step: set AccTrans and BankTransactions to 'cleared'
366     foreach my $element (@{ $self->{ELEMENTS} }) {
367       $element->cleared('1');
368       # veto either invoice_amount is fully assigned or not! No state tricks in later workflow!
369       $element->save;
370     }
371     # 2. step: insert entry in reconciliation_links
372     my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
373     #There is either a 1:n relation or a n:1 relation
374     if (scalar @{ $::form->{bt_ids} } == 1) {
375       my $bt_id = @{ $::form->{bt_ids} }[0];
376       foreach my $bb_id (@{ $::form->{bb_ids} }) {
377         my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
378                                                        acc_trans_id        => $bb_id,
379                                                        rec_group           => $rec_group);
380         $rec_link->save;
381       }
382     } else {
383       my $bb_id = @{ $::form->{bb_ids} }[0];
384       foreach my $bt_id (@{ $::form->{bt_ids} }) {
385         my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
386                                                        acc_trans_id        => $bb_id,
387                                                        rec_group           => $rec_group);
388         $rec_link->save;
389       }
390     }
391
392   1;
393   }) or die t8('Unable to reconcile, database transaction failure');
394 }
395
396 sub _filter_to_where {
397   my ($self) = @_;
398
399   my %parse_filter = parse_filter($::form->{filter});
400   my %filter = @{ $parse_filter{query} };
401
402   my (@rl_where, @bt_where, @bb_where);
403   @rl_where = ('bank_transaction.local_bank_account_id' => $filter{local_bank_account_id});
404   @bt_where = (local_bank_account_id => $filter{local_bank_account_id});
405   @bb_where = (chart_id              => $self->{bank_account}->chart_id);
406
407   if ($filter{fromdate} and $filter{todate}) {
408
409     push @rl_where, (or => [ and => [ 'acc_trans.transdate'        => $filter{fromdate},
410                                       'acc_trans.transdate'        => $filter{todate}   ],
411                              and => [ 'bank_transaction.transdate' => $filter{fromdate},
412                                       'bank_transaction.transdate' => $filter{todate}   ] ] );
413
414     push @bt_where, (transdate => $filter{todate} );
415     push @bt_where, (transdate => $filter{fromdate} );
416     push @bb_where, (transdate => $filter{todate} );
417     push @bb_where, (transdate => $filter{fromdate} );
418   }
419
420   if ( $self->{bank_account}->reconciliation_starting_date ) {
421     push @bt_where, (transdate => { ge => $self->{bank_account}->reconciliation_starting_date });
422     push @bb_where, (transdate => { ge => $self->{bank_account}->reconciliation_starting_date });
423   }
424
425   # don't try to reconcile opening and closing balance transactions
426   push @bb_where, ('acc_trans.ob_transaction' => 0);
427   push @bb_where, ('acc_trans.cb_transaction' => 0);
428
429   if ($filter{fromdate} and not $filter{todate}) {
430     push @rl_where, (or => [ 'acc_trans.transdate'        => $filter{fromdate},
431                              'bank_transaction.transdate' => $filter{fromdate} ] );
432     push @bt_where, (transdate                    => $filter{fromdate} );
433     push @bb_where, (transdate                    => $filter{fromdate} );
434   }
435
436   if ($filter{todate} and not $filter{fromdate}) {
437     push @rl_where, ( or => [ 'acc_trans.transdate'        => $filter{todate} ,
438                               'bank_transaction.transdate' => $filter{todate} ] );
439     push @bt_where, (transdate                    => $filter{todate} );
440     push @bb_where, (transdate                    => $filter{todate} );
441   }
442
443   if ($filter{cleared}) {
444     $filter{cleared} = $filter{cleared} eq 'FALSE' ? '0' : '1';
445     push @rl_where, ('acc_trans.cleared'        => $filter{cleared} );
446
447     push @bt_where, (cleared                    => $filter{cleared} );
448     push @bb_where, (cleared                    => $filter{cleared} );
449   }
450
451   $self->{rl_where} = \@rl_where;
452   $self->{bt_where} = \@bt_where;
453   $self->{bb_where} = \@bb_where;
454 }
455
456 sub _get_linked_transactions {
457   my ($self) = @_;
458
459   $self->_filter_to_where;
460
461   my (@where, @bt_where, @bb_where);
462   # don't try to reconcile opening and closing balances
463   # instead use an offset in configuration
464
465   @where    = (@{ $self->{rl_where} });
466   @bt_where = (@{ $self->{bt_where} }, cleared => '0');
467   @bb_where = (@{ $self->{bb_where} }, cleared => '0');
468
469   my @rows;
470
471   my $reconciliation_groups = SL::DB::Manager::ReconciliationLink->get_all(distinct => 1,
472                                                                            select => ['rec_group'],
473                                                                            where => \@where,
474                                                                            with_objects => ['bank_transaction', 'acc_trans']);
475
476   my $fromdate = $::locale->parse_date_to_object($::form->{filter}->{fromdate_date__ge});
477   my $todate   = $::locale->parse_date_to_object($::form->{filter}->{todate_date__le});
478
479   foreach my $rec_group (@{ $reconciliation_groups }) {
480     my $linked_transactions = SL::DB::Manager::ReconciliationLink->get_all(where => [rec_group => $rec_group->rec_group], with_objects => ['bank_transaction', 'acc_trans']);
481     my $line;
482     my $first_transaction = shift @{ $linked_transactions };
483     my $first_bt = $first_transaction->bank_transaction;
484     my $first_bb = $first_transaction->acc_trans;
485
486     if (defined $fromdate) {
487       $first_bt->{class} = 'out_of_balance' if ( $first_bt->transdate lt $fromdate );
488       $first_bb->{class} = 'out_of_balance' if ( $first_bb->transdate lt $fromdate );
489     }
490     if (defined $todate) {
491       $first_bt->{class} = 'out_of_balance' if ( $first_bt->transdate gt $todate );
492       $first_bb->{class} = 'out_of_balance' if ( $first_bb->transdate gt $todate );
493     }
494     $line->{BT} = [ $first_bt ];
495     $line->{BB} = [ $first_bb ];
496     $line->{rec_group} = $first_transaction->rec_group;
497     $line->{type} = 'Link';
498
499     #add the rest of transaction of this group
500     my ($previous_bt_id, $previous_acc_trans_id) = ($first_transaction->bank_transaction_id, $first_transaction->acc_trans_id);
501     foreach my $linked_transaction (@{ $linked_transactions }) {
502       my $bank_transaction = $linked_transaction->bank_transaction;
503       my $acc_transaction  = $linked_transaction->acc_trans;
504       if (defined $fromdate) {
505         $bank_transaction->{class} = 'out_of_balance' if ( $bank_transaction->transdate lt $fromdate );
506         $acc_transaction->{class}  = 'out_of_balance' if ( $acc_transaction->transdate  lt $fromdate );
507       }
508       if (defined $todate) {
509         $bank_transaction->{class} = 'out_of_balance' if ( $bank_transaction->transdate gt $todate );
510         $acc_transaction->{class}  = 'out_of_balance' if ( $acc_transaction->transdate  gt $todate );
511       }
512       if ($bank_transaction->id != $previous_bt_id) {
513         push @{ $line->{BT} }, $bank_transaction;
514       }
515       if ($acc_transaction->acc_trans_id != $previous_acc_trans_id) {
516         push @{ $line->{BB} }, $acc_transaction;
517       }
518     }
519     push @rows, $line;
520   }
521
522   # add non-cleared bank transactions
523   my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => \@bt_where);
524   foreach my $bt (@{ $bank_transactions }) {
525     my $line;
526     $line->{BT} = [ $bt ];
527     $line->{type} = 'BT';
528     $line->{id} = $bt->id;
529     push @rows, $line;
530   }
531
532   # add non-cleared bookings on bank
533   my $bookings_on_bank = SL::DB::Manager::AccTransaction->get_all(where => \@bb_where);
534   foreach my $bb (@{ $bookings_on_bank }) {
535     if ($::form->{filter}->{show_stornos} or !$bb->record->storno) {
536       my $line;
537       $line->{BB} = [ $bb ];
538       $line->{type} = 'BB';
539       $line->{id} = $bb->acc_trans_id;
540       push @rows, $line;
541     }
542   }
543
544   #sort lines
545   @rows = sort sort_by_transdate @rows;
546
547   $self->{LINKED_TRANSACTIONS} = \@rows;
548 }
549
550 sub sort_by_transdate {
551   if ($a->{BT} and $b->{BT}) {
552     return $a->{BT}[0]->amount <=> $b->{BT}[0]->amount if $a->{BT}[0]->transdate eq $b->{BT}[0]->transdate;
553     return $a->{BT}[0]->transdate cmp $b->{BT}[0]->transdate;
554   }
555   if ($a->{BT}) {
556     return $a->{BT}[0]->amount <=> (-1 * $b->{BB}[0]->amount) if $a->{BT}[0]->transdate eq $b->{BB}[0]->transdate;
557     return $a->{BT}[0]->transdate cmp $b->{BB}[0]->transdate;
558   }
559   if ($b->{BT}) {
560     return (-1 * $a->{BB}[0]->amount) <=> $b->{BT}[0]->amount if $a->{BB}[0]->transdate eq $b->{BT}[0]->transdate;
561     return $a->{BB}[0]->transdate cmp $b->{BT}[0]->transdate;
562   }
563   return (-1 * $a->{BB}[0]->amount) <=> (-1 * $b->{BB}[0]->amount) if $a->{BB}[0]->transdate eq $b->{BB}[0]->transdate;
564   return $a->{BB}[0]->transdate cmp $b->{BB}[0]->transdate;
565 }
566
567 sub _get_balances {
568   my ($self) = @_;
569
570   $self->_filter_to_where;
571
572   my (@bt_where, @bb_where);
573   @bt_where = @{ $self->{bt_where} };
574   @bb_where = @{ $self->{bb_where} };
575
576   my @all_bt_where = (local_bank_account_id => $self->{bank_account}->id);
577   my @all_bb_where = (chart_id              => $self->{bank_account}->chart_id);
578
579   my ($bt_balance, $bb_balance) = (0,0);
580   my ($absolut_bt_balance, $absolut_bb_balance) = (0,0);
581
582   if ( $self->{bank_account}->reconciliation_starting_date ) {
583     $bt_balance         = $self->{bank_account}->reconciliation_starting_balance;
584     $bb_balance         = $self->{bank_account}->reconciliation_starting_balance * -1;
585     $absolut_bt_balance = $self->{bank_account}->reconciliation_starting_balance;
586     $absolut_bb_balance = $self->{bank_account}->reconciliation_starting_balance * -1;
587
588     push @all_bt_where, ( transdate => { gt => $self->{bank_account}->reconciliation_starting_date });
589     push @all_bb_where, ( transdate => { gt => $self->{bank_account}->reconciliation_starting_date });
590   }
591
592   my $bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => \@bt_where );
593   my $payments          = SL::DB::Manager::AccTransaction ->get_all(where => \@bb_where );
594
595   # for absolute balance get all bookings until todate
596   my $todate   = $::locale->parse_date_to_object($::form->{filter}->{todate_date__le});
597   my $fromdate = $::locale->parse_date_to_object($::form->{filter}->{fromdate_date__le});
598
599   if ($todate) {
600     push @all_bt_where, (transdate => { le => $todate });
601     push @all_bb_where, (transdate => { le => $todate });
602   }
603
604   my $all_bank_transactions = SL::DB::Manager::BankTransaction->get_all(where => \@all_bt_where);
605   my $all_payments          = SL::DB::Manager::AccTransaction ->get_all(where => \@all_bb_where);
606
607   $bt_balance += sum map { $_->amount } @{ $bank_transactions };
608   $bb_balance += sum map { $_->amount if ($::form->{filter}->{show_stornos} or !$_->record->storno) } @{ $payments };
609
610   $absolut_bt_balance += sum map { $_->amount } @{ $all_bank_transactions };
611   $absolut_bb_balance += sum map { $_->amount } @{ $all_payments };
612
613
614   $self->{bt_balance}         = $bt_balance || 0;
615   $self->{bb_balance}         = $bb_balance || 0;
616   $self->{absolut_bt_balance} = $absolut_bt_balance || 0;
617   $self->{absolut_bb_balance} = $absolut_bb_balance || 0;
618
619   $self->{difference} = $bt_balance + $bb_balance;
620 }
621
622 sub init_cleared {
623   [ { title => t8("all"),       value => ''           },
624     { title => t8("cleared"),   value => 'TRUE'       },
625     { title => t8("uncleared"), value => 'FALSE'      }, ]
626 }
627
628 sub init_BANK_ACCOUNTS {
629   SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
630 }
631
632 sub setup_search_action_bar {
633   my ($self, %params) = @_;
634
635   for my $bar ($::request->layout->get('actionbar')) {
636     $bar->add(
637       action => [
638         t8('Show'),
639         submit    => [ '#search_form', { action => 'Reconciliation/reconciliation' } ],
640         accesskey => 'enter',
641       ],
642     );
643   }
644 }
645
646 sub setup_reconciliation_action_bar {
647   my ($self, %params) = @_;
648
649   for my $bar ($::request->layout->get('actionbar')) {
650     $bar->add(
651       action => [
652         t8('Filter'),
653         call      => [ 'filter_table' ],
654         accesskey => 'enter',
655       ],
656     );
657   }
658 }
659
660 1;