File indexing completed on 2024-05-26 05:09:14

0001 /*
0002     SPDX-FileCopyrightText: 2013-2015 Christian Dávid <christian-david@web.de>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "aqbankingkmmoperators.h"
0007 
0008 #include <aqbanking/types/transactionlimits.h>
0009 #include <aqbanking/types/transaction.h>
0010 #include <aqbanking/types/account_spec.h>
0011 #include <aqbanking/types/value.h>
0012 
0013 #include "payeeidentifier/payeeidentifiertyped.h"
0014 #include "payeeidentifier/nationalaccount/nationalaccount.h"
0015 #include "tasksettings/credittransfersettingsbase.h"
0016 #include "onlinetasks/sepa/sepaonlinetransfer.h"
0017 #include "gwenhywfarqtoperators.h"
0018 #include "mymoneymoney.h"
0019 
0020 /**
0021  * @brief SEPA Charset
0022  *
0023  * Additional lower case letters were added, or should the input mask replace them with the uppercase version? The bank should do that
0024  * anyway.
0025  */
0026 static QString sepaChars()
0027 {
0028     return QLatin1String("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz':?.,- (+)/");
0029 }
0030 
0031 /** @todo Check if AB_TransactionLimits_GetMaxLenCustomerReference really is the limit for the sepa reference */
0032 QSharedPointer<sepaOnlineTransfer::settings> AB_TransactionLimits_toSepaOnlineTaskSettings(const AB_TRANSACTION_LIMITS* aqlimits)
0033 {
0034     Q_CHECK_PTR(aqlimits);
0035 
0036     QSharedPointer<creditTransferSettingsBase> settings(new creditTransferSettingsBase);
0037 
0038     settings->setPurposeLimits(AB_TransactionLimits_GetMaxLinesPurpose(aqlimits),
0039                                AB_TransactionLimits_GetMaxLenPurpose(aqlimits),
0040                                AB_TransactionLimits_GetMinLenPurpose(aqlimits)
0041                               );
0042 
0043     // AqBanking returns 0 as min length even if it requires one
0044     int minLength = AB_TransactionLimits_GetMinLenRemoteName(aqlimits);
0045     if (minLength == 0)
0046         minLength = 1;
0047     settings->setRecipientNameLimits(1 /*AB_TransactionLimits_GetMaxLinesRemoteName(aqlimits)*/,
0048                                      AB_TransactionLimits_GetMaxLenRemoteName(aqlimits),
0049                                      minLength
0050                                     );
0051 
0052     // AqBanking returns 0 as min length even if it requires one
0053     minLength = AB_TransactionLimits_GetMinLenLocalName(aqlimits);
0054     if (minLength == 0)
0055         minLength = 1;
0056     settings->setPayeeNameLimits(1, AB_TransactionLimits_GetMaxLenLocalName(aqlimits), minLength);
0057 
0058     //settings->referenceLength = AB_TransactionLimits_GetMax( aqlimits );
0059     settings->setEndToEndReferenceLength(32);
0060 
0061     settings->setAllowedChars(sepaChars());
0062 
0063     return settings.dynamicCast<sepaOnlineTransfer::settings>();
0064 }
0065 
0066 void AB_Transaction_SetRemoteAccount(AB_TRANSACTION* transaction, const payeeIdentifiers::nationalAccount& ident)
0067 {
0068     Q_CHECK_PTR(transaction);
0069 
0070     AB_Transaction_SetRemoteAccountNumber(transaction, ident.accountNumber().toUtf8().constData());
0071     AB_Transaction_SetRemoteBankCode(transaction, ident.bankCode().toUtf8().constData());
0072     AB_Transaction_SetRemoteName(transaction, ident.ownerName().toUtf8().constData());
0073 }
0074 
0075 void AB_Transaction_SetRemoteAccount(AB_TRANSACTION* transaction, const payeeIdentifiers::ibanBic& ident)
0076 {
0077     Q_CHECK_PTR(transaction);
0078 
0079     AB_Transaction_SetRemoteAccountNumber(transaction, ident.electronicIban().toUtf8().constData());
0080     AB_Transaction_SetRemoteBankCode(transaction, ident.fullStoredBic().toUtf8().constData());
0081     AB_Transaction_SetRemoteName(transaction, ident.ownerName().toUtf8().constData());
0082 }
0083 
0084 void AB_Transaction_SetLocalAccount(AB_TRANSACTION* transaction, const AB_ACCOUNT_SPEC* account)
0085 {
0086     Q_CHECK_PTR(transaction);
0087     Q_CHECK_PTR(account);
0088 
0089     AB_Transaction_SetLocalName(transaction, AB_AccountSpec_GetOwnerName(account));
0090     AB_Transaction_SetLocalAccountNumber(transaction, AB_AccountSpec_GetAccountNumber(account));
0091     AB_Transaction_SetLocalBankCode(transaction, AB_AccountSpec_GetBankCode(account));
0092 
0093     AB_Transaction_SetLocalIban(transaction, AB_AccountSpec_GetIban(account));
0094     AB_Transaction_SetLocalBic(transaction, AB_AccountSpec_GetBic(account));
0095 }
0096 
0097 void AB_Transaction_SetLocalAccount(AB_TRANSACTION* transaction, const payeeIdentifiers::nationalAccount& ident)
0098 {
0099     Q_CHECK_PTR(transaction);
0100 
0101     AB_Transaction_SetLocalName(transaction, ident.ownerName().toUtf8().constData());
0102     AB_Transaction_SetLocalAccountNumber(transaction, ident.accountNumber().toUtf8().constData());
0103     AB_Transaction_SetLocalBankCode(transaction, ident.bankCode().toUtf8().constData());
0104 }
0105 
0106 bool AB_Transaction_SetLocalAccount(AB_TRANSACTION* transaction, const QList<payeeIdentifier>& accountNumbers)
0107 {
0108     Q_CHECK_PTR(transaction);
0109 
0110     bool validOriginAccountSet = false;
0111     for (const payeeIdentifier& accountNumber : accountNumbers) {
0112         if (!accountNumber.isValid())
0113             continue;
0114 
0115         try {
0116             payeeIdentifierTyped<payeeIdentifiers::ibanBic> iban(accountNumber);
0117             AB_Transaction_SetLocalIban(transaction, iban->electronicIban().toUtf8().constData());
0118             AB_Transaction_SetLocalBic(transaction, iban->fullStoredBic().toUtf8().constData());
0119         } catch (...) {
0120         }
0121 
0122         try {
0123             payeeIdentifierTyped<payeeIdentifiers::nationalAccount> national(accountNumber);
0124             AB_Transaction_SetLocalAccount(transaction, *(national.data()));
0125             validOriginAccountSet = true;
0126         } catch (...) {
0127         }
0128     }
0129     return validOriginAccountSet;
0130 }
0131 
0132 AB_VALUE* AB_Value_fromMyMoneyMoney(const MyMoneyMoney& input)
0133 {
0134     return (AB_Value_fromString(input.toString().toUtf8().constData()));
0135 }
0136 
0137 MyMoneyMoney AB_Value_toMyMoneyMoney(const AB_VALUE *const value)
0138 {
0139     // I've read somewhere that in M1 were about 12 trillion dollar in 2013. So the buffer length of 32 should be sufficient.
0140     char buffer[32];
0141     memset(buffer, 0, sizeof(buffer));
0142     AB_Value_GetNumDenomString(value, &buffer[0], sizeof(buffer));
0143     return MyMoneyMoney(QString::fromUtf8(buffer));
0144 }