gchar * gnc_ab_memo_to_gnc(const AB_TRANSACTION *ab_trans) { const gchar *ab_remote_accountnumber = AB_Transaction_GetRemoteAccountNumber(ab_trans); const gchar *ab_remote_bankcode = AB_Transaction_GetRemoteBankCode(ab_trans); gchar *ab_other_accountid; gchar *ab_other_bankcode; gboolean have_accountid; gboolean have_bankcode; gchar *retval; // For SEPA transactions, we need to ask for something different here if (!ab_remote_accountnumber) ab_remote_accountnumber = AB_Transaction_GetRemoteIban(ab_trans); if (!ab_remote_bankcode) ab_remote_bankcode = AB_Transaction_GetRemoteBic(ab_trans); ab_other_accountid = g_strdup(ab_remote_accountnumber ? ab_remote_accountnumber : ""); ab_other_bankcode = g_strdup(ab_remote_bankcode ? ab_remote_bankcode : ""); /* Ensure string is in utf8 */ gnc_utf8_strip_invalid(ab_other_accountid); gnc_utf8_strip_invalid(ab_other_bankcode); /* and -then- trim it */ g_strstrip(ab_other_accountid); g_strstrip(ab_other_bankcode); have_accountid = ab_other_accountid && *ab_other_accountid; have_bankcode = ab_other_bankcode && *ab_other_bankcode; if ( have_accountid || have_bankcode ) { retval = g_strdup_printf("%s %s %s %s", have_accountid ? _("Account") : "", have_accountid ? ab_other_accountid : "", have_bankcode ? _("Bank") : "", have_bankcode ? ab_other_bankcode : "" ); g_strstrip(retval); } else { retval = g_strdup(""); } g_free(ab_other_accountid); g_free(ab_other_bankcode); return retval; }
static PyObject *aqbanking_Account_transactions(aqbanking_Account* self, PyObject *args, PyObject *kwds) { int rv; double tmpDateTime = 0; const char *bank_code; const char *account_no; #if PY_VERSION_HEX >= 0x03030000 bank_code = PyUnicode_AsUTF8(self->bank_code); account_no = PyUnicode_AsUTF8(self->no); #else PyObject *s = _PyUnicode_AsDefaultEncodedString(self->bank_code, NULL); bank_code = PyBytes_AS_STRING(s); s = _PyUnicode_AsDefaultEncodedString(self->no, NULL); account_no = PyBytes_AS_STRING(s); #endif GWEN_TIME *gwTime; const char *dateFrom=NULL, *dateTo=NULL; static char *kwlist[] = {"dateFrom", "dateTo", NULL}; if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ss", kwlist, &dateFrom, &dateTo)) { return NULL; } AB_ACCOUNT *a; AB_JOB *job = 0; AB_JOB_LIST2 *jl = 0; AB_IMEXPORTER_CONTEXT *ctx = 0; AB_IMEXPORTER_ACCOUNTINFO *ai; /*aqbanking_Transaction *trans = NULL;*/ PyObject *transList = PyList_New(0); // Valid data set? if (self->no == NULL) { PyErr_SetString(PyExc_AttributeError, "no"); } if (self->bank_code == NULL) { PyErr_SetString(PyExc_AttributeError, "bank_code"); } // Initialize aqbanking. rv = AB_create(self); if (rv > 0) { Py_DECREF(transList); return NULL; } // Let us find the account! a = AB_Banking_GetAccountByCodeAndNumber(self->ab, bank_code, account_no); if (!a) { PyErr_SetString(AccountNotFound, "Could not find the given account! "); Py_DECREF(transList); return NULL; } // Create job and execute it. job = AB_JobGetTransactions_new(a); if (dateFrom != NULL) { gwTime = GWEN_Time_fromString(dateFrom, "YYYYMMDD"); AB_JobGetTransactions_SetFromTime(job, gwTime); } if (dateTo != NULL) { gwTime = GWEN_Time_fromString(dateTo, "YYYYMMDD"); AB_JobGetTransactions_SetToTime(job, gwTime); } // Check for availability rv = AB_Job_CheckAvailability(job); if (rv) { PyErr_SetString(ExecutionFailed, "Transaction retrieval is not supported!"); Py_DECREF(transList); return NULL; } jl = AB_Job_List2_new(); AB_Job_List2_PushBack(jl, job); ctx = AB_ImExporterContext_new(); rv = AB_Banking_ExecuteJobs(self->ab, jl, ctx); if (rv) { PyErr_SetString(ExecutionFailed, "Could not retrieve transactions!"); Py_DECREF(transList); return NULL; } // With success. No process the result. ai = AB_ImExporterContext_GetFirstAccountInfo (ctx); while(ai) { const AB_TRANSACTION *t; t = AB_ImExporterAccountInfo_GetFirstTransaction(ai); while(t) { const AB_VALUE *v; AB_TRANSACTION_STATUS state; v=AB_Transaction_GetValue(t); if (v) { const GWEN_STRINGLIST *sl; const GWEN_TIME *tdtime; const char *purpose; const char *remoteName; aqbanking_Transaction *trans = (aqbanking_Transaction*) PyObject_CallObject((PyObject *) &aqbanking_TransactionType, NULL); /* The purpose (memo field) might contain multiple lines. * Therefore AqBanking stores the purpose in a string list * of which the first entry is used in this tutorial */ sl = AB_Transaction_GetPurpose(t); if (sl) { purpose = GWEN_StringList_FirstString(sl); if (purpose == NULL) { purpose = ""; } } else { purpose = ""; } #ifdef DEBUGSTDERR fprintf(stderr, "[%-10d]: [%-10s/%-10s][%-10s/%-10s] %-32s (%.2f %s)\n", AB_Transaction_GetUniqueId(t), AB_Transaction_GetRemoteIban(t), AB_Transaction_GetRemoteBic(t), AB_Transaction_GetRemoteAccountNumber(t), AB_Transaction_GetRemoteBankCode(t), purpose, AB_Value_GetValueAsDouble(v), AB_Value_GetCurrency(v) ); #endif tdtime = AB_Transaction_GetDate(t); tmpDateTime = PyLong_AsDouble(PyLong_FromSize_t(GWEN_Time_Seconds(tdtime))); trans->date = PyDate_FromTimestamp(Py_BuildValue("(O)", PyFloat_FromDouble(tmpDateTime))); tdtime = AB_Transaction_GetValutaDate(t); tmpDateTime = PyLong_AsDouble(PyLong_FromSize_t(GWEN_Time_Seconds(tdtime))); trans->valutaDate = PyDate_FromTimestamp(Py_BuildValue("(O)", PyFloat_FromDouble(tmpDateTime))); trans->purpose = PyUnicode_FromString(purpose); // Local user if (AB_Transaction_GetLocalAccountNumber(t) == NULL) { trans->localAccount = Py_None; Py_INCREF(Py_None); } else { trans->localAccount = PyUnicode_FromString(AB_Transaction_GetLocalAccountNumber(t)); } if (AB_Transaction_GetLocalBankCode(t) == NULL) { trans->localBank = Py_None; Py_INCREF(Py_None); } else { trans->localBank = PyUnicode_FromString(AB_Transaction_GetLocalBankCode(t)); } if (AB_Transaction_GetLocalIban(t) == NULL) { trans->localIban = Py_None; Py_INCREF(Py_None); } else { trans->localIban = PyUnicode_FromString(AB_Transaction_GetLocalIban(t)); } if (AB_Transaction_GetLocalBic(t) == NULL) { trans->localBic = Py_None; Py_INCREF(Py_None); } else { trans->localBic = PyUnicode_FromString(AB_Transaction_GetLocalBic(t)); } if (AB_Transaction_GetLocalName(t) == NULL) { trans->localName = Py_None; Py_INCREF(Py_None); } else { trans->localName = PyUnicode_FromString(AB_Transaction_GetLocalName(t)); } // Remote user if (AB_Transaction_GetRemoteAccountNumber(t) == NULL) { trans->remoteAccount = Py_None; Py_INCREF(Py_None); } else { trans->remoteAccount = PyUnicode_FromString(AB_Transaction_GetRemoteAccountNumber(t)); } if (AB_Transaction_GetRemoteBankCode(t) == NULL) { trans->remoteBank = Py_None; Py_INCREF(Py_None); } else { trans->remoteBank = PyUnicode_FromString(AB_Transaction_GetRemoteBankCode(t)); } if (AB_Transaction_GetRemoteIban(t) == NULL) { trans->remoteIban = Py_None; Py_INCREF(Py_None); } else { trans->remoteIban = PyUnicode_FromString(AB_Transaction_GetRemoteIban(t)); } if (AB_Transaction_GetRemoteBic(t) == NULL) { trans->remoteBic = Py_None; Py_INCREF(Py_None); } else { trans->remoteBic = PyUnicode_FromString(AB_Transaction_GetRemoteBic(t)); } if (AB_Transaction_GetRemoteName(t) == NULL) { trans->remoteName = Py_None; Py_INCREF(Py_None); } else { sl = AB_Transaction_GetRemoteName(t); remoteName = GWEN_StringList_FirstString(sl); if (remoteName == NULL) { trans->remoteName = Py_None; } else { trans->remoteName = PyUnicode_FromString(remoteName); } } trans->value = PyFloat_FromDouble(AB_Value_GetValueAsDouble(v)); trans->currency = PyUnicode_FromString("EUR"); trans->uniqueId = PyLong_FromLong(AB_Transaction_GetUniqueId(t)); if (AB_Transaction_GetTransactionText(t) == NULL) { trans->transactionText = PyUnicode_FromString(""); } else { trans->transactionText = PyUnicode_FromString(AB_Transaction_GetTransactionText(t)); } trans->transactionCode = PyLong_FromLong(AB_Transaction_GetTransactionCode(t)); trans->textKey = PyLong_FromLong(AB_Transaction_GetTextKey(t)); trans->textKeyExt = PyLong_FromLong(AB_Transaction_GetTextKeyExt(t)); if (AB_Transaction_GetMandateId(t) == NULL) { trans->sepaMandateId = Py_None; } else { trans->sepaMandateId = PyUnicode_FromString(AB_Transaction_GetMandateId(t)); } if (AB_Transaction_GetCustomerReference(t) == NULL) { trans->customerReference = PyUnicode_FromString(""); } else { trans->customerReference = PyUnicode_FromString(AB_Transaction_GetCustomerReference(t)); } if (AB_Transaction_GetBankReference(t) == NULL) { trans->bankReference = PyUnicode_FromString(""); } else { trans->bankReference = PyUnicode_FromString(AB_Transaction_GetBankReference(t)); } if (AB_Transaction_GetEndToEndReference(t) == NULL) { trans->endToEndReference = PyUnicode_FromString(""); } else { trans->endToEndReference = PyUnicode_FromString(AB_Transaction_GetEndToEndReference(t)); } trans->state = 0; state = AB_Transaction_GetStatus(t); switch(state) { case AB_Transaction_StatusUnknown: trans->state = -1; break; case AB_Transaction_StatusNone: trans->state = 0; break; case AB_Transaction_StatusAccepted: trans->state = 1; break; case AB_Transaction_StatusRejected: trans->state = 2; break; case AB_Transaction_StatusPending: trans->state = 4; break; case AB_Transaction_StatusSending: trans->state = 8; break; case AB_Transaction_StatusAutoReconciled: trans->state = 16; break; case AB_Transaction_StatusManuallyReconciled: trans->state = 32; break; case AB_Transaction_StatusRevoked: trans->state = 64; break; case AB_Transaction_StatusAborted: trans->state = 128; break; } PyList_Append(transList, (PyObject *)trans); Py_DECREF(trans); } t = AB_ImExporterAccountInfo_GetNextTransaction(ai); } ai = AB_ImExporterContext_GetNextAccountInfo(ctx); } // Free jobs. AB_Job_free(job); AB_Job_List2_free(jl); AB_ImExporterContext_free(ctx); // Exit aqbanking. rv = AB_free(self); if (rv > 0) { //Py_XDECREF(trans); Py_DECREF(transList); return NULL; } return transList; }
/* --------------------------------------------------------------- FUNCTION */ int AH_Job_GetTransactions__ReadTransactions(AH_JOB *j, AB_IMEXPORTER_ACCOUNTINFO *ai, const char *docType, int noted, GWEN_BUFFER *buf){ GWEN_DBIO *dbio; GWEN_SYNCIO *sio; int rv; GWEN_DB_NODE *db; GWEN_DB_NODE *dbDay; GWEN_DB_NODE *dbParams; AB_ACCOUNT *a; AB_USER *u; uint32_t progressId; uint64_t cnt=0; a=AH_AccountJob_GetAccount(j); assert(a); u=AH_Job_GetUser(j); assert(u); dbio=GWEN_DBIO_GetPlugin("swift"); if (!dbio) { DBG_ERROR(AQHBCI_LOGDOMAIN, "Plugin SWIFT is not found"); GWEN_Gui_ProgressLog(0, GWEN_LoggerLevel_Error, I18N("Plugin \"SWIFT\" not found.")); return AB_ERROR_PLUGIN_MISSING; } GWEN_Buffer_Rewind(buf); sio=GWEN_SyncIo_Memory_new(buf, 0); db=GWEN_DB_Group_new("transactions"); dbParams=GWEN_DB_Group_new("params"); GWEN_DB_SetCharValue(dbParams, GWEN_DB_FLAGS_OVERWRITE_VARS, "type", docType); if (AH_User_GetFlags(u) & AH_USER_FLAGS_KEEP_MULTIPLE_BLANKS) GWEN_DB_SetIntValue(dbParams, GWEN_DB_FLAGS_OVERWRITE_VARS, "keepMultipleBlanks", 1); else GWEN_DB_SetIntValue(dbParams, GWEN_DB_FLAGS_OVERWRITE_VARS, "keepMultipleBlanks", 0); rv=GWEN_DBIO_Import(dbio, sio, db, dbParams, GWEN_PATH_FLAGS_CREATE_GROUP); if (rv<0) { DBG_ERROR(AQHBCI_LOGDOMAIN, "Error parsing SWIFT %s (%d)", docType, rv); GWEN_DB_Group_free(dbParams); GWEN_DB_Group_free(db); GWEN_SyncIo_free(sio); GWEN_DBIO_free(dbio); return rv; } GWEN_DB_Group_free(dbParams); GWEN_SyncIo_free(sio); GWEN_DBIO_free(dbio); /* first count the groups */ dbDay=GWEN_DB_FindFirstGroup(db, "day"); while(dbDay) { GWEN_DB_NODE *dbT; dbT=GWEN_DB_FindFirstGroup(dbDay, "transaction"); while(dbT) { cnt++; dbT=GWEN_DB_FindNextGroup(dbT, "transaction"); } /* while */ dbDay=GWEN_DB_FindNextGroup(dbDay, "day"); } /* while */ progressId=GWEN_Gui_ProgressStart(GWEN_GUI_PROGRESS_DELAY | GWEN_GUI_PROGRESS_ALLOW_EMBED | GWEN_GUI_PROGRESS_SHOW_PROGRESS | GWEN_GUI_PROGRESS_SHOW_ABORT, I18N("Importing transactions..."), NULL, cnt, 0); /* add transactions to list */ dbDay=GWEN_DB_FindFirstGroup(db, "day"); while(dbDay) { GWEN_DB_NODE *dbT; dbT=GWEN_DB_FindFirstGroup(dbDay, "transaction"); while(dbT) { AB_TRANSACTION *t; t=AB_Transaction_fromDb(dbT); if (!t) { DBG_ERROR(AQHBCI_LOGDOMAIN, "Bad transaction data:"); GWEN_DB_Dump(dbT, 2); } else { const char *s; AB_Transaction_SetLocalBankCode(t, AB_User_GetBankCode(u)); AB_Transaction_SetLocalAccountNumber(t, AB_Account_GetAccountNumber(a)); /* some translations */ s=AB_Transaction_GetRemoteIban(t); if (!(s && *s)) { const char *sAid; /* no remote IBAN set, check whether the bank sends this info in the * fields for national account specifications (instead of the SWIFT * field "?38" which was specified for this case) */ sAid=AB_Transaction_GetRemoteAccountNumber(t); if (sAid && *sAid && AB_Banking_CheckIban(sAid)==0) { /* there is a remote account number specification, and that is an IBAN, * so we set that accordingly */ DBG_INFO(AQBANKING_LOGDOMAIN, "Setting remote IBAN from account number"); AB_Transaction_SetRemoteIban(t, sAid); /* set remote BIC if it not already is */ s=AB_Transaction_GetRemoteBic(t); if (!(s && *s)) { const char *sBid; sBid=AB_Transaction_GetRemoteBankCode(t); if (sBid && *sBid) { DBG_INFO(AQBANKING_LOGDOMAIN, "Setting remote BIC from bank code"); AB_Transaction_SetRemoteBic(t, sBid); } } } } DBG_INFO(AQHBCI_LOGDOMAIN, "Adding transaction"); if (noted) AB_ImExporterAccountInfo_AddNotedTransaction(ai, t); else AB_ImExporterAccountInfo_AddTransaction(ai, t); } if (GWEN_ERROR_USER_ABORTED== GWEN_Gui_ProgressAdvance(progressId, GWEN_GUI_PROGRESS_ONE)) { GWEN_Gui_ProgressEnd(progressId); return GWEN_ERROR_USER_ABORTED; } dbT=GWEN_DB_FindNextGroup(dbT, "transaction"); } /* while */ /* read all endsaldos */ if (!noted) { dbT=GWEN_DB_FindFirstGroup(dbDay, "endSaldo"); while (dbT) { GWEN_DB_NODE *dbX; GWEN_TIME *ti=0; dbX=GWEN_DB_GetGroup(dbT, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "date"); if (dbX) ti=GWEN_Time_fromDb(dbX); dbX=GWEN_DB_GetGroup(dbT, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "value"); if (dbX) { AB_VALUE *v; v=AB_Value_fromDb(dbX); if (v) { AB_BALANCE *bal; AB_ACCOUNT_STATUS *as; bal=AB_Balance_new(v, ti); AB_Value_free(v); as=AB_AccountStatus_new(); if (ti) AB_AccountStatus_SetTime(as, ti); AB_AccountStatus_SetNotedBalance(as, bal); AB_Balance_free(bal); AB_ImExporterAccountInfo_AddAccountStatus(ai, as); } } GWEN_Time_free(ti); dbT=GWEN_DB_FindNextGroup(dbT, "endSaldo"); } /* while */ } dbDay=GWEN_DB_FindNextGroup(dbDay, "day"); } /* while */ GWEN_Gui_ProgressEnd(progressId); GWEN_DB_Group_free(db); return 0; }