Updated AccountSummary in EventCost

This commit is contained in:
Trial97
2021-08-31 17:23:31 +03:00
committed by Dan Christian Bogos
parent 1f0931f8f3
commit a417982f41
4 changed files with 33 additions and 6 deletions

View File

@@ -1212,3 +1212,23 @@ func (as *AccountSummary) FieldAsInterface(fldPath []string) (val interface{}, e
return as.Disabled, nil
}
}
// UpdateBalances will add the expired balances back
func (as *AccountSummary) UpdateBalances(old *AccountSummary) {
if old == nil {
return
}
for _, initialBal := range old.BalanceSummaries {
removed := true
for _, currentBal := range as.BalanceSummaries {
if currentBal.UUID == initialBal.UUID {
removed = false
break
}
}
if removed { // add back the expired balances
initialBal.Value = 0 // it expired so lose all the values only keep track of it in this
as.BalanceSummaries = append(as.BalanceSummaries, initialBal)
}
}
}

View File

@@ -792,6 +792,7 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) {
if err != nil {
return nil, err
}
initialAcnt := account.AsAccountSummary()
acntIDs, sgerr := account.GetUniqueSharedGroupMembers(cd)
if sgerr != nil {
return nil, sgerr
@@ -805,7 +806,7 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) {
_, err = guardian.Guardian.Guard(func() (iface interface{}, err error) {
cc, err = cd.debit(account, cd.DryRun, !cd.DenyNegativeAccount)
if err == nil {
cc.AccountSummary = cd.AccountSummary()
cc.AccountSummary = cd.AccountSummary(initialAcnt)
}
return
}, config.CgrConfig().GeneralCfg().LockingTimeout, lkIDs...)
@@ -825,6 +826,7 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) {
if err != nil {
return nil, err
}
initialAcnt := account.AsAccountSummary()
acntIDs, err := account.GetUniqueSharedGroupMembers(cd)
if err != nil {
return nil, err
@@ -846,7 +848,7 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) {
}
if err != nil || remainingDuration == 0 {
cc = cd.CreateCallCost()
cc.AccountSummary = cd.AccountSummary()
cc.AccountSummary = cd.AccountSummary(initialAcnt)
if cd.GetDuration() == 0 {
// add RatingInfo
err = cd.LoadRatingPlans()
@@ -869,7 +871,7 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) {
}
cc, err = cd.debit(account, cd.DryRun, !cd.DenyNegativeAccount)
if err == nil {
cc.AccountSummary = cd.AccountSummary()
cc.AccountSummary = cd.AccountSummary(initialAcnt)
}
return
}, config.CgrConfig().GeneralCfg().LockingTimeout, lkIDs...)
@@ -1030,11 +1032,13 @@ func (cd *CallDescriptor) Clone() *CallDescriptor {
}
// AccountSummary returns the AccountSummary for cached account
func (cd *CallDescriptor) AccountSummary() *AccountSummary {
func (cd *CallDescriptor) AccountSummary(initialAcnt *AccountSummary) *AccountSummary {
if cd.account == nil {
return nil
}
return cd.account.AsAccountSummary()
acntSummary := cd.account.AsAccountSummary()
acntSummary.UpdateBalances(initialAcnt)
return acntSummary
}
// FieldAsInterface is part of utils.DataProvider

View File

@@ -641,6 +641,7 @@ func (ec *EventCost) SyncKeys(refEC *EventCost) {
// Merge will merge a list of EventCosts into this one
func (ec *EventCost) Merge(ecs ...*EventCost) {
for _, newEC := range ecs {
newEC.AccountSummary.UpdateBalances(ec.AccountSummary)
ec.AccountSummary = newEC.AccountSummary // updated AccountSummary information
for cIlIdx := range newEC.Charges {
ec.appendChargingIntervalFromEventCost(newEC, cIlIdx)

View File

@@ -598,7 +598,9 @@ func (sS *SessionS) refundSession(s *Session, sRunIdx int, rUsage time.Duration)
return
}
if acnt.ID != "" { // Account info updated, update also cached AccountSummary
sr.EventCost.AccountSummary = acnt.AsAccountSummary()
acntSummary := acnt.AsAccountSummary()
acntSummary.UpdateBalances(sr.EventCost.AccountSummary)
sr.EventCost.AccountSummary = acntSummary
}
return
}