Skip to content

Commit

Permalink
Fix opening balance
Browse files Browse the repository at this point in the history
We used to add the "opening balance" without ID, resulting in
having only a single transaction for all accounts. Setting an
opening balance for a new account removed the existing
"opening balance" transaction from the previous account...

Now we add the opening transaction with an ID and only
remove+add the opening balance for non-new accounts (there
may already be an opening balance that got edited).
  • Loading branch information
humdingerb committed Dec 1, 2024
1 parent d4ae55f commit 995f581
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/AccountSettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ AccountSettingsWindow::AccountSettingsWindow(Account* account)
AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));

bool foundOpeningTransaction = false;
fOpeningTransactionExists = false;

if (fAccount == NULL)
SetTitle(B_TRANSLATE("New account"));
else
foundOpeningTransaction = _GetOpeningTransaction();
fOpeningTransactionExists = _GetOpeningTransaction();

fAccountName = new AutoTextControl("accname", B_TRANSLATE("Name:"),
(fAccount ? fAccount->Name() : NULL), new BMessage(M_DATA_CHANGED));
Expand All @@ -73,7 +73,7 @@ AccountSettingsWindow::AccountSettingsWindow(Account* account)
BSize size(height - 2, height);
fNegativeButton->SetExplicitSize(size);

if (foundOpeningTransaction) {
if (fOpeningTransactionExists) {
fOpeningDate->SetDate(fOpeningTransaction.Date());
fOpeningDate->Validate();
BString tempstr;
Expand Down Expand Up @@ -184,7 +184,9 @@ AccountSettingsWindow::MessageReceived(BMessage* msg)
Locale customLocale;
fPrefView->GetSettings(customLocale);

bool newAccount = false;
if (!fAccount) {
newAccount = true;
gDatabase.AddAccount(fAccountName->Text(), ACCOUNT_BANK, "Open",
fUseDefault->Value() == B_CONTROL_ON ? NULL : &customLocale);
fAccount = gDatabase.AccountByName(fAccountName->Text());
Expand All @@ -199,17 +201,17 @@ AccountSettingsWindow::MessageReceived(BMessage* msg)
fAccount->UseDefaultLocale(true);
}

// Opening balance date and amount not empty, create opening trnsaction.
// Opening balance date and amount not empty, create opening transaction.
if (strlen(fOpeningAmount->Text()) > 0 && strlen(fOpeningDate->Text()) > 0) {
const char* type = fNegativeButton->Value() == B_CONTROL_OFF ? "DEP" : "ATM";
fOpeningTransaction.Set(fAccount, fOpeningDate->Text(), type, NULL,
fOpeningAmount->Text(), B_TRANSLATE_CONTEXT("Opening balance", "CommonTerms"),
NULL, fOpeningTransaction.Status());
try {
gDatabase.RemoveTransaction(fOpeningTransaction.GetID());
if (fOpeningTransactionExists)
gDatabase.RemoveTransaction(fOpeningTransaction.GetID());

// This adds the transaction data without generating a new transaction id
gDatabase.AddTransaction(fOpeningTransaction, false);
gDatabase.AddTransaction(fOpeningTransaction);
} catch (CppSQLite3Exception& e) {
debugger(e.errorMessage());
}
Expand Down
1 change: 1 addition & 0 deletions src/AccountSettingsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AccountSettingsWindow : public BWindow {
bool _GetOpeningTransaction();
void _UpdateStates();

bool fOpeningTransactionExists;
AutoTextControl* fAccountName;
DateBox* fOpeningDate;
CurrencyBox* fOpeningAmount;
Expand Down

0 comments on commit 995f581

Please sign in to comment.