|
GnuCash 2.4.99
|
00001 /* 00002 * gnc-accounting-period.c -- 00003 * 00004 * Copyright (c) 2005 David Hampton <hampton@employees.org> 00005 * All rights reserved. 00006 * 00007 * GnuCash is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU Library General Public License as 00009 * published by the Free Software Foundation; either version 2 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * Gnucash is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, contact: 00019 * 00020 * Free Software Foundation Voice: +1-617-542-5942 00021 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 00022 * Boston, MA 02110-1301, USA gnu@gnu.org 00023 */ 00024 00044 #include "config.h" 00045 #include <string.h> 00046 #include "gnc-accounting-period.h" 00047 #include "gnc-gdate-utils.h" 00048 #include "gnc-date.h" 00049 #include "gnc-gconf-utils.h" 00050 #include "qof.h" 00051 #include "gnc-ui-util.h" 00052 00053 /* TODO: This should probably be changed eventually. */ 00054 #define GCONF_SECTION "window/pages/account_tree/summary" 00055 #define KEY_START_CHOICE "start_choice" 00056 #define KEY_START_DATE "start_date" 00057 #define KEY_START_PERIOD "start_period" 00058 #define KEY_END_CHOICE "end_choice" 00059 #define KEY_END_DATE "end_date" 00060 #define KEY_END_PERIOD "end_period" 00061 00062 static time_t 00063 lookup_start_date_option(const gchar *section, 00064 const gchar *key_choice, 00065 const gchar *key_absolute, 00066 const gchar *key_relative, 00067 GDate *fy_end) 00068 { 00069 gchar *choice; 00070 time_t time; 00071 int which; 00072 00073 choice = gnc_gconf_get_string(section, key_choice, NULL); 00074 if (choice && strcmp(choice, "absolute") == 0) 00075 { 00076 time = gnc_gconf_get_int(section, key_absolute, NULL); 00077 } 00078 else 00079 { 00080 which = gnc_gconf_get_int(section, key_relative, NULL); 00081 time = gnc_accounting_period_start_timet(which, fy_end, NULL); 00082 } 00083 g_free(choice); 00084 /* we will need the balance of the last transaction before the start 00085 date, so subtract 1 from start date */ 00086 /* CAS: we don't actually do what this comment says. I think that's 00087 because a bug in the engine has been fixed. */ 00088 return time; 00089 } 00090 00091 00092 static time_t 00093 lookup_end_date_option(const gchar *section, 00094 const gchar *key_choice, 00095 const gchar *key_absolute, 00096 const gchar *key_relative, 00097 GDate *fy_end) 00098 { 00099 gchar *choice; 00100 time_t time; 00101 int which; 00102 00103 choice = gnc_gconf_get_string(section, key_choice, NULL); 00104 if (choice && strcmp(choice, "absolute") == 0) 00105 { 00106 time = gnc_gconf_get_int(section, key_absolute, NULL); 00107 time = gnc_timet_get_day_end(time); 00108 } 00109 else 00110 { 00111 which = gnc_gconf_get_int(section, key_relative, NULL); 00112 time = gnc_accounting_period_end_timet(which, fy_end, NULL); 00113 } 00114 g_free(choice); 00115 if (time == 0) 00116 time = -1; 00117 return time; 00118 } 00119 00120 static GDate * 00121 get_fy_end(void) 00122 { 00123 QofBook *book; 00124 KvpFrame *book_frame; 00125 gint64 month, day; 00126 00127 book = gnc_get_current_book(); 00128 book_frame = qof_book_get_slots(book); 00129 month = kvp_frame_get_gint64(book_frame, "/book/fyear_end/month"); 00130 day = kvp_frame_get_gint64(book_frame, "/book/fyear_end/day"); 00131 if (g_date_valid_dmy(day, month, 2005 /* not leap year */)) 00132 return g_date_new_dmy(day, month, G_DATE_BAD_YEAR); 00133 return NULL; 00134 } 00135 00136 time_t 00137 gnc_accounting_period_fiscal_start(void) 00138 { 00139 time_t t; 00140 GDate *fy_end = get_fy_end(); 00141 t = lookup_start_date_option(GCONF_SECTION, KEY_START_CHOICE, 00142 KEY_START_DATE, KEY_START_PERIOD, fy_end); 00143 if (fy_end) 00144 g_date_free(fy_end); 00145 return t; 00146 } 00147 00148 time_t 00149 gnc_accounting_period_fiscal_end(void) 00150 { 00151 time_t t; 00152 GDate *fy_end = get_fy_end(); 00153 00154 t = lookup_end_date_option(GCONF_SECTION, KEY_END_CHOICE, 00155 KEY_END_DATE, KEY_END_PERIOD, fy_end); 00156 if (fy_end) 00157 g_date_free(fy_end); 00158 return t; 00159 } 00160 00161 GDate * 00162 gnc_accounting_period_start_gdate (GncAccountingPeriod which, 00163 const GDate *fy_end, 00164 const GDate *contains) 00165 { 00166 GDate *date; 00167 00168 if (contains) 00169 { 00170 date = g_date_new_dmy(g_date_get_day(contains), 00171 g_date_get_month(contains), 00172 g_date_get_year(contains)); 00173 } 00174 else 00175 { 00176 date = g_date_new(); 00177 g_date_set_time_t(date, time(NULL)); 00178 } 00179 00180 switch (which) 00181 { 00182 default: 00183 g_message("Undefined relative time constant %d", which); 00184 g_date_free(date); 00185 return NULL; 00186 00187 case GNC_ACCOUNTING_PERIOD_TODAY: 00188 /* Already have today's date */ 00189 break; 00190 00191 case GNC_ACCOUNTING_PERIOD_MONTH: 00192 gnc_gdate_set_month_start(date); 00193 break; 00194 00195 case GNC_ACCOUNTING_PERIOD_MONTH_PREV: 00196 gnc_gdate_set_prev_month_start(date); 00197 break; 00198 00199 case GNC_ACCOUNTING_PERIOD_QUARTER: 00200 gnc_gdate_set_quarter_start(date); 00201 break; 00202 00203 case GNC_ACCOUNTING_PERIOD_QUARTER_PREV: 00204 gnc_gdate_set_prev_quarter_start(date); 00205 break; 00206 00207 case GNC_ACCOUNTING_PERIOD_CYEAR: 00208 gnc_gdate_set_year_start(date); 00209 break; 00210 00211 case GNC_ACCOUNTING_PERIOD_CYEAR_PREV: 00212 gnc_gdate_set_prev_year_start(date); 00213 break; 00214 00215 case GNC_ACCOUNTING_PERIOD_FYEAR: 00216 if (fy_end == NULL) 00217 { 00218 g_message("Request for fisal year value but no fiscal year end value provided."); 00219 g_date_free(date); 00220 return NULL; 00221 } 00222 gnc_gdate_set_fiscal_year_start(date, fy_end); 00223 break; 00224 00225 case GNC_ACCOUNTING_PERIOD_FYEAR_PREV: 00226 if (fy_end == NULL) 00227 { 00228 g_message("Request for fisal year value but no fiscal year end value provided."); 00229 g_date_free(date); 00230 return NULL; 00231 } 00232 gnc_gdate_set_prev_fiscal_year_start(date, fy_end); 00233 break; 00234 } 00235 return date; 00236 } 00237 00238 time_t 00239 gnc_accounting_period_start_timet (GncAccountingPeriod which, 00240 const GDate *fy_end, 00241 const GDate *contains) 00242 { 00243 GDate *date; 00244 time_t secs; 00245 00246 date = gnc_accounting_period_start_gdate(which, fy_end, contains); 00247 if (!date) 00248 return 0; 00249 00250 secs = gnc_timet_get_day_start_gdate(date); 00251 g_date_free(date); 00252 return secs; 00253 } 00254 00255 GDate * 00256 gnc_accounting_period_end_gdate (GncAccountingPeriod which, 00257 const GDate *fy_end, 00258 const GDate *contains) 00259 { 00260 GDate *date; 00261 00262 if (contains) 00263 { 00264 date = g_date_new_dmy(g_date_get_day(contains), 00265 g_date_get_month(contains), 00266 g_date_get_year(contains)); 00267 } 00268 else 00269 { 00270 date = g_date_new(); 00271 g_date_set_time_t(date, time(NULL)); 00272 } 00273 00274 switch (which) 00275 { 00276 default: 00277 g_message("Undefined relative time constant %d", which); 00278 g_date_free(date); 00279 return 0; 00280 00281 case GNC_ACCOUNTING_PERIOD_TODAY: 00282 /* Already have today's date */ 00283 break; 00284 00285 case GNC_ACCOUNTING_PERIOD_MONTH: 00286 gnc_gdate_set_month_end(date); 00287 break; 00288 00289 case GNC_ACCOUNTING_PERIOD_MONTH_PREV: 00290 gnc_gdate_set_prev_month_end(date); 00291 break; 00292 00293 case GNC_ACCOUNTING_PERIOD_QUARTER: 00294 gnc_gdate_set_quarter_end(date); 00295 break; 00296 00297 case GNC_ACCOUNTING_PERIOD_QUARTER_PREV: 00298 gnc_gdate_set_prev_quarter_end(date); 00299 break; 00300 00301 case GNC_ACCOUNTING_PERIOD_CYEAR: 00302 gnc_gdate_set_year_end(date); 00303 break; 00304 00305 case GNC_ACCOUNTING_PERIOD_CYEAR_PREV: 00306 gnc_gdate_set_prev_year_end(date); 00307 break; 00308 00309 case GNC_ACCOUNTING_PERIOD_FYEAR: 00310 if (fy_end == NULL) 00311 { 00312 g_message("Request for fisal year value but no fiscal year end value provided."); 00313 g_date_free(date); 00314 return 0; 00315 } 00316 gnc_gdate_set_fiscal_year_end(date, fy_end); 00317 break; 00318 00319 case GNC_ACCOUNTING_PERIOD_FYEAR_PREV: 00320 if (fy_end == NULL) 00321 { 00322 g_message("Request for fisal year value but no fiscal year end value provided."); 00323 g_date_free(date); 00324 return 0; 00325 } 00326 gnc_gdate_set_prev_fiscal_year_end(date, fy_end); 00327 break; 00328 } 00329 00330 return date; 00331 } 00332 00333 time_t 00334 gnc_accounting_period_end_timet (GncAccountingPeriod which, 00335 const GDate *fy_end, 00336 const GDate *contains) 00337 { 00338 GDate *date; 00339 time_t secs; 00340 00341 date = gnc_accounting_period_end_gdate(which, fy_end, contains); 00342 if (!date) 00343 return 0; 00344 00345 secs = gnc_timet_get_day_end_gdate(date); 00346 g_date_free(date); 00347 return secs ; 00348 } 00349 00350
1.7.4