GnuCash c935c2f+
Loading...
Searching...
No Matches
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>
47#include "gnc-date.h"
48#include "gnc-prefs.h"
49#include "qof.h"
50#include "gnc-session.h"
51
52static const QofLogModule log_module = G_LOG_DOMAIN;
53static time64 gnc_accounting_period_start_time64 (GncAccountingPeriod which,
54 const GDate *fy_end,
55 const GDate *contains);
56static time64 gnc_accounting_period_end_time64 (GncAccountingPeriod which,
57 const GDate *fy_end,
58 const GDate *contains);
59
60static time64
61lookup_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
81static time64
82lookup_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
100static GDate *
101get_fy_end (void)
102{
103 QofBook *book;
104 GDate *date = NULL;
105
106 book = qof_session_get_book(gnc_get_current_session());
107 qof_instance_get (QOF_INSTANCE (book), "fy-end", &date, NULL);
108 return date;
109}
110
111time64
112gnc_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
122time64
123gnc_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
134GDate *
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 fiscal 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 fiscal 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
211static time64
212gnc_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
228GDate *
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:
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 }
300 break;
301 }
302
303 return date;
304}
305
306static time64
307gnc_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
General utilities for dealing with accounting periods.
Date and Time handling routines.
Generic api to store and retrieve preferences.
QofBook * qof_session_get_book(const QofSession *session)
Returns the QofBook of this session.
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...
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.
void gnc_gdate_set_prev_month_end(GDate *date)
Convert a GDate to the last day of the prebvious month.
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.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition gnc-date.h:87
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...
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.
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...
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...
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.
void gnc_gdate_set_fiscal_year_start(GDate *date, const GDate *fy_end)
This function modifies a GDate to set it to the first day of the fiscal year in which it falls.
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 ...
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.
void gnc_gdate_set_prev_fiscal_year_start(GDate *date, const GDate *fy_end)
This function modifies a GDate to set it to the first day of the fiscal year prior to the one in whic...
void gnc_gdate_set_today(GDate *gd)
Set a GDate to the current day.
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...
void gnc_gdate_set_month_end(GDate *date)
Convert a GDate to the last day of the month.
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...
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...
void gnc_gdate_set_fiscal_year_end(GDate *date, const GDate *fy_end)
This function modifies a GDate to set it to the last day of the fiscal year in which it falls.
void gnc_gdate_set_prev_month_start(GDate *date)
Convert a GDate to the first day of the prebvious month.
void gnc_gdate_set_prev_fiscal_year_end(GDate *date, const GDate *fy_end)
This function modifies a GDate to set it to the last day of the fiscal year prior to the one in which...
GncAccountingPeriod
This specifies a time interval.
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 qof_instance_get(const QofInstance *inst, const gchar *first_prop,...)
Wrapper for g_object_get.
#define PINFO(format, args...)
Print an informational note.
Definition qoflog.h:256
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.
gint64 gnc_prefs_get_int64(const gchar *group, const gchar *pref_name)
Get an 64 bit integer value from the preferences backend.
QofBook reference.
Definition qofbook-p.hpp:47