Skip to content

Commit

Permalink
take cost inflation index when calculating tax
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Sep 11, 2022
1 parent 22affa2 commit 6e9df8e
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 10 deletions.
5 changes: 3 additions & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,17 @@ commodities:
type: mutualfund
code: 120716
harvest: 365
grandfather: 2018-01-31
tax_category: equity
- name: NIFTY_JR
type: mutualfund
code: 120684
harvest: 365
grandfather: 2018-01-31
tax_category: equity
- name: ABCBF
type: mutualfund
code: 119533
harvest: 1095
tax_category: debt
- name: NPS_HDFC_E
type: nps
code: SM008001
Expand Down
4 changes: 4 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ var updateCmd = &cobra.Command{
if syncAll || updateCommodities {
model.SyncCommodities(db)
}

if syncAll {
model.SyncCII(db)
}
},
}

Expand Down
42 changes: 42 additions & 0 deletions internal/model/cii/cii.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cii

import (
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)

type CII struct {
ID uint `gorm:"primaryKey" json:"id"`
FinancialYear string `json:"financial_year"`
CostInflationIndex uint `json:"cost_inflation_index"`
}

func UpsertAll(db *gorm.DB, ciis []*CII) {
err := db.Transaction(func(tx *gorm.DB) error {
err := tx.Exec("DELETE FROM ciis").Error
if err != nil {
return err
}
for _, cii := range ciis {
err := tx.Create(cii).Error
if err != nil {
return err
}
}

return nil
})

if err != nil {
log.Fatal(err)
}
}

func GetIndex(db *gorm.DB, financialYear string) uint {
var cii CII
result := db.Where("financial_year = ?", financialYear).First(&cii)
if result.Error != nil {
log.Fatal(result.Error)
}
return cii.CostInflationIndex
}
12 changes: 12 additions & 0 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package model

import (
"github.com/ananthakumaran/paisa/internal/ledger"
"github.com/ananthakumaran/paisa/internal/model/cii"
"github.com/ananthakumaran/paisa/internal/model/commodity"
"github.com/ananthakumaran/paisa/internal/model/posting"
"github.com/ananthakumaran/paisa/internal/model/price"
"github.com/ananthakumaran/paisa/internal/scraper/india"
"github.com/ananthakumaran/paisa/internal/scraper/mutualfund"
"github.com/ananthakumaran/paisa/internal/scraper/nps"
"github.com/logrusorgru/aurora"
Expand Down Expand Up @@ -48,3 +50,13 @@ func SyncCommodities(db *gorm.DB) {
price.UpsertAll(db, commodity.Type, schemeCode, prices)
}
}

func SyncCII(db *gorm.DB) {
db.AutoMigrate(&cii.CII{})
log.Info("Fetching taxation related info")
ciis, err := india.GetCostInflationIndex()
if err != nil {
log.Fatal(err)
}
cii.UpsertAll(db, ciis)
}
2 changes: 1 addition & 1 deletion internal/model/mutualfund/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func FindScheme(db *gorm.DB, amc string, NAVName string) Scheme {
var scheme Scheme
result := db.Where("amc = ? and nav_name = ?", amc, NAVName).First(&scheme)
if result.Error != nil {
log.Fatal(result)
log.Fatal(result.Error)
}
return scheme
}
2 changes: 1 addition & 1 deletion internal/model/nps/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func FindScheme(db *gorm.DB, pfm string, schemeName string) Scheme {
var scheme Scheme
result := db.Where("pfm_name = ? and scheme_name = ?", pfm, schemeName).First(&scheme)
if result.Error != nil {
log.Fatal(result)
log.Fatal(result.Error)
}
return scheme
}
47 changes: 47 additions & 0 deletions internal/scraper/india/cii.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package india

import (
"net/http"

"encoding/json"
"io/ioutil"

"github.com/ananthakumaran/paisa/internal/model/cii"
log "github.com/sirupsen/logrus"
)

func GetCostInflationIndex() ([]*cii.CII, error) {
log.Info("Fetching Cost Inflation Index from Purified Bytes")
resp, err := http.Get("https://india.purifiedbytes.com/api/cii/v2.json")
if err != nil {
return nil, err
}
defer resp.Body.Close()

respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

type CII struct {
FinancialYear string `json:"financial_year"`
CostInflationIndex uint `json:"cost_inflation_index"`
}
type Result struct {
Data []CII
}

var result Result
err = json.Unmarshal(respBytes, &result)
if err != nil {
return nil, err
}

var ciis []*cii.CII
for _, s := range result.Data {
c := cii.CII{FinancialYear: s.FinancialYear, CostInflationIndex: s.CostInflationIndex}
ciis = append(ciis, &c)

}
return ciis, nil
}
5 changes: 3 additions & 2 deletions internal/scraper/mutualfund/nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package mutualfund
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"strconv"
"time"

"net/http"

"github.com/ananthakumaran/paisa/internal/model/price"
)

func GetNav(schemeCode string, commodityName string) ([]*price.Price, error) {
log.Info("Fetching Mutual Fund nav from mfapi.in")
url := fmt.Sprintf("https://api.mfapi.in/mf/%s", schemeCode)
resp, err := http.Get(url)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/scraper/nps/nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package nps
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"time"

"net/http"
"time"

"github.com/ananthakumaran/paisa/internal/model/price"
)

func GetNav(schemeCode string, commodityName string) ([]*price.Price, error) {
log.Info("Fetching NPS Fund nav from Purified Bytes")
url := fmt.Sprintf("https://nps.purifiedbytes.com/api/schemes/%s/nav.json", schemeCode)
resp, err := http.Get(url)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion internal/server/harvest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"time"

"github.com/ananthakumaran/paisa/internal/model/cii"
c "github.com/ananthakumaran/paisa/internal/model/commodity"
"github.com/ananthakumaran/paisa/internal/model/posting"
"github.com/ananthakumaran/paisa/internal/query"
Expand Down Expand Up @@ -48,7 +49,8 @@ type CapitalGain struct {
Harvestable Harvestable `json:"harvestable"`
}

var EQUITY_GRANDFATHER_DATE, _ = time.Parse("2006-01-02", "2018-01-31")
var EQUITY_GRANDFATHER_DATE, _ = time.Parse("2006-01-02", "2018-02-01")
var CII_START_DATE, _ = time.Parse("2006-01-02", "2001-03-31")

func GetHarvest(db *gorm.DB) gin.H {
commodities := lo.Filter(c.All(), func(c c.Commodity, _ int) bool {
Expand Down Expand Up @@ -118,6 +120,10 @@ func computeCapitalGains(db *gorm.DB, account string, commodity c.Commodity, pos
if grandfather && p.Date.Before(EQUITY_GRANDFATHER_DATE) {
taxableGain = grandfatherUnitPrice*p.Quantity - p.Amount
}

if commodity.TaxCategory == c.Debt && p.Date.After(CII_START_DATE) {
taxableGain = currentPrice.Value*p.Quantity - (p.Amount*float64(cii.GetIndex(db, utils.FY(today))))/float64(cii.GetIndex(db, utils.FY(p.Date)))
}
harvestable.HarvestableUnits += p.Quantity
harvestable.UnrealizedGain += gain
harvestable.TaxableUnrealizedGain += taxableGain
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func BTreeDescendFirstLessOrEqual[I btree.Item](tree *btree.BTree, item I) I {

func FY(date time.Time) string {
if date.Month() < time.April {
return fmt.Sprintf("%d-%d", date.Year()+1, date.Year()%100)
return fmt.Sprintf("%d-%d", date.Year()-1, date.Year()%100)
} else {
return fmt.Sprintf("%d-%d", date.Year(), (date.Year()+1)%100)
}
Expand Down

0 comments on commit 6e9df8e

Please sign in to comment.