diff --git a/pkg/models/subscriptions.go b/pkg/models/subscriptions.go index 00f3a70..e4bd06e 100644 --- a/pkg/models/subscriptions.go +++ b/pkg/models/subscriptions.go @@ -48,3 +48,33 @@ func (cm *Subscription) ToTrans() *Transaction { trans.SubscriptionID = cm.Id 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 + } +} \ No newline at end of file diff --git a/pkg/services/subscriptions.go b/pkg/services/subscriptions.go index bed7782..72b38b5 100644 --- a/pkg/services/subscriptions.go +++ b/pkg/services/subscriptions.go @@ -75,7 +75,7 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) { for startDate.Before(stopDate) { trans := subModel.ToTrans() trans.TransactionDate = startDate - if startDate.After(subModel.LastTransactionDate) { + if startDate.After(subModel.LastTransactionDate) && (startDate.Before(now) || startDate.Equal(now)) { *transactions = append(*transactions, *trans) } if subModel.SubscriptionType.Type == "monthly" { diff --git a/pkg/services/wallets.go b/pkg/services/wallets.go index 5679f5c..d466688 100644 --- a/pkg/services/wallets.go +++ b/pkg/services/wallets.go @@ -63,7 +63,9 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal firstOfMonthAfterNext := time.Date(currentYear, currentMonth+2, 1, 0, 0, 0, 0, currentLocation) for _, sub := range *subscriptions { - as.Ss.SubToTrans(&sub) + if sub.HasNew() { + as.Ss.SubToTrans(&sub) + } } query := as.Db.Model(transactions).Relation("Wallet").Where("wallet.? = ?", pg.Ident("user_id"), am.Id).Relation("TransactionType") @@ -78,7 +80,7 @@ func (as *WalletService) GetHeader(am *models.Auth, walletId string) *models.Wal for i, wallet := range *wallets { 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" { (*wallets)[i].CurrentBalance -= trans.Amount } else {