gboolean
gnc_stock_split_assistant_cash_complete (GtkAssistant *assistant,
        gpointer user_data)
{
    StockSplitInfo *info = user_data;
    gnc_numeric amount;
    gint result;
    Account *account;

    result = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->cash_edit), &amount, TRUE);
    if (result == -1)
        return TRUE; /* Optional field is empty */
    else if ( result > 0)
        return FALSE; /* Parsing error */
    else if (gnc_numeric_negative_p (amount))
        return FALSE; /* Negative cash amount is not allowed */

    /* We have a positive cash amount */
    account = gnc_tree_view_account_get_selected_account (GNC_TREE_VIEW_ACCOUNT(info->income_tree));
    if (!account)
        return FALSE;

    account = gnc_tree_view_account_get_selected_account (GNC_TREE_VIEW_ACCOUNT(info->asset_tree));
    if (!account)
        return FALSE;

    return TRUE;
}
gboolean
gnc_stock_split_assistant_details_complete (GtkAssistant *assistant,
        gpointer user_data)
{
    StockSplitInfo *info = user_data;
    gnc_numeric amount;
    gint result;

    result = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->distribution_edit), &amount, TRUE);
    if ( result != 0)
        return FALSE; /* Parsing error or field is empty */

    if (gnc_numeric_zero_p (amount))
        return FALSE; /* field value is 0 */

    result = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->price_edit), &amount, TRUE);
    if (result == -1)
        return TRUE; /* Optional field is empty */
    else if ( result > 0)
        return FALSE; /* Parsing error */
    else if (gnc_numeric_negative_p (amount))
        return FALSE; /* Negative price is not allowed */
    else
        return TRUE; /* Valid positive price */
}
Ejemplo n.º 3
0
/********************************************************************\
 * gnc_set_label_color                                              *
 *   sets the color of the label given the value                    *
 *                                                                  *
 * Args: label - gtk label widget                                   *
 *       value - value to use to set color                          *
 * Returns: none                                                    *
 \*******************************************************************/
void
gnc_set_label_color(GtkWidget *label, gnc_numeric value)
{
    gboolean deficit;
    GdkColormap *cm;
    GtkStyle *style;

    if (!gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED))
        return;

    cm = gtk_widget_get_colormap(GTK_WIDGET(label));
    gtk_widget_ensure_style(GTK_WIDGET(label));
    style = gtk_widget_get_style(GTK_WIDGET(label));

    style = gtk_style_copy(style);

    deficit = gnc_numeric_negative_p (value);

    if (deficit)
    {
        gnc_get_deficit_color(&style->fg[GTK_STATE_NORMAL]);
        gdk_colormap_alloc_color(cm, &style->fg[GTK_STATE_NORMAL], FALSE, TRUE);
    }
    else
        style->fg[GTK_STATE_NORMAL] = style->black;

    gtk_widget_set_style(label, style);

    g_object_unref(style);
}
Ejemplo n.º 4
0
/********************************************************************\
 * gnc_set_label_color                                              *
 *   sets the color of the label given the value                    *
 *                                                                  *
 * Args: label - gtk label widget                                   *
 *       value - value to use to set color                          *
 * Returns: none                                                    *
 \*******************************************************************/
void
gnc_set_label_color(GtkWidget *label, gnc_numeric value)
{
    gboolean deficit;

    if (!gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED))
        return;

    deficit = gnc_numeric_negative_p (value);

    if (deficit)
        gnc_widget_set_style_context (GTK_WIDGET(label), "negative-numbers");
    else
        gnc_widget_set_style_context (GTK_WIDGET(label), "default-color");
}
Ejemplo n.º 5
0
static Split *
DirectionPolicyGetSplit (GNCPolicy *pcy, GNCLot *lot, short reverse)
{
    Split *split;
    SplitList *node;
    gnc_commodity *common_currency;
    gboolean want_positive;
    gnc_numeric baln;
    Split *osplit;
    Transaction *otrans;
    Timespec open_ts;
    Account* lot_account;

    if (!pcy || !lot || !gnc_lot_get_split_list(lot)) return NULL;
    lot_account = gnc_lot_get_account(lot);
    if (!lot_account) return NULL;

    /* Recomputing the balance re-evaluates the lot closure */
    baln = gnc_lot_get_balance (lot);
    if (gnc_lot_is_closed(lot)) return NULL;

    want_positive = gnc_numeric_negative_p (baln);

    /* All splits in lot must share a common transaction currency. */
    split = gnc_lot_get_split_list(lot)->data;
    common_currency = split->parent->common_currency;

    /* Don't add a split to the lot unless it will be the new last
       split in the lot.  Otherwise our balance tests will be wrong
       and the lot may end up too thin or too fat. */
    osplit = gnc_lot_get_latest_split (lot);
    otrans = osplit ? xaccSplitGetParent (osplit) : 0;
    open_ts = xaccTransRetDatePostedTS (otrans);

    /* Walk over *all* splits in the account, till we find one that
     * hasn't been assigned to a lot.  Return that split.
     * Make use of the fact that the splits in an account are
     * already in date order; so we don't have to sort. */
    node = xaccAccountGetSplitList (lot_account);
    if (reverse)
    {
        node = g_list_last (node);
    }
    while (node)
    {
        gboolean is_match;
        gboolean is_positive;
        Timespec this_ts;
        split = node->data;
        if (split->lot) goto donext;

        /* Skip it if it's too early */
        this_ts = xaccTransRetDatePostedTS ( xaccSplitGetParent (split));
        if ((this_ts.tv_sec < open_ts.tv_sec) ||
                ((this_ts.tv_sec == open_ts.tv_sec) &&
                 (this_ts.tv_nsec < open_ts.tv_nsec)))
        {
            if (reverse)
                /* Going backwards, no point in looking further */
                break;
            goto donext;
        }

        /* Allow equiv currencies */
        is_match = gnc_commodity_equiv (common_currency,
                                        split->parent->common_currency);
        if (FALSE == is_match) goto donext;

        /* Disallow zero-amount splits in general. */
        if (gnc_numeric_zero_p(split->amount)) goto donext;

        is_positive = gnc_numeric_positive_p (split->amount);
        if ((want_positive && is_positive) ||
                ((!want_positive) && (!is_positive))) return split;
donext:
        if (reverse)
        {
            node = node->prev;
        }
        else
        {
            node = node->next;
        }
    }
    return NULL;
}
Ejemplo n.º 6
0
/* Determine whether the value can be calculated. If it can, return
 * NULL. Otherwise, return a string describing the reason and the offending
 * entry in error_item. */
static const char *
can_calc_value(FinCalcDialog *fcd, FinCalcValue value, int *error_item)
{
    const char *missing = _("This program can only calculate one value at a time. "
                            "You must enter values for all but one quantity.");
    const char *bad_exp = _("GnuCash cannot determine the value in one of the fields. "
                            "You must enter a valid expression.");
    const char *string;
    gnc_numeric nvalue;
    unsigned int i;

    if (fcd == NULL)
        return NULL;

    /* Check for missing values */
    for (i = 0; i < NUM_FIN_CALC_VALUES; i++)
        if (i != value)
        {
            string = gtk_entry_get_text(GTK_ENTRY(fcd->amounts[i]));
            if ((string == NULL) || (*string == '\0'))
            {
                *error_item = i;
                return missing;
            }

            if (!gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (fcd->amounts[i])))
            {
                *error_item = i;
                return bad_exp;
            }
        }

    /* Check for zero interest */
    switch (value)
    {
    case PAYMENT_PERIODS:
    case PRESENT_VALUE:
    case PERIODIC_PAYMENT:
    case FUTURE_VALUE:
        nvalue = gnc_amount_edit_get_amount
                 (GNC_AMOUNT_EDIT (fcd->amounts[INTEREST_RATE]));
        if (gnc_numeric_zero_p (nvalue))
        {
            *error_item = INTEREST_RATE;
            return _("The interest rate cannot be zero.");
        }
        break;
    default:
        break;
    }

    /* Check for zero payment periods */
    switch (value)
    {
    case INTEREST_RATE:
    case PRESENT_VALUE:
    case PERIODIC_PAYMENT:
    case FUTURE_VALUE:
        nvalue = gnc_amount_edit_get_amount
                 (GNC_AMOUNT_EDIT(fcd->amounts[PAYMENT_PERIODS]));
        if (gnc_numeric_zero_p (nvalue))
        {
            *error_item = PAYMENT_PERIODS;
            return _("The number of payments cannot be zero.");
        }
        if (gnc_numeric_negative_p (nvalue))
        {
            *error_item = PAYMENT_PERIODS;
            return _("The number of payments cannot be negative.");
        }
        break;
    default:
        break;
    }

    return NULL;
}