Skip to content

Commit

Permalink
fix(moneymanagerex#6827), part 2: inactive billsdeposits; None -> Once (
Browse files Browse the repository at this point in the history
moneymanagerex#6836)

* fix(moneymanagerex#6827): Repetitions, special values of NUMOCCURRENCES

* fix(moneymanagerex#6827): add enum REPEAT_NUM

* fix(moneymanagerex#6827), part 2

* bug fix in Model_Billsdeposits::completeBDInSeries
  • Loading branch information
georgeef authored Aug 21, 2024
1 parent 95cad72 commit a4c1295
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 156 deletions.
216 changes: 123 additions & 93 deletions src/billsdepositsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,26 @@
#include "model/Model_CurrencyHistory.h"
#include <wx/valnum.h>

enum { NONE, WEEKLY, FORTNIGHTLY, MONTHLY
, BIMONTHLY, QUARTERLY, HALFYEARLY
, YEARLY, FOURMONTHS, FOURWEEKS
, DAILY, INXDAYS, INXMONTHS
, EVERYXDAYS, EVERYXMONTHS, MONTHLYLASTDAY, MONTHLYLASTBUSINESSDAY
};

// the order in gui may be different than the database encoding order.
const std::vector<std::pair<int, wxString> > mmBDDialog::BILLSDEPOSITS_REPEATS =
{
{ NONE, wxTRANSLATE("None")},
{ WEEKLY, wxTRANSLATE("Weekly")},
{ FORTNIGHTLY, wxTRANSLATE("Fortnightly")},
{ MONTHLY, wxTRANSLATE("Monthly")},
{ BIMONTHLY, wxTRANSLATE("Every 2 Months")},
{ QUARTERLY, wxTRANSLATE("Quarterly")},
{ HALFYEARLY, wxTRANSLATE("Half-Yearly")},
{ YEARLY, wxTRANSLATE("Yearly")},
{ FOURMONTHS, wxTRANSLATE("Four Months")},
{ FOURWEEKS, wxTRANSLATE("Four Weeks")},
{ DAILY, wxTRANSLATE("Daily")},
{ INXDAYS, wxTRANSLATE("In (n) Days")},
{ INXMONTHS, wxTRANSLATE("In (n) Months")},
{ EVERYXDAYS, wxTRANSLATE("Every (n) Days")},
{ EVERYXMONTHS, wxTRANSLATE("Every (n) Months")},
{ MONTHLYLASTDAY, wxTRANSLATE("Monthly (last day)")},
{ MONTHLYLASTBUSINESSDAY, wxTRANSLATE("Monthly (last business day)")}

{ Model_Billsdeposits::REPEAT_ONCE, wxTRANSLATE("Once")},
{ Model_Billsdeposits::REPEAT_WEEKLY, wxTRANSLATE("Weekly")},
{ Model_Billsdeposits::REPEAT_BI_WEEKLY, wxTRANSLATE("Fortnightly")},
{ Model_Billsdeposits::REPEAT_MONTHLY, wxTRANSLATE("Monthly")},
{ Model_Billsdeposits::REPEAT_BI_MONTHLY, wxTRANSLATE("Every 2 Months")},
{ Model_Billsdeposits::REPEAT_QUARTERLY, wxTRANSLATE("Quarterly")},
{ Model_Billsdeposits::REPEAT_HALF_YEARLY, wxTRANSLATE("Half-Yearly")},
{ Model_Billsdeposits::REPEAT_YEARLY, wxTRANSLATE("Yearly")},
{ Model_Billsdeposits::REPEAT_FOUR_MONTHLY, wxTRANSLATE("Four Months")},
{ Model_Billsdeposits::REPEAT_FOUR_WEEKLY, wxTRANSLATE("Four Weeks")},
{ Model_Billsdeposits::REPEAT_DAILY, wxTRANSLATE("Daily")},
{ Model_Billsdeposits::REPEAT_IN_X_DAYS, wxTRANSLATE("In (n) Days")},
{ Model_Billsdeposits::REPEAT_IN_X_MONTHS, wxTRANSLATE("In (n) Months")},
{ Model_Billsdeposits::REPEAT_EVERY_X_DAYS, wxTRANSLATE("Every (n) Days")},
{ Model_Billsdeposits::REPEAT_EVERY_X_MONTHS, wxTRANSLATE("Every (n) Months")},
{ Model_Billsdeposits::REPEAT_MONTHLY_LAST_DAY, wxTRANSLATE("Monthly (last day)")},
{ Model_Billsdeposits::REPEAT_MONTHLY_LAST_BUSINESS_DAY, wxTRANSLATE("Monthly (last business day)")}
};

// Used to determine if we need to refresh the tag text ctrl after
Expand Down Expand Up @@ -211,16 +204,15 @@ void mmBDDialog::dataToControls()
{
m_choice_repeat->Append(wxGetTranslation(entry.second));
}
m_choice_repeat->SetSelection(MONTHLY);
setRepeatType(Model_Billsdeposits::REPEAT_MONTHLY);

for (const auto& i : Model_Billsdeposits::all_type())
{
if (i != Model_Billsdeposits::all_type()[Model_Billsdeposits::TRANSFER] || Model_Account::instance().all().size() > 1)
{
m_choice_transaction_type->Append(wxGetTranslation(i), new wxStringClientData(i));
}
if (i == Model_Billsdeposits::all_type()[Model_Billsdeposits::TRANSFER] && Model_Account::instance().all().size() < 2)
break;
m_choice_transaction_type->Append(wxGetTranslation(i), new wxStringClientData(i));
}
m_choice_transaction_type->SetSelection(0);
m_choice_transaction_type->SetSelection(Model_Billsdeposits::WITHDRAWAL);

SetTransferControls(); // hide appropriate fields
setCategoryLabel();
Expand All @@ -230,9 +222,6 @@ void mmBDDialog::dataToControls()
}

m_choice_status->SetSelection(Model_Checking::status(m_bill_data.STATUS));
if (m_bill_data.NUMOCCURRENCES > 0) {
textNumRepeats_->SetValue(wxString::Format("%d", m_bill_data.NUMOCCURRENCES));
}

// Set the date paid
wxDateTime field_date;
Expand All @@ -245,28 +234,39 @@ void mmBDDialog::dataToControls()

// Have used repeatSel to multiplex auto repeat fields.
if (m_bill_data.REPEATS >= 2 * BD_REPEATS_MULTIPLEX_BASE)
{
autoExecuteSilent_ = true;
itemCheckBoxAutoExeSilent_->SetValue(true);
itemCheckBoxAutoExeUserAck_->Enable(false);
} else if (m_bill_data.REPEATS >= BD_REPEATS_MULTIPLEX_BASE)
{
else if (m_bill_data.REPEATS >= BD_REPEATS_MULTIPLEX_BASE)
autoExecuteUserAck_ = true;
itemCheckBoxAutoExeUserAck_->SetValue(true);
itemCheckBoxAutoExeSilent_->Enable(false);
}
m_bill_data.REPEATS %= BD_REPEATS_MULTIPLEX_BASE;

if (m_bill_data.REPEATS == 0) {// if none
textNumRepeats_->SetValue("");
// fix m_bill_data.REPEATS
if (m_bill_data.REPEATS < Model_Billsdeposits::REPEAT_ONCE || m_bill_data.REPEATS > Model_Billsdeposits::REPEAT_MONTHLY_LAST_BUSINESS_DAY)
{
wxFAIL;
m_bill_data.REPEATS = Model_Billsdeposits::REPEAT_MONTHLY;
}
if (m_bill_data.REPEATS >= Model_Billsdeposits::REPEAT_IN_X_DAYS && m_bill_data.REPEATS <= Model_Billsdeposits::REPEAT_EVERY_X_MONTHS && m_bill_data.NUMOCCURRENCES < 1)
{
// old inactive entry. transform to REPEAT_ONCE and turn off automatic execution.
m_bill_data.REPEATS = Model_Billsdeposits::REPEAT_ONCE;
autoExecuteSilent_ = false;
autoExecuteUserAck_ = false;
}
setRepeatType(m_bill_data.REPEATS);

if (m_bill_data.REPEATS < NONE || m_bill_data.REPEATS > MONTHLYLASTBUSINESSDAY) {
wxFAIL;
m_choice_repeat->SetSelection(MONTHLY);
if (m_bill_data.REPEATS != Model_Billsdeposits::REPEAT_ONCE && m_bill_data.NUMOCCURRENCES > 0) {
textNumRepeats_->SetValue(wxString::Format("%d", m_bill_data.NUMOCCURRENCES));
}
else {
m_choice_repeat->SetSelection(m_bill_data.REPEATS);

if (autoExecuteSilent_)
{
itemCheckBoxAutoExeSilent_->SetValue(true);
itemCheckBoxAutoExeUserAck_->Enable(false);
}
else if (autoExecuteUserAck_)
{
itemCheckBoxAutoExeUserAck_->SetValue(true);
itemCheckBoxAutoExeSilent_->Enable(false);
}
setRepeatDetails();

Expand Down Expand Up @@ -1006,8 +1006,10 @@ void mmBDDialog::OnOk(wxCommandEvent& WXUNUSED(event))
}
}

// store repeats (it is used again later)
int repeats = getRepeatType();
// Multiplex Auto executable onto the repeat field of the database.
m_bill_data.REPEATS = m_choice_repeat->GetSelection();
m_bill_data.REPEATS = repeats;
if (autoExecuteUserAck_) {
m_bill_data.REPEATS += BD_REPEATS_MULTIPLEX_BASE;
}
Expand All @@ -1017,12 +1019,12 @@ void mmBDDialog::OnOk(wxCommandEvent& WXUNUSED(event))

const wxString& numRepeatStr = textNumRepeats_->GetValue();
m_bill_data.NUMOCCURRENCES = -1;

if (repeats >= Model_Billsdeposits::REPEAT_IN_X_DAYS && repeats <= Model_Billsdeposits::REPEAT_EVERY_X_MONTHS)
m_bill_data.NUMOCCURRENCES = 1;
if (!numRepeatStr.empty())
{
long cnt = 0;
if (numRepeatStr.ToLong(&cnt)) {
wxASSERT(std::numeric_limits<int>::min() <= cnt);
if (numRepeatStr.ToLong(&cnt) && cnt > 0) {
wxASSERT(cnt <= std::numeric_limits<int>::max());
m_bill_data.NUMOCCURRENCES = static_cast<int>(cnt);
}
Expand Down Expand Up @@ -1128,10 +1130,12 @@ void mmBDDialog::OnOk(wxCommandEvent& WXUNUSED(event))
}
else
{
// repeats now hold extra info. Need to get repeats from dialog selection
if ((m_choice_repeat->GetSelection() < INXDAYS)
|| (m_choice_repeat->GetSelection() > EVERYXMONTHS)
|| (m_bill_data.REPEATS > NONE))
// m_bill_data.REPEATS holds extra info; use repeats instead
// the following condition is always true, since old inactive entries of type
// REPEAT_IN_X_*, REPEAT_EVERY_X_* have been converted to entries of type REPEAT_ONCE
if ((repeats < Model_Billsdeposits::REPEAT_IN_X_DAYS)
|| (repeats > Model_Billsdeposits::REPEAT_EVERY_X_MONTHS)
|| (m_bill_data.NUMOCCURRENCES > 0))
{
Model_Checking::Data* tran = Model_Checking::instance().create();
tran->ACCOUNTID = m_bill_data.ACCOUNTID;
Expand Down Expand Up @@ -1292,48 +1296,29 @@ void mmBDDialog::SetAdvancedTransferControls(bool advanced)

void mmBDDialog::setRepeatDetails()
{
const wxString repeatLabelRepeats = _("Repeats");
const wxString repeatLabelActivate = _("Activates");

const wxString timeLabelDays = _("Period: Days");
const wxString timeLabelMonths = _("Period: Months");
staticTextRepeats_->SetLabelText(_("Repeats"));

int repeats = m_choice_repeat->GetSelection();
if (repeats == INXDAYS)
int repeats = getRepeatType();
if (repeats == Model_Billsdeposits::REPEAT_IN_X_DAYS || repeats == Model_Billsdeposits::REPEAT_EVERY_X_DAYS)
{
staticTextRepeats_->SetLabelText(repeatLabelActivate);
staticTimesRepeat_->SetLabelText(timeLabelDays);
const auto toolTipsStr = _("Specify period in Days to activate.\n"
"Becomes blank when not active.");
staticTimesRepeat_->SetLabelText(_("Period: Days"));
const auto toolTipsStr = _("Specify period in Days.");
mmToolTip(textNumRepeats_, toolTipsStr);
}
else if (repeats == INXMONTHS)
else if (repeats == Model_Billsdeposits::REPEAT_IN_X_MONTHS || repeats == Model_Billsdeposits::REPEAT_EVERY_X_MONTHS)
{
staticTextRepeats_->SetLabelText(repeatLabelActivate);
staticTimesRepeat_->SetLabelText(timeLabelMonths);
const auto toolTipsStr = _("Specify period in Months to activate.\n"
"Becomes blank when not active.");
staticTimesRepeat_->SetLabelText(_("Period: Months"));
const auto toolTipsStr = _("Specify period in Months.");
mmToolTip(textNumRepeats_, toolTipsStr);
}
else if (repeats == EVERYXDAYS)
else if (repeats == Model_Billsdeposits::REPEAT_ONCE)
{
staticTextRepeats_->SetLabelText(repeatLabelRepeats);
staticTimesRepeat_->SetLabelText(timeLabelDays);
const auto toolTipsStr = _("Specify period in Days to activate.\n"
"Leave blank when not active.");
mmToolTip(textNumRepeats_, toolTipsStr);
}
else if (repeats == EVERYXMONTHS)
{
staticTextRepeats_->SetLabelText(repeatLabelRepeats);
staticTimesRepeat_->SetLabelText(timeLabelMonths);
const auto toolTipsStr = _("Specify period in Months to activate.\n"
"Leave blank when not active.");
staticTimesRepeat_->SetLabelText(_("Payments Left"));
const auto toolTipsStr = _("Ignored (leave blank).");
mmToolTip(textNumRepeats_, toolTipsStr);
}
else
{
staticTextRepeats_->SetLabelText(repeatLabelRepeats);
staticTimesRepeat_->SetLabelText(_("Payments Left"));
const auto toolTipsStr = _("Specify the number of payments to be made.\n"
"Leave blank if the payments continue forever.");
Expand All @@ -1346,22 +1331,67 @@ void mmBDDialog::OnRepeatTypeChanged(wxCommandEvent& WXUNUSED(event))
setRepeatDetails();
}

int mmBDDialog::getRepeatType()
{
int repeatIndex = m_choice_repeat->GetSelection();
return repeatIndex >= 0 ? BILLSDEPOSITS_REPEATS.at(repeatIndex).first : -1;
}

void mmBDDialog::setRepeatType(int repeatType)
{
if (repeatType < 0)
{
wxFAIL;
return;
}

// fast path
int repeatIndex = repeatType;
if (BILLSDEPOSITS_REPEATS.at(repeatIndex).first != repeatType)
{
// slow path: BILLSDEPOSITS_REPEATS is not sorted by REPEAT_TYPE
// cache the mapping from type to index
static std::vector<int> index;
if (index.size() == 0) {
wxLogDebug("mmBDDialog::setRepeatType : cache index");
index.resize(BILLSDEPOSITS_REPEATS.size(), -1);
for (size_t i = 0; i < BILLSDEPOSITS_REPEATS.size(); i++)
{
int j = BILLSDEPOSITS_REPEATS.at(i).first;
if (j >= 0 && j < BILLSDEPOSITS_REPEATS.size() && index.at(j) == -1)
index.at(j) = i;
else
wxFAIL;
}
}

repeatIndex = index.at(repeatType);
if (repeatIndex == -1)
{
wxFAIL;
repeatIndex = 0;
}
}

m_choice_repeat->SetSelection(repeatIndex);
}

void mmBDDialog::OnsetPrevOrNextRepeatDate(wxCommandEvent& event)
{
int repeatType = m_choice_repeat->GetSelection();
int repeatType = getRepeatType();
wxString valueStr = textNumRepeats_->GetValue();
int span = 1;
bool goPrev = (event.GetId() == ID_DIALOG_TRANS_BUTTONTRANSNUMPREV);

switch (repeatType)
{
case INXDAYS:
case Model_Billsdeposits::REPEAT_IN_X_DAYS:
wxFALLTHROUGH;
case INXMONTHS:
case Model_Billsdeposits::REPEAT_IN_X_MONTHS:
wxFALLTHROUGH;
case EVERYXDAYS:
case Model_Billsdeposits::REPEAT_EVERY_X_DAYS:
wxFALLTHROUGH;
case EVERYXMONTHS:
case Model_Billsdeposits::REPEAT_EVERY_X_MONTHS:
span = wxAtoi(valueStr);
if (!valueStr.IsNumber() || !span) {
mmErrorDialogs::ToolTip4Object(textNumRepeats_, _("Invalid value"), _("Error"));
Expand Down Expand Up @@ -1562,4 +1592,4 @@ void mmBDDialog::OnCalculator(wxCommandEvent& WXUNUSED(event))
{
calcPopup_->SetTarget(calcTarget_);
calcPopup_->Popup();
}
}
2 changes: 2 additions & 0 deletions src/billsdepositsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class mmBDDialog : public wxDialog
void OnRepeatTypeChanged(wxCommandEvent& event);
void OnsetPrevOrNextRepeatDate(wxCommandEvent& event);
void setRepeatDetails();
int getRepeatType();
void setRepeatType(int repeatType);
void OnMoreFields(wxCommandEvent& event);

void activateSplitTransactionsDlg();
Expand Down
Loading

0 comments on commit a4c1295

Please sign in to comment.