Skip to content
Snippets Groups Projects
Unverified Commit 5afd76fb authored by shall0pass's avatar shall0pass Committed by GitHub
Browse files

Goals: Compounding changes for By and Schedules (#1028)

parent 54c8d5b7
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,6 @@ import { ...@@ -2,7 +2,6 @@ import {
differenceInCalendarMonths, differenceInCalendarMonths,
addMonths, addMonths,
addWeeks, addWeeks,
addDays,
format, format,
} from 'date-fns'; } from 'date-fns';
...@@ -305,9 +304,7 @@ async function applyCategoryTemplate( ...@@ -305,9 +304,7 @@ async function applyCategoryTemplate(
let limit; let limit;
let hold; let hold;
let last_month_balance = balance - spent - budgeted; let last_month_balance = balance - spent - budgeted;
let totalTarget = 0; let remainder = 0;
let totalMonths = 0;
let skipMonths = 0;
for (let l = 0; l < template_lines.length; l++) { for (let l = 0; l < template_lines.length; l++) {
let template = template_lines[l]; let template = template_lines[l];
switch (template.type) { switch (template.type) {
...@@ -339,7 +336,7 @@ async function applyCategoryTemplate( ...@@ -339,7 +336,7 @@ async function applyCategoryTemplate(
} }
case 'by': { case 'by': {
// by has 'amount' and 'month' params // by has 'amount' and 'month' params
let N = template_lines.length; let target = 0;
let target_month = new Date(`${template_lines[l].month}-01`); let target_month = new Date(`${template_lines[l].month}-01`);
let num_months = differenceInCalendarMonths( let num_months = differenceInCalendarMonths(
target_month, target_month,
...@@ -356,21 +353,19 @@ async function applyCategoryTemplate( ...@@ -356,21 +353,19 @@ async function applyCategoryTemplate(
current_month, current_month,
); );
} }
if (num_months < 0) { if (l === 0) remainder = last_month_balance;
skipMonths++; remainder = amountToInteger(template_lines[l].amount) - remainder;
if (remainder >= 0) {
target = remainder;
remainder = 0;
} else { } else {
totalTarget += amountToInteger(template_lines[l].amount); target = 0;
totalMonths += num_months + 1; remainder = Math.abs(remainder);
} }
let diff = num_months > -1 ? Math.round(target / (num_months + 1)) : 0;
let diff = totalTarget - last_month_balance; if (diff >= 0) {
if (diff >= 0 && totalMonths > 0 && l === N - 1) { if (to_budget + diff < budgetAvailable || !priority) {
let increment = Math.round( to_budget += diff;
((totalTarget - last_month_balance) / totalMonths) *
(N - skipMonths),
);
if (to_budget + increment < budgetAvailable || !priority) {
to_budget += increment;
} else { } else {
if (budgetAvailable > 0) to_budget += budgetAvailable; if (budgetAvailable > 0) to_budget += budgetAvailable;
errors.push(`Insufficient funds.`); errors.push(`Insufficient funds.`);
...@@ -506,44 +501,35 @@ async function applyCategoryTemplate( ...@@ -506,44 +501,35 @@ async function applyCategoryTemplate(
let conditions = rule.serialize().conditions; let conditions = rule.serialize().conditions;
let { date: dateCond, amount: amountCond } = let { date: dateCond, amount: amountCond } =
extractScheduleConds(conditions); extractScheduleConds(conditions);
let isRepeating =
Object(dateCond.value) === dateCond.value &&
'frequency' in dateCond.value;
let next_date_string = getNextDate(dateCond, current_month); let next_date_string = getNextDate(dateCond, current_month);
let num_months = differenceInCalendarMonths( let num_months = differenceInCalendarMonths(
new Date(next_date_string), new Date(next_date_string),
current_month, current_month,
); );
let target = -getScheduledAmount(amountCond.value); if (l === 0) remainder = last_month_balance;
let diff = target - balance + budgeted; remainder = -getScheduledAmount(amountCond.value) - remainder;
let target = 0;
if (remainder >= 0) {
target = remainder;
remainder = 0;
} else {
target = 0;
remainder = Math.abs(remainder);
}
let diff = num_months > 0 ? Math.round(target / num_months) : 0;
if (num_months < 0) { if (num_months < 0) {
errors.push( errors.push(
`Non-repeating schedule ${template.name} was due on ${next_date_string}, which is in the past.`, `Non-repeating schedule ${template.name} was due on ${next_date_string}, which is in the past.`,
); );
return { errors }; return { errors };
} else if (num_months > 0) { } else if (num_months > 0) {
if (diff >= 0 && num_months > -1) { if (
to_budget += Math.round(diff / num_months); (diff >= 0 &&
} num_months > -1 &&
} else { to_budget + diff < budgetAvailable) ||
let monthly_target = 0; !priority
let next_month = addMonths(current_month, 1); ) {
let next_date = new Date(next_date_string); to_budget += diff;
if (isRepeating) {
while (next_date.getTime() < next_month.getTime()) {
if (next_date.getTime() >= current_month.getTime()) {
monthly_target += target;
}
next_date = addDays(next_date, 1);
next_date_string = getNextDate(dateCond, next_date);
next_date = new Date(next_date_string);
}
} else {
monthly_target = target;
}
let increment = monthly_target - balance + budgeted;
if (to_budget + increment < budgetAvailable || !priority) {
to_budget += increment;
} else { } else {
if (budgetAvailable > 0) to_budget = budgetAvailable; if (budgetAvailable > 0) to_budget = budgetAvailable;
errors.push(`Insufficient funds.`); errors.push(`Insufficient funds.`);
......
---
category: Bugfix
authors: [shall0pass]
---
Bugfix: Goals template compounding - Large target differences resulted in not enough funding
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment