2005-11-01 21:32:36 -06:00
|
|
|
/*
|
2009-12-29 14:12:48 -06:00
|
|
|
* gnc-accounting-period.c --
|
2005-11-01 21:32:36 -06:00
|
|
|
*
|
|
|
|
* Copyright (c) 2005 David Hampton <hampton@employees.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* GnuCash is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Gnucash is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, contact:
|
|
|
|
*
|
|
|
|
* Free Software Foundation Voice: +1-617-542-5942
|
2005-11-16 23:35:02 -06:00
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org
|
2005-11-01 21:32:36 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** @addtogroup GUI
|
|
|
|
@{ */
|
|
|
|
/** @file gnc-accounting-period.c
|
|
|
|
@brief General utilities for dealing with accounting periods.
|
|
|
|
@author David Hampton <hampton@employees.org>
|
|
|
|
|
|
|
|
These are general utility functions for specifying an accounting
|
|
|
|
period and converting it to a value usable by the gnucash engine.
|
|
|
|
The choice of src/app-utils is arbitrary as these utilities don't
|
|
|
|
fit well anywhere else. They are at a higher level than a GDate,
|
|
|
|
so they don't fit in src/core-utils/gnc-gdate-utils.c. They don't
|
|
|
|
operate on engine data structures, so they don't belong in
|
|
|
|
src/engine/Period.c. Putting them into src/engine/gnc-date.c
|
|
|
|
would be the best place for them, but then that creates a new
|
|
|
|
dependancy from the src/engine directory to the src/core-utils
|
|
|
|
directory that doesn't currently exist. Since that might be a
|
|
|
|
problem for CashUtils, the app-file directory was chosen.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2006-02-10 23:00:02 -06:00
|
|
|
#include <string.h>
|
2005-11-01 21:32:36 -06:00
|
|
|
#include "gnc-accounting-period.h"
|
|
|
|
#include "gnc-gdate-utils.h"
|
2006-02-10 23:00:02 -06:00
|
|
|
#include "gnc-date.h"
|
2013-10-07 09:08:17 -05:00
|
|
|
#include "gnc-prefs.h"
|
2006-02-10 23:00:02 -06:00
|
|
|
#include "qof.h"
|
|
|
|
#include "gnc-ui-util.h"
|
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
static time64 gnc_accounting_period_start_time64 (GncAccountingPeriod which,
|
|
|
|
const GDate *fy_end,
|
|
|
|
const GDate *contains);
|
|
|
|
static time64 gnc_accounting_period_end_time64 (GncAccountingPeriod which,
|
|
|
|
const GDate *fy_end,
|
|
|
|
const GDate *contains);
|
|
|
|
|
|
|
|
static time64
|
2013-10-07 09:16:36 -05:00
|
|
|
lookup_start_date_option(GDate *fy_end)
|
2006-02-10 23:00:02 -06:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
gchar *choice;
|
2012-12-01 16:44:41 -06:00
|
|
|
time64 time;
|
2009-12-29 14:12:48 -06:00
|
|
|
int which;
|
|
|
|
|
2013-10-07 09:16:36 -05:00
|
|
|
|
2013-10-07 09:30:10 -05:00
|
|
|
if (gnc_prefs_get_bool (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_CHOICE_ABS))
|
2009-12-29 14:12:48 -06:00
|
|
|
{
|
2013-10-07 09:30:10 -05:00
|
|
|
GVariant *var = gnc_prefs_get_value(GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_DATE);
|
2013-10-07 09:19:29 -05:00
|
|
|
time = g_variant_get_int64 (var);
|
|
|
|
g_variant_unref (var);
|
2009-12-29 14:12:48 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-07 09:30:10 -05:00
|
|
|
which = gnc_prefs_get_int(GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_PERIOD);
|
2012-12-01 16:44:41 -06:00
|
|
|
time = gnc_accounting_period_start_time64(which, fy_end, NULL);
|
2009-12-29 14:12:48 -06:00
|
|
|
}
|
|
|
|
/* we will need the balance of the last transaction before the start
|
|
|
|
date, so subtract 1 from start date */
|
|
|
|
/* CAS: we don't actually do what this comment says. I think that's
|
|
|
|
because a bug in the engine has been fixed. */
|
|
|
|
return time;
|
2006-02-10 23:00:02 -06:00
|
|
|
}
|
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
static time64
|
2013-10-07 09:16:36 -05:00
|
|
|
lookup_end_date_option(GDate *fy_end)
|
2006-02-10 23:00:02 -06:00
|
|
|
{
|
2012-12-01 16:44:41 -06:00
|
|
|
time64 time;
|
2009-12-29 14:12:48 -06:00
|
|
|
int which;
|
|
|
|
|
2013-10-07 09:30:10 -05:00
|
|
|
if (gnc_prefs_get_bool (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_END_CHOICE_ABS))
|
2009-12-29 14:12:48 -06:00
|
|
|
{
|
2013-10-07 09:30:10 -05:00
|
|
|
GVariant *var = gnc_prefs_get_value(GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_END_DATE);
|
2013-10-07 09:19:29 -05:00
|
|
|
time = g_variant_get_int64 (var);
|
2012-12-01 16:44:41 -06:00
|
|
|
time = gnc_time64_get_day_end(time);
|
2013-10-07 09:19:29 -05:00
|
|
|
g_variant_unref (var);
|
2009-12-29 14:12:48 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-07 09:30:10 -05:00
|
|
|
which = gnc_prefs_get_int(GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_END_PERIOD);
|
2012-12-01 16:44:41 -06:00
|
|
|
time = gnc_accounting_period_end_time64(which, fy_end, NULL);
|
2009-12-29 14:12:48 -06:00
|
|
|
}
|
|
|
|
if (time == 0)
|
|
|
|
time = -1;
|
|
|
|
return time;
|
2006-02-10 23:00:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static GDate *
|
2009-12-29 14:12:48 -06:00
|
|
|
get_fy_end(void)
|
2006-02-10 23:00:02 -06:00
|
|
|
{
|
|
|
|
QofBook *book;
|
|
|
|
KvpFrame *book_frame;
|
|
|
|
gint64 month, day;
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2006-02-10 23:00:02 -06:00
|
|
|
book = gnc_get_current_book();
|
|
|
|
book_frame = qof_book_get_slots(book);
|
|
|
|
month = kvp_frame_get_gint64(book_frame, "/book/fyear_end/month");
|
|
|
|
day = kvp_frame_get_gint64(book_frame, "/book/fyear_end/day");
|
|
|
|
if (g_date_valid_dmy(day, month, 2005 /* not leap year */))
|
|
|
|
return g_date_new_dmy(day, month, G_DATE_BAD_YEAR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
time64
|
2006-02-10 23:00:02 -06:00
|
|
|
gnc_accounting_period_fiscal_start(void)
|
|
|
|
{
|
2012-12-01 16:44:41 -06:00
|
|
|
time64 t;
|
2006-02-10 23:00:02 -06:00
|
|
|
GDate *fy_end = get_fy_end();
|
2013-10-07 09:16:36 -05:00
|
|
|
t = lookup_start_date_option(fy_end);
|
2006-02-10 23:00:02 -06:00
|
|
|
if (fy_end)
|
|
|
|
g_date_free(fy_end);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
time64
|
2006-02-10 23:00:02 -06:00
|
|
|
gnc_accounting_period_fiscal_end(void)
|
|
|
|
{
|
2012-12-01 16:44:41 -06:00
|
|
|
time64 t;
|
2006-02-10 23:00:02 -06:00
|
|
|
GDate *fy_end = get_fy_end();
|
2009-12-29 14:12:48 -06:00
|
|
|
|
2013-10-07 09:16:36 -05:00
|
|
|
t = lookup_end_date_option(fy_end);
|
2006-02-10 23:00:02 -06:00
|
|
|
if (fy_end)
|
|
|
|
g_date_free(fy_end);
|
|
|
|
return t;
|
|
|
|
}
|
2005-11-01 21:32:36 -06:00
|
|
|
|
|
|
|
GDate *
|
|
|
|
gnc_accounting_period_start_gdate (GncAccountingPeriod which,
|
2009-12-29 14:12:48 -06:00
|
|
|
const GDate *fy_end,
|
|
|
|
const GDate *contains)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
GDate *date;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (contains)
|
|
|
|
{
|
|
|
|
date = g_date_new_dmy(g_date_get_day(contains),
|
|
|
|
g_date_get_month(contains),
|
|
|
|
g_date_get_year(contains));
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
2009-12-29 14:12:48 -06:00
|
|
|
else
|
|
|
|
{
|
2012-12-01 16:44:41 -06:00
|
|
|
date = g_date_new ();
|
|
|
|
gnc_gdate_set_today (date);
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
2009-12-29 14:12:48 -06:00
|
|
|
|
|
|
|
switch (which)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
g_message("Undefined relative time constant %d", which);
|
|
|
|
g_date_free(date);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_TODAY:
|
|
|
|
/* Already have today's date */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_MONTH:
|
|
|
|
gnc_gdate_set_month_start(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_MONTH_PREV:
|
|
|
|
gnc_gdate_set_prev_month_start(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_QUARTER:
|
|
|
|
gnc_gdate_set_quarter_start(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_QUARTER_PREV:
|
|
|
|
gnc_gdate_set_prev_quarter_start(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_CYEAR:
|
|
|
|
gnc_gdate_set_year_start(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_CYEAR_PREV:
|
|
|
|
gnc_gdate_set_prev_year_start(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_FYEAR:
|
|
|
|
if (fy_end == NULL)
|
|
|
|
{
|
|
|
|
g_message("Request for fisal year value but no fiscal year end value provided.");
|
|
|
|
g_date_free(date);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
gnc_gdate_set_fiscal_year_start(date, fy_end);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_FYEAR_PREV:
|
|
|
|
if (fy_end == NULL)
|
|
|
|
{
|
|
|
|
g_message("Request for fisal year value but no fiscal year end value provided.");
|
|
|
|
g_date_free(date);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
gnc_gdate_set_prev_fiscal_year_start(date, fy_end);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return date;
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
static time64
|
|
|
|
gnc_accounting_period_start_time64 (GncAccountingPeriod which,
|
2009-12-29 14:12:48 -06:00
|
|
|
const GDate *fy_end,
|
|
|
|
const GDate *contains)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
GDate *date;
|
2012-12-01 16:44:41 -06:00
|
|
|
time64 secs;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
date = gnc_accounting_period_start_gdate(which, fy_end, contains);
|
|
|
|
if (!date)
|
|
|
|
return 0;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
secs = gnc_time64_get_day_start_gdate(date);
|
2009-12-29 14:12:48 -06:00
|
|
|
g_date_free(date);
|
|
|
|
return secs;
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
GDate *
|
|
|
|
gnc_accounting_period_end_gdate (GncAccountingPeriod which,
|
2009-12-29 14:12:48 -06:00
|
|
|
const GDate *fy_end,
|
|
|
|
const GDate *contains)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
GDate *date;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
if (contains)
|
|
|
|
{
|
|
|
|
date = g_date_new_dmy(g_date_get_day(contains),
|
|
|
|
g_date_get_month(contains),
|
|
|
|
g_date_get_year(contains));
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
2009-12-29 14:12:48 -06:00
|
|
|
else
|
|
|
|
{
|
2012-12-01 16:44:41 -06:00
|
|
|
date = g_date_new ();
|
|
|
|
gnc_gdate_set_today (date);
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
switch (which)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
g_message("Undefined relative time constant %d", which);
|
|
|
|
g_date_free(date);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_TODAY:
|
|
|
|
/* Already have today's date */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_MONTH:
|
|
|
|
gnc_gdate_set_month_end(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_MONTH_PREV:
|
|
|
|
gnc_gdate_set_prev_month_end(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_QUARTER:
|
|
|
|
gnc_gdate_set_quarter_end(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_QUARTER_PREV:
|
|
|
|
gnc_gdate_set_prev_quarter_end(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_CYEAR:
|
|
|
|
gnc_gdate_set_year_end(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_CYEAR_PREV:
|
|
|
|
gnc_gdate_set_prev_year_end(date);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_FYEAR:
|
|
|
|
if (fy_end == NULL)
|
|
|
|
{
|
|
|
|
g_message("Request for fisal year value but no fiscal year end value provided.");
|
|
|
|
g_date_free(date);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
gnc_gdate_set_fiscal_year_end(date, fy_end);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GNC_ACCOUNTING_PERIOD_FYEAR_PREV:
|
|
|
|
if (fy_end == NULL)
|
|
|
|
{
|
|
|
|
g_message("Request for fisal year value but no fiscal year end value provided.");
|
|
|
|
g_date_free(date);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
gnc_gdate_set_prev_fiscal_year_end(date, fy_end);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return date;
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
static time64
|
|
|
|
gnc_accounting_period_end_time64 (GncAccountingPeriod which,
|
2009-12-29 14:12:48 -06:00
|
|
|
const GDate *fy_end,
|
|
|
|
const GDate *contains)
|
2005-11-01 21:32:36 -06:00
|
|
|
{
|
2009-12-29 14:12:48 -06:00
|
|
|
GDate *date;
|
2012-12-01 16:44:41 -06:00
|
|
|
time64 secs;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2009-12-29 14:12:48 -06:00
|
|
|
date = gnc_accounting_period_end_gdate(which, fy_end, contains);
|
|
|
|
if (!date)
|
|
|
|
return 0;
|
2005-11-01 21:32:36 -06:00
|
|
|
|
2012-12-01 16:44:41 -06:00
|
|
|
secs = gnc_time64_get_day_end_gdate(date);
|
2009-12-29 14:12:48 -06:00
|
|
|
g_date_free(date);
|
|
|
|
return secs ;
|
2005-11-01 21:32:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|