2003-10-11 22:12:59 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* gncBillTerm.c -- the Gnucash Billing Terms interface *
|
|
|
|
* *
|
|
|
|
* 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 *
|
2005-11-16 23:35:02 -06:00
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
2003-10-11 22:12:59 -05:00
|
|
|
* *
|
|
|
|
\********************************************************************/
|
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002 Derek Atkins
|
2003-10-11 22:12:59 -05:00
|
|
|
* Copyright (C) 2003 Linas Vepstas <linas@linas.org>
|
2002-06-21 11:44:30 -05:00
|
|
|
* Author: Derek Atkins <warlord@MIT.EDU>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2003-06-26 02:27:52 -05:00
|
|
|
#include "gnc-engine.h"
|
2002-06-21 11:44:30 -05:00
|
|
|
#include "gncBillTermP.h"
|
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
struct _gncBillTerm
|
|
|
|
{
|
|
|
|
QofInstance inst;
|
2003-10-22 08:56:48 -05:00
|
|
|
|
|
|
|
/* 'visible' data fields directly manipulated by user */
|
2003-10-11 20:49:08 -05:00
|
|
|
char * name;
|
|
|
|
char * desc;
|
|
|
|
GncBillTermType type;
|
|
|
|
gint due_days;
|
|
|
|
gint disc_days;
|
|
|
|
gnc_numeric discount;
|
|
|
|
gint cutoff;
|
|
|
|
|
2003-10-22 08:56:48 -05:00
|
|
|
/* Internal management fields */
|
2003-10-12 14:08:47 -05:00
|
|
|
/* See src/doc/business.txt for an explanation of the following */
|
2003-10-12 22:56:23 -05:00
|
|
|
/* Code that handles this is *identical* to that in gncTaxTable */
|
2003-10-11 20:49:08 -05:00
|
|
|
gint64 refcount;
|
|
|
|
GncBillTerm * parent; /* if non-null, we are an immutable child */
|
|
|
|
GncBillTerm * child; /* if non-null, we have not changed */
|
|
|
|
gboolean invisible;
|
|
|
|
GList * children; /* list of children for disconnection */
|
2002-06-21 11:44:30 -05:00
|
|
|
};
|
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
struct _book_info
|
|
|
|
{
|
|
|
|
GList * terms; /* visible terms */
|
2002-06-21 11:44:30 -05:00
|
|
|
};
|
2002-11-03 14:21:42 -06:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
static QofLogModule log_module = GNC_MOD_BUSINESS;
|
2002-06-21 11:44:30 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
#define _GNC_MOD_NAME GNC_ID_BILLTERM
|
2002-06-21 11:44:30 -05:00
|
|
|
|
2002-11-03 18:41:01 -06:00
|
|
|
#define SET_STR(obj, member, str) { \
|
2003-10-11 20:49:08 -05:00
|
|
|
char * tmp; \
|
|
|
|
\
|
|
|
|
if (!safe_strcmp (member, str)) return; \
|
|
|
|
gncBillTermBeginEdit (obj); \
|
|
|
|
tmp = CACHE_INSERT (str); \
|
|
|
|
CACHE_REMOVE (member); \
|
|
|
|
member = tmp; \
|
|
|
|
}
|
2002-06-21 11:44:30 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
AS_STRING_DEC(GncBillTermType, ENUM_TERMS_TYPE)
|
|
|
|
FROM_STRING_DEC(GncBillTermType, ENUM_TERMS_TYPE)
|
|
|
|
|
2003-10-11 22:10:44 -05:00
|
|
|
/* ============================================================== */
|
2003-10-14 16:20:55 -05:00
|
|
|
/* Misc inline utilities */
|
2003-07-09 22:18:17 -05:00
|
|
|
|
2003-10-12 14:08:47 -05:00
|
|
|
static inline void
|
2002-06-21 11:44:30 -05:00
|
|
|
mark_term (GncBillTerm *term)
|
|
|
|
{
|
2006-05-02 21:28:18 -05:00
|
|
|
qof_instance_set_dirty(&term->inst);
|
2006-03-08 21:48:49 -06:00
|
|
|
qof_event_gen (&term->inst.entity, QOF_EVENT_MODIFY, NULL);
|
2003-10-11 20:49:08 -05:00
|
|
|
}
|
2002-06-21 11:44:30 -05:00
|
|
|
|
2003-10-12 14:08:47 -05:00
|
|
|
static inline void maybe_resort_list (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
struct _book_info *bi;
|
|
|
|
|
|
|
|
if (term->parent || term->invisible) return;
|
2003-10-14 16:20:55 -05:00
|
|
|
bi = qof_book_get_data (term->inst.book, _GNC_MOD_NAME);
|
2003-10-12 14:08:47 -05:00
|
|
|
bi->terms = g_list_sort (bi->terms, (GCompareFunc)gncBillTermCompare);
|
|
|
|
}
|
|
|
|
|
2003-10-20 08:34:03 -05:00
|
|
|
static inline void addObj (GncBillTerm *term)
|
2003-10-12 14:08:47 -05:00
|
|
|
{
|
|
|
|
struct _book_info *bi;
|
2003-10-14 16:20:55 -05:00
|
|
|
bi = qof_book_get_data (term->inst.book, _GNC_MOD_NAME);
|
2003-10-20 08:34:03 -05:00
|
|
|
bi->terms = g_list_insert_sorted (bi->terms, term,
|
2003-10-12 14:08:47 -05:00
|
|
|
(GCompareFunc)gncBillTermCompare);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
static inline void remObj (GncBillTerm *term)
|
|
|
|
{
|
2003-10-20 08:34:03 -05:00
|
|
|
struct _book_info *bi;
|
|
|
|
bi = qof_book_get_data (term->inst.book, _GNC_MOD_NAME);
|
|
|
|
bi->terms = g_list_remove (bi->terms, term);
|
2003-10-11 20:49:08 -05:00
|
|
|
}
|
|
|
|
|
2003-10-11 22:10:44 -05:00
|
|
|
static inline void
|
|
|
|
gncBillTermAddChild (GncBillTerm *table, GncBillTerm *child)
|
|
|
|
{
|
|
|
|
g_return_if_fail(table->inst.do_free == FALSE);
|
|
|
|
table->children = g_list_prepend(table->children, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
gncBillTermRemoveChild (GncBillTerm *table, GncBillTerm *child)
|
|
|
|
{
|
2003-10-20 08:34:03 -05:00
|
|
|
if (table->inst.do_free) return;
|
2003-10-11 22:10:44 -05:00
|
|
|
table->children = g_list_remove(table->children, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================== */
|
2003-10-11 20:49:08 -05:00
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
/* Create/Destroy Functions */
|
2003-06-26 22:05:25 -05:00
|
|
|
GncBillTerm * gncBillTermCreate (QofBook *book)
|
2002-06-21 11:44:30 -05:00
|
|
|
{
|
|
|
|
GncBillTerm *term;
|
|
|
|
if (!book) return NULL;
|
|
|
|
|
|
|
|
term = g_new0 (GncBillTerm, 1);
|
2003-10-14 16:20:55 -05:00
|
|
|
qof_instance_init(&term->inst, _GNC_MOD_NAME, book);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->name = CACHE_INSERT ("");
|
|
|
|
term->desc = CACHE_INSERT ("");
|
|
|
|
term->discount = gnc_numeric_zero ();
|
|
|
|
addObj (term);
|
2006-03-08 21:48:49 -06:00
|
|
|
qof_event_gen (&term->inst.entity, QOF_EVENT_CREATE, NULL);
|
2002-06-21 11:44:30 -05:00
|
|
|
return term;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermDestroy (GncBillTerm *term)
|
2002-11-03 14:21:42 -06:00
|
|
|
{
|
|
|
|
if (!term) return;
|
2006-01-30 00:43:41 -06:00
|
|
|
DEBUG("destroying bill term %s (%p)",
|
2006-01-29 23:57:08 -06:00
|
|
|
guid_to_string(qof_instance_get_guid(&term->inst)), term);
|
2003-10-11 20:49:08 -05:00
|
|
|
term->inst.do_free = TRUE;
|
2006-05-02 21:28:18 -05:00
|
|
|
qof_instance_set_dirty (&term->inst);
|
2002-11-03 14:21:42 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gncBillTermFree (GncBillTerm *term)
|
2002-06-21 11:44:30 -05:00
|
|
|
{
|
2003-06-14 00:17:07 -05:00
|
|
|
GncBillTerm *child;
|
|
|
|
GList *list;
|
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
if (!term) return;
|
|
|
|
|
2006-03-08 21:48:49 -06:00
|
|
|
qof_event_gen (&term->inst.entity, QOF_EVENT_DESTROY, NULL);
|
2002-06-21 11:44:30 -05:00
|
|
|
CACHE_REMOVE (term->name);
|
|
|
|
CACHE_REMOVE (term->desc);
|
|
|
|
remObj (term);
|
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
if (!term->inst.do_free)
|
2003-06-14 00:17:07 -05:00
|
|
|
PERR("free a billterm without do_free set!");
|
|
|
|
|
2003-07-09 22:18:17 -05:00
|
|
|
/* disconnect from parent */
|
|
|
|
if (term->parent)
|
|
|
|
gncBillTermRemoveChild(term->parent, term);
|
|
|
|
|
2003-06-14 00:17:07 -05:00
|
|
|
/* disconnect from the children */
|
|
|
|
for (list = term->children; list; list=list->next) {
|
|
|
|
child = list->data;
|
|
|
|
gncBillTermSetParent(child, NULL);
|
|
|
|
}
|
|
|
|
g_list_free(term->children);
|
|
|
|
|
2003-10-12 10:28:29 -05:00
|
|
|
qof_instance_release(&term->inst);
|
2002-06-21 11:44:30 -05:00
|
|
|
g_free (term);
|
|
|
|
}
|
|
|
|
|
2003-10-11 22:10:44 -05:00
|
|
|
GncBillTerm *
|
|
|
|
gncCloneBillTerm (GncBillTerm *from, QofBook *book)
|
2003-06-14 00:17:07 -05:00
|
|
|
{
|
2003-10-12 22:56:23 -05:00
|
|
|
GList *node;
|
2003-10-11 22:10:44 -05:00
|
|
|
GncBillTerm *term;
|
2003-06-14 00:17:07 -05:00
|
|
|
|
2003-10-20 08:34:03 -05:00
|
|
|
if (!book || !from) return NULL;
|
2003-06-14 00:17:07 -05:00
|
|
|
|
2003-10-11 22:10:44 -05:00
|
|
|
term = g_new0 (GncBillTerm, 1);
|
2003-10-14 16:20:55 -05:00
|
|
|
qof_instance_init(&term->inst, _GNC_MOD_NAME, book);
|
2003-10-11 22:10:44 -05:00
|
|
|
qof_instance_gemini (&term->inst, &from->inst);
|
2003-06-14 00:17:07 -05:00
|
|
|
|
2003-10-11 22:10:44 -05:00
|
|
|
term->name = CACHE_INSERT (from->name);
|
|
|
|
term->desc = CACHE_INSERT (from->desc);
|
|
|
|
term->type = from->type;
|
|
|
|
term->due_days = from->due_days;
|
|
|
|
term->disc_days = from->disc_days;
|
|
|
|
term->discount = from->discount;
|
|
|
|
term->cutoff = from->cutoff;
|
2003-10-12 14:08:47 -05:00
|
|
|
term->invisible = from->invisible;
|
|
|
|
|
2003-10-12 22:56:23 -05:00
|
|
|
term->refcount = 0;
|
2003-10-12 14:08:47 -05:00
|
|
|
|
|
|
|
/* Make copies of parents and children. Note that this can be
|
2003-10-12 22:56:23 -05:00
|
|
|
* a recursive copy ... treat as doubly-linked list. */
|
2003-10-12 14:08:47 -05:00
|
|
|
if (from->child)
|
|
|
|
{
|
|
|
|
term->child = gncBillTermObtainTwin (from->child, book);
|
|
|
|
term->child->parent = term;
|
|
|
|
}
|
|
|
|
if (from->parent)
|
|
|
|
{
|
|
|
|
term->parent = gncBillTermObtainTwin (from->parent, book);
|
|
|
|
term->parent->child = term;
|
|
|
|
}
|
2003-10-12 22:56:23 -05:00
|
|
|
for (node=g_list_last(from->children); node; node=node->next)
|
|
|
|
{
|
|
|
|
GncBillTerm *btrm = node->data;
|
|
|
|
btrm = gncBillTermObtainTwin (btrm, book);
|
|
|
|
btrm->parent = term;
|
|
|
|
term->children = g_list_prepend(term->children, btrm);
|
|
|
|
}
|
2003-10-11 22:10:44 -05:00
|
|
|
|
|
|
|
addObj (term);
|
2006-03-08 21:48:49 -06:00
|
|
|
qof_event_gen (&term->inst.entity, QOF_EVENT_CREATE, NULL);
|
2003-10-11 22:10:44 -05:00
|
|
|
return term;
|
2003-06-14 00:17:07 -05:00
|
|
|
}
|
|
|
|
|
2003-10-12 14:08:47 -05:00
|
|
|
GncBillTerm *
|
|
|
|
gncBillTermObtainTwin (GncBillTerm *from, QofBook *book)
|
|
|
|
{
|
|
|
|
GncBillTerm *term;
|
|
|
|
if (!from) return NULL;
|
|
|
|
|
|
|
|
term = (GncBillTerm *) qof_instance_lookup_twin (QOF_INSTANCE(from), book);
|
|
|
|
if (!term)
|
|
|
|
{
|
|
|
|
term = gncCloneBillTerm (from, book);
|
|
|
|
}
|
|
|
|
return term;
|
|
|
|
}
|
|
|
|
|
2003-10-11 22:10:44 -05:00
|
|
|
/* ============================================================== */
|
2002-06-21 11:44:30 -05:00
|
|
|
/* Set Functions */
|
2003-10-11 22:10:44 -05:00
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
void gncBillTermSetName (GncBillTerm *term, const char *name)
|
|
|
|
{
|
|
|
|
if (!term || !name) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
SET_STR (term, term->name, name);
|
2002-06-21 11:44:30 -05:00
|
|
|
mark_term (term);
|
2002-06-22 20:23:28 -05:00
|
|
|
maybe_resort_list (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermSetDescription (GncBillTerm *term, const char *desc)
|
|
|
|
{
|
|
|
|
if (!term || !desc) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
SET_STR (term, term->desc, desc);
|
2002-06-21 11:44:30 -05:00
|
|
|
mark_term (term);
|
2002-06-22 20:23:28 -05:00
|
|
|
maybe_resort_list (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermSetType (GncBillTerm *term, GncBillTermType type)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
|
|
|
if (term->type == type) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->type = type;
|
|
|
|
mark_term (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
/** \brief Convert bill term types from text. */
|
|
|
|
FROM_STRING_FUNC(GncBillTermType, ENUM_TERMS_TYPE)
|
|
|
|
|
|
|
|
static
|
|
|
|
void qofBillTermSetType (GncBillTerm *term, const char *type_label)
|
|
|
|
{
|
|
|
|
GncBillTermType type;
|
|
|
|
|
|
|
|
type = GncBillTermTypefromString(type_label);
|
|
|
|
gncBillTermSetType(term, type);
|
|
|
|
}
|
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
void gncBillTermSetDueDays (GncBillTerm *term, gint days)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
|
|
|
if (term->due_days == days) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->due_days = days;
|
|
|
|
mark_term (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermSetDiscountDays (GncBillTerm *term, gint days)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
|
|
|
if (term->disc_days == days) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->disc_days = days;
|
|
|
|
mark_term (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermSetDiscount (GncBillTerm *term, gnc_numeric discount)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
|
|
|
if (gnc_numeric_eq (term->discount, discount)) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->discount = discount;
|
|
|
|
mark_term (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermSetCutoff (GncBillTerm *term, gint cutoff)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
|
|
|
if (term->cutoff == cutoff) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->cutoff = cutoff;
|
|
|
|
mark_term (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
2003-10-12 14:08:47 -05:00
|
|
|
/* XXX this doesn't seem right. If the parent/child relationship
|
|
|
|
* is a doubly-linked list, then there shouldn't be separate set-parent,
|
|
|
|
* set-child routines, else misuse of the routines will goof up
|
|
|
|
* relationships. These ops should be atomic, I think.
|
|
|
|
*/
|
2002-06-21 11:44:30 -05:00
|
|
|
void gncBillTermSetParent (GncBillTerm *term, GncBillTerm *parent)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2003-06-14 00:17:07 -05:00
|
|
|
if (term->parent)
|
|
|
|
gncBillTermRemoveChild(term->parent, term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->parent = parent;
|
2003-06-14 00:17:07 -05:00
|
|
|
if (parent)
|
|
|
|
gncBillTermAddChild(parent, term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->refcount = 0;
|
|
|
|
gncBillTermMakeInvisible (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermSetChild (GncBillTerm *term, GncBillTerm *child)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->child = child;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermIncRef (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
2003-10-11 20:49:08 -05:00
|
|
|
if (term->parent || term->invisible) return; /* children dont need refcounts */
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->refcount++;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermDecRef (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
2003-10-11 20:49:08 -05:00
|
|
|
if (term->parent || term->invisible) return; /* children dont need refcounts */
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->refcount--;
|
|
|
|
g_return_if_fail (term->refcount >= 0);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermSetRefcount (GncBillTerm *term, gint64 refcount)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
|
|
|
term->refcount = refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermMakeInvisible (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermBeginEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
term->invisible = TRUE;
|
2003-10-20 08:34:03 -05:00
|
|
|
remObj (term);
|
2002-11-03 18:41:01 -06:00
|
|
|
gncBillTermCommitEdit (term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncBillTermChanged (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return;
|
|
|
|
term->child = NULL;
|
|
|
|
}
|
|
|
|
|
2002-11-03 14:21:42 -06:00
|
|
|
void gncBillTermBeginEdit (GncBillTerm *term)
|
2002-06-21 11:44:30 -05:00
|
|
|
{
|
2004-06-13 11:48:32 -05:00
|
|
|
QOF_BEGIN_EDIT (&term->inst);
|
2002-11-03 14:21:42 -06:00
|
|
|
}
|
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
static void gncBillTermOnError (QofInstance *inst, QofBackendError errcode)
|
2002-11-03 14:21:42 -06:00
|
|
|
{
|
2003-06-26 22:36:02 -05:00
|
|
|
PERR("BillTerm QofBackend Failure: %d", errcode);
|
2002-11-03 14:21:42 -06:00
|
|
|
}
|
2002-06-21 14:00:34 -05:00
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
static inline void bill_free (QofInstance *inst)
|
2002-11-03 14:21:42 -06:00
|
|
|
{
|
2003-10-11 20:49:08 -05:00
|
|
|
GncBillTerm *term = (GncBillTerm *) inst;
|
|
|
|
gncBillTermFree(term);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
static inline void on_done (QofInstance *inst) {}
|
|
|
|
|
2002-11-03 14:21:42 -06:00
|
|
|
void gncBillTermCommitEdit (GncBillTerm *term)
|
|
|
|
{
|
2006-04-21 23:42:29 -05:00
|
|
|
if (!qof_commit_edit (QOF_INSTANCE(term))) return;
|
2006-02-26 12:36:05 -06:00
|
|
|
qof_commit_edit_part2 (&term->inst, gncBillTermOnError,
|
2003-10-11 20:49:08 -05:00
|
|
|
on_done, bill_free);
|
2002-11-03 14:21:42 -06:00
|
|
|
}
|
2002-06-21 11:44:30 -05:00
|
|
|
|
|
|
|
/* Get Functions */
|
|
|
|
|
2003-06-26 22:05:25 -05:00
|
|
|
GncBillTerm *gncBillTermLookupByName (QofBook *book, const char *name)
|
2002-06-21 11:44:30 -05:00
|
|
|
{
|
|
|
|
GList *list = gncBillTermGetTerms (book);
|
|
|
|
|
|
|
|
for ( ; list; list = list->next) {
|
|
|
|
GncBillTerm *term = list->data;
|
|
|
|
if (!safe_strcmp (term->name, name))
|
|
|
|
return list->data;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-06-26 22:05:25 -05:00
|
|
|
GList * gncBillTermGetTerms (QofBook *book)
|
2002-06-21 11:44:30 -05:00
|
|
|
{
|
|
|
|
struct _book_info *bi;
|
|
|
|
if (!book) return NULL;
|
|
|
|
|
2003-10-14 16:20:55 -05:00
|
|
|
bi = qof_book_get_data (book, _GNC_MOD_NAME);
|
2002-06-21 11:44:30 -05:00
|
|
|
return bi->terms;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *gncBillTermGetName (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return NULL;
|
|
|
|
return term->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *gncBillTermGetDescription (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return NULL;
|
|
|
|
return term->desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
GncBillTermType gncBillTermGetType (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return 0;
|
|
|
|
return term->type;
|
|
|
|
}
|
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
/** \brief Convert bill term types to text. */
|
|
|
|
AS_STRING_FUNC(GncBillTermType, ENUM_TERMS_TYPE)
|
|
|
|
|
|
|
|
static
|
|
|
|
const char* qofBillTermGetType (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) { return NULL; }
|
|
|
|
return GncBillTermTypeasString(term->type);
|
|
|
|
}
|
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
gint gncBillTermGetDueDays (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return 0;
|
|
|
|
return term->due_days;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint gncBillTermGetDiscountDays (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return 0;
|
|
|
|
return term->disc_days;
|
|
|
|
}
|
|
|
|
|
|
|
|
gnc_numeric gncBillTermGetDiscount (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return gnc_numeric_zero ();
|
|
|
|
return term->discount;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint gncBillTermGetCutoff (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return 0;
|
|
|
|
return term->cutoff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GncBillTerm *gncBillTermCopy (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
GncBillTerm *t;
|
|
|
|
|
|
|
|
if (!term) return NULL;
|
2003-10-11 20:49:08 -05:00
|
|
|
t = gncBillTermCreate (term->inst.book);
|
2003-06-17 16:38:41 -05:00
|
|
|
|
|
|
|
gncBillTermBeginEdit(t);
|
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
gncBillTermSetName (t, term->name);
|
|
|
|
gncBillTermSetDescription (t, term->desc);
|
|
|
|
|
2003-06-17 16:38:41 -05:00
|
|
|
t->type = term->type;
|
|
|
|
t->due_days = term->due_days;
|
|
|
|
t->disc_days = term->disc_days;
|
|
|
|
t->discount = term->discount;
|
|
|
|
t->cutoff = term->cutoff;
|
|
|
|
|
|
|
|
gncBillTermCommitEdit(t);
|
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
GncBillTerm *gncBillTermReturnChild (GncBillTerm *term, gboolean make_new)
|
|
|
|
{
|
|
|
|
GncBillTerm *child = NULL;
|
|
|
|
|
|
|
|
if (!term) return NULL;
|
|
|
|
if (term->child) return term->child;
|
2003-06-14 00:17:07 -05:00
|
|
|
if (term->parent || term->invisible) return term;
|
2002-06-21 11:44:30 -05:00
|
|
|
if (make_new) {
|
|
|
|
child = gncBillTermCopy (term);
|
|
|
|
gncBillTermSetChild (term, child);
|
|
|
|
gncBillTermSetParent (child, term);
|
|
|
|
}
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
GncBillTerm *gncBillTermGetParent (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return NULL;
|
|
|
|
return term->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint64 gncBillTermGetRefcount (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return 0;
|
|
|
|
return term->refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncBillTermGetInvisible (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return FALSE;
|
|
|
|
return term->invisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gncBillTermCompare (GncBillTerm *a, GncBillTerm *b)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!a && !b) return 0;
|
|
|
|
if (!a) return -1;
|
|
|
|
if (!b) return 1;
|
|
|
|
|
|
|
|
ret = safe_strcmp (a->name, b->name);
|
2002-06-22 20:23:28 -05:00
|
|
|
if (ret) return ret;
|
2002-06-21 11:44:30 -05:00
|
|
|
|
|
|
|
return safe_strcmp (a->desc, b->desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncBillTermIsDirty (GncBillTerm *term)
|
|
|
|
{
|
|
|
|
if (!term) return FALSE;
|
2003-10-11 20:49:08 -05:00
|
|
|
return term->inst.dirty;
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
2002-06-22 16:55:00 -05:00
|
|
|
/********************************************************/
|
|
|
|
/* functions to compute dates from Bill Terms */
|
|
|
|
|
|
|
|
#define SECS_PER_DAY 86400
|
|
|
|
|
|
|
|
/* Based on the timespec and a proximo type, compute the month and
|
|
|
|
* year this is due. The actual day is filled in below.
|
2003-10-22 08:56:48 -05:00
|
|
|
* XXX explain this, the logic is totally opaque to me.
|
2002-06-22 16:55:00 -05:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
compute_monthyear (GncBillTerm *term, Timespec post_date,
|
2003-10-11 20:49:08 -05:00
|
|
|
int *month, int *year)
|
2002-06-22 16:55:00 -05:00
|
|
|
{
|
|
|
|
int iday, imonth, iyear;
|
|
|
|
int cutoff = term->cutoff;
|
|
|
|
|
|
|
|
g_return_if_fail (term->type == GNC_TERM_TYPE_PROXIMO);
|
|
|
|
|
|
|
|
gnc_timespec2dmy (post_date, &iday, &imonth, &iyear);
|
|
|
|
|
|
|
|
if (cutoff <= 0)
|
|
|
|
cutoff += gnc_timespec_last_mday (post_date);
|
|
|
|
|
|
|
|
if (iday <= cutoff) {
|
|
|
|
/* We apply this to next month */
|
|
|
|
imonth++;
|
|
|
|
} else {
|
|
|
|
/* We apply to the following month */
|
|
|
|
imonth += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imonth > 12) {
|
|
|
|
iyear++;
|
|
|
|
imonth -= 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (month) *month = imonth;
|
|
|
|
if (year) *year = iyear;
|
|
|
|
}
|
|
|
|
|
2003-10-22 08:56:48 -05:00
|
|
|
/* XXX explain this, the logic is totally opaque to me. */
|
|
|
|
|
2002-06-22 16:55:00 -05:00
|
|
|
static Timespec
|
|
|
|
compute_time (GncBillTerm *term, Timespec post_date, int days)
|
|
|
|
{
|
|
|
|
Timespec res = post_date;
|
|
|
|
int day, month, year;
|
|
|
|
|
|
|
|
switch (term->type) {
|
|
|
|
case GNC_TERM_TYPE_DAYS:
|
|
|
|
res.tv_sec += (SECS_PER_DAY * days);
|
|
|
|
break;
|
|
|
|
case GNC_TERM_TYPE_PROXIMO:
|
|
|
|
compute_monthyear (term, post_date, &month, &year);
|
|
|
|
day = gnc_date_my_last_mday (month, year);
|
|
|
|
if (days < day)
|
|
|
|
day = days;
|
|
|
|
res = gnc_dmy2timespec (day, month, year);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timespec
|
|
|
|
gncBillTermComputeDueDate (GncBillTerm *term, Timespec post_date)
|
|
|
|
{
|
|
|
|
Timespec res = post_date;
|
|
|
|
if (!term) return res;
|
|
|
|
|
|
|
|
return compute_time (term, post_date, term->due_days);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timespec
|
|
|
|
gncBillTermComputeDiscountDate (GncBillTerm *term, Timespec post_date)
|
|
|
|
{
|
|
|
|
Timespec res = post_date;
|
|
|
|
if (!term) return res;
|
|
|
|
|
|
|
|
return compute_time (term, post_date, term->disc_days);
|
|
|
|
}
|
|
|
|
|
2002-06-21 11:44:30 -05:00
|
|
|
/* Package-Private functions */
|
|
|
|
|
2003-06-26 22:05:25 -05:00
|
|
|
static void _gncBillTermCreate (QofBook *book)
|
2002-06-21 11:44:30 -05:00
|
|
|
{
|
|
|
|
struct _book_info *bi;
|
|
|
|
|
|
|
|
if (!book) return;
|
|
|
|
|
|
|
|
bi = g_new0 (struct _book_info, 1);
|
2003-10-14 16:20:55 -05:00
|
|
|
qof_book_set_data (book, _GNC_MOD_NAME, bi);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|
|
|
|
|
2003-06-26 22:05:25 -05:00
|
|
|
static void _gncBillTermDestroy (QofBook *book)
|
2002-06-21 11:44:30 -05:00
|
|
|
{
|
|
|
|
struct _book_info *bi;
|
|
|
|
|
|
|
|
if (!book) return;
|
|
|
|
|
2003-10-14 16:20:55 -05:00
|
|
|
bi = qof_book_get_data (book, _GNC_MOD_NAME);
|
2002-06-21 11:44:30 -05:00
|
|
|
|
|
|
|
g_list_free (bi->terms);
|
|
|
|
g_free (bi);
|
|
|
|
}
|
|
|
|
|
2003-10-11 20:49:08 -05:00
|
|
|
static QofObject gncBillTermDesc =
|
|
|
|
{
|
|
|
|
interface_version: QOF_OBJECT_VERSION,
|
2003-10-16 23:32:04 -05:00
|
|
|
e_type: _GNC_MOD_NAME,
|
2003-10-11 20:49:08 -05:00
|
|
|
type_label: "Billing Term",
|
2004-10-16 10:51:26 -05:00
|
|
|
create: (gpointer)gncBillTermCreate,
|
2003-10-11 20:49:08 -05:00
|
|
|
book_begin: _gncBillTermCreate,
|
|
|
|
book_end: _gncBillTermDestroy,
|
2003-10-19 00:13:59 -05:00
|
|
|
is_dirty: qof_collection_is_dirty,
|
|
|
|
mark_clean: qof_collection_mark_clean,
|
|
|
|
foreach: qof_collection_foreach,
|
2004-06-12 15:27:08 -05:00
|
|
|
printable: NULL,
|
|
|
|
version_cmp: (int (*)(gpointer, gpointer)) qof_instance_version_cmp,
|
2002-06-21 11:44:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
gboolean gncBillTermRegister (void)
|
|
|
|
{
|
2003-09-27 05:21:27 -05:00
|
|
|
static QofParam params[] = {
|
2004-10-16 10:51:26 -05:00
|
|
|
{ GNC_BILLTERM_NAME, QOF_TYPE_STRING, (QofAccessFunc)gncBillTermGetName, (QofSetterFunc)gncBillTermSetName },
|
|
|
|
{ GNC_BILLTERM_DESC, QOF_TYPE_STRING, (QofAccessFunc)gncBillTermGetDescription, (QofSetterFunc)gncBillTermSetDescription },
|
2005-11-01 21:32:36 -06:00
|
|
|
{ GNC_BILLTERM_TYPE, QOF_TYPE_STRING, (QofAccessFunc)qofBillTermGetType, (QofSetterFunc)qofBillTermSetType },
|
2004-10-16 10:51:26 -05:00
|
|
|
{ GNC_BILLTERM_DUEDAYS, QOF_TYPE_INT32, (QofAccessFunc)gncBillTermGetDueDays, (QofSetterFunc)gncBillTermSetDueDays },
|
|
|
|
{ GNC_BILLTERM_DISCDAYS, QOF_TYPE_INT32, (QofAccessFunc)gncBillTermGetDiscountDays, (QofSetterFunc)gncBillTermSetDiscountDays },
|
|
|
|
{ GNC_BILLTERM_DISCOUNT, QOF_TYPE_NUMERIC, (QofAccessFunc)gncBillTermGetDiscount, (QofSetterFunc)gncBillTermSetDiscount },
|
|
|
|
{ GNC_BILLTERM_CUTOFF, QOF_TYPE_INT32, (QofAccessFunc)gncBillTermGetCutoff, (QofSetterFunc)gncBillTermSetCutoff },
|
|
|
|
{ GNC_BILLTERM_REFCOUNT, QOF_TYPE_INT64, (QofAccessFunc)gncBillTermGetRefcount, NULL },
|
|
|
|
{ QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL },
|
|
|
|
{ QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL },
|
2002-06-21 11:44:30 -05:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2003-09-27 11:33:06 -05:00
|
|
|
qof_class_register (_GNC_MOD_NAME, (QofSortFunc)gncBillTermCompare, params);
|
2002-06-21 11:44:30 -05:00
|
|
|
|
2003-06-26 22:09:39 -05:00
|
|
|
return qof_object_register (&gncBillTermDesc);
|
2002-06-21 11:44:30 -05:00
|
|
|
}
|