fixed transaction generation

This commit is contained in:
Fran Jurmanović
2021-06-26 15:06:35 +02:00
parent 830fa65f8a
commit 69d9b97fae
3 changed files with 35 additions and 3 deletions

View File

@@ -48,3 +48,33 @@ func (cm *Subscription) ToTrans() *Transaction {
trans.SubscriptionID = cm.Id trans.SubscriptionID = cm.Id
return trans return trans
} }
func (cm *Subscription) HasNew() bool {
trans := cm.TransactionType;
switch trans.Type {
case "monthly":
lastDate := time.Now().AddDate(0, -cm.CustomRange, 0)
if cm.LastTransactionDate.Before(lastDate) {
return true
}
return false
case "weekly":
lastDate := time.Now().AddDate(0, 0, -(7*cm.CustomRange))
if cm.LastTransactionDate.Before(lastDate) {
return true
}
return false
case "daily":
lastDate := time.Now().AddDate(0, 0, -cm.CustomRange)
if cm.LastTransactionDate.Before(lastDate) {
return true
}
return false
default:
lastDate := time.Now().AddDate(-cm.CustomRange, 0, 0)
if cm.LastTransactionDate.Before(lastDate) {
return true
}
return false
}
}

View File

@@ -75,7 +75,7 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
for startDate.Before(stopDate) { for startDate.Before(stopDate) {
trans := subModel.ToTrans() trans := subModel.ToTrans()
trans.TransactionDate = startDate trans.TransactionDate = startDate
if startDate.After(subModel.LastTransactionDate) { if startDate.After(subModel.LastTransactionDate) && (startDate.Before(now) || startDate.Equal(now)) {
*transactions = append(*transactions, *trans) *transactions = append(*transactions, *trans)
} }
if subModel.SubscriptionType.Type == "monthly" { if subModel.SubscriptionType.Type == "monthly" {

View File

@@ -63,8 +63,10 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation) firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation)
for _, sub := range *subscriptions { for _, sub := range *subscriptions {
if sub.HasNew() {
as.Ss.SubToTrans(&sub) as.Ss.SubToTrans(&sub)
} }
}
query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType")
if walletId != "" { if walletId != "" {
@@ -78,7 +80,7 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal
for i, wallet := range *wallets { for i, wallet := range *wallets {
for _, trans := range wallet.Transactions { for _, trans := range wallet.Transactions {
if trans.TransactionDate.Local().Before(firstOfNextMonth) && trans.TransactionDate.Local().After(firstOfMonth) { if trans.TransactionDate.Local().Before(firstOfNextMonth) && trans.TransactionDate.Local().After(firstOfMonth) || trans.TransactionDate.Local().Equal(firstOfMonth) {
if trans.TransactionType.Type == "expense" { if trans.TransactionType.Type == "expense" {
(*wallets)[i].CurrentBalance -= trans.Amount (*wallets)[i].CurrentBalance -= trans.Amount
} else { } else {