2001-11-21 19:23:07 -06:00
|
|
|
/*
|
|
|
|
* gncOrder.c -- the Core Business Order
|
2002-02-03 14:01:08 -06:00
|
|
|
* Copyright (C) 2001,2002 Derek Atkins
|
2001-11-21 19:23:07 -06:00
|
|
|
* Author: Derek Atkins <warlord@MIT.EDU>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "messages.h"
|
|
|
|
#include "gnc-numeric.h"
|
|
|
|
#include "kvp_frame.h"
|
|
|
|
#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-21 19:23:07 -06:00
|
|
|
|
|
|
|
#include "gncBusiness.h"
|
|
|
|
#include "gncEntry.h"
|
|
|
|
#include "gncEntryP.h"
|
|
|
|
#include "gncOrder.h"
|
|
|
|
#include "gncOrderP.h"
|
2001-12-05 23:46:42 -06:00
|
|
|
#include "gncOwner.h"
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
struct _gncOrder {
|
2001-11-24 23:34:34 -06:00
|
|
|
GNCBook *book;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
GUID guid;
|
|
|
|
char * id;
|
|
|
|
char * notes;
|
2001-12-05 23:46:42 -06:00
|
|
|
GncOwner owner;
|
2001-11-21 19:23:07 -06:00
|
|
|
GList * entries;
|
|
|
|
Timespec opened;
|
|
|
|
Timespec closed;
|
|
|
|
gboolean active;
|
|
|
|
|
|
|
|
gboolean dirty;
|
|
|
|
};
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
#define _GNC_MOD_NAME GNC_ORDER_MODULE_NAME
|
|
|
|
|
2001-11-21 19:23:07 -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));
|
|
|
|
|
|
|
|
#define SET_STR(member, str) { \
|
|
|
|
char * tmp; \
|
|
|
|
\
|
|
|
|
if (!safe_strcmp (member, str)) return; \
|
|
|
|
tmp = CACHE_INSERT (str); \
|
|
|
|
CACHE_REMOVE (member); \
|
|
|
|
member = tmp; \
|
|
|
|
}
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
static void addObj (GncOrder *order);
|
|
|
|
static void remObj (GncOrder *order);
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
/* Create/Destroy Functions */
|
|
|
|
|
2001-12-05 23:46:42 -06:00
|
|
|
GncOrder *gncOrderCreate (GNCBook *book)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
GncOrder *order;
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
if (!book) return NULL;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
order = g_new0 (GncOrder, 1);
|
2001-11-24 23:34:34 -06:00
|
|
|
order->book = book;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
order->id = CACHE_INSERT ("");
|
|
|
|
order->notes = CACHE_INSERT ("");
|
|
|
|
|
|
|
|
order->active = TRUE;
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
xaccGUIDNew (&order->guid, book);
|
|
|
|
addObj (order);
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderDestroy (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return;
|
|
|
|
|
|
|
|
g_list_free (order->entries);
|
|
|
|
CACHE_REMOVE (order->id);
|
|
|
|
CACHE_REMOVE (order->notes);
|
2001-11-24 23:34:34 -06:00
|
|
|
remObj (order);
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
g_free (order);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Functions */
|
|
|
|
|
|
|
|
void gncOrderSetGUID (GncOrder *order, const GUID *guid)
|
|
|
|
{
|
|
|
|
if (!order || !guid) return;
|
2001-11-24 23:34:34 -06:00
|
|
|
if (guid_equal (guid, &order->guid)) return;
|
|
|
|
|
|
|
|
remObj (order);
|
2001-11-21 19:23:07 -06:00
|
|
|
order->guid = *guid;
|
2001-11-24 23:34:34 -06:00
|
|
|
addObj (order);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderSetID (GncOrder *order, const char *id)
|
|
|
|
{
|
|
|
|
if (!order || !id) return;
|
|
|
|
SET_STR (order->id, id);
|
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
2001-12-05 23:46:42 -06:00
|
|
|
void gncOrderSetOwner (GncOrder *order, GncOwner *owner)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
2001-12-05 23:46:42 -06:00
|
|
|
if (!order || !owner) return;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
2001-12-05 23:46:42 -06:00
|
|
|
gncOwnerCopy (owner, &order->owner);
|
2001-11-21 19:23:07 -06:00
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderSetDateOpened (GncOrder *order, Timespec *date)
|
|
|
|
{
|
|
|
|
if (!order || !date) return;
|
|
|
|
order->opened = *date;
|
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderSetDateClosed (GncOrder *order, Timespec *date)
|
|
|
|
{
|
|
|
|
if (!order || !date) return;
|
|
|
|
order->closed = *date;
|
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderSetNotes (GncOrder *order, const char *notes)
|
|
|
|
{
|
|
|
|
if (!order || !notes) return;
|
|
|
|
SET_STR (order->notes, notes);
|
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderSetActive (GncOrder *order, gboolean active)
|
|
|
|
{
|
|
|
|
if (!order) return;
|
|
|
|
order->active = active;
|
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderSetDirty (GncOrder *order, gboolean dirty)
|
|
|
|
{
|
|
|
|
if (!order) return;
|
|
|
|
order->dirty = dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an Entry to the Order */
|
|
|
|
void gncOrderAddEntry (GncOrder *order, GncEntry *entry)
|
|
|
|
{
|
|
|
|
GncOrder *old;
|
|
|
|
|
|
|
|
if (!order || !entry) return;
|
|
|
|
|
|
|
|
old = gncEntryGetOrder (entry);
|
|
|
|
if (old == order) return; /* I already own it */
|
|
|
|
if (old) gncOrderRemoveEntry (old, entry);
|
|
|
|
|
|
|
|
gncEntrySetOrder (entry, order);
|
|
|
|
order->entries = g_list_append (order->entries, entry);
|
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderRemoveEntry (GncOrder *order, GncEntry *entry)
|
|
|
|
{
|
|
|
|
if (!order || !entry) return;
|
|
|
|
|
|
|
|
gncEntrySetOrder (entry, NULL);
|
|
|
|
order->entries = g_list_remove (order->entries, entry);
|
|
|
|
order->dirty = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get Functions */
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
GNCBook * gncOrderGetBook (GncOrder *order)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
if (!order) return NULL;
|
2001-11-24 23:34:34 -06:00
|
|
|
return order->book;
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const GUID * gncOrderGetGUID (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return NULL;
|
|
|
|
return &(order->guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncOrderGetID (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return NULL;
|
|
|
|
return order->id;
|
|
|
|
}
|
|
|
|
|
2001-12-05 23:46:42 -06:00
|
|
|
GncOwner * gncOrderGetOwner (GncOrder *order)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
if (!order) return NULL;
|
2001-12-05 23:46:42 -06:00
|
|
|
return &order->owner;
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Timespec gncOrderGetDateOpened (GncOrder *order)
|
|
|
|
{
|
|
|
|
Timespec ts; ts.tv_sec = 0; ts.tv_nsec = 0;
|
|
|
|
if (!order) return ts;
|
|
|
|
return order->opened;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timespec gncOrderGetDateClosed (GncOrder *order)
|
|
|
|
{
|
|
|
|
Timespec ts; ts.tv_sec = 0; ts.tv_nsec = 0;
|
|
|
|
if (!order) return ts;
|
|
|
|
return order->closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncOrderGetNotes (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return NULL;
|
|
|
|
return order->notes;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncOrderGetActive (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return FALSE;
|
|
|
|
return order->active;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the list Entries */
|
|
|
|
GList * gncOrderGetEntries (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return NULL;
|
|
|
|
return order->entries;
|
|
|
|
}
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
GncOrder * gncOrderLookup (GNCBook *book, const GUID *guid)
|
|
|
|
{
|
|
|
|
if (!book || !guid) return NULL;
|
|
|
|
return xaccLookupEntity (gnc_book_get_entity_table (book),
|
|
|
|
guid, _GNC_MOD_NAME);
|
|
|
|
}
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
gboolean gncOrderIsDirty (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return FALSE;
|
|
|
|
return order->dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderBeginEdit (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncOrderCommitEdit (GncOrder *order)
|
|
|
|
{
|
|
|
|
if (!order) return;
|
|
|
|
}
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
int gncOrderCompare (GncOrder *a, GncOrder *b)
|
|
|
|
{
|
|
|
|
int compare;
|
|
|
|
|
|
|
|
if (a == b) return 0;
|
|
|
|
if (!a && b) return -1;
|
|
|
|
if (a && !b) return 1;
|
|
|
|
|
|
|
|
compare = safe_strcmp (a->id, b->id);
|
|
|
|
if (!compare) return compare;
|
|
|
|
|
|
|
|
compare = timespec_cmp (&(a->opened), &(b->opened));
|
|
|
|
if (!compare) return compare;
|
|
|
|
|
|
|
|
compare = timespec_cmp (&(a->closed), &(b->closed));
|
|
|
|
if (!compare) return compare;
|
|
|
|
|
|
|
|
return guid_compare (&(a->guid), &(b->guid));
|
|
|
|
}
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
/* Package-Private functions */
|
|
|
|
|
|
|
|
static void addObj (GncOrder *order)
|
|
|
|
{
|
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
xaccStoreEntity (gnc_book_get_entity_table (order->book),
|
|
|
|
order, &order->guid, _GNC_MOD_NAME);
|
|
|
|
|
|
|
|
ht = gnc_book_get_data (order->book, _GNC_MOD_NAME);
|
|
|
|
g_hash_table_insert (ht, &order->guid, order);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remObj (GncOrder *order)
|
|
|
|
{
|
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
xaccRemoveEntity (gnc_book_get_entity_table (order->book), &order->guid);
|
|
|
|
ht = gnc_book_get_data (order->book, _GNC_MOD_NAME);
|
|
|
|
g_hash_table_remove (ht, &order->guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _gncOrderCreate (GNCBook *book)
|
|
|
|
{
|
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
if (!book) return;
|
|
|
|
|
|
|
|
ht = guid_hash_table_new ();
|
|
|
|
gnc_book_set_data (book, _GNC_MOD_NAME, ht);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _gncOrderDestroy (GNCBook *book)
|
|
|
|
{
|
|
|
|
GHashTable *ht;
|
|
|
|
|
|
|
|
if (!book) return;
|
|
|
|
|
|
|
|
ht = gnc_book_get_data (book, _GNC_MOD_NAME);
|
|
|
|
|
|
|
|
/* XXX : Destroy the objects? */
|
|
|
|
g_hash_table_destroy (ht);
|
|
|
|
}
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
static void _gncOrderForeach (GNCBook *book, foreachObjectCB cb,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
if (!book || !cb) return;
|
|
|
|
gncBusinessForeach (book, _GNC_MOD_NAME, cb, user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GncObject_t gncOrderDesc = {
|
|
|
|
GNC_OBJECT_VERSION,
|
2001-11-24 23:34:34 -06:00
|
|
|
_GNC_MOD_NAME,
|
2001-11-21 19:23:07 -06:00
|
|
|
"Purchase/Sales Order",
|
2001-11-24 23:34:34 -06:00
|
|
|
_gncOrderCreate,
|
|
|
|
_gncOrderDestroy,
|
2002-02-03 14:01:08 -06:00
|
|
|
_gncOrderForeach,
|
2001-11-21 19:23:07 -06:00
|
|
|
NULL /* printable */
|
|
|
|
};
|
|
|
|
|
|
|
|
gboolean gncOrderRegister (void)
|
|
|
|
{
|
2002-02-03 14:01:08 -06:00
|
|
|
static QueryObjectDef params[] = {
|
|
|
|
{ ORDER_GUID, QUERYCORE_GUID, (QueryAccess)gncOrderGetGUID },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
static const QueryConvertDef converters[] = {
|
|
|
|
{ GNC_ID_BOOK, (QueryConvert)gncOrderGetBook },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
gncQueryObjectRegister (_GNC_MOD_NAME, (QuerySort)gncOrderCompare,
|
|
|
|
params, converters);
|
|
|
|
|
|
|
|
return gncObjectRegister (&gncOrderDesc);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint lastId = 471; /* XXX */
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
gint gncOrderNextID (GNCBook *book)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
return lastId++;
|
|
|
|
}
|