-
Notifications
You must be signed in to change notification settings - Fork 761
/
gnc-budget.c
370 lines (309 loc) · 11.2 KB
/
gnc-budget.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/********************************************************************\
* gnc-budget.c -- Implementation of the top level Budgeting API. *
* Copyright (C) 04 sep 2003 Darin Willits <darin@willits.ca> *
* Copyright (C) 2005-2006 Chris Shoemaker <c.shoemaker@cox.net> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program 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 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 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gi18n.h>
#include <time.h>
#include "qof.h"
#include "Account.h"
#include "Group.h"
#include "gnc-budget.h"
#include "gnc-commodity.h"
#include "gnc-gdate-utils.h"
static QofLogModule log_module = GNC_MOD_ENGINE;
struct gnc_budget_private{
QofInstance inst;
gchar* name;
gchar* description;
Recurrence recurrence;
guint num_periods;
};
GncBudget*
gnc_budget_new(QofBook *book)
{
GncBudget* budget;
GDate date;
g_return_val_if_fail(book, NULL);
ENTER(" ");
budget = g_new0(GncBudget, 1);
qof_instance_init (&budget->inst, GNC_ID_BUDGET, book);
g_date_set_time(&date, time(NULL));
g_date_subtract_days(&date, g_date_get_day(&date)-1);
recurrenceSet(&budget->recurrence, 1, PERIOD_MONTH, &date);
gnc_budget_set_name(budget, _("Unnamed Budget"));
gnc_budget_set_description(budget, "");
gnc_budget_set_num_periods(budget, 12);
qof_event_gen( &budget->inst.entity, QOF_EVENT_CREATE , NULL);
LEAVE(" ");
return budget;
}
void
gnc_budget_free(GncBudget* budget)
{
if (budget == NULL)
return;
g_return_if_fail(GNC_IS_BUDGET(budget));
qof_instance_set_dirty(&budget->inst);
/* We first send the message that this object is about to be
* destroyed so that any GUI elements can remove it before it is
* actually gone. */
qof_event_gen( &budget->inst.entity, QOF_EVENT_DESTROY, NULL);
CACHE_REMOVE(budget->name);
CACHE_REMOVE(budget->description);
qof_instance_release (&budget->inst);
g_free(budget);
}
void
gnc_budget_set_name(GncBudget* budget, const gchar* name)
{
g_return_if_fail(GNC_IS_BUDGET(budget) && name);
CACHE_REPLACE(budget->name, name);
qof_instance_set_dirty(&budget->inst);
qof_event_gen( &budget->inst.entity, QOF_EVENT_MODIFY, NULL);
}
const gchar*
gnc_budget_get_name(GncBudget* budget)
{
g_return_val_if_fail(GNC_IS_BUDGET(budget), NULL);
return budget->name;
}
void
gnc_budget_set_description(GncBudget* budget, const gchar* description)
{
g_return_if_fail(GNC_IS_BUDGET(budget));
g_return_if_fail(description);
CACHE_REPLACE(budget->description, description);
qof_instance_set_dirty(&budget->inst);
qof_event_gen( &budget->inst.entity, QOF_EVENT_MODIFY, NULL);
}
const gchar*
gnc_budget_get_description(GncBudget* budget)
{
g_return_val_if_fail(GNC_IS_BUDGET(budget), NULL);
return budget->description;
}
void
gnc_budget_set_recurrence(GncBudget *budget, const Recurrence *r)
{
g_return_if_fail(budget && r);
budget->recurrence = *r;
qof_instance_set_dirty(&budget->inst);
qof_event_gen(&budget->inst.entity, QOF_EVENT_MODIFY, NULL);
}
const Recurrence *
gnc_budget_get_recurrence(GncBudget *budget)
{
g_return_val_if_fail(budget, NULL);
return (&budget->recurrence);
}
const GUID*
gnc_budget_get_guid(GncBudget* budget)
{
g_return_val_if_fail(budget, NULL);
g_return_val_if_fail(GNC_IS_BUDGET(budget), NULL);
return qof_entity_get_guid(QOF_ENTITY(budget));
}
void
gnc_budget_set_num_periods(GncBudget* budget, guint num_periods)
{
g_return_if_fail(GNC_IS_BUDGET(budget));
budget->num_periods = num_periods;
qof_instance_set_dirty(&budget->inst);
qof_event_gen( &budget->inst.entity, QOF_EVENT_MODIFY, NULL);
}
guint
gnc_budget_get_num_periods(GncBudget* budget)
{
g_return_val_if_fail(GNC_IS_BUDGET(budget), 0);
return budget->num_periods;
}
#define BUF_SIZE (10 + GUID_ENCODING_LENGTH + \
GNC_BUDGET_MAX_NUM_PERIODS_DIGITS)
/* period_num is zero-based */
/* What happens when account is deleted, after we have an entry for it? */
void
gnc_budget_set_account_period_value(GncBudget *budget, Account *account,
guint period_num, gnc_numeric val)
{
const GUID *guid;
KvpFrame *frame;
gchar path[BUF_SIZE];
gchar *bufend;
frame = qof_instance_get_slots(QOF_INSTANCE(budget));
guid = xaccAccountGetGUID(account);
bufend = guid_to_string_buff(guid, path);
g_sprintf(bufend, "/%d", period_num);
kvp_frame_set_numeric(frame, path, val);
qof_instance_set_dirty(&budget->inst);
qof_event_gen( &budget->inst.entity, QOF_EVENT_MODIFY, NULL);
}
/* We don't need these here, but maybe they're useful somewhere else?
Maybe this should move to Account.h */
#if 0
static gpointer
is_same_commodity(Account *a, gpointer data)
{
gnc_commodity *acct_comm;
gnc_commodity *comm;
g_return_val_if_fail(data, NULL);
// What? No type-checking macro?
comm = (gnc_commodity *) data;
acct_comm = xaccAccountGetCommodity(a);
return gnc_commodity_equal(comm, acct_comm) ? NULL : data;
}
static gboolean
xaccAccountChildrenHaveSameCommodity(Account *account)
{
AccountGroup *grp;
gpointer different;
gnc_commodity *comm;
comm = xaccAccountGetCommodity(account);
grp = xaccAccountGetChildren(account);
different = xaccGroupForEachAccount(
grp, is_same_commodity, comm, TRUE);
return (different == NULL);
}
#endif
/* Does not distinguish between unset and zero - returns zero either way. */
gnc_numeric
gnc_budget_get_account_period_value(GncBudget *budget, Account *account,
guint period_num)
{
gnc_numeric numeric;
gchar path[BUF_SIZE];
gchar *bufend;
KvpFrame *frame;
numeric = gnc_numeric_zero();
g_return_val_if_fail(GNC_IS_BUDGET(budget), numeric);
g_return_val_if_fail(account, numeric);
frame = qof_instance_get_slots(QOF_INSTANCE(budget));
bufend = guid_to_string_buff(xaccAccountGetGUID(account), path);
g_sprintf(bufend, "/%d", period_num);
numeric = kvp_frame_get_numeric(frame, path); /* Zero if unset */
return numeric;
}
Timespec
gnc_budget_get_period_start_date(GncBudget *budget, guint period_num)
{
Timespec ts;
timespecFromTime_t(
&ts, recurrenceGetPeriodTime(&budget->recurrence, period_num, FALSE));
return ts;
}
gnc_numeric
gnc_budget_get_account_period_actual_value(
GncBudget *budget, Account *acc, guint period_num)
{
// FIXME: maybe zero is not best error return val.
g_return_val_if_fail(GNC_IS_BUDGET(budget) && acc, gnc_numeric_zero());
return recurrenceGetAccountPeriodValue(&budget->recurrence,
acc, period_num);
}
QofBook*
gnc_budget_get_book(GncBudget* budget)
{
g_return_val_if_fail(GNC_IS_BUDGET(budget), NULL);
return qof_instance_get_book(&budget->inst);
}
GncBudget*
gnc_budget_lookup (const GUID *guid, QofBook *book)
{
QofCollection *col;
g_return_val_if_fail(guid, NULL);
g_return_val_if_fail(book, NULL);
col = qof_book_get_collection (book, GNC_ID_BUDGET);
return GNC_BUDGET(qof_collection_lookup_entity (col, guid));
}
static void just_get_one(QofEntity *ent, gpointer data)
{
GncBudget **bgt = (GncBudget**)data;
if (bgt && !*bgt) *bgt = GNC_BUDGET(ent);
}
GncBudget*
gnc_budget_get_default (QofBook *book)
{
QofCollection *col;
GncBudget *bgt = NULL;
g_return_val_if_fail(book, NULL);
col = qof_book_get_collection(book, GNC_ID_BUDGET);
if (qof_collection_count(col) > 0) {
qof_collection_foreach(col, just_get_one, &bgt);
}
return bgt;
}
/* Define the QofObject. */
static QofObject budget_object_def =
{
interface_version: QOF_OBJECT_VERSION,
e_type: GNC_ID_BUDGET,
type_label: "Budget",
create: (gpointer)gnc_budget_new,
book_begin: NULL,
book_end: NULL,
is_dirty: qof_collection_is_dirty,
mark_clean: qof_collection_mark_clean,
foreach: qof_collection_foreach,
printable: (const char* (*)(gpointer)) gnc_budget_get_name,
version_cmp: (int (*)(gpointer, gpointer)) qof_instance_version_cmp,
};
/* Static wrapper getters for the recurrence params */
static PeriodType gnc_budget_get_rec_pt(const GncBudget *bgt)
{ return recurrenceGetPeriodType(&(bgt->recurrence)); }
static guint gnc_budget_get_rec_mult(const GncBudget *bgt)
{ return recurrenceGetMultiplier(&(bgt->recurrence)); }
static GDate gnc_budget_get_rec_date(const GncBudget *bgt)
{ return recurrenceGetDate(&(bgt->recurrence)); }
/* Register ourselves with the engine. */
gboolean gnc_budget_register (void)
{
static QofParam params[] = {
{ "name", QOF_TYPE_STRING,
(QofAccessFunc) gnc_budget_get_name,
(QofSetterFunc) gnc_budget_set_name },
{ "description", QOF_TYPE_STRING,
(QofAccessFunc) gnc_budget_get_description,
(QofSetterFunc) gnc_budget_set_description },
{ "recurrence_period_type", QOF_TYPE_INT32,
(QofAccessFunc) gnc_budget_get_rec_pt, NULL },
/* Signedness problem: Should be unsigned. */
{ "recurrence_multiplier", QOF_TYPE_INT32,
(QofAccessFunc) gnc_budget_get_rec_mult, NULL },
/* This is the same way that SchedXaction.c uses QOF_TYPE_DATE
but I don't think QOF actually supports a GDate, so I think
this is wrong. */
{ "recurrence_date", QOF_TYPE_DATE,
(QofAccessFunc) gnc_budget_get_rec_date, NULL },
/* Signedness problem: Should be unsigned. */
{ "num_periods", QOF_TYPE_INT32,
(QofAccessFunc) gnc_budget_get_num_periods,
(QofSetterFunc) gnc_budget_set_num_periods },
{ QOF_PARAM_BOOK, QOF_ID_BOOK,
(QofAccessFunc) qof_instance_get_book, NULL },
{ QOF_PARAM_GUID, QOF_TYPE_GUID,
(QofAccessFunc) qof_instance_get_guid, NULL },
{ NULL },
};
qof_class_register(GNC_ID_BUDGET, (QofSortFunc) NULL, params);
return qof_object_register(&budget_object_def);
}