From e9657e5acd95021133578c2647d6808c57492a3f Mon Sep 17 00:00:00 2001 From: Anantha Kumaran Date: Sat, 20 Aug 2022 15:47:18 +0530 Subject: [PATCH] show expense in yearly card --- docs/src/accounts.md | 2 +- internal/server/investment.go | 29 ++++++++++++++++++----------- web/src/investment.ts | 16 +++++++++++----- web/src/utils.ts | 3 ++- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/docs/src/accounts.md b/docs/src/accounts.md index 44f85a8..448db4f 100644 --- a/docs/src/accounts.md +++ b/docs/src/accounts.md @@ -21,4 +21,4 @@ All your income should come from `Income:`. ### Tax -Income tax paid to government should be credited to `Tax` account +Income tax paid to government should be credited to `Expenses:Tax` account diff --git a/internal/server/investment.go b/internal/server/investment.go index f8ec5af..3a5944a 100644 --- a/internal/server/investment.go +++ b/internal/server/investment.go @@ -5,7 +5,6 @@ import ( "time" "github.com/ananthakumaran/paisa/internal/model/posting" - "github.com/ananthakumaran/paisa/internal/service" "github.com/ananthakumaran/paisa/internal/utils" "github.com/gin-gonic/gin" "github.com/samber/lo" @@ -22,24 +21,24 @@ type YearlyCard struct { NetTax float64 `json:"net_tax"` NetIncome float64 `json:"net_income"` NetInvestment float64 `json:"net_investment"` + NetExpense float64 `json:"net_expense"` } func GetInvestment(db *gorm.DB) gin.H { - var postings []posting.Posting + var assets []posting.Posting var incomes []posting.Posting - var taxes []posting.Posting - result := db.Where("account like ? order by date asc", "Assets:%").Find(&postings) + var expenses []posting.Posting + result := db.Where("account like ? order by date asc", "Assets:%").Find(&assets) if result.Error != nil { log.Fatal(result.Error) } - postings = lo.Filter(postings, func(p posting.Posting, _ int) bool { return !service.IsInterest(db, p) }) result = db.Where("account like ? order by date asc", "Income:%").Find(&incomes) if result.Error != nil { log.Fatal(result.Error) } - result = db.Where("account = ? order by date asc", "Tax").Find(&taxes) + result = db.Where("account like ? order by date asc", "Expenses:%").Find(&expenses) if result.Error != nil { log.Fatal(result.Error) } @@ -50,10 +49,10 @@ func GetInvestment(db *gorm.DB) gin.H { log.Fatal(result.Error) } - return gin.H{"postings": postings, "yearly_cards": computeYearlyCard(p.Date, postings, taxes, incomes)} + return gin.H{"assets": assets, "yearly_cards": computeYearlyCard(p.Date, assets, expenses, incomes)} } -func computeYearlyCard(start time.Time, assets []posting.Posting, taxes []posting.Posting, incomes []posting.Posting) []YearlyCard { +func computeYearlyCard(start time.Time, assets []posting.Posting, expenses []posting.Posting, incomes []posting.Posting) []YearlyCard { var yearlyCards []YearlyCard = make([]YearlyCard, 0) if len(assets) == 0 { @@ -71,12 +70,19 @@ func computeYearlyCard(start time.Time, assets []posting.Posting, taxes []postin } var currentYearTaxes []posting.Posting = make([]posting.Posting, 0) - for len(taxes) > 0 && utils.IsWithDate(taxes[0].Date, start, yearEnd) { - p, taxes = taxes[0], taxes[1:] - currentYearTaxes = append(currentYearTaxes, p) + var currentYearExpenses []posting.Posting = make([]posting.Posting, 0) + + for len(expenses) > 0 && utils.IsWithDate(expenses[0].Date, start, yearEnd) { + p, expenses = expenses[0], expenses[1:] + if p.Account == "Expenses:Tax" { + currentYearTaxes = append(currentYearTaxes, p) + } else { + currentYearExpenses = append(currentYearExpenses, p) + } } netTax := lo.SumBy(currentYearTaxes, func(p posting.Posting) float64 { return p.Amount }) + netExpense := lo.SumBy(currentYearExpenses, func(p posting.Posting) float64 { return p.Amount }) var currentYearIncomes []posting.Posting = make([]posting.Posting, 0) for len(incomes) > 0 && utils.IsWithDate(incomes[0].Date, start, yearEnd) { @@ -110,6 +116,7 @@ func computeYearlyCard(start time.Time, assets []posting.Posting, taxes []postin GrossOtherIncome: grossOtherIncome, NetIncome: grossSalaryIncome + grossOtherIncome - netTax, NetInvestment: netInvestment, + NetExpense: netExpense, }) } diff --git a/web/src/investment.ts b/web/src/investment.ts index 548ff57..38a529e 100644 --- a/web/src/investment.ts +++ b/web/src/investment.ts @@ -16,15 +16,15 @@ import { } from "./utils"; export default async function () { - const { postings: postings, yearly_cards: yearlyCards } = await ajax( + const { assets: assets, yearly_cards: yearlyCards } = await ajax( "/api/investment" ); - _.each(postings, (p) => (p.timestamp = dayjs(p.date))); + _.each(assets, (p) => (p.timestamp = dayjs(p.date))); _.each(yearlyCards, (c) => { c.start_date_timestamp = dayjs(c.start_date); c.end_date_timestamp = dayjs(c.end_date); }); - renderMonthlyInvestmentTimeline(postings); + renderMonthlyInvestmentTimeline(assets); renderYearlyInvestmentTimeline(yearlyCards); renderYearlyCards(yearlyCards); } @@ -433,7 +433,7 @@ function renderYearlyCards(yearlyCards: YearlyCard[]) { )} - Net Tax + Tax ${formatCurrency( card.net_tax )} @@ -445,7 +445,13 @@ function renderYearlyCards(yearlyCards: YearlyCard[]) { )} - Net Investment + Net Expense + ${formatCurrency( + card.net_expense + )} + + + Investment ${formatCurrency( card.net_investment )} diff --git a/web/src/utils.ts b/web/src/utils.ts index 3a76571..66a4ed9 100644 --- a/web/src/utils.ts +++ b/web/src/utils.ts @@ -78,6 +78,7 @@ export interface YearlyCard { gross_other_income: number; net_income: number; net_investment: number; + net_expense: number; start_date_timestamp: dayjs.Dayjs; end_date_timestamp: dayjs.Dayjs; @@ -85,7 +86,7 @@ export interface YearlyCard { export function ajax( route: "/api/investment" -): Promise<{ postings: Posting[]; yearly_cards: YearlyCard[] }>; +): Promise<{ assets: Posting[]; yearly_cards: YearlyCard[] }>; export function ajax( route: "/api/ledger" ): Promise<{ postings: Posting[]; breakdowns: Breakdown[] }>;