6c58061470d5794ea81260bcac5c4e45fd56023d
[kivitendo-erp.git] / SL / Controller / AccTrans.pm
1 package SL::Controller::AccTrans;
2 use strict;
3 use parent qw(SL::Controller::Base);
4 use SL::DB::AccTransaction;
5
6 __PACKAGE__->run_before('check_auth');
7
8 sub action_list_transactions {
9   my ($self) = @_;
10
11   return $self->render('generic/error', { layout => 0 }, label_error => "list_transactions needs a trans_id") unless $::form->{trans_id};
12
13   my $transactions = SL::DB::Manager::AccTransaction->get_all(query => [ trans_id => $::form->{trans_id} ], sort_by => 'acc_trans_id ASC');
14
15   return $self->render(\'', { type => 'json' }) unless scalar @{$transactions};
16
17   my $acc_trans_table = $self->_mini_ledger($transactions);
18   my $balances_table  = $self->_mini_trial_balance($transactions);
19
20   return $self->render('acc_trans/acc_trans', { header => 0 }, acc_trans_table => $acc_trans_table, balances_table => $balances_table);
21 }
22
23 sub _mini_ledger {
24   my ($self, $transactions) = @_;
25
26   $::auth->assert('general_ledger');
27
28   my $debit_sum  = 0;
29   my $credit_sum = 0;
30
31   foreach my $t ( @{ $transactions } ) {
32     $debit_sum  += $t->amount if $t->amount < 0;
33     $credit_sum += $t->amount if $t->amount > 0;
34   };
35
36   return $self->render('acc_trans/_mini_ledger', { output => 0 }, TRANSACTIONS => $transactions, debit_sum => $debit_sum, credit_sum => $credit_sum, title => $::locale->text('Transactions') );
37 }
38
39 sub _mini_trial_balance {
40   my ($self, $transactions) = @_;
41
42   $::auth->assert('general_ledger');
43
44   my $rec = {};
45   foreach my $t ( @{ $transactions } ) {
46     $rec->{$t->chart->accno}->{description} = $t->chart->description;
47     $rec->{$t->chart->accno}->{accno}       = $t->chart->accno;
48     $rec->{$t->chart->accno}->{balance}    += $t->amount;
49   };
50
51   my @balances;
52   foreach ( sort keys %{ $rec } ) {
53     push @balances, $rec->{$_} if $rec->{$_}->{balance} != 0;
54   };
55
56   return $self->render('acc_trans/_mini_trial_balance', { output => 0 }, BALANCES => \@balances, title => $::locale->text('Balances') );
57 }
58
59 sub check_auth {
60   $::auth->assert('general_ledger');
61 }
62
63 1;
64
65 __END__
66
67 =pod
68
69 =encoding utf8
70
71 =head1 NAME
72
73 SL::Controller::AccTrans - module to list all transactions and balances of an invoice
74
75 =head1 SYNOPSIS
76
77   list_transactions takes an id of an invoice and displays all the transactions in two HTML tables:
78
79   * mini_ledger: list of all transactions of the invoice,  showing date, chart info and the amount as debit or credit, like a small general ledger just for this invoice.
80
81   * mini_trial_balance: list of all charts from the transactions with their current sum, shown as debit or credit.
82
83   The two tables are returned as an HTML div blob.
84
85   # sample code for console:
86   use SL::Controller::AccTrans;
87   # get an invoice (ar/ap/is/ir)
88   my $invoice = SL::DB::Manager::Invoice->find_by( invnumber => 1 );
89   # the function is called from an opened invoice and needs the trans_id as a parameter
90   $::form->{trans_id} = $invoice->id;
91   SL::Controller::AccTrans->action_list_transactions();
92
93   The HTML blob can also be opened directly as a url:
94   controller.pl?action=AccTrans/print_table&trans_id=7
95
96 =head1 TODO
97
98 =head1 BUGS
99
100 Nothing here yet.
101
102 =head1 AUTHOR
103
104 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
105
106 =cut