32 #include <qofinstance-p.h> 35 #include "gncBillTermP.h" 62 QofInstanceClass parent_class;
70 static QofLogModule log_module = GNC_MOD_BUSINESS;
72 #define _GNC_MOD_NAME GNC_ID_BILLTERM 74 #define SET_STR(obj, member, str) { \ 75 if (!g_strcmp0 (member, str)) return; \ 76 gncBillTermBeginEdit (obj); \ 77 CACHE_REPLACE(member, str); \ 87 mark_term (GncBillTerm *term)
89 qof_instance_set_dirty(&term->inst);
93 static inline void maybe_resort_list (GncBillTerm *term)
97 if (term->parent || term->invisible)
return;
102 static inline void addObj (GncBillTerm *term)
106 bi->terms = g_list_insert_sorted (bi->terms, term,
110 static inline void remObj (GncBillTerm *term)
114 bi->terms = g_list_remove (bi->terms, term);
118 gncBillTermAddChild (GncBillTerm *
table, GncBillTerm *child)
121 table->children = g_list_prepend(
table->children, child);
125 gncBillTermRemoveChild (GncBillTerm *
table, GncBillTerm *child)
128 table->children = g_list_remove(
table->children, child);
140 G_DEFINE_TYPE(GncBillTerm, gnc_billterm, QOF_TYPE_INSTANCE)
143 gnc_billterm_init(GncBillTerm* bt)
148 gnc_billterm_dispose(GObject *btp)
150 G_OBJECT_CLASS(gnc_billterm_parent_class)->dispose(btp);
154 gnc_billterm_finalize(GObject* btp)
156 G_OBJECT_CLASS(gnc_billterm_parent_class)->finalize(btp);
160 gnc_billterm_get_property (GObject *
object,
167 g_return_if_fail(GNC_IS_BILLTERM(
object));
169 bt = GNC_BILLTERM(
object);
173 g_value_set_string(value, bt->name);
176 G_OBJECT_WARN_INVALID_PROPERTY_ID(
object, prop_id, pspec);
182 gnc_billterm_set_property (GObject *
object,
189 g_return_if_fail(GNC_IS_BILLTERM(
object));
191 bt = GNC_BILLTERM(
object);
192 g_assert (qof_instance_get_editlevel(bt));
197 gncBillTermSetName(bt, g_value_get_string(value));
200 G_OBJECT_WARN_INVALID_PROPERTY_ID(
object, prop_id, pspec);
219 gnc_billterm_class_init (GncBillTermClass *klass)
221 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
222 QofInstanceClass* qof_class = QOF_INSTANCE_CLASS(klass);
224 gobject_class->dispose = gnc_billterm_dispose;
225 gobject_class->finalize = gnc_billterm_finalize;
226 gobject_class->set_property = gnc_billterm_set_property;
227 gobject_class->get_property = gnc_billterm_get_property;
229 qof_class->get_display_name = NULL;
230 qof_class->refers_to_object = NULL;
231 qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list;
233 g_object_class_install_property
236 g_param_spec_string (
"name",
238 "The bill term name is an arbitrary string " 239 "assigned by the user. It is intended to " 240 "a short, 10 to 30 character long string " 241 "that is displayed by the GUI as the " 242 "billterm mnemonic.",
248 GncBillTerm * gncBillTermCreate (QofBook *book)
251 if (!book)
return NULL;
253 term = g_object_new (GNC_TYPE_BILLTERM, NULL);
255 term->name = CACHE_INSERT (
"");
256 term->desc = CACHE_INSERT (
"");
257 term->discount = gnc_numeric_zero ();
263 void gncBillTermDestroy (GncBillTerm *term)
268 DEBUG(
"destroying bill term %s (%p)", guidstr, term);
269 qof_instance_set_destroying(term, TRUE);
270 qof_instance_set_dirty (&term->inst);
271 gncBillTermCommitEdit (term);
274 static void gncBillTermFree (GncBillTerm *term)
282 CACHE_REMOVE (term->name);
283 CACHE_REMOVE (term->desc);
287 PERR(
"free a billterm without do_free set!");
291 gncBillTermRemoveChild(term->parent, term);
294 for (list = term->children; list; list = list->next)
297 gncBillTermSetParent(child, NULL);
299 g_list_free(term->children);
302 g_object_unref (term);
308 void gncBillTermSetName (GncBillTerm *term,
const char *name)
310 if (!term || !name)
return;
311 SET_STR (term, term->name, name);
313 maybe_resort_list (term);
314 gncBillTermCommitEdit (term);
317 void gncBillTermSetDescription (GncBillTerm *term,
const char *desc)
319 if (!term || !desc)
return;
320 SET_STR (term, term->desc, desc);
322 maybe_resort_list (term);
323 gncBillTermCommitEdit (term);
326 void gncBillTermSetType (GncBillTerm *term, GncBillTermType type)
329 if (term->type == type)
return;
330 gncBillTermBeginEdit (term);
333 gncBillTermCommitEdit (term);
340 void qofBillTermSetType (GncBillTerm *term,
const char *type_label)
342 GncBillTermType type;
344 type = GncBillTermTypefromString(type_label);
345 gncBillTermSetType(term, type);
348 void gncBillTermSetDueDays (GncBillTerm *term, gint days)
351 if (term->due_days == days)
return;
352 gncBillTermBeginEdit (term);
353 term->due_days = days;
355 gncBillTermCommitEdit (term);
358 void gncBillTermSetDiscountDays (GncBillTerm *term, gint days)
361 if (term->disc_days == days)
return;
362 gncBillTermBeginEdit (term);
363 term->disc_days = days;
365 gncBillTermCommitEdit (term);
368 void gncBillTermSetDiscount (GncBillTerm *term, gnc_numeric discount)
372 gncBillTermBeginEdit (term);
373 term->discount = discount;
375 gncBillTermCommitEdit (term);
378 void gncBillTermSetCutoff (GncBillTerm *term, gint cutoff)
381 if (term->cutoff == cutoff)
return;
382 gncBillTermBeginEdit (term);
383 term->cutoff = cutoff;
385 gncBillTermCommitEdit (term);
393 void gncBillTermSetParent (GncBillTerm *term, GncBillTerm *parent)
396 gncBillTermBeginEdit (term);
398 gncBillTermRemoveChild(term->parent, term);
399 term->parent = parent;
401 gncBillTermAddChild(parent, term);
403 if ( parent != NULL )
405 gncBillTermMakeInvisible (term);
408 gncBillTermCommitEdit (term);
411 void gncBillTermSetChild (GncBillTerm *term, GncBillTerm *child)
414 gncBillTermBeginEdit (term);
417 gncBillTermCommitEdit (term);
420 void gncBillTermIncRef (GncBillTerm *term)
423 if (term->parent || term->invisible)
return;
424 gncBillTermBeginEdit (term);
427 gncBillTermCommitEdit (term);
430 void gncBillTermDecRef (GncBillTerm *term)
433 if (term->parent || term->invisible)
return;
434 g_return_if_fail (term->refcount >= 1);
435 gncBillTermBeginEdit (term);
438 gncBillTermCommitEdit (term);
441 void gncBillTermSetRefcount (GncBillTerm *term, gint64 refcount)
444 gncBillTermBeginEdit (term);
445 term->refcount = refcount;
447 gncBillTermCommitEdit (term);
450 void gncBillTermMakeInvisible (GncBillTerm *term)
453 gncBillTermBeginEdit (term);
454 term->invisible = TRUE;
457 gncBillTermCommitEdit (term);
460 void gncBillTermChanged (GncBillTerm *term)
466 void gncBillTermBeginEdit (GncBillTerm *term)
473 PERR(
"BillTerm QofBackend Failure: %d", errcode);
474 gnc_engine_signal_commit_error( errcode );
479 GncBillTerm *term = (GncBillTerm *) inst;
480 gncBillTermFree(term);
485 void gncBillTermCommitEdit (GncBillTerm *term)
494 GncBillTerm *gncBillTermLookupByName (QofBook *book,
const char *name)
496 GList *list = gncBillTermGetTerms (book);
498 for ( ; list; list = list->next)
500 GncBillTerm *term = list->data;
501 if (!g_strcmp0 (term->name, name))
507 GList * gncBillTermGetTerms (QofBook *book)
510 if (!book)
return NULL;
516 const char *gncBillTermGetName (
const GncBillTerm *term)
518 if (!term)
return NULL;
522 const char *gncBillTermGetDescription (
const GncBillTerm *term)
524 if (!term)
return NULL;
528 GncBillTermType gncBillTermGetType (
const GncBillTerm *term)
538 const char* qofBillTermGetType (
const GncBillTerm *term)
544 return GncBillTermTypeasString(term->type);
547 gint gncBillTermGetDueDays (
const GncBillTerm *term)
550 return term->due_days;
553 gint gncBillTermGetDiscountDays (
const GncBillTerm *term)
556 return term->disc_days;
559 gnc_numeric gncBillTermGetDiscount (
const GncBillTerm *term)
561 if (!term)
return gnc_numeric_zero ();
562 return term->discount;
565 gint gncBillTermGetCutoff (
const GncBillTerm *term)
571 static GncBillTerm *gncBillTermCopy (
const GncBillTerm *term)
575 if (!term)
return NULL;
578 gncBillTermBeginEdit(t);
580 gncBillTermSetName (t, term->name);
581 gncBillTermSetDescription (t, term->desc);
583 t->type = term->type;
584 t->due_days = term->due_days;
585 t->disc_days = term->disc_days;
586 t->discount = term->discount;
587 t->cutoff = term->cutoff;
590 gncBillTermCommitEdit(t);
595 GncBillTerm *gncBillTermReturnChild (GncBillTerm *term, gboolean make_new)
597 GncBillTerm *child = NULL;
599 if (!term)
return NULL;
600 if (term->child)
return term->child;
601 if (term->parent || term->invisible)
return term;
604 child = gncBillTermCopy (term);
605 gncBillTermSetChild (term, child);
606 gncBillTermSetParent (child, term);
611 GncBillTerm *gncBillTermGetParent (
const GncBillTerm *term)
613 if (!term)
return NULL;
617 gint64 gncBillTermGetRefcount (
const GncBillTerm *term)
620 return term->refcount;
623 gboolean gncBillTermGetInvisible (
const GncBillTerm *term)
625 if (!term)
return FALSE;
626 return term->invisible;
633 if (!a && !b)
return 0;
637 ret = g_strcmp0 (a->name, b->name);
640 return g_strcmp0 (a->desc, b->desc);
645 if (a == NULL && b == NULL)
return TRUE;
646 if (a == NULL || b == NULL)
return FALSE;
648 g_return_val_if_fail(GNC_IS_BILLTERM(a), FALSE);
649 g_return_val_if_fail(GNC_IS_BILLTERM(b), FALSE);
651 if (g_strcmp0(a->name, b->name) != 0)
653 PWARN(
"Names differ: %s vs %s", a->name, b->name);
657 if (g_strcmp0(a->desc, b->desc) != 0)
659 PWARN(
"Descriptions differ: %s vs %s", a->desc, b->desc);
663 if (a->type != b->type)
665 PWARN(
"Types differ");
669 if (a->due_days != b->due_days)
671 PWARN(
"Due days differ: %d vs %d", a->due_days, b->due_days);
675 if (a->disc_days != b->disc_days)
677 PWARN(
"Discount days differ: %d vs %d", a->disc_days, b->disc_days);
683 PWARN(
"Discounts differ");
687 if (a->cutoff != b->cutoff)
689 PWARN(
"Cutoffs differ: %d vs %d", a->cutoff, b->cutoff);
693 if (a->invisible != b->invisible)
695 PWARN(
"Invisible flags differ");
715 gboolean gncBillTermIsDirty (
const GncBillTerm *term)
717 if (!term)
return FALSE;
724 #define SECS_PER_DAY 86400 752 compute_monthyear (
const GncBillTerm *term,
time64 post_date,
753 int *month,
int *year)
755 int iday, imonth, iyear;
757 int cutoff = term->cutoff;
759 g_return_if_fail (term->type == GNC_TERM_TYPE_PROXIMO);
762 imonth = tm.tm_mon + 1;
763 iyear = tm.tm_year + 1900;
785 if (month) *month = imonth;
786 if (year) *year = iyear;
801 compute_time (
const GncBillTerm *term,
time64 post_date,
int days)
804 int day, month, year;
808 case GNC_TERM_TYPE_DAYS:
809 res += (SECS_PER_DAY * days);
812 case GNC_TERM_TYPE_PROXIMO:
813 compute_monthyear (term, post_date, &month, &year);
824 gncBillTermComputeDueDate (
const GncBillTerm *term,
time64 post_date)
826 if (!term)
return post_date;
827 return compute_time (term, post_date, term->due_days);
832 static void _gncBillTermCreate (QofBook *book)
844 destroy_billterm_on_book_close (
QofInstance *ent, gpointer data)
846 GncBillTerm *term = GNC_BILLTERM(ent);
848 gncBillTermBeginEdit (term);
849 gncBillTermDestroy (term);
852 static void _gncBillTermDestroy (QofBook *book)
862 qof_collection_foreach (col, destroy_billterm_on_book_close, NULL);
864 g_list_free (bi->terms);
868 static QofObject gncBillTermDesc =
871 DI(.e_type = ) _GNC_MOD_NAME,
872 DI(.type_label = ) "Billing Term",
873 DI(.create = ) (gpointer)gncBillTermCreate,
874 DI(.book_begin = ) _gncBillTermCreate,
875 DI(.book_end = ) _gncBillTermDestroy,
878 DI(.foreach = ) qof_collection_foreach,
879 DI(.printable = ) NULL,
883 gboolean gncBillTermRegister (
void)
885 static QofParam params[] =
891 { GNC_BILLTERM_DISCDAYS, QOF_TYPE_INT32, (
QofAccessFunc)gncBillTermGetDiscountDays, (
QofSetterFunc)gncBillTermSetDiscountDays },
894 { GNC_BILLTERM_REFCOUNT, QOF_TYPE_INT64, (
QofAccessFunc)gncBillTermGetRefcount, NULL },
int gncBillTermCompare(const GncBillTerm *a, const GncBillTerm *b)
Compare BillTerms on their name for sorting.
int qof_instance_version_cmp(const QofInstance *left, const QofInstance *right)
Compare two instances, based on their last update times.
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
time64 gnc_dmy2time64_neutral(gint day, gint month, gint year)
Converts a day, month, and year to a time64 representing 11:00:00 UTC 11:00:00 UTC falls on the same ...
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
gboolean qof_collection_is_dirty(const QofCollection *col)
Return value of 'dirty' flag on collection.
QofBackendError
The errors that can be reported to the GUI & other front-end users.
#define DEBUG(format, args...)
Print a debugging message.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
void qof_class_register(QofIdTypeConst obj_name, QofSortFunc default_sort_function, const QofParam *params)
This function registers a new object class with the Qof subsystem.
#define ENUM_TERMS_TYPE(_)
How to interpret the amount.
gchar * guid_to_string_buff(const GncGUID *guid, gchar *str)
The guid_to_string_buff() routine puts a null-terminated string encoding of the id into the memory po...
int(* QofSortFunc)(gconstpointer, gconstpointer)
This function is the default sort function for a particular object type.
int gnc_date_get_last_mday(int month, int year)
Get the numerical last date of the month.
#define QOF_OBJECT_VERSION
Defines the version of the core object object registration interface.
gboolean qof_commit_edit(QofInstance *inst)
commit_edit helpers
#define PERR(format, args...)
Log a serious error.
#define QOF_PARAM_BOOK
"Known" Object Parameters – all objects must support these
struct tm * gnc_localtime_r(const time64 *secs, struct tm *time)
fill out a time struct from a 64-bit time value adjusted for the current time zone.
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
gboolean gncBillTermEqual(const GncBillTerm *a, const GncBillTerm *b)
Check if all internal fields of a and b match.
#define PWARN(format, args...)
Log a warning.
gboolean gncBillTermIsFamily(const GncBillTerm *a, const GncBillTerm *b)
Check only if the bill terms are "family".
void qof_instance_init_data(QofInstance *inst, QofIdType type, QofBook *book)
Initialise the settings associated with an instance.
gboolean qof_begin_edit(QofInstance *inst)
begin_edit
#define GUID_ENCODING_LENGTH
Number of characters needed to encode a guid as a string not including the null terminator.
gboolean qof_instance_get_dirty_flag(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object has been modified.
void qof_book_set_data(QofBook *book, const gchar *key, gpointer data)
The qof_book_set_data() allows arbitrary pointers to structs to be stored in QofBook.
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
part2 – deal with the backend
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
void qof_collection_mark_clean(QofCollection *)
reset value of dirty flag
gboolean gnc_numeric_eq(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b are exactly the same (have the same numerator and ...
All type declarations for the whole Gnucash engine.
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
gboolean qof_object_register(const QofObject *object)
Register new types of object objects.
FROM_STRING_DEC(CheckItemType, ENUM_CHECK_ITEM_TYPE)
void qof_event_gen(QofInstance *entity, QofEventId event_id, gpointer event_data)
Invoke all registered event handlers using the given arguments.
gpointer qof_book_get_data(const QofBook *book, const gchar *key)
Retrieves arbitrary pointers to structs stored by qof_book_set_data.
time64 gnc_time64_get_day_neutral(time64 time_val)
The gnc_time64_get_day_neutral() routine will take the given time in seconds and adjust it to 10:59:0...