1 package SL::DB::Helper::AccountingPeriod;
 
   5 use parent qw(Exporter);
 
   7 our @EXPORT = qw(get_balance_starting_date);
 
  11 sub get_balance_starting_date {
 
  12   my ($self,$asofdate) = @_;
 
  14   $asofdate ||= DateTime->today_local;
 
  16   unless ( ref $asofdate eq 'DateTime' ) {
 
  17     $asofdate = $::locale->parse_date_to_object($asofdate);
 
  20   my $dbh = $::form->get_standard_dbh;
 
  22   my $startdate_method = $::instance_conf->get_balance_startdate_method;
 
  24   # We could use the following objects to determine the starting date for
 
  25   # calculating the balance from asofdate (the reference date for the balance):
 
  26   # * start_of_year - 1.1., no deviating fiscal year supported
 
  27   # * closed_to - all transactions since the books were last closed
 
  28   # * last_ob - all transactions since last opening balance transaction (usually 1.1.)
 
  29   # * mindate - all transactions in database
 
  31   my $start_of_year = $asofdate->clone();
 
  32   $start_of_year->set_day(1);
 
  33   $start_of_year->set_month(1);
 
  35   # closedto assumes that we only close the books at the end of a fiscal year,
 
  36   # never during the fiscal year. If this assumption is valid closedto should
 
  37   # also work for deviating fiscal years. But as the trial balance (SuSa)
 
  38   # doesn't yet deal with deviating fiscal years, and it is useful to also close
 
  39   # the books after a month has been exported via DATEV, this method of
 
  40   # determining the starting date isn't recommended and is not the default.
 
  42   my $closedto = $::instance_conf->get_closedto;
 
  44     $closedto = $::locale->parse_date_to_object($closedto);
 
  45     $closedto->subtract(years => 1) while ($asofdate - $closedto)->is_negative;
 
  46     $closedto->add(days => 1);
 
  49   my ($query, $startdate, $last_ob, $mindate);
 
  50   $query = qq|select max(transdate) from acc_trans where ob_transaction is true and transdate <= ?|;
 
  51   ($last_ob) = selectrow_query($::form, $dbh, $query, $::locale->format_date(\%::myconfig, $asofdate));
 
  52   $last_ob = $::locale->parse_date_to_object($last_ob) if $last_ob;
 
  54   $query = qq|select min(transdate) from acc_trans|;
 
  55   ($mindate) = selectrow_query($::form, $dbh, $query);
 
  56   $mindate = $::locale->parse_date_to_object($mindate);
 
  58   # the default method is to use all transactions ($mindate)
 
  60   if ( $startdate_method eq 'closed_to' and $closedto ) {
 
  61     # if no closedto is configured use default
 
  62     return $::locale->format_date(\%::myconfig, $closedto);
 
  64   } elsif ( $startdate_method eq 'start_of_year' ) {
 
  66     return $::locale->format_date(\%::myconfig, $start_of_year);
 
  68   } elsif ( $startdate_method eq 'all_transactions' ) {
 
  70     return $::locale->format_date(\%::myconfig, $mindate);
 
  72   } elsif ( $startdate_method eq 'last_ob_or_all_transactions' and $last_ob ) {
 
  73     # use default if there are no ob transactions
 
  75     return $::locale->format_date(\%::myconfig, $last_ob);
 
  77   } elsif ( $startdate_method eq 'last_ob_or_start_of_year' ) {
 
  80       return $::locale->format_date(\%::myconfig, $last_ob);
 
  82       return $::locale->format_date(\%::myconfig, $start_of_year);
 
  86     # default action, also used for closedto and last_ob_or_all_transactions if
 
  87     # there are no valid dates
 
  89     return $::locale->format_date(\%::myconfig, $mindate);
 
 103 SL::DB::Helper::AccountingPeriod - Helper functions for calculating dates relative to the financial year
 
 109 =item C<get_balance_starting_date $date>
 
 111 Given a date this method calculates and returns the starting date of the
 
 112 financial period relative to that date, according to the configured
 
 113 balance_startdate_method in the client configuration. The returned date is
 
 114 locale-formatted and can be used for SQL queries.
 
 116 If $date isn't a DateTime object a date string is assumed, which then gets
 
 119 If no argument is passed the current day is assumed as default.
 
 129 G. Richardson E<lt>information@kivitendo-premium.deE<gt>