2017-10-04 19:35:59 -04:00
package handlers
2015-06-25 22:36:58 -04:00
2015-06-27 17:46:06 -04:00
import (
"encoding/json"
"errors"
2015-07-11 08:58:36 -04:00
"gopkg.in/gorp.v1"
2015-06-27 17:46:06 -04:00
"log"
"net/http"
2015-07-11 08:58:36 -04:00
"regexp"
2015-06-27 17:46:06 -04:00
"strings"
)
2017-02-08 05:40:51 -05:00
type AccountType int64
2015-06-25 22:36:58 -04:00
const (
2017-02-08 05:40:51 -05:00
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
2015-06-25 22:36:58 -04:00
)
2017-02-08 05:40:51 -05:00
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 ""
}
2015-06-25 22:36:58 -04:00
type Account struct {
2016-02-02 21:46:27 -05:00
AccountId int64
ExternalAccountId string
UserId int64
SecurityId int64
ParentAccountId int64 // -1 if this account is at the root
2017-02-08 05:40:51 -05:00
Type AccountType
2016-02-02 21:46:27 -05:00
Name string
2015-07-11 08:58:36 -04:00
// monotonically-increasing account transaction version number. Used for
// allowing a client to ensure they have a consistent version when paging
// through transactions.
2015-08-05 21:25:25 -04:00
AccountVersion int64 ` json:"Version" `
2017-05-31 21:04:01 -04:00
// 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
2015-06-27 17:46:06 -04:00
}
type AccountList struct {
Accounts * [ ] Account ` json:"accounts" `
}
2015-07-11 08:58:36 -04:00
var accountTransactionsRE * regexp . Regexp
2016-02-02 21:46:27 -05:00
var accountImportRE * regexp . Regexp
2015-07-11 08:58:36 -04:00
func init ( ) {
accountTransactionsRE = regexp . MustCompile ( ` ^/account/[0-9]+/transactions/?$ ` )
2016-02-15 11:28:44 -05:00
accountImportRE = regexp . MustCompile ( ` ^/account/[0-9]+/import/[a-z]+/?$ ` )
2015-07-11 08:58:36 -04:00
}
2015-06-27 17:46:06 -04:00
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 )
}
2017-10-04 08:05:51 -04:00
func GetAccount ( db * DB , accountid int64 , userid int64 ) ( * Account , error ) {
2015-06-27 17:46:06 -04:00
var a Account
2017-10-04 08:05:51 -04:00
err := db . SelectOne ( & a , "SELECT * from accounts where UserId=? AND AccountId=?" , userid , accountid )
2015-06-27 17:46:06 -04:00
if err != nil {
return nil , err
}
return & a , nil
}
2015-07-11 08:58:36 -04:00
func GetAccountTx ( transaction * gorp . Transaction , accountid int64 , userid int64 ) ( * Account , error ) {
var a Account
err := transaction . SelectOne ( & a , "SELECT * from accounts where UserId=? AND AccountId=?" , userid , accountid )
if err != nil {
return nil , err
}
return & a , nil
}
2017-10-04 08:05:51 -04:00
func GetAccounts ( db * DB , userid int64 ) ( * [ ] Account , error ) {
2015-06-27 17:46:06 -04:00
var accounts [ ] Account
2017-10-04 08:05:51 -04:00
_ , err := db . Select ( & accounts , "SELECT * from accounts where UserId=?" , userid )
2015-06-27 17:46:06 -04:00
if err != nil {
return nil , err
}
return & accounts , nil
}
2016-02-15 11:28:44 -05:00
// Get (and attempt to create if it doesn't exist). Matches on UserId,
// SecurityId, Type, Name, and ParentAccountId
func GetCreateAccountTx ( transaction * gorp . Transaction , a Account ) ( * Account , error ) {
var accounts [ ] Account
2016-02-10 18:36:11 -05:00
var account Account
// Try to find the top-level trading account
2016-02-15 11:28:44 -05:00
_ , err := transaction . Select ( & accounts , "SELECT * from accounts where UserId=? AND SecurityId=? AND Type=? AND Name=? AND ParentAccountId=? ORDER BY AccountId ASC LIMIT 1" , a . UserId , a . SecurityId , a . Type , a . Name , a . ParentAccountId )
2016-02-10 18:36:11 -05:00
if err != nil {
return nil , err
}
if len ( accounts ) == 1 {
account = accounts [ 0 ]
} else {
2016-02-15 11:28:44 -05:00
account . UserId = a . UserId
account . SecurityId = a . SecurityId
account . Type = a . Type
account . Name = a . Name
account . ParentAccountId = a . ParentAccountId
2016-02-10 18:36:11 -05:00
err = transaction . Insert ( & account )
2016-02-11 05:53:44 -05:00
if err != nil {
return nil , err
}
}
return & account , nil
}
// Get (and attempt to create if it doesn't exist) the security/currency
2016-02-15 11:28:44 -05:00
// trading account for the supplied security/currency
func GetTradingAccount ( transaction * gorp . Transaction , userid int64 , securityid int64 ) ( * Account , error ) {
var tradingAccount Account
2016-02-11 05:53:44 -05:00
var account Account
2017-09-21 21:00:30 -04:00
user , err := GetUserTx ( transaction , userid )
if err != nil {
return nil , err
}
2016-02-15 11:28:44 -05:00
tradingAccount . UserId = userid
tradingAccount . Type = Trading
tradingAccount . Name = "Trading"
2017-09-21 21:00:30 -04:00
tradingAccount . SecurityId = user . DefaultCurrency
2016-02-15 11:28:44 -05:00
tradingAccount . ParentAccountId = - 1
// Find/create the top-level trading account
ta , err := GetCreateAccountTx ( transaction , tradingAccount )
2016-02-11 05:53:44 -05:00
if err != nil {
return nil , err
}
2017-09-20 21:30:17 -04:00
security , err := GetSecurityTx ( transaction , securityid , userid )
2016-10-16 08:19:11 -04:00
if err != nil {
return nil , err
}
2016-02-15 11:28:44 -05:00
account . UserId = userid
account . Name = security . Name
account . ParentAccountId = ta . AccountId
account . SecurityId = securityid
account . Type = Trading
a , err := GetCreateAccountTx ( transaction , account )
2016-02-11 05:53:44 -05:00
if err != nil {
return nil , err
}
2016-02-15 11:28:44 -05:00
return a , nil
}
// Get (and attempt to create if it doesn't exist) the security/currency
// imbalance account for the supplied security/currency
func GetImbalanceAccount ( transaction * gorp . Transaction , userid int64 , securityid int64 ) ( * Account , error ) {
var imbalanceAccount Account
var account Account
2017-06-04 16:01:42 -04:00
xxxtemplate := FindSecurityTemplate ( "XXX" , Currency )
if xxxtemplate == nil {
return nil , errors . New ( "Couldn't find XXX security template" )
}
xxxsecurity , err := ImportGetCreateSecurity ( transaction , userid , xxxtemplate )
if err != nil {
return nil , errors . New ( "Couldn't create XXX security" )
}
2016-02-11 05:53:44 -05:00
2016-02-15 11:28:44 -05:00
imbalanceAccount . UserId = userid
imbalanceAccount . Name = "Imbalances"
imbalanceAccount . ParentAccountId = - 1
2017-06-04 16:01:42 -04:00
imbalanceAccount . SecurityId = xxxsecurity . SecurityId
2016-02-15 11:28:44 -05:00
imbalanceAccount . Type = Bank
// Find/create the top-level trading account
ia , err := GetCreateAccountTx ( transaction , imbalanceAccount )
2016-02-11 05:53:44 -05:00
if err != nil {
return nil , err
}
2017-06-04 16:01:42 -04:00
security , err := GetSecurityTx ( transaction , securityid , userid )
2016-10-16 08:19:11 -04:00
if err != nil {
return nil , err
}
2016-02-15 11:28:44 -05:00
account . UserId = userid
account . Name = security . Name
account . ParentAccountId = ia . AccountId
account . SecurityId = securityid
account . Type = Bank
2016-02-10 18:36:11 -05:00
2016-02-15 11:28:44 -05:00
a , err := GetCreateAccountTx ( transaction , account )
2016-02-10 18:36:11 -05:00
if err != nil {
return nil , err
}
2016-02-15 11:28:44 -05:00
return a , nil
2016-02-10 18:36:11 -05:00
}
2015-06-27 17:46:06 -04:00
type ParentAccountMissingError struct { }
func ( pame ParentAccountMissingError ) Error ( ) string {
return "Parent account missing"
}
2017-10-04 08:05:51 -04:00
func insertUpdateAccount ( db * DB , a * Account , insert bool ) error {
transaction , err := db . Begin ( )
2015-06-27 17:46:06 -04:00
if err != nil {
return err
}
if a . ParentAccountId != - 1 {
existing , err := transaction . SelectInt ( "SELECT count(*) from accounts where AccountId=?" , a . ParentAccountId )
if err != nil {
transaction . Rollback ( )
return err
}
if existing != 1 {
transaction . Rollback ( )
return ParentAccountMissingError { }
}
}
if insert {
err = transaction . Insert ( a )
if err != nil {
transaction . Rollback ( )
return err
}
} else {
2015-07-11 08:58:36 -04:00
oldacct , err := GetAccountTx ( transaction , a . AccountId , a . UserId )
if err != nil {
transaction . Rollback ( )
return err
}
2015-08-05 21:25:25 -04:00
a . AccountVersion = oldacct . AccountVersion + 1
2015-07-11 08:58:36 -04:00
2015-06-27 17:46:06 -04:00
count , err := transaction . Update ( a )
if err != nil {
transaction . Rollback ( )
return err
}
if count != 1 {
transaction . Rollback ( )
return errors . New ( "Updated more than one account" )
}
}
err = transaction . Commit ( )
if err != nil {
transaction . Rollback ( )
return err
}
return nil
}
2017-10-04 08:05:51 -04:00
func InsertAccount ( db * DB , a * Account ) error {
return insertUpdateAccount ( db , a , true )
2015-06-27 17:46:06 -04:00
}
2017-10-04 08:05:51 -04:00
func UpdateAccount ( db * DB , a * Account ) error {
return insertUpdateAccount ( db , a , false )
2015-06-27 17:46:06 -04:00
}
2017-10-04 08:05:51 -04:00
func DeleteAccount ( db * DB , a * Account ) error {
transaction , err := db . Begin ( )
2015-06-29 07:25:48 -04:00
if err != nil {
return err
}
2015-07-04 21:11:00 -04:00
if a . ParentAccountId != - 1 {
// Re-parent splits to this account's parent account if this account isn't a root account
_ , err = transaction . Exec ( "UPDATE splits SET AccountId=? WHERE AccountId=?" , a . ParentAccountId , a . AccountId )
if err != nil {
transaction . Rollback ( )
return err
}
} else {
// Delete splits if this account is a root account
_ , err = transaction . Exec ( "DELETE FROM splits WHERE AccountId=?" , a . AccountId )
if err != nil {
transaction . Rollback ( )
return err
}
2015-06-29 07:25:48 -04:00
}
// Re-parent child accounts to this account's parent account
_ , err = transaction . Exec ( "UPDATE accounts SET ParentAccountId=? WHERE ParentAccountId=?" , a . ParentAccountId , a . AccountId )
if err != nil {
transaction . Rollback ( )
return err
}
count , err := transaction . Delete ( a )
if err != nil {
transaction . Rollback ( )
return err
}
if count != 1 {
transaction . Rollback ( )
return errors . New ( "Was going to delete more than one account" )
}
err = transaction . Commit ( )
if err != nil {
transaction . Rollback ( )
return err
}
return nil
}
2017-10-04 08:05:51 -04:00
func AccountHandler ( w http . ResponseWriter , r * http . Request , db * DB ) {
user , err := GetUserFromSession ( db , r )
2015-06-27 17:46:06 -04:00
if err != nil {
WriteError ( w , 1 /*Not Signed In*/ )
return
}
if r . Method == "POST" {
2016-02-02 21:46:27 -05:00
// if URL looks like /account/[0-9]+/import, use the account
// import handler
if accountImportRE . MatchString ( r . URL . Path ) {
var accountid int64
2016-02-15 11:28:44 -05:00
var importtype string
n , err := GetURLPieces ( r . URL . Path , "/account/%d/import/%s" , & accountid , & importtype )
2016-02-02 21:46:27 -05:00
2016-02-15 11:28:44 -05:00
if err != nil || n != 2 {
2016-02-02 21:46:27 -05:00
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
2017-10-04 08:05:51 -04:00
AccountImportHandler ( db , w , r , user , accountid , importtype )
2016-02-02 21:46:27 -05:00
return
}
2015-06-27 17:46:06 -04:00
account_json := r . PostFormValue ( "account" )
if account_json == "" {
WriteError ( w , 3 /*Invalid Request*/ )
return
}
var account Account
err := account . Read ( account_json )
if err != nil {
WriteError ( w , 3 /*Invalid Request*/ )
return
}
account . AccountId = - 1
account . UserId = user . UserId
2015-08-05 21:25:25 -04:00
account . AccountVersion = 0
2015-06-27 17:46:06 -04:00
2017-10-04 08:05:51 -04:00
security , err := GetSecurity ( db , account . SecurityId , user . UserId )
2016-10-16 08:19:11 -04:00
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
if security == nil {
2015-06-27 17:46:06 -04:00
WriteError ( w , 3 /*Invalid Request*/ )
return
}
2017-10-04 08:05:51 -04:00
err = InsertAccount ( db , & account )
2015-06-27 17:46:06 -04:00
if err != nil {
if _ , ok := err . ( ParentAccountMissingError ) ; ok {
WriteError ( w , 3 /*Invalid Request*/ )
} else {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
}
return
}
2016-09-29 09:17:57 -04:00
w . WriteHeader ( 201 /*Created*/ )
err = account . Write ( w )
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
2015-06-27 17:46:06 -04:00
} else if r . Method == "GET" {
2015-07-11 08:58:36 -04:00
var accountid int64
n , err := GetURLPieces ( r . URL . Path , "/account/%d" , & accountid )
if err != nil || n != 1 {
2015-06-27 17:46:06 -04:00
//Return all Accounts
var al AccountList
2017-10-04 08:05:51 -04:00
accounts , err := GetAccounts ( db , user . UserId )
2015-06-27 17:46:06 -04:00
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
al . Accounts = accounts
err = ( & al ) . Write ( w )
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
} else {
2015-07-11 08:58:36 -04:00
// if URL looks like /account/[0-9]+/transactions, use the account
// transaction handler
if accountTransactionsRE . MatchString ( r . URL . Path ) {
2017-10-04 08:05:51 -04:00
AccountTransactionsHandler ( db , w , r , user , accountid )
2015-07-11 08:58:36 -04:00
return
}
2015-06-29 07:25:48 -04:00
// Return Account with this Id
2017-10-04 08:05:51 -04:00
account , err := GetAccount ( db , accountid , user . UserId )
2015-06-27 17:46:06 -04:00
if err != nil {
WriteError ( w , 3 /*Invalid Request*/ )
return
}
2015-07-11 08:58:36 -04:00
2015-06-27 17:46:06 -04:00
err = account . Write ( w )
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
}
} else {
accountid , err := GetURLID ( r . URL . Path )
if err != nil {
WriteError ( w , 3 /*Invalid Request*/ )
return
}
if r . Method == "PUT" {
account_json := r . PostFormValue ( "account" )
if account_json == "" {
WriteError ( w , 3 /*Invalid Request*/ )
return
}
var account Account
err := account . Read ( account_json )
if err != nil || account . AccountId != accountid {
WriteError ( w , 3 /*Invalid Request*/ )
return
}
account . UserId = user . UserId
2017-10-04 08:05:51 -04:00
security , err := GetSecurity ( db , account . SecurityId , user . UserId )
2016-10-16 08:19:11 -04:00
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
if security == nil {
2015-06-27 17:46:06 -04:00
WriteError ( w , 3 /*Invalid Request*/ )
return
}
2017-10-04 08:05:51 -04:00
err = UpdateAccount ( db , & account )
2015-06-27 17:46:06 -04:00
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
2016-09-29 09:17:57 -04:00
err = account . Write ( w )
if err != nil {
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
2015-06-27 17:46:06 -04:00
} else if r . Method == "DELETE" {
2017-10-04 08:05:51 -04:00
account , err := GetAccount ( db , accountid , user . UserId )
2015-06-27 17:46:06 -04:00
if err != nil {
WriteError ( w , 3 /*Invalid Request*/ )
return
}
2017-10-04 08:05:51 -04:00
err = DeleteAccount ( db , account )
2015-06-29 07:25:48 -04:00
if err != nil {
2015-06-27 17:46:06 -04:00
WriteError ( w , 999 /*Internal Error*/ )
log . Print ( err )
return
}
WriteSuccess ( w )
}
}
2015-06-25 22:36:58 -04:00
}