changeset 924:c5dfc1758438

IN 40: Fixed the budget item account assoication is not removed when a goal or debt is removed. Adding an association gives back the BankAccount information.
author John Schneiderman <JohnMS@member.fsf.org>
date Thu, 01 Dec 2022 17:27:56 +0100
parents f62ef19b8028
children f2e113654335
files ChangeLog src/navigation/internal/BankLedgers.cpp src/navigation/internal/BankLedgers.h src/navigation/internal/BudgetBankLedgers.cpp src/navigation/internal/BudgetBankLedgers.h src/navigation/unit-tests/BudgetBankLedgers-unit-tests.cpp
diffstat 6 files changed, 97 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Nov 03 19:11:35 2022 +0100
+++ b/ChangeLog	Thu Dec 01 17:27:56 2022 +0100
@@ -18,6 +18,7 @@
 #**  along with this program. If not, see <http://www.gnu.org/licenses/>.    ***
 #*******************************************************************************
 XXXX-XX-XX John Schneiderman <JohnMS_AT_member_DOT_fsf_DOT_org> 0.5.0
+	* Bug Fix #40: Fixed removing a goal budget item not updating the bank account association.
 	* Bug Fix #45: Fixed allowing selecting the bank account type when posting a budget item.
 	* Bug Fix #46: Fixed the budget item source not adjusting its width when posting a budget item.
 	* Bug Fix #48: Fixed a crash when reconciling a transaction between different currencies whose
--- a/src/navigation/internal/BankLedgers.cpp	Thu Nov 03 19:11:35 2022 +0100
+++ b/src/navigation/internal/BankLedgers.cpp	Thu Dec 01 17:27:56 2022 +0100
@@ -59,6 +59,8 @@
 using drn::accounting::GeneralLedger;
 #include <accounting/AccountNumberGenerator.h>
 using drn::accounting::AccountNumberGenerator;
+#include <banking/BankAccount.h>
+using drn::banking::BankAccount;
 #include <banking/BankMap.h>
 using drn::banking::BankMap;
 #include <banking/BankingErrors.h>
@@ -262,7 +264,7 @@
 	}
 }
 
-void BankLedgers::addAssociation(
+BankAccount BankLedgers::addAssociation(
 	const BankName& name,
 	const AccountNumber& number,
 	const SupportedAccountTypes& type
@@ -283,6 +285,7 @@
 		throw BankError{name, QObject::tr("The bank account type cannot be unknown.")};
 	this->banks_->add(name, number);
 	this->bankAccountTypes_[name][number] = type;
+	return BankAccount{name, BankAccountType{this->ledgers_->ledger(number).account_.code(), type}};
 }
 
 void BankLedgers::rename(const BankName& original, const BankName& changed)
--- a/src/navigation/internal/BankLedgers.h	Thu Nov 03 19:11:35 2022 +0100
+++ b/src/navigation/internal/BankLedgers.h	Thu Dec 01 17:27:56 2022 +0100
@@ -54,6 +54,12 @@
 class GeneralLedger;
 
 }
+namespace banking
+{
+
+struct BankAccount;
+
+}
 namespace foundation
 {
 
@@ -174,8 +180,10 @@
 	 * @param name The name of the bank to add the account number to.
 	 * @param number The number to add to the bank.
 	 * @param type The type of bank account the general ledger account is managing.
+	 *
+	 * @returns The bank account associated.
 	 */
-	void addAssociation(
+	banking::BankAccount addAssociation(
 		const banking::BankName& name,
 		const accounting::AccountNumber& number,
 		const banking::SupportedAccountTypes& type
--- a/src/navigation/internal/BudgetBankLedgers.cpp	Thu Nov 03 19:11:35 2022 +0100
+++ b/src/navigation/internal/BudgetBankLedgers.cpp	Thu Dec 01 17:27:56 2022 +0100
@@ -370,12 +370,22 @@
 
 void BudgetBankLedgers::removeDebt(const BudgetSource& source)
 {
+	const auto accountCode{this->budgetLedgers_.budgetAccountCode<Debt>(source)};
+	const auto bank{this->bankLedgers_.findBankName(accountCode.number())};
 	this->budgetLedgers_.removeDebt(source);
+
+	if (bank.hasValue() && this->bankLedgers_.hasAssociation(*bank, accountCode.number()))
+		this->bankLedgers_.removeAssociation(*bank, accountCode.number());
 }
 
 void BudgetBankLedgers::removeGoal(const BudgetSource& source)
 {
+	const auto accountCode{this->budgetLedgers_.budgetAccountCode<Goal>(source)};
+	const auto bank{this->bankLedgers_.findBankName(accountCode.number())};
 	this->budgetLedgers_.removeGoal(source);
+
+	if (bank.hasValue() && this->bankLedgers_.hasAssociation(*bank, accountCode.number()))
+		this->bankLedgers_.removeAssociation(*bank, accountCode.number());
 }
 
 void BudgetBankLedgers::removeNontrack(const BudgetSource& source)
@@ -567,13 +577,13 @@
 	this->bankLedgers_.add(move(b), move(accountNumbersBankTypes));
 }
 
-void BudgetBankLedgers::addAssociation(
+BankAccount BudgetBankLedgers::addAssociation(
 	const BankName& name,
 	const AccountNumber& number,
 	const SupportedAccountTypes& type
 )
 {
-	this->bankLedgers_.addAssociation(name, number, type);
+	return this->bankLedgers_.addAssociation(name, number, type);
 }
 
 AccountCode BudgetBankLedgers::addAccount(
--- a/src/navigation/internal/BudgetBankLedgers.h	Thu Nov 03 19:11:35 2022 +0100
+++ b/src/navigation/internal/BudgetBankLedgers.h	Thu Dec 01 17:27:56 2022 +0100
@@ -283,6 +283,8 @@
 	 * @tparam BudgetItemType The type of the budget item to add.
 	 *
 	 * @param item The budget item that is to be added.
+	 * @param ba When supplied, the bank account to associate with the budget item, e.g. bank's
+	 * saving account.
 	 *
 	 * @return The account code created for storing the budget item's transactions.
 	 */
@@ -432,8 +434,10 @@
 	 * @param name The name of the bank to add the account code to.
 	 * @param number The number to add to the bank.
 	 * @param type The type of bank account the general ledger account is managing.
+	 *
+	 * @returns The bank account associated.
 	 */
-	void addAssociation(
+	banking::BankAccount addAssociation(
 		const banking::BankName& name,
 		const accounting::AccountNumber& number,
 		const banking::SupportedAccountTypes& type
--- a/src/navigation/unit-tests/BudgetBankLedgers-unit-tests.cpp	Thu Nov 03 19:11:35 2022 +0100
+++ b/src/navigation/unit-tests/BudgetBankLedgers-unit-tests.cpp	Thu Dec 01 17:27:56 2022 +0100
@@ -70,6 +70,7 @@
 #include <banking/BankAccount.h>
 using drn::banking::BankAccount;
 #include <banking/BankAccountType.h>
+using drn::banking::BankAccountType;
 using drn::banking::SupportedAccountTypes;
 #include <banking/BankingErrors.h>
 using drn::banking::BankError;
@@ -2472,6 +2473,71 @@
 		QVERIFY_EXCEPTION_THROWN(bbl.remove(this->c1stName_), BankError);
 	}
 
+	void remove_debtBudgetItem_ShouldRemove()
+	{
+		BudgetBankLedgers bbl{};
+		const Bank c1st{this->c1stName_};
+		bbl.add(c1st);
+		const Debt carLoan{
+			BudgetSource{"Car Loan"},
+			Money{150, 0u, Iso4217Codes::PLN},
+			EventFrequency::Monthly,
+			QDate::currentDate().addMonths(1),
+			Money{150000, 0u, Iso4217Codes::PLN},
+			Percentage{15.0}
+		};
+		const auto carAccount{
+			bbl.add(
+				carLoan,
+				BankAccount{
+					this->c1stName_,
+					BankAccountType{AccountCode{"Member Loan"}, SupportedAccountTypes::Loan}
+				}
+			)
+		};
+		QVERIFY_TRUE(bbl.generalLedger().hasLedger(carAccount.number()));
+		QVERIFY_NO_THROW(bbl.removeDebt(carLoan.source()));
+		QVERIFY_TRUE(bbl.budget().debts_.empty());
+		QVERIFY_FALSE(bbl.generalLedger().hasLedger(carAccount.number()));
+		QVERIFY_THAT(
+			bbl.banks().bank(this->c1stName_).accounts(),
+			equals(set<AccountNumber>{})
+		);
+	}
+
+	void remove_goalBudgetItem_ShouldRemove()
+	{
+		BudgetBankLedgers bbl{};
+		const Bank c1st{this->c1stName_};
+		bbl.add(c1st);
+		const auto savings{
+			bbl.addAccount(
+				"Member Account",
+				SupportedAccountTypes::Savings,
+				Money{Iso4217Codes::PLN}
+			)
+		};
+		const auto bankSavings{
+			bbl.addAssociation(this->c1stName_, savings.number(), SupportedAccountTypes::Savings)
+		};
+		const Goal cardDownPayment{
+			BudgetSource{"Car Down Payment"},
+			Money{150, 0u, Iso4217Codes::PLN},
+			EventFrequency::Monthly,
+			QDate::currentDate().addMonths(1),
+			Money{15000, 0u, Iso4217Codes::PLN},
+		};
+		const auto cardDownPaymentAccount{bbl.add(cardDownPayment, bankSavings)};
+		QVERIFY_NO_THROW(bbl.removeGoal(cardDownPayment.source()));
+		QVERIFY_TRUE(bbl.budget().goals_.empty());
+		QVERIFY_TRUE(bbl.generalLedger().hasLedger(savings.number()));
+		QVERIFY_FALSE(bbl.generalLedger().hasLedger(cardDownPaymentAccount.number()));
+		QVERIFY_THAT(
+			bbl.banks().bank(this->c1stName_).accounts(),
+			equals(set<AccountNumber>{savings.number()})
+		);
+	}
+
 	//}
 
 	//{ setManaged