GnuCash  5.6-150-g038405b370+
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 gnu@gnu.org
20  */
21 
30 #include <config.h>
31 
32 #include "gnc-ab-utils.h"
33 
34 #include <glib/gi18n.h>
35 #include <aqbanking/banking.h>
36 # include <aqbanking/types/transaction.h>
37 #include "Account.h"
38 #include "dialog-ab-daterange.h"
39 #include "gnc-ab-gettrans.h"
40 #include "gnc-ab-kvp.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  time64 last, until;
54  gboolean use_last_date = TRUE;
55  gboolean use_earliest_date = TRUE;
56  gboolean use_until_now = TRUE;
57 
58  g_return_val_if_fail(from_date && to_date, FALSE);
59 
60  /* Get time of last retrieval */
61  last = gnc_ab_get_account_trans_retrieval(gnc_acc);
62  if (last == 0)
63  {
64  use_last_date = FALSE;
65  last = gnc_time (NULL);
66  }
67  until = gnc_time (NULL);
68 
69  /* Let the user choose the date range of retrieval */
70  if (!gnc_ab_enter_daterange(parent, NULL,
71  &last,
72  &use_last_date, &use_earliest_date,
73  &until, &use_until_now))
74  return FALSE;
75 
76  /* Now calculate from date */
77  if (use_earliest_date)
78  {
79  *from_date = NULL;
80  }
81  else
82  {
83  if (use_last_date)
84  last = gnc_ab_get_account_trans_retrieval(gnc_acc);
85  *from_date = GWEN_Time_fromSeconds(last);
86  }
87 
88  /* Now calculate to date */
89  if (use_until_now)
90  until = gnc_time (NULL);
91  *to_date = GWEN_Time_fromSeconds(until);
92 
93  return TRUE;
94 }
95 
96 void
97 gnc_ab_gettrans(GtkWidget *parent, Account *gnc_acc)
98 {
99  AB_BANKING *api;
100  GNC_AB_ACCOUNT_SPEC *ab_acc;
101  GWEN_TIME *from_date = NULL, *to_date = NULL;
102  time64 until;
103  GNC_AB_JOB *job = NULL;
104  GNC_AB_JOB_LIST2 *job_list = NULL;
105  GncGWENGui *gui = NULL;
106  AB_IMEXPORTER_CONTEXT *context = NULL;
107  GncABImExContextImport *ieci = NULL;
108  GNC_AB_JOB_STATUS job_status;
109 
110  g_return_if_fail(parent && gnc_acc);
111 
112  /* Get the API */
113  api = gnc_AB_BANKING_new();
114  if (!api)
115  {
116  g_warning("gnc_ab_gettrans: Couldn't get AqBanking API");
117  return;
118  }
119  /* Get the AqBanking Account */
120  ab_acc = gnc_ab_get_ab_account(api, gnc_acc);
121  if (!ab_acc)
122  {
123  g_warning("gnc_ab_gettrans: No AqBanking account found");
124  gnc_error_dialog (GTK_WINDOW (parent), _("No valid online banking account assigned."));
125  goto cleanup;
126  }
127 
128  /* Get the start and end dates for the GetTransactions job. */
129  if (!gettrans_dates(parent, gnc_acc, &from_date, &to_date))
130  {
131  DEBUG("gnc_ab_gettrans: gettrans_dates aborted");
132  goto cleanup;
133  }
134  /* Use this as a local storage for the until_time below. */
135  until = GWEN_Time_toTime_t(to_date);
136 
137  /* Get a GetTransactions job and enqueue it */
138  if (!AB_AccountSpec_GetTransactionLimitsForCommand(ab_acc, AB_Transaction_CommandGetTransactions))
139  {
140  g_warning("gnc_ab_gettrans: JobGetTransactions not available for this "
141  "account");
142  gnc_error_dialog (GTK_WINDOW (parent), _("Online action \"Get Transactions\" not available for this account."));
143  goto cleanup;
144  }
145  job = AB_Transaction_new();
146  AB_Transaction_SetCommand(job, AB_Transaction_CommandGetTransactions);
147  AB_Transaction_SetUniqueAccountId(job, AB_AccountSpec_GetUniqueId(ab_acc));
148 
149  if (from_date) /* TODO: this should be simplified */
150  {
151  GWEN_DATE *dt;
152 
153  dt=GWEN_Date_fromLocalTime(GWEN_Time_toTime_t(from_date));
154  AB_Transaction_SetFirstDate(job, dt);
155  GWEN_Date_free(dt);
156  }
157 
158  if (to_date)
159  {
160  GWEN_DATE *dt;
161 
162  dt=GWEN_Date_fromLocalTime(GWEN_Time_toTime_t(to_date));
163  AB_Transaction_SetLastDate(job, dt);
164  GWEN_Date_free(dt);
165  }
166 
167  job_list = AB_Transaction_List2_new();
168  AB_Transaction_List2_PushBack(job_list, job);
169  /* Get a GUI object */
170  gui = gnc_GWEN_Gui_get(parent);
171  if (!gui)
172  {
173  g_warning("gnc_ab_gettrans: Couldn't initialize Gwenhywfar GUI");
174  goto cleanup;
175  }
176 
177  /* Create a context to store the results */
178  context = AB_ImExporterContext_new();
179 
180  /* Execute the job */
181  AB_Banking_SendCommands(api, job_list, context);
182 
183  /* Ignore the return value of AB_Banking_ExecuteJobs(), as the job's
184  * status always describes better whether the job was actually
185  * transferred to and accepted by the bank. See also
186  * https://lists.gnucash.org/pipermail/gnucash-de/2008-September/006389.html
187  */
188  job_status = AB_Transaction_GetStatus(job);
189  if (job_status != AB_Transaction_StatusAccepted
190  && job_status != AB_Transaction_StatusPending)
191  {
192  g_warning("gnc_ab_gettrans: Error on executing job");
193  gnc_error_dialog (GTK_WINDOW (parent),
194  _("Error on executing job.\n\nStatus: %s (%d)"),
195  AB_Transaction_Status_toString(job_status),
196  job_status);
197  goto cleanup;
198  }
199 
200  /* Import the results */
201  ieci = gnc_ab_import_context(context, AWAIT_TRANSACTIONS, FALSE, NULL,
202  parent);
203  if (!(gnc_ab_ieci_get_found(ieci) & FOUND_TRANSACTIONS))
204  {
205  /* No transaction found */
206  GtkWidget *dialog = gtk_message_dialog_new(
207  GTK_WINDOW(parent),
208  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
209  GTK_MESSAGE_INFO,
210  GTK_BUTTONS_OK,
211  "%s",
212  _("The Online Banking import returned no transactions "
213  "for the selected time period."));
214  gtk_dialog_run(GTK_DIALOG(dialog));
215  gtk_widget_destroy(dialog);
216  }
217 
218  /* Store the date of this retrieval */
219  gnc_ab_set_account_trans_retrieval(gnc_acc, until);
220 
221 cleanup:
222  if (ieci)
223  g_free(ieci);
224  if (context)
225  AB_ImExporterContext_free(context);
226  if (gui)
228  if (job_list)
229  AB_Transaction_List2_free(job_list);
230  if (job)
231  AB_Transaction_free(job);
232  if (to_date)
233  GWEN_Time_free(to_date);
234  if (from_date)
235  GWEN_Time_free(from_date);
236  gnc_AB_BANKING_fini(api);
237 }
time64 gnc_ab_get_account_trans_retrieval(const Account *a)
Return the time of last online transaction retrieval for Account a.
Definition: gnc-ab-kvp.c:99
void gnc_ab_set_account_trans_retrieval(Account *a, time64 time)
Set the time of last online transaction retrieval for Account a.
Definition: gnc-ab-kvp.c:109
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
STRUCTS.
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
GncGWENGui * gnc_GWEN_Gui_get(GtkWidget *parent)
When called for the first time, create a unique GncGWENGui object featuring a GWEN_GUI with all neces...
Definition: gnc-gwen-gui.c:245
Account handling public routines.
void gnc_GWEN_Gui_release(GncGWENGui *gui)
Currently a no-op.
Definition: gnc-gwen-gui.c:280
AB_BANKING * gnc_AB_BANKING_new(void)
If there is a cached AB_BANKING object, return it initialized.
Definition: gnc-ab-utils.c:163
gint gnc_AB_BANKING_fini(AB_BANKING *api)
Finish the AB_BANKING api.
Definition: gnc-ab-utils.c:226
gboolean gnc_ab_enter_daterange(GtkWidget *parent, const char *heading, time64 *from_date, gboolean *last_retv_date, gboolean *first_possible_date, time64 *to_date, gboolean *to_now)
Show a dialog to pick a time frame using a sensible set of default options.
GncABImExContextImport * gnc_ab_import_context(AB_IMEXPORTER_CONTEXT *context, guint awaiting, gboolean execute_txns, AB_BANKING *api, GtkWidget *parent)
Import balances and transactions found in a AB_IMEXPORTER_CONTEXT into GnuCash.
GNC_AB_ACCOUNT_SPEC * gnc_ab_get_ab_account(const AB_BANKING *api, Account *gnc_acc)
Get the corresponding AqBanking account to the GnuCash account gnc_acc.
Definition: gnc-ab-utils.c:249
time64 gnc_time(time64 *tbuf)
get the current time
Definition: gnc-date.cpp:261
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
AqBanking KVP handling.
GUI callbacks for AqBanking.
guint gnc_ab_ieci_get_found(GncABImExContextImport *ieci)
Extract awaiting from data.
void gnc_ab_gettrans(GtkWidget *parent, Account *gnc_acc)
Execute a GetTransactions job.
AqBanking utility functions.