changeset 896:c58d735ec6be

IN 32: Removed the add member function from the ReconciledSurveys to prevent possible confusion on how to add new completed surveys. Adds presentationText for a BankAccount. Adds presentationText for a BankAccountType.
author John Schneiderman <JohnMS@member.fsf.org>
date Mon, 03 Oct 2022 17:23:45 +0200
parents 1a9f8199914b
children 2b0bc66b951f
files src/banking/external/banking/BankAccount.cpp src/banking/external/banking/BankAccount.h src/banking/external/banking/BankAccountType.cpp src/banking/external/banking/BankAccountType.h src/file-storage/internal/XmlBudgetFile.cpp src/surveying/external/surveying/ReconciledSurvey.cpp src/surveying/external/surveying/ReconciledSurvey.h
diffstat 7 files changed, 70 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/banking/external/banking/BankAccount.cpp	Thu Sep 29 19:02:45 2022 +0200
+++ b/src/banking/external/banking/BankAccount.cpp	Mon Oct 03 17:23:45 2022 +0200
@@ -19,6 +19,10 @@
 *******************************************************************************/
 #include "BankAccount.h"
 using drn::banking::BankAccount;
+using drn::banking::presentationText;
+
+#include <QString>
+using ::QString;
 
 #include <ostream>
 using std::ostream;
@@ -45,3 +49,10 @@
 {
 	return out << "Bank (" << ba.bank_ << "), Account (" << ba.account_ << ')';
 }
+
+QString drn::banking::presentationText(const BankAccount& ba)
+{
+	return QStringLiteral("%1 %2")
+		.arg(presentationText(ba.bank_))
+		.arg(presentationText(ba.account_));
+}
--- a/src/banking/external/banking/BankAccount.h	Thu Sep 29 19:02:45 2022 +0200
+++ b/src/banking/external/banking/BankAccount.h	Mon Oct 03 17:23:45 2022 +0200
@@ -27,6 +27,8 @@
 #include "banking_export.h"
 
 
+class QString;
+
 namespace drn
 {
 namespace banking
@@ -42,6 +44,7 @@
 DRN_BANKING_EXPORT bool operator==(const BankAccount& lhs, const BankAccount& rhs);
 DRN_BANKING_EXPORT bool operator!=(const BankAccount& lhs, const BankAccount& rhs);
 DRN_BANKING_EXPORT std::ostream& operator<<(std::ostream& out, const BankAccount& ba);
+DRN_BANKING_EXPORT ::QString presentationText(const BankAccount& ba);
 
 }}
 
--- a/src/banking/external/banking/BankAccountType.cpp	Thu Sep 29 19:02:45 2022 +0200
+++ b/src/banking/external/banking/BankAccountType.cpp	Mon Oct 03 17:23:45 2022 +0200
@@ -19,9 +19,12 @@
 *******************************************************************************/
 #include "BankAccountType.h"
 using std::array;
+using drn::banking::numberOfSupportedAccountTypes;
+using drn::banking::presentationText;
 using drn::banking::SupportedAccountTypes;
-using drn::banking::numberOfSupportedAccountTypes;
 
+#include <QObject>
+using ::QObject;
 #include <QString>
 using ::QString;
 
@@ -54,12 +57,12 @@
 QString drn::banking::presentationText(const SupportedAccountTypes& sat)
 {
 	BEGIN_ENUMERATION_TO_OTHER(sat)
-		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Chequeing, "Chequeing");
-		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Goal, "Goal");
-		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::CreditCard, "Credit Card");
-		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Loan, "Loan");
-		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Savings, "Savings");
-		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Unknown, "Unknown");
+		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Chequeing, QObject::tr("Chequeing"));
+		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Goal, QObject::tr("Goal"));
+		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::CreditCard, QObject::tr("Credit Card"));
+		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Loan, QObject::tr("Loan"));
+		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Savings, QObject::tr("Savings"));
+		MAP_ENUMERATION_TO_OTHER(SupportedAccountTypes::Unknown, QObject::tr("Unknown"));
 	END_ENUMERATION_TO_OTHER(sat, SupportedAccountTypes)
 }
 
@@ -101,6 +104,14 @@
 			<< ')';
 }
 
+QString drn::banking::presentationText(const BankAccountType& bat)
+{
+	return QObject::tr("%1 (%2)")
+		.arg(presentationText(bat.code_))
+		.arg(presentationText(bat.type_));
+}
+
+
 const array<
 	SupportedAccountTypes,
 	numberOfSupportedAccountTypes
--- a/src/banking/external/banking/BankAccountType.h	Thu Sep 29 19:02:45 2022 +0200
+++ b/src/banking/external/banking/BankAccountType.h	Mon Oct 03 17:23:45 2022 +0200
@@ -82,6 +82,7 @@
 DRN_BANKING_EXPORT bool operator==(const BankAccountType& lhs, const BankAccountType& rhs);
 DRN_BANKING_EXPORT bool operator!=(const BankAccountType& lhs, const BankAccountType& rhs);
 DRN_BANKING_EXPORT std::ostream& operator<<(std::ostream& out, const BankAccountType& bat);
+DRN_BANKING_EXPORT ::QString presentationText(const BankAccountType& bat);
 
 constexpr quint8 numberOfSupportedAccountTypes{6};
 extern DRN_BANKING_EXPORT const std::array<
--- a/src/file-storage/internal/XmlBudgetFile.cpp	Thu Sep 29 19:02:45 2022 +0200
+++ b/src/file-storage/internal/XmlBudgetFile.cpp	Mon Oct 03 17:23:45 2022 +0200
@@ -589,7 +589,7 @@
 			BudgetSource{nontrackElement.sourceName_},
 			ledgers.at(AccountCode{AccountNumber{nontrackElement.accountId_}}).account_.code()
 		);
-	ReconciledSurveys surveys;
+	map<BankAccount, ReconciledSurvey> surveys{};
 
 	for (const auto& reconciliationElement : reconciliationsElement.reconciliations_)
 	{
@@ -618,22 +618,24 @@
 						"bank information for %1."
 				).arg(presentationText(accountNumber))
 			};
-		surveys.add(
+		const BankAccount ba{
+			bankInfo->first,
+			BankAccountType{
+				AccountCode{
+					accountNumber,
+					findAccountName(
+						accountsElement.accounts_,
+					 reconciliationElement.accountId_
+					)
+				},
+				bankInfo->second.at(accountNumber)
+			}
+		};
+		surveys.emplace(
+			ba,
 			ReconciledSurvey{
 				ReconciledBankAccount{
-					BankAccount{
-						bankInfo->first,
-						BankAccountType{
-							AccountCode{
-								accountNumber,
-								findAccountName(
-									accountsElement.accounts_,
-									reconciliationElement.accountId_
-								)
-							},
-							bankInfo->second.at(accountNumber)
-						}
-					},
+					ba,
 					Money{
 						reconciliationElement.major_,
 						reconciliationElement.minor_,
@@ -651,7 +653,7 @@
 		move(budgetCodes),
 		move(banks),
 		move(bankAccountTypes),
-		move(surveys)
+		ReconciledSurveys{move(surveys)}
 	);
 }
 
--- a/src/surveying/external/surveying/ReconciledSurvey.cpp	Thu Sep 29 19:02:45 2022 +0200
+++ b/src/surveying/external/surveying/ReconciledSurvey.cpp	Mon Oct 03 17:23:45 2022 +0200
@@ -21,6 +21,7 @@
 using pecunia::currency::Money;
 using std::map;
 using drn::banking::BankAccount;
+using drn::banking::presentationText;
 using drn::banking::ReconciledBankAccount;
 using drn::budgeting::BudgetItemIdentifier;
 using drn::surveying::ReconciledSurvey;
@@ -98,12 +99,17 @@
 
 //{ ReconciledSurveys
 
-void ReconciledSurveys::add(ReconciledSurvey survey)
+ReconciledSurveys::ReconciledSurveys(map<BankAccount, ReconciledSurvey> surveys) :
+	map<BankAccount, ReconciledSurvey>{move(surveys)}
 {
-	// TODO: check that it doesn't already exist else throw.
-	const auto ba{survey.reconciled().bankAccount()};
-	const auto emplaced{(*this).emplace(ba, move(survey))};
-	assert(emplaced.second && "The new survey should always be inserted.");
+	for (const auto& accountSurvey : *this)
+		if (accountSurvey.first != accountSurvey.second.reconciled().bankAccount())
+			throw BankError{
+				accountSurvey.first.bank_,
+				QObject::tr("The index bank account, %1, must match the surveyed bank account, %2.")
+					.arg(presentationText(accountSurvey.first))
+					.arg(presentationText(accountSurvey.second.reconciled().bankAccount()))
+			};
 }
 
 Optional<ReconciledSurvey> ReconciledSurveys::find(const BankAccount& ba) const
@@ -149,23 +155,20 @@
 				.arg(presentationText(reconciledBalance))
 				.arg(presentationText(expectedBalance))
 		};
+	ReconciledSurvey survey{
+		ReconciledBankAccount{ba, reconciledBalance, completedOn},
+		distribution
+	};
 
 	// TODO: Add the bank account for the reconciliations if it doesn't exist.
 	// TODO: update the stored survey with the new balance and date information.
 	if (this->count(ba) == 0)
 	{
-		this->add(
-			ReconciledSurvey{
-				ReconciledBankAccount{ba, reconciledBalance, completedOn},
-				distribution
-			}
-		);
+		const auto emplaced{(*this).emplace(ba, move(survey))};
+		assert(emplaced.second && "The new survey should always be inserted.");
 	}
 	else
-		(*this)[ba] = ReconciledSurvey{
-			ReconciledBankAccount{ba, reconciledBalance, completedOn},
-			distribution
-		};
+		(*this)[ba] = move(survey);
 }
 
 bool drn::surveying::operator==(const ReconciledSurveys& lhs, const ReconciledSurveys& rhs)
--- a/src/surveying/external/surveying/ReconciledSurvey.h	Thu Sep 29 19:02:45 2022 +0200
+++ b/src/surveying/external/surveying/ReconciledSurvey.h	Mon Oct 03 17:23:45 2022 +0200
@@ -79,6 +79,7 @@
 class DRN_SURVEYING_EXPORT ReconciledSurveys : std::map<banking::BankAccount, ReconciledSurvey>
 {
 public:
+	explicit ReconciledSurveys(std::map<banking::BankAccount, ReconciledSurvey> surveys);
 	using std::map<banking::BankAccount, ReconciledSurvey>::map;
 	using std::map<banking::BankAccount, ReconciledSurvey>::const_iterator;
 	using std::map<banking::BankAccount, ReconciledSurvey>::cbegin;
@@ -90,7 +91,6 @@
 	using std::map<banking::BankAccount, ReconciledSurvey>::at;
 	using std::map<banking::BankAccount, ReconciledSurvey>::size;
 
-	void add(ReconciledSurvey survey);
 	foundation::Optional<ReconciledSurvey> find(const banking::BankAccount& ba) const;
 	foundation::Optional<ReconciledSurvey> find(
 		const banking::BankName& bn,