Improved accounts code

This commit is contained in:
porosnicuadrian
2022-01-25 14:05:54 +02:00
committed by Dan Christian Bogos
parent 6244374b5d
commit b8e4cfdaea
2 changed files with 15 additions and 19 deletions

View File

@@ -268,18 +268,26 @@ func (aS *AccountS) refundCharges(ctx *context.Context, tnt string, ecs *utils.E
}
for _, chrg := range ecs.Charges {
acntChrg := ecs.Accounting[chrg.ChargingID]
var uf *utils.UnitFactor
if _, has := ecs.UnitFactors[acntChrg.UnitFactorID]; has {
uf = ecs.UnitFactors[acntChrg.UnitFactorID]
}
if acntChrg.BalanceID != utils.MetaTransAbstract { // *transAbstracts is not a real balance, hence the exception
refundUnitsOnAccount(
acntsIdxed[acntChrg.AccountID],
uncompressUnits(acntChrg.Units, chrg.CompressFactor, acntChrg, ecs.UnitFactors),
uncompressUnits(acntChrg.Units, chrg.CompressFactor, acntChrg, uf),
ecs.Accounts[acntChrg.AccountID].Balances[acntChrg.BalanceID])
alteredAcnts.Add(acntChrg.AccountID)
}
for _, chrgID := range acntChrg.JoinedChargeIDs { // refund extra charges
extraChrg := ecs.Accounting[chrgID]
var joinedChargeUf *utils.UnitFactor
if _, has := ecs.UnitFactors[extraChrg.UnitFactorID]; has {
joinedChargeUf = ecs.UnitFactors[extraChrg.UnitFactorID]
}
refundUnitsOnAccount(
acntsIdxed[extraChrg.AccountID],
uncompressUnits(extraChrg.Units, chrg.CompressFactor, extraChrg, ecs.UnitFactors),
uncompressUnits(extraChrg.Units, chrg.CompressFactor, extraChrg, joinedChargeUf),
ecs.Accounts[acntChrg.AccountID].Balances[extraChrg.BalanceID])
alteredAcnts.Add(extraChrg.AccountID)
}

View File

@@ -229,12 +229,8 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big,
origConcrtUnts := cloneUnitsFromConcretes(cncrtBlncs) // so we can revert on errors
paidConcrtUnts := origConcrtUnts // so we can revert when higher abstracts are not possible
var aPaid, aDenied *decimal.Big
maxItr := config.CgrConfig().AccountSCfg().MaxIterations
ecBkp := utils.NewEventCharges() // so we can keep the record of the last sucessful debit
if ec != nil {
ecBkp.Merge(ec) // copy the original EC inside
}
ec = utils.NewEventCharges() //
for i := 0; i <= maxItr; i++ {
if i != 0 {
restoreUnitsFromClones(cncrtBlncs, origConcrtUnts)
@@ -294,18 +290,10 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big,
}
continue
}
ec = ecBkp
if ecDbt != nil {
ec.Merge(ecDbt) // write the partial units debited
}
} else { // debit for the usage succeeded
aPaid = utils.CloneDecimalBig(aUnits)
paidConcrtUnts = cloneUnitsFromConcretes(cncrtBlncs)
if ec == nil {
ec = utils.NewEventCharges()
}
ec = utils.NewEventCharges() // so we can restore the backup inside
ec.Merge(ecBkp) // restore the backup to avoid recording all loops
ec = utils.NewEventCharges() // so we can keep the record of the last sucessful debit
ec.Merge(ecDbt) // merge the last debit
if i == 0 { // no estimation done, covering full
break
@@ -359,15 +347,15 @@ func unlockAccounts(acnts utils.AccountsWithWeight) {
}
// uncompressUnits returns the uncompressed value of the units if compressFactor is provided
func uncompressUnits(units *utils.Decimal, cmprsFctr int, acntChrg *utils.AccountCharge, uf map[string]*utils.UnitFactor) (tU *utils.Decimal) {
func uncompressUnits(units *utils.Decimal, cmprsFctr int, acntChrg *utils.AccountCharge, uf *utils.UnitFactor) (tU *utils.Decimal) {
tU = units
if cmprsFctr > 1 {
tU = &utils.Decimal{utils.MultiplyBig(tU.Big,
decimal.New(int64(cmprsFctr), 0))}
}
// check it this has unit factor
if newUf, has := uf[acntChrg.UnitFactorID]; has {
tU = utils.MultiplyDecimal(tU, newUf.Factor)
if uf != nil {
tU = utils.MultiplyDecimal(tU, uf.Factor)
}
return
}