Various small improvements to check & repair on business accounts

- don't attempt to merge splits that belong to an invoice transaction. These splits are managed by the business code.
- lot link cleanup can leave empty splits, remove these as well
This commit is contained in:
Geert Janssens 2016-11-09 18:44:21 +01:00
parent d52f44a8c7
commit 396117eec9
3 changed files with 45 additions and 7 deletions

View File

@ -43,6 +43,7 @@
#include "ScrubP.h"
#include "cap-gains.h"
#include "gnc-engine.h"
#include "gncInvoice.h"
#include "gnc-lot.h"
#include "policy-p.h"
@ -355,6 +356,11 @@ xaccScrubMergeSubSplits (Split *split, gboolean strict)
if (strict && (FALSE == is_subsplit (split))) return FALSE;
txn = split->parent;
// Don't mess with splits from an invoice transaction
// Those are the responsibility of the business code
if (gncInvoiceGetInvoiceFromTxn (txn)) return FALSE;
lot = xaccSplitGetLot (split);
ENTER ("(Lot=%s)", gnc_lot_get_title(lot));
@ -366,6 +372,10 @@ restart:
if (s == split) continue;
if (qof_instance_get_destroying(s)) continue;
// Don't mess with splits from an invoice transaction
// Those are the responsibility of the business code
if (gncInvoiceGetInvoiceFromTxn (s->parent)) return FALSE;
if (strict)
{
/* OK, this split is in the same lot (and thus same account)
@ -386,9 +396,13 @@ restart:
rc = TRUE;
goto restart;
}
if (gnc_numeric_zero_p (split->amount))
if (rc && gnc_numeric_zero_p (split->amount))
{
time64 pdate = xaccTransGetDate (txn);
gchar *pdatestr = gnc_ctime (&pdate);
PWARN ("Result of merge has zero amt!");
PWARN ("Transaction details - posted date %s - description %s", pdatestr, xaccTransGetDescription(txn));
g_free (pdatestr);
}
LEAVE (" splits merged=%d", rc);
return rc;

View File

@ -462,13 +462,14 @@ gncScrubBusinessLot (GNCLot *lot)
return splits_deleted;
}
void
gboolean
gncScrubBusinessSplit (Split *split)
{
const gchar *memo = _("Please delete this transaction. Explanation at http://wiki.gnucash.org/wiki/Business_Features_Issues#Double_Posting");
Transaction *txn;
gboolean deleted_split = FALSE;
if (!split) return;
if (!split) return FALSE;
ENTER ("(split=%p)", split);
txn = xaccSplitGetParent (split);
@ -500,10 +501,23 @@ gncScrubBusinessSplit (Split *split)
txn_date);
g_free (txn_date);
}
/* Next delete any empty splits that aren't part of an invoice transaction
* Such splits may be the result of scrubbing the business lots, which can
* merge splits together while reducing superfluous lot links
*/
else if (gnc_numeric_zero_p (xaccSplitGetAmount(split)) && !gncInvoiceGetInvoiceFromTxn (txn))
{
time64 pdate = xaccTransGetDate (txn);
gchar *pdatestr = gnc_ctime (&pdate);
PINFO ("Destroying empty split %p from transaction %s (%s)", split, pdatestr, xaccTransGetDescription(txn));
xaccSplitDestroy (split);
deleted_split = TRUE;
}
}
LEAVE ("(split=%p)", split);
return deleted_split;
}
/* ============================================================== */
@ -563,7 +577,7 @@ gncScrubBusinessAccountSplits (Account *acc, QofPercentageFunc percentagefunc)
{
SplitList *splits, *node;
gint split_count = 0;
gint curr_split_no = 0;
gint curr_split_no;
const gchar *str;
const char *message = _( "Checking business splits in account %s: %u of %u");
@ -577,6 +591,8 @@ gncScrubBusinessAccountSplits (Account *acc, QofPercentageFunc percentagefunc)
PINFO ("Cleaning up superfluous lot links in account %s \n", str);
xaccAccountBeginEdit(acc);
restart:
curr_split_no = 0;
splits = xaccAccountGetSplitList(acc);
split_count = g_list_length (splits);
for (node = splits; node; node = node->next)
@ -594,7 +610,10 @@ gncScrubBusinessAccountSplits (Account *acc, QofPercentageFunc percentagefunc)
}
if (split)
gncScrubBusinessSplit (split);
// If gncScrubBusinessSplit returns true, a split was deleted and hence
// The account split list has become invalid, so we need to start over
if (gncScrubBusinessSplit (split))
goto restart;
PINFO("Finished processing split %d of %d",
curr_split_no + 1, split_count);

View File

@ -57,7 +57,9 @@ gboolean gncScrubBusinessLot (GNCLot *lot);
/** The gncScrubBusinessSplit() function will fix all issues found with
* the given split.
*
* Currently this function only does one thing: check if the split is
* Current checks are:
*
* * check if the split is
* part of a transaction that was generated as the result of a doubly
* posted invoice/bill/credit note. Refer to
* https://bugzilla.gnome.org/show_bug.cgi?id=754209 to learn how this
@ -66,8 +68,11 @@ gboolean gncScrubBusinessLot (GNCLot *lot);
* a warning is written to the trace file. Considering the user may
* already have added a correcting transaction we leave it up to the user
* to decide whether to also delete the transaction or not.
*
* * remove empty splits, on condition they aren't part of an invoice transaction.
* In this case the function returns true so the caller knows a split was removed.
*/
void gncScrubBusinessSplit (Split *split);
gboolean gncScrubBusinessSplit (Split *split);
/** The gncScrubBusinessAccountLots() function will call
* gncScrubBusinessLot() on each lot in the given account.