Add properties "invoice", "owner-type", and "owner-guid"

Replaces direct KVP access.
This commit is contained in:
John Ralls 2013-10-15 17:11:01 -07:00
parent e3e21b602b
commit f29ea9fbe9
6 changed files with 134 additions and 58 deletions

View File

@ -256,5 +256,10 @@ void gnc_engine_signal_commit_error( QofBackendError errcode );
*/
#define GNC_INVOICE_ID "gncInvoice"
#define GNC_INVOICE_GUID "invoice-guid"
#define GNC_OWNER_ID "gncOwner"
#define GNC_OWNER_TYPE "owner-type"
#define GNC_OWNER_GUID "owner-guid"
#endif
/** @} */

View File

@ -65,7 +65,11 @@ enum
{
PROP_0,
PROP_IS_CLOSED,
PROP_MARKER
PROP_MARKER,
PROP_INVOICE,
PROP_OWNER_TYPE,
PROP_OWNER_GUID,
};
typedef struct LotPrivate
@ -94,6 +98,11 @@ typedef struct LotPrivate
/* ============================================================= */
static void gnc_lot_set_invoice (GNCLot* lot, GncGUID *guid);
static GncGUID *gnc_lot_get_invoice (GNCLot* lot);
/* ============================================================= */
/* GObject Initialization */
G_DEFINE_TYPE(GNCLot, gnc_lot, QOF_TYPE_INSTANCE)
@ -126,6 +135,9 @@ gnc_lot_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec*
{
GNCLot* lot;
LotPrivate* priv;
KvpFrame *frame;
gchar *key;
GValue *temp;
g_return_if_fail(GNC_IS_LOT(object));
@ -139,6 +151,21 @@ gnc_lot_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec*
case PROP_MARKER:
g_value_set_int(value, priv->marker);
break;
case PROP_INVOICE:
key = GNC_INVOICE_ID "/" GNC_INVOICE_GUID;
qof_instance_get_kvp (QOF_INSTANCE (lot), key, value);
break;
case PROP_OWNER_TYPE:
key = GNC_OWNER_ID"/" GNC_OWNER_TYPE;
qof_instance_get_kvp (QOF_INSTANCE (lot), key, value);
break;
case PROP_OWNER_GUID:
key = GNC_OWNER_ID "/" GNC_OWNER_GUID;
qof_instance_get_kvp (QOF_INSTANCE (lot), key, value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
@ -147,6 +174,8 @@ gnc_lot_set_property(GObject* object, guint prop_id, const GValue* value, GParam
{
GNCLot* lot;
LotPrivate* priv;
KvpFrame *frame;
gchar *key = NULL;
g_return_if_fail(GNC_IS_LOT(object));
@ -160,7 +189,22 @@ gnc_lot_set_property(GObject* object, guint prop_id, const GValue* value, GParam
case PROP_MARKER:
priv->marker = g_value_get_int(value);
break;
}
case PROP_INVOICE:
key = GNC_INVOICE_ID"/" GNC_INVOICE_GUID;
qof_instance_set_kvp (QOF_INSTANCE (lot), key, value);
break;
case PROP_OWNER_TYPE:
key = GNC_OWNER_ID "/" GNC_OWNER_TYPE;
qof_instance_set_kvp (QOF_INSTANCE (lot), key, value);
break;
case PROP_OWNER_GUID:
key = GNC_OWNER_ID "/" GNC_OWNER_GUID;
qof_instance_set_kvp (QOF_INSTANCE (lot), key, value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
@ -194,8 +238,32 @@ gnc_lot_class_init(GNCLotClass* klass)
0, G_MAXINT8, 0,
G_PARAM_READWRITE));
g_object_class_install_property(
gobject_class,
PROP_INVOICE,
g_param_spec_boxed("invoice",
"Invoice attached to lot",
"Used by GncInvoice",
GNC_TYPE_GUID,
G_PARAM_READWRITE));
g_object_class_install_property(
gobject_class,
PROP_OWNER_TYPE,
g_param_spec_int64("owner-type",
"Owning Entity Type of lot",
"Used by GncOwner",
0, G_MAXINT64, 0,
G_PARAM_READWRITE));
g_object_class_install_property(
gobject_class,
PROP_OWNER_GUID,
g_param_spec_boxed("owner-guid",
"Owner attached to lot",
"Used by GncOwner",
GNC_TYPE_GUID,
G_PARAM_READWRITE));
}
GNCLot *

View File

@ -82,8 +82,6 @@ static QofLogModule log_module = GNC_MOD_BUSINESS;
#define _GNC_MOD_NAME GNC_ID_INVOICE
#define GNC_INVOICE_ID "gncInvoice"
#define GNC_INVOICE_GUID "invoice-guid"
#define GNC_INVOICE_IS_CN "credit-note"
#define SET_STR(obj, member, str) { \
@ -1080,52 +1078,37 @@ qofInvoiceSetJob (GncInvoice *invoice, GncJob *job)
static void
gncInvoiceDetachFromLot (GNCLot *lot)
{
KvpFrame *kvp;
if (!lot) return;
gnc_lot_begin_edit (lot);
kvp = gnc_lot_get_slots (lot);
kvp_frame_set_slot_path (kvp, NULL, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
qof_instance_set_dirty (QOF_INSTANCE (lot));
qof_instance_set (QOF_INSTANCE (lot), "invoice", NULL, NULL);
gnc_lot_commit_edit (lot);
}
static void
gncInvoiceAttachToLot (GncInvoice *invoice, GNCLot *lot)
{
KvpFrame *kvp;
KvpValue *value;
GncGUID *guid;
if (!invoice || !lot)
return;
if (invoice->posted_lot) return; /* Cannot reset invoice's lot */
guid = (GncGUID*)qof_instance_get_guid (QOF_INSTANCE (invoice));
gnc_lot_begin_edit (lot);
kvp = gnc_lot_get_slots (lot);
value = kvp_value_new_guid (qof_instance_get_guid (QOF_INSTANCE(invoice)));
kvp_frame_set_slot_path (kvp, value, GNC_INVOICE_ID, GNC_INVOICE_GUID, NULL);
qof_instance_set_dirty (QOF_INSTANCE (lot));
qof_instance_set (QOF_INSTANCE (lot), "invoice", guid, NULL);
gnc_lot_commit_edit (lot);
kvp_value_delete (value);
gncInvoiceSetPostedLot (invoice, lot);
}
GncInvoice * gncInvoiceGetInvoiceFromLot (GNCLot *lot)
{
KvpFrame *kvp;
KvpValue *value;
GncGUID *guid;
QofBook *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);
qof_instance_get (QOF_INSTANCE (lot), "invoice", &guid, NULL);
return gncInvoiceLookup(book, guid);
}

View File

@ -251,6 +251,7 @@ GncInvoice * gncInvoiceGetInvoiceFromLot (GNCLot *lot);
*/
static inline GncInvoice * gncInvoiceLookup (const QofBook *book, const GncGUID *guid)
{
if (book == NULL || guid == NULL) return NULL;
QOF_BOOK_RETURN_ENTITY(book, guid, GNC_ID_INVOICE, GncInvoice);
}
@ -288,7 +289,6 @@ QofBook *gncInvoiceGetBook(GncInvoice *x);
/** deprecated functions */
#define gncInvoiceGetGUID(x) qof_instance_get_guid(QOF_INSTANCE(x))
#define gncInvoiceRetGUID(x) (x ? *(qof_instance_get_guid(QOF_INSTANCE(x))) : *(guid_null()))
#define gncInvoiceLookupDirect(G,B) gncInvoiceLookup((B),&(G))
/** Test support function used by test-dbi-business-stuff.c */
gboolean gncInvoiceEqual(const GncInvoice *a, const GncInvoice *b);

View File

@ -51,8 +51,6 @@
#define _GNC_MOD_NAME GNC_ID_OWNER
#define GNC_OWNER_ID "gncOwner"
#define GNC_OWNER_TYPE "owner-type"
#define GNC_OWNER_GUID "owner-guid"
GncOwner * gncOwnerNew (void)
{
@ -568,31 +566,20 @@ const GncGUID * gncOwnerGetEndGUID (const GncOwner *owner)
void gncOwnerAttachToLot (const GncOwner *owner, GNCLot *lot)
{
KvpFrame *kvp;
KvpValue *value;
if (!owner || !lot)
if (!owner || !lot)
return;
kvp = gnc_lot_get_slots (lot);
gnc_lot_begin_edit (lot);
value = kvp_value_new_gint64 (gncOwnerGetType (owner));
kvp_frame_set_slot_path (kvp, value, GNC_OWNER_ID, GNC_OWNER_TYPE, NULL);
kvp_value_delete (value);
value = kvp_value_new_guid (gncOwnerGetGUID (owner));
kvp_frame_set_slot_path (kvp, value, GNC_OWNER_ID, GNC_OWNER_GUID, NULL);
qof_instance_set_dirty (QOF_INSTANCE (lot));
qof_instance_set (QOF_INSTANCE (lot),
"owner-type", (gint64)gncOwnerGetType (owner),
"owner-guid", gncOwnerGetGUID (owner),
NULL);
gnc_lot_commit_edit (lot);
kvp_value_delete (value);
}
gboolean gncOwnerGetOwnerFromLot (GNCLot *lot, GncOwner *owner)
{
KvpFrame *kvp;
KvpValue *value;
GncGUID *guid;
QofBook *book;
GncOwnerType type;
@ -600,20 +587,10 @@ gboolean gncOwnerGetOwnerFromLot (GNCLot *lot, GncOwner *owner)
if (!lot || !owner) return FALSE;
book = gnc_lot_get_book (lot);
kvp = gnc_lot_get_slots (lot);
value = kvp_frame_get_slot_path (kvp, GNC_OWNER_ID, GNC_OWNER_TYPE, NULL);
if (!value) return FALSE;
type = kvp_value_get_gint64 (value);
value = kvp_frame_get_slot_path (kvp, GNC_OWNER_ID, GNC_OWNER_GUID, NULL);
if (!value) return FALSE;
guid = kvp_value_get_guid (value);
if (!guid)
return FALSE;
qof_instance_get (QOF_INSTANCE (lot),
"owner-type", &type,
"owner-guid", &guid,
NULL);
switch (type)
{
case GNC_OWNER_CUSTOMER:

View File

@ -47,6 +47,7 @@ typedef struct
{
Account *acct;
Transaction *trans;
GNCLot *lot;
GncCustomer *cust;
GncEmployee *emp;
GncJob *job;
@ -77,6 +78,13 @@ setup_trans (Fixture *fixture, gconstpointer pData)
fixture->trans = xaccMallocTransaction (book);
}
static void
setup_lot (Fixture *fixture, gconstpointer pData)
{
QofBook *book = qof_book_new ();
fixture->lot = gnc_lot_new (book);
}
static void
teardown (Fixture *fixture, gconstpointer pData)
{
@ -130,8 +138,43 @@ test_trans_kvp_properties (Fixture *fixture, gconstpointer pData)
guid_free (guid_r);
}
static void
test_lot_kvp_properties (Fixture *fixture, gconstpointer pData)
{
GncGUID *invoice = guid_malloc ();
GncGUID *invoice_r;
gint64 owner_type = 47;
gint64 owner_type_r;
GncGUID *owner = guid_malloc ();
GncGUID *owner_r;
qof_instance_set (QOF_INSTANCE (fixture->lot),
"invoice", invoice,
"owner-type", owner_type,
"owner-guid", owner,
NULL);
g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->lot)));
qof_instance_mark_clean (QOF_INSTANCE (fixture->lot));
qof_instance_get (QOF_INSTANCE (fixture->lot),
"invoice", &invoice_r,
"owner-type", &owner_type_r,
"owner-guid", &owner_r,
NULL);
g_assert (guid_equal (invoice, invoice_r));
g_assert_cmpint (owner_type, ==, owner_type_r);
g_assert (guid_equal (owner, owner_r));
g_assert (!qof_instance_is_dirty (QOF_INSTANCE (fixture->lot)));
guid_free (invoice);
guid_free (invoice_r);
guid_free (owner);
guid_free (owner_r);
}
void test_suite_engine_kvp_properties (void)
{
GNC_TEST_ADD (suitename, "Account", Fixture, NULL, setup_account, test_account_kvp_properties, teardown);
GNC_TEST_ADD (suitename, "Transaction", Fixture, NULL, setup_trans, test_trans_kvp_properties, teardown);
GNC_TEST_ADD (suitename, "Lot", Fixture, NULL, setup_lot, test_lot_kvp_properties, teardown);
}