32 #include <qofinstance-p.h>
35 #include "gncBillTermP.h"
72 #define _GNC_MOD_NAME GNC_ID_BILLTERM
74 #define SET_STR(obj, member, str) { \
77 if (!g_strcmp0 (member, str)) return; \
78 gncBillTermBeginEdit (obj); \
79 tmp = CACHE_INSERT (str); \
80 CACHE_REMOVE (member); \
93 qof_instance_set_dirty(&term->inst);
97 static inline void maybe_resort_list (
GncBillTerm *term)
101 if (term->parent || term->invisible)
return;
110 bi->terms = g_list_insert_sorted (bi->terms, term,
118 bi->terms = g_list_remove (bi->terms, term);
125 table->children = g_list_prepend(table->children, child);
132 table->children = g_list_remove(table->children, child);
144 G_DEFINE_TYPE(
GncBillTerm, gnc_billterm, QOF_TYPE_INSTANCE);
152 gnc_billterm_dispose(GObject *btp)
154 G_OBJECT_CLASS(gnc_billterm_parent_class)->dispose(btp);
158 gnc_billterm_finalize(GObject* btp)
160 G_OBJECT_CLASS(gnc_billterm_parent_class)->finalize(btp);
164 gnc_billterm_get_property (GObject *
object,
171 g_return_if_fail(GNC_IS_BILLTERM(
object));
173 bt = GNC_BILLTERM(
object);
177 g_value_set_string(value, bt->name);
180 G_OBJECT_WARN_INVALID_PROPERTY_ID(
object, prop_id, pspec);
186 gnc_billterm_set_property (GObject *
object,
193 g_return_if_fail(GNC_IS_BILLTERM(
object));
195 bt = GNC_BILLTERM(
object);
196 g_assert (qof_instance_get_editlevel(bt));
201 gncBillTermSetName(bt, g_value_get_string(value));
204 G_OBJECT_WARN_INVALID_PROPERTY_ID(
object, prop_id, pspec);
225 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
228 gobject_class->dispose = gnc_billterm_dispose;
229 gobject_class->finalize = gnc_billterm_finalize;
230 gobject_class->set_property = gnc_billterm_set_property;
231 gobject_class->get_property = gnc_billterm_get_property;
233 qof_class->get_display_name = NULL;
234 qof_class->refers_to_object = NULL;
235 qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list;
237 g_object_class_install_property
240 g_param_spec_string (
"name",
242 "The bill term name is an arbitrary string "
243 "assigned by the user. It is intended to "
244 "a short, 10 to 30 character long string "
245 "that is displayed by the GUI as the "
246 "billterm mnemonic.",
255 if (!book)
return NULL;
257 term = g_object_new (GNC_TYPE_BILLTERM, NULL);
259 term->name = CACHE_INSERT (
"");
260 term->desc = CACHE_INSERT (
"");
261 term->discount = gnc_numeric_zero ();
272 DEBUG(
"destroying bill term %s (%p)", guidstr, term);
273 qof_instance_set_destroying(term, TRUE);
274 qof_instance_set_dirty (&term->inst);
275 gncBillTermCommitEdit (term);
286 CACHE_REMOVE (term->name);
287 CACHE_REMOVE (term->desc);
291 PERR(
"free a billterm without do_free set!");
295 gncBillTermRemoveChild(term->parent, term);
298 for (list = term->children; list; list = list->next)
301 gncBillTermSetParent(child, NULL);
303 g_list_free(term->children);
306 g_object_unref (term);
312 void gncBillTermSetName (
GncBillTerm *term,
const char *name)
314 if (!term || !name)
return;
315 SET_STR (term, term->name, name);
317 maybe_resort_list (term);
318 gncBillTermCommitEdit (term);
321 void gncBillTermSetDescription (
GncBillTerm *term,
const char *desc)
323 if (!term || !desc)
return;
324 SET_STR (term, term->desc, desc);
326 maybe_resort_list (term);
327 gncBillTermCommitEdit (term);
330 void gncBillTermSetType (
GncBillTerm *term, GncBillTermType type)
333 if (term->type == type)
return;
334 gncBillTermBeginEdit (term);
337 gncBillTermCommitEdit (term);
344 void qofBillTermSetType (
GncBillTerm *term, const
char *type_label)
346 GncBillTermType type;
348 type = GncBillTermTypefromString(type_label);
349 gncBillTermSetType(term, type);
352 void gncBillTermSetDueDays (
GncBillTerm *term, gint days)
355 if (term->due_days == days)
return;
356 gncBillTermBeginEdit (term);
357 term->due_days = days;
359 gncBillTermCommitEdit (term);
362 void gncBillTermSetDiscountDays (
GncBillTerm *term, gint days)
365 if (term->disc_days == days)
return;
366 gncBillTermBeginEdit (term);
367 term->disc_days = days;
369 gncBillTermCommitEdit (term);
376 gncBillTermBeginEdit (term);
377 term->discount = discount;
379 gncBillTermCommitEdit (term);
382 void gncBillTermSetCutoff (
GncBillTerm *term, gint cutoff)
385 if (term->cutoff == cutoff)
return;
386 gncBillTermBeginEdit (term);
387 term->cutoff = cutoff;
389 gncBillTermCommitEdit (term);
400 gncBillTermBeginEdit (term);
402 gncBillTermRemoveChild(term->parent, term);
403 term->parent = parent;
405 gncBillTermAddChild(parent, term);
407 if ( parent != NULL )
409 gncBillTermMakeInvisible (term);
412 gncBillTermCommitEdit (term);
418 gncBillTermBeginEdit (term);
421 gncBillTermCommitEdit (term);
427 if (term->parent || term->invisible)
return;
428 gncBillTermBeginEdit (term);
431 gncBillTermCommitEdit (term);
437 if (term->parent || term->invisible)
return;
438 g_return_if_fail (term->refcount >= 1);
439 gncBillTermBeginEdit (term);
442 gncBillTermCommitEdit (term);
445 void gncBillTermSetRefcount (
GncBillTerm *term, gint64 refcount)
448 gncBillTermBeginEdit (term);
449 term->refcount = refcount;
451 gncBillTermCommitEdit (term);
457 gncBillTermBeginEdit (term);
458 term->invisible = TRUE;
461 gncBillTermCommitEdit (term);
477 PERR(
"BillTerm QofBackend Failure: %d", errcode);
478 gnc_engine_signal_commit_error( errcode );
484 gncBillTermFree(term);
500 GList *list = gncBillTermGetTerms (book);
502 for ( ; list; list = list->next)
505 if (!g_strcmp0 (term->name, name))
511 GList * gncBillTermGetTerms (
QofBook *book)
514 if (!book)
return NULL;
520 const char *gncBillTermGetName (
const GncBillTerm *term)
522 if (!term)
return NULL;
526 const char *gncBillTermGetDescription (
const GncBillTerm *term)
528 if (!term)
return NULL;
532 GncBillTermType gncBillTermGetType (
const GncBillTerm *term)
542 const
char* qofBillTermGetType (const
GncBillTerm *term)
548 return GncBillTermTypeasString(term->type);
551 gint gncBillTermGetDueDays (
const GncBillTerm *term)
554 return term->due_days;
557 gint gncBillTermGetDiscountDays (
const GncBillTerm *term)
560 return term->disc_days;
565 if (!term)
return gnc_numeric_zero ();
566 return term->discount;
569 gint gncBillTermGetCutoff (
const GncBillTerm *term)
579 if (!term)
return NULL;
582 gncBillTermBeginEdit(t);
584 gncBillTermSetName (t, term->name);
585 gncBillTermSetDescription (t, term->desc);
587 t->type = term->type;
588 t->due_days = term->due_days;
589 t->disc_days = term->disc_days;
590 t->discount = term->discount;
591 t->cutoff = term->cutoff;
594 gncBillTermCommitEdit(t);
603 if (!term)
return NULL;
604 if (term->child)
return term->child;
605 if (term->parent || term->invisible)
return term;
608 child = gncBillTermCopy (term);
609 gncBillTermSetChild (term, child);
610 gncBillTermSetParent (child, term);
617 if (!term)
return NULL;
621 gint64 gncBillTermGetRefcount (
const GncBillTerm *term)
624 return term->refcount;
627 gboolean gncBillTermGetInvisible (
const GncBillTerm *term)
629 if (!term)
return FALSE;
630 return term->invisible;
637 if (!a && !b)
return 0;
641 ret = g_strcmp0 (a->name, b->name);
644 return g_strcmp0 (a->desc, b->desc);
649 if (a == NULL && b == NULL)
return TRUE;
650 if (a == NULL || b == NULL)
return FALSE;
652 g_return_val_if_fail(GNC_IS_BILLTERM(a), FALSE);
653 g_return_val_if_fail(GNC_IS_BILLTERM(b), FALSE);
655 if (g_strcmp0(a->name, b->name) != 0)
657 PWARN(
"Names differ: %s vs %s", a->name, b->name);
661 if (g_strcmp0(a->desc, b->desc) != 0)
663 PWARN(
"Descriptions differ: %s vs %s", a->desc, b->desc);
667 if (a->type != b->type)
669 PWARN(
"Types differ");
673 if (a->due_days != b->due_days)
675 PWARN(
"Due days differ: %d vs %d", a->due_days, b->due_days);
679 if (a->disc_days != b->disc_days)
681 PWARN(
"Discount days differ: %d vs %d", a->disc_days, b->disc_days);
687 PWARN(
"Discounts differ");
691 if (a->cutoff != b->cutoff)
693 PWARN(
"Cutoffs differ: %d vs %d", a->cutoff, b->cutoff);
697 if (a->invisible != b->invisible)
699 PWARN(
"Invisible flags differ");
719 gboolean gncBillTermIsDirty (
const GncBillTerm *term)
721 if (!term)
return FALSE;
728 #define SECS_PER_DAY 86400
757 int *month,
int *year)
759 int iday, imonth, iyear;
760 int cutoff = term->cutoff;
762 g_return_if_fail (term->type == GNC_TERM_TYPE_PROXIMO);
786 if (month) *month = imonth;
787 if (year) *year = iyear;
805 int day, month, year;
809 case GNC_TERM_TYPE_DAYS:
810 res.tv_sec += (SECS_PER_DAY * days);
812 case GNC_TERM_TYPE_PROXIMO:
813 compute_monthyear (term, post_date, &month, &year);
827 if (!term)
return res;
829 return compute_time (term, post_date, term->due_days);
833 static void _gncBillTermCreate (
QofBook *book)
843 static void _gncBillTermDestroy (
QofBook *book)
851 g_list_free (bi->terms);
858 DI(.e_type = ) _GNC_MOD_NAME,
859 DI(.type_label = ) "Billing Term",
860 DI(.create = ) (gpointer)gncBillTermCreate,
861 DI(.book_begin = ) _gncBillTermCreate,
862 DI(.book_end = ) _gncBillTermDestroy,
866 DI(.printable = ) NULL,
870 gboolean gncBillTermRegister (
void)
878 { GNC_BILLTERM_DISCDAYS, QOF_TYPE_INT32, (
QofAccessFunc)gncBillTermGetDiscountDays, (
QofSetterFunc)gncBillTermSetDiscountDays },
881 { GNC_BILLTERM_REFCOUNT, QOF_TYPE_INT64, (
QofAccessFunc)gncBillTermGetRefcount, NULL },
int gncBillTermCompare(const GncBillTerm *a, const GncBillTerm *b)
int qof_instance_version_cmp(const QofInstance *left, const QofInstance *right)
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
const GncGUID * qof_instance_get_guid(gconstpointer)
QofBook * qof_instance_get_book(gconstpointer)
gboolean qof_collection_is_dirty(const QofCollection *col)
QofBackendError
The errors that can be reported to the GUI & other front-end users.
#define DEBUG(format, args...)
gboolean qof_instance_get_destroying(gconstpointer ptr)
void qof_class_register(QofIdTypeConst obj_name, QofSortFunc default_sort_fcn, const QofParam *params)
#define ENUM_TERMS_TYPE(_)
Use a 64-bit unsigned int timespec.
gchar * guid_to_string_buff(const GncGUID *guid, gchar *buff)
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
#define QOF_OBJECT_VERSION
gboolean qof_commit_edit(QofInstance *inst)
Timespec gnc_dmy2timespec(gint day, gint month, gint year)
#define PERR(format, args...)
void qof_collection_foreach(const QofCollection *, QofInstanceForeachCB, gpointer user_data)
gboolean gncBillTermEqual(const GncBillTerm *a, const GncBillTerm *b)
#define PWARN(format, args...)
gboolean gncBillTermIsFamily(const GncBillTerm *a, const GncBillTerm *b)
void qof_instance_init_data(QofInstance *, QofIdType, QofBook *)
gboolean qof_begin_edit(QofInstance *inst)
#define GUID_ENCODING_LENGTH
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
int(* QofSortFunc)(gconstpointer, gconstpointer)
int gnc_date_get_last_mday(int month, int year)
void qof_book_set_data(QofBook *book, const gchar *key, gpointer data)
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
void qof_collection_mark_clean(QofCollection *)
gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b)
All type declarations for the whole Gnucash engine.
void(* QofSetterFunc)(gpointer, gpointer)
gboolean qof_object_register(const QofObject *object)
void qof_event_gen(QofInstance *entity, QofEventId event_type, gpointer event_data)
Invoke all registered event handlers using the given arguments.
gpointer qof_book_get_data(const QofBook *book, const gchar *key)
void gnc_timespec2dmy(Timespec ts, gint *day, gint *month, gint *year)
const gchar * QofLogModule