requires that all splits in lot share a common transaction currency.


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@9973 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Linas Vepstas 2004-05-29 18:04:40 +00:00
parent a9bbb453e7
commit e10faa04b5

View File

@ -46,6 +46,60 @@
/* static short module = MOD_LOT; */
static Split *
DirectionPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot, short reverse)
{
Split *split;
SplitList *node;
gnc_commodity *common_currency;
gboolean want_positive;
gnc_numeric baln;
if (!pcy || !lot || !lot->account || !lot->splits) return NULL;
/* Recomputing the balance re-evaluates the lot closure */
baln = gnc_lot_get_balance (lot);
if (gnc_lot_is_closed(lot)) return NULL;
want_positive = gnc_numeric_negative_p (baln);
/* All splits in lot must share a common transaction currency. */
split = lot->splits->data;
common_currency = split->parent->common_currency;
/* Walk over *all* splits in the account, till we find one that
* hasn't been assigned to a lot. Return that split.
* Make use of the fact that the splits in an account are
* already in date order; so we don't have to sort. */
node = xaccAccountGetSplitList (lot->account);
if (reverse)
{
node = g_list_last (node);
}
while (node)
{
gboolean is_positive;
split = node->data;
if (split->lot) goto donext;
if (common_currency != split->parent->common_currency) goto donext;
is_positive = gnc_numeric_positive_p (split->amount);
if ((want_positive && is_positive) ||
((!want_positive) && (!is_positive))) return split;
donext:
if (reverse)
{
node=node->prev;
}
else
{
node=node->next;
}
}
return NULL;
}
/* ============================================================== */
static GNCLot *
@ -57,24 +111,7 @@ FIFOPolicyGetLot (GNCPolicy *pcy, Split *split)
static Split *
FIFOPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot)
{
SplitList *node;
gboolean want_positive;
want_positive = gnc_numeric_negative_p (gnc_lot_get_balance (lot));
/* Make use of the fact that the splits in a lot are already
* in date order; so we don't have to search for the earliest. */
for (node = xaccAccountGetSplitList (lot->account); node; node=node->next)
{
gboolean is_positive;
Split *split = node->data;
if (split->lot) continue;
is_positive = gnc_numeric_positive_p (split->amount);
if ((want_positive && is_positive) ||
((!want_positive) && (!is_positive))) return split;
}
return NULL;
return DirectionPolicyGetSplit (pcy, lot, 0);
}
static void
@ -134,27 +171,7 @@ LIFOPolicyGetLot (GNCPolicy *pcy, Split *split)
static Split *
LIFOPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot)
{
SplitList *node;
gboolean want_positive;
want_positive = gnc_numeric_negative_p (gnc_lot_get_balance (lot));
/* Make use of the fact that the splits in a lot are already
* in date order; so we don't have to search for the latest,
* we merely start at the end and go backwards. */
node = xaccAccountGetSplitList (lot->account);
node = g_list_last (node);
for (; node; node=node->prev)
{
gboolean is_positive;
Split *split = node->data;
if (split->lot) continue;
is_positive = gnc_numeric_positive_p (split->amount);
if ((want_positive && is_positive) ||
((!want_positive) && (!is_positive))) return split;
}
return NULL;
return DirectionPolicyGetSplit (pcy, lot, 1);
}
/* This routine is actually identical to FIFO... */