sub to trans

This commit is contained in:
Fran Jurmanović
2021-06-20 12:03:56 +02:00
parent 3d43716669
commit 27742223ed
7 changed files with 101 additions and 77 deletions

View File

@@ -1,6 +1,7 @@
package services
import (
"math"
"time"
"wallet-api/pkg/models"
@@ -14,7 +15,7 @@ type SubscriptionService struct {
func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Subscription {
tm := new(models.Subscription)
amount, _ := body.Amount.Int64()
amount, _ := body.Amount.Float64()
customRange, _ := body.CustomRange.Int64()
tm.Init()
@@ -26,7 +27,7 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
tm.StartDate = body.StartDate
tm.HasEnd = body.HasEnd
tm.EndDate = body.EndDate
tm.Amount = int(amount)
tm.Amount = float32(math.Round(amount*100) / 100)
if body.StartDate.IsZero() {
tm.StartDate = time.Now()
@@ -34,6 +35,8 @@ func (as *SubscriptionService) New(body *models.NewSubscriptionBody) *models.Sub
as.Db.Model(tm).Insert()
as.SubToTrans(tm)
return tm
}
@@ -61,13 +64,19 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
stopDate = subModel.EndDate.Local()
}
transactions := new([]models.Transaction)
if subModel.SubscriptionType == nil {
st := new(models.SubscriptionType)
as.Db.Model(st).Where("? = ?", pg.Ident("id"), subModel.SubscriptionTypeID).Select()
subModel.SubscriptionType = st
}
for startDate.Before(stopDate) {
trans := subModel.ToTrans()
trans.TransactionDate = startDate
if startDate.After(subModel.LastTransactionDate) {
as.Db.Model(trans).Insert()
subModel.LastTransactionDate = trans.TransactionDate
as.Db.Model(subModel).WherePK().Update()
*transactions = append(*transactions, *trans)
}
if subModel.SubscriptionType.Type == "monthly" {
startDate = startDate.AddDate(0, subModel.CustomRange, 0)
@@ -79,4 +88,11 @@ func (as *SubscriptionService) SubToTrans(subModel *models.Subscription) {
startDate = startDate.AddDate(subModel.CustomRange, 0, 0)
}
}
if len(*transactions) > 0 {
as.Db.Model(transactions).Insert()
subModel.LastTransactionDate = (*transactions)[len(*transactions)-1].TransactionDate
as.Db.Model(subModel).WherePK().Update()
}
}