5 use SL::DB::MetaSetup::Chart;
6 use SL::DB::Manager::Chart;
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);
12 __PACKAGE__->meta->add_relationships(taxkeys => { type => 'one to many',
13 class => 'SL::DB::TaxKey',
14 column_map => { id => 'chart_id' },
18 __PACKAGE__->meta->initialize;
20 sub get_active_taxkey {
21 my ($self, $date) = @_;
22 $date ||= DateTime->today_local;
24 my $cache = $::request->cache("get_active_taxkey")->{$date} //= {};
25 return $cache->{$self->id} if $cache->{$self->id};
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];
34 sub get_active_taxrate {
35 my ($self, $date) = @_;
36 $date ||= DateTime->today_local;
38 my $tax = SL::DB::Manager::Tax->find_by( id => $self->get_active_taxkey->tax_id );
44 my ($self, %params) = @_;
46 return undef unless $self->id;
48 # return empty string if user doesn't have rights
49 return "" unless ($main::auth->assert('general_ledger', 1));
51 my $query = qq|SELECT SUM(amount) AS sum FROM acc_trans WHERE chart_id = ? AND transdate >= ? and transdate <= ?|;
53 my $startdate = $self->get_balance_starting_date;
54 my $today = DateTime->today_local;
56 my ($balance) = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id, $startdate, $today);
61 sub formatted_balance_dc {
62 my ($self, %params) = @_;
64 # return empty string if user doesn't have rights
65 return "" unless ($main::auth->assert('general_ledger', 1));
67 # return empty string if chart has never been booked
68 return "" unless $self->has_transaction;
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);
75 return "$amount $dc_abbreviation";
78 sub number_of_transactions {
81 my ($acc_trans) = $self->db->dbh->selectrow_array('select count(acc_trans_id) from acc_trans where chart_id = ?', {}, $self->id);
89 my ($id) = $self->db->dbh->selectrow_array('select acc_trans_id from acc_trans where chart_id = ? limit 1', {}, $self->id) || 0;
91 $id ? return 1 : return 0;
95 sub displayable_name {
98 return join ' ', grep $_, $self->accno, $self->description;
101 sub displayable_category {
104 return t8("Account Category E") if $self->category eq "E";
105 return t8("Account Category I") if $self->category eq "I";
106 return t8("Account Category A") if $self->category eq "A";
107 return t8("Account Category L") if $self->category eq "L";
108 return t8("Account Category Q") if $self->category eq "Q";
109 return t8("Account Category C") if $self->category eq "C";
113 sub date_of_last_transaction {
116 die unless $self->id;
118 return '' unless $self->has_transaction;
120 my ($transdate) = $self->db->dbh->selectrow_array('select max(transdate) from acc_trans where chart_id = ?', {}, $self->id);
121 return DateTime->from_lxoffice($transdate);
134 SL::DB::Chart - Rose database model for the "chart" table
140 =item C<get_active_taxkey $date>
142 Returns the active tax key object for a given date. C<$date> defaults
143 to the current date if undefined.
145 =item C<get_active_taxrate $date>
147 Returns the tax rate of the active tax key as determined by
148 C<get_active_taxkey>.
152 Returns the current balance of the chart (sum of amount in acc_trans, positive
153 or negative). The transactions are filtered by transdate, the maximum date is
154 the current day, the minimum date is determined by get_balance_starting_date.
156 The balance should be same as that in the balance report for that chart, with
157 the asofdate as the current day, and the accounting_method "accrual".
159 =item C<formatted_balance_dc>
161 Returns a formatted version of C<get_balance>, taking the absolute value and
162 adding the translated abbreviation for debit or credit after the number.
164 =item C<has_transaction>
166 Returns 1 or 0, depending whether the chart has a transaction in the database
169 =item C<date_of_last_transaction>
171 Returns the date of the last transaction of the chart in the database, which
172 may lie in the future.
182 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
184 G. Richardson E<lt>information@kivitendo-premium.deE<gt>