Skip to content

Commit

Permalink
Merge pull request #8 from darcys22/dev
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
darcys22 authored Apr 20, 2020
2 parents 65368ec + 1a08583 commit a9013ca
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 161 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,4 @@ SELECT * FROM accounts where account_id in (select account_id from account_tag w
### TODO
- Create Yurnell - programmable journal entries
- Add an edit transaction function
- Add MySQL as a database
- test releases from scratch
- run GoDBLedger on a separate server and access the open port through the network
48 changes: 47 additions & 1 deletion godbledger/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type LedgerConfig struct {
LogVerbosity string // LogVerbosity defines the logging level {debug, info, warn, error, fatal, panic}
ConfigFile string // Location of the TOML config file, including directory path
DatabaseType string // Type of Database being used
DatabaseLocation string // Location of the database file, including directory path
DatabaseLocation string // Location of the database file, including directory path or connection string
}

var (
Expand All @@ -33,6 +33,21 @@ var (
Description: `The dumpconfig command shows configuration values.`,
}

InitConfigCommand = &cli.Command{
Action: initConfig,
Name: "init",
Usage: "godbledger init [-m] [databaseLocation]",
ArgsUsage: "",
Category: "MISCELLANEOUS COMMANDS",
Description: `The init command creates configuration file.`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "mysql",
Aliases: []string{"m"},
Usage: "set the database to use mysql rather than sqlite"},
},
}

defaultLedgerConfig = &LedgerConfig{
RPCPort: "50051",
DataDirectory: DefaultDataDir(),
Expand Down Expand Up @@ -128,3 +143,34 @@ func dumpConfig(ctx *cli.Context) error {

return nil
}

// initConfig is the init command.
func initConfig(ctx *cli.Context) error {

config := defaultLedgerConfig
if ctx.Bool("mysql") {
config.DatabaseType = "mysql"
config.DatabaseLocation = "godbledger:password@tcp(127.0.0.1:3306)/ledger?charset=utf8"
}

if len(ctx.Args().Get(0)) > 0 {
config.DatabaseLocation = ctx.Args().Get(0)
}
_, err := os.Stat(config.ConfigFile)
if os.IsNotExist(err) {
log.Infof("Config File doesn't exist creating at %s", config.ConfigFile)
os.MkdirAll(filepath.Dir(config.ConfigFile), os.ModePerm)
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(config); err != nil {
return err
}
dump, err := os.OpenFile(config.ConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer dump.Close()
dump.Write(buf.Bytes())
}

return nil
}
19 changes: 4 additions & 15 deletions godbledger/core/trialbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@ package core

import ()

type Tag struct {
Name string `json:"Name"`
Total int `json:"Total"`
Accounts []PDFAccount `json:"Accounts"`
}

type PDFAccount struct {
Account string `json:"Account"`
Amount int `json:"Amount"`
}

var Reporteroutput struct {
Data []Tag `json:"Tags"`
Profit int `json:"Profit"`
NetAssets int `json:"NetAssets"`
type TBAccount struct {
Account string `json:"Account"`
Amount int `json:"Amount"`
Tags []string `json:"Tags"`
}
2 changes: 1 addition & 1 deletion godbledger/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ type Database interface {
FindUser(pubKey string) (*core.User, error)
AddUser(usr *core.User) error
SafeAddUser(usr *core.User) error
GetTB(date time.Time) error
GetTB(date time.Time) (*[]core.TBAccount, error)
Query(query string, args ...interface{}) (*sql.Rows, error)
}
79 changes: 40 additions & 39 deletions godbledger/db/mysql/mysqlfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,63 +427,64 @@ func (db *Database) TestDB() error {
return err
}

func (db *Database) GetTB(date time.Time) error {
func (db *Database) GetTB(queryDate time.Time) (*[]core.TBAccount, error) {

queryDB := `
SELECT
tags.tag_name,
Table_Aggregate.account_id,
sums
FROM account_tag
join ((SELECT
split_accounts.account_id as account_id,
SUM(splits.amount) as sums
FROM splits
JOIN split_accounts
ON splits.split_id = split_accounts.split_id
GROUP BY split_accounts.account_id
)) AS Table_Aggregate
on account_tag.account_id = Table_Aggregate.account_id
join tags
on tags.tag_id = account_tag.tag_id
order BY tags.tag_name
SELECT
split_accounts.account_id,
SUM(splits.amount)
FROM splits
JOIN split_accounts
ON splits.split_id = split_accounts.split_id
WHERE splits.split_date <= ?
GROUP BY split_accounts.account_id
;`

rows, err := db.DB.Query(queryDB)
log.Debug("Querying Database for Trial Balance")

rows, err := db.DB.Query(queryDB, queryDate)
if err != nil {
log.Debug(err)
return err
log.Fatal(err)
}
defer rows.Close()

accounts := make(map[string][]*core.PDFAccount)
totals := make(map[string]int)
accounts := []core.TBAccount{}

for rows.Next() {
var t *core.PDFAccount
var name string
if err := rows.Scan(&name, &t.Account, &t.Amount); err != nil {
var t core.TBAccount
if err := rows.Scan(&t.Account, &t.Amount); err != nil {
log.Fatal(err)
}
log.Debugf("%v", t)
if val, ok := accounts[name]; ok {
accounts[name] = append(val, t)
totals[name] = totals[name] + t.Amount
} else {
accounts[name] = []*core.PDFAccount{t}
totals[name] = t.Amount
}
accounts = append(accounts, t)
}
if rows.Err() != nil {
log.Fatal(err)
}

//for k, v := range accounts {
//reporteroutput.Data = append(reporteroutput.Data, Tag{k, totals[k], v})
//}
tagsQuery := `
SELECT tag_name
FROM tags
JOIN account_tag
ON account_tag.tag_id = tags.tag_id
JOIN accounts
ON accounts.account_id = account_tag.account_id
WHERE accounts.NAME = ?;
`

for index, element := range accounts {
rows, err = db.DB.Query(tagsQuery, element.Account)

for rows.Next() {
var tag string
if err := rows.Scan(&tag); err != nil {
log.Fatal(err)
}
accounts[index].Tags = append(accounts[index].Tags, tag)
}

return nil
}

return &accounts, nil

}

Expand Down
82 changes: 42 additions & 40 deletions godbledger/db/sqlite3/sqlite3funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,66 +427,68 @@ func (db *Database) TestDB() error {
return err
}

func (db *Database) GetTB(date time.Time) error {
func (db *Database) GetTB(queryDate time.Time) (*[]core.TBAccount, error) {

queryDB := `
SELECT
tags.tag_name,
Table_Aggregate.account_id,
sums
FROM account_tag
join ((SELECT
split_accounts.account_id as account_id,
SUM(splits.amount) as sums
FROM splits
JOIN split_accounts
ON splits.split_id = split_accounts.split_id
GROUP BY split_accounts.account_id
)) AS Table_Aggregate
on account_tag.account_id = Table_Aggregate.account_id
join tags
on tags.tag_id = account_tag.tag_id
order BY tags.tag_name
SELECT
split_accounts.account_id,
SUM(splits.amount)
FROM splits
JOIN split_accounts
ON splits.split_id = split_accounts.split_id
WHERE splits.split_date <= ?
GROUP BY split_accounts.account_id
;`

rows, err := db.DB.Query(queryDB)
log.Debug("Querying Database for Trial Balance")

rows, err := db.DB.Query(queryDB, queryDate)
if err != nil {
log.Debug(err)
return err
log.Fatal(err)
}
defer rows.Close()

accounts := make(map[string][]*core.PDFAccount)
totals := make(map[string]int)
accounts := []core.TBAccount{}

for rows.Next() {
var t *core.PDFAccount
var name string
if err := rows.Scan(&name, &t.Account, &t.Amount); err != nil {
var t core.TBAccount
if err := rows.Scan(&t.Account, &t.Amount); err != nil {
log.Fatal(err)
}
log.Debugf("%v", t)
if val, ok := accounts[name]; ok {
accounts[name] = append(val, t)
totals[name] = totals[name] + t.Amount
} else {
accounts[name] = []*core.PDFAccount{t}
totals[name] = t.Amount
}
accounts = append(accounts, t)
}
if rows.Err() != nil {
log.Fatal(err)
}

//for k, v := range accounts {
////reporteroutput.Data = append(reporteroutput.Data, Tag{k, totals[k], v})
//}
tagsQuery := `
SELECT tag_name
FROM tags
JOIN account_tag
ON account_tag.tag_id = tags.tag_id
JOIN accounts
ON accounts.account_id = account_tag.account_id
WHERE accounts.NAME = ?;
`

for index, element := range accounts {
rows, err = db.DB.Query(tagsQuery, element.Account)

for rows.Next() {
var tag string
if err := rows.Scan(&tag); err != nil {
log.Fatal(err)
}
accounts[index].Tags = append(accounts[index].Tags, tag)
}

}

return &accounts, nil

return nil
}

func (db *Database) Query(query string, args ...interface{}) (*sql.Rows, error) {
return db.Query(query, args...)
return db.DB.Query(query, args...)

}
6 changes: 2 additions & 4 deletions godbledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,8 @@ func (l *Ledger) GetAccounts(txn *core.Transaction) ([]*core.Account, error) {
return accounts, nil
}

func (l *Ledger) GetTB(date time.Time) (int, error) {
//accounts := []*core.Account{}

return 1, nil
func (l *Ledger) GetTB(date time.Time) (*[]core.TBAccount, error) {
return l.LedgerDb.GetTB(date)
}

func (l *Ledger) Start() {
Expand Down
1 change: 1 addition & 0 deletions godbledger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
app.Commands = []*cli.Command{
// See config.go
cmd.DumpConfigCommand,
cmd.InitConfigCommand,
}

app.Flags = []cli.Flag{
Expand Down
17 changes: 15 additions & 2 deletions godbledger/rpc/ledger_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,20 @@ func (s *LedgerServer) DeleteTag(ctx context.Context, in *pb.DeleteTagRequest) (

func (s *LedgerServer) GetTB(ctx context.Context, in *pb.TBRequest) (*pb.TBResponse, error) {
log.Info("Received New TB Request")
s.ld.GetTB(time.Now())
accounts, err := s.ld.GetTB(time.Now())

return &pb.TBResponse{}, nil
//log.Debug(accounts)

response := pb.TBResponse{}

for _, account := range *accounts {
response.Lines = append(response.Lines,
&pb.TBLine{
Accountname: account.Account,
Amount: int64(account.Amount),
Tags: account.Tags,
})
}

return &response, err
}
8 changes: 4 additions & 4 deletions godbledger/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
)

const (
VersionMajor = 0 // Major version component of the current release
VersionMinor = 1 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionMeta = "unstable" // Version metadata to append to the version string
VersionMajor = 0 // Major version component of the current release
VersionMinor = 2 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionMeta = "alpha" // Version metadata to append to the version string
)

// Version holds the textual version string.
Expand Down
Loading

0 comments on commit a9013ca

Please sign in to comment.