GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-ab-gettrans.c
1 /*
2  * gnc-ab-gettrans.c --
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, contact:
16  *
17  * Free Software Foundation Voice: +1-617-542-5942
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
19  * Boston, MA 02110-1301, USA [email protected]
20  */
21 
30 #include "config.h"
31 
32 #include <glib/gi18n.h>
33 #include <aqbanking/banking.h>
34 #include <aqbanking/jobgettransactions.h>
35 
36 #include "Account.h"
37 #include "dialog-ab-daterange.h"
38 #include "gnc-ab-gettrans.h"
39 #include "gnc-ab-kvp.h"
40 #include "gnc-ab-utils.h"
41 #include "gnc-gwen-gui.h"
42 #include "gnc-ui.h"
43 
44 /* This static indicates the debugging module that this .o belongs to. */
45 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
46 
47 static gboolean gettrans_dates(GtkWidget *parent, Account *gnc_acc, GWEN_TIME **from_date, GWEN_TIME **to_date);
48 
49 static gboolean
50 gettrans_dates(GtkWidget *parent, Account *gnc_acc,
51  GWEN_TIME **from_date, GWEN_TIME **to_date)
52 {
53  Timespec last_timespec, until_timespec;
54  time64 now = gnc_time (NULL);
55  gboolean use_last_date = TRUE;
56  gboolean use_earliest_date = TRUE;
57  gboolean use_until_now = TRUE;
58 
59  g_return_val_if_fail(from_date && to_date, FALSE);
60 
61  /* Get time of last retrieval */
62  last_timespec = gnc_ab_get_account_trans_retrieval(gnc_acc);
63  if (last_timespec.tv_sec == 0)
64  {
65  use_last_date = FALSE;
66  timespecFromTime64 (&last_timespec, now);
67  }
68  timespecFromTime64 (&until_timespec, now);
69 
70  /* Let the user choose the date range of retrieval */
71  if (!gnc_ab_enter_daterange(parent, NULL,
72  &last_timespec,
73  &use_last_date, &use_earliest_date,
74  &until_timespec, &use_until_now))
75  return FALSE;
76 
77  /* Now calculate from date */
78  if (use_earliest_date)
79  {
80  *from_date = NULL;
81  }
82  else
83  {
84  if (use_last_date)
85  last_timespec = gnc_ab_get_account_trans_retrieval(gnc_acc);
86  *from_date = GWEN_Time_fromSeconds(timespecToTime64(last_timespec));
87  }
88 
89  /* Now calculate to date */
90  if (use_until_now)
91  timespecFromTime64(&until_timespec, now);
92  *to_date = GWEN_Time_fromSeconds(timespecToTime64(until_timespec));
93 
94  return TRUE;
95 }
96 
97 void
98 gnc_ab_gettrans(GtkWidget *parent, Account *gnc_acc)
99 {
100  AB_BANKING *api;
101  gboolean online = FALSE;
102  AB_ACCOUNT *ab_acc;
103  GWEN_TIME *from_date = NULL, *to_date = NULL;
104  Timespec until_timespec;
105  AB_JOB *job = NULL;
106  AB_JOB_LIST2 *job_list = NULL;
107  GncGWENGui *gui = NULL;
108  AB_IMEXPORTER_CONTEXT *context = NULL;
109  GncABImExContextImport *ieci = NULL;
110  AB_JOB_STATUS job_status;
111 
112  g_return_if_fail(parent && gnc_acc);
113 
114  /* Get the API */
115  api = gnc_AB_BANKING_new();
116  if (!api)
117  {
118  g_warning("gnc_ab_gettrans: Couldn't get AqBanking API");
119  return;
120  }
121  if (AB_Banking_OnlineInit(api
122 #ifdef AQBANKING_VERSION_4_EXACTLY
123  , 0
124 #endif
125  ) != 0)
126  {
127  g_warning("gnc_ab_gettrans: Couldn't initialize AqBanking API");
128  goto cleanup;
129  }
130  online = TRUE;
131 
132  /* Get the AqBanking Account */
133  ab_acc = gnc_ab_get_ab_account(api, gnc_acc);
134  if (!ab_acc)
135  {
136  g_warning("gnc_ab_gettrans: No AqBanking account found");
137  gnc_error_dialog(parent, _("No valid online banking account assigned."));
138  goto cleanup;
139  }
140 
141  /* Get the start and end dates for the GetTransactions job. */
142  if (!gettrans_dates(parent, gnc_acc, &from_date, &to_date))
143  {
144  g_debug("gnc_ab_gettrans: gettrans_dates aborted");
145  goto cleanup;
146  }
147  /* Use this as a local storage for the until_time below. */
148  timespecFromTime64(&until_timespec, GWEN_Time_toTime_t(to_date));
149 
150  /* Get a GetTransactions job and enqueue it */
151  job = AB_JobGetTransactions_new(ab_acc);
152  if (!job || AB_Job_CheckAvailability(job
153 #ifndef AQBANKING_VERSION_5_PLUS
154  , 0
155 #endif
156  ))
157  {
158  g_warning("gnc_ab_gettrans: JobGetTransactions not available for this "
159  "account");
160  gnc_error_dialog(parent, _("Online action \"Get Transactions\" not available for this account."));
161  goto cleanup;
162  }
163  AB_JobGetTransactions_SetFromTime(job, from_date);
164  AB_JobGetTransactions_SetToTime(job, to_date);
165  job_list = AB_Job_List2_new();
166  AB_Job_List2_PushBack(job_list, job);
167 
168  /* Get a GUI object */
169  gui = gnc_GWEN_Gui_get(parent);
170  if (!gui)
171  {
172  g_warning("gnc_ab_gettrans: Couldn't initialize Gwenhywfar GUI");
173  goto cleanup;
174  }
175 
176  /* Create a context to store the results */
177  context = AB_ImExporterContext_new();
178 
179  /* Execute the job */
180  AB_Banking_ExecuteJobs(api, job_list, context
181 #ifndef AQBANKING_VERSION_5_PLUS
182  , 0
183 #endif
184  );
185  /* Ignore the return value of AB_Banking_ExecuteJobs(), as the job's
186  * status always describes better whether the job was actually
187  * transferred to and accepted by the bank. See also
188  * http://lists.gnucash.org/pipermail/gnucash-de/2008-September/006389.html
189  */
190  job_status = AB_Job_GetStatus(job);
191  if (job_status != AB_Job_StatusFinished
192  && job_status != AB_Job_StatusPending)
193  {
194  g_warning("gnc_ab_gettrans: Error on executing job");
195  gnc_error_dialog(parent, _("Error on executing job.\n\nStatus: %s - %s")
196  , AB_Job_Status2Char(job_status)
197  , AB_Job_GetResultText(job));
198  goto cleanup;
199  }
200 
201  /* Import the results */
202  ieci = gnc_ab_import_context(context, AWAIT_TRANSACTIONS, FALSE, NULL,
203  parent);
204  if (!(gnc_ab_ieci_get_found(ieci) & FOUND_TRANSACTIONS))
205  {
206  /* No transaction found */
207  GtkWidget *dialog = gtk_message_dialog_new(
208  GTK_WINDOW(parent),
209  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
210  GTK_MESSAGE_INFO,
211  GTK_BUTTONS_OK,
212  "%s",
213  _("The Online Banking import returned no transactions "
214  "for the selected time period."));
215  gtk_dialog_run(GTK_DIALOG(dialog));
216  gtk_widget_destroy(dialog);
217  }
218 
219  /* Store the date of this retrieval */
220  gnc_ab_set_account_trans_retrieval(gnc_acc, until_timespec);
221 
222 cleanup:
223  if (ieci)
224  g_free(ieci);
225  if (context)
226  AB_ImExporterContext_free(context);
227  if (gui)
229  if (job_list)
230  AB_Job_List2_free(job_list);
231  if (job)
232  AB_Job_free(job);
233  if (to_date)
234  GWEN_Time_free(to_date);
235  if (from_date)
236  GWEN_Time_free(from_date);
237  if (online)
238 #ifdef AQBANKING_VERSION_4_EXACTLY
239  AB_Banking_OnlineFini(api, 0);
240 #else
241  AB_Banking_OnlineFini(api);
242 #endif
243  gnc_AB_BANKING_fini(api);
244 }
Timespec gnc_ab_get_account_trans_retrieval(const Account *a)
Definition: gnc-ab-kvp.c:99
time64 timespecToTime64(Timespec ts)
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
GncGWENGui * gnc_GWEN_Gui_get(GtkWidget *parent)
Definition: gnc-gwen-gui.c:311
Account handling public routines.
void gnc_GWEN_Gui_release(GncGWENGui *gui)
Definition: gnc-gwen-gui.c:346
void gnc_ab_set_account_trans_retrieval(Account *a, Timespec time)
Definition: gnc-ab-kvp.c:109
AB_BANKING * gnc_AB_BANKING_new(void)
Definition: gnc-ab-utils.c:137
gint gnc_AB_BANKING_fini(AB_BANKING *api)
Definition: gnc-ab-utils.c:237
AB_ACCOUNT * gnc_ab_get_ab_account(const AB_BANKING *api, Account *gnc_acc)
Definition: gnc-ab-utils.c:264
GncABImExContextImport * gnc_ab_import_context(AB_IMEXPORTER_CONTEXT *context, guint awaiting, gboolean execute_txns, AB_BANKING *api, GtkWidget *parent)
gboolean gnc_ab_enter_daterange(GtkWidget *parent, const char *heading, Timespec *from_date, gboolean *last_retv_date, gboolean *first_possible_date, Timespec *to_date, gboolean *to_now)
time64 gnc_time(time64 *tbuf)
get the current local time
gint64 time64
Definition: gnc-date.h:83
AqBanking KVP handling.
GUI callbacks for AqBanking.
guint gnc_ab_ieci_get_found(GncABImExContextImport *ieci)
void gnc_ab_gettrans(GtkWidget *parent, Account *gnc_acc)
AqBanking utility functions.
const gchar * QofLogModule
Definition: qofid.h:89
void timespecFromTime64(Timespec *ts, time64 t)