[SX-ttinfo.cpp] tidier, using c++ stl and algo

This commit is contained in:
Christopher Lam 2024-06-01 12:23:22 +08:00
parent b1c3b3ee86
commit 47791734bb
10 changed files with 260 additions and 635 deletions

View File

@ -33,6 +33,7 @@
#include <math.h>
#include "assistant-loan.h"
#include "SchedXaction.h"
#include "SchedXaction.hpp"
#include "SX-book.h"
#include "SX-ttinfo.hpp"
#include "gnc-amount-edit.h"
@ -309,9 +310,9 @@ typedef struct toCreateSX_
/** The current 'instance-num' count. */
gint instNum;
/** The main/source transaction being created. */
TTInfo *mainTxn;
TTInfoPtr mainTxn;
/** The optional escrow transaction being created. */
TTInfo *escrowTxn;
TTInfoPtr escrowTxn;
} toCreateSX;
/**************************************************************************/
@ -369,7 +370,6 @@ static void loan_get_ipmt_formula( LoanAssistantData *ldd, GString *gstr );
static float loan_apr_to_simple_formula (float rate, float compounding_periods);
static void loan_create_sxes( LoanAssistantData *ldd );
static gint loan_find_ttsplit_with_acct( gconstpointer elt, gconstpointer crit );
static void loan_create_sx_from_tcSX( LoanAssistantData *ldd, toCreateSX *tcSX );
static void loan_tcSX_free( gpointer data, gpointer user_data );
@ -2494,30 +2494,12 @@ loan_tcSX_free( gpointer data, gpointer user_data )
{
toCreateSX *tcSX = (toCreateSX*)data;
g_free( tcSX->name );
if ( tcSX->mainTxn )
gnc_ttinfo_free( tcSX->mainTxn );
if ( tcSX->escrowTxn )
gnc_ttinfo_free( tcSX->escrowTxn );
tcSX->mainTxn.~TTInfoPtr();
tcSX->escrowTxn.~TTInfoPtr();
g_free( tcSX );
}
/**
* Custom GCompareFunc to find the element of a GList of TTSplitInfo's which
* has the given [Account*] criteria.
* @return 0 if match, as per GCompareFunc in the g_list_find_custom context.
**/
static
gint
loan_find_ttsplit_with_acct( gconstpointer elt,
gconstpointer crit )
{
TTSplitInfo *ttsi = (TTSplitInfo*)elt;
return ( (gnc_ttsplitinfo_get_account( ttsi )
== (Account*)crit) ? 0 : 1 );
}
/**
* Enters into the books a Scheduled Transaction from the given toCreateSX.
**/
@ -2527,7 +2509,7 @@ loan_create_sx_from_tcSX( LoanAssistantData *ldd, toCreateSX *tcSX )
{
SchedXaction *sx;
SchedXactions *sxes;
GList *ttxnList;
TTInfoVec ttxn_vec;
sx = xaccSchedXactionMalloc( gnc_get_current_book() );
xaccSchedXactionSetName( sx, tcSX->name );
@ -2537,24 +2519,29 @@ loan_create_sx_from_tcSX( LoanAssistantData *ldd, toCreateSX *tcSX )
xaccSchedXactionSetEndDate( sx, &tcSX->end );
gnc_sx_set_instance_count( sx, tcSX->instNum );
ttxnList = NULL;
if ( tcSX->mainTxn )
ttxnList = g_list_append( ttxnList, tcSX->mainTxn );
ttxn_vec.push_back (tcSX->mainTxn);
if ( tcSX->escrowTxn )
ttxnList = g_list_append( ttxnList, tcSX->escrowTxn );
ttxn_vec.push_back (tcSX->escrowTxn);
g_assert( ttxnList != NULL );
g_assert (!ttxn_vec.empty());
xaccSchedXactionSetTemplateTrans( sx, ttxnList,
gnc_get_current_book() );
xaccSchedXactionSetTemplateTrans (sx, ttxn_vec, gnc_get_current_book());
sxes = gnc_book_get_schedxactions(gnc_get_current_book());
gnc_sxes_add_sx(sxes, sx);
g_list_free( ttxnList );
ttxnList = NULL;
}
static TTSplitInfoPtr
find_account_from_template_splits (const TTInfoPtr& txn, const Account* account)
{
auto& splits{txn->get_template_splits ()};
auto has_acct = [account](auto ttinfo){ return ttinfo->get_account() == account; };
auto it = std::find_if (splits.begin(), splits.end(), has_acct);
return it == splits.end() ? nullptr : *it;
}
/**
* Does the work to setup the given toCreateSX structure for a specific
* repayment. Note that if the RepayOptData doesn't specify a unique
@ -2632,10 +2619,9 @@ ld_setup_repayment_sx( LoanAssistantData *ldd,
/** Now, the actual implementation... */
GString *gstr;
GList *elt;
TTSplitInfo *fromSplit = NULL;
TTSplitInfo *ttsi;
TTInfo *toTxn = NULL;
TTSplitInfoPtr fromSplit;
TTSplitInfoPtr ttsi;
TTInfoPtr toTxn;
GNCPrintAmountInfo pricePAI = gnc_default_price_print_info(NULL);
#define AMTBUF_LEN 64
gchar amtBuf[AMTBUF_LEN];
@ -2654,16 +2640,11 @@ ld_setup_repayment_sx( LoanAssistantData *ldd,
/* Add the repayment amount into the string of the existing
* ttsplit. */
{
elt = g_list_find_custom(
gnc_ttinfo_get_template_splits( paymentSX->mainTxn ),
ldd->ld.escrowAcct,
loan_find_ttsplit_with_acct );
g_assert( elt );
ttsi = (TTSplitInfo*)elt->data;
g_assert( ttsi );
gstr = g_string_new( gnc_ttsplitinfo_get_debit_formula( ttsi ) );
auto ttsi = find_account_from_template_splits (paymentSX->mainTxn, ldd->ld.escrowAcct);
g_assert (ttsi);
gstr = g_string_new (ttsi->get_debit_formula());
g_string_append_printf( gstr, " + %s", amtBuf );
gnc_ttsplitinfo_set_debit_formula( ttsi, gstr->str );
ttsi->set_debit_formula (gstr->str);
g_string_free( gstr, TRUE );
gstr = NULL;
ttsi = NULL;
@ -2676,24 +2657,16 @@ ld_setup_repayment_sx( LoanAssistantData *ldd,
fromSplit = NULL;
/* tcSX.escrow.split( rep->escrow ).debCred += repAmt */
elt = g_list_find_custom(
gnc_ttinfo_get_template_splits( tcSX->escrowTxn ),
ldd->ld.escrowAcct,
loan_find_ttsplit_with_acct );
ttsi = NULL;
if ( elt )
{
ttsi = (TTSplitInfo*)elt->data;
}
auto ttsi = find_account_from_template_splits (tcSX->escrowTxn, ldd->ld.escrowAcct);
if ( !ttsi )
{
/* create split */
ttsi = gnc_ttsplitinfo_malloc();
gnc_ttsplitinfo_set_memo( ttsi, rod->txnMemo );
gnc_ttsplitinfo_set_account( ttsi, ldd->ld.escrowAcct );
gnc_ttinfo_append_template_split( tcSX->escrowTxn, ttsi );
ttsi = std::make_shared<TTSplitInfo>();
ttsi->set_memo (rod->txnMemo);
ttsi->set_account (ldd->ld.escrowAcct);
tcSX->escrowTxn->append_template_split (ttsi);
}
if ( (str = (gchar*)gnc_ttsplitinfo_get_credit_formula( ttsi ))
if ( (str = (gchar*)ttsi->get_credit_formula ())
== NULL )
{
gstr = g_string_sized_new( 16 );
@ -2707,7 +2680,7 @@ ld_setup_repayment_sx( LoanAssistantData *ldd,
g_string_append_printf( gstr, " + " );
}
g_string_append_printf( gstr, "%s", amtBuf );
gnc_ttsplitinfo_set_credit_formula( ttsi, gstr->str );
ttsi->set_credit_formula (gstr->str);
g_string_free( gstr, TRUE );
gstr = NULL;
ttsi = NULL;
@ -2715,19 +2688,15 @@ ld_setup_repayment_sx( LoanAssistantData *ldd,
else
{
/* (fromSplit = paymentSX.main.split( ldd->ld.repFromAcct )) */
elt = g_list_find_custom(
gnc_ttinfo_get_template_splits( paymentSX->mainTxn ),
ldd->ld.repFromAcct,
loan_find_ttsplit_with_acct );
g_assert( elt );
fromSplit = (TTSplitInfo*)elt->data;
fromSplit = find_account_from_template_splits (paymentSX->mainTxn, ldd->ld.repFromAcct);
g_assert (fromSplit);
/* tcSX.escrow.splits += split( rep->escrow, -repAmt ) */
ttsi = gnc_ttsplitinfo_malloc();
gnc_ttsplitinfo_set_memo( ttsi, rod->txnMemo );
gnc_ttsplitinfo_set_account( ttsi, ldd->ld.escrowAcct );
gnc_ttsplitinfo_set_credit_formula( ttsi, amtBuf );
gnc_ttinfo_append_template_split( tcSX->escrowTxn, ttsi );
ttsi = std::make_shared<TTSplitInfo>();
ttsi->set_memo (rod->txnMemo);
ttsi->set_account (ldd->ld.escrowAcct);
ttsi->set_credit_formula (amtBuf);
tcSX->escrowTxn->append_template_split (ttsi);
ttsi = NULL;
}
}
@ -2741,63 +2710,45 @@ ld_setup_repayment_sx( LoanAssistantData *ldd,
}
else
{
/* (fromSplit = paymentSX.main.split( ldd->ld.repFromAcct )) */
elt = g_list_find_custom(
gnc_ttinfo_get_template_splits( tcSX->mainTxn ),
ldd->ld.repFromAcct,
loan_find_ttsplit_with_acct );
fromSplit = NULL;
if ( elt )
{
/* This is conditionally true in the case of
* a repayment on it's own schedule. */
fromSplit = (TTSplitInfo*)elt->data;
}
fromSplit = find_account_from_template_splits (tcSX->mainTxn, ldd->ld.repFromAcct);
}
}
if ( fromSplit != NULL )
{
/* Update the existing from-split. */
gstr = g_string_new( gnc_ttsplitinfo_get_credit_formula( fromSplit ) );
gstr = g_string_new (fromSplit->get_credit_formula ());
g_string_append_printf( gstr, " + %s", amtBuf );
gnc_ttsplitinfo_set_credit_formula( fromSplit, gstr->str );
fromSplit->set_credit_formula (gstr->str);
g_string_free( gstr, TRUE );
gstr = NULL;
}
else
{
TTInfo *tti;
TTInfoPtr tti;
/* Create a new from-split. */
ttsi = gnc_ttsplitinfo_malloc();
gnc_ttsplitinfo_set_memo( ttsi, rod->txnMemo );
if ( rod->from )
{
gnc_ttsplitinfo_set_account( ttsi, rod->from );
}
else
{
gnc_ttsplitinfo_set_account( ttsi, ldd->ld.repFromAcct );
}
gnc_ttsplitinfo_set_credit_formula( ttsi, amtBuf );
ttsi = std::make_shared<TTSplitInfo>();
ttsi->set_memo (rod->txnMemo);
ttsi->set_account (rod->from ? rod->from : ldd->ld.repFromAcct);
ttsi->set_credit_formula (amtBuf);
tti = tcSX->mainTxn;
if ( rod->throughEscrowP )
{
tti = paymentSX->mainTxn;
}
gnc_ttinfo_append_template_split( tti, ttsi );
tti->append_template_split (ttsi);
ttsi = NULL;
tti = NULL;
}
/* Add to-account split. */
{
ttsi = gnc_ttsplitinfo_malloc();
gnc_ttsplitinfo_set_memo( ttsi, rod->txnMemo );
gnc_ttsplitinfo_set_account( ttsi, rod->to );
gnc_ttsplitinfo_set_debit_formula( ttsi, amtBuf );
gnc_ttinfo_append_template_split( toTxn, ttsi );
ttsi = std::make_shared<TTSplitInfo>();
ttsi->set_memo (rod->txnMemo);
ttsi->set_account (rod->to);
ttsi->set_debit_formula (amtBuf);
toTxn->append_template_split (ttsi);
ttsi = NULL;
}
}
@ -2828,8 +2779,8 @@ loan_create_sxes( LoanAssistantData *ldd )
/* The currently-being-referenced toCreateSX. */
toCreateSX *tcSX;
int i;
TTInfo *ttxn;
TTSplitInfo *ttsi;
TTInfoPtr ttxn;
TTSplitInfoPtr ttsi;
GString *gstr;
paymentSX = g_new0( toCreateSX, 1 );
@ -2849,9 +2800,8 @@ loan_create_sxes( LoanAssistantData *ldd )
(ldd->ld.numPer * ( ldd->ld.perSize == GNC_YEARS ? 12 : 1 ))
- ldd->ld.numMonRemain + 1;
paymentSX->mainTxn = gnc_ttinfo_malloc();
gnc_ttinfo_set_currency( paymentSX->mainTxn,
gnc_default_currency() );
paymentSX->mainTxn = std::make_shared<TTInfo>();
paymentSX->mainTxn->set_currency (gnc_default_currency());
{
GString *payMainTxnDesc = g_string_sized_new( 32 );
@ -2863,8 +2813,7 @@ loan_create_sxes( LoanAssistantData *ldd )
: _("Escrow Payment") )
);
gnc_ttinfo_set_description( paymentSX->mainTxn,
payMainTxnDesc->str );
paymentSX->mainTxn->set_description(payMainTxnDesc->str);
g_string_free( payMainTxnDesc, TRUE );
}
@ -2896,94 +2845,92 @@ loan_create_sxes( LoanAssistantData *ldd )
loan_get_pmt_formula( ldd, gstr );
/* ttxn.splits += split( realSrcAcct, -pmt ); */
{
ttsi = gnc_ttsplitinfo_malloc();
gnc_ttsplitinfo_set_memo( ttsi, ldd->ld.repMemo );
gnc_ttsplitinfo_set_account( ttsi, realSrcAcct );
gnc_ttsplitinfo_set_credit_formula( ttsi, gstr->str );
gnc_ttinfo_append_template_split( ttxn, ttsi );
ttsi = std::make_shared<TTSplitInfo>();
ttsi->set_memo (ldd->ld.repMemo);
ttsi->set_account (realSrcAcct);
ttsi->set_credit_formula (gstr->str);
ttxn->append_template_split (ttsi);
ttsi = NULL;
}
/* ttxn.splits += split( escrowAcct, +pmt ); */
{
ttsi = gnc_ttsplitinfo_malloc();
gnc_ttsplitinfo_set_memo( ttsi, ldd->ld.repMemo );
gnc_ttsplitinfo_set_account( ttsi, ldd->ld.escrowAcct );
gnc_ttsplitinfo_set_debit_formula( ttsi, gstr->str );
gnc_ttinfo_append_template_split( ttxn, ttsi );
ttsi = std::make_shared<TTSplitInfo>();
ttsi->set_memo (ldd->ld.repMemo);
ttsi->set_account (ldd->ld.escrowAcct);
ttsi->set_debit_formula (gstr->str);
ttxn->append_template_split (ttsi);
ttsi = NULL;
}
g_string_free( gstr, TRUE );
gstr = NULL;
paymentSX->escrowTxn = gnc_ttinfo_malloc();
gnc_ttinfo_set_currency( paymentSX->escrowTxn,
gnc_default_currency() );
paymentSX->escrowTxn = std::make_shared<TTInfo>();
paymentSX->escrowTxn->set_currency (gnc_default_currency());
{
GString *escrowTxnDesc;
escrowTxnDesc = g_string_new( ldd->ld.repMemo );
g_string_append_printf( escrowTxnDesc, " - %s", _("Payment") );
gnc_ttinfo_set_description( paymentSX->escrowTxn,
escrowTxnDesc->str );
paymentSX->escrowTxn->set_description (escrowTxnDesc->str);
g_string_free( escrowTxnDesc, TRUE );
}
ttxn = paymentSX->escrowTxn;
}
/* ttxn.splits += split( srcAcct, -pmt ); */
{
ttsi = gnc_ttsplitinfo_malloc();
ttsi = std::make_shared<TTSplitInfo>();
{
gstr = g_string_new( ldd->ld.repMemo );
g_string_append_printf( gstr, " - %s",
_("Payment") );
gnc_ttsplitinfo_set_memo( ttsi, gstr->str );
ttsi->set_memo (gstr->str);
g_string_free( gstr, TRUE );
gstr = NULL;
}
gnc_ttsplitinfo_set_account( ttsi, srcAcct );
ttsi->set_account (srcAcct);
gstr = g_string_sized_new( 32 );
loan_get_pmt_formula( ldd, gstr );
gnc_ttsplitinfo_set_credit_formula( ttsi, gstr->str );
gnc_ttinfo_append_template_split( ttxn, ttsi );
ttsi->set_credit_formula (gstr->str);
ttxn->append_template_split (ttsi);
g_string_free( gstr, TRUE );
gstr = NULL;
ttsi = NULL;
}
/* ttxn.splits += split( ldd->ld.repPriAcct, +ppmt ); */
{
ttsi = gnc_ttsplitinfo_malloc();
ttsi = std::make_shared<TTSplitInfo>();
{
gstr = g_string_new( ldd->ld.repMemo );
g_string_append_printf( gstr, " - %s",
_("Principal") );
gnc_ttsplitinfo_set_memo( ttsi, gstr->str );
ttsi->set_memo (gstr->str);
g_string_free( gstr, TRUE );
gstr = NULL;
}
gnc_ttsplitinfo_set_account( ttsi, ldd->ld.repPriAcct );
ttsi->set_account (ldd->ld.repPriAcct);
gstr = g_string_sized_new( 32 );
loan_get_ppmt_formula( ldd, gstr );
gnc_ttsplitinfo_set_debit_formula( ttsi, gstr->str );
gnc_ttinfo_append_template_split( ttxn, ttsi );
ttsi->set_debit_formula (gstr->str);
ttxn->append_template_split (ttsi);
g_string_free( gstr, TRUE );
gstr = NULL;
ttsi = NULL;
}
/* ttxn.splits += split( ldd->ld.repIntAcct, +ipmt ); */
{
ttsi = gnc_ttsplitinfo_malloc();
ttsi = std::make_shared<TTSplitInfo>();
{
gstr = g_string_new( ldd->ld.repMemo );
g_string_append_printf( gstr, " - %s",
_("Interest") );
gnc_ttsplitinfo_set_memo( ttsi, gstr->str );
ttsi->set_memo (gstr->str);
g_string_free( gstr, TRUE );
gstr = NULL;
}
gnc_ttsplitinfo_set_account( ttsi, ldd->ld.repIntAcct );
ttsi->set_account (ldd->ld.repIntAcct);
gstr = g_string_sized_new( 32 );
loan_get_ipmt_formula( ldd, gstr );
gnc_ttsplitinfo_set_debit_formula( ttsi, gstr->str );
gnc_ttinfo_append_template_split( ttxn, ttsi );
ttsi->set_debit_formula (gstr->str);
ttxn->append_template_split (ttsi);
g_string_free( gstr, TRUE );
gstr = NULL;
ttsi = NULL;
@ -3015,16 +2962,12 @@ loan_create_sxes( LoanAssistantData *ldd )
tcSX->instNum =
ld_calc_sx_instance_num(&tcSX->start, rod->schedule);
rod->schedule = NULL;
tcSX->mainTxn = gnc_ttinfo_malloc();
gnc_ttinfo_set_currency( tcSX->mainTxn,
gnc_default_currency() );
gnc_ttinfo_set_description( tcSX->mainTxn,
gstr->str );
tcSX->escrowTxn = gnc_ttinfo_malloc();
gnc_ttinfo_set_currency( tcSX->escrowTxn,
gnc_default_currency() );
gnc_ttinfo_set_description( tcSX->escrowTxn,
gstr->str );
tcSX->mainTxn = std::make_shared<TTInfo>();
tcSX->mainTxn->set_currency(gnc_default_currency());
tcSX->mainTxn->set_description (gstr->str);
tcSX->escrowTxn = std::make_shared<TTInfo>();
tcSX->escrowTxn->set_currency (gnc_default_currency());
tcSX->escrowTxn->set_description(gstr->str);
g_string_free( gstr, TRUE );
gstr = NULL;

View File

@ -42,6 +42,7 @@
#include "qof.h"
#include "Recurrence.h"
#include "SchedXaction.h"
#include "SchedXaction.hpp"
#include "SX-book.h"
#include "SX-ttinfo.hpp"
#include <glib/gi18n.h>
@ -207,28 +208,29 @@ sxftd_add_template_trans(SXFromTransInfo *sxfti)
{
Transaction *tr = sxfti->trans;
GList *tt_list = NULL;
GList *splits, *template_splits = NULL;
TTInfo *tti = gnc_ttinfo_malloc();
TTSplitInfo *ttsi;
GList *splits = NULL;
TTInfoPtr tti = std::make_shared<TTInfo>();
TTSplitInfoPtr ttsi;
gnc_numeric runningBalance;
gnc_numeric split_value;
const char *tmpStr;
runningBalance = gnc_numeric_zero();
gnc_ttinfo_set_description(tti, xaccTransGetDescription(tr));
gnc_ttinfo_set_num(tti, gnc_get_num_action(tr, NULL));
gnc_ttinfo_set_notes (tti, xaccTransGetNotes (tr));
gnc_ttinfo_set_currency(tti, xaccTransGetCurrency(tr));
tti->set_description(xaccTransGetDescription(tr));
tti->set_num(gnc_get_num_action(tr, NULL));
tti->set_notes (xaccTransGetNotes (tr));
tti->set_currency(xaccTransGetCurrency(tr));
tti->clear_template_splits ();
for (splits = xaccTransGetSplitList(tr); splits; splits = splits->next)
{
auto sp = GNC_SPLIT(splits->data);
ttsi = gnc_ttsplitinfo_malloc();
gnc_ttsplitinfo_set_action(ttsi, gnc_get_num_action(NULL, sp));
ttsi = std::make_shared<TTSplitInfo>();
ttsi->set_action (gnc_get_num_action(NULL, sp));
split_value = xaccSplitGetValue(sp);
gnc_ttsplitinfo_set_memo(ttsi, xaccSplitGetMemo(sp));
ttsi->set_memo (xaccSplitGetMemo(sp));
runningBalance = gnc_numeric_add( runningBalance, split_value,
100, (GNC_DENOM_AUTO | GNC_HOW_DENOM_LCD) );
@ -237,20 +239,20 @@ sxftd_add_template_trans(SXFromTransInfo *sxfti)
{
tmpStr = xaccPrintAmount( split_value,
gnc_default_print_info(FALSE) );
gnc_ttsplitinfo_set_debit_formula( ttsi, tmpStr );
ttsi->set_debit_formula (tmpStr);
}
else
{
/* Negate the numeric so it prints w/o the sign at the front. */
tmpStr = xaccPrintAmount( gnc_numeric_neg( split_value ),
gnc_default_print_info(FALSE) );
gnc_ttsplitinfo_set_credit_formula( ttsi, tmpStr );
ttsi->set_credit_formula (tmpStr);
}
/* Copy over per-split account info */
gnc_ttsplitinfo_set_account( ttsi, xaccSplitGetAccount( sp ) );
ttsi->set_account (xaccSplitGetAccount (sp));
template_splits = g_list_append(template_splits, ttsi);
tti->append_template_split (ttsi);
}
if ( ! gnc_numeric_zero_p( runningBalance )
@ -265,13 +267,8 @@ sxftd_add_template_trans(SXFromTransInfo *sxfti)
return SXFTD_ERRNO_UNBALANCED_XACTION;
}
gnc_ttinfo_set_template_splits(tti, template_splits);
tt_list = g_list_append(tt_list, tti);
gnc_suspend_gui_refresh ();
xaccSchedXactionSetTemplateTrans(sxfti->sx, tt_list,
gnc_get_current_book ());
xaccSchedXactionSetTemplateTrans (sxfti->sx, { tti }, gnc_get_current_book ());
gnc_resume_gui_refresh ();
return 0;

View File

@ -29,6 +29,7 @@
#include "gnc-session.h"
#include "gnc-sx-instance-model.h"
#include "gnc-ui-util.h"
#include "SchedXaction.hpp"
#include "test-stuff.h"
#include "test-engine-stuff.h"
@ -222,13 +223,13 @@ test_state_changes()
}
static void
make_one_transaction_begin(TTInfo **tti, Account **account1, Account **account2)
make_one_transaction_begin (TTInfoPtr& tti, Account **account1, Account **account2)
{
QofBook *book = qof_session_get_book(gnc_get_current_session());
*account1 = get_random_account(book);
*account2 = get_random_account(book);
*tti = gnc_ttinfo_malloc();
tti = std::make_shared<TTInfo>();
// Both accounts need to have the same currency
xaccAccountBeginEdit(*account2);
@ -238,41 +239,37 @@ make_one_transaction_begin(TTInfo **tti, Account **account1, Account **account2)
}
static void
make_one_transaction_end(TTInfo **tti, SchedXaction *sx)
make_one_transaction_end(TTInfoPtr& tti, SchedXaction *sx)
{
QofBook *book = qof_session_get_book(gnc_get_current_session());
GList *txns = g_list_append(NULL, *tti);
xaccSchedXactionSetTemplateTrans(sx, txns, book);
gnc_ttinfo_free(*tti);
*tti = NULL;
g_list_free (txns);
xaccSchedXactionSetTemplateTrans (sx, { tti }, book);
}
static void
make_one_transaction_with_two_splits(SchedXaction *sx, const char *value1,
const char *value2, int set_txcurr)
{
TTInfo *tti;
TTInfoPtr tti;
Account *account1;
Account *account2;
make_one_transaction_begin(&tti, &account1, &account2);
make_one_transaction_begin (tti, &account1, &account2);
if (set_txcurr)
gnc_ttinfo_set_currency(tti, xaccAccountGetCommodity(account1));
tti->set_currency (xaccAccountGetCommodity(account1));
TTSplitInfo *split1 = gnc_ttsplitinfo_malloc();
TTSplitInfo *split2 = gnc_ttsplitinfo_malloc();
TTSplitInfoPtr split1 = std::make_shared<TTSplitInfo>();
TTSplitInfoPtr split2 = std::make_shared<TTSplitInfo>();
gnc_ttsplitinfo_set_account(split1, account1);
gnc_ttsplitinfo_set_debit_formula(split1, value1);
gnc_ttinfo_append_template_split(tti, split1);
split1->set_account (account1);
split1->set_debit_formula (value1);
tti->append_template_split (split1);
gnc_ttsplitinfo_set_account(split2, account2);
gnc_ttsplitinfo_set_credit_formula(split2, value2);
gnc_ttinfo_append_template_split(tti, split2);
split2->set_account (account2);
split2->set_credit_formula (value2);
tti->append_template_split (split2);
make_one_transaction_end(&tti, sx);
make_one_transaction_end (tti, sx);
}
static void

View File

@ -36,6 +36,7 @@ set (engine_HEADERS
FreqSpec.h
Recurrence.h
SchedXaction.h
SchedXaction.hpp
SX-book.h
SX-ttinfo.hpp
Query.h
@ -141,7 +142,6 @@ set (engine_SOURCES
Query.cpp
SchedXaction.cpp
SX-book.cpp
SX-ttinfo.cpp
Scrub.cpp
Scrub2.cpp
Scrub3.cpp

View File

@ -1,358 +0,0 @@
/********************************************************************\
* SX-ttinfo.c -- Template Transaction manipulation functions *
* for scheduled transactions *
* Copyright (C) 2001 Robert Merkel <rgmerk@mira.net> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
#include <config.h>
#include "SX-ttinfo.hpp"
struct TTInfo_s
{
char *description; /* owned by us */
char *num; /* owned by us */
char *notes;
gnc_commodity *common_currency; /* not freed */
GList *splits; /* list of template splits, owned by us */
};
struct TTSplitInfo_s
{
char *action; /* owned by us */
char *memo; /* owned by us */
char *credit_formula, *debit_formula; /* owned by us */
Account *acc;
};
TTInfo *
gnc_ttinfo_malloc(void)
{
TTInfo *tti = g_new0(TTInfo, 1);
return tti;
}
static void
delete_splitinfo(gpointer data, gpointer user_data)
{
gnc_ttsplitinfo_free( (TTSplitInfo *) data);
return;
}
void gnc_ttinfo_free(TTInfo *info)
{
g_return_if_fail(info);
g_free(info->description);
g_free(info->num);
g_free (info->notes);
g_list_foreach(info->splits,
delete_splitinfo,
NULL);
g_list_free(info->splits);
g_free(info);
return;
}
void
gnc_ttinfo_set_description(TTInfo *tti, const char *description)
{
g_return_if_fail(tti);
if (tti->description)
{
g_free(tti->description);
}
tti->description = g_strdup(description);
return;
}
const char *
gnc_ttinfo_get_description(TTInfo *tti)
{
g_return_val_if_fail(tti, NULL);
return tti->description;
}
void
gnc_ttinfo_set_num(TTInfo *tti, const char *num)
{
g_return_if_fail(tti);
if (tti->num)
{
g_free(tti->num);
}
tti->num = g_strdup(num);
return;
}
const char*
gnc_ttinfo_get_num(TTInfo *tti)
{
g_return_val_if_fail(tti, NULL);
return tti->num;
}
void
gnc_ttinfo_set_notes (TTInfo *tti, const char *notes)
{
g_return_if_fail (tti);
if (tti->notes)
{
g_free (tti->notes);
}
tti->notes = g_strdup (notes);
return;
}
const char*
gnc_ttinfo_get_notes (TTInfo *tti)
{
g_return_val_if_fail (tti, NULL);
return tti->notes;
}
void
gnc_ttinfo_set_currency(TTInfo *tti, gnc_commodity *common_currency)
{
g_return_if_fail(tti);
tti->common_currency = common_currency;
return;
}
gnc_commodity *
gnc_ttinfo_get_currency(TTInfo *tti)
{
g_return_val_if_fail(tti, NULL);
return tti->common_currency;
}
void gnc_ttinfo_set_template_splits(TTInfo *tti, GList *splits)
{
g_return_if_fail(tti);
tti->splits = splits;
return;
}
void gnc_ttinfo_append_template_split(TTInfo *tti, TTSplitInfo *split_i)
{
g_return_if_fail(tti && split_i);
tti->splits = g_list_append(tti->splits, split_i);
return;
}
GList *
gnc_ttinfo_get_template_splits(TTInfo *tti)
{
g_return_val_if_fail(tti, NULL);
return tti->splits;
}
TTSplitInfo *
gnc_ttsplitinfo_malloc(void)
{
TTSplitInfo *ttsi = g_new0(TTSplitInfo, 1);
return ttsi;
}
void
gnc_ttsplitinfo_free(TTSplitInfo *ttsi)
{
if ( ttsi->action )
g_free(ttsi->action);
if ( ttsi->memo )
g_free(ttsi->memo);
if ( ttsi->credit_formula )
g_free(ttsi->credit_formula);
if ( ttsi->debit_formula )
g_free(ttsi->debit_formula);
g_free(ttsi);
return;
}
void
gnc_ttsplitinfo_set_action(TTSplitInfo *ttsi, const char *action)
{
g_return_if_fail(ttsi);
if (ttsi->action)
g_free(ttsi->action);
ttsi->action = g_strdup(action);
return;
}
const char *
gnc_ttsplitinfo_get_action(TTSplitInfo *ttsi)
{
g_return_val_if_fail(ttsi, NULL);
return ttsi->action;
}
void
gnc_ttsplitinfo_set_memo(TTSplitInfo *ttsi, const char *memo)
{
g_return_if_fail(ttsi);
if (ttsi->memo)
g_free(ttsi->memo);
ttsi->memo = g_strdup(memo);
return;
}
const char *
gnc_ttsplitinfo_get_memo(TTSplitInfo *ttsi)
{
g_return_val_if_fail(ttsi, NULL);
return ttsi->memo;
}
void
gnc_ttsplitinfo_set_credit_formula_numeric(TTSplitInfo *ttsi, gnc_numeric credit)
{
g_return_if_fail(ttsi);
if (ttsi->credit_formula)
g_free(ttsi->credit_formula);
ttsi->credit_formula = gnc_numeric_to_string(credit);
if (ttsi->debit_formula)
{
g_free(ttsi->debit_formula);
ttsi->debit_formula = NULL;
}
}
void
gnc_ttsplitinfo_set_credit_formula(TTSplitInfo *ttsi, const char *credit_formula)
{
g_return_if_fail(ttsi);
if (ttsi->credit_formula)
g_free(ttsi->credit_formula);
ttsi->credit_formula = g_strdup(credit_formula);
if (ttsi->debit_formula)
{
g_free(ttsi->debit_formula);
ttsi->debit_formula = NULL;
}
return;
}
const char *
gnc_ttsplitinfo_get_credit_formula(TTSplitInfo *ttsi)
{
g_return_val_if_fail(ttsi, NULL);
return ttsi->credit_formula;
}
const char *
gnc_ttsplitinfo_get_debit_formula(TTSplitInfo *ttsi)
{
g_return_val_if_fail(ttsi, NULL);
return ttsi->debit_formula;
}
void
gnc_ttsplitinfo_set_debit_formula_numeric(TTSplitInfo *ttsi, gnc_numeric debit)
{
g_return_if_fail(ttsi);
if (ttsi->debit_formula)
{
g_free(ttsi->debit_formula);
}
ttsi->debit_formula = gnc_numeric_to_string(debit);
if (ttsi->credit_formula)
{
g_free(ttsi->credit_formula);
ttsi->credit_formula = NULL;
}
return;
}
void
gnc_ttsplitinfo_set_debit_formula(TTSplitInfo *ttsi, const char *debit_formula)
{
g_return_if_fail(ttsi);
if (ttsi->debit_formula)
g_free(ttsi->debit_formula);
ttsi->debit_formula = g_strdup(debit_formula);
if (ttsi->credit_formula)
{
g_free(ttsi->credit_formula);
ttsi->credit_formula = NULL;
}
return;
}
void
gnc_ttsplitinfo_set_account(TTSplitInfo *ttsi, Account *acc)
{
g_return_if_fail(ttsi && acc);
ttsi->acc = acc;
return;
}
Account *
gnc_ttsplitinfo_get_account(TTSplitInfo *ttsi)
{
g_return_val_if_fail(ttsi, NULL);
return ttsi->acc;
}

View File

@ -30,61 +30,91 @@
#include "SchedXaction.h"
#include "Account.h"
#include "gnc-commodity.h"
#include "gnc-numeric.hpp"
typedef struct TTInfo_s TTInfo;
typedef struct TTSplitInfo_s TTSplitInfo;
#include <vector>
#include <optional>
#include <string>
#include <algorithm>
#include <memory>
TTInfo *gnc_ttinfo_malloc(void);
struct OptionalString
{
const char* operator* () const { return m_str ? m_str->c_str() : nullptr; };
void operator= (const char* str) { if (str) m_str = str; else reset(); };
void reset () { m_str = std::nullopt; };
protected:
std::optional<std::string> m_str;
};
void gnc_ttinfo_free(TTInfo *info);
struct OptionalStringFromNumeric : public OptionalString
{
using OptionalString::operator=;
void operator= (std::optional<gnc_numeric> num)
{ if (num) m_str = GncNumeric (*num).to_string(); else reset(); }
};
/* these two deep-copy their arguments */
void gnc_ttinfo_set_description(TTInfo *tti, const char *description);
void gnc_ttinfo_set_num(TTInfo *tti, const char *num);
void gnc_ttinfo_set_notes (TTInfo *tti, const char *notes);
struct TTSplitInfo
{
OptionalString m_action;
OptionalString m_memo;
OptionalStringFromNumeric m_credit_formula, m_debit_formula;
Account *m_acc = nullptr;
/* this one points to a persistent pointer so ownership isn't relevant */
void gnc_ttinfo_set_currency(TTInfo *tti, gnc_commodity *common_currency);
const char* get_action () const { return *m_action; };
const char* get_memo () const { return *m_memo; };
const Account* get_account () const { return m_acc; };
const char* get_credit_formula () const { return *m_credit_formula; };
const char* get_debit_formula () const { return *m_debit_formula; };
void set_action (const char *action) { m_action = action; };
void set_memo (const char *memo) { m_memo = memo; };
void set_account (Account *acc) { m_acc = acc; };
void set_credit_formula (const char *credit_formula) { set_formulas (nullptr, credit_formula); };
void set_debit_formula (const char *debit_formula) { set_formulas (debit_formula, nullptr); };
void set_credit_formula_numeric (gnc_numeric num) { set_formulas_numeric ({}, num); };
void set_debit_formula_numeric (gnc_numeric num) { set_formulas_numeric (num, {}); };
/* no deep copy occurs - if you want a deep copy make one yourself ! */
void gnc_ttinfo_set_template_splits(TTInfo *tti, GList *splits);
private:
void set_formulas (const char* debit, const char *credit)
{
m_debit_formula = debit;
m_credit_formula = credit;
}
void set_formulas_numeric (std::optional<gnc_numeric> debit, std::optional<gnc_numeric> credit)
{
m_debit_formula = debit;
m_credit_formula = credit;
}
};
const char * gnc_ttinfo_get_description(TTInfo *tti);
const char * gnc_ttinfo_get_num(TTInfo *tti);
const char *gnc_ttinfo_get_notes (TTInfo *tti);
gnc_commodity * gnc_ttinfo_get_currency(TTInfo *tti);
GList * gnc_ttinfo_get_template_splits(TTInfo *tti);
using TTSplitInfoPtr = std::shared_ptr<TTSplitInfo>;
using TTSplitInfoVec = std::vector<TTSplitInfoPtr>;
/* split_i IS NOT deep copied and becomes owned by TTI */
void gnc_ttinfo_append_template_split(TTInfo *tti, TTSplitInfo *split_i);
struct TTInfo
{
OptionalString m_description;
OptionalString m_num;
OptionalString m_notes;
gnc_commodity *m_common_currency = nullptr;
TTSplitInfoVec m_splits;
TTSplitInfo * gnc_ttsplitinfo_malloc(void);
void gnc_ttsplitinfo_free(TTSplitInfo *split_i);
const char* get_description () const { return *m_description; };
const char* get_num () const { return *m_num; };
const char* get_notes () const { return *m_notes; };
gnc_commodity* get_currency () const { return m_common_currency; };
const TTSplitInfoVec& get_template_splits () const { return m_splits; };
void gnc_ttsplitinfo_set_action(TTSplitInfo *split_i, const char *action);
const char * gnc_ttsplitinfo_get_action(TTSplitInfo *split_i);
void set_description (const char *description) { m_description = description; };
void set_num (const char *num) { m_num = num; };
void set_notes (const char *notes) { m_notes = notes; };
void set_currency (gnc_commodity *currency) { m_common_currency = currency; };
void clear_template_splits () { m_splits.clear(); };
void append_template_split (TTSplitInfoPtr& ttsi) { m_splits.push_back (ttsi); };
;
};
void gnc_ttsplitinfo_set_memo(TTSplitInfo *split_i, const char *memo);
const char *gnc_ttsplitinfo_get_memo(TTSplitInfo *split_i);
void gnc_ttsplitinfo_set_credit_formula(TTSplitInfo *split_i,
const char *credit_formula);
void gnc_ttsplitinfo_set_credit_formula_numeric(TTSplitInfo *split_i,
gnc_numeric credit_formula);
const char *gnc_ttsplitinfo_get_credit_formula(TTSplitInfo *split_i);
void gnc_ttsplitinfo_set_debit_formula(TTSplitInfo *split_i,
const char *debit_formula);
void gnc_ttsplitinfo_set_debit_formula_numeric(TTSplitInfo *split_i,
gnc_numeric debit_formula);
const char *gnc_ttsplitinfo_get_debit_formula(TTSplitInfo *split_i);
void gnc_ttsplitinfo_set_account(TTSplitInfo *split_i, Account *acc);
Account *gnc_ttsplitinfo_get_account(TTSplitInfo *split_i);
using TTInfoPtr = std::shared_ptr<TTInfo>;
using TTInfoVec = std::vector<TTInfoPtr>;
#endif

View File

@ -36,6 +36,7 @@
#include "SX-book-p.h"
#include "SX-ttinfo.hpp"
#include "SchedXaction.h"
#include "SchedXaction.hpp"
#include "Transaction.h"
#include "gnc-engine.h"
#include "engine-helpers.h"
@ -963,7 +964,7 @@ xaccSchedXactionGetSplits( const SchedXaction *sx )
}
static Split *
pack_split_info (TTSplitInfo *s_info, Account *parent_acct,
pack_split_info (TTSplitInfoPtr s_info, Account *parent_acct,
Transaction *parent_trans, QofBook *book)
{
Split *split;
@ -973,20 +974,18 @@ pack_split_info (TTSplitInfo *s_info, Account *parent_acct,
split = xaccMallocSplit(book);
xaccSplitSetMemo(split,
gnc_ttsplitinfo_get_memo(s_info));
xaccSplitSetMemo(split, s_info->get_memo());
/* Set split-action with gnc_set_num_action which is the same as
* xaccSplitSetAction with these arguments */
gnc_set_num_action(NULL, split, NULL,
gnc_ttsplitinfo_get_action(s_info));
gnc_set_num_action(NULL, split, NULL, s_info->get_action());
xaccAccountInsertSplit(parent_acct,
split);
credit_formula = gnc_ttsplitinfo_get_credit_formula(s_info);
debit_formula = gnc_ttsplitinfo_get_debit_formula(s_info);
acc_guid = qof_entity_get_guid(QOF_INSTANCE(gnc_ttsplitinfo_get_account(s_info)));
credit_formula = s_info->get_credit_formula ();
debit_formula = s_info->get_debit_formula ();
acc_guid = qof_entity_get_guid(QOF_INSTANCE(s_info->get_account ()));
qof_instance_set (QOF_INSTANCE (split),
"sx-credit-formula", credit_formula,
"sx-debit-formula", debit_formula,
@ -998,46 +997,31 @@ pack_split_info (TTSplitInfo *s_info, Account *parent_acct,
void
xaccSchedXactionSetTemplateTrans(SchedXaction *sx, GList *t_t_list,
QofBook *book)
xaccSchedXactionSetTemplateTrans (SchedXaction *sx, const TTInfoVec& tt_vec, QofBook *book)
{
Transaction *new_trans;
Split *new_split;
GList *split_list;
g_return_if_fail (book);
/* delete any old transactions, if there are any */
delete_template_trans( sx );
for (; t_t_list != NULL; t_t_list = t_t_list->next)
for (auto tti : tt_vec)
{
auto tti = static_cast<TTInfo*>(t_t_list->data);
new_trans = xaccMallocTransaction(book);
xaccTransBeginEdit(new_trans);
xaccTransSetDescription(new_trans,
gnc_ttinfo_get_description(tti));
xaccTransSetDescription(new_trans, tti->get_description());
xaccTransSetDatePostedSecsNormalized(new_trans, gnc_time (NULL));
/* Set tran-num with gnc_set_num_action which is the same as
* xaccTransSetNum with these arguments */
gnc_set_num_action(new_trans, NULL,
gnc_ttinfo_get_num(tti), NULL);
xaccTransSetNotes (new_trans, gnc_ttinfo_get_notes (tti));
xaccTransSetCurrency( new_trans,
gnc_ttinfo_get_currency(tti) );
gnc_set_num_action (new_trans, NULL, tti->get_num(), NULL);
xaccTransSetNotes (new_trans, tti->get_notes ());
xaccTransSetCurrency (new_trans, tti->get_currency ());
for (split_list = gnc_ttinfo_get_template_splits(tti);
split_list;
split_list = split_list->next)
for (auto s_info : tti->get_template_splits())
{
auto s_info = static_cast<TTSplitInfo*>(split_list->data);
new_split = pack_split_info(s_info, sx->template_acct,
new_trans, book);
auto new_split = pack_split_info(s_info, sx->template_acct, new_trans, book);
xaccTransAppendSplit(new_trans, new_split);
}
xaccTransCommitEdit(new_trans);

View File

@ -281,15 +281,6 @@ SXTmpStateData *gnc_sx_clone_temporal_state( SXTmpStateData *stateData );
GDate xaccSchedXactionGetNextInstance(const SchedXaction *sx,
SXTmpStateData *stateData);
/** \brief Set the schedxaction's template transaction.
t_t_list is a glist of TTInfo's as defined in SX-ttinfo.h.
The edit dialog doesn't use this mechanism; maybe it should.
*/
void xaccSchedXactionSetTemplateTrans( SchedXaction *sx,
GList *t_t_list,
QofBook *book );
/** \brief Adds an instance to the deferred list of the SX.
Added instances are added in date-sorted order.

View File

@ -0,0 +1,42 @@
/********************************************************************\
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
* *
\********************************************************************/
#ifndef XACC_SCHEDXACTION_HPP
#define XACC_SCHEDXACTION_HPP
#include "qof.h"
#include "gnc-engine.h"
#include <vector>
#include "SX-ttinfo.hpp"
/** \brief Set the schedxaction's template transaction. tt_vec is a
vector of TTInfo's as defined in SX-ttinfo.hpp The edit dialog doesn't
use this mechanism; maybe it should.
@param sx SchedXaction* the SchedXaction to modify
@param tt_vec TTInfoVec vector of transaction templates to assign to the SX
@param book QofBook* the book that the SX belongs to
*/
void xaccSchedXactionSetTemplateTrans (SchedXaction*, const TTInfoVec&, QofBook*);
#endif /* XACC_SCHEDXACTION_HPP */

View File

@ -691,7 +691,6 @@ libgnucash/engine/ScrubBusiness.c
libgnucash/engine/Scrub.cpp
libgnucash/engine/Split.cpp
libgnucash/engine/SX-book.cpp
libgnucash/engine/SX-ttinfo.cpp
libgnucash/engine/Transaction.cpp
libgnucash/engine/TransLog.cpp
libgnucash/gnc-module/example/gncmod-example.c