fix walletheader

This commit is contained in:
Fran Jurmanović
2021-06-18 22:51:59 +02:00
parent 8e0f34665e
commit 820f5ef9bc
2 changed files with 63 additions and 41 deletions

View File

@@ -30,3 +30,16 @@ type NewSubscriptionBody struct {
Description string `json:"description" form:"description"` Description string `json:"description" form:"description"`
Amount json.Number `json:"amount" form:"amount"` Amount json.Number `json:"amount" form:"amount"`
} }
func (cm *Subscription) ToTrans() *Transaction {
trans := new(Transaction)
trans.Init()
trans.Amount = cm.Amount
trans.Description = cm.Description
trans.WalletID = cm.WalletID
trans.Wallet = cm.Wallet
trans.TransactionTypeID = cm.TransactionTypeID
trans.TransactionType = cm.TransactionType
trans.DateCreated = cm.DateCreated
return trans
}

View File

@@ -88,6 +88,24 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
addWhere(&wallets, trans.WalletID, trans) addWhere(&wallets, trans.WalletID, trans)
} }
for _, sub := range *subscriptions {
startDate := sub.StartDate
for startDate.Before(firstOfMonthAfterNext) {
trans := sub.ToTrans()
trans.TransactionDate = startDate
addWhere(&wallets, sub.WalletID, *trans)
if sub.SubscriptionType.Type == "monthly" {
startDate = startDate.AddDate(0, sub.CustomRange, 0)
} else if sub.SubscriptionType.Type == "weekly" {
startDate = startDate.AddDate(0, 0, 7*sub.CustomRange)
} else if sub.SubscriptionType.Type == "daily" {
startDate = startDate.AddDate(0, 0, sub.CustomRange)
} else {
startDate = startDate.AddDate(sub.CustomRange, 0, 0)
}
}
}
for _, wallet := range wallets { for _, wallet := range wallets {
wg.Add(1) wg.Add(1)
go func() { go func() {
@@ -116,45 +134,36 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
}() }()
} }
for _, sub := range *subscriptions { // for _, sub := range *subscriptions {
wg.Add(1) // wg.Add(1)
go func() { // go func() {
defer wg.Done() // defer wg.Done()
startDate := sub.StartDate // startDate := sub.StartDate
now := time.Now() // now := time.Now()
for startDate.Before(now) { // for startDate.Before(now) {
if startDate.Before(firstOfNextMonth) && startDate.After(firstOfMonth) { // if startDate.Before(firstOfNextMonth) && startDate.After(firstOfMonth) {
if sub.TransactionType.Type == "expense" { // if sub.TransactionType.Type == "expense" {
subCurrentBalance -= sub.Amount // subCurrentBalance -= sub.Amount
} else { // } else {
subCurrentBalance += sub.Amount // subCurrentBalance += sub.Amount
} // }
} else if startDate.Before(firstOfMonthAfterNext) && startDate.After(firstOfNextMonth) { // } else if startDate.Before(firstOfMonthAfterNext) && startDate.After(firstOfNextMonth) {
if sub.TransactionType.Type == "expense" { // if sub.TransactionType.Type == "expense" {
subNextMonth -= sub.Amount // subNextMonth -= sub.Amount
} else { // } else {
subNextMonth += sub.Amount // subNextMonth += sub.Amount
} // }
} else if startDate.Before(firstOfMonth) { // } else if startDate.Before(firstOfMonth) {
if sub.TransactionType.Type == "expense" { // if sub.TransactionType.Type == "expense" {
subLastMonthBalance -= sub.Amount // subLastMonthBalance -= sub.Amount
} else { // } else {
subLastMonthBalance += sub.Amount // subLastMonthBalance += sub.Amount
} // }
} // }
if sub.SubscriptionType.Type == "monthly" { // }
startDate = startDate.AddDate(0, sub.CustomRange, 0) // }()
} else if sub.SubscriptionType.Type == "weekly" { // }
startDate = startDate.AddDate(0, 0, 7*sub.CustomRange)
} else if sub.SubscriptionType.Type == "daily" {
startDate = startDate.AddDate(0, 0, sub.CustomRange)
} else {
startDate = startDate.AddDate(sub.CustomRange, 0, 0)
}
}
}()
}
wg.Wait() wg.Wait()
@@ -175,9 +184,9 @@ func (as *WalletService) GetHeader(am *models.Auth, embed string, walletId strin
func addWhere(s *[]models.WalletTransactions, walletId string, e models.Transaction) { func addWhere(s *[]models.WalletTransactions, walletId string, e models.Transaction) {
var exists bool var exists bool
for _, a := range *s { for a, _ := range *s {
if a.WalletId == walletId { if (*s)[a].WalletId == walletId {
a.Transactions = append(a.Transactions, e) (*s)[a].Transactions = append((*s)[a].Transactions, e)
} }
exists = true exists = true
} }