/* Get the rate from the price db */ static gnc_numeric gtu_sr_get_rate_from_db (gnc_commodity *from, gnc_commodity *to) { GNCPrice *prc; gnc_numeric rate_split; gboolean have_rate = FALSE; QofBook *book = gnc_get_current_book (); /* Do we have a rate allready */ prc = gnc_pricedb_lookup_latest (gnc_pricedb_get_db (book), from, to); if (prc) { rate_split = gnc_price_get_value (prc); gnc_price_unref (prc); have_rate = TRUE; } /* Lets try reversing the commodities */ if (!have_rate) { prc = gnc_pricedb_lookup_latest (gnc_pricedb_get_db (book), to, from); if (prc) { rate_split = gnc_numeric_div (gnc_numeric_create (100, 100), gnc_price_get_value (prc), GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE); gnc_price_unref (prc); have_rate = TRUE; } } /* No rate, set to 1/1 */ if (!have_rate) rate_split = gnc_numeric_create (100, 100); return rate_split; }
static void load_all_prices (GncSqlBackend* be) { GncSqlStatement* stmt; GncSqlResult* result; QofBook* pBook; GNCPriceDB* pPriceDB; g_return_if_fail (be != NULL); pBook = be->book; pPriceDB = gnc_pricedb_get_db (pBook); stmt = gnc_sql_create_select_statement (be, TABLE_NAME); if (stmt != NULL) { result = gnc_sql_execute_select_statement (be, stmt); gnc_sql_statement_dispose (stmt); if (result != NULL) { GNCPrice* pPrice; GncSqlRow* row = gnc_sql_result_get_first_row (result); gchar* sql; gnc_pricedb_set_bulk_update (pPriceDB, TRUE); while (row != NULL) { pPrice = load_single_price (be, row); if (pPrice != NULL) { (void)gnc_pricedb_add_price (pPriceDB, pPrice); gnc_price_unref (pPrice); } row = gnc_sql_result_get_next_row (result); } gnc_sql_result_dispose (result); gnc_pricedb_set_bulk_update (pPriceDB, FALSE); sql = g_strdup_printf ("SELECT DISTINCT guid FROM %s", TABLE_NAME); gnc_sql_slots_load_for_sql_subquery (be, sql, (BookLookupFn)gnc_price_lookup); g_free (sql); } } }
static void cleanup_gnc_price(sixtp_child_result *result) { if (result->data) gnc_price_unref((GNCPrice *) result->data); }
void gnc_stock_split_assistant_finish (GtkAssistant *assistant, gpointer user_data) { StockSplitInfo *info = user_data; GList *account_commits; GList *node; gnc_numeric amount; Transaction *trans; Account *account; Split *split; time64 date; account = info->acct; g_return_if_fail (account != NULL); amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (info->distribution_edit)); g_return_if_fail (!gnc_numeric_zero_p (amount)); gnc_suspend_gui_refresh (); trans = xaccMallocTransaction (gnc_get_current_book ()); xaccTransBeginEdit (trans); xaccTransSetCurrency (trans, gnc_default_currency ()); date = gnc_date_edit_get_date (GNC_DATE_EDIT (info->date_edit)); xaccTransSetDatePostedSecs (trans, date); { const char *description; description = gtk_entry_get_text (GTK_ENTRY (info->description_entry)); xaccTransSetDescription (trans, description); } split = xaccMallocSplit (gnc_get_current_book ()); xaccAccountBeginEdit (account); account_commits = g_list_prepend (NULL, account); xaccTransAppendSplit (trans, split); xaccAccountInsertSplit (account, split); xaccSplitSetAmount (split, amount); xaccSplitMakeStockSplit (split); /* Set split-action with gnc_set_num_action which is the same as * xaccSplitSetAction with these arguments */ /* Translators: This string has a disambiguation prefix */ gnc_set_num_action (NULL, split, NULL, Q_("Action Column|Split")); amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (info->price_edit)); if (gnc_numeric_positive_p (amount)) { QofBook *book; GNCPrice *price; GNCPriceDB *pdb; GNCCurrencyEdit *ce; Timespec ts; ce = GNC_CURRENCY_EDIT (info->price_currency_edit); ts.tv_sec = date; ts.tv_nsec = 0; price = gnc_price_create (gnc_get_current_book ()); gnc_price_begin_edit (price); gnc_price_set_commodity (price, xaccAccountGetCommodity (account)); gnc_price_set_currency (price, gnc_currency_edit_get_currency (ce)); gnc_price_set_time (price, ts); gnc_price_set_source (price, "user:stock-split"); gnc_price_set_typestr (price, "unknown"); gnc_price_set_value (price, amount); gnc_price_commit_edit (price); book = gnc_get_current_book (); pdb = gnc_pricedb_get_db (book); if (!gnc_pricedb_add_price (pdb, price)) gnc_error_dialog (info->window, "%s", _("Error adding price.")); gnc_price_unref (price); } amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (info->cash_edit)); if (gnc_numeric_positive_p (amount)) { const char *memo; memo = gtk_entry_get_text (GTK_ENTRY (info->memo_entry)); /* asset split */ account = gnc_tree_view_account_get_selected_account (GNC_TREE_VIEW_ACCOUNT(info->asset_tree)); split = xaccMallocSplit (gnc_get_current_book ()); xaccAccountBeginEdit (account); account_commits = g_list_prepend (account_commits, account); xaccAccountInsertSplit (account, split); xaccTransAppendSplit (trans, split); xaccSplitSetAmount (split, amount); xaccSplitSetValue (split, amount); xaccSplitSetMemo (split, memo); /* income split */ account = gnc_tree_view_account_get_selected_account (GNC_TREE_VIEW_ACCOUNT(info->income_tree)); split = xaccMallocSplit (gnc_get_current_book ()); xaccAccountBeginEdit (account); account_commits = g_list_prepend (account_commits, account); xaccAccountInsertSplit (account, split); xaccTransAppendSplit (trans, split); xaccSplitSetAmount (split, gnc_numeric_neg (amount)); xaccSplitSetValue (split, gnc_numeric_neg (amount)); xaccSplitSetMemo (split, memo); } xaccTransCommitEdit (trans); for (node = account_commits; node; node = node->next) xaccAccountCommitEdit (node->data); g_list_free (account_commits); gnc_resume_gui_refresh (); gnc_close_gui_component_by_data (ASSISTANT_STOCK_SPLIT_CM_CLASS, info); }
static gboolean price_parse_xml_end_handler(gpointer data_for_children, GSList* data_from_children, GSList* sibling_data, gpointer parent_data, gpointer global_data, gpointer *result, const gchar *tag) { gboolean ok = TRUE; xmlNodePtr price_xml = (xmlNodePtr) data_for_children; xmlNodePtr child; GNCPrice *p = NULL; gxpf_data *gdata = global_data; QofBook *book = gdata->bookdata; /* we haven't been handed the *top* level node yet... */ if (parent_data) return TRUE; *result = NULL; if (!price_xml) return FALSE; if (price_xml->next) { ok = FALSE; goto cleanup_and_exit; } if (price_xml->prev) { ok = FALSE; goto cleanup_and_exit; } if (!price_xml->xmlChildrenNode) { ok = FALSE; goto cleanup_and_exit; } p = gnc_price_create(book); if (!p) { ok = FALSE; goto cleanup_and_exit; } for (child = price_xml->xmlChildrenNode; child; child = child->next) { switch (child->type) { case XML_COMMENT_NODE: case XML_TEXT_NODE: break; case XML_ELEMENT_NODE: if (!price_parse_xml_sub_node(p, child, book)) { ok = FALSE; goto cleanup_and_exit; } break; default: PERR("Unknown node type (%d) while parsing gnc-price xml.", child->type); child = NULL; ok = FALSE; goto cleanup_and_exit; break; } } cleanup_and_exit: if (ok) { *result = p; } else { *result = NULL; gnc_price_unref(p); } xmlFreeNode(price_xml); return ok; }