Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[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 $fromdate = $params{fromdate} || $::locale->parse_date_to_object($self->get_balance_starting_date);
54   my $todate   = $params{todate}   || DateTime->today_local;
55
56   die "get_balance: fromdate and todate arguments must be DateTime Objects" unless ref($fromdate) eq 'DateTime' and ref($todate) eq 'DateTime';
57
58   my ($balance)  = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id, $fromdate, $todate);
59
60   return $balance;
61 };
62
63 sub formatted_balance_dc {
64   my ($self, %params) = @_;
65
66   # return empty string if user doesn't have rights
67   return "" unless ($main::auth->assert('general_ledger', 1));
68
69   # return empty string if chart has never been booked
70   return "" unless $self->has_transaction;
71
72   # return abs of current balance with the abbreviation for debit or credit behind it
73   my $balance = $self->get_balance(%params) || 0;
74   my $dc_abbreviation = $balance > 0 ? t8("Credit (one letter abbreviation)") : t8("Debit (one letter abbreviation)");
75   my $amount = $::form->format_amount(\%::myconfig, abs($balance), 2);
76
77   return "$amount $dc_abbreviation";
78 };
79
80 sub number_of_transactions {
81   my ($self) = @_;
82   require SL::DB::AccTransaction;
83   return SL::DB::Manager::AccTransaction->get_all_count( where => [ chart_id => $self->id ] );
84 };
85
86 sub has_transaction {
87   my ($self) = @_;
88
89   $self->db->dbh->selectrow_array('select exists(select 1 from acc_trans where chart_id = ?)', {}, $self->id);
90 }
91
92 sub new_chart_valid {
93   my ($self) = @_;
94
95   if ( $self->valid_from && DateTime->today >= $self->valid_from ) {
96     return 1;
97   } else {
98     return 0;
99   };
100 }
101
102 sub displayable_name {
103   my ($self) = @_;
104
105   return join ' ', grep $_, $self->accno, $self->description;
106 }
107
108 sub displayable_category {
109   my ($self) = @_;
110
111   return t8("Account Category E") if $self->category eq "E";
112   return t8("Account Category I") if $self->category eq "I";
113   return t8("Account Category A") if $self->category eq "A";
114   return t8("Account Category L") if $self->category eq "L";
115   return t8("Account Category Q") if $self->category eq "Q";
116   return t8("Account Category C") if $self->category eq "C";
117   return '';
118 }
119
120 sub date_of_last_transaction {
121   my ($self) = @_;
122
123   die unless $self->id;
124
125   return '' unless $self->has_transaction;
126
127   my ($transdate) = $self->db->dbh->selectrow_array('select max(transdate) from acc_trans where chart_id = ?', {}, $self->id);
128   return DateTime->from_lxoffice($transdate);
129 }
130
131 1;
132
133 __END__
134
135 =pod
136
137 =encoding utf8
138
139 =head1 NAME
140
141 SL::DB::Chart - Rose database model for the "chart" table
142
143 =head1 FUNCTIONS
144
145 =over 4
146
147 =item C<get_active_taxkey $date>
148
149 Returns the active tax key object for a given date. C<$date> defaults
150 to the current date if undefined.
151
152 =item C<get_active_taxrate $date>
153
154 Returns the tax rate of the active tax key as determined by
155 C<get_active_taxkey>.
156
157 =item C<get_balance %PARAMS>
158
159 Returns the current balance of the chart (sum of amount in acc_trans, positive
160 or negative). The transactions are filtered by transdate, the maximum date is
161 the current day, the minimum date is determined by get_balance_starting_date.
162
163 The balance should be same as that in the balance report for that chart, with
164 the asofdate as the current day, and the accounting_method "accrual".
165
166 If DateTime objects are passed via the params fromdate and todate, the balance
167 is calculated only for that period.
168
169 =item C<formatted_balance_dc %PARAMS>
170
171 Returns a formatted version of C<get_balance>, taking the absolute value and
172 adding the translated abbreviation for debit or credit after the number.
173
174 Any params are passed on to C<get_balance>.
175
176 =item C<number_of_transactions>
177
178 Returns number of transactions that exist for this chart in acc_trans.
179
180 =item C<has_transaction>
181
182 Returns 1 or 0, depending whether the chart has a transaction in the database
183 or not.
184
185 =item C<date_of_last_transaction>
186
187 Returns the date of the last transaction of the chart in the database, which
188 may lie in the future.
189
190 =item C<new_chart_valid>
191
192 Checks whether a follow-up chart is configured, and returns 1 or 0 depending on
193 whether the valid_from date is before or after the current date.
194 Is this even used anywhere?
195
196 =back
197
198 =head1 BUGS
199
200 Nothing here yet.
201
202 =head1 AUTHOR
203
204 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
205
206 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
207
208 =cut