show expense in yearly card
This commit is contained in:
parent
eade476b57
commit
e9657e5acd
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
|
||||
}
|
||||
|
|
|
@ -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[]) {
|
|||
)}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Net Tax</td>
|
||||
<td>Tax</td>
|
||||
<td class='has-text-right has-text-weigh-bold'>${formatCurrency(
|
||||
card.net_tax
|
||||
)}</td>
|
||||
|
@ -445,7 +445,13 @@ function renderYearlyCards(yearlyCards: YearlyCard[]) {
|
|||
)}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Net Investment</td>
|
||||
<td>Net Expense</td>
|
||||
<td class='has-text-right has-text-weigh-bold'>${formatCurrency(
|
||||
card.net_expense
|
||||
)}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Investment</td>
|
||||
<td class='has-text-right has-text-weigh-bold'>${formatCurrency(
|
||||
card.net_investment
|
||||
)}</td>
|
||||
|
|
|
@ -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[] }>;
|
||||
|
|
Loading…
Reference in New Issue