static void
_show_created_transactions(GncSxSinceLastRunDialog *app_dialog, GList *created_txn_guids)
{
    GNCLedgerDisplay *ledger;
    GncPluginPage *page;
    Query *book_query, *guid_query, *query;
    GList *guid_iter;

    book_query = qof_query_create_for(GNC_ID_SPLIT);
    guid_query = qof_query_create_for(GNC_ID_SPLIT);
    qof_query_set_book(book_query, gnc_get_current_book());
    for (guid_iter = created_txn_guids; guid_iter != NULL; guid_iter = guid_iter->next)
    {
        xaccQueryAddGUIDMatch(guid_query, (GncGUID*)guid_iter->data, GNC_ID_TRANS, QOF_QUERY_OR);
    }
    query = qof_query_merge(book_query, guid_query, QOF_QUERY_AND);

    // inspired by dialog-find-transactions:do_find_cb:
    ledger = gnc_ledger_display_query(query, SEARCH_LEDGER, REG_STYLE_JOURNAL);
    gnc_ledger_display_refresh(ledger);
    page = gnc_plugin_page_register_new_ledger(ledger);
    g_object_set(G_OBJECT(page), "page-name", _("Created Transactions"), NULL);
    gnc_main_window_open_page(NULL, page);

    qof_query_destroy(query);
    qof_query_destroy(book_query);
    qof_query_destroy(guid_query);
}
Example #2
0
static void
run_tests (void)
{
    Query *q;
    SCM val2str;
    int i;

    val2str = scm_c_eval_string ("gnc:value->string");
    g_return_if_fail (scm_is_procedure (val2str));

    for (i = 0; i < 1000; i++)
    {
        q = get_random_query ();
        test_query (q, val2str);
        qof_query_destroy (q);
        fflush(stdout);
    }

    {
        q = get_random_query ();
        test_query (q, val2str);
        qof_query_destroy (q);
        fflush(stdout);
    }

}
Example #3
0
static int
test_trans_query (Transaction *trans, gpointer data)
{
    QofBook *book = data;
    GList *list;
    QofQuery *q;

    q = make_trans_query (trans, ALL_QT);
    qof_query_set_book (q, book);

    list = xaccQueryGetTransactions (q, QUERY_TXN_MATCH_ANY);
    if (g_list_length (list) != 1)
    {
        failure_args ("test number returned", __FILE__, __LINE__,
                      "number of matching transactions %d not 1",
                      g_list_length (list));
        g_list_free (list);
        return 13;
    }

    if (list->data != trans)
    {
        failure ("matching transaction is wrong");
        g_list_free (list);
        return 13;
    }

    success ("found right transaction");
    qof_query_destroy (q);
    g_list_free (list);

    return 0;
}
Example #4
0
static void
test_query (QofQuery *q)
{
    SCM scm_q;
    QofQuery *q2;

    scm_q = gnc_query2scm (q);

    q2 = gnc_scm2query (scm_q);

    if (!qof_query_equal (q, q2))
    {
        failure ("queries don't match");
        scm_display (scm_q, SCM_UNDEFINED);
        scm_newline (SCM_UNDEFINED);
        scm_q = gnc_query2scm (q2);
        scm_display (scm_q, SCM_UNDEFINED);
        scm_newline (SCM_UNDEFINED);
        exit (1);
    }
    else
    {
        success ("queries match");
    }

    qof_query_destroy (q2);
}
Example #5
0
/** Finds the GncEntry with the matching description string as given
 * in "desc", but searches this in the whole book. */
static GncEntry*
find_entry_in_book_by_desc(GncEntryLedger *reg, const char* desc)
{
    GncEntry *result = NULL;
    gboolean use_invoice;
    QofQuery *query;
    GList *entries = NULL;

    switch (reg->type)
    {
    case GNCENTRY_INVOICE_ENTRY:
    case GNCENTRY_INVOICE_VIEWER:
    case GNCENTRY_CUST_CREDIT_NOTE_ENTRY:
    case GNCENTRY_CUST_CREDIT_NOTE_VIEWER:
        use_invoice = TRUE;
        break;
    default:
        use_invoice = FALSE;
    };

    query = new_query_for_entry_desc(reg, desc, use_invoice);
    entries = qof_query_run(query);

    /* Do we have a non-empty result? */
    if (entries)
    {
        /* That's the result. */
        result = (GncEntry*) entries->data;
        /*g_warning("Found %d GncEntry items", g_list_length (entries));*/
    }

    qof_query_destroy(query);
    return result;
}
static AddressQF* build_shared_quickfill (QofBook *book, const char * key)
{
    AddressQF *result;
    QofQuery *query = new_query_for_addresss(book);
    GList *entries = qof_query_run(query);

    /*     g_warning("Found %d GncAddress items", g_list_length (entries)); */

    result = g_new0(AddressQF, 1);

    result->qf_addr2 = gnc_quickfill_new();
    result->qf_addr3 = gnc_quickfill_new();
    result->qf_addr4 = gnc_quickfill_new();
    result->qf_sort = QUICKFILL_ALPHA;
    result->book = book;

    g_list_foreach (entries, address_cb, result);

    qof_query_destroy(query);

    result->listener =
        qof_event_register_handler (listen_for_gncaddress_events,
                                    result);

    qof_book_set_data_fin (book, key, result, shared_quickfill_destroy);

    return result;
}
Example #7
0
static int
get_num_xactions_before_date(QofBook *book, time64 close_date)
{
    QofQuery *q;
    GSList *param;
    QofQueryPredData *pred;
    GList *res, *n;
    int cnt = 0;

    q = qof_query_create_for(GNC_ID_TRANS);
    qof_query_set_max_results(q, -1);
    qof_query_set_book (q, book);

    /* Look for transactions earlier than the closing date */
    param = g_slist_prepend (NULL, TRANS_DATE_POSTED);
    pred = qof_query_date_predicate (QOF_COMPARE_LTE, QOF_DATE_MATCH_NORMAL, close_date);
    qof_query_add_term (q,  param, pred, QOF_QUERY_FIRST_TERM);

    /* Run the query, find how many transactions there are */
    res = qof_query_run (q);

    cnt = 0;
    for (n = res; n; n = n->next) cnt ++;

    qof_query_destroy (q);
    return cnt;
}
Example #8
0
static void
gnc_query_list_destroy (GtkObject *object)
{
    GNCQueryList *list = GNC_QUERY_LIST(object);
    GNCQueryListPriv *priv;

    priv = GNC_QUERY_LIST_GET_PRIVATE(list);
    if (priv->component_id > 0)
    {
        gnc_unregister_gui_component (priv->component_id);
        priv->component_id = 0;
    }
    if (list->query)
    {
        qof_query_destroy(list->query);
        list->query = NULL;
    }
    if (list->column_params)
    {
        /* XXX: free the params list??? */
    }
    if (list->title_arrows)
    {
        g_free(list->title_arrows);
        list->title_arrows = NULL;
    }
    if (list->title_widths)
    {
        g_free(list->title_widths);
        list->title_widths = NULL;
    }

    if (GTK_OBJECT_CLASS(parent_class)->destroy)
        GTK_OBJECT_CLASS(parent_class)->destroy (object);
}
static void
do_find_cb (QofQuery *query, gpointer user_data, gpointer *result)
{
    struct _ftd_data *ftd = user_data;
    GNCLedgerDisplay2 *ledger;
    gboolean new_ledger = FALSE;
    GncPluginPage *page;

    ledger = gnc_ledger_display2_find_by_query (ftd->ledger_q);

    if (!ledger)
    {
        new_ledger = TRUE;
        ledger = gnc_ledger_display2_query (query, SEARCH_LEDGER2,
                                           REG2_STYLE_JOURNAL);
    }
    else
        gnc_ledger_display2_set_query (ledger, query);

    gnc_ledger_display2_refresh (ledger);

    if (new_ledger)
    {
        page = gnc_plugin_page_register2_new_ledger (ledger);
        gnc_main_window_open_page (NULL, page);
    }

    qof_query_destroy (ftd->q);

    gnc_search_dialog_destroy (ftd->sw);
}
Example #10
0
static void
close_handler (gpointer user_data)
{
    GNCLedgerDisplay2 *ld = user_data;

    if (!ld)
        return;

    ENTER(" ");

    gnc_unregister_gui_component (ld->component_id);

    if (ld->destroy)
        ld->destroy (ld);

    gnc_tree_model_split_reg_destroy (ld->model);
    ld->model = NULL;
    ld->view = NULL;

    qof_query_destroy (ld->query);
    ld->query = NULL;

    LEAVE(" ");
    g_free (ld);
}
Example #11
0
void
xaccQueryAddDateMatchTS (QofQuery * q,
                         gboolean use_start, Timespec sts,
                         gboolean use_end, Timespec ets,
                         QofQueryOp op)
{
    QofQuery *tmp_q = NULL;
    QofQueryPredData *pred_data;
    GSList *param_list;

    if (!q || (!use_start && !use_end))
        return;

    tmp_q = qof_query_create ();

    if (use_start)
    {
        pred_data = qof_query_date_predicate (QOF_COMPARE_GTE, QOF_DATE_MATCH_NORMAL, sts);
        if (!pred_data)
        {
            qof_query_destroy (tmp_q);
            return;
        }

        param_list = qof_query_build_param_list (SPLIT_TRANS, TRANS_DATE_POSTED, NULL);
        qof_query_add_term (tmp_q, param_list, pred_data, QOF_QUERY_AND);
    }

    if (use_end)
    {
        pred_data = qof_query_date_predicate (QOF_COMPARE_LTE, QOF_DATE_MATCH_NORMAL, ets);
        if (!pred_data)
        {
            qof_query_destroy (tmp_q);
            return;
        }

        param_list = qof_query_build_param_list (SPLIT_TRANS, TRANS_DATE_POSTED, NULL);
        qof_query_add_term (tmp_q, param_list, pred_data, QOF_QUERY_AND);
    }

    qof_query_merge_in_place (q, tmp_q, op);
    qof_query_destroy (tmp_q);
}
Example #12
0
static void
run_tests (void)
{
    QofQuery *q;
    int i;

    test_query (NULL);

    q = qof_query_create_for(GNC_ID_SPLIT);
    test_query (q);
    qof_query_destroy (q);

    for (i = 0; i < 50; i++)
    {
        q = get_random_query ();
        test_query (q);
        qof_query_destroy (q);
    }
}
Example #13
0
void gnc_query_list_reset_query (GNCQueryList *list, Query *query)
{
    g_return_if_fail(list);
    g_return_if_fail(query);
    g_return_if_fail (IS_GNC_QUERY_LIST(list));

    qof_query_destroy(list->query);
    list->query = qof_query_copy(query);
    gnc_query_list_set_query_sort(list, TRUE);
}
Example #14
0
static void
free_userdata_cb (gpointer user_data)
{
    struct _customer_select_window *sw = user_data;

    g_return_if_fail (sw);

    qof_query_destroy (sw->q);
    g_free (sw);
}
Example #15
0
void
gnc_ledger_display_set_query (GNCLedgerDisplay *ledger_display, Query *q)
{
    if (!ledger_display || !q)
        return;

    g_return_if_fail (ledger_display->ld_type == LD_GL);

    qof_query_destroy (ledger_display->query);
    ledger_display->query = qof_query_copy (q);
}
Example #16
0
static void
gnc_ledger_display_make_query (GNCLedgerDisplay *ld,
                               gint limit,
                               SplitRegisterType type)
{
    Account *leader;
    GList *accounts;

    if (!ld)
        return;

    switch (ld->ld_type)
    {
    case LD_SINGLE:
    case LD_SUBACCOUNT:
        break;

    case LD_GL:
        return;

    default:
        PERR ("unknown ledger type: %d", ld->ld_type);
        return;
    }

    qof_query_destroy (ld->query);
    ld->query = qof_query_create_for(GNC_ID_SPLIT);

    /* This is a bit of a hack. The number of splits should be
     * configurable, or maybe we should go back a time range instead
     * of picking a number, or maybe we should be able to exclude
     * based on reconciled status. Anyway, this works for now. */
    if ((limit != 0) && (type != SEARCH_LEDGER))
        qof_query_set_max_results (ld->query, limit);

    qof_query_set_book (ld->query, gnc_get_current_book());

    leader = gnc_ledger_display_leader (ld);

    if (ld->ld_type == LD_SUBACCOUNT)
        accounts = gnc_account_get_descendants (leader);
    else
        accounts = NULL;

    accounts = g_list_prepend (accounts, leader);

    xaccQueryAddAccountMatch (ld->query, accounts,
                              QOF_GUID_MATCH_ANY, QOF_QUERY_AND);

    g_list_free (accounts);
}
Example #17
0
static void
test_query (Query *q, SCM val2str)
{
    SCM scm_q;
    SCM str_q;
    SCM res_q;
    SCM args = SCM_EOL;
    Query *q2;
    gchar *str2 = NULL;

    scm_q = gnc_query2scm (q);
    args = scm_cons (scm_q, SCM_EOL);
    str_q = scm_apply (val2str, args, SCM_EOL);

    args = scm_cons (scm_from_utf8_string ("'"), scm_cons (str_q, SCM_EOL));
    str_q = scm_string_append (args);

    str2 = gnc_scm_to_utf8_string (str_q);
    if (str2)
    {
        res_q = scm_c_eval_string (str2);
    }
    else
    {
        res_q = SCM_BOOL_F;
    }

    q2 = gnc_scm2query (res_q);

    if (!qof_query_equal (q, q2))
    {
        failure ("queries don't match");
        fprintf (stderr, "%s\n\n", str2 ? str2 : "(null)");
        scm_q = gnc_query2scm (q2);
        scm_display (scm_q, SCM_UNDEFINED);
        scm_newline (SCM_UNDEFINED);
        g_free(str2);
        exit (1);
    }
    else
    {
        success ("queries match");
    }
    g_free(str2);
    if (q2) qof_query_destroy (q2);
}
static void
run_tests (int count)
{
    Query *q;
    SCM val2str;
    int i;

    val2str = scm_c_eval_string ("gnc:value->string");
    g_return_if_fail (scm_is_procedure (val2str));

    for (i = 0; i < count; i++)
    {
        q = get_random_query ();
        test_query (q, val2str);
        qof_query_destroy (q);
    }
    success ("");
}
Example #19
0
static void
close_handler (gpointer user_data)
{
    GNCLedgerDisplay *ld = user_data;

    if (!ld)
        return;

    gnc_unregister_gui_component (ld->component_id);

    if (ld->destroy)
        ld->destroy (ld);

    gnc_split_register_destroy (ld->reg);
    ld->reg = NULL;

    qof_query_destroy (ld->query);
    ld->query = NULL;

    g_free (ld);
}
Example #20
0
/*******************************************************************
 * get_earliest_in_book
 *
 * Find the earliest date occuring in the book.  Do this by making
 * a query and sorting by date. Since the truncated sort returns
 * only the *last* search results, sort in decreasing order.
 *******************************************************************/
static time64
get_earliest_in_book (QofBook *book)
{
    QofQuery *q;
    GSList *p1, *p2;
    GList *res;
    time64 earliest;

    q = qof_query_create_for (GNC_ID_SPLIT);
    qof_query_set_max_results (q, 1);
    qof_query_set_book (q, book);

    /* Sort by transaction date */
    p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED);
    p1 = g_slist_prepend (p1, SPLIT_TRANS);
    p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT);
    qof_query_set_sort_order (q, p1, p2, NULL);

    /* Reverse the sort order */
    qof_query_set_sort_increasing (q, FALSE, FALSE, FALSE);

    /* Run the query, find the earliest transaction date */
    res = qof_query_run (q);

    if (res)
    {
        earliest = xaccQueryGetEarliestDateFound (q);
    }
    else
    {
        /* If no results, we don't want to bomb totally */
        earliest = gnc_time (0);
    }

    qof_query_destroy (q);
    return earliest;
}
Example #21
0
int
main (int argc, char *argv[])
{
    int err, fake_argc = 1;
    char * fake_argv[] = {"hello", 0};
    QofBook *book;
    Account *root;
    char *bufp;
    int rc, sz;

    /* intitialize the engine */
    gnc_engine_init (fake_argc, fake_argv);

    /* contact the database, which is a flat file for this demo */
    book = qof_book_new ();

    rc = gnc_book_begin (book, "file:/tmp/demo.gnucash", FALSE);
    if (!rc) goto bookerrexit;

    rc = gnc_book_load (book);
    if (!rc) goto bookerrexit;

    /* the root pointer points to our local cache of the data */
    root = gnc_book_get_root_account (book);

    /* --------------------------------------------------- */
    /* done with initialization, go into event loop */

    while (FCGI_Accept() >= 0)
    {
        GList *split_list;
        Query *q = NULL;
        char *request_method;
        int read_len = 0;

        /* get the request method */
        request_method = getenv ("REQUEST_METHOD");

        /* Lets pretend that method=get means user has logged
         * in.  Send the user the accounts and currencies,
         * but not the transactions/splits. */
        if (!strcmp ("GET", request_method))
        {
            gncxml_write_account_tree_to_buf(root, &bufp, &sz);

            /* print the HTTP header */
            printf("Content-type: text/gnc-xml\r\n"
                   "Content-Length: %d\r\n"
                   "\r\n", sz);

            /* send the xml to the client */
            printf ("%s", bufp);
            free (bufp);

            /* wait for the next request */
            continue;
        }


        if (!strcmp ("POST", request_method))
        {
            char * content_length = getenv("CONTENT_LENGTH");
            read_len = atoi (content_length);

            /* read 'read_len' bytes from stdin ... */
            bufp = (char *) malloc (read_len);
            fread (bufp, read_len, 1, stdin);

            /* conver the xml input into a gnucash query structure... */
            q = gncxml_read_query (bufp, read_len);
            xaccQuerySetGroup (q, root);

            /* hack -- limit to 30 splits ... */
            qof_query_set_max_results (q, 30);
            split_list = qof_query_run (q);

            qof_query_destroy (q);

            /* wait for the next request */
            continue;
        }

        /* if we got to here, an error -- unknown method */
        printf("Content-type: text/plain\r\n"
               "\r\n"
               "unknown request type \n");


    }

bookerrexit:

    err = gnc_book_get_error (book);

    /* 500 Server Error */
    FCGI_SetExitStatus (500);

    printf("Content-type: text/plain\r\n\r\n"
           "error was %s\n", strerror (err));

    FCGI_Finish();

    /* close the book */
    qof_book_destroy (book);

    /* shut down the engine */
    gnc_engine_shutdown ();

    sleep (1);

    return 0;
}
static void
create_children (GNCGeneralSearch *gsl,
                 const char       *label,
                 gboolean          text_editable,
                 QofIdTypeConst    type,
                 QofBook          *book)
{
    GtkListStore *	list_store;
    QofQuery *	q;
    GtkTreeIter iter;
    GList * list, * it;
    GtkEntryCompletion *completion;

    /* Add a text entry box */
    gsl->entry = gtk_entry_new ();
    if (!text_editable)
        gtk_editable_set_editable (GTK_EDITABLE (gsl->entry), FALSE);
    gtk_box_pack_start (GTK_BOX (gsl), gsl->entry, TRUE, TRUE, 0);


    /* Setup a GtkEntryCompletion auxiliary widget for our Entry box
     * This requires an internal table ("model") with the possible
     * auto-completion text entries */

    /* Query for the requested object type */
    q = qof_query_create_for (type);
    qof_query_add_boolean_match(q, g_slist_prepend
                                (NULL, QOF_PARAM_ACTIVE), TRUE, QOF_QUERY_AND);
    qof_query_set_book (q, book);
    list = qof_query_run(q);

    /* Setup the internal model */
    list_store = gtk_list_store_new (GSL_N_COLUMNS, G_TYPE_STRING, G_TYPE_OBJECT);
    for (it = list; it != NULL ; it = it->next)
    {
        char * name;

        name = g_strdup(qof_object_printable(type, it->data));
        /* Add a new row to the model */
        if (name)
        {
            gtk_list_store_append (list_store, &iter);
            gtk_list_store_set (list_store, &iter,
                                GSL_COLUMN_TEXT, name,
                                GSL_COLUMN_QOFOBJECT, G_OBJECT(it->data),
                                -1);
            g_free(name);
        }

    }

    qof_query_destroy(q);

    /* Add the GtkEntryCompletion widget */
    completion = gtk_entry_completion_new();
    gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(list_store));
    gtk_entry_completion_set_text_column(completion, 0);
    gtk_entry_completion_set_inline_completion(completion, TRUE);
    gtk_entry_set_completion(GTK_ENTRY(gsl->entry), completion);

    g_signal_connect (G_OBJECT (completion), "match_selected",
                      G_CALLBACK (gnc_gsl_match_selected_cb), gsl);
    g_signal_connect (G_OBJECT (gsl->entry), "focus-out-event",
                      G_CALLBACK (gnc_gsl_focus_out_cb), gsl);

    g_object_unref(completion);
    gtk_widget_show (gsl->entry);

    /* Add the search button */
    gsl->button = gtk_button_new_with_label (label);
    gtk_box_pack_start (GTK_BOX (gsl), gsl->button, FALSE, FALSE, 0);
    g_signal_connect (G_OBJECT (gsl->button), "clicked",
                      G_CALLBACK (search_cb), gsl);
    gtk_widget_show (gsl->button);
}
Example #23
0
int
main (int argc, char *argv[])
{
    int err, fake_argc = 1;
    char * fake_argv[] = {"hello", 0};
    QofBook *book;
    Account *root;
    char *request_bufp, *reply_bufp;
    int rc, sz;

    /* intitialize the engine */
    gnc_engine_init (fake_argc, fake_argv);

    /* contact the database, which is a flat file for this demo */
    /* this should really be an SQL server */
    book = qof_book_new ();

    rc = gnc_book_begin (book, "file:/tmp/demo.gnucash", FALSE);
    if (!rc) goto bookerrexit;

    rc = gnc_book_load (book);
    if (!rc) goto bookerrexit;

    /* the root pointer points to our local cache of the data */
    root = gnc_book_get_root_account (book);

    /* --------------------------------------------------- */
    /* done with initialization, go into event loop */

    while (FCGI_Accept() >= 0)
    {
        GList *split_list;
        Query *q = NULL;
        const char *request_method;
        const char *user_agent;
        const char *auth_string;
        const char *content_length;
        gchar guidstr[GUID_ENCODING_LENGTH+1];
        int read_len = 0;
        int send_accts = 0;

        /* get the user agent; reject if wrong agent */
        user_agent = getenv ("HTTP_USER_AGENT");
        if (strncmp ("gnucash", user_agent, 7))
        {
            reject_user_agent (user_agent);
            continue;
        }

        /* get the request method */
        request_method = getenv ("REQUEST_METHOD");
        if (strcmp ("POST", request_method))
        {
            /* method=post is the only spported method*/
            reject_method(request_method);
            continue;
        }

        /* ----------------------------------------------- */
        /* look for an authentication cookie */
        auth_string = find_cookie ("gnc-server");

        /* found the cookie, lets make sure that it is valid */
        if (auth_string)
        {
            gboolean valid_session;
            valid_session = have_session (auth_string);
            if (!valid_session)
            {

                /* XXX invalid sessions are a sign of hacking;
                 * this event should be noted in a security log
                 * and the server admin contacted.
                 */
                reject_session (auth_string);
                continue;
            }
        }

        /* go ahead and read the message body.
         * we'll need this soon enough */
        content_length = getenv("CONTENT_LENGTH");
        read_len = atoi (content_length);

        /* read 'read_len' bytes from stdin ... */
        request_bufp = (char *) g_malloc (read_len);
        fread (request_bufp, read_len, 1, stdin);

        /* if no previously authenticated session,
         * authenticate now */
        if (!auth_string)
        {
            char *name = NULL, *passwd = NULL;
            parse_for_login (request_bufp, &name, &passwd);

            auth_string = auth_user (name, passwd, guidstr);
            if (!auth_string)
            {
                reject_auth();
                g_free (request_bufp);
                continue;
            }
            send_accts = 1;
        }

        /* ----------------------------------------------- */
        /* send only the accounts to the user */
        if (send_accts)
        {
            /* print the HTTP header */
            printf("Content-type: text/gnc-xml\r\n"
                   "Set-Cookie: %s\r\n"
                   "Content-Length: %d\r\n"
                   "\r\n",
                   auth_string, sz);

            /* since this is the first time the user is logging
             * in, send them the full set of accounts.
             * (Do not send them any transactions yet).
             */
            gncxml_write_account_tree_to_buf(root, &reply_bufp, &sz);

            /* send the xml to the client */
            printf ("%s", reply_bufp);
            g_free (request_bufp);

            /* wait for the next request */
            continue;
        }

        /* ----------------------------------------------- */
        /* If we got to here, then the ser should be sending
         * us a query xml.
         * we should somehow error check that what we got
         * is really a valid query
         */

        /* conver the xml input into a gnucash query structure... */
        q = gncxml_read_query (request_bufp, read_len);
        xaccQuerySetGroup (q, root);

        /* hack -- limit to 30 splits ... */
        qof_query_set_max_results (q, 30);
        split_list = qof_query_run (q);

        /* poke those splits into an ccount group structure */
        /* XXX not implemented */

        /* send the account group structure back to the user */
        /* XXX not implemented */

        qof_query_destroy (q);
        g_free (request_bufp);

    }

bookerrexit:

    err = gnc_book_get_error (book);

    /* 500 Server Error */
    FCGI_SetExitStatus (500);

    printf("Content-type: text/plain\r\n\r\n"
           "error was %s\n", strerror (err));

    FCGI_Finish();

    /* close the book */
    qof_book_destroy (book);

    /* shut down the engine */
    gnc_engine_shutdown ();

    sleep (1);

    /* must return a non-zero error code, otherwise fastcgi
     * attempts to respawn this daemon. */
    return 500;
}
Example #24
0
GtkWidget *
gnc_reconcile_view_new (Account *account, GNCReconcileViewType type,
                       time64 statement_date)
{
    GNCReconcileView *view;
    GtkListStore     *liststore;
    gboolean          include_children, auto_check;
    GList            *accounts = NULL;
    GList            *splits;
    Query            *query;

    g_return_val_if_fail (account, NULL);
    g_return_val_if_fail ((type == RECLIST_DEBIT) ||
                         (type == RECLIST_CREDIT), NULL);

    view = g_object_new (GNC_TYPE_RECONCILE_VIEW, NULL);

    /* Create the list store with 6 columns and add to treeview,
       column 0 will be a pointer to the entry */
    liststore = gtk_list_store_new (6, G_TYPE_POINTER, G_TYPE_STRING,
                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,  G_TYPE_BOOLEAN );
    gtk_tree_view_set_model (GTK_TREE_VIEW (view), GTK_TREE_MODEL (liststore));
    g_object_unref (liststore);

    view->account = account;
    view->view_type = type;
    view->statement_date = statement_date;

    query = qof_query_create_for (GNC_ID_SPLIT);
    qof_query_set_book (query, gnc_get_current_book ());

    include_children = xaccAccountGetReconcileChildrenStatus (account);
    if (include_children)
        accounts = gnc_account_get_descendants (account);

    /* match the account */
    accounts = g_list_prepend (accounts, account);

    xaccQueryAddAccountMatch (query, accounts, QOF_GUID_MATCH_ANY, QOF_QUERY_AND);

    g_list_free (accounts);

    /* limit the matches to CREDITs and DEBITs only, depending on the type */
    if (type == RECLIST_CREDIT)
        xaccQueryAddValueMatch(query, gnc_numeric_zero (),
                               QOF_NUMERIC_MATCH_CREDIT,
                               QOF_COMPARE_GTE, QOF_QUERY_AND);
    else
        xaccQueryAddValueMatch(query, gnc_numeric_zero (),
                               QOF_NUMERIC_MATCH_DEBIT,
                               QOF_COMPARE_GTE, QOF_QUERY_AND);

    /* limit the matches only to Cleared and Non-reconciled splits */
    xaccQueryAddClearedMatch (query, CLEARED_NO | CLEARED_CLEARED, QOF_QUERY_AND);

    /* Initialize the QueryList */
    gnc_reconcile_view_construct (view, query);

    /* find the list of splits to auto-reconcile */
    auto_check = gnc_prefs_get_bool (GNC_PREFS_GROUP_RECONCILE, GNC_PREF_CHECK_CLEARED);

    if (auto_check)
    {
        time64 statement_date_day_end = gnc_time64_get_day_end(statement_date);
        for (splits = qof_query_run (query); splits; splits = splits->next)
        {
            Split *split = splits->data;
            char recn = xaccSplitGetReconcile (split);
            time64 trans_date = xaccTransGetDate (xaccSplitGetParent (split));

            /* Just an extra verification that our query is correct ;) */
            g_assert (recn == NREC || recn == CREC);

            if (recn == CREC &&
        gnc_difftime (trans_date, statement_date_day_end) <= 0)
        g_hash_table_insert (view->reconciled, split, split);
        }
    }

    /* Free the query -- we don't need it anymore */
    qof_query_destroy (query);

    return GTK_WIDGET (view);
}
/*******************************************************
 * account_splits
 *
 * gather the splits / transactions for an account and
 * send them to a file
 *******************************************************/
static
void account_splits (CsvExportInfo *info, Account *acc, FILE *fh )
{
    Query   *q;
    GSList  *p1, *p2;
    GList   *splits;
    QofBook *book;

    q = qof_query_create_for (GNC_ID_SPLIT);
    book = gnc_get_current_book();
    qof_query_set_book (q, book);

    /* Sort by transaction date */
    p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED);
    p1 = g_slist_prepend (p1, SPLIT_TRANS);
    p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT);
    qof_query_set_sort_order (q, p1, p2, NULL);

    xaccQueryAddSingleAccountMatch (q, acc, QOF_QUERY_AND);
    xaccQueryAddDateMatchTT (q, TRUE, info->csvd.start_time, TRUE, info->csvd.end_time, QOF_QUERY_AND);

    /* Run the query */
    for (splits = qof_query_run (q); splits; splits = splits->next)
    {
        Split       *split;
        Transaction *trans;
        SplitList   *s_list;
        GList       *node;
        Split       *t_split;
        int          nSplits;
        int          cnt;
        gchar       *line;

        split = splits->data;
        trans = xaccSplitGetParent (split);
        nSplits = xaccTransCountSplits (trans);
        s_list = xaccTransGetSplitList (trans);

        // Look for trans already exported in trans_list
        if (g_list_find (info->trans_list, trans) != NULL)
            continue;

        // Simple Layout
        if (info->simple_layout)
        {
            line = make_simple_trans_line (acc, trans, split, info);

            /* Write to file */
            if (!write_line_to_file (fh, line))
            {
                info->failed = TRUE;
                break;
            }
            g_free (line);
            continue;
        }

        // Complex Transaction Line.
        line = make_complex_trans_line (acc, trans, split, info);

        /* Write to file */
        if (!write_line_to_file (fh, line))
        {
            info->failed = TRUE;
            break;
        }
        g_free (line);

        /* Loop through the list of splits for the Transaction */
        node = s_list;
        cnt = 0;
        while ((cnt < nSplits) && (info->failed == FALSE))
        {
            t_split = node->data;

            // Complex Split Line.
            line = make_complex_split_line (trans, t_split, info);

            if (!write_line_to_file (fh, line))
                info->failed = TRUE;

            g_free (line);

            cnt++;
            node = node->next;
        }
        info->trans_list = g_list_prepend (info->trans_list, trans); // add trans to trans_list
    }
    qof_query_destroy (q);
    g_list_free (splits);
}
Example #26
0
/*******************************************************
 * account_splits
 *
 * gather the splits / transactions for an account and
 * send them to a file
 *******************************************************/
static
void account_splits (CsvExportInfo *info, Account *acc, FILE *fh )
{
    Query   *q;
    GSList  *p1, *p2;
    GList   *splits;
    QofBook *book;

    gchar   *end_sep;
    gchar   *mid_sep;

    q = qof_query_create_for(GNC_ID_SPLIT);
    book = gnc_get_current_book();
    qof_query_set_book (q, book);

    /* Set up separators */
    if (info->use_quotes)
    {
        end_sep = "\"";
        mid_sep = g_strconcat ( "\"", info->separator_str, "\"", NULL);
    }
    else
    {
        end_sep = "";
        mid_sep = g_strconcat ( info->separator_str, NULL);
    }

    /* Sort by transaction date */
    p1 = g_slist_prepend (NULL, TRANS_DATE_POSTED);
    p1 = g_slist_prepend (p1, SPLIT_TRANS);
    p2 = g_slist_prepend (NULL, QUERY_DEFAULT_SORT);
    qof_query_set_sort_order (q, p1, p2, NULL);

    xaccQueryAddSingleAccountMatch (q, acc, QOF_QUERY_AND);
    xaccQueryAddDateMatchTT (q, TRUE, info->csvd.start_time, TRUE, info->csvd.end_time, QOF_QUERY_AND);

    /* Run the query */
    for (splits = qof_query_run(q); splits; splits = splits->next)
    {
        Split       *split;
        Transaction *trans;
        SplitList   *s_list;
        GList       *node;
        Split       *t_split;
        int          nSplits;
        int          cnt;
        gchar       *part1;
        gchar       *part2;
        gchar       *date;
        const gchar *currentSel;
        const gchar *split_amount;

        split = splits->data;
        trans = xaccSplitGetParent(split);
        nSplits = xaccTransCountSplits(trans);
        s_list = xaccTransGetSplitList(trans);

        /* Date */
        date = qof_print_date ( xaccTransGetDate(trans));
        part1 = g_strconcat ( end_sep, date, mid_sep, NULL);
        g_free(date);
        /* Name */
        currentSel = xaccAccountGetName(acc);
        part2 = g_strconcat ( part1, currentSel, mid_sep, NULL);
        g_free(part1);
        /* Number */
        currentSel = gnc_get_num_action(trans, NULL);
        part1 = g_strconcat ( part2, currentSel, mid_sep, NULL);
        g_free(part2);
        /* Description */
        currentSel = xaccTransGetDescription(trans);
        part2 = g_strconcat ( part1, currentSel, mid_sep, NULL);
        g_free(part1);
        /* Notes */
        currentSel = xaccTransGetNotes(trans);
        if (currentSel == NULL)
            part1 = g_strconcat ( part2, mid_sep, NULL);
        else
            part1 = g_strconcat ( part2, currentSel, mid_sep, NULL);
        g_free(part2);
        /* Memo */
        currentSel = xaccSplitGetMemo(split);
        part2 = g_strconcat ( part1, currentSel, mid_sep, NULL);
        g_free(part1);
        /* Category */
        currentSel = xaccSplitGetCorrAccountName(split);
        part1 = g_strconcat ( part2, currentSel, mid_sep, "T", mid_sep, NULL);
        g_free(part2);
        /* Action */
        currentSel =  gnc_get_num_action(NULL, split);
        part2 = g_strconcat ( part1, currentSel, mid_sep, NULL);
        g_free(part1);
        /* Reconcile */
        switch (xaccSplitGetReconcile (split))
        {
        case NREC:
            currentSel = "N";
            break;
        case CREC:
            currentSel = "C";
            break;
        case YREC:
            currentSel = "Y";
            break;
        case FREC:
            currentSel = "F";
            break;
        case VREC:
            currentSel = "V";
            break;
        default:
            currentSel = "N";
        }
        part1 = g_strconcat ( part2, currentSel, mid_sep, NULL);
        g_free(part2);
        /* To with Symbol */
        split_amount = xaccPrintAmount(xaccSplitGetAmount(split), gnc_split_amount_print_info(split, TRUE));
        part2 = g_strconcat ( part1, split_amount, mid_sep, NULL);
        g_free(part1);

        /* From with Symbol */
        part1 = g_strconcat ( part2, "", mid_sep, NULL);
        g_free(part2);

        /* To Number Only */
        split_amount = xaccPrintAmount(xaccSplitGetAmount(split), gnc_split_amount_print_info(split, FALSE));
        part2 = g_strconcat ( part1, split_amount, mid_sep, NULL);
        g_free(part1);

        /* From Number Only */
        part1 = g_strconcat ( part2, "", mid_sep, "", mid_sep, "", end_sep, "\n", NULL);
        g_free(part2);

        /* Write to file */
        if (!write_line_to_file(fh, part1))
        {
            info->failed = TRUE;
            break;
        }
        g_free(part1);

        /* Loop through the list of splits for the Transcation */
        node = s_list;
        cnt = 0;
        while ( (cnt < nSplits) && (info->failed == FALSE))
        {
            t_split = node->data;

            /* Start of line */
            part1 = g_strconcat ( end_sep, mid_sep, mid_sep, mid_sep, mid_sep, mid_sep, NULL);

            /* Memo */
            currentSel = xaccSplitGetMemo(t_split);
            part2 = g_strconcat ( part1, currentSel, mid_sep, NULL);
            g_free(part1);

            /* Account */
            currentSel = xaccAccountGetName( xaccSplitGetAccount(t_split));
            part1 = g_strconcat ( part2, currentSel, mid_sep, "S", mid_sep, NULL);
            g_free(part2);

            /* Action */
            currentSel = gnc_get_num_action(NULL, t_split);
            part2 = g_strconcat ( part1, currentSel, mid_sep, NULL);
            g_free(part1);

            /* Reconcile */
            switch (xaccSplitGetReconcile (split))
            {
            case NREC:
                currentSel = "N";
                break;
            case CREC:
                currentSel = "C";
                break;
            case YREC:
                currentSel = "Y";
                break;
            case FREC:
                currentSel = "F";
                break;
            case VREC:
                currentSel = "V";
                break;
            default:
                currentSel = "N";
            }
            part1 = g_strconcat ( part2, currentSel, mid_sep, NULL);
            g_free(part2);

            /* From / To with Symbol */
            split_amount = xaccPrintAmount(xaccSplitGetAmount(t_split), gnc_split_amount_print_info(t_split, TRUE));
            if (xaccSplitGetAccount(t_split) == acc)
                part2 = g_strconcat ( part1,  split_amount, mid_sep, mid_sep, NULL);
            else
                part2 = g_strconcat ( part1, mid_sep, split_amount, mid_sep, NULL);
            g_free(part1);

            /* From / To Numbers only */
            split_amount = xaccPrintAmount(xaccSplitGetAmount(t_split), gnc_split_amount_print_info(t_split, FALSE));
            if (xaccSplitGetAccount(t_split) == acc)
                part1 = g_strconcat ( part2,  split_amount, mid_sep, mid_sep, NULL);
            else
                part1 = g_strconcat ( part2, mid_sep, split_amount, mid_sep, NULL);
            g_free(part2);

            /* From / To - Share Price / Conversion factor */
            split_amount = xaccPrintAmount(xaccSplitGetSharePrice(t_split), gnc_split_amount_print_info(t_split, FALSE));
            if (xaccSplitGetAccount(t_split) == acc)
                part2 = g_strconcat ( part1,  split_amount, mid_sep, end_sep, "\n", NULL);
            else
                part2 = g_strconcat ( part1, mid_sep, split_amount, end_sep, "\n", NULL);
            g_free(part1);

            if (!write_line_to_file(fh, part2))
                info->failed = TRUE;

            g_free(part2);
            cnt++;
            node = node->next;
        }
    }
    g_free(mid_sep);
    qof_query_destroy (q);
    g_list_free( splits );
}
Example #27
0
static void
search_update_query (GNCSearchWindow *sw)
{
    static GSList *active_params = NULL;
    QofQuery *q, *q2, *new_q;
    GList *node;
    QofQueryOp op;
    QofQueryPredData* pdata;

    if (sw->grouping == GNC_SEARCH_MATCH_ANY)
        op = QOF_QUERY_OR;
    else
        op = QOF_QUERY_AND;

    if (active_params == NULL)
        active_params = g_slist_prepend (NULL, QOF_PARAM_ACTIVE);

    /* Make sure we supply a book! */
    if (sw->start_q == NULL)
    {
        sw->start_q = qof_query_create_for (sw->search_for);
        qof_query_set_book (sw->start_q, gnc_get_current_book ());
    }
    else
    {
        /* We've got a query -- purge it of any "active" parameters */
        qof_query_purge_terms (sw->start_q, active_params);
    }

    /* Now create a new query to work from */
    q = qof_query_create_for (sw->search_for);

    /* Walk the list of criteria */
    for (node = sw->crit_list; node; node = node->next)
    {
        struct _crit_data *data = node->data;

        pdata = gnc_search_core_type_get_predicate (data->element);
        if (pdata)
            qof_query_add_term (q, gnc_search_param_get_param_path (data->param),
                                pdata, op);
    }

    /* Now combine this query with the existing query, depending on
     * what we want to do...  We can assume that cases 1, 2, and 3
     * already have sw->q being valid!
     */

    switch (sw->search_type)
    {
    case 0:			/* New */
        new_q = qof_query_merge (sw->start_q, q, QOF_QUERY_AND);
        qof_query_destroy (q);
        break;
    case 1:			/* Refine */
        new_q = qof_query_merge (sw->q, q, QOF_QUERY_AND);
        qof_query_destroy (q);
        break;
    case 2:			/* Add */
        new_q = qof_query_merge (sw->q, q, QOF_QUERY_OR);
        qof_query_destroy (q);
        break;
    case 3:			/* Delete */
        q2 = qof_query_invert (q);
        new_q = qof_query_merge (sw->q, q2, QOF_QUERY_AND);
        qof_query_destroy (q2);
        qof_query_destroy (q);
        break;
    default:
        g_warning ("bad search type: %d", sw->search_type);
        new_q = q;
        break;
    }

    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (sw->active_only_check)))
    {
        qof_query_add_boolean_match (new_q, active_params, TRUE, QOF_QUERY_AND);
        active_params = NULL;
    }

    /* Destroy the old query */
    if (sw->q)
        qof_query_destroy (sw->q);

    /* And save the new one */
    sw->q = new_q;
}