mirror of
https://github.com/aclindsa/moneygo.git
synced 2024-12-28 00:02:29 -05:00
imports.go: Automatically create Imbalances and Trading accounts
This commit is contained in:
parent
b71c862cbf
commit
6856d617ec
84
imports.go
84
imports.go
@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -62,24 +63,87 @@ func AccountImportHandler(w http.ResponseWriter, r *http.Request, user *User, ac
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var transactions []Transaction
|
||||||
for _, transaction := range *itl.Transactions {
|
for _, transaction := range *itl.Transactions {
|
||||||
|
transaction.UserId = user.UserId
|
||||||
|
transaction.Status = Imported
|
||||||
|
|
||||||
if !transaction.Valid() {
|
if !transaction.Valid() {
|
||||||
WriteError(w, 3 /*Invalid Request*/)
|
WriteError(w, 3 /*Invalid Request*/)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check if transactions are balanced too
|
imbalances, err := transaction.GetImbalances()
|
||||||
// balanced, err := transaction.Balanced()
|
if err != nil {
|
||||||
// if !balanced || err != nil {
|
WriteError(w, 999 /*Internal Error*/)
|
||||||
// WriteError(w, 3 /*Invalid Request*/)
|
log.Print(err)
|
||||||
// return
|
return
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////// TODO ////////////////////////
|
// Fixup any imbalances in transactions
|
||||||
for _, transaction := range *itl.Transactions {
|
var zero big.Rat
|
||||||
transaction.UserId = user.UserId
|
var num_imbalances int
|
||||||
transaction.Status = Imported
|
for _, imbalance := range imbalances {
|
||||||
|
if imbalance.Cmp(&zero) != 0 {
|
||||||
|
num_imbalances += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for imbalanced_security, imbalance := range imbalances {
|
||||||
|
if imbalance.Cmp(&zero) != 0 {
|
||||||
|
var imbalanced_account *Account
|
||||||
|
// If we're dealing with exactly two securities, assume any imbalances
|
||||||
|
// from imports are from trading currencies/securities
|
||||||
|
if num_imbalances == 2 {
|
||||||
|
imbalanced_account, err = GetTradingAccount(user.UserId, imbalanced_security)
|
||||||
|
} else {
|
||||||
|
imbalanced_account, err = GetImbalanceAccount(user.UserId, imbalanced_security)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, 999 /*Internal Error*/)
|
||||||
|
log.Print(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new split to fixup imbalance
|
||||||
|
split := new(Split)
|
||||||
|
r := new(big.Rat)
|
||||||
|
r.Neg(&imbalance)
|
||||||
|
security := GetSecurity(imbalanced_security)
|
||||||
|
split.Amount = r.FloatString(security.Precision)
|
||||||
|
split.SecurityId = -1
|
||||||
|
split.AccountId = imbalanced_account.AccountId
|
||||||
|
transaction.Splits = append(transaction.Splits, split)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move any splits with SecurityId but not AccountId to Imbalances
|
||||||
|
// accounts
|
||||||
|
for _, split := range transaction.Splits {
|
||||||
|
if split.SecurityId != -1 || split.AccountId == -1 {
|
||||||
|
imbalanced_account, err := GetImbalanceAccount(user.UserId, split.SecurityId)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, 999 /*Internal Error*/)
|
||||||
|
log.Print(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
split.AccountId = imbalanced_account.AccountId
|
||||||
|
split.SecurityId = -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
balanced, err := transaction.Balanced()
|
||||||
|
if !balanced || err != nil {
|
||||||
|
WriteError(w, 999 /*Internal Error*/)
|
||||||
|
log.Print(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
transactions = append(transactions, transaction)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, transaction := range transactions {
|
||||||
err := InsertTransaction(&transaction, user)
|
err := InsertTransaction(&transaction, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteError(w, 999 /*Internal Error*/)
|
WriteError(w, 999 /*Internal Error*/)
|
||||||
|
Loading…
Reference in New Issue
Block a user