GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-accounting-period.c
Go to the documentation of this file.
1 /*
2  * gnc-accounting-period.c --
3  *
4  * Copyright (c) 2005 David Hampton <[email protected]>
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 [email protected]
23  */
24 
44 #include "config.h"
45 #include <string.h>
46 #include "gnc-accounting-period.h"
47 #include "gnc-gdate-utils.h"
48 #include "gnc-date.h"
49 #include "gnc-prefs.h"
50 #include "qof.h"
51 #include "gnc-ui-util.h"
52 
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  gchar *choice;
64  time64 time;
65  int which;
66 
67 
68  if (gnc_prefs_get_bool (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_CHOICE_ABS))
69  time = gnc_prefs_get_int64 (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_DATE);
70  else
71  {
72  which = gnc_prefs_get_int(GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_START_PERIOD);
73  time = gnc_accounting_period_start_time64(which, fy_end, NULL);
74  }
75  /* we will need the balance of the last transaction before the start
76  date, so subtract 1 from start date */
77  /* CAS: we don't actually do what this comment says. I think that's
78  because a bug in the engine has been fixed. */
79  return time;
80 }
81 
82 static time64
83 lookup_end_date_option(GDate *fy_end)
84 {
85  time64 time;
86  int which;
87 
88  if (gnc_prefs_get_bool (GNC_PREFS_GROUP_ACCT_SUMMARY, GNC_PREF_END_CHOICE_ABS))
89  time = gnc_prefs_get_int64 (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  KvpFrame *book_frame;
105  GDate *date = NULL;
106 
107  book = gnc_get_current_book();
108  qof_instance_get (QOF_INSTANCE (book),
109  "fy-end", &date,
110  NULL);
111  return date;
112 }
113 
114 time64
115 gnc_accounting_period_fiscal_start(void)
116 {
117  time64 t;
118  GDate *fy_end = get_fy_end();
119  t = lookup_start_date_option(fy_end);
120  if (fy_end)
121  g_date_free(fy_end);
122  return t;
123 }
124 
125 time64
126 gnc_accounting_period_fiscal_end(void)
127 {
128  time64 t;
129  GDate *fy_end = get_fy_end();
130 
131  t = lookup_end_date_option(fy_end);
132  if (fy_end)
133  g_date_free(fy_end);
134  return t;
135 }
136 
137 GDate *
139  const GDate *fy_end,
140  const GDate *contains)
141 {
142  GDate *date;
143 
144  if (contains)
145  {
146  date = g_date_new_dmy(g_date_get_day(contains),
147  g_date_get_month(contains),
148  g_date_get_year(contains));
149  }
150  else
151  {
152  date = g_date_new ();
153  gnc_gdate_set_today (date);
154  }
155 
156  switch (which)
157  {
158  default:
159  g_message("Undefined relative time constant %d", which);
160  g_date_free(date);
161  return NULL;
162 
163  case GNC_ACCOUNTING_PERIOD_TODAY:
164  /* Already have today's date */
165  break;
166 
167  case GNC_ACCOUNTING_PERIOD_MONTH:
169  break;
170 
171  case GNC_ACCOUNTING_PERIOD_MONTH_PREV:
173  break;
174 
175  case GNC_ACCOUNTING_PERIOD_QUARTER:
177  break;
178 
179  case GNC_ACCOUNTING_PERIOD_QUARTER_PREV:
181  break;
182 
183  case GNC_ACCOUNTING_PERIOD_CYEAR:
185  break;
186 
187  case GNC_ACCOUNTING_PERIOD_CYEAR_PREV:
189  break;
190 
191  case GNC_ACCOUNTING_PERIOD_FYEAR:
192  if (fy_end == NULL)
193  {
194  g_message("Request for fisal year value but no fiscal year end value provided.");
195  g_date_free(date);
196  return NULL;
197  }
198  gnc_gdate_set_fiscal_year_start(date, fy_end);
199  break;
200 
201  case GNC_ACCOUNTING_PERIOD_FYEAR_PREV:
202  if (fy_end == NULL)
203  {
204  g_message("Request for fisal year value but no fiscal year end value provided.");
205  g_date_free(date);
206  return NULL;
207  }
209  break;
210  }
211  return date;
212 }
213 
214 static time64
215 gnc_accounting_period_start_time64 (GncAccountingPeriod which,
216  const GDate *fy_end,
217  const GDate *contains)
218 {
219  GDate *date;
220  time64 secs;
221 
222  date = gnc_accounting_period_start_gdate(which, fy_end, contains);
223  if (!date)
224  return 0;
225 
226  secs = gnc_time64_get_day_start_gdate(date);
227  g_date_free(date);
228  return secs;
229 }
230 
231 GDate *
233  const GDate *fy_end,
234  const GDate *contains)
235 {
236  GDate *date;
237 
238  if (contains)
239  {
240  date = g_date_new_dmy(g_date_get_day(contains),
241  g_date_get_month(contains),
242  g_date_get_year(contains));
243  }
244  else
245  {
246  date = g_date_new ();
247  gnc_gdate_set_today (date);
248  }
249 
250  switch (which)
251  {
252  default:
253  g_message("Undefined relative time constant %d", which);
254  g_date_free(date);
255  return 0;
256 
257  case GNC_ACCOUNTING_PERIOD_TODAY:
258  /* Already have today's date */
259  break;
260 
261  case GNC_ACCOUNTING_PERIOD_MONTH:
263  break;
264 
265  case GNC_ACCOUNTING_PERIOD_MONTH_PREV:
267  break;
268 
269  case GNC_ACCOUNTING_PERIOD_QUARTER:
271  break;
272 
273  case GNC_ACCOUNTING_PERIOD_QUARTER_PREV:
275  break;
276 
277  case GNC_ACCOUNTING_PERIOD_CYEAR:
279  break;
280 
281  case GNC_ACCOUNTING_PERIOD_CYEAR_PREV:
283  break;
284 
285  case GNC_ACCOUNTING_PERIOD_FYEAR:
286  if (fy_end == NULL)
287  {
288  g_message("Request for fisal year value but no fiscal year end value provided.");
289  g_date_free(date);
290  return 0;
291  }
292  gnc_gdate_set_fiscal_year_end(date, fy_end);
293  break;
294 
295  case GNC_ACCOUNTING_PERIOD_FYEAR_PREV:
296  if (fy_end == NULL)
297  {
298  g_message("Request for fisal year value but no fiscal year end value provided.");
299  g_date_free(date);
300  return 0;
301  }
303  break;
304  }
305 
306  return date;
307 }
308 
309 static time64
310 gnc_accounting_period_end_time64 (GncAccountingPeriod which,
311  const GDate *fy_end,
312  const GDate *contains)
313 {
314  GDate *date;
315  time64 secs;
316 
317  date = gnc_accounting_period_end_gdate(which, fy_end, contains);
318  if (!date)
319  return 0;
320 
321  secs = gnc_time64_get_day_end_gdate(date);
322  g_date_free(date);
323  return secs ;
324 }
325 
void gnc_gdate_set_today(GDate *gd)
void gnc_gdate_set_prev_quarter_start(GDate *date)
void gnc_gdate_set_prev_fiscal_year_end(GDate *date, const GDate *fy_end)
void qof_instance_get(const QofInstance *inst, const gchar *first_param,...)
Wrapper for g_object_get.
Date and Time handling routines.
utility functions for the GnuCash UI
void gnc_gdate_set_month_end(GDate *date)
time64 gnc_time64_get_day_start_gdate(const GDate *date)
void gnc_gdate_set_prev_year_end(GDate *date)
time64 gnc_time64_get_day_end_gdate(const GDate *date)
gint64 gnc_prefs_get_int64(const gchar *group, const gchar *pref_name)
Definition: gnc-prefs.c:216
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Definition: gnc-prefs.c:206
void gnc_gdate_set_prev_quarter_end(GDate *date)
void gnc_gdate_set_month_start(GDate *date)
void gnc_gdate_set_quarter_end(GDate *date)
General utilities for dealing with accounting periods.
void gnc_gdate_set_quarter_start(GDate *date)
void gnc_gdate_set_prev_year_start(GDate *date)
void gnc_gdate_set_fiscal_year_start(GDate *date, const GDate *fy_end)
GncAccountingPeriod
void gnc_gdate_set_year_start(GDate *date)
Generic api to store and retrieve preferences.
GDate * gnc_accounting_period_end_gdate(GncAccountingPeriod which, const GDate *fy_end, const GDate *contains)
GDate * gnc_accounting_period_start_gdate(GncAccountingPeriod which, const GDate *fy_end, const GDate *contains)
GDate helper routines.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Definition: gnc-prefs.c:196
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
gint64 time64
Definition: gnc-date.h:83
void gnc_gdate_set_year_end(GDate *date)
void gnc_gdate_set_prev_month_end(GDate *date)
void gnc_gdate_set_prev_month_start(GDate *date)
void gnc_gdate_set_fiscal_year_end(GDate *date, const GDate *fy_end)
void gnc_gdate_set_prev_fiscal_year_start(GDate *date, const GDate *fy_end)