Provide new function gnc_price_invert.

To invert a price when it's currency is what we want to use as a commodity.
Generalizes the source-type PRICE_SOURCE_INVOICE to PRICE_SOURCE_TEMPORARY
for all cases like this where we don't want to save the price.
This commit is contained in:
John Ralls 2015-10-26 15:46:28 -07:00
parent 62c7693860
commit edefc9e57c
5 changed files with 36 additions and 8 deletions

View File

@ -203,7 +203,7 @@ write_price( GNCPrice* p, gpointer data )
g_return_val_if_fail( p != NULL, FALSE );
g_return_val_if_fail( data != NULL, FALSE );
if ( s->is_ok && gnc_price_get_source(p) != PRICE_SOURCE_INVOICE)
if ( s->is_ok && gnc_price_get_source(p) != PRICE_SOURCE_TEMP)
{
s->is_ok = save_price( s->be, QOF_INSTANCE(p) );
}

View File

@ -898,7 +898,7 @@ gnc_invoice_post(InvoiceWindow *iw, struct post_invoice_params *post_params)
gnc_price_set_commodity (convprice, account_currency);
gnc_price_set_currency (convprice, gncInvoiceGetCurrency (invoice));
gnc_price_set_time (convprice, postdate);
gnc_price_set_source (convprice, PRICE_SOURCE_INVOICE);
gnc_price_set_source (convprice, PRICE_SOURCE_TEMP);
gnc_price_set_typestr (convprice, PRICE_TYPE_LAST);
gnc_price_set_value (convprice, exch_rate);
gncInvoiceAddPrice(invoice, convprice);

View File

@ -382,7 +382,7 @@ KvpValue* qof_book_get_option (QofBook *book, GSList *key_path);
SET_ENUM("PRICE-SOURCE-XFER-DLG-VAL");
SET_ENUM("PRICE-SOURCE-SPLIT-REG");
SET_ENUM("PRICE-SOURCE-STOCK-SPLIT");
SET_ENUM("PRICE-SOURCE-INVOICE");
SET_ENUM("PRICE-SOURCE-TEMP");
SET_ENUM("PRICE-SOURCE-INVALID");
#undef SET_ENUM

View File

@ -95,11 +95,10 @@ gnc_price_init(GNCPrice* price)
/* Array of char constants for converting price-source enums. Be sure to keep in
* sync with the enum values in gnc-pricedb.h The string user:price-editor is
* explicitly used by price_to_gui() in dialog-price-editor.c and the string
* Finance::Quote is explicitly used by fq-results->commod-tz-quote-triples in
* price-quotes.scm. Take care to keep them in sync if you make changes. Beware
* explicitly used by price_to_gui() in dialog-price-editor.c. Beware
* that the strings are used to store the enum values in the backends so any
* changes will affect backward data compatibility.
* The last two values, temporary and invalid, are *not* used.
*/
static const char* source_names[] =
{
@ -112,7 +111,7 @@ static const char* source_names[] =
"user:xfer-dialog",
"user:split-register",
"user:stock-split",
"user:invoice-post",
"temporary",
"invalid"
};
@ -383,6 +382,23 @@ gnc_price_clone (GNCPrice* p, QofBook *book)
return(new_p);
}
GNCPrice *
gnc_price_invert (GNCPrice *p)
{
QofBook *book = qof_instance_get_book (QOF_INSTANCE(p));
GNCPrice *new_p = gnc_price_create (book);
qof_instance_copy_version(new_p, p);
gnc_price_begin_edit(new_p);
gnc_price_set_time(new_p, gnc_price_get_time(p));
gnc_price_set_source(new_p, PRICE_SOURCE_TEMP);
gnc_price_set_typestr(new_p, gnc_price_get_typestr(p));
gnc_price_set_commodity(new_p, gnc_price_get_currency(p));
gnc_price_set_currency(new_p, gnc_price_get_commodity(p));
gnc_price_set_value(new_p, gnc_numeric_invert(gnc_price_get_value(p)));
gnc_price_commit_edit(new_p);
return new_p;
}
/* ==================================================================== */
void

View File

@ -169,7 +169,8 @@ typedef enum
PRICE_SOURCE_SPLIT_REG, // "user:split-register"
PRICE_SOURCE_STOCK_SPLIT, // "user:stock-split"
PRICE_SOURCE_INVOICE, // "user:invoice-post"
PRICE_SOURCE_INVALID,
PRICE_SOURCE_TEMP, // "temporary"
PRICE_SOURCE_INVALID, // "invalid"
} PriceSource;
#define PRICE_TYPE_LAST "last"
@ -188,6 +189,17 @@ GNCPrice *gnc_price_create(QofBook *book);
content-wise duplicate of the given price, p. The returned clone
will have a reference count of 1. */
GNCPrice *gnc_price_clone(GNCPrice* p, QofBook *book);
/** Return a newly-allocated price that's the inverse of the given price, p.
*
* Inverse means that the commodity and currency are swapped and the value is
* the numeric inverse of the original's. The source is set to PRICE_SOURCE_TEMP
* to prevent it being saved in the pricedb.
* @param p The price to invert
* @return a new price, with a ref-count of 1. Don't forget to unref it!
*/
GNCPrice *gnc_price_invert(GNCPrice *p);
/** @} */
/* ------------------ */