-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split available funds in "income" and "not budgeted" and "Start…
…ing Balance" also im to lazy to split this out into a single commit sorry future maintainers fix #51
- Loading branch information
Showing
25 changed files
with
424 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
tell application "MoneyMoney" to export transactions from date 1900 - 1 - 1 as "plist" | ||
tell application "MoneyMoney" to export transactions from date 1900-1-1 as "plist" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import startOfMonth from 'date-fns/startOfMonth'; | ||
import subMonths from 'date-fns/subMonths'; | ||
import isAfter from 'date-fns/isAfter'; | ||
import { getToday } from '../lib'; | ||
import { | ||
getAccounts, | ||
getTransactions, | ||
filterAccounts, | ||
Transaction, | ||
Account, | ||
} from '../moneymoney'; | ||
|
||
import { BudgetState, IncomeCategory, VERSION } from './Types'; | ||
|
||
function getStartBalance( | ||
startDate: Date, | ||
transactions: Transaction[], | ||
accounts: Account[], | ||
): number { | ||
const transactionsSinceStart = transactions.filter(({ bookingDate }) => | ||
isAfter(bookingDate, startDate), | ||
); | ||
const transactionBal = transactionsSinceStart.reduce( | ||
(m, { amount }) => m + amount, | ||
0, | ||
); | ||
const accountsBal = accounts.reduce((m, { balance }) => m + balance, 0); | ||
|
||
return accountsBal + transactionBal * -1; | ||
} | ||
|
||
function isLaterHalfOfMonth({ bookingDate }: Transaction) { | ||
return bookingDate.getDate() >= 15; | ||
} | ||
|
||
function getIncomeCategories(transactions: Transaction[]): IncomeCategory[] { | ||
const transactionsByCat = transactions.reduce((memo, transaction) => { | ||
const { categoryUuid, amount } = transaction; | ||
|
||
if (!memo[categoryUuid]) { | ||
memo[categoryUuid] = { transactions: [], balance: 0, hasNegative: false }; | ||
} | ||
|
||
memo[categoryUuid].transactions.push(transaction); | ||
memo[categoryUuid].balance += amount; | ||
if (amount < 0) { | ||
memo[categoryUuid].hasNegative = true; | ||
} | ||
|
||
return memo; | ||
}, {} as { [key: string]: { transactions: Transaction[]; balance: number; hasNegative: boolean } }); | ||
|
||
const positiveCats = Object.entries(transactionsByCat) | ||
.filter(([_, { hasNegative }]) => !hasNegative) | ||
.sort(([_, { balance: a }], [__, { balance: b }]) => b - a); | ||
|
||
return positiveCats.map(([id, { transactions }]) => ({ | ||
id, | ||
availableIn: transactions.some(isLaterHalfOfMonth) ? 1 : 0, | ||
})); | ||
} | ||
|
||
export default async function createInitialState(): Promise<BudgetState> { | ||
const [allAccounts, allTransactions] = await Promise.all([ | ||
getAccounts(), | ||
getTransactions(), | ||
]); | ||
const currenciesWithUsage = allAccounts.reduce( | ||
(memo, { group, currency }) => { | ||
if (group) { | ||
return memo; | ||
} | ||
memo[currency] = (memo[currency] || 0) + 1; | ||
return memo; | ||
}, | ||
{ USD: 1 } as { [key: string]: number }, | ||
); | ||
const currenciesByUsage = Object.entries(currenciesWithUsage) | ||
.sort(([_, a], [__, b]) => b - a) | ||
.map(([c]) => c); | ||
const currency = currenciesByUsage[0]; | ||
const accounts = filterAccounts(currency, allAccounts).filter( | ||
({ group, portfolio }) => !group && !portfolio, | ||
); | ||
const accountUuids = accounts.map(({ uuid }) => uuid); | ||
const transactionsOfAccounts = allTransactions.filter(({ accountUuid }) => | ||
accountUuids.includes(accountUuid), | ||
); | ||
const startDate = startOfMonth(subMonths(getToday(), 1)); | ||
|
||
return { | ||
name: '', | ||
version: VERSION, | ||
budgets: {}, | ||
settings: { | ||
currency, | ||
incomeCategories: getIncomeCategories(transactionsOfAccounts), | ||
accounts: accountUuids, | ||
fractionDigits: 2, | ||
startDate: startDate.getTime(), | ||
startBalance: getStartBalance( | ||
startDate, | ||
transactionsOfAccounts, | ||
accounts, | ||
), | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.