+ return $cache->{$self->id} = SL::DB::Manager::TaxKey->get_all(
+ query => [ and => [ chart_id => $self->id,
+ startdate => { le => $date } ] ],
+ sort_by => "startdate DESC")->[0];
+}
+
+sub get_active_taxrate {
+ my ($self, $date) = @_;
+ $date ||= DateTime->today_local;
+ require SL::DB::Tax;
+ my $tax = SL::DB::Manager::Tax->find_by( id => $self->get_active_taxkey->tax_id );
+ return $tax->rate;
+}
+
+
+sub get_balance {
+ my ($self, %params) = @_;
+
+ return undef unless $self->id;
+
+ # return empty string if user doesn't have rights
+ return "" unless ($main::auth->assert('general_ledger', 1));
+
+ my $query = qq|SELECT SUM(amount) AS sum FROM acc_trans WHERE chart_id = ? AND transdate >= ? and transdate <= ?|;
+
+ my $startdate = $self->get_balance_starting_date;
+ my $today = DateTime->today_local;
+
+ my ($balance) = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id, $startdate, $today);
+
+ return $balance;
+};
+
+sub formatted_balance_dc {
+ my ($self, %params) = @_;
+
+ # return empty string if user doesn't have rights
+ return "" unless ($main::auth->assert('general_ledger', 1));
+
+ # return empty string if chart has never been booked
+ return "" unless $self->has_transaction;
+
+ # return abs of current balance with the abbreviation for debit or credit behind it
+ my $balance = $self->get_balance || 0;
+ my $dc_abbreviation = $balance > 0 ? t8("Credit (one letter abbreviation)") : t8("Debit (one letter abbreviation)");
+ my $amount = $::form->format_amount(\%::myconfig, abs($balance), 2);
+
+ return "$amount $dc_abbreviation";
+};
+
+sub number_of_transactions {
+ my ($self) = @_;
+ require SL::DB::AccTransaction;
+ return SL::DB::Manager::AccTransaction->get_all_count( where => [ chart_id => $self->id ] );
+};
+
+sub has_transaction {
+ my ($self) = @_;
+
+ $self->db->dbh->selectrow_array('select exists(select 1 from acc_trans where chart_id = ?)', {}, $self->id);
+}
+
+sub new_chart_valid {
+ my ($self) = @_;
+
+ if ( $self->valid_from && DateTime->today >= $self->valid_from ) {
+ return 1;
+ } else {
+ return 0;
+ };
+}
+
+sub displayable_name {
+ my ($self) = @_;
+
+ return join ' ', grep $_, $self->accno, $self->description;
+}
+
+sub displayable_category {
+ my ($self) = @_;
+
+ return t8("Account Category E") if $self->category eq "E";
+ return t8("Account Category I") if $self->category eq "I";
+ return t8("Account Category A") if $self->category eq "A";
+ return t8("Account Category L") if $self->category eq "L";
+ return t8("Account Category Q") if $self->category eq "Q";
+ return t8("Account Category C") if $self->category eq "C";
+ return '';
+}
+
+sub date_of_last_transaction {
+ my ($self) = @_;
+
+ die unless $self->id;
+
+ return '' unless $self->has_transaction;
+
+ my ($transdate) = $self->db->dbh->selectrow_array('select max(transdate) from acc_trans where chart_id = ?', {}, $self->id);
+ return DateTime->from_lxoffice($transdate);