SL::DB::Chart - Refactoring der Transaktionsmethoden
[kivitendo-erp.git] / SL / DB / Chart.pm
1 package SL::DB::Chart;
2
3 use strict;
4
5 use SL::DB::MetaSetup::Chart;
6 use SL::DB::Manager::Chart;
7 use SL::DBUtils;
8 use Rose::DB::Object::Helpers qw(as_tree);
9 use SL::DB::Helper::AccountingPeriod qw(get_balance_starting_date);
10 use SL::Locale::String qw(t8);
11
12 __PACKAGE__->meta->add_relationships(taxkeys => { type         => 'one to many',
13                                                   class        => 'SL::DB::TaxKey',
14                                                   column_map   => { id => 'chart_id' },
15                                                 },
16                                     );
17
18 __PACKAGE__->meta->initialize;
19
20 sub get_active_taxkey {
21   my ($self, $date) = @_;
22   $date ||= DateTime->today_local;
23
24   my $cache = $::request->cache("get_active_taxkey")->{$date} //= {};
25   return $cache->{$self->id} if $cache->{$self->id};
26
27   require SL::DB::TaxKey;
28   return $cache->{$self->id} = SL::DB::Manager::TaxKey->get_all(
29     query   => [ and => [ chart_id  => $self->id,
30                           startdate => { le => $date } ] ],
31     sort_by => "startdate DESC")->[0];
32 }
33
34 sub get_active_taxrate {
35   my ($self, $date) = @_;
36   $date ||= DateTime->today_local;
37   require SL::DB::Tax;
38   my $tax = SL::DB::Manager::Tax->find_by( id => $self->get_active_taxkey->tax_id );
39   return $tax->rate;
40 }
41
42
43 sub get_balance {
44   my ($self, %params) = @_;
45
46   return undef unless $self->id;
47
48   # return empty string if user doesn't have rights
49   return "" unless ($main::auth->assert('general_ledger', 1));
50
51   my $query = qq|SELECT SUM(amount) AS sum FROM acc_trans WHERE chart_id = ? AND transdate >= ? and transdate <= ?|;
52
53   my $startdate = $self->get_balance_starting_date;
54   my $today     = DateTime->today_local;
55
56   my ($balance)  = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id, $startdate, $today);
57
58   return $balance;
59 };
60
61 sub formatted_balance_dc {
62   my ($self, %params) = @_;
63
64   # return empty string if user doesn't have rights
65   return "" unless ($main::auth->assert('general_ledger', 1));
66
67   # return empty string if chart has never been booked
68   return "" unless $self->has_transaction;
69
70   # return abs of current balance with the abbreviation for debit or credit behind it
71   my $balance = $self->get_balance || 0;
72   my $dc_abbreviation = $balance > 0 ? t8("Credit (one letter abbreviation)") : t8("Debit (one letter abbreviation)");
73   my $amount = $::form->format_amount(\%::myconfig, abs($balance), 2);
74
75   return "$amount $dc_abbreviation";
76 };
77
78 sub number_of_transactions {
79   my ($self) = @_;
80   require SL::DB::AccTransaction;
81   return SL::DB::Manager::AccTransaction->get_all_count( where => [ chart_id => $self->id ] );
82 };
83
84 sub has_transaction {
85   my ($self) = @_;
86
87   $self->db->dbh->selectrow_array('select exists(select 1 from acc_trans where chart_id = ?)', {}, $self->id);
88 }
89
90 sub displayable_name {
91   my ($self) = @_;
92
93   return join ' ', grep $_, $self->accno, $self->description;
94 }
95
96 sub displayable_category {
97   my ($self) = @_;
98
99   return t8("Account Category E") if $self->category eq "E";
100   return t8("Account Category I") if $self->category eq "I";
101   return t8("Account Category A") if $self->category eq "A";
102   return t8("Account Category L") if $self->category eq "L";
103   return t8("Account Category Q") if $self->category eq "Q";
104   return t8("Account Category C") if $self->category eq "C";
105   return '';
106 }
107
108 sub date_of_last_transaction {
109   my ($self) = @_;
110
111   die unless $self->id;
112
113   return '' unless $self->has_transaction;
114
115   my ($transdate) = $self->db->dbh->selectrow_array('select max(transdate) from acc_trans where chart_id = ?', {}, $self->id);
116   return DateTime->from_lxoffice($transdate);
117 }
118
119 1;
120
121 __END__
122
123 =pod
124
125 =encoding utf8
126
127 =head1 NAME
128
129 SL::DB::Chart - Rose database model for the "chart" table
130
131 =head1 FUNCTIONS
132
133 =over 4
134
135 =item C<get_active_taxkey $date>
136
137 Returns the active tax key object for a given date. C<$date> defaults
138 to the current date if undefined.
139
140 =item C<get_active_taxrate $date>
141
142 Returns the tax rate of the active tax key as determined by
143 C<get_active_taxkey>.
144
145 =item C<get_balance>
146
147 Returns the current balance of the chart (sum of amount in acc_trans, positive
148 or negative). The transactions are filtered by transdate, the maximum date is
149 the current day, the minimum date is determined by get_balance_starting_date.
150
151 The balance should be same as that in the balance report for that chart, with
152 the asofdate as the current day, and the accounting_method "accrual".
153
154 =item C<formatted_balance_dc>
155
156 Returns a formatted version of C<get_balance>, taking the absolute value and
157 adding the translated abbreviation for debit or credit after the number.
158
159 =item C<number_of_transactions>
160
161 Returns number of transactions that exist for this chart in acc_trans.
162
163 =item C<has_transaction>
164
165 Returns 1 or 0, depending whether the chart has a transaction in the database
166 or not.
167
168 =item C<date_of_last_transaction>
169
170 Returns the date of the last transaction of the chart in the database, which
171 may lie in the future.
172
173 =back
174
175 =head1 BUGS
176
177 Nothing here yet.
178
179 =head1 AUTHOR
180
181 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
182
183 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
184
185 =cut