00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "config.h"
00028
00029 #include <gtk/gtk.h>
00030 #include <glib/gi18n.h>
00031 #include <time.h>
00032
00033 #include "qof.h"
00034 #include "AccountP.h"
00035 #include "Transaction.h"
00036 #include "dialog-chart-export.h"
00037 #include "gnc-ui-util.h"
00038 #include "dialog-utils.h"
00039 #include "gnc-engine.h"
00040 #include "gnc-file.h"
00041 #include "gnc-ui.h"
00042 #include "gnc-session.h"
00043
00044 #define EQUITY_ACCOUNT_NAME _("Opening Balances")
00045 #define OPENING_BALANCE_DESC _("Opening Balance")
00046
00047 void chart_export_response_cb (GtkDialog *dialog, gint response, gpointer user_data);
00048
00049 typedef struct chart_data_s
00050 {
00051 GladeXML *xml;
00052 GtkWidget *dialog;
00053 GtkWidget *calendar;
00054 time_t chart_time_t;
00055 QofSession *chart_session;
00056 Account *equity_account;
00057 GList *param_ref_list;
00058 } chart_data;
00059
00060 static void
00061 chart_collection_cb(QofInstance *ent, gpointer user_data)
00062 {
00063 chart_data *data;
00064 Account *acc;
00065 gboolean success;
00066 const GncGUID *guid;
00067 QofCollection *copy_coll;
00068 QofBook *book;
00069
00070 g_return_if_fail(user_data != NULL);
00071 data = (chart_data*)user_data;
00072 acc = (Account*)ent;
00073 if (0 == safe_strcmp(EQUITY_ACCOUNT_NAME, xaccAccountGetName(acc))
00074 && (xaccAccountGetType(acc) == ACCT_TYPE_EQUITY))
00075 {
00076 success = qof_instance_copy_to_session(data->chart_session, ent);
00077 if (!success)
00078 {
00079 return;
00080 }
00081 guid = qof_entity_get_guid(ent);
00082 book = qof_session_get_book(data->chart_session);
00083 copy_coll = qof_book_get_collection(book, GNC_ID_ACCOUNT);
00084 data->equity_account = (Account*)qof_collection_lookup_entity(copy_coll, guid);
00085 return;
00086 }
00087 }
00088
00089 static void
00090 chart_reference_cb(QofInstance *ent, gpointer user_data)
00091 {
00092 QofInstanceReference *reference;
00093 QofParam *ref_param;
00094 chart_data *data;
00095
00096 g_return_if_fail(user_data != NULL);
00097 data = (chart_data*)user_data;
00098 while (data->param_ref_list != NULL)
00099 {
00100 ref_param = data->param_ref_list->data;
00101 reference = qof_instance_get_reference_from(ent, ref_param);
00102 qof_session_update_reference_list(data->chart_session, reference);
00103 data->param_ref_list = data->param_ref_list->next;
00104 }
00105 }
00106
00107 static void
00108 chart_entity_cb(QofInstance *ent, gpointer user_data)
00109 {
00110 chart_data *data;
00111 Account *acc_ent, *equity_account;
00112 Transaction *trans;
00113 Split *split;
00114 gnc_numeric balance;
00115 QofBook *book;
00116 QofCollection *coll;
00117 const GncGUID *guid;
00118 time_t trans_time;
00119 GList *ref;
00120 QofInstanceReference *ent_ref;
00121
00122 g_return_if_fail(user_data != NULL);
00123 data = (chart_data*)user_data;
00124 trans_time = data->chart_time_t;
00125 data->param_ref_list = NULL;
00126 guid = qof_entity_get_guid(ent);
00127 acc_ent = (Account*)ent;
00128 ref = NULL;
00129 equity_account = data->equity_account;
00130 g_return_if_fail(equity_account != NULL);
00131 balance = xaccAccountGetBalanceAsOfDate(acc_ent, data->chart_time_t);
00132 qof_instance_copy_to_session(data->chart_session, ent);
00133 book = qof_session_get_book(data->chart_session);
00134 coll = qof_book_get_collection(book, GNC_ID_ACCOUNT);
00135 acc_ent = (Account*)qof_collection_lookup_entity(coll, guid);
00136 if (xaccAccountGetCommodity(acc_ent) == NULL)
00137 {
00138 xaccAccountSetCommodity(acc_ent, gnc_default_currency());
00139 }
00140
00141
00142 xaccAccountBeginEdit (acc_ent);
00143 xaccAccountBeginEdit (equity_account);
00144 trans = xaccMallocTransaction (book);
00145 xaccTransBeginEdit (trans);
00146 xaccTransSetCurrency (trans, xaccAccountGetCommodity (acc_ent));
00147 xaccTransSetDateSecs (trans, trans_time);
00148 xaccTransSetDateEnteredSecs (trans, trans_time);
00149 xaccTransSetDescription (trans, OPENING_BALANCE_DESC);
00150
00151 split = xaccMallocSplit (book);
00152 xaccTransAppendSplit (trans, split);
00153 xaccAccountInsertSplit (acc_ent, split);
00154 xaccSplitSetAmount (split, balance);
00155 xaccSplitSetValue (split, balance);
00156 ref = qof_class_get_referenceList(GNC_ID_SPLIT);
00157 while (ref != NULL)
00158 {
00159 ent_ref = qof_instance_get_reference_from(QOF_INSTANCE(split), ref->data);
00160 qof_session_update_reference_list(data->chart_session, ent_ref);
00161 ref = g_list_next(ref);
00162 }
00163 g_list_free(ref);
00164 balance = gnc_numeric_neg (balance);
00165
00166 split = xaccMallocSplit (book);
00167 xaccTransAppendSplit (trans, split);
00168 xaccAccountInsertSplit (equity_account, split);
00169 xaccSplitSetAmount (split, balance);
00170 xaccSplitSetValue (split, balance);
00171 xaccTransCommitEdit (trans);
00172 xaccAccountCommitEdit (equity_account);
00173 xaccAccountCommitEdit (acc_ent);
00174 ref = qof_class_get_referenceList(GNC_ID_TRANS);
00175 while (ref != NULL)
00176 {
00177 ent_ref = qof_instance_get_reference_from(QOF_INSTANCE(trans), ref->data);
00178 qof_session_update_reference_list(data->chart_session, ent_ref);
00179 ref = g_list_next(ref);
00180 }
00181 g_list_free(ref);
00182 ref = qof_class_get_referenceList(GNC_ID_SPLIT);
00183 while (ref != NULL)
00184 {
00185 ent_ref = qof_instance_get_reference_from(QOF_INSTANCE(split), ref->data);
00186 qof_session_update_reference_list(data->chart_session, ent_ref);
00187 ref = g_list_next(ref);
00188 }
00189 g_list_free(ref);
00190 }
00191
00192 void
00193 gnc_main_window_chart_export(void)
00194 {
00195 GladeXML *xml;
00196 chart_data *data;
00197
00198 xml = gnc_glade_xml_new ("chart-export.glade", "chart-export");
00199 data = g_new0(chart_data, 1);
00200 data->xml = xml;
00201 data->dialog = glade_xml_get_widget(xml, "chart-export");
00202 data->calendar = glade_xml_get_widget(xml, "chart-calendar");
00203 glade_xml_signal_autoconnect_full(xml,
00204 gnc_glade_autoconnect_full_func,
00205 data);
00206 gtk_widget_show(data->dialog);
00207 }
00208
00209 static void
00210 on_dateok_clicked (chart_data *data)
00211 {
00212 guint year, month, day;
00213 struct tm *chart_tm;
00214 gchar *filename;
00215 QofSession *current_session, *chart_session;
00216 QofBook *book;
00217 QofCollection *coll;
00218
00219 data->chart_time_t = time(NULL);
00220 chart_tm = gmtime(&data->chart_time_t);
00221
00222 year = chart_tm->tm_year + 1900;
00223 month = chart_tm->tm_mon + 1;
00224 day = chart_tm->tm_mday;
00225 gtk_calendar_get_date(GTK_CALENDAR(data->calendar),
00226 &year, &month, &day);
00227 if ((year + 1900) != chart_tm->tm_year)
00228 {
00229 chart_tm->tm_year = year - 1900;
00230 }
00231 if (month != chart_tm->tm_mon)
00232 {
00233 chart_tm->tm_mon = month;
00234 }
00235 if (day != chart_tm->tm_yday)
00236 {
00237 chart_tm->tm_mday = day;
00238 }
00239 data->chart_time_t = mktime(chart_tm);
00240 current_session = gnc_get_current_session();
00241 book = qof_session_get_book(current_session);
00242 chart_session = qof_session_new();
00243 filename = gnc_file_dialog(_("Export Chart of Accounts to QSF XML"),
00244 NULL, NULL, GNC_FILE_DIALOG_EXPORT);
00245 if (filename)
00246 {
00247 gnc_set_busy_cursor(NULL, TRUE);
00248 qof_event_suspend();
00249 qof_session_begin(chart_session, filename, TRUE, TRUE);
00250 data->chart_session = chart_session;
00251 data->equity_account = NULL;
00252 coll = qof_book_get_collection(book, GNC_ID_ACCOUNT);
00253 qof_collection_foreach(coll, chart_collection_cb, data);
00254 if (data->equity_account == NULL)
00255 {
00256 data->equity_account = xaccMallocAccount (qof_session_get_book(chart_session));
00257 xaccAccountBeginEdit (data->equity_account);
00258 xaccAccountSetName (data->equity_account, EQUITY_ACCOUNT_NAME);
00259 xaccAccountSetDescription(data->equity_account, EQUITY_ACCOUNT_NAME);
00260 xaccAccountSetType (data->equity_account, ACCT_TYPE_EQUITY);
00261 xaccAccountSetCommodity (data->equity_account, gnc_default_currency());
00262 }
00263 qof_object_foreach(GNC_ID_ACCOUNT, book, chart_entity_cb, data);
00264 data->param_ref_list = qof_class_get_referenceList(GNC_ID_TRANS);
00265 qof_object_foreach(GNC_ID_TRANS, book, chart_reference_cb, data);
00266 g_list_free(data->param_ref_list);
00267 data->param_ref_list = qof_class_get_referenceList(GNC_ID_SPLIT);
00268 qof_object_foreach(GNC_ID_SPLIT, book, chart_reference_cb, data);
00269 g_list_free(data->param_ref_list);
00270 qof_session_save(chart_session, NULL);
00271 show_session_error(qof_session_get_error(chart_session),
00272 filename, GNC_FILE_DIALOG_EXPORT);
00273 qof_event_resume();
00274 gnc_unset_busy_cursor(NULL);
00275 }
00276 qof_session_end(chart_session);
00277 gnc_set_current_session(current_session);
00278 }
00279
00280 void
00281 chart_export_response_cb (GtkDialog *dialog, gint response, gpointer user_data)
00282 {
00283 chart_data *data;
00284 data = (chart_data*)user_data;
00285 switch (response)
00286 {
00287 case GTK_RESPONSE_OK:
00288 gtk_widget_hide(data->dialog);
00289 on_dateok_clicked(data);
00290 break;
00291
00292 default:
00293
00294 break;
00295 }
00296
00297 gtk_widget_destroy(data->dialog);
00298 g_object_unref(data->xml);
00299 g_free(data);
00300 }