1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2025-06-13 21:48:39 -04:00

Split accounts and transactions into models

This commit is contained in:
2017-12-04 05:55:25 -05:00
parent f72c86ef58
commit 128ea57c4d
16 changed files with 568 additions and 553 deletions

118
internal/models/accounts.go Normal file
View File

@ -0,0 +1,118 @@
package models
import (
"encoding/json"
"net/http"
"strings"
)
type AccountType int64
const (
Bank AccountType = 1 // start at 1 so that the default (0) is invalid
Cash = 2
Asset = 3
Liability = 4
Investment = 5
Income = 6
Expense = 7
Trading = 8
Equity = 9
Receivable = 10
Payable = 11
)
var AccountTypes = []AccountType{
Bank,
Cash,
Asset,
Liability,
Investment,
Income,
Expense,
Trading,
Equity,
Receivable,
Payable,
}
func (t AccountType) String() string {
switch t {
case Bank:
return "Bank"
case Cash:
return "Cash"
case Asset:
return "Asset"
case Liability:
return "Liability"
case Investment:
return "Investment"
case Income:
return "Income"
case Expense:
return "Expense"
case Trading:
return "Trading"
case Equity:
return "Equity"
case Receivable:
return "Receivable"
case Payable:
return "Payable"
}
return ""
}
type Account struct {
AccountId int64
ExternalAccountId string
UserId int64
SecurityId int64
ParentAccountId int64 // -1 if this account is at the root
Type AccountType
Name string
// monotonically-increasing account transaction version number. Used for
// allowing a client to ensure they have a consistent version when paging
// through transactions.
AccountVersion int64 `json:"Version"`
// Optional fields specifying how to fetch transactions from a bank via OFX
OFXURL string
OFXORG string
OFXFID string
OFXUser string
OFXBankID string // OFX BankID (BrokerID if AcctType == Investment)
OFXAcctID string
OFXAcctType string // ofxgo.acctType
OFXClientUID string
OFXAppID string
OFXAppVer string
OFXVersion string
OFXNoIndent bool
}
type AccountList struct {
Accounts *[]Account `json:"accounts"`
}
func (a *Account) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(a)
}
func (a *Account) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(a)
}
func (al *AccountList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(al)
}
func (al *AccountList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(al)
}

View File

@ -0,0 +1,133 @@
package models
import (
"encoding/json"
"errors"
"math/big"
"net/http"
"strings"
"time"
)
// Split.Status
const (
Imported int64 = 1
Entered = 2
Cleared = 3
Reconciled = 4
Voided = 5
)
// Split.ImportSplitType
const (
Default int64 = 0
ImportAccount = 1 // This split belongs to the main account being imported
SubAccount = 2 // This split belongs to a sub-account of that being imported
ExternalAccount = 3
TradingAccount = 4
Commission = 5
Taxes = 6
Fees = 7
Load = 8
IncomeAccount = 9
ExpenseAccount = 10
)
type Split struct {
SplitId int64
TransactionId int64
Status int64
ImportSplitType int64
// One of AccountId and SecurityId must be -1
// In normal splits, AccountId will be valid and SecurityId will be -1. The
// only case where this is reversed is for transactions that have been
// imported and not yet associated with an account.
AccountId int64
SecurityId int64
RemoteId string // unique ID from server, for detecting duplicates
Number string // Check or reference number
Memo string
Amount string // String representation of decimal, suitable for passing to big.Rat.SetString()
}
func GetBigAmount(amt string) (*big.Rat, error) {
var r big.Rat
_, success := r.SetString(amt)
if !success {
return nil, errors.New("Couldn't convert string amount to big.Rat via SetString()")
}
return &r, nil
}
func (s *Split) GetAmount() (*big.Rat, error) {
return GetBigAmount(s.Amount)
}
func (s *Split) Valid() bool {
if (s.AccountId == -1) == (s.SecurityId == -1) {
return false
}
_, err := s.GetAmount()
return err == nil
}
type Transaction struct {
TransactionId int64
UserId int64
Description string
Date time.Time
Splits []*Split `db:"-"`
}
type TransactionList struct {
Transactions *[]Transaction `json:"transactions"`
}
type AccountTransactionsList struct {
Account *Account
Transactions *[]Transaction
TotalTransactions int64
BeginningBalance string
EndingBalance string
}
func (t *Transaction) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(t)
}
func (t *Transaction) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(t)
}
func (tl *TransactionList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(tl)
}
func (tl *TransactionList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(tl)
}
func (atl *AccountTransactionsList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(atl)
}
func (atl *AccountTransactionsList) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(atl)
}
func (t *Transaction) Valid() bool {
for i := range t.Splits {
if !t.Splits[i].Valid() {
return false
}
}
return true
}