2001-11-16 19:17:06 -06:00
|
|
|
/*
|
|
|
|
* gncVendor.c -- the Core Vendor Interface
|
2002-01-22 11:16:02 -06:00
|
|
|
* Copyright (C) 2001, 2002 Derek Atkins
|
2001-11-16 19:17:06 -06:00
|
|
|
* Author: Derek Atkins <warlord@MIT.EDU>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "guid.h"
|
|
|
|
#include "messages.h"
|
2001-11-21 19:23:07 -06:00
|
|
|
#include "gnc-engine-util.h"
|
2001-11-24 23:34:34 -06:00
|
|
|
#include "gnc-book-p.h"
|
|
|
|
#include "GNCIdP.h"
|
2002-02-03 14:01:08 -06:00
|
|
|
#include "QueryObject.h"
|
2001-11-16 19:17:06 -06:00
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
#include "gncBusiness.h"
|
2001-11-16 19:17:06 -06:00
|
|
|
#include "gncVendor.h"
|
|
|
|
#include "gncVendorP.h"
|
|
|
|
#include "gncAddress.h"
|
|
|
|
|
|
|
|
struct _gncVendor {
|
2001-11-24 23:34:34 -06:00
|
|
|
GNCBook * book;
|
2001-11-16 19:17:06 -06:00
|
|
|
GUID guid;
|
|
|
|
char * id;
|
|
|
|
char * name;
|
|
|
|
char * notes;
|
|
|
|
GncAddress * addr;
|
|
|
|
gint terms;
|
|
|
|
gboolean taxincluded;
|
|
|
|
gboolean active;
|
2002-01-22 11:16:02 -06:00
|
|
|
GList * jobs;
|
2001-11-16 19:17:06 -06:00
|
|
|
gboolean dirty;
|
|
|
|
};
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
#define _GNC_MOD_NAME GNC_VENDOR_MODULE_NAME
|
|
|
|
|
2001-11-16 19:17:06 -06:00
|
|
|
#define CACHE_INSERT(str) g_cache_insert(gnc_engine_get_string_cache(), (gpointer)(str));
|
|
|
|
#define CACHE_REMOVE(str) g_cache_remove(gnc_engine_get_string_cache(), (str));
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
static void addObj (GncVendor *vendor);
|
|
|
|
static void remObj (GncVendor *vendor);
|
|
|
|
|
2001-11-16 19:17:06 -06:00
|
|
|
/* Create/Destroy Functions */
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
GncVendor *gncVendorCreate (GNCBook *book)
|
2001-11-16 19:17:06 -06:00
|
|
|
{
|
|
|
|
GncVendor *vendor;
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
if (!book) return NULL;
|
2001-11-16 19:17:06 -06:00
|
|
|
|
|
|
|
vendor = g_new0 (GncVendor, 1);
|
2001-11-24 23:34:34 -06:00
|
|
|
vendor->book = book;
|
2001-11-16 19:17:06 -06:00
|
|
|
vendor->dirty = FALSE;
|
|
|
|
vendor->id = CACHE_INSERT ("");
|
|
|
|
vendor->name = CACHE_INSERT ("");
|
|
|
|
vendor->notes = CACHE_INSERT ("");
|
2001-11-24 23:34:34 -06:00
|
|
|
vendor->addr = gncAddressCreate (book);
|
2001-11-16 19:17:06 -06:00
|
|
|
vendor->terms = 0;
|
|
|
|
vendor->taxincluded = FALSE;
|
|
|
|
vendor->active = TRUE;
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
xaccGUIDNew (&vendor->guid, book);
|
|
|
|
addObj (vendor);
|
2001-11-16 19:17:06 -06:00
|
|
|
|
|
|
|
return vendor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorDestroy (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return;
|
|
|
|
|
|
|
|
CACHE_REMOVE (vendor->id);
|
|
|
|
CACHE_REMOVE (vendor->name);
|
|
|
|
CACHE_REMOVE (vendor->notes);
|
|
|
|
gncAddressDestroy (vendor->addr);
|
2002-01-22 11:16:02 -06:00
|
|
|
g_list_free (vendor->jobs);
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
remObj (vendor);
|
2001-11-16 19:17:06 -06:00
|
|
|
|
|
|
|
g_free (vendor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Functions */
|
|
|
|
|
|
|
|
#define SET_STR(member, str) { \
|
|
|
|
char * tmp; \
|
|
|
|
\
|
2001-11-21 19:23:07 -06:00
|
|
|
if (!safe_strcmp (member, str)) return; \
|
2001-11-16 19:17:06 -06:00
|
|
|
tmp = CACHE_INSERT (str); \
|
|
|
|
CACHE_REMOVE (member); \
|
|
|
|
member = tmp; \
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorSetID (GncVendor *vendor, const char *id)
|
|
|
|
{
|
|
|
|
if (!vendor) return;
|
|
|
|
if (!id) return;
|
|
|
|
SET_STR(vendor->id, id);
|
|
|
|
vendor->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorSetName (GncVendor *vendor, const char *name)
|
|
|
|
{
|
|
|
|
if (!vendor) return;
|
|
|
|
if (!name) return;
|
|
|
|
SET_STR(vendor->name, name);
|
|
|
|
vendor->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorSetNotes (GncVendor *vendor, const char *notes)
|
|
|
|
{
|
|
|
|
if (!vendor) return;
|
|
|
|
if (!notes) return;
|
|
|
|
SET_STR(vendor->notes, notes);
|
|
|
|
vendor->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorSetGUID (GncVendor *vendor, const GUID *guid)
|
|
|
|
{
|
|
|
|
if (!vendor || !guid) return;
|
|
|
|
if (guid_equal (guid, &vendor->guid)) return;
|
2001-11-24 23:34:34 -06:00
|
|
|
|
|
|
|
remObj (vendor);
|
2001-11-16 19:17:06 -06:00
|
|
|
vendor->guid = *guid;
|
2001-11-24 23:34:34 -06:00
|
|
|
addObj (vendor);
|
2001-11-16 19:17:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorSetTerms (GncVendor *vendor, gint terms)
|
|
|
|
{
|
|
|
|
if (!vendor) return;
|
|
|
|
if (terms == vendor->terms) return;
|
|
|
|
vendor->terms = terms;
|
|
|
|
vendor->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorSetTaxIncluded (GncVendor *vendor, gboolean taxincl)
|
|
|
|
{
|
|
|
|
if (!vendor) return;
|
|
|
|
if (taxincl == vendor->taxincluded) return;
|
|
|
|
vendor->taxincluded = taxincl;
|
|
|
|
vendor->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorSetActive (GncVendor *vendor, gboolean active)
|
|
|
|
{
|
|
|
|
if (!vendor) return;
|
|
|
|
if (active == vendor->active) return;
|
|
|
|
vendor->active = active;
|
|
|
|
vendor->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get Functions */
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
GNCBook * gncVendorGetBook (GncVendor *vendor)
|
2001-11-16 19:17:06 -06:00
|
|
|
{
|
|
|
|
if (!vendor) return NULL;
|
2001-11-24 23:34:34 -06:00
|
|
|
return vendor->book;
|
2001-11-16 19:17:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const GUID * gncVendorGetGUID (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return NULL;
|
|
|
|
return &vendor->guid;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncVendorGetID (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return NULL;
|
|
|
|
return vendor->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncVendorGetName (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return NULL;
|
|
|
|
return vendor->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
GncAddress * gncVendorGetAddr (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return NULL;
|
|
|
|
return vendor->addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncVendorGetNotes (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return NULL;
|
|
|
|
return vendor->notes;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint gncVendorGetTerms (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return 0;
|
|
|
|
return vendor->terms;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncVendorGetTaxIncluded (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return FALSE;
|
|
|
|
return vendor->taxincluded;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncVendorGetActive (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return FALSE;
|
|
|
|
return vendor->active;
|
|
|
|
}
|
|
|
|
|
2002-01-22 11:16:02 -06:00
|
|
|
/* Note that JobList changes do not affect the "dirtiness" of the vendor */
|
|
|
|
void gncVendorAddJob (GncVendor *vendor, GncJob *job)
|
2001-11-24 23:34:34 -06:00
|
|
|
{
|
2002-01-22 11:16:02 -06:00
|
|
|
if (!vendor) return;
|
|
|
|
if (!job) return;
|
|
|
|
|
|
|
|
if (g_list_index(vendor->jobs, job) == -1)
|
|
|
|
vendor->jobs = g_list_insert_sorted (vendor->jobs, job, gncJobSortFunc);
|
2001-11-24 23:34:34 -06:00
|
|
|
}
|
|
|
|
|
2002-01-22 11:16:02 -06:00
|
|
|
void gncVendorRemoveJob (GncVendor *vendor, GncJob *job)
|
2001-11-16 19:17:06 -06:00
|
|
|
{
|
2002-01-22 11:16:02 -06:00
|
|
|
GList *node;
|
|
|
|
|
|
|
|
if (!vendor) return;
|
|
|
|
if (!job) return;
|
|
|
|
|
|
|
|
node = g_list_find (vendor->jobs, job);
|
|
|
|
if (!node) {
|
|
|
|
/* PERR ("split not in account"); */
|
|
|
|
} else {
|
|
|
|
vendor->jobs = g_list_remove_link (vendor->jobs, node);
|
|
|
|
g_list_free_1 (node);
|
|
|
|
}
|
2001-11-16 19:17:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncVendorCommitEdit (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* XXX COMMIT TO DATABASE */
|
|
|
|
vendor->dirty = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Other functions */
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
int gncVendorCompare (GncVendor *a, GncVendor *b)
|
|
|
|
{
|
|
|
|
if (!a && !b) return 0;
|
|
|
|
if (!a && b) return 1;
|
|
|
|
if (a && !b) return -1;
|
|
|
|
|
|
|
|
return(strcmp(a->name, b->name));
|
2001-11-16 19:17:06 -06:00
|
|
|
}
|
|
|
|
|
2002-01-22 11:16:02 -06:00
|
|
|
GList * gncVendorGetJoblist (GncVendor *vendor, gboolean show_all)
|
|
|
|
{
|
|
|
|
if (!vendor) return NULL;
|
|
|
|
|
|
|
|
if (show_all) {
|
|
|
|
return (g_list_copy (vendor->jobs));
|
|
|
|
} else {
|
|
|
|
GList *list = NULL, *iterator;
|
|
|
|
for (iterator = vendor->jobs; iterator; iterator=iterator->next) {
|
|
|
|
GncJob *j = iterator->data;
|
|
|
|
if (gncJobGetActive (j))
|
|
|
|
list = g_list_append (list, j);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GncVendor * gncVendorLookup (GNCBook *book, const GUID *guid)
|
|
|
|
{
|
|
|
|
if (!book || !guid) return NULL;
|
|
|
|
return xaccLookupEntity (gnc_book_get_entity_table (book),
|
|
|
|
guid, _GNC_MOD_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncVendorIsDirty (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
if (!vendor) return FALSE;
|
|
|
|
return (vendor->dirty || gncAddressIsDirty (vendor->addr));
|
|
|
|
}
|
|
|
|
|
2001-11-16 19:17:06 -06:00
|
|
|
/* Package-Private functions */
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
static void addObj (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
xaccStoreEntity (gnc_book_get_entity_table (vendor->book),
|
|
|
|
vendor, &vendor->guid, _GNC_MOD_NAME);
|
|
|
|
|
|
|
|
ht = gnc_book_get_data (vendor->book, _GNC_MOD_NAME);
|
|
|
|
g_hash_table_insert (ht, &vendor->guid, vendor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remObj (GncVendor *vendor)
|
|
|
|
{
|
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
xaccRemoveEntity (gnc_book_get_entity_table (vendor->book), &vendor->guid);
|
|
|
|
ht = gnc_book_get_data (vendor->book, _GNC_MOD_NAME);
|
|
|
|
g_hash_table_remove (ht, &vendor->guid);
|
|
|
|
}
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
static void _gncVendorForeach (GNCBook *book, foreachObjectCB cb,
|
|
|
|
gpointer user_data)
|
2001-11-16 19:17:06 -06:00
|
|
|
{
|
2002-02-03 14:01:08 -06:00
|
|
|
if (!book || !cb) return;
|
|
|
|
gncBusinessForeach (book, _GNC_MOD_NAME, cb, user_data);
|
2001-11-16 19:17:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * _gncVendorPrintable (gpointer item)
|
|
|
|
{
|
|
|
|
GncVendor *v;
|
|
|
|
|
|
|
|
if (!item) return NULL;
|
|
|
|
|
|
|
|
v = item;
|
|
|
|
return v->name;
|
|
|
|
}
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
static void _gncVendorCreate (GNCBook *book)
|
2001-11-16 19:17:06 -06:00
|
|
|
{
|
2001-11-24 23:34:34 -06:00
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
if (!book) return;
|
|
|
|
|
|
|
|
ht = guid_hash_table_new ();
|
|
|
|
gnc_book_set_data (book, _GNC_MOD_NAME, ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _gncVendorDestroy (GNCBook *book)
|
|
|
|
{
|
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
if (!book) return;
|
|
|
|
|
|
|
|
ht = gnc_book_get_data (book, _GNC_MOD_NAME);
|
2001-11-16 19:17:06 -06:00
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
/* XXX : Destroy the objects? */
|
|
|
|
g_hash_table_destroy (ht);
|
2001-11-16 19:17:06 -06:00
|
|
|
}
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
static GncObject_t gncVendorDesc = {
|
|
|
|
GNC_OBJECT_VERSION,
|
2001-11-24 23:34:34 -06:00
|
|
|
_GNC_MOD_NAME,
|
2001-11-16 19:17:06 -06:00
|
|
|
"Vendor",
|
2001-11-24 23:34:34 -06:00
|
|
|
_gncVendorCreate,
|
2001-11-16 19:17:06 -06:00
|
|
|
_gncVendorDestroy,
|
2002-02-03 14:01:08 -06:00
|
|
|
_gncVendorForeach,
|
2001-11-16 19:17:06 -06:00
|
|
|
_gncVendorPrintable
|
|
|
|
};
|
|
|
|
|
|
|
|
gboolean gncVendorRegister (void)
|
|
|
|
{
|
2002-02-03 14:01:08 -06:00
|
|
|
static QueryObjectDef params[] = {
|
|
|
|
{ VENDOR_GUID, QUERYCORE_GUID, (QueryAccess)gncVendorGetGUID },
|
|
|
|
{ VENDOR_ID, QUERYCORE_STRING, (QueryAccess)gncVendorGetID },
|
|
|
|
{ VENDOR_NAME, QUERYCORE_STRING, (QueryAccess)gncVendorGetName },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
static const QueryConvertDef converters[] = {
|
|
|
|
{ GNC_ID_BOOK, (QueryConvert)gncVendorGetBook },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
gncQueryObjectRegister (_GNC_MOD_NAME, (QuerySort)gncVendorCompare,
|
|
|
|
params, converters);
|
|
|
|
|
|
|
|
return gncObjectRegister (&gncVendorDesc);
|
2001-11-16 19:17:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint lastVendor = 17;
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
gint gncVendorNextID (GNCBook *book)
|
2001-11-16 19:17:06 -06:00
|
|
|
{
|
|
|
|
return ++lastVendor; /* XXX: Look into Database! */
|
|
|
|
}
|