Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / DB / Chart.pm
index ee7ad6a..0697195 100644 (file)
@@ -50,10 +50,12 @@ sub get_balance {
 
   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 $fromdate = $params{fromdate} || $::locale->parse_date_to_object($self->get_balance_starting_date);
+  my $todate   = $params{todate}   || DateTime->today_local;
 
-  my ($balance)  = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id, $startdate, $today);
+  die "get_balance: fromdate and todate arguments must be DateTime Objects" unless ref($fromdate) eq 'DateTime' and ref($todate) eq 'DateTime';
+
+  my ($balance)  = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id, $fromdate, $todate);
 
   return $balance;
 };
@@ -68,7 +70,7 @@ sub formatted_balance_dc {
   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 $balance = $self->get_balance(%params) || 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);
 
@@ -77,20 +79,25 @@ sub formatted_balance_dc {
 
 sub number_of_transactions {
   my ($self) = @_;
-
-  my ($acc_trans) = $self->db->dbh->selectrow_array('select count(acc_trans_id) from acc_trans where chart_id = ?', {}, $self->id);
-
-  return $acc_trans;
+  require SL::DB::AccTransaction;
+  return SL::DB::Manager::AccTransaction->get_all_count( where => [ chart_id => $self->id ] );
 };
 
 sub has_transaction {
   my ($self) = @_;
 
-  my ($id) = $self->db->dbh->selectrow_array('select acc_trans_id from acc_trans where chart_id = ? limit 1', {}, $self->id) || 0;
+  $self->db->dbh->selectrow_array('select exists(select 1 from acc_trans where chart_id = ?)', {}, $self->id);
+}
 
-  $id ? return 1 : return 0;
+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) = @_;
@@ -147,7 +154,7 @@ to the current date if undefined.
 Returns the tax rate of the active tax key as determined by
 C<get_active_taxkey>.
 
-=item C<get_balance>
+=item C<get_balance %PARAMS>
 
 Returns the current balance of the chart (sum of amount in acc_trans, positive
 or negative). The transactions are filtered by transdate, the maximum date is
@@ -156,11 +163,20 @@ the current day, the minimum date is determined by get_balance_starting_date.
 The balance should be same as that in the balance report for that chart, with
 the asofdate as the current day, and the accounting_method "accrual".
 
-=item C<formatted_balance_dc>
+If DateTime objects are passed via the params fromdate and todate, the balance
+is calculated only for that period.
+
+=item C<formatted_balance_dc %PARAMS>
 
 Returns a formatted version of C<get_balance>, taking the absolute value and
 adding the translated abbreviation for debit or credit after the number.
 
+Any params are passed on to C<get_balance>.
+
+=item C<number_of_transactions>
+
+Returns number of transactions that exist for this chart in acc_trans.
+
 =item C<has_transaction>
 
 Returns 1 or 0, depending whether the chart has a transaction in the database
@@ -171,6 +187,12 @@ or not.
 Returns the date of the last transaction of the chart in the database, which
 may lie in the future.
 
+=item C<new_chart_valid>
+
+Checks whether a follow-up chart is configured, and returns 1 or 0 depending on
+whether the valid_from date is before or after the current date.
+Is this even used anywhere?
+
 =back
 
 =head1 BUGS