45 #include <glib/gi18n.h> 46 #include <qofinstance-p.h> 51 #include "gnc-lot-p.h" 54 #include "TransactionP.h" 58 static QofLogModule log_module = GNC_MOD_LOT;
92 GncInvoice *cached_invoice;
95 signed char is_closed;
96 #define LOT_CLOSED_UNKNOWN (-1) 102 #define GET_PRIVATE(o) \ 103 ((GNCLotPrivate*)g_type_instance_get_private((GTypeInstance*)o, GNC_TYPE_LOT)) 105 #define gnc_lot_set_guid(L,G) qof_instance_set_guid(QOF_INSTANCE(L),&(G)) 113 G_DEFINE_TYPE_WITH_PRIVATE(GNCLot, gnc_lot, QOF_TYPE_INSTANCE)
116 gnc_lot_init(GNCLot* lot)
120 priv = GET_PRIVATE(lot);
121 priv->account = NULL;
123 priv->cached_invoice = NULL;
124 priv->is_closed = LOT_CLOSED_UNKNOWN;
125 priv->title = is_unset;
126 priv->notes = is_unset;
131 gnc_lot_dispose(GObject *lotp)
133 G_OBJECT_CLASS(gnc_lot_parent_class)->dispose(lotp);
137 gnc_lot_finalize(GObject* lotp)
139 G_OBJECT_CLASS(gnc_lot_parent_class)->finalize(lotp);
143 gnc_lot_get_property(GObject*
object, guint prop_id, GValue* value, GParamSpec* pspec)
149 g_return_if_fail(GNC_IS_LOT(
object));
151 lot = GNC_LOT(
object);
152 priv = GET_PRIVATE(lot);
156 g_value_set_int(value, priv->is_closed);
159 g_value_set_int(value, priv->marker);
164 case PROP_OWNER_TYPE:
167 case PROP_OWNER_GUID:
171 G_OBJECT_WARN_INVALID_PROPERTY_ID(
object, prop_id, pspec);
177 gnc_lot_set_property (GObject*
object,
186 g_return_if_fail(GNC_IS_LOT(
object));
188 lot = GNC_LOT(
object);
189 if (prop_id < PROP_RUNTIME_0)
190 g_assert (qof_instance_get_editlevel(lot));
192 priv = GET_PRIVATE(lot);
196 priv->is_closed = g_value_get_int(value);
199 priv->marker = g_value_get_int(value);
204 case PROP_OWNER_TYPE:
207 case PROP_OWNER_GUID:
211 G_OBJECT_WARN_INVALID_PROPERTY_ID(
object, prop_id, pspec);
217 gnc_lot_class_init(GNCLotClass* klass)
219 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
221 gobject_class->dispose = gnc_lot_dispose;
222 gobject_class->finalize = gnc_lot_finalize;
223 gobject_class->get_property = gnc_lot_get_property;
224 gobject_class->set_property = gnc_lot_set_property;
226 g_object_class_install_property(
229 g_param_spec_int(
"is-closed",
231 "Indication of whether this lot is open " 232 "or closed to further changes.",
236 g_object_class_install_property(
239 g_param_spec_int(
"marker",
245 g_object_class_install_property(
248 g_param_spec_boxed(
"invoice",
249 "Invoice attached to lot",
250 "Used by GncInvoice",
254 g_object_class_install_property(
257 g_param_spec_int64(
"owner-type",
258 "Owning Entity Type of lot",
263 g_object_class_install_property(
266 g_param_spec_boxed(
"owner-guid",
267 "Owner attached to lot",
274 gnc_lot_new (QofBook *book)
277 g_return_val_if_fail (book, NULL);
279 lot = g_object_new (GNC_TYPE_LOT, NULL);
286 gnc_lot_free(GNCLot* lot)
292 ENTER (
"(lot=%p)", lot);
295 priv = GET_PRIVATE(lot);
296 for (node = priv->splits; node; node = node->next)
298 Split *s = node->data;
301 g_list_free (priv->splits);
304 xaccAccountRemoveLot (priv->account, lot);
306 if (priv->notes != is_unset)
307 g_free (priv->notes);
309 if (priv->title != is_unset)
310 g_free (priv->title);
314 priv->account = NULL;
315 priv->is_closed = TRUE;
317 g_object_unref (lot);
323 gnc_lot_destroy (GNCLot *lot)
327 gnc_lot_begin_edit(lot);
328 qof_instance_set_destroying(lot, TRUE);
329 gnc_lot_commit_edit(lot);
335 gnc_lot_begin_edit (GNCLot *lot)
342 PERR (
"Failed to commit: %d", errcode);
343 gnc_engine_signal_commit_error( errcode );
346 static void lot_free(QofInstance* inst)
348 GNCLot* lot = GNC_LOT(inst);
353 static void noop (QofInstance *inst) {}
356 gnc_lot_commit_edit (GNCLot *lot)
365 gnc_lot_lookup (
const GncGUID *guid, QofBook *book)
368 if (!guid || !book)
return NULL;
374 gnc_lot_get_book (GNCLot *lot)
385 if (!lot)
return TRUE;
386 priv = GET_PRIVATE(lot);
388 return priv->is_closed;
395 if (!lot)
return NULL;
396 priv = GET_PRIVATE(lot);
397 return priv->account;
402 if (!lot)
return NULL;
403 return GET_PRIVATE(lot)->cached_invoice;
407 gnc_lot_set_cached_invoice(GNCLot* lot, GncInvoice *invoice)
410 GET_PRIVATE(lot)->cached_invoice = invoice;
414 gnc_lot_set_account(GNCLot* lot,
Account* account)
419 priv = GET_PRIVATE(lot);
420 priv->account = account;
430 priv = GET_PRIVATE(lot);
431 priv->is_closed = LOT_CLOSED_UNKNOWN;
439 if (!lot)
return NULL;
440 priv = GET_PRIVATE(lot);
444 gint gnc_lot_count_splits (
const GNCLot *lot)
448 priv = GET_PRIVATE(lot);
449 return g_list_length (priv->splits);
459 if (!lot)
return NULL;
460 priv = GET_PRIVATE (lot);
461 if (priv->title == is_unset)
464 GValue v = G_VALUE_INIT;
466 priv->title = G_VALUE_HOLDS_STRING (&v) ? g_value_dup_string (&v) : NULL;
473 gnc_lot_get_notes (
const GNCLot *lot)
476 if (!lot)
return NULL;
477 priv = GET_PRIVATE (lot);
478 if (priv->notes == is_unset)
480 GValue v = G_VALUE_INIT;
482 priv->notes = G_VALUE_HOLDS_STRING (&v) ? g_value_dup_string (&v) : NULL;
489 gnc_lot_set_title (GNCLot *lot,
const char *str)
491 GValue v = G_VALUE_INIT;
494 priv = GET_PRIVATE (lot);
495 if (priv->title != is_unset)
496 g_free (priv->title);
499 g_value_init (&v, G_TYPE_STRING);
500 g_value_set_string (&v, str);
501 priv->title = g_strdup (str);
503 qof_instance_set_dirty(QOF_INSTANCE(lot));
504 gnc_lot_commit_edit(lot);
509 gnc_lot_set_notes (GNCLot *lot,
const char *str)
511 GValue v = G_VALUE_INIT;
514 priv = GET_PRIVATE (lot);
515 if (priv->notes != is_unset)
516 g_free (priv->notes);
518 g_value_init (&v, G_TYPE_STRING);
519 g_value_set_string (&v, str);
520 priv->notes = g_strdup (str);
522 qof_instance_set_dirty(QOF_INSTANCE(lot));
523 gnc_lot_commit_edit(lot);
534 gnc_numeric zero = gnc_numeric_zero();
535 gnc_numeric baln = zero;
536 if (!lot)
return zero;
538 priv = GET_PRIVATE(lot);
541 priv->is_closed = FALSE;
548 for (node = priv->splits; node; node = node->next)
550 Split *s = node->data;
552 baln = gnc_numeric_add_fixed (baln, amt);
559 priv->is_closed = TRUE;
563 priv->is_closed = FALSE;
573 gnc_numeric *amount, gnc_numeric *value)
577 gnc_numeric zero = gnc_numeric_zero();
578 gnc_numeric amt = zero;
579 gnc_numeric val = zero;
583 if (lot == NULL)
return;
585 priv = GET_PRIVATE(lot);
588 Transaction *ta, *tb;
597 for (node = priv->splits; node; node = node->next)
599 Split *s = node->data;
604 if ((ta == tb && source != target) ||
608 amt = gnc_numeric_add_fixed (amt, tmpval);
610 val = gnc_numeric_add_fixed (val, tmpval);
626 if (!lot || !split)
return;
627 priv = GET_PRIVATE(lot);
629 ENTER (
"(lot=%p, split=%p) %s amt=%s val=%s", lot, split,
633 gnc_lot_begin_edit(lot);
635 qof_instance_set_dirty(QOF_INSTANCE(lot));
636 if (NULL == priv->account)
640 else if (priv->account != acc)
642 PERR (
"splits from different accounts cannot " 643 "be added to this lot!\n" 644 "\tlot account=\'%s\', split account=\'%s\'\n",
646 gnc_lot_commit_edit(lot);
647 LEAVE(
"different accounts");
651 if (lot == split->lot)
653 gnc_lot_commit_edit(lot);
654 LEAVE(
"already in lot");
659 gnc_lot_remove_split (split->lot, split);
663 priv->splits = g_list_append (priv->splits, split);
666 priv->is_closed = LOT_CLOSED_UNKNOWN;
667 gnc_lot_commit_edit(lot);
670 LEAVE(
"added to lot");
674 gnc_lot_remove_split (GNCLot *lot, Split *split)
677 if (!lot || !split)
return;
678 priv = GET_PRIVATE(lot);
680 ENTER (
"(lot=%p, split=%p)", lot, split);
681 gnc_lot_begin_edit(lot);
682 qof_instance_set_dirty(QOF_INSTANCE(lot));
683 priv->splits = g_list_remove (priv->splits, split);
685 priv->is_closed = LOT_CLOSED_UNKNOWN;
687 if (NULL == priv->splits)
689 xaccAccountRemoveLot (priv->account, lot);
690 priv->account = NULL;
692 gnc_lot_commit_edit(lot);
694 LEAVE(
"removed from lot");
704 if (!lot)
return NULL;
705 priv = GET_PRIVATE(lot);
706 if (! priv->splits)
return NULL;
707 priv->splits = g_list_sort (priv->splits, (GCompareFunc) xaccSplitOrderDateOnly);
708 return priv->splits->data;
718 if (!lot)
return NULL;
719 priv = GET_PRIVATE(lot);
720 if (! priv->splits)
return NULL;
721 priv->splits = g_list_sort (priv->splits, (GCompareFunc) xaccSplitOrderDateOnly);
723 for (node = priv->splits; node->next; node = node->next)
732 destroy_lot_on_book_close(QofInstance *ent, gpointer data)
734 GNCLot* lot = GNC_LOT(ent);
736 gnc_lot_destroy(lot);
740 gnc_lot_book_end(QofBook* book)
755 static QofObject gncLotDesc =
758 DI(.e_type = ) GNC_ID_LOT,
759 DI(.type_label = ) "Lot",
760 DI(.create = ) (gpointer)gnc_lot_new,
761 DI(.book_begin = ) NULL,
762 DI(.book_end = ) gnc_lot_book_end,
766 DI(.printable = ) NULL,
771 gboolean gnc_lot_register (
void)
773 static const QofParam params[] =
776 LOT_TITLE, QOF_TYPE_STRING,
781 LOT_NOTES, QOF_TYPE_STRING,
786 QOF_PARAM_GUID, QOF_TYPE_GUID,
794 LOT_IS_CLOSED, QOF_TYPE_BOOLEAN,
798 LOT_BALANCE, QOF_TYPE_NUMERIC,
819 buff = g_strdup_printf (
"%s %" G_GINT64_FORMAT, _(
"Lot"),
id);
820 gnc_lot_set_title (lot, buff);
int qof_instance_version_cmp(const QofInstance *left, const QofInstance *right)
Compare two instances, based on their last update times.
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
Equivalence predicate: Returns TRUE (1) if a and b represent the same number.
gchar * gnc_num_dbg_to_string(gnc_numeric n)
Convert to string.
void qof_instance_get(const QofInstance *inst, const gchar *first_prop,...)
Wrapper for g_object_get.
void qof_instance_set_kvp(QofInstance *, GValue const *value, unsigned count,...)
Sets a KVP slot to a value from a GValue.
QofBook * qof_instance_get_book(gconstpointer inst)
Return the book pointer.
gboolean qof_collection_is_dirty(const QofCollection *col)
Return value of 'dirty' flag on collection.
QofInstance * qof_collection_lookup_entity(const QofCollection *col, const GncGUID *guid)
Find the entity going only from its guid.
GncInvoice * gnc_lot_get_cached_invoice(const GNCLot *lot)
The gnc_lot_get_cached_invoice() routine returns the invoice with which this lot is associated...
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Identifies that something sold at one time was bought at another.
gboolean qof_instance_get_destroying(gconstpointer ptr)
Retrieve the flag that indicates whether or not this object is about to be destroyed.
void qof_instance_set(QofInstance *inst, const gchar *first_prop,...)
Wrapper for g_object_set Group setting multiple parameters in a single begin/commit/rollback.
void qof_class_register(QofIdTypeConst obj_name, QofSortFunc default_sort_function, const QofParam *params)
This function registers a new object class with the Qof subsystem.
void gnc_lot_add_split(GNCLot *lot, Split *split)
The gnc_lot_add_split() routine adds a split to this lot.
Split * xaccSplitGetGainsSourceSplit(const Split *split)
The xaccSplitGetGainsSourceSplit() routine returns the split that is the source of the cap gains in t...
void xaccAccountInsertLot(Account *acc, GNCLot *lot)
The xaccAccountInsertLot() method will register the indicated lot with this account.
Transaction * xaccSplitGetParent(const Split *split)
Returns the parent transaction of the split.
#define QOF_OBJECT_VERSION
Defines the version of the core object object registration interface.
gboolean qof_commit_edit(QofInstance *inst)
commit_edit helpers
#define PERR(format, args...)
Log a serious error.
#define ENTER(format, args...)
Print a function entry debugging message.
#define QOF_PARAM_BOOK
"Known" Object Parameters – all objects must support these
void gnc_lot_set_closed_unknown(GNCLot *lot)
Reset closed flag so that it will be recalculated.
void qof_collection_foreach(const QofCollection *col, QofInstanceForeachCB cb_func, gpointer user_data)
Call the callback for each entity in the collection.
void(* QofSetterFunc)(gpointer, gpointer)
The QofSetterFunc defines an function pointer for parameter setters.
void qof_instance_get_kvp(QofInstance *, GValue *value, unsigned count,...)
Retrieves the contents of a KVP slot into a provided GValue.
Split * gnc_lot_get_earliest_split(GNCLot *lot)
The gnc_lot_get_earliest_split() routine is a convenience routine that helps identify the earliest da...
const char * gnc_lot_get_title(const GNCLot *lot)
Get and set the account title, or the account notes, or the marker.
Split * gnc_lot_get_latest_split(GNCLot *lot)
The gnc_lot_get_latest_split() routine is a convenience routine that helps identify the date this lot...
void qof_instance_init_data(QofInstance *inst, QofIdType type, QofBook *book)
Initialise the settings associated with an instance.
gboolean qof_begin_edit(QofInstance *inst)
begin_edit
GList SplitList
GList of Split.
Account handling public routines.
SplitList * gnc_lot_get_split_list(const GNCLot *lot)
The gnc_lot_get_split_list() routine returns a GList of all the splits in this lot.
void gnc_lot_get_balance_before(const GNCLot *lot, const Split *split, gnc_numeric *amount, gnc_numeric *value)
The gnc_lot_get_balance_before routine computes both the balance and value in the lot considering onl...
gboolean qof_commit_edit_part2(QofInstance *inst, void(*on_error)(QofInstance *, QofBackendError), void(*on_done)(QofInstance *), void(*on_free)(QofInstance *))
part2 – deal with the backend
gpointer(* QofAccessFunc)(gpointer object, const QofParam *param)
The QofAccessFunc defines an arbitrary function pointer for access functions.
void qof_collection_mark_clean(QofCollection *)
reset value of dirty flag
#define GNC_INVOICE_ID
STRING CONSTANTS ********************************************** Used to declare constant KVP keys use...
const GncGUID * qof_entity_get_guid(gconstpointer ent)
void xaccSplitSetLot(Split *split, GNCLot *lot)
Assigns the split to a specific Lot.
Business Invoice Interface.
GNCLot * gnc_lot_make_default(Account *acc)
XXX: Document?
gboolean gnc_lot_is_closed(GNCLot *lot)
The gnc_lot_is_closed() routine returns a boolean flag: is this lot closed? A lot is closed if its ba...
gnc_numeric xaccSplitGetValue(const Split *split)
Returns the value of this split in the transaction's commodity.
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
Account * xaccSplitGetAccount(const Split *split)
Returns the account of this split, which was set through xaccAccountInsertSplit().
This is the private header for the account structure.
#define LEAVE(format, args...)
Print a function exit debugging message.
int xaccTransOrder(const Transaction *ta, const Transaction *tb)
The xaccTransOrder(ta,tb) method is useful for sorting.
GNCNumericErrorCode gnc_numeric_check(gnc_numeric in)
Check for error signal in value.
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
Account * gnc_lot_get_account(const GNCLot *lot)
The gnc_lot_get_account() routine returns the account with which this lot is associated.
gboolean qof_object_register(const QofObject *object)
Register new types of object objects.
const char * xaccAccountGetName(const Account *acc)
Get the account's name.
void qof_event_gen(QofInstance *entity, QofEventId event_id, gpointer event_data)
Invoke all registered event handlers using the given arguments.
API for Transactions and Splits (journal entries)
The type used to store guids in C.
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
Utilities to Automatically Compute Capital Gains/Losses.
gnc_numeric gnc_lot_get_balance(GNCLot *lot)
The gnc_lot_get_balance() routine returns the balance of the lot.
gnc_numeric xaccSplitGetAmount(const Split *split)
Returns the amount of the split in the account's commodity.