2003-08-25 13:36:54 -05:00
|
|
|
/********************************************************************\
|
|
|
|
* cap-gains.c -- Automatically Compute Capital Gains/Losses *
|
|
|
|
* *
|
|
|
|
* 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 *
|
2005-11-16 23:35:02 -06:00
|
|
|
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
|
|
|
|
* Boston, MA 02110-1301, USA gnu@gnu.org *
|
2003-08-25 13:36:54 -05:00
|
|
|
\********************************************************************/
|
|
|
|
|
|
|
|
/** @file cap-gains.c
|
2005-11-01 21:32:36 -06:00
|
|
|
* @brief Utilities to Automatically Compute Capital Gains/Losses.
|
2003-08-25 13:36:54 -05:00
|
|
|
* @author Created by Linas Vepstas August 2003
|
2004-05-29 15:06:03 -05:00
|
|
|
* @author Copyright (c) 2003,2004 Linas Vepstas <linas@linas.org>
|
2003-08-25 13:36:54 -05:00
|
|
|
*
|
|
|
|
* This file implements the various routines to automatically
|
2010-03-02 15:40:28 -06:00
|
|
|
* compute and handle Cap Gains/Losses resulting from trading
|
|
|
|
* activities. Some of these routines might have broader
|
|
|
|
* applicability, for handling depreciation & etc.
|
2003-08-25 13:36:54 -05:00
|
|
|
*
|
2004-07-10 09:33:09 -05:00
|
|
|
* This code is under development, and is 'beta': we think we're
|
|
|
|
* mostly done, and we've tested and "things work for us", but there
|
2010-03-02 15:40:28 -06:00
|
|
|
* may still be something missing, and there might still be some
|
2004-07-10 09:33:09 -05:00
|
|
|
* bugs.
|
2003-09-20 19:53:49 -05:00
|
|
|
*
|
2010-03-02 15:40:28 -06:00
|
|
|
* This code uses a 'gains dirty' flag: A 'dirty' flag on the source
|
2003-09-20 19:53:49 -05:00
|
|
|
* split indicates that the gains transaction needs to be recomputed.
|
|
|
|
* Another flag, the gains transaction flag, marks the split as
|
2010-03-02 15:40:28 -06:00
|
|
|
* being a gains split, and that the source transaction should be
|
2003-09-20 19:53:49 -05:00
|
|
|
* checked for dirtiness before returning the date, the amount, the
|
|
|
|
* value, etc. Finally, these flags make amount and value read-only
|
|
|
|
* for the gains splits. (the memo is user-modifieable).
|
2010-03-02 15:40:28 -06:00
|
|
|
*
|
2003-09-20 19:53:49 -05:00
|
|
|
* If the amount in a split is changed, then the lot has to be recomputed.
|
2010-03-02 15:40:28 -06:00
|
|
|
* This has a potential trickle-through effect on all later lots.
|
|
|
|
* Ideally, later lots are dissolved, and recomputed. However, some
|
2003-09-20 19:53:49 -05:00
|
|
|
* lots may have been user-hand-built. These should be left alone.
|
|
|
|
*
|
|
|
|
ToDo:
|
2003-09-06 10:02:21 -05:00
|
|
|
o XXX Need to create a data-integrity scrubber, tht makes sure that
|
|
|
|
the various flags, and pointers & etc. match. See sections marked
|
|
|
|
with XXX below for things that might go wrong.
|
2003-08-25 13:36:54 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
2005-11-19 17:53:34 -06:00
|
|
|
#include <glib/gi18n.h>
|
2003-08-25 13:36:54 -05:00
|
|
|
|
|
|
|
#include "AccountP.h"
|
2003-09-16 07:44:01 -05:00
|
|
|
#include "Scrub2.h"
|
|
|
|
#include "Scrub3.h"
|
2003-08-25 13:36:54 -05:00
|
|
|
#include "Transaction.h"
|
|
|
|
#include "TransactionP.h"
|
2003-08-25 15:02:41 -05:00
|
|
|
#include "cap-gains.h"
|
2003-08-25 13:36:54 -05:00
|
|
|
#include "gnc-engine.h"
|
Implement a Book Option to give users the choice to use the transaction number field (default and current behaviour) or, alternatively, the split action field of the anchor split for 'Num' column on registers, reports, import and export, where applicable. The difference between the two is that with the default, the same 'Num' is displayed for a given transaction regardless of what register it is viewed in and with the new option, a different 'Num' can be entered/displayed for a given transaction in each register it is viewed in. In both cases, the 'next number logic' of the 'Num' cell is based on the account of the register the transaction is entered/viewed from but in one case it fills the transaction number, in the other, it fills the split action field of the anchor split (with the transaction number field displayed in the register cell labeled 'T-Num'). In both cases, both fields are visible and can be used as free-form text in double-line mode for any value the user wants. If the new option is not selected, there should be no change in Gnucash's behaviour with the exception of displaying the book options dialog in 'new book' situations.
The changes made are:
1. Implement the new book option to select source for 'num' cell:
libqof/qof/qofbookslots.h - define num source option
engine/engine.i - define 'NUM-FIELD-SOURCE' as an option name and
'SPLIT-ACTION' as a term for sorting queries
app-utils/business-prefs.scm - define book option for num-field-source
app-utils/app-utils.scm - define and export book option for num-field-source
2. Implement functions to get book option setting and to test:
libqof/qof/qofbook.c & .h - define function to get number source book option
libqof/qof/test/test-qofbook.c - test function to get number source book option
3. Implement a notification mechanism for book option changes:
engine/engine-helpers.c & .h - define functions to register callbacks,
define function to remove callbacks and
define function to initiate callbacks on book num-source option changes
core-utils/gnc-features.c & .h - define feature for num-source
app-utils/gnc-ui-util.c & .h - define gnc_book_option_num_field_source_change_cb;
include setting of feature flag on initial use
gnome-utils/gnc-main-window.c - change name of gnc_options_dialog_apply_cb
function to gnc_book_options_dialog_apply_cb and adjust function to
call function gnc_book_option_num_field_source_change_cb when book
option for num-field-source changes; change name of
gnc_options_dialog_close_cb to gnc_book_options_dialog_close_cb
4. Implement 'help' button response on Book Option Dialog:
gnome-utils/gnc-ui.h - define "book-options" section of help manual
gnome-utils/gnc-gnome-utils.c & .h - implement Book Options help callback
gnome-utils/gnc-main-window.c - use gnc_options_dialog_set_book_options_help_cb
to make help button active
5. Implement utility functions to get/set number/action based on book option:
engine/engine-helpers.c & .h - define 'gnc_get_num_action' function
define 'gnc_get_action_num' function
define 'gnc_set_num_action' function
engine/engine.i - make get/set num/action functions available to guile &
allow #f to be used in place of NULL from guile
engine/Transaction.h - add reference to utility functions in comments to
xaccTransSetNum and xaccTransGetNum functions
engine/Split.h - add reference to utility functions in comments to
xaccSplitSetAction and xaccSplitGetAction functions
6. Implement register use of num source book option; for General, Income and Search
Ledgers, since there is no anchor split, always show tran-num in transaction
cursor, but vary label to correspond to book option:
register/ledger-core/split-register.h - define TNUM_CELL
define CURSOR_DOUBLE_LEDGER_NUM_ACTN cursor name to use TNUM_CELL
define CURSOR_DOUBLE_JOURNAL_NUM_ACTN cursor name to use TNUM_CELL
add boolean to split_reg structure to hold num source book option
register/ledger-core/split-register-layout.c -
add logic to define and set up new ledger/journal cursors with TNUM_CELLs
add logic to add the new cursors to a layout
add logic to add TNUM_CELL to a layout
register/ledger-core/split-register-model.c -
define function to get TNUM_CELL label
add logic to recognize ledger/journal cursors with TNUM_CELL
modify function to get NUM_CELL value based on book option and register
type
define function to get TNUM_CELL value
adjust num help text to reflect book option choice
define function to get TNUM_CELL help
modify function to get action help to reflect book option choice
set up handlers for TNUM_CELL
register/ledger-core/split-register-model-save.c -
use 'gnc_set_num_action' function to set num/action for NUM_CELL based
on book option
define function to save TNUM_CELL using 'gnc_set_num_action' function
set save handler for TNUM_CELL
register/ledger-core/split-register-util.c -
modify logic to recognize ledger/journal cursors with tnum_cell
register/ledger-core/split-register.c -
define split_register_book_option_changed function
register split_register_book_option_changed function in register init
initialize register structure setting to book option
remove split_register_book_option_changed function in register destroy
modify gnc_split_register_save_to_scm to handle TNUM_CELL
7. Enhance transaction copy/duplicate logic to accommodate new book option:
gnome/gtkbuilder/gnc-plugin-page-register.glade -
add widgets to handle trans number and/or split action based on book option
gnome-utils/dialog-dup-trans.c & .h -
add capability to display and handle 'Num' field according to book option
gnome/gnc-plugin-page-register.c - define gnc_find_register_by_account
register/ledger-core/split-register.c -
modify gnc_split_register_duplicate_current to accomodate book option
setting
register/ledger-core/Makefile.am - add GNOME_CFLAGS
register/ledger-core/test/Makefile.am - add libgnc-gnome.la
8. Adjust register sort and sort labels related to Num/Action radio buttons to
correspond to book option for clarity:
gnome/gnc-plugin-page-register.c -
define gnc_plugin_page_register_sort_book_option_changed to adjust labels
register function in gnc_plugin_page_register_cmd_view_sort_by
and save num and action radio widgets
initialize radio widget labels to correspond to book option
remove function in gnc_plugin_page_register_sort_response_cb
and clear num and action radio widgets
gnome/gnc-split-reg.c & h. -
create versions of gnc_split_reg_sort and gnc_split_reg_set_sort to force
a re-sort to be used when num-source book option changes
engine/Transaction.c & .h -
modify default sort to use split action in place of tran num per book
option if called from xaccSplitOrder
engine/Split.c -
modify xaccSplitOrder to call xaccTransOrder_num_action to switch use of
split action and tran num per book option
engine/test/utest-Split.c -
modify test_xaccSplitOrder to call xaccTransOrder_num_action based on
book option
9. Adjust transaction find labels related to Num/Action to correspond to book option
for clarity:
gnome/dialog-find-transactions.c - adjust labels in search criteria based
on book option
gnome-search/dialog-search.c -
define gnc_search_dialog_book_option_changed to adjust labels
register function in gnc_search_dialog_init_widgets when searching
for splits
remove function in gnc_search_dialog_close_cb if registered
10. Modify new file process to allow setting of num-source (actually, all) book
option(s) during New Account Hierarchy Setup:
gnome-utils/gtkbuilder/dialog-preferences.glade - add a user preference for
the default setting for 'num-source' for new books
core-utils/gnc-gconf-utils.h - define KEY_NUM_SOURCE
gnome-utils/gnc-gnome-utils.c & h. -
define gnc_options_dialog_set_new_book_option_values to retrieve
preference values
gnome-utils/dialog-options.c & .h - define gnc_options_dialog_new_modal to
surpress apply button
gnome-utils/gnc-main-window.c - define function gnc_book_options_dialog_cb
with arguments so that the window can be called in modal mode and the
title changed and return dialog widget; call gnc_options_dialog_new_modal
instead of gnc_options_dialog_new; call this function from
gnc_main_window_cmd_file_properties with new arguments; for modal mode,
call gnc-options-dialog-set-new-book-option-values to initialize values
from preferences
app-utils/gnc-ui-util.c & h. - create function gnc_is_new_book for
determining when a new book is being created
gnome-utils/dialog-utils.c & h. - define gnc_new_book_option_display to
display book options dialog in modal mode
gnome/gtkbuilder/assistant-hierarchy.glade - add notification about book
options to currency page
gnome/assistant-hierarchy.c - add ability to set book option initialized
from preferences value for new books by using functions gnc_is_new_book
and gnc_new_book_option_display
11. Modify Import QIF process to recognize a new-book situation and allow setting
of book options during first import into a new book; also follow book option
setting for num source in importing number whether new book or not:
import-export/qif-import/assistant-qif-import.glade - add notification about
book options to currency page
import-export/qif-import/assistant-qif-import.c - using function
gnc_is_new_book to identify 'new book' situation and set flag; add
logic to handle 'new-book' situation by calling
gnc_new_book_option_display function
import-export/qif-import/qif-to-gnc.scm - use 'gnc_set_num_action' function
to import number to correct field based on book option
12. Modify Import OFX/QFX process to recognize a new-book situation and allow setting
of book options during first import into a new book; also follow book option
setting for num source in importing number whether new book or not:
import-export/ofx/gnc-ofx-import.c - use 'gnc_set_num_action' function to import
number based on book option; identify 'new book' situation using function
gnc_is_new_book and set flag; add logic to handle 'new-book' situation
by raising book options dialog by calling gnc_new_book_option_display
function
import-export/import-backend.c - use 'gnc_get_num_action' function to
retrieve number from transactions to be matched based on book option
13. Modify Import AqBanking process to recognize a new-book situation and allow
setting of book options during first import into a new book; also follow
book option setting for num source in importing number:
import-export/aqbanking/gnc-file-aqb-import.c - identify 'new book'
situation using function gnc_is_new_book and handle by raising book
options dialog by calling gnc_new_book_option_display function
import-export/aqbanking/gnc-ab-utils.c - use 'gnc_set_num_action' function
to set number based on book option
14. Modify Import Transactions from CSV process to recognize a new-book situation
and allow setting of book options during first import into a new book; also
follow book option setting for num source in importing number whether new
book or not:
import-export/csv-import/assistant-csv-trans-import.glade - modify
commments to alert user of raising book options dialog in cases of csv
import into a new book
import-export/csv-import/gnc-csv-model.c & h. - use 'gnc_set_num_action'
function to import number based on book option
import-export/csv-import/assistant-csv-trans-import.c - add logic to handle
'new-book' situation by raising book options dialog by calling
gnc_new_book_option_display function; identify 'new book' situation
using function gnc_is_new_book and set flag
15. Modify Import Accounts from CSV process to recognize a new-book situation
and allow setting of book options during first import into a new book
import-export/csv-import/assistant-csv-account-import.c & .h - add logic to
handle 'new-book' situation by raising book options dialog by calling
gnc_new_book_option_display function; identify 'new book' situation
using function gnc_is_new_book and set flag
16. Modify Export Transactions to CSV process to follow book option setting
for num source in exporting transactions:
import-export/csv-export/csv-transactions-export.c - modify headings to
reflect num-source book option and use 'gnc_get_num_action' function
to retrieve number and action
17. Modify register reporting to reflect book option setting for num source:
gnome/gnc-plugin-page-register.c - determine and pass along ledger-type?
argument to gnc:register-report-create function
gnome/gnc-plugin-page-register2.c - determine and pass along ledger-type?
argument to gnc:register-report-create function
report/standard-reports/standard-reports.scm - add ledger-type? argument
to gnc:register-report-create function; pass it along to
gnc:register-report-create-internal function
report/standard-reports/register.scm - add ledger-type? argument to
gnc:register-report-create-internal function; use 'gnc-get-num-action'
function in place of xaccTransGetNum and 'gnc-get-action-num' function
in place of xaccSplitGetAction and modify report headings and option
text accordingly
18. Modify all places that use xaccTransSetNum and/or xaccSplitSetAction to use
'gnc_set_num_action' function to set number based on book option except
engine/Transaction.c & .h, engine/test-core/test-engine-stuff.c,
engine/Split.c & .h, engine/test/utest-Split.c, engine/engine-helpers.c & .h,
engine/engine-interface.scm, backend/xml/gnc-transaction-xml-v2.c,
backend/xml/io-gncxml-v1.c, import-export/log-replay/gnc-log-replay.c:
engine/gncOwner.c - use 'gnc_set_num_action' function to set number based
on book option
engine/gncInvoice.c - use 'gnc_set_num_action' function to set number based
on book option
engine/SchedXaction.c - use 'gnc_set_num_action' function to set number and
action
engine/cap-gains.c - use 'gnc_set_num_action' function to set split-action
gnome-utils/dialog-transfer.c - use 'gnc_set_num_action' function to set
number based on book option
gnome-utils/gnc-tree-view-split-reg.c - use 'gnc_set_num_action' function
to set number/action based on book option
gnome/assistant-stock-split.c - use 'gnc_set_num_action' function to set
split-action
19. Modify all places that use xaccTransGetNum and/or xaccSplitGetAction to use
'gnc_get_num_action' or 'gnc_get_action_num' functions to get number based
on book option except engine/Transaction.c & .h,
engine/test-core/test-engine-stuff.c, engine/Split.c & h.,
engine/test/utest-Split.c, engine/engine-helpers.c,
engine/engine-interface.scm, backend/xml/gnc-transaction-xml-v2.c,
backend/xml/test/test-xml-transaction.c:
gnome/dialog-lot-viewer.c - use 'gnc_get_num_action' function to retrieve
number based on book option
gnome/dialog-sx-from-trans.c - use 'gnc_get_num_action' function to
retrieve number and action
gnome/dialog-print-check.c - use 'gnc_get_num_action' function to
retrieve number and 'gnc_get_action_num' function to retrieve action
register/ledger-core/split-register-model.c - use 'gnc_get_num_action'
function to retrieve number based on book option
register/ledger-core/split-register-load.c - use 'gnc_get_num_action'
function to retrieve number based on book option
register/ledger-core/split-register-control.c - use 'gnc_get_num_action'
function to retrieve action
register/ledger-core/split-register.c - use 'gnc_get_num_action'
function to retrieve transaction-num
engine/cap-gains.c - use 'gnc_get_num_action' function to get split-action
gnome-utils/gnc-tree-view-split-reg.c - use 'gnc_get_num_action' function
to retrieve number and 'gnc_get_action_num' function to retrieve action
gnome-utils/gnc-tree-model-split-reg.c - use 'gnc_get_num_action' function
to retrieve transaction-number but may not be getting what is expected
if book option to use split-action for 'num' is set
report/locale-specific/us/taxtxf.scm - use 'gnc-get-num-action' function in
place of xaccTransGetNum and 'gnc-get-action-num' function in place of
xaccSplitGetAction and modify report headings and option text accordingly
report/standard-reports/transaction.scm - use 'gnc-get-num-action' function
in place of xaccTransGetNum, adjust sorting based on book option and
modify report headings and option text accordingly; add 'SPLIT-ACTION'
as a term for sorting queries
report/business-reports/customer-summary.scm - use 'gnc-get-num-action'
function in place of xaccTransGetNum
report/business-reports/owner-report.scm - use 'gnc-get-num-action'
function in place of xaccTransGetNum
report/business-reports/job-report.scm - use 'gnc-get-num-action' function
in place of xaccTransGetNum
business/business-gnome/dialog-payment.c - use 'gnc_get_num_action'
function to retrieve number based on book option
git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22681 57a11ea4-9604-0410-9ed3-97b8803252fd
2012-12-22 00:00:54 -06:00
|
|
|
#include "engine-helpers.h"
|
2003-08-25 13:36:54 -05:00
|
|
|
#include "gnc-lot.h"
|
2003-09-16 07:44:01 -05:00
|
|
|
#include "policy.h"
|
|
|
|
#include "policy-p.h"
|
2003-08-25 13:36:54 -05:00
|
|
|
|
2005-11-01 21:32:36 -06:00
|
|
|
static QofLogModule log_module = GNC_MOD_LOT;
|
2003-08-25 13:36:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* ============================================================== */
|
2003-08-25 15:02:41 -05:00
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
gboolean
|
2007-12-29 11:43:46 -06:00
|
|
|
xaccAccountHasTrades (const Account *acc)
|
2003-08-25 15:02:41 -05:00
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
gnc_commodity *acc_comm;
|
|
|
|
SplitList *splits, *node;
|
|
|
|
|
|
|
|
if (!acc) return FALSE;
|
2003-08-25 15:02:41 -05:00
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
if (xaccAccountIsPriced (acc))
|
|
|
|
return TRUE;
|
2003-08-25 15:02:41 -05:00
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
acc_comm = xaccAccountGetCommodity(acc);
|
2003-08-25 15:02:41 -05:00
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
splits = xaccAccountGetSplitList(acc);
|
|
|
|
for (node = splits; node; node = node->next)
|
|
|
|
{
|
|
|
|
Split *s = node->data;
|
|
|
|
Transaction *t = s->parent;
|
|
|
|
if (acc_comm != t->common_currency) return TRUE;
|
|
|
|
}
|
2003-08-25 15:02:41 -05:00
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
return FALSE;
|
2003-08-25 15:02:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================== */
|
|
|
|
|
2003-09-22 22:25:37 -05:00
|
|
|
struct find_lot_s
|
2003-08-25 15:02:41 -05:00
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
GNCLot *lot;
|
|
|
|
gnc_commodity *currency;
|
|
|
|
Timespec ts;
|
|
|
|
int (*numeric_pred)(gnc_numeric);
|
|
|
|
gboolean (*date_pred)(Timespec e, Timespec tr);
|
2003-08-25 15:02:41 -05:00
|
|
|
};
|
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
static gboolean
|
2003-09-22 22:25:37 -05:00
|
|
|
earliest_pred (Timespec earl, Timespec tran)
|
2003-08-25 15:02:41 -05:00
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
return ((earl.tv_sec > tran.tv_sec) ||
|
|
|
|
((earl.tv_sec == tran.tv_sec) && (earl.tv_nsec > tran.tv_nsec)));
|
2003-09-22 22:25:37 -05:00
|
|
|
}
|
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
static gboolean
|
2003-09-22 22:25:37 -05:00
|
|
|
latest_pred (Timespec earl, Timespec tran)
|
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
return ((earl.tv_sec < tran.tv_sec) ||
|
|
|
|
((earl.tv_sec == tran.tv_sec) && (earl.tv_nsec < tran.tv_nsec)));
|
2003-09-22 22:25:37 -05:00
|
|
|
}
|
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
static gpointer
|
2003-09-22 22:25:37 -05:00
|
|
|
finder_helper (GNCLot *lot, gpointer user_data)
|
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
struct find_lot_s *els = user_data;
|
|
|
|
Split *s;
|
|
|
|
Transaction *trans;
|
|
|
|
gnc_numeric bal;
|
|
|
|
gboolean opening_is_positive, bal_is_positive;
|
|
|
|
|
|
|
|
if (gnc_lot_is_closed (lot)) return NULL;
|
|
|
|
|
|
|
|
s = gnc_lot_get_earliest_split (lot);
|
|
|
|
if (s == NULL) return NULL;
|
|
|
|
|
|
|
|
/* We want a lot whose balance is of the correct sign. All splits
|
|
|
|
in a lot must be the opposite sign of the opening split. We also
|
|
|
|
want to ignore lots that are overfull, i.e., where the balance in
|
|
|
|
the lot is of opposite sign to the opening split in the lot. */
|
|
|
|
if (0 == (els->numeric_pred) (s->amount)) return NULL;
|
|
|
|
bal = gnc_lot_get_balance (lot);
|
|
|
|
opening_is_positive = gnc_numeric_positive_p (s->amount);
|
|
|
|
bal_is_positive = gnc_numeric_positive_p (bal);
|
|
|
|
if (opening_is_positive != bal_is_positive) return NULL;
|
|
|
|
|
|
|
|
trans = s->parent;
|
|
|
|
if (els->currency &&
|
|
|
|
(FALSE == gnc_commodity_equiv (els->currency,
|
|
|
|
trans->common_currency)))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (els->date_pred (els->ts, trans->date_posted))
|
|
|
|
{
|
|
|
|
els->ts = trans->date_posted;
|
|
|
|
els->lot = lot;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2003-08-25 15:02:41 -05:00
|
|
|
}
|
|
|
|
|
2003-09-22 22:25:37 -05:00
|
|
|
static inline GNCLot *
|
2010-03-02 15:40:28 -06:00
|
|
|
xaccAccountFindOpenLot (Account *acc, gnc_numeric sign,
|
|
|
|
gnc_commodity *currency,
|
|
|
|
gint64 guess,
|
|
|
|
gboolean (*date_pred)(Timespec, Timespec))
|
2003-08-25 15:02:41 -05:00
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
struct find_lot_s es;
|
|
|
|
|
|
|
|
es.lot = NULL;
|
|
|
|
es.currency = currency;
|
|
|
|
es.ts.tv_sec = guess;
|
|
|
|
es.ts.tv_nsec = 0;
|
|
|
|
es.date_pred = date_pred;
|
|
|
|
|
|
|
|
if (gnc_numeric_positive_p(sign)) es.numeric_pred = gnc_numeric_negative_p;
|
|
|
|
else es.numeric_pred = gnc_numeric_positive_p;
|
|
|
|
|
|
|
|
xaccAccountForEachLot (acc, finder_helper, &es);
|
|
|
|
return es.lot;
|
2003-08-25 15:02:41 -05:00
|
|
|
}
|
|
|
|
|
2003-09-22 22:25:37 -05:00
|
|
|
GNCLot *
|
2010-03-02 15:40:28 -06:00
|
|
|
xaccAccountFindEarliestOpenLot (Account *acc, gnc_numeric sign,
|
2004-05-29 15:06:03 -05:00
|
|
|
gnc_commodity *currency)
|
2003-09-22 22:25:37 -05:00
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
GNCLot *lot;
|
|
|
|
ENTER (" sign=%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT, sign.num,
|
|
|
|
sign.denom);
|
|
|
|
|
|
|
|
lot = xaccAccountFindOpenLot (acc, sign, currency,
|
|
|
|
G_MAXINT64, earliest_pred);
|
|
|
|
LEAVE ("found lot=%p %s baln=%s", lot, gnc_lot_get_title (lot),
|
|
|
|
gnc_num_dbg_to_string(gnc_lot_get_balance(lot)));
|
|
|
|
return lot;
|
2003-09-22 22:25:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
GNCLot *
|
2004-05-29 15:06:03 -05:00
|
|
|
xaccAccountFindLatestOpenLot (Account *acc, gnc_numeric sign,
|
|
|
|
gnc_commodity *currency)
|
2003-09-22 22:25:37 -05:00
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
GNCLot *lot;
|
|
|
|
ENTER (" sign=%" G_GINT64_FORMAT "/%" G_GINT64_FORMAT,
|
|
|
|
sign.num, sign.denom);
|
|
|
|
|
|
|
|
lot = xaccAccountFindOpenLot (acc, sign, currency,
|
|
|
|
G_MININT64, latest_pred);
|
|
|
|
LEAVE ("found lot=%p %s", lot, gnc_lot_get_title (lot));
|
|
|
|
return lot;
|
2003-09-22 22:25:37 -05:00
|
|
|
}
|
|
|
|
|
2003-08-25 15:02:41 -05:00
|
|
|
/* ============================================================== */
|
2003-09-16 07:44:01 -05:00
|
|
|
|
|
|
|
Split *
|
|
|
|
xaccSplitAssignToLot (Split *split, GNCLot *lot)
|
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
Account *acc;
|
|
|
|
gnc_numeric baln;
|
|
|
|
int cmp;
|
|
|
|
gboolean baln_is_positive, amt_is_positive;
|
|
|
|
|
|
|
|
if (!lot) return split;
|
|
|
|
if (!split) return NULL;
|
|
|
|
|
|
|
|
/* If this split already belongs to a lot, we are done. */
|
|
|
|
if (split->lot) return NULL;
|
|
|
|
|
|
|
|
/* Anomolous situation; except for voided transactions,
|
|
|
|
* we don't expect to see splits with no amount ..
|
|
|
|
* unless they're gains splits, and we shouldn't see those.
|
|
|
|
*/
|
|
|
|
if (gnc_numeric_zero_p (split->amount))
|
|
|
|
{
|
|
|
|
if (xaccTransGetVoidStatus(split->parent)) return NULL;
|
|
|
|
|
|
|
|
PWARN ("split with zero amount; value=%s gflag=%x gsplit=%p",
|
|
|
|
gnc_num_dbg_to_string (split->amount),
|
|
|
|
split->gains,
|
|
|
|
split->gains_split);
|
|
|
|
if (split->gains_split)
|
|
|
|
{
|
|
|
|
PWARN ("gains amt=%s value=%s",
|
|
|
|
gnc_num_dbg_to_string (split->gains_split->amount),
|
|
|
|
gnc_num_dbg_to_string (split->gains_split->value));
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the lot is closed, we can't add anything to it */
|
|
|
|
baln = gnc_lot_get_balance (lot);
|
|
|
|
if (gnc_lot_is_closed (lot)) return split;
|
|
|
|
|
|
|
|
/* If the lot balance is zero, but the lot is open, then
|
|
|
|
* the lot is empty. Unconditionally add the split. */
|
|
|
|
if (gnc_numeric_zero_p (baln))
|
|
|
|
{
|
|
|
|
acc = split->acc;
|
|
|
|
xaccAccountBeginEdit (acc);
|
|
|
|
gnc_lot_add_split (lot, split);
|
|
|
|
PINFO ("added split to empty lot, new lot baln=%s (%s)",
|
|
|
|
gnc_num_dbg_to_string (gnc_lot_get_balance(lot)),
|
|
|
|
gnc_lot_get_title (lot));
|
|
|
|
xaccAccountCommitEdit (acc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the sign of the split is the same as the sign of the lot,
|
|
|
|
* add the split, but complain about it ... none of the currently
|
|
|
|
* implemented accounting policies should be giving us splits
|
|
|
|
* that make lots larger. One a lot is open, the FIFO/LIFO
|
|
|
|
* policies should be working only to make the lot smaller.
|
|
|
|
* We can remove teh warning emssage come the day we have
|
|
|
|
* fancier policies.
|
|
|
|
*/
|
|
|
|
baln_is_positive = gnc_numeric_positive_p (baln);
|
|
|
|
amt_is_positive = gnc_numeric_positive_p (split->amount);
|
|
|
|
if ((baln_is_positive && amt_is_positive) ||
|
|
|
|
((!baln_is_positive) && (!amt_is_positive)))
|
|
|
|
{
|
|
|
|
PWARN ("accounting policy gave us split that enlarges the lot!\n"
|
|
|
|
"old lot baln=%s split amt=%s lot=%s",
|
|
|
|
gnc_num_dbg_to_string (gnc_lot_get_balance(lot)),
|
|
|
|
gnc_num_dbg_to_string (split->amount),
|
|
|
|
gnc_lot_get_title (lot));
|
|
|
|
|
|
|
|
acc = split->acc;
|
|
|
|
xaccAccountBeginEdit (acc);
|
|
|
|
gnc_lot_add_split (lot, split);
|
|
|
|
xaccAccountCommitEdit (acc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If adding the split would make the lot balance change sign,
|
|
|
|
* then we split the split into two pieces: one piece that will
|
|
|
|
* bring the lot balance to zero, and another to be dealt with
|
|
|
|
* later. */
|
|
|
|
cmp = gnc_numeric_compare (gnc_numeric_abs(split->amount),
|
|
|
|
gnc_numeric_abs(baln));
|
|
|
|
|
|
|
|
PINFO ("found open lot with baln=%s (%s)", gnc_num_dbg_to_string (baln),
|
2004-07-01 23:03:59 -05:00
|
|
|
gnc_lot_get_title (lot));
|
2010-03-02 15:40:28 -06:00
|
|
|
|
|
|
|
/* cmp == -1 if amt < baln, ==0 if amt==baln */
|
|
|
|
if (0 >= cmp)
|
|
|
|
{
|
|
|
|
acc = split->acc;
|
|
|
|
xaccAccountBeginEdit (acc);
|
|
|
|
gnc_lot_add_split (lot, split);
|
|
|
|
PINFO ("simple added split to lot, new lot baln=%s",
|
|
|
|
gnc_num_dbg_to_string (gnc_lot_get_balance(lot)));
|
|
|
|
xaccAccountCommitEdit (acc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are here, then (cmp == +1 iff (amt > baln)) and we need
|
|
|
|
* to split up the split into pieces. Do it. */
|
|
|
|
{
|
2012-12-01 16:44:53 -06:00
|
|
|
time64 now = gnc_time (NULL);
|
2010-03-02 15:40:28 -06:00
|
|
|
Split * new_split;
|
|
|
|
gnc_numeric amt_a, amt_b, amt_tot;
|
|
|
|
gnc_numeric val_a, val_b, val_tot;
|
|
|
|
gnc_numeric frac;
|
|
|
|
Transaction *trans;
|
|
|
|
Timespec ts;
|
|
|
|
|
|
|
|
acc = split->acc;
|
|
|
|
xaccAccountBeginEdit (acc);
|
|
|
|
trans = split->parent;
|
|
|
|
xaccTransBeginEdit (trans);
|
|
|
|
|
|
|
|
amt_tot = split->amount;
|
|
|
|
amt_a = gnc_numeric_neg (baln);
|
|
|
|
amt_b = gnc_numeric_sub_fixed (amt_tot, amt_a);
|
|
|
|
|
|
|
|
PINFO ("++++++++++++++ splitting split=%p into amt = %s + %s",
|
|
|
|
split,
|
|
|
|
gnc_num_dbg_to_string(amt_a),
|
|
|
|
gnc_num_dbg_to_string(amt_b) );
|
|
|
|
|
|
|
|
/* Compute the value so that it holds in the same proportion:
|
|
|
|
* i.e. so that (amt_a / amt_tot) = (val_a / val_tot)
|
|
|
|
*/
|
|
|
|
val_tot = split->value;
|
|
|
|
frac = gnc_numeric_div (amt_a, amt_tot,
|
|
|
|
GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE);
|
|
|
|
val_a = gnc_numeric_mul (frac, val_tot,
|
|
|
|
gnc_numeric_denom(val_tot),
|
2010-10-23 05:38:48 -05:00
|
|
|
GNC_HOW_RND_ROUND_HALF_UP | GNC_HOW_DENOM_EXACT);
|
2010-03-02 15:40:28 -06:00
|
|
|
|
|
|
|
val_b = gnc_numeric_sub_fixed (val_tot, val_a);
|
|
|
|
if (gnc_numeric_check(val_a))
|
|
|
|
{
|
|
|
|
PERR("Numeric overflow\n"
|
|
|
|
"Acct=%s Txn=%s\n"
|
|
|
|
"\tval_tot=%s amt_a=%s amt_tot=%s\n",
|
|
|
|
xaccAccountGetName(acc),
|
|
|
|
xaccTransGetDescription(trans),
|
|
|
|
gnc_num_dbg_to_string(val_tot),
|
|
|
|
gnc_num_dbg_to_string(amt_a),
|
|
|
|
gnc_num_dbg_to_string(amt_tot));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gnc_numeric_zero_p(val_a) || gnc_numeric_zero_p(val_b))
|
|
|
|
{
|
|
|
|
PERR ("Failed to split into two!");
|
|
|
|
}
|
|
|
|
|
|
|
|
PINFO ("split value is = %s = %s + %s",
|
|
|
|
gnc_num_dbg_to_string(val_tot),
|
|
|
|
gnc_num_dbg_to_string(val_a),
|
|
|
|
gnc_num_dbg_to_string(val_b) );
|
|
|
|
|
|
|
|
xaccSplitSetAmount (split, amt_a);
|
|
|
|
xaccSplitSetValue (split, val_a);
|
|
|
|
|
|
|
|
/* Adding this split will have the effect of closing this lot,
|
|
|
|
* because the new balance should be precisely zero. */
|
|
|
|
gnc_lot_add_split (lot, split);
|
|
|
|
|
|
|
|
/* Put the remainder of the balance into a new split,
|
|
|
|
* which is in other respects just a clone of this one. */
|
|
|
|
new_split = xaccMallocSplit (qof_instance_get_book(acc));
|
|
|
|
|
|
|
|
/* Copy most of the split attributes */
|
|
|
|
xaccSplitSetMemo (new_split, xaccSplitGetMemo (split));
|
Implement a Book Option to give users the choice to use the transaction number field (default and current behaviour) or, alternatively, the split action field of the anchor split for 'Num' column on registers, reports, import and export, where applicable. The difference between the two is that with the default, the same 'Num' is displayed for a given transaction regardless of what register it is viewed in and with the new option, a different 'Num' can be entered/displayed for a given transaction in each register it is viewed in. In both cases, the 'next number logic' of the 'Num' cell is based on the account of the register the transaction is entered/viewed from but in one case it fills the transaction number, in the other, it fills the split action field of the anchor split (with the transaction number field displayed in the register cell labeled 'T-Num'). In both cases, both fields are visible and can be used as free-form text in double-line mode for any value the user wants. If the new option is not selected, there should be no change in Gnucash's behaviour with the exception of displaying the book options dialog in 'new book' situations.
The changes made are:
1. Implement the new book option to select source for 'num' cell:
libqof/qof/qofbookslots.h - define num source option
engine/engine.i - define 'NUM-FIELD-SOURCE' as an option name and
'SPLIT-ACTION' as a term for sorting queries
app-utils/business-prefs.scm - define book option for num-field-source
app-utils/app-utils.scm - define and export book option for num-field-source
2. Implement functions to get book option setting and to test:
libqof/qof/qofbook.c & .h - define function to get number source book option
libqof/qof/test/test-qofbook.c - test function to get number source book option
3. Implement a notification mechanism for book option changes:
engine/engine-helpers.c & .h - define functions to register callbacks,
define function to remove callbacks and
define function to initiate callbacks on book num-source option changes
core-utils/gnc-features.c & .h - define feature for num-source
app-utils/gnc-ui-util.c & .h - define gnc_book_option_num_field_source_change_cb;
include setting of feature flag on initial use
gnome-utils/gnc-main-window.c - change name of gnc_options_dialog_apply_cb
function to gnc_book_options_dialog_apply_cb and adjust function to
call function gnc_book_option_num_field_source_change_cb when book
option for num-field-source changes; change name of
gnc_options_dialog_close_cb to gnc_book_options_dialog_close_cb
4. Implement 'help' button response on Book Option Dialog:
gnome-utils/gnc-ui.h - define "book-options" section of help manual
gnome-utils/gnc-gnome-utils.c & .h - implement Book Options help callback
gnome-utils/gnc-main-window.c - use gnc_options_dialog_set_book_options_help_cb
to make help button active
5. Implement utility functions to get/set number/action based on book option:
engine/engine-helpers.c & .h - define 'gnc_get_num_action' function
define 'gnc_get_action_num' function
define 'gnc_set_num_action' function
engine/engine.i - make get/set num/action functions available to guile &
allow #f to be used in place of NULL from guile
engine/Transaction.h - add reference to utility functions in comments to
xaccTransSetNum and xaccTransGetNum functions
engine/Split.h - add reference to utility functions in comments to
xaccSplitSetAction and xaccSplitGetAction functions
6. Implement register use of num source book option; for General, Income and Search
Ledgers, since there is no anchor split, always show tran-num in transaction
cursor, but vary label to correspond to book option:
register/ledger-core/split-register.h - define TNUM_CELL
define CURSOR_DOUBLE_LEDGER_NUM_ACTN cursor name to use TNUM_CELL
define CURSOR_DOUBLE_JOURNAL_NUM_ACTN cursor name to use TNUM_CELL
add boolean to split_reg structure to hold num source book option
register/ledger-core/split-register-layout.c -
add logic to define and set up new ledger/journal cursors with TNUM_CELLs
add logic to add the new cursors to a layout
add logic to add TNUM_CELL to a layout
register/ledger-core/split-register-model.c -
define function to get TNUM_CELL label
add logic to recognize ledger/journal cursors with TNUM_CELL
modify function to get NUM_CELL value based on book option and register
type
define function to get TNUM_CELL value
adjust num help text to reflect book option choice
define function to get TNUM_CELL help
modify function to get action help to reflect book option choice
set up handlers for TNUM_CELL
register/ledger-core/split-register-model-save.c -
use 'gnc_set_num_action' function to set num/action for NUM_CELL based
on book option
define function to save TNUM_CELL using 'gnc_set_num_action' function
set save handler for TNUM_CELL
register/ledger-core/split-register-util.c -
modify logic to recognize ledger/journal cursors with tnum_cell
register/ledger-core/split-register.c -
define split_register_book_option_changed function
register split_register_book_option_changed function in register init
initialize register structure setting to book option
remove split_register_book_option_changed function in register destroy
modify gnc_split_register_save_to_scm to handle TNUM_CELL
7. Enhance transaction copy/duplicate logic to accommodate new book option:
gnome/gtkbuilder/gnc-plugin-page-register.glade -
add widgets to handle trans number and/or split action based on book option
gnome-utils/dialog-dup-trans.c & .h -
add capability to display and handle 'Num' field according to book option
gnome/gnc-plugin-page-register.c - define gnc_find_register_by_account
register/ledger-core/split-register.c -
modify gnc_split_register_duplicate_current to accomodate book option
setting
register/ledger-core/Makefile.am - add GNOME_CFLAGS
register/ledger-core/test/Makefile.am - add libgnc-gnome.la
8. Adjust register sort and sort labels related to Num/Action radio buttons to
correspond to book option for clarity:
gnome/gnc-plugin-page-register.c -
define gnc_plugin_page_register_sort_book_option_changed to adjust labels
register function in gnc_plugin_page_register_cmd_view_sort_by
and save num and action radio widgets
initialize radio widget labels to correspond to book option
remove function in gnc_plugin_page_register_sort_response_cb
and clear num and action radio widgets
gnome/gnc-split-reg.c & h. -
create versions of gnc_split_reg_sort and gnc_split_reg_set_sort to force
a re-sort to be used when num-source book option changes
engine/Transaction.c & .h -
modify default sort to use split action in place of tran num per book
option if called from xaccSplitOrder
engine/Split.c -
modify xaccSplitOrder to call xaccTransOrder_num_action to switch use of
split action and tran num per book option
engine/test/utest-Split.c -
modify test_xaccSplitOrder to call xaccTransOrder_num_action based on
book option
9. Adjust transaction find labels related to Num/Action to correspond to book option
for clarity:
gnome/dialog-find-transactions.c - adjust labels in search criteria based
on book option
gnome-search/dialog-search.c -
define gnc_search_dialog_book_option_changed to adjust labels
register function in gnc_search_dialog_init_widgets when searching
for splits
remove function in gnc_search_dialog_close_cb if registered
10. Modify new file process to allow setting of num-source (actually, all) book
option(s) during New Account Hierarchy Setup:
gnome-utils/gtkbuilder/dialog-preferences.glade - add a user preference for
the default setting for 'num-source' for new books
core-utils/gnc-gconf-utils.h - define KEY_NUM_SOURCE
gnome-utils/gnc-gnome-utils.c & h. -
define gnc_options_dialog_set_new_book_option_values to retrieve
preference values
gnome-utils/dialog-options.c & .h - define gnc_options_dialog_new_modal to
surpress apply button
gnome-utils/gnc-main-window.c - define function gnc_book_options_dialog_cb
with arguments so that the window can be called in modal mode and the
title changed and return dialog widget; call gnc_options_dialog_new_modal
instead of gnc_options_dialog_new; call this function from
gnc_main_window_cmd_file_properties with new arguments; for modal mode,
call gnc-options-dialog-set-new-book-option-values to initialize values
from preferences
app-utils/gnc-ui-util.c & h. - create function gnc_is_new_book for
determining when a new book is being created
gnome-utils/dialog-utils.c & h. - define gnc_new_book_option_display to
display book options dialog in modal mode
gnome/gtkbuilder/assistant-hierarchy.glade - add notification about book
options to currency page
gnome/assistant-hierarchy.c - add ability to set book option initialized
from preferences value for new books by using functions gnc_is_new_book
and gnc_new_book_option_display
11. Modify Import QIF process to recognize a new-book situation and allow setting
of book options during first import into a new book; also follow book option
setting for num source in importing number whether new book or not:
import-export/qif-import/assistant-qif-import.glade - add notification about
book options to currency page
import-export/qif-import/assistant-qif-import.c - using function
gnc_is_new_book to identify 'new book' situation and set flag; add
logic to handle 'new-book' situation by calling
gnc_new_book_option_display function
import-export/qif-import/qif-to-gnc.scm - use 'gnc_set_num_action' function
to import number to correct field based on book option
12. Modify Import OFX/QFX process to recognize a new-book situation and allow setting
of book options during first import into a new book; also follow book option
setting for num source in importing number whether new book or not:
import-export/ofx/gnc-ofx-import.c - use 'gnc_set_num_action' function to import
number based on book option; identify 'new book' situation using function
gnc_is_new_book and set flag; add logic to handle 'new-book' situation
by raising book options dialog by calling gnc_new_book_option_display
function
import-export/import-backend.c - use 'gnc_get_num_action' function to
retrieve number from transactions to be matched based on book option
13. Modify Import AqBanking process to recognize a new-book situation and allow
setting of book options during first import into a new book; also follow
book option setting for num source in importing number:
import-export/aqbanking/gnc-file-aqb-import.c - identify 'new book'
situation using function gnc_is_new_book and handle by raising book
options dialog by calling gnc_new_book_option_display function
import-export/aqbanking/gnc-ab-utils.c - use 'gnc_set_num_action' function
to set number based on book option
14. Modify Import Transactions from CSV process to recognize a new-book situation
and allow setting of book options during first import into a new book; also
follow book option setting for num source in importing number whether new
book or not:
import-export/csv-import/assistant-csv-trans-import.glade - modify
commments to alert user of raising book options dialog in cases of csv
import into a new book
import-export/csv-import/gnc-csv-model.c & h. - use 'gnc_set_num_action'
function to import number based on book option
import-export/csv-import/assistant-csv-trans-import.c - add logic to handle
'new-book' situation by raising book options dialog by calling
gnc_new_book_option_display function; identify 'new book' situation
using function gnc_is_new_book and set flag
15. Modify Import Accounts from CSV process to recognize a new-book situation
and allow setting of book options during first import into a new book
import-export/csv-import/assistant-csv-account-import.c & .h - add logic to
handle 'new-book' situation by raising book options dialog by calling
gnc_new_book_option_display function; identify 'new book' situation
using function gnc_is_new_book and set flag
16. Modify Export Transactions to CSV process to follow book option setting
for num source in exporting transactions:
import-export/csv-export/csv-transactions-export.c - modify headings to
reflect num-source book option and use 'gnc_get_num_action' function
to retrieve number and action
17. Modify register reporting to reflect book option setting for num source:
gnome/gnc-plugin-page-register.c - determine and pass along ledger-type?
argument to gnc:register-report-create function
gnome/gnc-plugin-page-register2.c - determine and pass along ledger-type?
argument to gnc:register-report-create function
report/standard-reports/standard-reports.scm - add ledger-type? argument
to gnc:register-report-create function; pass it along to
gnc:register-report-create-internal function
report/standard-reports/register.scm - add ledger-type? argument to
gnc:register-report-create-internal function; use 'gnc-get-num-action'
function in place of xaccTransGetNum and 'gnc-get-action-num' function
in place of xaccSplitGetAction and modify report headings and option
text accordingly
18. Modify all places that use xaccTransSetNum and/or xaccSplitSetAction to use
'gnc_set_num_action' function to set number based on book option except
engine/Transaction.c & .h, engine/test-core/test-engine-stuff.c,
engine/Split.c & .h, engine/test/utest-Split.c, engine/engine-helpers.c & .h,
engine/engine-interface.scm, backend/xml/gnc-transaction-xml-v2.c,
backend/xml/io-gncxml-v1.c, import-export/log-replay/gnc-log-replay.c:
engine/gncOwner.c - use 'gnc_set_num_action' function to set number based
on book option
engine/gncInvoice.c - use 'gnc_set_num_action' function to set number based
on book option
engine/SchedXaction.c - use 'gnc_set_num_action' function to set number and
action
engine/cap-gains.c - use 'gnc_set_num_action' function to set split-action
gnome-utils/dialog-transfer.c - use 'gnc_set_num_action' function to set
number based on book option
gnome-utils/gnc-tree-view-split-reg.c - use 'gnc_set_num_action' function
to set number/action based on book option
gnome/assistant-stock-split.c - use 'gnc_set_num_action' function to set
split-action
19. Modify all places that use xaccTransGetNum and/or xaccSplitGetAction to use
'gnc_get_num_action' or 'gnc_get_action_num' functions to get number based
on book option except engine/Transaction.c & .h,
engine/test-core/test-engine-stuff.c, engine/Split.c & h.,
engine/test/utest-Split.c, engine/engine-helpers.c,
engine/engine-interface.scm, backend/xml/gnc-transaction-xml-v2.c,
backend/xml/test/test-xml-transaction.c:
gnome/dialog-lot-viewer.c - use 'gnc_get_num_action' function to retrieve
number based on book option
gnome/dialog-sx-from-trans.c - use 'gnc_get_num_action' function to
retrieve number and action
gnome/dialog-print-check.c - use 'gnc_get_num_action' function to
retrieve number and 'gnc_get_action_num' function to retrieve action
register/ledger-core/split-register-model.c - use 'gnc_get_num_action'
function to retrieve number based on book option
register/ledger-core/split-register-load.c - use 'gnc_get_num_action'
function to retrieve number based on book option
register/ledger-core/split-register-control.c - use 'gnc_get_num_action'
function to retrieve action
register/ledger-core/split-register.c - use 'gnc_get_num_action'
function to retrieve transaction-num
engine/cap-gains.c - use 'gnc_get_num_action' function to get split-action
gnome-utils/gnc-tree-view-split-reg.c - use 'gnc_get_num_action' function
to retrieve number and 'gnc_get_action_num' function to retrieve action
gnome-utils/gnc-tree-model-split-reg.c - use 'gnc_get_num_action' function
to retrieve transaction-number but may not be getting what is expected
if book option to use split-action for 'num' is set
report/locale-specific/us/taxtxf.scm - use 'gnc-get-num-action' function in
place of xaccTransGetNum and 'gnc-get-action-num' function in place of
xaccSplitGetAction and modify report headings and option text accordingly
report/standard-reports/transaction.scm - use 'gnc-get-num-action' function
in place of xaccTransGetNum, adjust sorting based on book option and
modify report headings and option text accordingly; add 'SPLIT-ACTION'
as a term for sorting queries
report/business-reports/customer-summary.scm - use 'gnc-get-num-action'
function in place of xaccTransGetNum
report/business-reports/owner-report.scm - use 'gnc-get-num-action'
function in place of xaccTransGetNum
report/business-reports/job-report.scm - use 'gnc-get-num-action' function
in place of xaccTransGetNum
business/business-gnome/dialog-payment.c - use 'gnc_get_num_action'
function to retrieve number based on book option
git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22681 57a11ea4-9604-0410-9ed3-97b8803252fd
2012-12-22 00:00:54 -06:00
|
|
|
/* Set split-action with gnc_set_num_action which is the same as
|
|
|
|
* xaccSplitSetAction with these arguments; use gnc_get_num_action to get
|
|
|
|
* split-action which is the same as xaccSplitGetAction */
|
|
|
|
gnc_set_num_action(NULL, new_split, NULL, gnc_get_num_action(NULL, split));
|
2010-03-02 15:40:28 -06:00
|
|
|
xaccSplitSetReconcile (new_split, xaccSplitGetReconcile (split));
|
|
|
|
ts = xaccSplitRetDateReconciledTS (split);
|
|
|
|
xaccSplitSetDateReconciledTS (new_split, &ts);
|
|
|
|
|
2014-05-07 08:37:14 -05:00
|
|
|
/* Set the lot-split and peer_guid properties on the two
|
|
|
|
* splits to indicate that they're linked.
|
2010-03-02 15:40:28 -06:00
|
|
|
*/
|
2014-05-07 08:37:14 -05:00
|
|
|
qof_instance_set (QOF_INSTANCE (split),
|
|
|
|
"lot-split", now,
|
|
|
|
"peer_guid", xaccSplitGetGUID (new_split),
|
|
|
|
NULL);
|
2010-03-02 15:40:28 -06:00
|
|
|
|
2014-05-07 08:37:14 -05:00
|
|
|
qof_instance_set (QOF_INSTANCE (new_split),
|
|
|
|
"lot-split", now,
|
|
|
|
"peer_guid", xaccSplitGetGUID (split),
|
|
|
|
NULL);
|
2010-03-02 15:40:28 -06:00
|
|
|
|
|
|
|
xaccAccountInsertSplit (acc, new_split);
|
|
|
|
xaccTransAppendSplit (trans, new_split);
|
|
|
|
/* Set the amount and value after the split is in the transaction
|
|
|
|
so it can find the correct denominator to use. Otherwise it
|
|
|
|
uses 100000 which may cause an overflow in some of the tests
|
|
|
|
in test-period */
|
|
|
|
xaccSplitSetAmount (new_split, amt_b);
|
|
|
|
xaccSplitSetValue (new_split, val_b);
|
|
|
|
xaccTransCommitEdit (trans);
|
|
|
|
xaccAccountCommitEdit (acc);
|
|
|
|
return new_split;
|
|
|
|
}
|
2003-09-16 07:44:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================== */
|
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
/* Accounting-policy callback. Given an account and an amount,
|
|
|
|
* this routine should return a lot. By implementing this as
|
2003-08-25 18:34:34 -05:00
|
|
|
* a callback, we can 'easily' add other accounting policies.
|
2003-08-25 13:36:54 -05:00
|
|
|
*/
|
2003-09-22 22:25:37 -05:00
|
|
|
gboolean
|
|
|
|
xaccSplitAssign (Split *split)
|
2003-08-25 13:36:54 -05:00
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
Account *acc;
|
|
|
|
gboolean splits_split_up = FALSE;
|
|
|
|
GNCLot *lot;
|
|
|
|
GNCPolicy *pcy;
|
|
|
|
|
|
|
|
if (!split) return FALSE;
|
|
|
|
|
|
|
|
/* If this split already belongs to a lot or the account doesn't
|
|
|
|
* have lots, we are done.
|
|
|
|
*/
|
|
|
|
if (split->lot) return FALSE;
|
|
|
|
acc = split->acc;
|
|
|
|
if (!xaccAccountHasTrades (acc))
|
|
|
|
return FALSE;
|
|
|
|
if (gnc_numeric_zero_p (split->amount))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ENTER ("(split=%p)", split);
|
|
|
|
|
|
|
|
pcy = gnc_account_get_policy(acc);
|
|
|
|
xaccAccountBeginEdit (acc);
|
|
|
|
|
|
|
|
/* If we are here, this split does not belong to any lot.
|
|
|
|
* We ask the policy for a lot to assign it to. This
|
|
|
|
* block is written in the form of a while loop, since we
|
|
|
|
* may have to bust a split across several lots.
|
|
|
|
*/
|
|
|
|
while (split)
|
|
|
|
{
|
|
|
|
PINFO ("have split %p amount=%s", split,
|
|
|
|
gnc_num_dbg_to_string (split->amount));
|
|
|
|
split->gains |= GAINS_STATUS_VDIRTY;
|
|
|
|
lot = pcy->PolicyGetLot (pcy, split);
|
|
|
|
if (!lot)
|
|
|
|
{
|
|
|
|
lot = gnc_lot_make_default (acc);
|
|
|
|
PINFO ("start new lot (%s)", gnc_lot_get_title(lot));
|
|
|
|
}
|
|
|
|
split = xaccSplitAssignToLot (split, lot);
|
|
|
|
if (split) splits_split_up = TRUE;
|
|
|
|
}
|
|
|
|
xaccAccountCommitEdit (acc);
|
|
|
|
|
|
|
|
LEAVE (" split_up=%d", splits_split_up);
|
|
|
|
return splits_split_up;
|
2003-08-25 13:36:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================== */
|
2003-08-25 18:34:34 -05:00
|
|
|
|
|
|
|
Split *
|
2005-12-31 17:35:55 -06:00
|
|
|
xaccSplitGetCapGainsSplit (const Split *split)
|
2003-08-25 18:34:34 -05:00
|
|
|
{
|
2010-03-27 16:01:21 -05:00
|
|
|
GncGUID *gains_guid;
|
2010-03-02 15:40:28 -06:00
|
|
|
Split *gains_split;
|
|
|
|
|
|
|
|
if (!split) return NULL;
|
|
|
|
|
2014-05-07 08:37:14 -05:00
|
|
|
qof_instance_get (QOF_INSTANCE (split),
|
|
|
|
"gains-split", &gains_guid,
|
|
|
|
NULL);
|
2010-03-02 15:40:28 -06:00
|
|
|
if (!gains_guid) return NULL;
|
|
|
|
|
|
|
|
/* Both splits will be in the same collection, so search there. */
|
|
|
|
gains_split = (Split*) qof_collection_lookup_entity (
|
|
|
|
qof_instance_get_collection(split), gains_guid);
|
|
|
|
PINFO ("split=%p has gains-split=%p", split, gains_split);
|
|
|
|
return gains_split;
|
2003-08-25 18:34:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================== */
|
2003-08-25 13:36:54 -05:00
|
|
|
|
2006-04-29 22:52:05 -05:00
|
|
|
Split *
|
|
|
|
xaccSplitGetGainsSourceSplit (const Split *split)
|
|
|
|
{
|
2010-03-27 16:01:21 -05:00
|
|
|
GncGUID *source_guid;
|
2010-03-02 15:40:28 -06:00
|
|
|
Split *source_split;
|
|
|
|
|
|
|
|
if (!split) return NULL;
|
|
|
|
|
2014-05-07 08:37:14 -05:00
|
|
|
qof_instance_get (QOF_INSTANCE (split),
|
|
|
|
"gains-source", &source_guid,
|
|
|
|
NULL);
|
2010-03-02 15:40:28 -06:00
|
|
|
if (!source_guid) return NULL;
|
|
|
|
|
|
|
|
/* Both splits will be in the same collection, so search there. */
|
|
|
|
source_split = (Split*) qof_collection_lookup_entity(
|
|
|
|
qof_instance_get_collection(split), source_guid);
|
|
|
|
PINFO ("split=%p has source-split=%p", split, source_split);
|
|
|
|
return source_split;
|
2006-04-29 22:52:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ============================================================== */
|
|
|
|
|
2003-08-25 13:36:54 -05:00
|
|
|
void
|
|
|
|
xaccSplitComputeCapGains(Split *split, Account *gain_acc)
|
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
SplitList *node;
|
|
|
|
GNCLot *lot;
|
|
|
|
GNCPolicy *pcy;
|
|
|
|
gnc_commodity *currency = NULL;
|
|
|
|
gnc_numeric zero = gnc_numeric_zero();
|
|
|
|
gnc_numeric value = zero;
|
|
|
|
gnc_numeric frac;
|
|
|
|
gnc_numeric opening_amount, opening_value;
|
|
|
|
gnc_numeric lot_amount, lot_value;
|
|
|
|
gnc_commodity *opening_currency;
|
|
|
|
|
|
|
|
if (!split) return;
|
|
|
|
lot = split->lot;
|
|
|
|
if (!lot) return;
|
|
|
|
pcy = gnc_account_get_policy(gnc_lot_get_account(lot));
|
|
|
|
currency = split->parent->common_currency;
|
|
|
|
|
|
|
|
ENTER ("(split=%p gains=%p status=0x%x lot=%s)", split,
|
2014-05-07 08:37:14 -05:00
|
|
|
split->gains_split, split->gains, gnc_lot_get_title(lot));
|
2010-03-02 15:40:28 -06:00
|
|
|
|
|
|
|
/* Make sure the status flags and pointers are initialized */
|
|
|
|
xaccSplitDetermineGainStatus(split);
|
|
|
|
|
|
|
|
/* Not possible to have gains if the transaction currency and
|
|
|
|
* account commodity are identical. */
|
|
|
|
if (gnc_commodity_equal (currency,
|
|
|
|
xaccAccountGetCommodity(split->acc)))
|
|
|
|
{
|
|
|
|
LEAVE ("Currency transfer, gains not possible, returning.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pcy->PolicyIsOpeningSplit (pcy, lot, split))
|
|
|
|
{
|
|
|
|
#if MOVE_THIS_TO_A_DATA_INTEGRITY_SCRUBBER
|
|
|
|
/* Check to make sure that this opening split doesn't
|
|
|
|
* have a cap-gain transaction associated with it.
|
|
|
|
* If it does, that's wrong, and we ruthlessly destroy it.
|
|
|
|
* XXX Don't do this, it leads to infinite loops.
|
|
|
|
* We need to scrub out errors like this elsewhere!
|
|
|
|
*/
|
|
|
|
if (xaccSplitGetCapGainsSplit (split))
|
|
|
|
{
|
|
|
|
Split *gains_split = xaccSplitGetCapGainsSplit(split);
|
|
|
|
Transaction *trans = gains_split->parent;
|
|
|
|
PERR ("Opening Split must not have cap gains!!\n");
|
|
|
|
|
|
|
|
xaccTransBeginEdit (trans);
|
|
|
|
xaccTransDestroy (trans);
|
|
|
|
xaccTransCommitEdit (trans);
|
|
|
|
}
|
2003-09-20 19:53:49 -05:00
|
|
|
#endif
|
2010-03-02 15:40:28 -06:00
|
|
|
LEAVE ("Lot opening split, returning.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-07 12:24:55 -05:00
|
|
|
if (g_strcmp0 ("stock-split", xaccSplitGetType (split)) == 0)
|
2010-03-02 15:40:28 -06:00
|
|
|
{
|
|
|
|
LEAVE ("Stock split split, returning.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GAINS_STATUS_GAINS & split->gains)
|
|
|
|
{
|
|
|
|
Split *s;
|
|
|
|
PINFO ("split is a gains recording split, switch over");
|
|
|
|
/* If this is the split that records the gains, then work with
|
|
|
|
* the split that generates the gains.
|
|
|
|
*/
|
|
|
|
/* split = xaccSplitGetCapGainsSplit (split); */
|
|
|
|
s = split->gains_split;
|
|
|
|
|
|
|
|
/* This should never be NULL, and if it is, and its matching
|
|
|
|
* parent can't be found, then its a bug, and we should be
|
|
|
|
* discarding this split. But ... for now .. return.
|
|
|
|
* XXX move appropriate actions to a 'scrub' routine'
|
|
|
|
*/
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
PERR ("Bad gains-split pointer! .. trying to recover.");
|
|
|
|
split->gains_split = xaccSplitGetCapGainsSplit (split);
|
|
|
|
s = split->gains_split;
|
|
|
|
if (!s) return;
|
|
|
|
#if MOVE_THIS_TO_A_DATA_INTEGRITY_SCRUBBER
|
|
|
|
xaccTransDestroy (trans);
|
2003-09-06 10:02:21 -05:00
|
|
|
#endif
|
2010-03-02 15:40:28 -06:00
|
|
|
}
|
|
|
|
split = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: if the value of the 'opening' split(s) has changed,
|
|
|
|
* then the cap gains are changed. So we need to check not
|
|
|
|
* only if this split is dirty, but also the lot-opening splits. */
|
|
|
|
for (node = gnc_lot_get_split_list(lot); node; node = node->next)
|
|
|
|
{
|
|
|
|
Split *s = node->data;
|
|
|
|
if (pcy->PolicyIsOpeningSplit(pcy, lot, s))
|
|
|
|
{
|
|
|
|
if (GAINS_STATUS_UNKNOWN == s->gains) xaccSplitDetermineGainStatus (s);
|
|
|
|
if (s->gains & GAINS_STATUS_VDIRTY)
|
|
|
|
{
|
|
|
|
/* Force a recompute to occur */
|
|
|
|
split->gains |= GAINS_STATUS_VDIRTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it doesn't look like this split is 'dirty', then there's
|
|
|
|
* nothing to do. Just return. */
|
|
|
|
if ((FALSE == (split->gains & GAINS_STATUS_A_VDIRTY)) &&
|
|
|
|
(split->gains_split) &&
|
|
|
|
(FALSE == (split->gains_split->gains & GAINS_STATUS_A_VDIRTY)))
|
|
|
|
{
|
|
|
|
LEAVE ("split not dirty, returning");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yow! If amount is zero, there's nothing to do! Amount-zero splits
|
|
|
|
* may exist if users attempted to manually record gains. */
|
|
|
|
if (gnc_numeric_zero_p (split->amount)) return;
|
|
|
|
|
|
|
|
/* If we got to here, then the split or something related is
|
|
|
|
* 'dirty' and the gains really do need to be recomputed.
|
|
|
|
* So start working things. */
|
|
|
|
|
|
|
|
/* Get the amount and value in this lot at the time of this transaction. */
|
|
|
|
gnc_lot_get_balance_before (lot, split, &lot_amount, &lot_value);
|
|
|
|
|
|
|
|
pcy->PolicyGetLotOpening (pcy, lot, &opening_amount, &opening_value,
|
|
|
|
&opening_currency);
|
|
|
|
|
|
|
|
/* Check to make sure the lot-opening currency and this split
|
|
|
|
* use the same currency */
|
|
|
|
if (FALSE == gnc_commodity_equiv (currency, opening_currency))
|
|
|
|
{
|
|
|
|
/* OK, the purchase and the sale were made in different currencies.
|
|
|
|
* I don't know how to compute cap gains for that. This is not
|
|
|
|
* an error. Just punt, silently.
|
|
|
|
*/
|
|
|
|
LEAVE ("Can't compute gains, mismatched commodities!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Opening amount should be larger (or equal) to current split,
|
|
|
|
* and it should be of the opposite sign.
|
|
|
|
* XXX This should really be a part of a scrub routine that
|
|
|
|
* cleans up the lot, before we get at it!
|
|
|
|
*/
|
|
|
|
if (0 > gnc_numeric_compare (gnc_numeric_abs(lot_amount),
|
|
|
|
gnc_numeric_abs(split->amount)))
|
|
|
|
{
|
|
|
|
GList *n;
|
|
|
|
for (n = gnc_lot_get_split_list(lot); n; n = n->next)
|
|
|
|
{
|
|
|
|
Split *s = n->data;
|
|
|
|
PINFO ("split amt=%s", gnc_num_dbg_to_string(s->amount));
|
|
|
|
}
|
|
|
|
PERR ("Malformed Lot \"%s\"! (too thin!) "
|
|
|
|
"opening amt=%s split amt=%s baln=%s",
|
|
|
|
gnc_lot_get_title (lot),
|
|
|
|
gnc_num_dbg_to_string (lot_amount),
|
|
|
|
gnc_num_dbg_to_string (split->amount),
|
|
|
|
gnc_num_dbg_to_string (gnc_lot_get_balance(lot)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( (gnc_numeric_negative_p(lot_amount) ||
|
|
|
|
gnc_numeric_positive_p(split->amount)) &&
|
|
|
|
(gnc_numeric_positive_p(lot_amount) ||
|
|
|
|
gnc_numeric_negative_p(split->amount)))
|
|
|
|
{
|
|
|
|
GList *n;
|
|
|
|
for (n = gnc_lot_get_split_list(lot); n; n = n->next)
|
|
|
|
{
|
|
|
|
Split *s = n->data;
|
|
|
|
PINFO ("split amt=%s", gnc_num_dbg_to_string(s->amount));
|
|
|
|
}
|
|
|
|
PERR ("Malformed Lot \"%s\"! (too fat!) "
|
|
|
|
"opening amt=%s split amt=%s baln=%s",
|
|
|
|
gnc_lot_get_title (lot),
|
|
|
|
gnc_num_dbg_to_string (lot_amount),
|
|
|
|
gnc_num_dbg_to_string (split->amount),
|
|
|
|
gnc_num_dbg_to_string (gnc_lot_get_balance(lot)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The cap gains is the difference between the basis prior to the
|
|
|
|
* current split, and the current split, pro-rated for an equal
|
|
|
|
* amount of shares.
|
|
|
|
* i.e. purchase_price = lot_value / lot_amount
|
|
|
|
* cost_basis = purchase_price * current_split_amount
|
|
|
|
* cap_gain = current_split_value - cost_basis
|
|
|
|
*/
|
|
|
|
/* Fraction of the lot that this split represents: */
|
|
|
|
frac = gnc_numeric_div (split->amount, lot_amount,
|
|
|
|
GNC_DENOM_AUTO,
|
2004-07-01 23:03:59 -05:00
|
|
|
GNC_HOW_DENOM_REDUCE);
|
2010-03-02 15:40:28 -06:00
|
|
|
/* Basis for this split: */
|
|
|
|
value = gnc_numeric_mul (frac, lot_value,
|
|
|
|
gnc_numeric_denom(opening_value),
|
2010-10-23 05:38:48 -05:00
|
|
|
GNC_HOW_DENOM_EXACT | GNC_HOW_RND_ROUND_HALF_UP);
|
2010-03-02 15:40:28 -06:00
|
|
|
/* Capital gain for this split: */
|
|
|
|
value = gnc_numeric_sub (value, split->value,
|
|
|
|
GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED);
|
|
|
|
PINFO ("Open amt=%s val=%s; split amt=%s val=%s; gains=%s\n",
|
|
|
|
gnc_num_dbg_to_string (lot_amount),
|
|
|
|
gnc_num_dbg_to_string (lot_value),
|
|
|
|
gnc_num_dbg_to_string (split->amount),
|
|
|
|
gnc_num_dbg_to_string (split->value),
|
|
|
|
gnc_num_dbg_to_string (value));
|
|
|
|
if (gnc_numeric_check (value))
|
|
|
|
{
|
|
|
|
PERR ("Numeric overflow during gains calculation\n"
|
|
|
|
"Acct=%s Txn=%s\n"
|
|
|
|
"\tOpen amt=%s val=%s\n\tsplit amt=%s val=%s\n\tgains=%s\n",
|
|
|
|
xaccAccountGetName(split->acc),
|
|
|
|
xaccTransGetDescription(split->parent),
|
|
|
|
gnc_num_dbg_to_string (lot_amount),
|
|
|
|
gnc_num_dbg_to_string (lot_value),
|
|
|
|
gnc_num_dbg_to_string (split->amount),
|
|
|
|
gnc_num_dbg_to_string (split->value),
|
|
|
|
gnc_num_dbg_to_string (value));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are the cap gains zero? If not, add a balancing transaction.
|
|
|
|
* As per design doc lots.txt: the transaction has two splits,
|
|
|
|
* with equal & opposite values. The amt of one iz zero (so as
|
|
|
|
* not to upset the lot balance), the amt of the other is the same
|
|
|
|
* as its value (its the realized gain/loss).
|
|
|
|
*/
|
|
|
|
if (FALSE == gnc_numeric_zero_p (value))
|
|
|
|
{
|
|
|
|
Transaction *trans;
|
|
|
|
Split *lot_split, *gain_split;
|
|
|
|
Timespec ts;
|
|
|
|
gboolean new_gain_split;
|
|
|
|
gnc_numeric negvalue = gnc_numeric_neg (value);
|
|
|
|
|
|
|
|
/* See if there already is an associated gains transaction.
|
|
|
|
* If there is, adjust its value as appropriate. Else, create
|
|
|
|
* a new gains transaction.
|
|
|
|
*/
|
|
|
|
/* lot_split = xaccSplitGetCapGainsSplit (split); */
|
|
|
|
lot_split = split->gains_split;
|
|
|
|
|
|
|
|
if (NULL == lot_split)
|
|
|
|
{
|
|
|
|
Account *lot_acc = gnc_lot_get_account(lot);
|
|
|
|
QofBook *book = qof_instance_get_book(lot_acc);
|
2013-11-25 15:11:08 -06:00
|
|
|
Transaction *base_txn = xaccSplitGetParent (split);
|
2010-03-02 15:40:28 -06:00
|
|
|
|
2006-05-17 21:06:36 -05:00
|
|
|
new_gain_split = TRUE;
|
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
lot_split = xaccMallocSplit (book);
|
|
|
|
gain_split = xaccMallocSplit (book);
|
|
|
|
|
|
|
|
/* Check to make sure the gains account currency matches. */
|
|
|
|
if ((NULL == gain_acc) ||
|
|
|
|
(FALSE == gnc_commodity_equiv (currency,
|
|
|
|
xaccAccountGetCommodity(gain_acc))))
|
2006-05-17 21:06:36 -05:00
|
|
|
{
|
2014-05-07 08:37:14 -05:00
|
|
|
gain_acc = xaccAccountGainsAccount (lot_acc, currency);
|
2006-05-17 21:06:36 -05:00
|
|
|
}
|
2010-03-02 15:40:28 -06:00
|
|
|
|
|
|
|
xaccAccountBeginEdit (gain_acc);
|
|
|
|
xaccAccountInsertSplit (gain_acc, gain_split);
|
|
|
|
xaccAccountCommitEdit (gain_acc);
|
|
|
|
|
|
|
|
xaccAccountBeginEdit (lot_acc);
|
|
|
|
xaccAccountInsertSplit (lot_acc, lot_split);
|
|
|
|
xaccAccountCommitEdit (lot_acc);
|
|
|
|
|
|
|
|
trans = xaccMallocTransaction (book);
|
|
|
|
|
|
|
|
xaccTransBeginEdit (trans);
|
|
|
|
xaccTransSetCurrency (trans, currency);
|
|
|
|
xaccTransSetDescription (trans, _("Realized Gain/Loss"));
|
|
|
|
|
|
|
|
xaccTransAppendSplit (trans, lot_split);
|
|
|
|
xaccTransAppendSplit (trans, gain_split);
|
|
|
|
|
|
|
|
xaccSplitSetMemo (lot_split, _("Realized Gain/Loss"));
|
|
|
|
xaccSplitSetMemo (gain_split, _("Realized Gain/Loss"));
|
|
|
|
|
2014-05-07 08:37:14 -05:00
|
|
|
/* For the new transaction, set the split properties indicating
|
2010-03-02 15:40:28 -06:00
|
|
|
* that this is the gains transaction that corresponds
|
|
|
|
* to the gains source.
|
|
|
|
*/
|
2013-11-25 15:11:08 -06:00
|
|
|
xaccTransBeginEdit (base_txn);
|
2014-05-07 08:37:14 -05:00
|
|
|
qof_instance_set (QOF_INSTANCE (split),
|
|
|
|
"gains-split", xaccSplitGetGUID (lot_split),
|
|
|
|
NULL);
|
2013-11-25 15:11:08 -06:00
|
|
|
xaccTransCommitEdit (base_txn);
|
2014-05-07 08:37:14 -05:00
|
|
|
qof_instance_set (QOF_INSTANCE (lot_split),
|
|
|
|
"gains-source", xaccSplitGetGUID (split),
|
|
|
|
NULL);
|
2010-03-02 15:40:28 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trans = lot_split->parent;
|
|
|
|
gain_split = xaccSplitGetOtherSplit (lot_split);
|
2010-10-03 07:27:44 -05:00
|
|
|
|
2010-07-27 22:48:07 -05:00
|
|
|
/* If the gains transaction has been edited so that it no longer has
|
|
|
|
just two splits, ignore it and assume it's still correct. */
|
|
|
|
if (!gain_split)
|
|
|
|
{
|
|
|
|
new_gain_split = FALSE;
|
|
|
|
}
|
2010-03-02 15:40:28 -06:00
|
|
|
/* If the gain is already recorded corectly do nothing. This is
|
|
|
|
* more than just an optimization since this may be called during
|
|
|
|
* gnc_book_partition_txn and depending on the order in which things
|
|
|
|
* happen some splits may be in the wrong book at that time. */
|
2010-07-27 22:48:07 -05:00
|
|
|
else if (split->gains_split == lot_split &&
|
2010-10-03 07:27:44 -05:00
|
|
|
lot_split->gains_split == split &&
|
|
|
|
gain_split->gains_split == split &&
|
|
|
|
gnc_numeric_equal (xaccSplitGetValue (lot_split), value) &&
|
|
|
|
gnc_numeric_zero_p (xaccSplitGetAmount (lot_split)) &&
|
|
|
|
gnc_numeric_equal (xaccSplitGetValue (gain_split), negvalue) &&
|
|
|
|
gnc_numeric_equal (xaccSplitGetAmount (gain_split), negvalue))
|
2010-03-02 15:40:28 -06:00
|
|
|
{
|
|
|
|
new_gain_split = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_gain_split = TRUE;
|
|
|
|
xaccTransBeginEdit (trans);
|
|
|
|
|
|
|
|
/* Make sure the existing gains trans has the correct currency,
|
|
|
|
* just in case someone screwed with it! */
|
|
|
|
if (FALSE == gnc_commodity_equiv(currency, trans->common_currency))
|
|
|
|
{
|
|
|
|
PWARN ("Resetting the transaction currency!");
|
|
|
|
xaccTransSetCurrency (trans, currency);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_gain_split)
|
|
|
|
{
|
|
|
|
/* Common to both */
|
|
|
|
ts = xaccTransRetDatePostedTS (split->parent);
|
|
|
|
xaccTransSetDatePostedTS (trans, &ts);
|
2012-12-01 16:44:53 -06:00
|
|
|
xaccTransSetDateEnteredSecs (trans, gnc_time (NULL));
|
2010-03-02 15:40:28 -06:00
|
|
|
|
|
|
|
xaccSplitSetAmount (lot_split, zero);
|
|
|
|
xaccSplitSetValue (lot_split, value);
|
|
|
|
|
|
|
|
xaccSplitSetAmount (gain_split, negvalue);
|
|
|
|
xaccSplitSetValue (gain_split, negvalue);
|
|
|
|
|
2014-05-07 08:37:14 -05:00
|
|
|
/* Some short-cuts to help avoid the above property lookup. */
|
2010-03-02 15:40:28 -06:00
|
|
|
split->gains = GAINS_STATUS_CLEAN;
|
|
|
|
split->gains_split = lot_split;
|
|
|
|
lot_split->gains = GAINS_STATUS_GAINS;
|
|
|
|
lot_split->gains_split = split;
|
|
|
|
gain_split->gains = GAINS_STATUS_GAINS;
|
|
|
|
gain_split->gains_split = split;
|
|
|
|
|
|
|
|
/* Do this last since it may generate an event that will call us
|
|
|
|
recursively. */
|
|
|
|
gnc_lot_add_split (lot, lot_split);
|
|
|
|
|
|
|
|
xaccTransCommitEdit (trans);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LEAVE ("(lot=%s)", gnc_lot_get_title(lot));
|
2003-08-25 13:36:54 -05:00
|
|
|
}
|
|
|
|
|
2003-08-25 22:39:36 -05:00
|
|
|
/* ============================================================== */
|
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
gnc_numeric
|
2003-08-25 22:39:36 -05:00
|
|
|
xaccSplitGetCapGains(Split * split)
|
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
if (!split) return gnc_numeric_zero();
|
|
|
|
ENTER("(split=%p)", split);
|
|
|
|
|
|
|
|
if (GAINS_STATUS_UNKNOWN == split->gains)
|
|
|
|
xaccSplitDetermineGainStatus(split);
|
|
|
|
if ((split->gains & GAINS_STATUS_A_VDIRTY) ||
|
|
|
|
(split->gains_split &&
|
|
|
|
(split->gains_split->gains & GAINS_STATUS_A_VDIRTY)))
|
|
|
|
{
|
|
|
|
xaccSplitComputeCapGains (split, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is the source split, get the gains from the one
|
|
|
|
* that records the gains. If this already is the gains split,
|
|
|
|
* its a no-op. */
|
|
|
|
if (!(GAINS_STATUS_GAINS & split->gains))
|
|
|
|
{
|
|
|
|
/* split = xaccSplitGetCapGainsSplit (split); */
|
|
|
|
split = split->gains_split;
|
|
|
|
}
|
|
|
|
|
|
|
|
LEAVE("(split=%p)", split);
|
|
|
|
if (!split) return gnc_numeric_zero();
|
|
|
|
|
|
|
|
return split->value;
|
2003-08-25 22:39:36 -05:00
|
|
|
}
|
|
|
|
|
2003-09-16 07:44:01 -05:00
|
|
|
/* ============================================================== */
|
|
|
|
|
2010-03-02 15:40:28 -06:00
|
|
|
void
|
2003-09-16 07:44:01 -05:00
|
|
|
xaccLotComputeCapGains (GNCLot *lot, Account *gain_acc)
|
|
|
|
{
|
2010-03-02 15:40:28 -06:00
|
|
|
SplitList *node;
|
|
|
|
GNCPolicy *pcy;
|
|
|
|
gboolean is_dirty = FALSE;
|
|
|
|
|
|
|
|
/* Note: if the value of the 'opening' split(s) has changed,
|
|
|
|
* then the cap gains are changed. To capture this, we need
|
|
|
|
* to mark all splits dirty if the opening splits are dirty. */
|
|
|
|
|
|
|
|
ENTER("(lot=%p)", lot);
|
|
|
|
pcy = gnc_account_get_policy(gnc_lot_get_account(lot));
|
|
|
|
for (node = gnc_lot_get_split_list(lot); node; node = node->next)
|
|
|
|
{
|
|
|
|
Split *s = node->data;
|
|
|
|
if (pcy->PolicyIsOpeningSplit(pcy, lot, s))
|
|
|
|
{
|
|
|
|
if (GAINS_STATUS_UNKNOWN == s->gains)
|
|
|
|
xaccSplitDetermineGainStatus(s);
|
|
|
|
if (s->gains & GAINS_STATUS_VDIRTY)
|
|
|
|
{
|
|
|
|
is_dirty = TRUE;
|
|
|
|
s->gains &= ~GAINS_STATUS_VDIRTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_dirty)
|
|
|
|
{
|
|
|
|
for (node = gnc_lot_get_split_list(lot); node; node = node->next)
|
|
|
|
{
|
|
|
|
Split *s = node->data;
|
|
|
|
s->gains |= GAINS_STATUS_VDIRTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (node = gnc_lot_get_split_list(lot); node; node = node->next)
|
|
|
|
{
|
|
|
|
Split *s = node->data;
|
|
|
|
xaccSplitComputeCapGains (s, gain_acc);
|
|
|
|
}
|
|
|
|
LEAVE("(lot=%p)", lot);
|
2003-09-16 07:44:01 -05:00
|
|
|
}
|
|
|
|
|
2003-08-25 13:36:54 -05:00
|
|
|
/* =========================== END OF FILE ======================= */
|