From f4f56937b7a28a10bac7592c54933d334690a9e0 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 26 May 2015 13:50:54 +0300 Subject: [PATCH] fix for maxcost callcost information --- engine/balances.go | 4 +++- engine/calldesc.go | 2 +- engine/calldesc_test.go | 23 +++++++++++++++++++++++ engine/timespans.go | 11 ++++++++++- engine/timespans_test.go | 2 +- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/engine/balances.go b/engine/balances.go index f5c7e8efb..52b8d386d 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -337,7 +337,7 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala if ts.Increments == nil { ts.createIncrementsSlice() } - //log.Printf("TS: %+v", ts) + if ts.RateInterval == nil { Logger.Err(fmt.Sprintf("Nil RateInterval ERROR on TS: %+v, CC: %+v, from CD: %+v", ts, cc, cd)) return nil, errors.New("timespan with no rate interval assigned") @@ -472,6 +472,8 @@ func (b *Balance) DebitMoney(cd *CallDescriptor, ub *Account, count bool, dryRun if count { ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationIds: cc.Destination}}) } + + //log.Printf("TS: %+v", cc.Cost) // go to nextincrement continue } diff --git a/engine/calldesc.go b/engine/calldesc.go index 2a69a8830..7aa77fcd8 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -576,7 +576,7 @@ func (cd *CallDescriptor) debit(account *Account, dryRun bool, goNegative bool) } //log.Printf("Debit CD: %+v", cd) cc, err = account.debitCreditBalance(cd, !dryRun, dryRun, goNegative) - //log.Print("HERE: ", cc, err) + //log.Printf("HERE: %+v %v", cc, err) if err != nil { Logger.Err(fmt.Sprintf(" Error getting cost for account key <%s>: %s", cd.GetAccountKey(), err.Error())) return nil, err diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index 56d4cb2a3..df68c9a0a 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -558,6 +558,29 @@ func TestMaxSessionTimeWithMaxCostFree(t *testing.T) { } } +func TestMaxDebitWithMaxCostFree(t *testing.T) { + ap, _ := accountingStorage.GetActionTimings("TOPUP10_AT") + for _, at := range ap { + at.Execute() + } + cd := &CallDescriptor{ + Direction: "*out", + Category: "call", + Tenant: "cgrates.org", + Subject: "max", + Account: "max", + Destination: "0723123113", + TimeStart: time.Date(2015, 3, 23, 19, 0, 0, 0, time.UTC), + TimeEnd: time.Date(2015, 3, 23, 19, 30, 0, 0, time.UTC), + MaxCostSoFar: 0, + } + cc, err := cd.MaxDebit() + expected := 10.0 + if cc.Cost != expected || err != nil { + t.Errorf("Expected %v was %v", expected, cc.Cost) + } +} + func TestGetCostWithMaxCostFree(t *testing.T) { ap, _ := accountingStorage.GetActionTimings("TOPUP10_AT") for _, at := range ap { diff --git a/engine/timespans.go b/engine/timespans.go index 0da7e3040..30d76b292 100644 --- a/engine/timespans.go +++ b/engine/timespans.go @@ -274,7 +274,16 @@ func (ts *TimeSpan) getCost() float64 { ts.Cost = utils.Round(cost, ts.RateInterval.Rating.RoundingDecimals, ts.RateInterval.Rating.RoundingMethod) return ts.Cost } else { - return ts.Increments[0].Cost * float64(ts.Increments.Length()) + cost := 0.0 + // some increments may have 0 cost because of the max cost strategy + for _, inc := range ts.Increments { + cost += inc.Cost + } + if ts.RateInterval != nil && ts.RateInterval.Rating != nil { + return utils.Round(cost, ts.RateInterval.Rating.RoundingDecimals, ts.RateInterval.Rating.RoundingMethod) + } else { + return utils.Round(cost, globalRoundingDecimals, utils.ROUNDING_MIDDLE) + } } } diff --git a/engine/timespans_test.go b/engine/timespans_test.go index 57539eb7d..468e2a51e 100644 --- a/engine/timespans_test.go +++ b/engine/timespans_test.go @@ -235,7 +235,7 @@ func TestTimespanGetCostIntervals(t *testing.T) { ts.Increments[i] = &Increment{Cost: 0.02} } if ts.getCost() != 0.22 { - t.Error("Error caclulating timspan cost: ", ts.getCost()) + t.Error("Error caclulating timespan cost: ", ts.getCost()) } }