GnuCash  5.6-150-g038405b370+
gnc-ab-getbalance.c
1 /*
2  * gnc-ab-getbalance.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 
38 #include "gnc-ab-getbalance.h"
39 #include "gnc-ab-kvp.h"
40 #include "gnc-gwen-gui.h"
41 #include "gnc-ui.h"
42 
43 /* This static indicates the debugging module that this .o belongs to. */
44 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
45 
46 void
47 gnc_ab_getbalance(GtkWidget *parent, Account *gnc_acc)
48 {
49  AB_BANKING *api;
50  GNC_AB_ACCOUNT_SPEC *ab_acc;
51  GNC_AB_JOB *job = NULL;
52  GNC_AB_JOB_LIST2 *job_list = NULL;
53  GncGWENGui *gui = NULL;
54  AB_IMEXPORTER_CONTEXT *context = NULL;
55  GncABImExContextImport *ieci = NULL;
56  GNC_AB_JOB_STATUS job_status;
57 
58  g_return_if_fail(parent && gnc_acc);
59 
60  /* Get the API */
61  api = gnc_AB_BANKING_new();
62  if (!api)
63  {
64  g_warning("gnc_ab_gettrans: Couldn't get AqBanking API");
65  return;
66  }
67 
68  /* Get the AqBanking Account */
69  ab_acc = gnc_ab_get_ab_account(api, gnc_acc);
70  if (!ab_acc)
71  {
72  g_warning("gnc_ab_getbalance: No AqBanking account found");
73  gnc_error_dialog (GTK_WINDOW (parent), _("No valid online banking account assigned."));
74  goto cleanup;
75  }
76 
77  /* Get a GetBalance job and enqueue it */
78  if (!AB_AccountSpec_GetTransactionLimitsForCommand(ab_acc, AB_Transaction_CommandGetBalance))
79  {
80  g_warning("gnc_ab_getbalance: JobGetBalance not available for this "
81  "account");
82  gnc_error_dialog (GTK_WINDOW (parent), _("Online action \"Get Balance\" not available for this account."));
83  goto cleanup;
84  }
85  job = AB_Transaction_new();
86  AB_Transaction_SetCommand(job, AB_Transaction_CommandGetBalance);
87  AB_Transaction_SetUniqueAccountId(job, AB_AccountSpec_GetUniqueId(ab_acc));
88 
89  job_list = AB_Transaction_List2_new();
90  AB_Transaction_List2_PushBack(job_list, job);
91  /* Get a GUI object */
92  gui = gnc_GWEN_Gui_get(parent);
93  if (!gui)
94  {
95  g_warning("gnc_ab_getbalance: Couldn't initialize Gwenhywfar GUI");
96  goto cleanup;
97  }
98 
99  /* Create a context to store the results */
100  context = AB_ImExporterContext_new();
101 
102  /* Execute the job */
103  AB_Banking_SendCommands(api, job_list, context);
104  /* Ignore the return value of AB_Banking_ExecuteJobs(), as the job's
105  * status always describes better whether the job was actually
106  * transferred to and accepted by the bank. See also
107  * https://lists.gnucash.org/pipermail/gnucash-de/2008-September/006389.html
108  */
109  job_status = AB_Transaction_GetStatus(job);
110  if (job_status != AB_Transaction_StatusEnqueued
111  && job_status != AB_Transaction_StatusPending
112  && job_status != AB_Transaction_StatusAccepted)
113  {
114  g_warning("gnc_ab_getbalance: Error on executing job: %d", job_status);
115  gnc_error_dialog (GTK_WINDOW (parent),
116  _("Error on executing job.\n\nStatus: %s"),
117  AB_Transaction_Status_toString(job_status));
118  goto cleanup;
119  }
120 
121  /* Import the results */
122  ieci = gnc_ab_import_context(context, AWAIT_BALANCES, FALSE, NULL, parent);
123 
124 cleanup:
125  if (ieci)
126  g_free(ieci);
127  if (context)
128  AB_ImExporterContext_free(context);
129  if (gui)
131  if (job_list)
132  AB_Transaction_List2_free(job_list);
133  if (job)
134  AB_Transaction_free(job);
135  gnc_AB_BANKING_fini(api);
136 }
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
STRUCTS.
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
AqBanking getbalance functions.
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
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
void gnc_ab_getbalance(GtkWidget *parent, Account *gnc_acc)
Execute a GetBalance job, show the resulting balance and offer to reconcile the GnuCash account...
AqBanking KVP handling.
GUI callbacks for AqBanking.
AqBanking utility functions.