|
GnuCash 2.4.99
|
00001 /* 00002 * gnc-ab-transfer.c -- 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License as 00006 * published by the Free Software Foundation; either version 2 of 00007 * the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, contact: 00016 * 00017 * Free Software Foundation Voice: +1-617-542-5942 00018 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 00019 * Boston, MA 02110-1301, USA gnu@gnu.org 00020 */ 00021 00032 #include "config.h" 00033 00034 #include <glib/gi18n.h> 00035 #include <gtk/gtk.h> 00036 #include <aqbanking/banking.h> 00037 00038 #include "Transaction.h" 00039 #include "dialog-transfer.h" 00040 #include "gnc-ab-transfer.h" 00041 #include "gnc-ab-kvp.h" 00042 #include "gnc-ab-utils.h" 00043 #include "gnc-ab-trans-templ.h" 00044 #include "gnc-gwen-gui.h" 00045 #include "gnc-ui.h" 00046 00047 /* This static indicates the debugging module that this .o belongs to. */ 00048 static QofLogModule log_module = G_LOG_DOMAIN; 00049 00050 static void save_templates(GtkWidget *parent, Account *gnc_acc, GList *templates, 00051 gboolean dont_ask); 00052 static void txn_created_cb(Transaction *trans, gpointer user_data); 00053 00054 static void 00055 save_templates(GtkWidget *parent, Account *gnc_acc, GList *templates, 00056 gboolean dont_ask) 00057 { 00058 g_return_if_fail(gnc_acc); 00059 if (dont_ask || gnc_verify_dialog( 00060 parent, FALSE, "%s", 00061 _("You have changed the list of online transfer templates, " 00062 "but you cancelled the transfer dialog. " 00063 "Do you nevertheless want to store the changes?"))) 00064 { 00065 GList *kvp_list = gnc_ab_trans_templ_list_to_kvp_list(templates); 00066 gnc_ab_set_book_template_list(gnc_account_get_book(gnc_acc), kvp_list); 00067 } 00068 } 00069 00070 static void 00071 txn_created_cb(Transaction *trans, gpointer user_data) 00072 { 00073 Transaction **trans_loc = user_data; 00074 00075 if (!trans) return; 00076 g_return_if_fail(trans_loc); 00077 *trans_loc = trans; 00078 } 00079 00080 void 00081 gnc_ab_maketrans(GtkWidget *parent, Account *gnc_acc, 00082 GncABTransType trans_type) 00083 { 00084 AB_BANKING *api; 00085 gboolean online = FALSE; 00086 AB_ACCOUNT *ab_acc; 00087 GncABTransDialog *dialog = NULL; 00088 GList *templates = NULL; 00089 GncABTransDialog *td = NULL; 00090 gboolean successful = FALSE; 00091 gboolean aborted = FALSE; 00092 00093 g_return_if_fail(parent && gnc_acc); 00094 00095 /* Get the API */ 00096 api = gnc_AB_BANKING_new(); 00097 if (!api) 00098 { 00099 g_warning("gnc_ab_maketrans: Couldn't get AqBanking API"); 00100 return; 00101 } 00102 if (AB_Banking_OnlineInit(api 00103 #ifdef AQBANKING_VERSION_4_EXACTLY 00104 , 0 00105 #endif 00106 ) != 0) 00107 { 00108 g_warning("gnc_ab_maketrans: Couldn't initialize AqBanking API"); 00109 goto cleanup; 00110 } 00111 online = TRUE; 00112 00113 /* Get the AqBanking Account */ 00114 ab_acc = gnc_ab_get_ab_account(api, gnc_acc); 00115 if (!ab_acc) 00116 { 00117 g_warning("gnc_ab_gettrans: No AqBanking account found"); 00118 gnc_error_dialog(parent, _("No valid online banking account assigned.")); 00119 goto cleanup; 00120 } 00121 00122 /* Get list of template transactions */ 00123 templates = gnc_ab_trans_templ_list_new_from_kvp_list( 00124 gnc_ab_get_book_template_list(gnc_account_get_book(gnc_acc))); 00125 00126 /* Create new ABTransDialog */ 00127 td = gnc_ab_trans_dialog_new(parent, ab_acc, 00128 xaccAccountGetCommoditySCU(gnc_acc), 00129 trans_type, templates); 00130 templates = NULL; 00131 00132 /* Repeat until AqBanking action was successful or user pressed cancel */ 00133 do 00134 { 00135 GncGWENGui *gui = NULL; 00136 gint result; 00137 gboolean changed; 00138 const AB_TRANSACTION *ab_trans; 00139 AB_JOB *job = NULL; 00140 AB_JOB_LIST2 *job_list = NULL; 00141 XferDialog *xfer_dialog = NULL; 00142 gnc_numeric amount; 00143 gchar *description; 00144 gchar *memo; 00145 Transaction *gnc_trans = NULL; 00146 AB_IMEXPORTER_CONTEXT *context = NULL; 00147 AB_JOB_STATUS job_status; 00148 GncABImExContextImport *ieci = NULL; 00149 00150 /* Get a GUI object */ 00151 gui = gnc_GWEN_Gui_get(parent); 00152 if (!gui) 00153 { 00154 g_warning("gnc_ab_maketrans: Couldn't initialize Gwenhywfar GUI"); 00155 aborted = TRUE; 00156 goto repeat; 00157 } 00158 00159 /* Let the user enter the values */ 00160 result = gnc_ab_trans_dialog_run_until_ok(td); 00161 00162 if (result != GNC_RESPONSE_NOW && result != GNC_RESPONSE_LATER) 00163 { 00164 aborted = TRUE; 00165 goto repeat; 00166 } 00167 00168 /* Save the templates */ 00169 templates = gnc_ab_trans_dialog_get_templ(td, &changed); 00170 if (changed) 00171 save_templates(parent, gnc_acc, templates, 00172 (result == GNC_RESPONSE_NOW)); 00173 g_list_free(templates); 00174 templates = NULL; 00175 00176 /* Get a job and enqueue it */ 00177 ab_trans = gnc_ab_trans_dialog_get_ab_trans(td); 00178 job = gnc_ab_trans_dialog_get_job(td); 00179 if (!job || AB_Job_CheckAvailability(job 00180 #ifndef AQBANKING_VERSION_5_PLUS 00181 , 0 00182 #endif 00183 )) 00184 { 00185 if (!gnc_verify_dialog( 00186 parent, FALSE, "%s", 00187 _("The backend found an error during the preparation " 00188 "of the job. It is not possible to execute this job. \n" 00189 "\n" 00190 "Most probable the bank does not support your chosen " 00191 "job or your Online Banking account does not have the permission " 00192 "to execute this job. More error messages might be " 00193 "visible on your console log.\n" 00194 "\n" 00195 "Do you want to enter the job again?"))) 00196 aborted = TRUE; 00197 goto repeat; 00198 } 00199 job_list = AB_Job_List2_new(); 00200 AB_Job_List2_PushBack(job_list, job); 00201 00202 /* Setup a Transfer Dialog for the GnuCash transaction */ 00203 xfer_dialog = gnc_xfer_dialog(gnc_ab_trans_dialog_get_parent(td), 00204 gnc_acc); 00205 switch (trans_type) 00206 { 00207 case SINGLE_DEBITNOTE: 00208 gnc_xfer_dialog_set_title( 00209 xfer_dialog, _("Online Banking Direct Debit Note")); 00210 gnc_xfer_dialog_lock_to_account_tree(xfer_dialog); 00211 break; 00212 case SINGLE_INTERNAL_TRANSFER: 00213 gnc_xfer_dialog_set_title( 00214 xfer_dialog, _("Online Banking Bank-Internal Transfer")); 00215 gnc_xfer_dialog_lock_from_account_tree(xfer_dialog); 00216 break; 00217 case SINGLE_TRANSFER: 00218 default: 00219 gnc_xfer_dialog_set_title( 00220 xfer_dialog, _("Online Banking Transaction")); 00221 gnc_xfer_dialog_lock_from_account_tree(xfer_dialog); 00222 } 00223 gnc_xfer_dialog_set_to_show_button_active(xfer_dialog, TRUE); 00224 00225 amount = double_to_gnc_numeric( 00226 AB_Value_GetValueAsDouble(AB_Transaction_GetValue(ab_trans)), 00227 xaccAccountGetCommoditySCU(gnc_acc), 00228 GNC_HOW_RND_ROUND_HALF_UP); 00229 gnc_xfer_dialog_set_amount(xfer_dialog, amount); 00230 gnc_xfer_dialog_set_amount_sensitive(xfer_dialog, FALSE); 00231 00232 description = gnc_ab_description_to_gnc(ab_trans); 00233 gnc_xfer_dialog_set_description(xfer_dialog, description); 00234 g_free(description); 00235 00236 memo = gnc_ab_memo_to_gnc(ab_trans); 00237 gnc_xfer_dialog_set_memo(xfer_dialog, memo); 00238 g_free(memo); 00239 00240 gnc_xfer_dialog_set_txn_cb(xfer_dialog, txn_created_cb, &gnc_trans); 00241 00242 /* And run it */ 00243 successful = gnc_xfer_dialog_run_until_done(xfer_dialog); 00244 00245 /* On cancel, go back to the AB transaction dialog */ 00246 if (!successful || !gnc_trans) 00247 { 00248 successful = FALSE; 00249 goto repeat; 00250 } 00251 00252 if (result == GNC_RESPONSE_NOW) 00253 { 00254 /* Create a context to store possible results */ 00255 context = AB_ImExporterContext_new(); 00256 00257 gui = gnc_GWEN_Gui_get(parent); 00258 if (!gui) 00259 { 00260 g_warning("gnc_ab_maketrans: Couldn't initialize Gwenhywfar GUI"); 00261 aborted = TRUE; 00262 goto repeat; 00263 } 00264 00265 /* Finally, execute the job */ 00266 AB_Banking_ExecuteJobs(api, job_list, context 00267 #ifndef AQBANKING_VERSION_5_PLUS 00268 , 0 00269 #endif 00270 ); 00271 00272 /* Ignore the return value of AB_Banking_ExecuteJobs(), as the job's 00273 * status always describes better whether the job was actually 00274 * transferred to and accepted by the bank. See also 00275 * http://lists.gnucash.org/pipermail/gnucash-de/2008-September/006389.html 00276 */ 00277 job_status = AB_Job_GetStatus(job); 00278 if (job_status != AB_Job_StatusFinished 00279 && job_status != AB_Job_StatusPending) 00280 { 00281 successful = FALSE; 00282 if (!gnc_verify_dialog( 00283 parent, FALSE, "%s", 00284 _("An error occurred while executing the job. Please check " 00285 "the log window for the exact error message.\n" 00286 "\n" 00287 "Do you want to enter the job again?"))) 00288 { 00289 aborted = TRUE; 00290 } 00291 } 00292 else 00293 { 00294 successful = TRUE; 00295 } 00296 00297 if (successful) 00298 { 00299 /* Import the results, awaiting nothing */ 00300 ieci = gnc_ab_import_context(context, 0, FALSE, NULL, parent); 00301 } 00302 } 00303 /* Simply ignore any other case */ 00304 00305 repeat: 00306 /* Clean up */ 00307 if (gnc_trans && !successful) 00308 { 00309 xaccTransBeginEdit(gnc_trans); 00310 xaccTransDestroy(gnc_trans); 00311 xaccTransCommitEdit(gnc_trans); 00312 gnc_trans = NULL; 00313 } 00314 if (ieci) 00315 g_free(ieci); 00316 if (context) 00317 AB_ImExporterContext_free(context); 00318 if (job_list) 00319 { 00320 AB_Job_List2_free(job_list); 00321 job_list = NULL; 00322 } 00323 if (job) 00324 { 00325 AB_Job_free(job); 00326 job = NULL; 00327 } 00328 if (gui) 00329 { 00330 gnc_GWEN_Gui_release(gui); 00331 gui = NULL; 00332 } 00333 00334 } 00335 while (!successful && !aborted); 00336 00337 cleanup: 00338 if (td) 00339 gnc_ab_trans_dialog_free(td); 00340 if (online) 00341 #ifdef AQBANKING_VERSION_4_EXACTLY 00342 AB_Banking_OnlineFini(api, 0); 00343 #else 00344 AB_Banking_OnlineFini(api); 00345 #endif 00346 gnc_AB_BANKING_fini(api); 00347 }
1.7.4