2001-11-21 19:23:07 -06:00
|
|
|
/*
|
|
|
|
* gncInvoice.c -- the Core Business Invoice
|
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 "Transaction.h"
|
|
|
|
#include "Account.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"
|
2002-03-02 12:55:01 -06:00
|
|
|
#include "gnc-event-p.h"
|
2002-05-22 21:47:46 -05:00
|
|
|
#include "gnc-lot.h"
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
#include "gncBusiness.h"
|
|
|
|
#include "gncEntry.h"
|
|
|
|
#include "gncEntryP.h"
|
|
|
|
#include "gncInvoice.h"
|
|
|
|
#include "gncInvoiceP.h"
|
2001-12-05 23:46:42 -06:00
|
|
|
#include "gncOwner.h"
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
struct _gncInvoice {
|
2001-11-24 23:34:34 -06:00
|
|
|
GNCBook *book;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
GUID guid;
|
|
|
|
char * id;
|
|
|
|
char * notes;
|
2002-05-08 15:42:38 -05:00
|
|
|
char * billing_id;
|
2002-02-23 22:01:57 -06:00
|
|
|
char * printname;
|
2002-06-21 21:38:13 -05:00
|
|
|
GncBillTerm * terms;
|
2001-11-21 19:23:07 -06:00
|
|
|
GList * entries;
|
2001-12-05 23:46:42 -06:00
|
|
|
GncOwner owner;
|
2002-05-08 15:42:38 -05:00
|
|
|
GncJob * job;
|
2001-11-21 19:23:07 -06:00
|
|
|
Timespec date_opened;
|
2002-02-10 19:59:54 -06:00
|
|
|
Timespec date_posted;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
2002-03-04 13:34:52 -06:00
|
|
|
gnc_commodity * common_commodity;
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
Account * posted_acc;
|
|
|
|
Transaction * posted_txn;
|
2002-06-24 17:30:36 -05:00
|
|
|
GNCLot * posted_lot;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
gboolean active;
|
|
|
|
|
|
|
|
gboolean dirty;
|
|
|
|
};
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
#define _GNC_MOD_NAME GNC_INVOICE_MODULE_NAME
|
2001-11-24 23:34:34 -06:00
|
|
|
|
2001-11-23 23:35:08 -06:00
|
|
|
#define GNC_INVOICE_ID "gncInvoice"
|
|
|
|
#define GNC_INVOICE_GUID "invoice-guid"
|
|
|
|
|
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 (GncInvoice *invoice);
|
|
|
|
static void remObj (GncInvoice *invoice);
|
|
|
|
|
2002-06-21 14:00:34 -05:00
|
|
|
static void mark_invoice (GncInvoice *invoice);
|
|
|
|
static void
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
invoice->dirty = TRUE;
|
|
|
|
|
|
|
|
gnc_engine_generate_event (&invoice->guid, GNC_EVENT_MODIFY);
|
|
|
|
}
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
/* Create/Destroy Functions */
|
|
|
|
|
2001-12-05 23:46:42 -06:00
|
|
|
GncInvoice *gncInvoiceCreate (GNCBook *book)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
GncInvoice *invoice;
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
if (!book) return NULL;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
invoice = g_new0 (GncInvoice, 1);
|
2001-11-24 23:34:34 -06:00
|
|
|
invoice->book = book;
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
invoice->id = CACHE_INSERT ("");
|
|
|
|
invoice->notes = CACHE_INSERT ("");
|
2002-05-08 15:42:38 -05:00
|
|
|
invoice->billing_id = CACHE_INSERT ("");
|
2001-11-21 19:23:07 -06:00
|
|
|
|
|
|
|
invoice->active = TRUE;
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
xaccGUIDNew (&invoice->guid, book);
|
|
|
|
addObj (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
|
2002-03-02 12:55:01 -06:00
|
|
|
gnc_engine_generate_event (&invoice->guid, GNC_EVENT_CREATE);
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
return invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gncInvoiceDestroy (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
|
|
|
|
2002-03-02 12:55:01 -06:00
|
|
|
gnc_engine_generate_event (&invoice->guid, GNC_EVENT_DESTROY);
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
CACHE_REMOVE (invoice->id);
|
|
|
|
CACHE_REMOVE (invoice->notes);
|
2002-05-08 15:42:38 -05:00
|
|
|
CACHE_REMOVE (invoice->billing_id);
|
2001-11-21 19:23:07 -06:00
|
|
|
g_list_free (invoice->entries);
|
2001-11-24 23:34:34 -06:00
|
|
|
remObj (invoice);
|
|
|
|
|
2002-02-23 22:01:57 -06:00
|
|
|
if (invoice->printname) g_free (invoice->printname);
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
g_free (invoice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Functions */
|
|
|
|
|
|
|
|
void gncInvoiceSetGUID (GncInvoice *invoice, const GUID *guid)
|
|
|
|
{
|
|
|
|
if (!invoice || !guid) return;
|
2001-11-24 23:34:34 -06:00
|
|
|
if (guid_equal (guid, &invoice->guid)) return;
|
|
|
|
|
|
|
|
remObj (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
invoice->guid = *guid;
|
2001-11-24 23:34:34 -06:00
|
|
|
addObj (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncInvoiceSetID (GncInvoice *invoice, const char *id)
|
|
|
|
{
|
|
|
|
if (!invoice || !id) return;
|
|
|
|
SET_STR (invoice->id, id);
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2001-12-05 23:46:42 -06:00
|
|
|
void gncInvoiceSetOwner (GncInvoice *invoice, GncOwner *owner)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
2001-12-05 23:46:42 -06:00
|
|
|
if (!invoice || !owner) return;
|
2002-06-21 14:00:34 -05:00
|
|
|
if (gncOwnerEqual (&invoice->owner, owner)) return;
|
2001-12-05 23:46:42 -06:00
|
|
|
gncOwnerCopy (owner, &invoice->owner);
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-02-26 17:42:11 -06:00
|
|
|
void gncInvoiceSetDateOpened (GncInvoice *invoice, Timespec date)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
2002-02-26 17:42:11 -06:00
|
|
|
if (!invoice) return;
|
2002-06-21 14:00:34 -05:00
|
|
|
if (timespec_equal (&invoice->date_opened, &date)) return;
|
2002-02-26 17:42:11 -06:00
|
|
|
invoice->date_opened = date;
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-02-26 17:42:11 -06:00
|
|
|
void gncInvoiceSetDatePosted (GncInvoice *invoice, Timespec date)
|
2002-02-10 19:59:54 -06:00
|
|
|
{
|
2002-02-26 17:42:11 -06:00
|
|
|
if (!invoice) return;
|
2002-06-21 14:00:34 -05:00
|
|
|
if (timespec_equal (&invoice->date_posted, &date)) return;
|
2002-02-26 17:42:11 -06:00
|
|
|
invoice->date_posted = date;
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2002-02-10 19:59:54 -06:00
|
|
|
}
|
|
|
|
|
2002-06-21 21:38:13 -05:00
|
|
|
void gncInvoiceSetTerms (GncInvoice *invoice, GncBillTerm *terms)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
2002-02-26 17:42:11 -06:00
|
|
|
if (!invoice) return;
|
2002-06-21 21:38:13 -05:00
|
|
|
if (invoice->terms == terms) return;
|
|
|
|
if (invoice->terms)
|
|
|
|
gncBillTermDecRef (invoice->terms);
|
|
|
|
invoice->terms = terms;
|
|
|
|
if (invoice->terms)
|
|
|
|
gncBillTermIncRef (invoice->terms);
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-05-08 15:42:38 -05:00
|
|
|
void gncInvoiceSetBillingID (GncInvoice *invoice, const char *billing_id)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
if (!invoice) return;
|
2002-05-08 15:42:38 -05:00
|
|
|
SET_STR (invoice->billing_id, billing_id);
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncInvoiceSetNotes (GncInvoice *invoice, const char *notes)
|
|
|
|
{
|
|
|
|
if (!invoice || !notes) return;
|
|
|
|
SET_STR (invoice->notes, notes);
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncInvoiceSetActive (GncInvoice *invoice, gboolean active)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
2002-06-21 14:00:34 -05:00
|
|
|
if (invoice->active == active) return;
|
2001-11-21 19:23:07 -06:00
|
|
|
invoice->active = active;
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-03-04 13:34:52 -06:00
|
|
|
void gncInvoiceSetCommonCommodity (GncInvoice *invoice, gnc_commodity *com)
|
|
|
|
{
|
|
|
|
if (!invoice || !com) return;
|
2002-06-21 14:00:34 -05:00
|
|
|
if (invoice->common_commodity &&
|
|
|
|
gnc_commodity_equal (invoice->common_commodity, com))
|
|
|
|
return;
|
2002-03-04 13:34:52 -06:00
|
|
|
invoice->common_commodity = com;
|
|
|
|
mark_invoice (invoice);
|
|
|
|
}
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
void gncInvoiceSetDirty (GncInvoice *invoice, gboolean dirty)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
|
|
|
invoice->dirty = dirty;
|
|
|
|
}
|
|
|
|
|
2001-11-23 23:35:08 -06:00
|
|
|
void gncInvoiceSetPostedTxn (GncInvoice *invoice, Transaction *txn)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
2002-06-21 14:00:34 -05:00
|
|
|
g_return_if_fail (invoice->posted_txn == NULL);
|
2001-11-23 23:35:08 -06:00
|
|
|
|
|
|
|
invoice->posted_txn = txn;
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-23 23:35:08 -06:00
|
|
|
}
|
|
|
|
|
2002-06-24 17:30:36 -05:00
|
|
|
void gncInvoiceSetPostedLot (GncInvoice *invoice, GNCLot *lot)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
|
|
|
g_return_if_fail (invoice->posted_lot == NULL);
|
|
|
|
|
|
|
|
invoice->posted_lot = lot;
|
|
|
|
mark_invoice (invoice);
|
|
|
|
}
|
|
|
|
|
2001-11-23 23:35:08 -06:00
|
|
|
void gncInvoiceSetPostedAcc (GncInvoice *invoice, Account *acc)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
2002-06-21 14:00:34 -05:00
|
|
|
g_return_if_fail (invoice->posted_acc == NULL);
|
2001-11-23 23:35:08 -06:00
|
|
|
|
|
|
|
invoice->posted_acc = acc;
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-23 23:35:08 -06:00
|
|
|
}
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
void gncInvoiceAddEntry (GncInvoice *invoice, GncEntry *entry)
|
|
|
|
{
|
|
|
|
GncInvoice *old;
|
|
|
|
|
|
|
|
if (!invoice || !entry) return;
|
|
|
|
|
|
|
|
old = gncEntryGetInvoice (entry);
|
|
|
|
if (old == invoice) return; /* I already own this one */
|
|
|
|
if (old) gncInvoiceRemoveEntry (old, entry);
|
|
|
|
|
|
|
|
gncEntrySetInvoice (entry, invoice);
|
2002-02-27 23:10:07 -06:00
|
|
|
invoice->entries = g_list_insert_sorted (invoice->entries, entry,
|
|
|
|
(GCompareFunc)gncEntryCompare);
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void gncInvoiceRemoveEntry (GncInvoice *invoice, GncEntry *entry)
|
|
|
|
{
|
|
|
|
if (!invoice || !entry) return;
|
|
|
|
|
|
|
|
gncEntrySetInvoice (entry, NULL);
|
|
|
|
invoice->entries = g_list_remove (invoice->entries, entry);
|
2002-03-02 12:55:01 -06:00
|
|
|
mark_invoice (invoice);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get Functions */
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
GNCBook * gncInvoiceGetBook (GncInvoice *invoice)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
2001-11-24 23:34:34 -06:00
|
|
|
return invoice->book;
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const GUID * gncInvoiceGetGUID (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return &(invoice->guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncInvoiceGetID (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return invoice->id;
|
|
|
|
}
|
|
|
|
|
2001-12-05 23:46:42 -06:00
|
|
|
GncOwner * gncInvoiceGetOwner (GncInvoice *invoice)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
2001-12-05 23:46:42 -06:00
|
|
|
return &invoice->owner;
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Timespec gncInvoiceGetDateOpened (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
Timespec ts; ts.tv_sec = 0; ts.tv_nsec = 0;
|
|
|
|
if (!invoice) return ts;
|
|
|
|
return invoice->date_opened;
|
|
|
|
}
|
|
|
|
|
2002-02-10 19:59:54 -06:00
|
|
|
Timespec gncInvoiceGetDatePosted (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
Timespec ts; ts.tv_sec = 0; ts.tv_nsec = 0;
|
|
|
|
if (!invoice) return ts;
|
|
|
|
return invoice->date_posted;
|
|
|
|
}
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
Timespec gncInvoiceGetDateDue (GncInvoice *invoice)
|
|
|
|
{
|
2002-05-08 15:42:38 -05:00
|
|
|
Transaction *txn;
|
2001-11-21 19:23:07 -06:00
|
|
|
Timespec ts; ts.tv_sec = 0; ts.tv_nsec = 0;
|
|
|
|
if (!invoice) return ts;
|
2002-05-08 15:42:38 -05:00
|
|
|
txn = gncInvoiceGetPostedTxn (invoice);
|
|
|
|
if (!txn) return ts;
|
|
|
|
return xaccTransRetDateDueTS (txn);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-06-21 21:38:13 -05:00
|
|
|
GncBillTerm * gncInvoiceGetTerms (GncInvoice *invoice)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
2002-05-08 15:42:38 -05:00
|
|
|
if (!invoice) return 0;
|
|
|
|
return invoice->terms;
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-05-08 15:42:38 -05:00
|
|
|
const char * gncInvoiceGetBillingID (GncInvoice *invoice)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
|
|
|
if (!invoice) return 0;
|
2002-05-08 15:42:38 -05:00
|
|
|
return invoice->billing_id;
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncInvoiceGetNotes (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return invoice->notes;
|
|
|
|
}
|
|
|
|
|
2002-05-16 17:45:50 -05:00
|
|
|
static GncOwnerType gncInvoiceGetOwnerType (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
GncOwner *owner;
|
|
|
|
g_return_val_if_fail (invoice, GNC_OWNER_NONE);
|
|
|
|
|
|
|
|
owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
|
|
|
|
return (gncOwnerGetType (owner));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * gncInvoiceGetType (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
|
|
|
|
switch (gncInvoiceGetOwnerType (invoice)) {
|
|
|
|
case GNC_OWNER_CUSTOMER:
|
|
|
|
return _("Invoice");
|
|
|
|
case GNC_OWNER_VENDOR:
|
|
|
|
return _("Bill");
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-04 13:34:52 -06:00
|
|
|
gnc_commodity * gncInvoiceGetCommonCommodity (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return invoice->common_commodity;
|
|
|
|
}
|
|
|
|
|
2002-06-25 10:08:50 -05:00
|
|
|
GNCLot * gncInvoiceGetPostedLot (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return invoice->posted_lot;
|
|
|
|
}
|
|
|
|
|
2001-11-23 23:35:08 -06:00
|
|
|
Transaction * gncInvoiceGetPostedTxn (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return invoice->posted_txn;
|
|
|
|
}
|
|
|
|
|
|
|
|
Account * gncInvoiceGetPostedAcc (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return invoice->posted_acc;
|
|
|
|
}
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
gboolean gncInvoiceGetActive (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return FALSE;
|
|
|
|
return invoice->active;
|
|
|
|
}
|
|
|
|
|
|
|
|
GList * gncInvoiceGetEntries (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return NULL;
|
|
|
|
return invoice->entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncInvoiceIsDirty (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return FALSE;
|
|
|
|
return invoice->dirty;
|
|
|
|
}
|
|
|
|
|
2002-06-24 17:30:36 -05:00
|
|
|
static void
|
|
|
|
gncInvoiceAttachInvoiceToLot (GncInvoice *invoice, GNCLot *lot)
|
|
|
|
{
|
|
|
|
kvp_frame *kvp;
|
|
|
|
kvp_value *value;
|
|
|
|
|
|
|
|
if (!invoice || !lot)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (invoice->posted_lot) return; /* Cannot reset invoice's lot */
|
|
|
|
|
|
|
|
kvp = gnc_lot_get_slots (lot);
|
|
|
|
value = kvp_value_new_guid (gncInvoiceGetGUID (invoice));
|
|
|
|
kvp_frame_set_slot_path (kvp, value, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
|
|
|
|
kvp_value_delete (value);
|
|
|
|
gncInvoiceSetPostedLot (invoice, lot);
|
|
|
|
}
|
|
|
|
|
|
|
|
GncInvoice * gncInvoiceGetInvoiceFromLot (GNCLot *lot)
|
|
|
|
{
|
|
|
|
kvp_frame *kvp;
|
|
|
|
kvp_value *value;
|
|
|
|
GUID *guid;
|
|
|
|
GNCBook *book;
|
|
|
|
|
|
|
|
if (!lot) return NULL;
|
|
|
|
|
|
|
|
book = gnc_lot_get_book (lot);
|
|
|
|
kvp = gnc_lot_get_slots (lot);
|
|
|
|
value = kvp_frame_get_slot_path (kvp, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
|
|
|
|
if (!value) return NULL;
|
|
|
|
|
|
|
|
guid = kvp_value_get_guid (value);
|
|
|
|
|
|
|
|
return xaccLookupEntity (gnc_book_get_entity_table (book),
|
|
|
|
guid, _GNC_MOD_NAME);
|
|
|
|
}
|
|
|
|
|
2002-02-10 19:59:54 -06:00
|
|
|
static void
|
2002-05-08 15:42:38 -05:00
|
|
|
gncInvoiceAttachInvoiceToTxn (GncInvoice *invoice, Transaction *txn)
|
2001-11-23 23:35:08 -06:00
|
|
|
{
|
|
|
|
kvp_frame *kvp;
|
|
|
|
kvp_value *value;
|
|
|
|
|
|
|
|
if (!invoice || !txn)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (invoice->posted_txn) return; /* Cannot reset invoice's txn */
|
|
|
|
|
|
|
|
xaccTransBeginEdit (txn);
|
|
|
|
kvp = xaccTransGetSlots (txn);
|
|
|
|
value = kvp_value_new_guid (gncInvoiceGetGUID (invoice));
|
|
|
|
kvp_frame_set_slot_path (kvp, value, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
|
|
|
|
kvp_value_delete (value);
|
2002-05-08 15:42:38 -05:00
|
|
|
xaccTransSetTxnType (txn, TXN_TYPE_INVOICE);
|
2001-11-23 23:35:08 -06:00
|
|
|
xaccTransCommitEdit (txn);
|
2002-05-08 15:42:38 -05:00
|
|
|
gncInvoiceSetPostedTxn (invoice, txn);
|
2001-11-23 23:35:08 -06:00
|
|
|
}
|
|
|
|
|
2002-06-24 17:30:36 -05:00
|
|
|
GncInvoice * gncInvoiceGetInvoiceFromTxn (Transaction *txn)
|
|
|
|
{
|
|
|
|
kvp_frame *kvp;
|
|
|
|
kvp_value *value;
|
|
|
|
GUID *guid;
|
|
|
|
GNCBook *book;
|
|
|
|
|
|
|
|
if (!txn) return NULL;
|
|
|
|
|
|
|
|
book = xaccTransGetBook (txn);
|
|
|
|
kvp = xaccTransGetSlots (txn);
|
|
|
|
value = kvp_frame_get_slot_path (kvp, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
|
|
|
|
if (!value) return NULL;
|
|
|
|
|
|
|
|
guid = kvp_value_get_guid (value);
|
|
|
|
|
|
|
|
return xaccLookupEntity (gnc_book_get_entity_table (book),
|
|
|
|
guid, _GNC_MOD_NAME);
|
|
|
|
}
|
|
|
|
|
2002-06-25 10:08:50 -05:00
|
|
|
struct lotmatch {
|
|
|
|
GncOwner *owner;
|
|
|
|
gboolean reverse;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gnc_lot_match_owner_payment (GNCLot *lot, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct lotmatch *lm = user_data;
|
|
|
|
GncOwner owner_def, *owner;
|
|
|
|
gnc_numeric balance = gnc_lot_get_balance (lot);
|
|
|
|
|
|
|
|
/* Is this a payment lot */
|
|
|
|
if (gnc_numeric_positive_p (lm->reverse ? balance :
|
|
|
|
gnc_numeric_neg (balance)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Is there an invoice attached? */
|
|
|
|
if (gncInvoiceGetInvoiceFromLot (lot))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Is it ours? */
|
|
|
|
if (!gncOwnerGetOwnerFromLot (lot, &owner_def))
|
|
|
|
return FALSE;
|
|
|
|
owner = gncOwnerGetEndOwner (&owner_def);
|
|
|
|
|
|
|
|
return gncOwnerEqual (owner, lm->owner);
|
|
|
|
}
|
|
|
|
|
2001-11-23 23:35:08 -06:00
|
|
|
Transaction * gncInvoicePostToAccount (GncInvoice *invoice, Account *acc,
|
2002-05-08 15:42:38 -05:00
|
|
|
Timespec *post_date, Timespec *due_date,
|
2002-05-16 17:45:50 -05:00
|
|
|
const char * memo)
|
2001-11-23 23:35:08 -06:00
|
|
|
{
|
|
|
|
Transaction *txn;
|
2002-06-25 10:08:50 -05:00
|
|
|
GNCLot *lot = NULL;
|
2001-12-07 21:17:51 -06:00
|
|
|
GList *iter;
|
2001-11-23 23:35:08 -06:00
|
|
|
GList *splitinfo = NULL;
|
|
|
|
gnc_numeric total;
|
2002-05-16 17:45:50 -05:00
|
|
|
gboolean reverse;
|
2002-05-11 23:39:49 -05:00
|
|
|
const char *name;
|
2001-11-23 23:35:08 -06:00
|
|
|
|
|
|
|
if (!invoice || !acc) return NULL;
|
|
|
|
|
2002-06-21 21:38:13 -05:00
|
|
|
/* Stabilize the Billing Terms of this invoice */
|
|
|
|
if (invoice->terms)
|
|
|
|
gncInvoiceSetTerms (invoice,
|
|
|
|
gncBillTermReturnChild (invoice->terms, TRUE));
|
|
|
|
|
|
|
|
/* Figure out if we need to "reverse" the numbers. */
|
2002-05-16 17:45:50 -05:00
|
|
|
reverse = (gncInvoiceGetOwnerType (invoice) == GNC_OWNER_CUSTOMER);
|
|
|
|
|
2002-06-25 10:08:50 -05:00
|
|
|
/* Find an existing payment-lot for this owner */
|
|
|
|
{
|
|
|
|
LotList *lot_list;
|
|
|
|
struct lotmatch lm;
|
|
|
|
|
|
|
|
lm.reverse = reverse;
|
|
|
|
lm.owner = gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice));
|
|
|
|
|
|
|
|
lot_list = xaccAccountFindOpenLots (acc, gnc_lot_match_owner_payment,
|
|
|
|
&lm, NULL);
|
|
|
|
if (lot_list)
|
|
|
|
lot = lot_list->data;
|
|
|
|
|
|
|
|
g_list_free (lot_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new lot for this invoice, if we need to do so */
|
|
|
|
if (!lot)
|
|
|
|
lot = gnc_lot_new (invoice->book);
|
2002-05-22 21:47:46 -05:00
|
|
|
|
|
|
|
/* Create a new transaction */
|
2001-11-24 23:34:34 -06:00
|
|
|
txn = xaccMallocTransaction (invoice->book);
|
2001-11-23 23:35:08 -06:00
|
|
|
xaccTransBeginEdit (txn);
|
|
|
|
|
2002-05-11 23:39:49 -05:00
|
|
|
name = gncOwnerGetName (gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice)));
|
|
|
|
|
2002-05-13 21:44:54 -05:00
|
|
|
/* Set Transaction Description (Owner Name) , Num (invoice ID), Currency */
|
|
|
|
xaccTransSetDescription (txn, name);
|
2001-11-23 23:35:08 -06:00
|
|
|
xaccTransSetNum (txn, gncInvoiceGetID (invoice));
|
2002-03-04 13:34:52 -06:00
|
|
|
xaccTransSetCurrency (txn, invoice->common_commodity);
|
2001-11-23 23:35:08 -06:00
|
|
|
|
|
|
|
/* Entered and Posted at date */
|
2002-05-08 15:42:38 -05:00
|
|
|
if (post_date) {
|
|
|
|
xaccTransSetDateEnteredTS (txn, post_date);
|
|
|
|
xaccTransSetDatePostedTS (txn, post_date);
|
|
|
|
gncInvoiceSetDatePosted (invoice, *post_date);
|
2001-11-23 23:35:08 -06:00
|
|
|
}
|
|
|
|
|
2002-05-08 15:42:38 -05:00
|
|
|
if (due_date)
|
|
|
|
xaccTransSetDateDueTS (txn, due_date);
|
2002-01-12 22:25:55 -06:00
|
|
|
|
2001-11-23 23:35:08 -06:00
|
|
|
/* Iterate through the entries; sum up everything for each account.
|
|
|
|
* then create the appropriate splits in this txn.
|
|
|
|
*/
|
|
|
|
total = gnc_numeric_zero();
|
|
|
|
for (iter = gncInvoiceGetEntries(invoice); iter; iter = iter->next) {
|
2002-01-12 22:25:55 -06:00
|
|
|
gnc_numeric value, tax;
|
2002-06-16 00:11:33 -05:00
|
|
|
GList *taxes;
|
2001-11-23 23:35:08 -06:00
|
|
|
GncEntry * entry = iter->data;
|
2002-01-12 22:25:55 -06:00
|
|
|
Account *this_acc;
|
|
|
|
|
2002-06-16 00:11:33 -05:00
|
|
|
/* Stabilize the TaxTable in this entry */
|
|
|
|
gncEntrySetTaxTable (entry,
|
|
|
|
gncTaxTableReturnChild (gncEntryGetTaxTable (entry),
|
|
|
|
TRUE));
|
|
|
|
|
|
|
|
/* Obtain the Entry's Value and TaxValues */
|
|
|
|
gncEntryGetValue (entry, &value, NULL, &tax, &taxes);
|
2002-01-12 22:25:55 -06:00
|
|
|
|
|
|
|
/* add the value for the account split */
|
|
|
|
this_acc = gncEntryGetAccount (entry);
|
|
|
|
if (this_acc) {
|
2002-05-10 19:35:43 -05:00
|
|
|
if (gnc_numeric_check (value) == GNC_ERROR_OK) {
|
2002-06-16 00:11:33 -05:00
|
|
|
splitinfo = gncAccountValueAdd (splitinfo, this_acc, value);
|
2002-05-27 14:19:26 -05:00
|
|
|
total = gnc_numeric_add (total, value, GNC_DENOM_AUTO, GNC_DENOM_LCD);
|
2002-05-10 19:35:43 -05:00
|
|
|
} else
|
|
|
|
g_warning ("bad value in our entry");
|
2001-11-23 23:35:08 -06:00
|
|
|
}
|
|
|
|
|
2002-06-16 00:11:33 -05:00
|
|
|
/* now merge in the TaxValues */
|
|
|
|
splitinfo = gncAccountValueAddList (splitinfo, taxes);
|
|
|
|
|
|
|
|
/* ... and add the tax total */
|
|
|
|
if (gnc_numeric_check (tax) == GNC_ERROR_OK)
|
|
|
|
total = gnc_numeric_add (total, tax, GNC_DENOM_AUTO, GNC_DENOM_LCD);
|
|
|
|
else
|
|
|
|
g_warning ("bad tax in our entry");
|
2001-11-23 23:35:08 -06:00
|
|
|
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
/* Iterate through the splitinfo list and generate the splits */
|
|
|
|
for (iter = splitinfo; iter; iter = iter->next) {
|
|
|
|
Split *split;
|
2002-06-16 00:11:33 -05:00
|
|
|
GncAccountValue *acc_val = iter->data;
|
2001-11-23 23:35:08 -06:00
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
split = xaccMallocSplit (invoice->book);
|
2001-11-23 23:35:08 -06:00
|
|
|
/* set action and memo? */
|
|
|
|
|
2002-05-13 21:44:54 -05:00
|
|
|
xaccSplitSetMemo (split, memo);
|
2002-05-11 23:39:49 -05:00
|
|
|
|
2002-06-16 00:11:33 -05:00
|
|
|
xaccSplitSetBaseValue (split, (reverse ? gnc_numeric_neg (acc_val->value)
|
|
|
|
: acc_val->value),
|
2002-03-04 13:34:52 -06:00
|
|
|
invoice->common_commodity);
|
2002-06-16 00:11:33 -05:00
|
|
|
xaccAccountBeginEdit (acc_val->account);
|
|
|
|
xaccAccountInsertSplit (acc_val->account, split);
|
|
|
|
xaccAccountCommitEdit (acc_val->account);
|
2001-11-23 23:35:08 -06:00
|
|
|
xaccTransAppendSplit (txn, split);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now create the Posted split (which is negative -- it's a credit) */
|
|
|
|
{
|
2001-11-24 23:34:34 -06:00
|
|
|
Split *split = xaccMallocSplit (invoice->book);
|
2002-05-10 19:35:43 -05:00
|
|
|
|
2002-05-22 21:47:46 -05:00
|
|
|
/* Set action/memo */
|
2002-05-13 21:44:54 -05:00
|
|
|
xaccSplitSetMemo (split, memo);
|
2002-05-16 17:45:50 -05:00
|
|
|
xaccSplitSetAction (split, gncInvoiceGetType (invoice));
|
2002-05-10 19:35:43 -05:00
|
|
|
|
2002-01-22 21:58:07 -06:00
|
|
|
xaccSplitSetBaseValue (split, (reverse ? total : gnc_numeric_neg (total)),
|
2002-03-04 13:34:52 -06:00
|
|
|
invoice->common_commodity);
|
2001-11-23 23:35:08 -06:00
|
|
|
xaccAccountBeginEdit (acc);
|
|
|
|
xaccAccountInsertSplit (acc, split);
|
|
|
|
xaccAccountCommitEdit (acc);
|
|
|
|
xaccTransAppendSplit (txn, split);
|
2002-06-25 10:08:50 -05:00
|
|
|
|
|
|
|
/* add this split to the lot */
|
|
|
|
gnc_lot_add_split (lot, split);
|
2001-11-23 23:35:08 -06:00
|
|
|
}
|
|
|
|
|
2002-06-24 17:30:36 -05:00
|
|
|
/* Now attach this invoice to the txn, lot, and account */
|
|
|
|
gncInvoiceAttachInvoiceToLot (invoice, lot);
|
2002-05-08 15:42:38 -05:00
|
|
|
gncInvoiceAttachInvoiceToTxn (invoice, txn);
|
2001-11-23 23:35:08 -06:00
|
|
|
gncInvoiceSetPostedAcc (invoice, acc);
|
|
|
|
|
|
|
|
xaccTransCommitEdit (txn);
|
|
|
|
|
2002-06-16 00:11:33 -05:00
|
|
|
gncAccountValueDestroy (splitinfo);
|
|
|
|
|
2002-06-25 10:08:50 -05:00
|
|
|
/* check the lot -- if we still look like a payment lot, then that
|
|
|
|
* means we need to create a balancing split and create a new payment
|
|
|
|
* lot for the next invoice
|
|
|
|
*/
|
|
|
|
total = gnc_lot_get_balance (lot);
|
|
|
|
if (!reverse)
|
|
|
|
total = gnc_numeric_neg (total);
|
|
|
|
|
|
|
|
if (gnc_numeric_negative_p (total)) {
|
|
|
|
Transaction *t2;
|
|
|
|
GNCLot *lot2;
|
|
|
|
Split *split;
|
|
|
|
char *memo2 = _("Automatic Payment Forward");
|
|
|
|
|
|
|
|
t2 = xaccMallocTransaction (invoice->book);
|
|
|
|
lot2 = gnc_lot_new (invoice->book);
|
|
|
|
gncOwnerAttachToLot (gncOwnerGetEndOwner (gncInvoiceGetOwner (invoice)),
|
|
|
|
lot2);
|
|
|
|
|
|
|
|
xaccTransBeginEdit (t2);
|
|
|
|
xaccAccountBeginEdit (acc);
|
|
|
|
|
|
|
|
/* Set Transaction Description (Owner Name), Currency */
|
|
|
|
xaccTransSetDescription (t2, name);
|
|
|
|
xaccTransSetCurrency (t2, invoice->common_commodity);
|
|
|
|
|
|
|
|
/* Entered and Posted at date */
|
|
|
|
if (post_date) {
|
|
|
|
xaccTransSetDateEnteredTS (t2, post_date);
|
|
|
|
xaccTransSetDatePostedTS (t2, post_date);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Balance out this lot */
|
|
|
|
split = xaccMallocSplit (invoice->book);
|
|
|
|
xaccSplitSetMemo (split, memo2);
|
|
|
|
xaccSplitSetBaseValue (split, gnc_numeric_neg (total),
|
|
|
|
invoice->common_commodity);
|
|
|
|
xaccAccountInsertSplit (acc, split);
|
|
|
|
xaccTransAppendSplit (t2, split);
|
|
|
|
gnc_lot_add_split (lot, split);
|
|
|
|
|
|
|
|
/* And apply the pre-payment to a new lot */
|
|
|
|
split = xaccMallocSplit (invoice->book);
|
|
|
|
xaccSplitSetMemo (split, memo2);
|
|
|
|
xaccSplitSetBaseValue (split, total, invoice->common_commodity);
|
|
|
|
xaccAccountInsertSplit (acc, split);
|
|
|
|
xaccTransAppendSplit (t2, split);
|
|
|
|
gnc_lot_add_split (lot2, split);
|
|
|
|
|
|
|
|
xaccTransCommitEdit (t2);
|
|
|
|
xaccAccountCommitEdit (acc);
|
|
|
|
}
|
|
|
|
|
2001-11-23 23:35:08 -06:00
|
|
|
return txn;
|
|
|
|
}
|
|
|
|
|
2002-02-19 12:23:53 -06:00
|
|
|
static gboolean gncInvoiceDateExists (Timespec *date)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (date, FALSE);
|
|
|
|
if (date->tv_sec || date->tv_nsec) return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean gncInvoiceIsPosted (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return FALSE;
|
|
|
|
return gncInvoiceDateExists (&(invoice->date_posted));
|
|
|
|
}
|
|
|
|
|
2002-05-27 13:17:31 -05:00
|
|
|
GUID gncInvoiceRetGUID (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice)
|
|
|
|
return *xaccGUIDNULL();
|
|
|
|
|
|
|
|
return invoice->guid;
|
|
|
|
}
|
|
|
|
|
|
|
|
GncInvoice * gncInvoiceLookupDirect (GUID guid, GNCBook *book)
|
|
|
|
{
|
|
|
|
if (!book) return NULL;
|
|
|
|
return gncInvoiceLookup (book, &guid);
|
|
|
|
}
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
GncInvoice * gncInvoiceLookup (GNCBook *book, const GUID *guid)
|
|
|
|
{
|
|
|
|
if (!book || !guid) return NULL;
|
|
|
|
return xaccLookupEntity (gnc_book_get_entity_table (book),
|
|
|
|
guid, _GNC_MOD_NAME);
|
2001-11-23 23:35:08 -06:00
|
|
|
}
|
|
|
|
|
2001-11-21 19:23:07 -06:00
|
|
|
void gncInvoiceBeginEdit (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
|
|
|
}
|
|
|
|
void gncInvoiceCommitEdit (GncInvoice *invoice)
|
|
|
|
{
|
|
|
|
if (!invoice) return;
|
2002-03-04 13:34:52 -06:00
|
|
|
if (invoice->dirty)
|
|
|
|
gncBusinessSetDirtyFlag (invoice->book, _GNC_MOD_NAME, TRUE);
|
|
|
|
invoice->dirty = FALSE;
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
int gncInvoiceCompare (GncInvoice *a, GncInvoice *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);
|
2002-02-27 23:10:07 -06:00
|
|
|
if (compare) return compare;
|
2002-02-03 14:01:08 -06:00
|
|
|
|
|
|
|
compare = timespec_cmp (&(a->date_opened), &(b->date_opened));
|
2002-02-27 23:10:07 -06:00
|
|
|
if (compare) return compare;
|
2002-02-03 14:01:08 -06:00
|
|
|
|
2002-02-10 19:59:54 -06:00
|
|
|
compare = timespec_cmp (&(a->date_posted), &(b->date_posted));
|
2002-02-27 23:10:07 -06:00
|
|
|
if (compare) return compare;
|
2002-02-10 19:59:54 -06:00
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
return guid_compare (&(a->guid), &(b->guid));
|
|
|
|
}
|
|
|
|
|
2001-11-24 23:34:34 -06:00
|
|
|
/* Package-Private functions */
|
|
|
|
|
|
|
|
static void addObj (GncInvoice *invoice)
|
|
|
|
{
|
2002-02-24 16:12:24 -06:00
|
|
|
gncBusinessAddObject (invoice->book, _GNC_MOD_NAME, invoice, &invoice->guid);
|
2001-11-24 23:34:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remObj (GncInvoice *invoice)
|
|
|
|
{
|
2002-02-24 16:12:24 -06:00
|
|
|
gncBusinessRemoveObject (invoice->book, _GNC_MOD_NAME, &invoice->guid);
|
2001-11-24 23:34:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _gncInvoiceCreate (GNCBook *book)
|
|
|
|
{
|
2002-02-24 16:12:24 -06:00
|
|
|
gncBusinessCreate (book, _GNC_MOD_NAME);
|
2001-11-24 23:34:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _gncInvoiceDestroy (GNCBook *book)
|
|
|
|
{
|
2002-02-24 16:12:24 -06:00
|
|
|
gncBusinessDestroy (book, _GNC_MOD_NAME);
|
|
|
|
}
|
2001-11-24 23:34:34 -06:00
|
|
|
|
2002-02-24 16:12:24 -06:00
|
|
|
static gboolean _gncInvoiceIsDirty (GNCBook *book)
|
|
|
|
{
|
|
|
|
return gncBusinessIsDirty (book, _GNC_MOD_NAME);
|
2001-11-24 23:34:34 -06:00
|
|
|
}
|
|
|
|
|
2002-03-04 13:34:52 -06:00
|
|
|
static void _gncInvoiceMarkClean (GNCBook *book)
|
|
|
|
{
|
|
|
|
gncBusinessSetDirtyFlag (book, _GNC_MOD_NAME, FALSE);
|
|
|
|
}
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
static void _gncInvoiceForeach (GNCBook *book, foreachObjectCB cb,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
gncBusinessForeach (book, _GNC_MOD_NAME, cb, user_data);
|
|
|
|
}
|
|
|
|
|
2002-02-24 16:12:24 -06:00
|
|
|
static const char * _gncInvoicePrintable (gpointer obj)
|
2002-02-23 22:01:57 -06:00
|
|
|
{
|
2002-02-24 16:12:24 -06:00
|
|
|
GncInvoice *invoice = obj;
|
|
|
|
|
2002-02-23 22:01:57 -06:00
|
|
|
g_return_val_if_fail (invoice, NULL);
|
|
|
|
|
|
|
|
if (invoice->dirty || invoice->printname == NULL) {
|
|
|
|
if (invoice->printname) g_free (invoice->printname);
|
|
|
|
|
|
|
|
invoice->printname =
|
2002-05-08 15:42:38 -05:00
|
|
|
g_strdup_printf ("%s%s", invoice->id,
|
|
|
|
gncInvoiceIsPosted (invoice) ? _(" (posted)") : "");
|
2002-02-23 22:01:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return invoice->printname;
|
|
|
|
}
|
|
|
|
|
2002-02-03 14:01:08 -06:00
|
|
|
static GncObject_t gncInvoiceDesc = {
|
|
|
|
GNC_OBJECT_VERSION,
|
2001-11-24 23:34:34 -06:00
|
|
|
_GNC_MOD_NAME,
|
2002-02-19 12:23:53 -06:00
|
|
|
"Invoice",
|
2001-11-24 23:34:34 -06:00
|
|
|
_gncInvoiceCreate,
|
|
|
|
_gncInvoiceDestroy,
|
2002-02-24 16:12:24 -06:00
|
|
|
_gncInvoiceIsDirty,
|
2002-03-04 13:34:52 -06:00
|
|
|
_gncInvoiceMarkClean,
|
2002-02-03 14:01:08 -06:00
|
|
|
_gncInvoiceForeach,
|
2002-02-23 22:01:57 -06:00
|
|
|
_gncInvoicePrintable,
|
2001-11-21 19:23:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
gboolean gncInvoiceRegister (void)
|
|
|
|
{
|
2002-02-03 14:01:08 -06:00
|
|
|
static QueryObjectDef params[] = {
|
2002-02-08 22:18:50 -06:00
|
|
|
{ INVOICE_ID, QUERYCORE_STRING, (QueryAccess)gncInvoiceGetID },
|
|
|
|
{ INVOICE_OWNER, GNC_OWNER_MODULE_NAME, (QueryAccess)gncInvoiceGetOwner },
|
|
|
|
{ INVOICE_OPENED, QUERYCORE_DATE, (QueryAccess)gncInvoiceGetDateOpened },
|
|
|
|
{ INVOICE_DUE, QUERYCORE_DATE, (QueryAccess)gncInvoiceGetDateDue },
|
2002-05-08 15:42:38 -05:00
|
|
|
{ INVOICE_POSTED, QUERYCORE_DATE, (QueryAccess)gncInvoiceGetDatePosted },
|
2002-02-19 12:23:53 -06:00
|
|
|
{ INVOICE_IS_POSTED, QUERYCORE_BOOLEAN, (QueryAccess)gncInvoiceIsPosted },
|
2002-05-08 15:42:38 -05:00
|
|
|
{ INVOICE_BILLINGID, QUERYCORE_STRING, (QueryAccess)gncInvoiceGetBillingID },
|
2002-02-08 22:18:50 -06:00
|
|
|
{ INVOICE_NOTES, QUERYCORE_STRING, (QueryAccess)gncInvoiceGetNotes },
|
2002-02-03 19:55:33 -06:00
|
|
|
{ INVOICE_ACC, GNC_ID_ACCOUNT, (QueryAccess)gncInvoiceGetPostedAcc },
|
2002-02-10 19:59:54 -06:00
|
|
|
{ INVOICE_POST_TXN, GNC_ID_TRANS, (QueryAccess)gncInvoiceGetPostedTxn },
|
2002-05-16 17:45:50 -05:00
|
|
|
{ INVOICE_TYPE, QUERYCORE_STRING, (QueryAccess)gncInvoiceGetType },
|
2002-06-21 21:38:13 -05:00
|
|
|
{ INVOICE_TERMS, GNC_BILLTERM_MODULE_NAME, (QueryAccess)gncInvoiceGetTerms },
|
2002-02-08 22:18:50 -06:00
|
|
|
{ QUERY_PARAM_BOOK, GNC_ID_BOOK, (QueryAccess)gncInvoiceGetBook },
|
2002-02-24 16:12:24 -06:00
|
|
|
{ QUERY_PARAM_GUID, QUERYCORE_GUID, (QueryAccess)gncInvoiceGetGUID },
|
2002-02-03 14:01:08 -06:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2002-02-03 19:55:33 -06:00
|
|
|
gncQueryObjectRegister (_GNC_MOD_NAME, (QuerySort)gncInvoiceCompare, params);
|
2002-02-03 14:01:08 -06:00
|
|
|
|
|
|
|
return gncObjectRegister (&gncInvoiceDesc);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|
|
|
|
|
2002-03-02 20:17:07 -06:00
|
|
|
gint64 gncInvoiceNextID (GNCBook *book)
|
2001-11-21 19:23:07 -06:00
|
|
|
{
|
2002-03-02 20:17:07 -06:00
|
|
|
return gnc_book_get_counter (book, _GNC_MOD_NAME);
|
2001-11-21 19:23:07 -06:00
|
|
|
}
|