/*******************************************************
 * 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 )
{
    GSList  *p1, *p2;
    GList   *splits;
    QofBook *book;

    // Setup the query for normal transaction export
    if (info->export_type == XML_EXPORT_TRANS)
    {
        info->query = qof_query_create_for (GNC_ID_SPLIT);
        book = gnc_get_current_book();
        qof_query_set_book (info->query, 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 (info->query, p1, p2, NULL);

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

    /* Run the query */
    for (splits = qof_query_run (info->query); 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;

        // Look for blank split
        if (xaccSplitGetAccount (split) == NULL)
            continue;

        // This will be a simple layout equivalent to a single line register view.
        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
    }
    if (info->export_type == XML_EXPORT_TRANS)
        qof_query_destroy (info->query);
    g_list_free (splits);
}
/*******************************************************
 * 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;
        gchar       *str_temp = NULL;

        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);
        str_temp = csv_txn_test_field_string (info, currentSel);
        part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
        g_free (str_temp);
        g_free (part1);
        /* Number */
        currentSel = gnc_get_num_action (trans, NULL);
        str_temp = csv_txn_test_field_string (info, currentSel);
        part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
        g_free (str_temp);
        g_free (part2);
        /* Description */
        currentSel = xaccTransGetDescription (trans) ? xaccTransGetDescription (trans) : "" ;
        str_temp = csv_txn_test_field_string (info, currentSel);
        part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
        g_free (str_temp);
        g_free (part1);
        /* Notes */
        currentSel = xaccTransGetNotes (trans) ? xaccTransGetNotes (trans) : "" ;
        str_temp = csv_txn_test_field_string (info, currentSel);
        part1 = g_strconcat (part2, str_temp, mid_sep, NULL);
        g_free (str_temp);
        g_free (part2);
        /* Memo */
        currentSel = xaccSplitGetMemo (split) ? xaccSplitGetMemo (split) : "" ;
        str_temp = csv_txn_test_field_string (info, currentSel);
        part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
        g_free (str_temp);
        g_free (part1);
        /* Category */
        currentSel = xaccSplitGetCorrAccountName (split);
        str_temp = csv_txn_test_field_string (info, currentSel);
        part1 = g_strconcat (part2, str_temp, mid_sep, "T", mid_sep, NULL);
        g_free (str_temp);
        g_free (part2);
        /* Action */
        currentSel = gnc_get_num_action (NULL, split);
        str_temp = csv_txn_test_field_string (info, currentSel);
        part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
        g_free (str_temp);
        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));
        str_temp = csv_txn_test_field_string (info, split_amount);
        part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
        g_free (str_temp);
        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));
        str_temp = csv_txn_test_field_string (info, split_amount);
        part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
        g_free (str_temp);
        g_free (part1);

        /* From Number Only */
        part1 = g_strconcat (part2, "", mid_sep, "", mid_sep, "", end_sep, EOLSTR, 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) ? xaccSplitGetMemo (t_split) : "" ;
            str_temp = csv_txn_test_field_string (info, currentSel);
            part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
            g_free (str_temp);
            g_free (part1);

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

            /* Action */
            currentSel = gnc_get_num_action (NULL, t_split);
            str_temp = csv_txn_test_field_string (info, currentSel);
            part2 = g_strconcat (part1, str_temp, mid_sep, NULL);
            g_free (str_temp);
            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));
            str_temp = csv_txn_test_field_string (info, split_amount);
            if (xaccSplitGetAccount(t_split) == acc)
                part2 = g_strconcat (part1,  str_temp, mid_sep, mid_sep, NULL);
            else
                part2 = g_strconcat (part1, mid_sep, str_temp, mid_sep, NULL);
            g_free (str_temp);
            g_free (part1);

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

            /* From / To - Share Price / Conversion factor */
            split_amount = xaccPrintAmount (xaccSplitGetSharePrice (t_split), gnc_split_amount_print_info (t_split, FALSE));
            str_temp = csv_txn_test_field_string (info, split_amount);
            if (xaccSplitGetAccount (t_split) == acc)
                part2 = g_strconcat (part1,  str_temp, mid_sep, end_sep, EOLSTR, NULL);
             else
                part2 = g_strconcat (part1, mid_sep, str_temp, end_sep, EOLSTR, NULL);
            g_free (str_temp);
            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);
}