GnuCash  4.11-137-g155922540d+
gnc-accounting-period.c
Go to the documentation of this file.
1 /*
2  * gnc-accounting-period.c --
3  *
4  * Copyright (c) 2005 David Hampton <hampton@employees.org>
5  * All rights reserved.
6  *
7  * GnuCash is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Library General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * Gnucash is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA gnu@gnu.org
23  */
24 
44 #include <config.h>
45 #include <string.h>
46 #include "gnc-accounting-period.h"
47 #include "gnc-date.h"
48 #include "gnc-prefs.h"
49 #include "qof.h"
50 #include "gnc-ui-util.h"
51 
52 static const QofLogModule log_module = G_LOG_DOMAIN;
53 static time64 gnc_accounting_period_start_time64 (GncAccountingPeriod which,
54  const GDate *fy_end,
55  const GDate *contains);
56 static time64 gnc_accounting_period_end_time64 (GncAccountingPeriod which,
57  const GDate *fy_end,
58  const GDate *contains);
59 
60 static time64
61 lookup_start_date_option (GDate *fy_end)
62 {
63  time64 time;
64  int which;
65 
66  if (gnc_prefs_get_bool (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_CHOICE_ABS))
68  (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_DATE));
69  else
70  {
71  which = gnc_prefs_get_int (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_PERIOD);
72  time = gnc_accounting_period_start_time64 (which, fy_end, NULL);
73  }
74  /* we will need the balance of the last transaction before the start
75  date, so subtract 1 from start date */
76  /* CAS: we don't actually do what this comment says. I think that's
77  because a bug in the engine has been fixed. */
78  return time;
79 }
80 
81 static time64
82 lookup_end_date_option (GDate *fy_end)
83 {
84  time64 time;
85  int which;
86 
87  if (gnc_prefs_get_bool (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_END_CHOICE_ABS))
89  (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_END_DATE));
90  else
91  {
92  which = gnc_prefs_get_int (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_END_PERIOD);
93  time = gnc_accounting_period_end_time64 (which, fy_end, NULL);
94  }
95  if (time == 0)
96  time = -1;
97  return time;
98 }
99 
100 static GDate *
101 get_fy_end (void)
102 {
103  QofBook *book;
104  GDate *date = NULL;
105 
106  book = gnc_get_current_book();
107  qof_instance_get (QOF_INSTANCE (book), "fy-end", &date, NULL);
108  return date;
109 }
110 
111 time64
112 gnc_accounting_period_fiscal_start (void)
113 {
114  time64 t;
115  GDate *fy_end = get_fy_end();
116  t = lookup_start_date_option (fy_end);
117  if (fy_end)
118  g_date_free (fy_end);
119  return t;
120 }
121 
122 time64
123 gnc_accounting_period_fiscal_end (void)
124 {
125  time64 t;
126  GDate *fy_end = get_fy_end();
127 
128  t = lookup_end_date_option (fy_end);
129  if (fy_end)
130  g_date_free (fy_end);
131  return t;
132 }
133 
134 GDate *
136  const GDate *fy_end,
137  const GDate *contains)
138 {
139  GDate *date;
140 
141  if (contains)
142  {
143  date = g_date_new_dmy (g_date_get_day (contains),
144  g_date_get_month (contains),
145  g_date_get_year (contains));
146  }
147  else
148  {
149  date = g_date_new ();
150  gnc_gdate_set_today (date);
151  }
152 
153  switch (which)
154  {
155  default:
156  PINFO ("Undefined relative time constant %d", which);
157  g_date_free (date);
158  return NULL;
159 
160  case GNC_ACCOUNTING_PERIOD_TODAY:
161  /* Already have today's date */
162  break;
163 
164  case GNC_ACCOUNTING_PERIOD_MONTH:
166  break;
167 
168  case GNC_ACCOUNTING_PERIOD_MONTH_PREV:
170  break;
171 
172  case GNC_ACCOUNTING_PERIOD_QUARTER:
174  break;
175 
176  case GNC_ACCOUNTING_PERIOD_QUARTER_PREV:
178  break;
179 
180  case GNC_ACCOUNTING_PERIOD_CYEAR:
182  break;
183 
184  case GNC_ACCOUNTING_PERIOD_CYEAR_PREV:
186  break;
187 
188  case GNC_ACCOUNTING_PERIOD_FYEAR:
189  if (fy_end == NULL)
190  {
191  PINFO ("Request for fisal year value but no fiscal year end value provided.");
192  g_date_free (date);
193  return NULL;
194  }
195  gnc_gdate_set_fiscal_year_start (date, fy_end);
196  break;
197 
198  case GNC_ACCOUNTING_PERIOD_FYEAR_PREV:
199  if (fy_end == NULL)
200  {
201  PINFO ("Request for fisal year value but no fiscal year end value provided.");
202  g_date_free (date);
203  return NULL;
204  }
206  break;
207  }
208  return date;
209 }
210 
211 static time64
212 gnc_accounting_period_start_time64 (GncAccountingPeriod which,
213  const GDate *fy_end,
214  const GDate *contains)
215 {
216  GDate *date;
217  time64 secs;
218 
219  date = gnc_accounting_period_start_gdate (which, fy_end, contains);
220  if (!date)
221  return 0;
222 
223  secs = gnc_time64_get_day_start_gdate (date);
224  g_date_free (date);
225  return secs;
226 }
227 
228 GDate *
230  const GDate *fy_end,
231  const GDate *contains)
232 {
233  GDate *date;
234 
235  if (contains)
236  {
237  date = g_date_new_dmy (g_date_get_day (contains),
238  g_date_get_month (contains),
239  g_date_get_year (contains));
240  }
241  else
242  {
243  date = g_date_new ();
244  gnc_gdate_set_today (date);
245  }
246 
247  switch (which)
248  {
249  default:
250  PINFO ("Undefined relative time constant %d", which);
251  g_date_free (date);
252  return 0;
253 
254  case GNC_ACCOUNTING_PERIOD_TODAY:
255  /* Already have today's date */
256  break;
257 
258  case GNC_ACCOUNTING_PERIOD_MONTH:
260  break;
261 
262  case GNC_ACCOUNTING_PERIOD_MONTH_PREV:
264  break;
265 
266  case GNC_ACCOUNTING_PERIOD_QUARTER:
268  break;
269 
270  case GNC_ACCOUNTING_PERIOD_QUARTER_PREV:
272  break;
273 
274  case GNC_ACCOUNTING_PERIOD_CYEAR:
275  gnc_gdate_set_year_end (date);
276  break;
277 
278  case GNC_ACCOUNTING_PERIOD_CYEAR_PREV:
280  break;
281 
282  case GNC_ACCOUNTING_PERIOD_FYEAR:
283  if (fy_end == NULL)
284  {
285  PINFO ("Request for fisal year value but no fiscal year end value provided.");
286  g_date_free (date);
287  return 0;
288  }
289  gnc_gdate_set_fiscal_year_end (date, fy_end);
290  break;
291 
292  case GNC_ACCOUNTING_PERIOD_FYEAR_PREV:
293  if (fy_end == NULL)
294  {
295  PINFO ("Request for fisal year value but no fiscal year end value provided.");
296  g_date_free (date);
297  return 0;
298  }
299  gnc_gdate_set_prev_fiscal_year_end (date, fy_end);
300  break;
301  }
302 
303  return date;
304 }
305 
306 static time64
307 gnc_accounting_period_end_time64 (GncAccountingPeriod which,
308  const GDate *fy_end,
309  const GDate *contains)
310 {
311  GDate *date;
312  time64 secs;
313 
314  date = gnc_accounting_period_end_gdate (which, fy_end, contains);
315  if (!date)
316  return 0;
317 
318  secs = gnc_time64_get_day_end_gdate (date);
319  g_date_free (date);
320  return secs ;
321 }
322 
void qof_instance_get(const QofInstance *inst, const gchar *first_prop,...)
Wrapper for g_object_get.
Date and Time handling routines.
void gnc_gdate_set_fiscal_year_end(GDate *date, const GDate *year_end)
This function modifies a GDate to set it to the last day of the fiscal year in which it falls...
Definition: gnc-date.cpp:1615
utility functions for the GnuCash UI
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
void gnc_gdate_set_quarter_start(GDate *date)
This function modifies a GDate to set it to the first day of the quarter in which it falls...
Definition: gnc-date.cpp:1520
void gnc_gdate_set_today(GDate *gd)
Set a GDate to the current day.
Definition: gnc-date.cpp:1239
void gnc_gdate_set_prev_month_end(GDate *date)
This function modifies a GDate to set it to the last day of the month prior to the one in which it fa...
Definition: gnc-date.cpp:1510
gint64 gnc_prefs_get_int64(const gchar *group, const gchar *pref_name)
Get an 64 bit integer value from the preferences backend.
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
void gnc_gdate_set_prev_year_end(GDate *date)
This function modifies a GDate to set it to the last day of the year prior to the one in which it fal...
Definition: gnc-date.cpp:1582
void gnc_gdate_set_prev_year_start(GDate *date)
This function modifies a GDate to set it to the first day of the year prior to the one in which it fa...
Definition: gnc-date.cpp:1575
General utilities for dealing with accounting periods.
time64 gnc_time64_get_day_start(time64 time_val)
The gnc_time64_get_day_start() routine will take the given time in seconds and adjust it to the first...
Definition: gnc-date.cpp:1299
GncAccountingPeriod
This specifies a time interval.
void gnc_gdate_set_month_start(GDate *date)
This function modifies a GDate to set it to the first day of the month in which it falls...
Definition: gnc-date.cpp:1465
void gnc_gdate_set_prev_fiscal_year_end(GDate *date, const GDate *year_end)
This function modifies a GDate to set it to the last day of the fiscal year prior to the one in which...
Definition: gnc-date.cpp:1649
Generic api to store and retrieve preferences.
void gnc_gdate_set_fiscal_year_start(GDate *date, const GDate *year_end)
This function modifies a GDate to set it to the first day of the fiscal year in which it falls...
Definition: gnc-date.cpp:1591
GDate * gnc_accounting_period_end_gdate(GncAccountingPeriod which, const GDate *fy_end, const GDate *contains)
This function returns the ending date for an accounting period.
GDate * gnc_accounting_period_start_gdate(GncAccountingPeriod which, const GDate *fy_end, const GDate *contains)
This function returns the starting date for an accounting period.
void gnc_gdate_set_year_end(GDate *date)
This function modifies a GDate to set it to the last day of the year in which it falls.
Definition: gnc-date.cpp:1568
void gnc_gdate_set_month_end(GDate *date)
This function modifies a GDate to set it to the last day of the month in which it falls...
Definition: gnc-date.cpp:1478
time64 gnc_time64_get_day_end_gdate(const GDate *date)
The gnc_time64_get_day_end() routine will take the given time in GLib GDate format and adjust it to t...
Definition: gnc-date.cpp:1443
void gnc_gdate_set_prev_fiscal_year_start(GDate *date, const GDate *year_end)
This function modifies a GDate to set it to the first day of the fiscal year prior to the one in whic...
Definition: gnc-date.cpp:1638
void gnc_gdate_set_quarter_end(GDate *date)
This function modifies a GDate to set it to the last day of the quarter in which it falls...
Definition: gnc-date.cpp:1533
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
gint64 time64
Many systems, including Microsoft Windows and BSD-derived Unixes like Darwin, are retaining the int-3...
Definition: gnc-date.h:93
void gnc_gdate_set_prev_quarter_end(GDate *date)
This function modifies a GDate to set it to the last day of the quarter prior to the one in which it ...
Definition: gnc-date.cpp:1552
time64 gnc_time64_get_day_end(time64 time_val)
The gnc_time64_get_day_end() routine will take the given time in seconds and adjust it to the last se...
Definition: gnc-date.cpp:1319
time64 gnc_time64_get_day_start_gdate(const GDate *date)
The gnc_time64_get_day_start() routine will take the given time in GLib GDate format and adjust it to...
Definition: gnc-date.cpp:1429
void gnc_gdate_set_prev_quarter_start(GDate *date)
This function modifies a GDate to set it to the first day of the quarter prior to the one in which it...
Definition: gnc-date.cpp:1545
void gnc_gdate_set_year_start(GDate *date)
This function modifies a GDate to set it to the first day of the year in which it falls...
Definition: gnc-date.cpp:1561
void gnc_gdate_set_prev_month_start(GDate *date)
This function modifies a GDate to set it to the first day of the month prior to the one in which it f...
Definition: gnc-date.cpp:1496