From bb52349e8f7a6d792c45fff749b753f28dd37847 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 17 Jun 2015 16:48:37 +0300 Subject: [PATCH] rating info for *zero based balances (#51, #96) --- engine/account_test.go | 11 ++++++++--- engine/balances.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/engine/account_test.go b/engine/account_test.go index 24183dfe1..b00acdbdc 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -1377,15 +1377,20 @@ func TestDebitDataUnits(t *testing.T) { }} var err error cc, err = rifsBalance.debitCreditBalance(cd, false, false, true) + // test rating information + ts := cc.Timespans[0] + if ts.MatchedSubject != "testm" || ts.MatchedPrefix != "0723" || ts.MatchedDestId != "NAT" || ts.RatingPlanId != utils.META_NONE { + t.Errorf("Error setting rating info: %+v", ts.ratingInfo) + } if err != nil { t.Error("Error debiting balance: ", err) } - if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testm" { - t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) + if ts.Increments[0].BalanceInfo.UnitBalanceUuid != "testm" { + t.Error("Error setting balance id to increment: ", ts.Increments[0]) } if rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].Value != 20 || rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 21 { - t.Log(cc.Timespans[0].Increments) + t.Log(ts.Increments) t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value) } } diff --git a/engine/balances.go b/engine/balances.go index 52b8d386d..ca4678913 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -25,6 +25,7 @@ import ( "strings" "time" + "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/utils" ) @@ -184,6 +185,26 @@ func (b *Balance) Clone() *Balance { } } +func (b *Balance) getMatchingPrefixAndDestId(dest string) (prefix, destId string) { + if b.DestinationIds != "" && b.DestinationIds != utils.ANY { + for _, p := range utils.SplitPrefix(dest, MIN_PREFIX_MATCH) { + if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + destIds := x.(map[interface{}]struct{}) + for dId, _ := range destIds { + balDestIds := strings.Split(b.DestinationIds, utils.INFIELD_SEP) + for _, balDestID := range balDestIds { + if dId == balDestID { + return p, balDestID + } + } + + } + } + } + } + return +} + // Returns the available number of seconds for a specified credit func (b *Balance) GetMinutesForCredit(origCD *CallDescriptor, initialCredit float64) (duration time.Duration, credit float64) { cd := origCD.Clone() @@ -292,6 +313,13 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala }, }, } + prefix, destid := b.getMatchingPrefixAndDestId(cd.Destination) + ts.setRatingInfo(&RatingInfo{ + MatchedSubject: b.Uuid, + MatchedPrefix: prefix, + MatchedDestId: destid, + RatingPlanId: utils.META_NONE, + }) ts.createIncrementsSlice() //log.Printf("CC: %+v", ts) for incIndex, inc := range ts.Increments {