mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
IMproved refund apis with unitfactor + concrete merge
This commit is contained in:
committed by
Dan Christian Bogos
parent
100f797ee7
commit
d2b3620d4a
@@ -268,23 +268,19 @@ func (aS *AccountS) refundCharges(ctx *context.Context, tnt string, ecs *utils.E
|
||||
}
|
||||
for _, chrg := range ecs.Charges {
|
||||
acntChrg := ecs.Accounting[chrg.ChargingID]
|
||||
var ufValue *utils.Decimal
|
||||
if uf, has := ecs.UnitFactors[acntChrg.UnitFactorID]; has {
|
||||
ufValue = uf.Factor
|
||||
}
|
||||
if acntChrg.BalanceID != utils.MetaTransAbstract { // *transAbstracts is not a real balance, hence the exception
|
||||
refundUnitsOnAccount(
|
||||
acntsIdxed[acntChrg.AccountID],
|
||||
uncompressUnits(acntChrg.Units, chrg.CompressFactor),
|
||||
ecs.Accounts[acntChrg.AccountID].Balances[acntChrg.BalanceID], ufValue)
|
||||
uncompressUnits(acntChrg.Units, chrg.CompressFactor, acntChrg, ecs.UnitFactors),
|
||||
ecs.Accounts[acntChrg.AccountID].Balances[acntChrg.BalanceID])
|
||||
alteredAcnts.Add(acntChrg.AccountID)
|
||||
}
|
||||
for _, chrgID := range acntChrg.JoinedChargeIDs { // refund extra charges
|
||||
extraChrg := ecs.Accounting[chrgID]
|
||||
refundUnitsOnAccount(
|
||||
acntsIdxed[extraChrg.AccountID],
|
||||
uncompressUnits(extraChrg.Units, chrg.CompressFactor),
|
||||
ecs.Accounts[acntChrg.AccountID].Balances[extraChrg.BalanceID], ufValue)
|
||||
uncompressUnits(extraChrg.Units, chrg.CompressFactor, extraChrg, ecs.UnitFactors),
|
||||
ecs.Accounts[acntChrg.AccountID].Balances[extraChrg.BalanceID])
|
||||
alteredAcnts.Add(extraChrg.AccountID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1603,7 +1603,9 @@ func TestV1DebitAbstractsEventCharges(t *testing.T) {
|
||||
Weight: 30,
|
||||
},
|
||||
},
|
||||
|
||||
Opts: map[string]interface{}{
|
||||
utils.MetaBalanceLimit: -200.0,
|
||||
},
|
||||
CostIncrements: []*utils.CostIncrement{
|
||||
{
|
||||
Increment: utils.NewDecimal(int64(time.Second), 0),
|
||||
|
||||
@@ -233,6 +233,7 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big,
|
||||
paidConcrtUnts := origConcrtUnts // so we can revert when higher abstracts are not possible
|
||||
var aPaid, aDenied *decimal.Big
|
||||
maxItr := config.CgrConfig().AccountSCfg().MaxIterations
|
||||
//lastSuccesDebit := new(utils.EventCharges)
|
||||
for i := 0; i <= maxItr; i++ {
|
||||
if i != 0 {
|
||||
restoreUnitsFromClones(cncrtBlncs, origConcrtUnts)
|
||||
@@ -279,6 +280,12 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big,
|
||||
return
|
||||
}
|
||||
err = nil
|
||||
/*
|
||||
if lastSuccesDebit != nil {
|
||||
ec = utils.NewEventCharges()
|
||||
ec.Merge(lastSuccesDebit)
|
||||
}
|
||||
*/
|
||||
// ErrInsufficientCredit
|
||||
aDenied = utils.CloneDecimalBig(aUnits)
|
||||
if aPaid == nil { // going backwards
|
||||
@@ -298,6 +305,7 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big,
|
||||
if ec == nil {
|
||||
ec = utils.NewEventCharges()
|
||||
}
|
||||
//lastSuccesDebit = ecDbt
|
||||
ec.Merge(ecDbt)
|
||||
if i == 0 { // no estimation done, covering full
|
||||
break
|
||||
@@ -322,9 +330,6 @@ func maxDebitAbstractsFromConcretes(ctx *context.Context, aUnits *decimal.Big,
|
||||
aPaid = decimal.New(0, 0)
|
||||
ec = utils.NewEventCharges()
|
||||
}
|
||||
/*
|
||||
utils.Logger.Debug(fmt.Sprintf("aPaid %v, incr: %v, round: %v", aPaid, costIcrm.Increment.Big, roundUnitsWithIncrements(aPaid, costIcrm.Increment.Big)))
|
||||
*/
|
||||
ec.Abstracts = &utils.Decimal{aPaid}
|
||||
restoreUnitsFromClones(cncrtBlncs, paidConcrtUnts)
|
||||
return
|
||||
@@ -354,26 +359,27 @@ func unlockAccounts(acnts utils.AccountsWithWeight) {
|
||||
}
|
||||
|
||||
// uncompressUnits returns the uncompressed value of the units if compressFactor is provided
|
||||
func uncompressUnits(units *utils.Decimal, cmprsFctr int) (tU *utils.Decimal) {
|
||||
func uncompressUnits(units *utils.Decimal, cmprsFctr int, acntChrg *utils.AccountCharge, uf map[string]*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)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// refundUnitsOnAccount is responsible for returning the units back to the balance
|
||||
// origBlnc is used for both it's ID as well as as a configuration backup in case when the balance is not longer present
|
||||
func refundUnitsOnAccount(acnt *utils.Account, units *utils.Decimal, origBlnc *utils.Balance, ufValue *utils.Decimal) {
|
||||
func refundUnitsOnAccount(acnt *utils.Account, units *utils.Decimal, origBlnc *utils.Balance) {
|
||||
if _, has := acnt.Balances[origBlnc.ID]; has {
|
||||
acnt.Balances[origBlnc.ID].Units = &utils.Decimal{
|
||||
utils.SumBig(
|
||||
acnt.Balances[origBlnc.ID].Units.Big,
|
||||
units.Big)}
|
||||
if ufValue != nil {
|
||||
acnt.Balances[origBlnc.ID].Units = utils.MultiplyDecimal(acnt.Balances[origBlnc.ID].Units, ufValue)
|
||||
}
|
||||
} else {
|
||||
acnt.Balances[origBlnc.ID] = origBlnc.Clone()
|
||||
acnt.Balances[origBlnc.ID].Units = &utils.Decimal{utils.CloneDecimalBig(units.Big)}
|
||||
|
||||
@@ -47,32 +47,28 @@ var (
|
||||
testAccSResetStorDb,
|
||||
testAccSStartEngine,
|
||||
testAccSRPCConn,
|
||||
/*
|
||||
testGetAccProfileBeforeSet,
|
||||
testGetAccProfilesBeforeSet,
|
||||
testAccSetAccProfile,
|
||||
testAccGetAccIDs,
|
||||
testAccGetAccs,
|
||||
testAccGetAccIDsCount,
|
||||
testGetAccBeforeSet2,
|
||||
testAccSetAcc2,
|
||||
testAccGetAccIDs2,
|
||||
testAccGetAccs2,
|
||||
testAccGetAccIDsCount2,
|
||||
testAccRemoveAcc,
|
||||
testAccGetAccs3,
|
||||
testAccGetAccsWithPrefix,
|
||||
testAccGetAccountsForEvent,
|
||||
testAccMaxAbstracts,
|
||||
testAccDebitAbstracts,
|
||||
testAccMaxConcretes,
|
||||
testAccDebitConcretes,
|
||||
*/
|
||||
testGetAccProfileBeforeSet,
|
||||
testGetAccProfilesBeforeSet,
|
||||
testAccSetAccProfile,
|
||||
testAccGetAccIDs,
|
||||
testAccGetAccs,
|
||||
testAccGetAccIDsCount,
|
||||
testGetAccBeforeSet2,
|
||||
testAccSetAcc2,
|
||||
testAccGetAccIDs2,
|
||||
testAccGetAccs2,
|
||||
testAccGetAccIDsCount2,
|
||||
testAccRemoveAcc,
|
||||
testAccGetAccs3,
|
||||
testAccGetAccsWithPrefix,
|
||||
testAccGetAccountsForEvent,
|
||||
testAccMaxAbstracts,
|
||||
testAccDebitAbstracts,
|
||||
testAccMaxConcretes,
|
||||
testAccDebitConcretes,
|
||||
// RefundCharges test
|
||||
testAccRefundCharges,
|
||||
/*
|
||||
testAccActionSetRmvBalance,
|
||||
*/
|
||||
testAccActionSetRmvBalance,
|
||||
testAccSKillEngine,
|
||||
}
|
||||
)
|
||||
@@ -1341,7 +1337,7 @@ func testAccRefundCharges(t *testing.T) {
|
||||
ID: "AB",
|
||||
Weights: ";10",
|
||||
Type: utils.MetaAbstract,
|
||||
Units: "5m",
|
||||
Units: "5m", // 300s
|
||||
UnitFactors: []*utils.APIUnitFactor{
|
||||
{
|
||||
Factor: 10,
|
||||
@@ -1363,6 +1359,11 @@ func testAccRefundCharges(t *testing.T) {
|
||||
Weights: ";5",
|
||||
Type: utils.MetaConcrete,
|
||||
Units: "50",
|
||||
UnitFactors: []*utils.APIUnitFactor{
|
||||
{
|
||||
Factor: 15,
|
||||
},
|
||||
},
|
||||
CostIncrements: []*utils.APICostIncrement{
|
||||
{
|
||||
Increment: "1s",
|
||||
|
||||
Reference in New Issue
Block a user