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 $fromdate = $params{fromdate} || $::locale->parse_date_to_object($self->get_balance_starting_date);
 
  54   my $todate   = $params{todate}   || DateTime->today_local;
 
  56   die "get_balance: fromdate and todate arguments must be DateTime Objects" unless ref($fromdate) eq 'DateTime' and ref($todate) eq 'DateTime';
 
  58   my ($balance)  = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id, $fromdate, $todate);
 
  63 sub formatted_balance_dc {
 
  64   my ($self, %params) = @_;
 
  66   # return empty string if user doesn't have rights
 
  67   return "" unless ($main::auth->assert('general_ledger', 1));
 
  69   # return empty string if chart has never been booked
 
  70   return "" unless $self->has_transaction;
 
  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);
 
  77   return "$amount $dc_abbreviation";
 
  80 sub number_of_transactions {
 
  82   require SL::DB::AccTransaction;
 
  83   return SL::DB::Manager::AccTransaction->get_all_count( where => [ chart_id => $self->id ] );
 
  89   $self->db->dbh->selectrow_array('select exists(select 1 from acc_trans where chart_id = ?)', {}, $self->id);
 
  95   if ( $self->valid_from && DateTime->today >= $self->valid_from ) {
 
 102 sub displayable_name {
 
 105   return join ' ', grep $_, $self->accno, $self->description;
 
 108 sub displayable_category {
 
 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";
 
 120 sub date_of_last_transaction {
 
 123   die unless $self->id;
 
 125   return '' unless $self->has_transaction;
 
 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);
 
 141 SL::DB::Chart - Rose database model for the "chart" table
 
 147 =item C<get_active_taxkey $date>
 
 149 Returns the active tax key object for a given date. C<$date> defaults
 
 150 to the current date if undefined.
 
 152 =item C<get_active_taxrate $date>
 
 154 Returns the tax rate of the active tax key as determined by
 
 155 C<get_active_taxkey>.
 
 157 =item C<get_balance %PARAMS>
 
 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.
 
 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".
 
 166 If DateTime objects are passed via the params fromdate and todate, the balance
 
 167 is calculated only for that period.
 
 169 =item C<formatted_balance_dc %PARAMS>
 
 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.
 
 174 Any params are passed on to C<get_balance>.
 
 176 =item C<number_of_transactions>
 
 178 Returns number of transactions that exist for this chart in acc_trans.
 
 180 =item C<has_transaction>
 
 182 Returns 1 or 0, depending whether the chart has a transaction in the database
 
 185 =item C<date_of_last_transaction>
 
 187 Returns the date of the last transaction of the chart in the database, which
 
 188 may lie in the future.
 
 190 =item C<new_chart_valid>
 
 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?
 
 204 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
 
 206 G. Richardson E<lt>information@kivitendo-premium.deE<gt>