diff --git a/engine/calldesc.go b/engine/calldesc.go index 85fd12b6c..9459cf910 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -499,7 +499,8 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) { cost := 0.0 // re-calculate call cost after balances for _, ts := range cc.Timespans { - cost += ts.getCost() // FIXME: floating point sum?? + cost += ts.getCost() + cost = utils.Round(cost, roundingDecimals, utils.ROUNDING_MIDDLE) // just get rid of the extra decimals } cc.Cost = cost } diff --git a/engine/rateinterval.go b/engine/rateinterval.go index 7d90248e1..a7522c1a6 100644 --- a/engine/rateinterval.go +++ b/engine/rateinterval.go @@ -239,8 +239,8 @@ func (i *RateInterval) Equal(o *RateInterval) bool { func (i *RateInterval) GetCost(duration, startSecond time.Duration) float64 { price, _, rateUnit := i. GetRateParameters(startSecond) - d := duration.Seconds() price /= rateUnit.Seconds() + d := duration.Seconds() return d * price } diff --git a/engine/timespans.go b/engine/timespans.go index ada1bc46f..3ede27e85 100644 --- a/engine/timespans.go +++ b/engine/timespans.go @@ -21,7 +21,6 @@ package engine import ( //"fmt" - "log" "time" "github.com/cgrates/cgrates/utils" @@ -215,12 +214,7 @@ func (ts *TimeSpan) getCost() float64 { ts.Cost = utils.Round(cost, ts.RateInterval.Rating.RoundingDecimals, ts.RateInterval.Rating.RoundingMethod) return ts.Cost } else { - cost := 0.0 - for _, inc := range ts.Increments { - cost += inc.Cost - log.Print(inc.Cost, cost) - } - return cost + return ts.Increments[0].Cost * float64(len(ts.Increments)) } return 0 } @@ -237,6 +231,7 @@ func (ts *TimeSpan) createIncrementsSlice() { //incrementCost := rate / rateUnit.Seconds() * rateIncrement.Seconds() nbIncrements := int(ts.GetDuration() / rateIncrement) incrementCost := ts.getCost() / float64(nbIncrements) + incrementCost = utils.Round(incrementCost, roundingDecimals, utils.ROUNDING_MIDDLE) // just get rid of the extra decimals for s := 0; s < nbIncrements; s++ { inc := &Increment{ Duration: rateIncrement, diff --git a/engine/timespans_test.go b/engine/timespans_test.go index 288a12cb5..f90a92ffc 100644 --- a/engine/timespans_test.go +++ b/engine/timespans_test.go @@ -228,6 +228,17 @@ func TestTimespanGetCost(t *testing.T) { } } +func TestTimespanGetCostIntervals(t *testing.T) { + ts := &TimeSpan{} + ts.Increments = make(Increments, 11) + for i := 0; i < 11; i++ { + ts.Increments[i] = &Increment{Cost: 0.02} + } + if ts.getCost() != 0.22 { + t.Error("Error caclulating timspan cost: ", ts.getCost()) + } +} + func TestSetRateInterval(t *testing.T) { i1 := &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{0, 1.0, 1 * time.Second, 1 * time.Second}}}} ts1 := TimeSpan{RateInterval: i1} @@ -718,7 +729,7 @@ func TestTimespanCreateIncrements(t *testing.T) { if len(ts.Increments) != 3 { t.Error("Error creating increment slice: ", len(ts.Increments)) } - if len(ts.Increments) < 3 || ts.Increments[2].Cost != 20.066666666666666 { + if len(ts.Increments) < 3 || ts.Increments[2].Cost != 20.0667 { t.Error("Wrong second slice: ", ts.Increments[2].Cost) } }