diff --git a/engine/balances.go b/engine/balances.go index 238ddb537..a16ece674 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -303,382 +303,6 @@ func (b *Balance) SetDirty() { b.dirty = true } -// debitUnits will debit units for call descriptor. -// returns the amount debited within cc -func (b *Balance) debitUnits(cd *CallDescriptor, ub *Account, moneyBalances Balances, count bool, dryRun, debitConnectFee bool, fltrS *FilterS) (cc *CallCost, err error) { - if !b.IsActiveAt(cd.TimeStart) || b.GetValue() <= 0 { - return - } - if duration, err := utils.ParseZeroRatingSubject(cd.ToR, b.RatingSubject, config.CgrConfig().RalsCfg().BalanceRatingSubject, true); err == nil { - // we have *zero based units - cc = cd.CreateCallCost() - cc.Timespans = append(cc.Timespans, &TimeSpan{ - TimeStart: cd.TimeStart, - TimeEnd: cd.TimeEnd, - }) - ts := cc.Timespans[0] - ts.RoundToDuration(duration) - ts.RateInterval = &RateInterval{ - Rating: &RIRate{ - Rates: RateGroups{ - &RGRate{ - GroupIntervalStart: 0, - Value: 0, - RateIncrement: duration, - RateUnit: duration, - }, - }, - }, - } - prefix, destid := b.getMatchingPrefixAndDestID(cd.Destination) - if prefix == "" { - prefix = cd.Destination - } - if destid == "" { - destid = utils.MetaAny - } - ts.setRatingInfo(&RatingInfo{ - MatchedSubject: b.Uuid, - MatchedPrefix: prefix, - MatchedDestId: destid, - RatingPlanId: utils.MetaNone, - }) - ts.createIncrementsSlice() - //log.Printf("CC: %+v", ts) - for incIndex, inc := range ts.Increments { - //log.Printf("INCREMENET: %+v", inc) - - amount := float64(inc.Duration.Nanoseconds()) - if b.Factors != nil { - amount = utils.Round(amount/b.Factors.GetValue(cd.ToR), - globalRoundingDecimals, utils.MetaRoundingUp) - } - if b.GetValue() >= amount { - b.SubtractValue(amount) - inc.BalanceInfo.Unit = &UnitInfo{ - UUID: b.Uuid, - ID: b.ID, - Value: b.Value, - DestinationID: cc.Destination, - Consumed: amount, - ToR: cc.ToR, - RateInterval: nil, - } - inc.BalanceInfo.AccountID = ub.ID - inc.Cost = 0 - if count { - ub.countUnits(amount, cc.ToR, cc, b, fltrS) - } - } else { - // delete the rest of the unpiad increments/timespans - if incIndex == 0 { - // cut the entire current timespan - cc.Timespans = nil - } else { - ts.SplitByIncrement(incIndex) - } - if len(cc.Timespans) == 0 { - cc = nil - } - return cc, nil - } - } - } else { - // get the cost from balance - //log.Printf("::::::: %+v", cd) - var debitedConnectFeeBalance Balance - var ok bool - cc, err = b.GetCost(cd, true) - if err != nil { - return nil, err - } - if debitConnectFee { - // this is the first add, debit the connect fee - if ok, debitedConnectFeeBalance = ub.DebitConnectionFee(cc, moneyBalances, count, true, fltrS); !ok { - // found blocker balance - return nil, nil - } - } - cc.Timespans.Decompress() - //log.Printf("CC: %+v", cc) - - for tsIndex, ts := range cc.Timespans { - if ts.Increments == nil { - ts.createIncrementsSlice() - } - if ts.RateInterval == nil { - utils.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") - } - - if tsIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee && cc.deductConnectFee && ok { - - inc := &Increment{ - Duration: 0, - Cost: ts.RateInterval.Rating.ConnectFee, - BalanceInfo: &DebitInfo{ - Monetary: &MonetaryInfo{ - UUID: debitedConnectFeeBalance.Uuid, - ID: debitedConnectFeeBalance.ID, - Value: debitedConnectFeeBalance.Value, - }, - AccountID: ub.ID, - }, - } - - incs := []*Increment{inc} - ts.Increments = append(incs, ts.Increments...) - } - - maxCost, strategy := ts.RateInterval.GetMaxCost() - for incIndex, inc := range ts.Increments { - if tsIndex == 0 && incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee && cc.deductConnectFee && ok { - // go to nextincrement - continue - } - - // debit minutes and money - amount := float64(inc.Duration.Nanoseconds()) - if b.Factors != nil { - amount = utils.Round(amount/b.Factors.GetValue(cd.ToR), globalRoundingDecimals, utils.MetaRoundingUp) - } - cost := inc.Cost - if strategy == utils.MetaMaxCostDisconnect && cd.MaxCostSoFar >= maxCost { - // cut the entire current timespan - cc.maxCostDisconect = true - if dryRun { - if incIndex == 0 { - // cut the entire current timespan - cc.Timespans = cc.Timespans[:tsIndex] - } else { - ts.SplitByIncrement(incIndex) - cc.Timespans = cc.Timespans[:tsIndex+1] - } - return cc, nil - } - } - if strategy == utils.MetaMaxCostFree && cd.MaxCostSoFar >= maxCost { - cost, inc.Cost = 0.0, 0.0 - inc.BalanceInfo.Monetary = &MonetaryInfo{ - UUID: b.Uuid, - ID: b.ID, - Value: b.Value, - RateInterval: ts.RateInterval, - } - inc.BalanceInfo.AccountID = ub.ID - if count { - ub.countUnits(cost, utils.MetaMonetary, cc, b, fltrS) - } - // go to nextincrement - continue - } - var moneyBal *Balance - for _, mb := range moneyBalances { - if mb.GetValue() >= cost { - moneyBal = mb - break - } - } - if cost != 0 && moneyBal == nil && (!dryRun || ub.AllowNegative) { // Fix for issue #685 - utils.Logger.Warning(fmt.Sprintf(" Going negative on account %s with AllowNegative: false", cd.GetAccountKey())) - moneyBal = ub.GetDefaultMoneyBalance() - } - if b.GetValue() >= amount && (moneyBal != nil || cost == 0) { - b.SubtractValue(amount) - inc.BalanceInfo.Unit = &UnitInfo{ - UUID: b.Uuid, - ID: b.ID, - Value: b.Value, - DestinationID: cc.Destination, - Consumed: amount, - ToR: cc.ToR, - RateInterval: ts.RateInterval, - } - inc.BalanceInfo.AccountID = ub.ID - if cost != 0 { - moneyBal.SubtractValue(cost) - inc.BalanceInfo.Monetary = &MonetaryInfo{ - UUID: moneyBal.Uuid, - ID: moneyBal.ID, - Value: moneyBal.Value, - } - cd.MaxCostSoFar += cost - } - if count { - ub.countUnits(amount, cc.ToR, cc, b, fltrS) - if cost != 0 { - ub.countUnits(cost, utils.MetaMonetary, cc, moneyBal, fltrS) - } - } - } else { - // delete the rest of the unpaid increments/timespans - if incIndex == 0 { - // cut the entire current timespan - cc.Timespans = cc.Timespans[:tsIndex] - } else { - ts.SplitByIncrement(incIndex) - cc.Timespans = cc.Timespans[:tsIndex+1] - } - if len(cc.Timespans) == 0 { - cc = nil - } - return cc, nil - } - } - } - } - return -} - -func (b *Balance) debitMoney(cd *CallDescriptor, ub *Account, moneyBalances Balances, count bool, dryRun, debitConnectFee bool, fltrS *FilterS) (cc *CallCost, err error) { - if !b.IsActiveAt(cd.TimeStart) || b.GetValue() <= 0 { - return - } - //log.Print("B: ", utils.ToJSON(b)) - //log.Printf("}}}}}}} %+v", cd.testCallcost) - cc, err = b.GetCost(cd, true) - if err != nil { - return nil, err - } - - var debitedConnectFeeBalance Balance - var ok bool - //log.Print("cc: " + utils.ToJSON(cc)) - if debitConnectFee { - // this is the first add, debit the connect fee - if ok, debitedConnectFeeBalance = ub.DebitConnectionFee(cc, moneyBalances, count, true, fltrS); !ok { - // balance is blocker - return nil, nil - } - } - - cc.Timespans.Decompress() - //log.Printf("CallCost In Debit: %+v", cc) - //for _, ts := range cc.Timespans { - // log.Printf("CC_TS: %+v", ts.RateInterval.Rating.Rates[0]) - //} - for tsIndex, ts := range cc.Timespans { - if ts.Increments == nil { - ts.createIncrementsSlice() - } - //log.Printf("TS: %+v", ts) - if ts.RateInterval == nil { - utils.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") - } - - if tsIndex == 0 && - ts.RateInterval.Rating.ConnectFee > 0 && - debitConnectFee && - cc.deductConnectFee && - ok { - - inc := &Increment{ - Duration: 0, - Cost: ts.RateInterval.Rating.ConnectFee, - BalanceInfo: &DebitInfo{ - Monetary: &MonetaryInfo{ - UUID: debitedConnectFeeBalance.Uuid, - ID: debitedConnectFeeBalance.ID, - Value: debitedConnectFeeBalance.Value, - }, - AccountID: ub.ID, - }, - } - - incs := []*Increment{inc} - ts.Increments = append(incs, ts.Increments...) - } - - maxCost, strategy := ts.RateInterval.GetMaxCost() - //log.Printf("Timing: %+v", ts.RateInterval.Timing) - //log.Printf("RGRate: %+v", ts.RateInterval.Rating) - for incIndex, inc := range ts.Increments { - // check standard subject tags - //log.Printf("INC: %+v", inc) - - if tsIndex == 0 && - incIndex == 0 && - ts.RateInterval.Rating.ConnectFee > 0 && - cc.deductConnectFee && - ok { - // go to nextincrement - continue - } - - amount := inc.Cost - if strategy == utils.MetaMaxCostDisconnect && cd.MaxCostSoFar >= maxCost { - // cut the entire current timespan - cc.maxCostDisconect = true - if dryRun { - if incIndex == 0 { - // cut the entire current timespan - cc.Timespans = cc.Timespans[:tsIndex] - } else { - ts.SplitByIncrement(incIndex) - cc.Timespans = cc.Timespans[:tsIndex+1] - } - return cc, nil - } - } - if strategy == utils.MetaMaxCostFree && cd.MaxCostSoFar >= maxCost { - amount, inc.Cost = 0.0, 0.0 - inc.BalanceInfo.Monetary = &MonetaryInfo{ - UUID: b.Uuid, - ID: b.ID, - Value: b.Value, - } - inc.BalanceInfo.AccountID = ub.ID - if b.RatingSubject != "" { - inc.BalanceInfo.Monetary.RateInterval = ts.RateInterval - } - if count { - ub.countUnits(amount, utils.MetaMonetary, cc, b, fltrS) - } - - //log.Printf("TS: %+v", cc.Cost) - // go to nextincrement - continue - } - - if b.GetValue() >= amount { - b.SubtractValue(amount) - cd.MaxCostSoFar += amount - inc.BalanceInfo.Monetary = &MonetaryInfo{ - UUID: b.Uuid, - ID: b.ID, - Value: b.Value, - } - inc.BalanceInfo.AccountID = ub.ID - if b.RatingSubject != "" { - inc.BalanceInfo.Monetary.RateInterval = ts.RateInterval - } - if count { - ub.countUnits(amount, utils.MetaMonetary, cc, b, fltrS) - } - } else { - // delete the rest of the unpiad increments/timespans - if incIndex == 0 { - // cut the entire current timespan - cc.Timespans = cc.Timespans[:tsIndex] - } else { - ts.SplitByIncrement(incIndex) - cc.Timespans = cc.Timespans[:tsIndex+1] - } - if len(cc.Timespans) == 0 { - cc = nil - } - return cc, nil - } - } - } - //log.Printf("END: %+v", cd.testCallcost) - if len(cc.Timespans) == 0 { - cc = nil - } - return cc, nil -} - // AsBalanceSummary converts the balance towards compressed information to be displayed func (b *Balance) AsBalanceSummary(typ string) *BalanceSummary { bd := &BalanceSummary{ diff --git a/engine/balances_test.go b/engine/balances_test.go index b70083bcd..f9e05e5df 100644 --- a/engine/balances_test.go +++ b/engine/balances_test.go @@ -21,12 +21,10 @@ import ( "bytes" "log" "os" - "reflect" "strings" "testing" "time" - "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) @@ -494,435 +492,6 @@ func TestBalancesHasBalance(t *testing.T) { } -func TestBalanceDebitUnits(t *testing.T) { - cfg := config.NewDefaultCGRConfig() - db := NewInternalDB(nil, nil, true, nil) - dm := NewDataManager(db, cfg.CacheCfg(), nil) - cd := &CallDescriptor{ - Category: "postpaid", - ToR: utils.MetaVoice, - Tenant: "foehn", - Subject: "foehn", - Account: "foehn", - Destination: "0034678096720", - TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - LoopIndex: 0, - DurationIndex: 17 * time.Second, - - FallbackSubject: "", - RatingInfos: RatingInfos{ - &RatingInfo{ - MatchedSubject: "*out:foehn:postpaid:foehn", - MatchedPrefix: "0034678", - MatchedDestId: "SPN_MOB", - ActivationTime: time.Date(2015, 4, 23, 0, 0, 0, 0, time.UTC), - RateIntervals: []*RateInterval{ - { - Timing: &RITiming{ - WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, - StartTime: "08:00:00", - }, - Rating: &RIRate{ - ConnectFee: 0, - RoundingMethod: "*up", - RoundingDecimals: 6, - Rates: RateGroups{ - &RGRate{Value: 1, RateIncrement: time.Second, RateUnit: time.Second}, - }, - }, - }, - }, - }, - }, - } - ub := &Account{ - ID: "vdf:broker", - BalanceMap: map[string]Balances{ - utils.MetaVoice: { - &Balance{Value: 20 * float64(time.Second), - DestinationIDs: utils.NewStringMap("NAT"), - Weight: 10, RatingSubject: "rif"}, - &Balance{Value: 100 * float64(time.Second), - DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, - }}, - } - - moneyBalances := Balances{{ - Uuid: "uuid", - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: true, - precision: 2, - }} - b := &Balance{ - Uuid: "uuid", - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: false, - precision: 2, - RatingSubject: "*zero34", - } - fltrs := FilterS{cfg, dm, nil} - config.SetCgrConfig(cfg) - exp := &CallCost{Category: "postpaid", - Tenant: "foehn", - Subject: "foehn", Account: "foehn", - Destination: "0034678096720", ToR: "*voice", - Cost: 0, - Timespans: TimeSpans{ - {TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - Cost: 0, - RateInterval: &RateInterval{ - Rating: &RIRate{ - ConnectFee: 0, - RoundingDecimals: 0, - MaxCost: 0, - Rates: RateGroups{ - { - GroupIntervalStart: 0, - Value: 0, - RateIncrement: 34, - RateUnit: 34}, - }}, - Weight: 0}, - DurationIndex: 26, - MatchedSubject: "uuid", - MatchedPrefix: "0034678096720", - MatchedDestId: "*any", - RatingPlanId: "*none", - CompressFactor: 0}}, - RatedUsage: 0, - } - - if val, err := b.debitUnits(cd, ub, moneyBalances, true, false, true, &fltrs); err != nil { - t.Errorf("received %v", err) - } else if reflect.DeepEqual(val, exp) { - t.Errorf("expected %+v ,received %+v", utils.ToJSON(exp), utils.ToJSON(val)) - } -} -func TestBalanceDebitMoneyMaxCostFree(t *testing.T) { - utils.Logger.SetLogLevel(3) - utils.Logger.SetSyslog(nil) - buf := new(bytes.Buffer) - log.SetOutput(buf) - defer func() { - utils.Logger.SetLogLevel(0) - log.SetOutput(os.Stderr) - }() - cd := &CallDescriptor{ - Category: "postpaid", - ToR: utils.MetaVoice, - Tenant: "foehn", - TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - MaxCostSoFar: 33, - testCallcost: &CallCost{ - Category: "generic", - Tenant: "cgrates.org", - Subject: "1001", - Account: "1001", - Destination: "data", - ToR: "*data", - Cost: 0, - deductConnectFee: true, - Timespans: TimeSpans{ - { - TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), - DurationIndex: 0, - Increments: Increments{ - {Cost: 2, BalanceInfo: &DebitInfo{ - Monetary: &MonetaryInfo{UUID: "moneya"}}, - }}, - RateInterval: &RateInterval{ - Rating: &RIRate{ - ConnectFee: 0.15, - MaxCost: 23.2, - MaxCostStrategy: utils.MetaMaxCostFree, - Rates: RateGroups{&RGRate{GroupIntervalStart: 0, - Value: 0.1, RateIncrement: time.Second, - RateUnit: time.Second}}}}, - }, - }, - }, - FallbackSubject: "", - } - ub := &Account{ - ID: "vdf:broker", - BalanceMap: map[string]Balances{ - utils.MetaVoice: { - &Balance{Value: 20 * float64(time.Second), - DestinationIDs: utils.NewStringMap("NAT"), - Weight: 10, RatingSubject: "rif"}, - &Balance{Value: 100 * float64(time.Second), - DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, - }}, - } - moneyBalances := Balances{} - b := &Balance{ - Uuid: "uuid", - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: false, - precision: 2, - RatingSubject: "*val34", - Factors: ValueFactors{ - "FACT_VAL": 20.22, - }, - } - - if _, err := b.debitMoney(cd, ub, moneyBalances, true, true, true, nil); err != nil { - t.Error(err) - } -} -func TestBalanceDebitMoneyMaxCostDisconnect(t *testing.T) { - utils.Logger.SetLogLevel(3) - utils.Logger.SetSyslog(nil) - buf := new(bytes.Buffer) - log.SetOutput(buf) - defer func() { - utils.Logger.SetLogLevel(0) - log.SetOutput(os.Stderr) - }() - cd := &CallDescriptor{ - Category: "postpaid", - ToR: utils.MetaVoice, - Tenant: "foehn", - TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - MaxCostSoFar: 33, - testCallcost: &CallCost{ - Category: "generic", - Tenant: "cgrates.org", - Subject: "1001", - Account: "1001", - Destination: "data", - ToR: "*data", - Cost: 0, - deductConnectFee: true, - Timespans: TimeSpans{ - { - TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), - DurationIndex: 0, - Increments: Increments{ - {Cost: 2, BalanceInfo: &DebitInfo{ - Monetary: &MonetaryInfo{UUID: "moneya"}}, - }}, - }, - }, - }, - FallbackSubject: "", - } - ub := &Account{ - ID: "vdf:broker", - BalanceMap: map[string]Balances{ - utils.MetaVoice: { - &Balance{Value: 20 * float64(time.Second), - DestinationIDs: utils.NewStringMap("NAT"), - Weight: 10, RatingSubject: "rif"}, - &Balance{Value: 100 * float64(time.Second), - DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, - }}, - } - moneyBalances := Balances{} - b := &Balance{ - Uuid: "uuid", - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: false, - precision: 2, - RatingSubject: "*val34", - Factors: ValueFactors{ - "FACT_VAL": 20.22, - }, - } - expLog := `Nil RateInterval ERROR on TS:` - if _, err := b.debitMoney(cd, ub, moneyBalances, true, true, true, nil); err == nil { - t.Error(err) - } else if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog) { - t.Errorf("Logger %v,doesn't contain %v", rcvLog, expLog) - } - cd.testCallcost.Timespans[0].RateInterval = &RateInterval{ - Rating: &RIRate{ - ConnectFee: 0.15, - MaxCost: 23.2, - MaxCostStrategy: utils.MetaMaxCostDisconnect, - Rates: RateGroups{&RGRate{GroupIntervalStart: 0, - Value: 0.1, RateIncrement: time.Second, - RateUnit: time.Second}}}} - if _, err := b.debitMoney(cd, ub, moneyBalances, true, true, true, nil); err != nil { - t.Error(err) - } -} -func TestBalanceDebitMoney(t *testing.T) { - utils.Logger.SetLogLevel(3) - utils.Logger.SetSyslog(nil) - buf := new(bytes.Buffer) - log.SetOutput(buf) - defer func() { - utils.Logger.SetLogLevel(0) - log.SetOutput(os.Stderr) - }() - cd := &CallDescriptor{ - Category: "postpaid", - ToR: utils.MetaVoice, - Tenant: "foehn", - TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - MaxCostSoFar: 33, - testCallcost: &CallCost{ - Category: "generic", - Tenant: "cgrates.org", - Subject: "1001", - Account: "1001", - Destination: "data", - ToR: "*data", - Cost: 0, - deductConnectFee: true, - Timespans: TimeSpans{ - { - TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), - DurationIndex: 0, - Increments: Increments{ - {Cost: 2, BalanceInfo: &DebitInfo{ - Monetary: &MonetaryInfo{UUID: "moneya"}}, - }}, - RateInterval: &RateInterval{ - Rating: &RIRate{ - ConnectFee: 0.15, - MaxCost: 23.2, - - Rates: RateGroups{&RGRate{GroupIntervalStart: 0, - Value: 0.1, RateIncrement: time.Second, - RateUnit: time.Second}}}}, - }, - }, - }, - FallbackSubject: "", - } - ub := &Account{ - ID: "vdf:broker", - BalanceMap: map[string]Balances{ - utils.MetaVoice: { - &Balance{Value: 20 * float64(time.Second), - DestinationIDs: utils.NewStringMap("NAT"), - Weight: 10, RatingSubject: "rif"}, - &Balance{Value: 100 * float64(time.Second), - DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, - }}, - } - moneyBalances := Balances{} - b := &Balance{ - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - precision: 2, - RatingSubject: "*val34", - Factors: ValueFactors{ - "FACT_VAL": 20.22, - }, - } - if _, err := b.debitMoney(cd, ub, moneyBalances, true, true, true, nil); err != nil { - t.Error(err) - } -} -func TestBalanceDebitUnits2(t *testing.T) { - cfg := config.NewDefaultCGRConfig() - db := NewInternalDB(nil, nil, true, nil) - dm := NewDataManager(db, cfg.CacheCfg(), nil) - cd := &CallDescriptor{ - Category: "postpaid", - ToR: utils.MetaVoice, - Tenant: "foehn", - TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - MaxCostSoFar: 23.8, - testCallcost: &CallCost{ - Category: "generic", - Tenant: "cgrates.org", - Subject: "1001", - Account: "1001", - - Destination: "data", - ToR: "*data", - Cost: 0, - deductConnectFee: true, - Timespans: TimeSpans{ - { - TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), - DurationIndex: 0, - Increments: Increments{ - {Cost: 2, BalanceInfo: &DebitInfo{ - Monetary: &MonetaryInfo{UUID: "moneya"}}, - }}, - RateInterval: &RateInterval{ - Rating: &RIRate{ - ConnectFee: 0.15, - MaxCost: 23.2, - MaxCostStrategy: utils.MetaMaxCostDisconnect, - Rates: RateGroups{&RGRate{GroupIntervalStart: 0, - Value: 0.1, RateIncrement: time.Second, - RateUnit: time.Second}}}}, - }, - }, - }, - FallbackSubject: "", - } - ub := &Account{ - ID: "vdf:broker", - BalanceMap: map[string]Balances{ - utils.MetaVoice: { - &Balance{Value: 20 * float64(time.Second), - DestinationIDs: utils.NewStringMap("NAT"), - Weight: 10, RatingSubject: "rif"}, - &Balance{Value: 100 * float64(time.Second), - DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, - }}, - } - - moneyBalances := Balances{{ - Uuid: "uuid", - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: true, - precision: 2, - }} - b := &Balance{ - Uuid: "uuid", - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: false, - precision: 2, - RatingSubject: "*val34", - Factors: ValueFactors{ - "FACT_VAL": 20.22, - }, - } - fltrs := FilterS{cfg, dm, nil} - config.SetCgrConfig(cfg) - - if _, err := b.debitUnits(cd, ub, moneyBalances, true, true, true, &fltrs); err != nil { - t.Errorf("received %v", err) - } -} - func TestGetMinutesForCredi(t *testing.T) { utils.Logger.SetLogLevel(4) utils.Logger.SetSyslog(nil) @@ -957,133 +526,3 @@ func TestGetMinutesForCredi(t *testing.T) { t.Errorf("expected %v,received %v", utils.ToJSON(expLog), utils.ToJSON(rcvLog)) } } - -func TestBalanceDebitUnits3(t *testing.T) { - cc := &CallCost{ - Destination: "0723045326", - Timespans: []*TimeSpan{ - { - TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), - DurationIndex: 0, - RateInterval: &RateInterval{ - Rating: &RIRate{Rates: RateGroups{ - &RGRate{GroupIntervalStart: 0, Value: 1, - RateIncrement: 10 * time.Second, - RateUnit: time.Second}}}}, - }, - { - TimeStart: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 24, 10, 49, 20, 0, time.UTC), - DurationIndex: 10 * time.Second, - RateInterval: &RateInterval{ - Rating: &RIRate{Rates: RateGroups{ - &RGRate{GroupIntervalStart: 0, - Value: 1, - RateIncrement: 10 * time.Second, - RateUnit: time.Second}}}}, - }, - }, - ToR: utils.MetaVoice, - } - b1 := &Balance{ - Uuid: "testb", Value: 10 * float64(time.Second), Weight: 10, - DestinationIDs: utils.StringMap{"NAT": true}, - RatingSubject: "*zero1s"} - cd := &CallDescriptor{ - TimeStart: cc.Timespans[0].TimeStart, - TimeEnd: cc.Timespans[1].TimeEnd, - Destination: cc.Destination, - ToR: cc.ToR, - DurationIndex: cc.GetDuration(), - testCallcost: cc, - } - rifsBalance := &Account{ID: "other", BalanceMap: map[string]Balances{ - utils.MetaVoice: {b1}, - utils.MetaMonetary: {{Uuid: "moneya", Value: 110}}, - }} - moneyBalances := Balances{ - {Uuid: "moneyc", Value: 130, SharedGroups: utils.NewStringMap("SG_TEST")}, - } - if _, err := b1.debitUnits(cd, rifsBalance, moneyBalances, true, true, true, nil); err != nil { - t.Error(err) - } -} - -func TestBalanceDebitUnits5(t *testing.T) { - utils.Logger.SetLogLevel(3) - utils.Logger.SetSyslog(nil) - buf := new(bytes.Buffer) - log.SetOutput(buf) - defer func() { - utils.Logger.SetLogLevel(0) - log.SetOutput(os.Stderr) - }() - cd := &CallDescriptor{ - Category: "postpaid", - ToR: utils.MetaVoice, - Tenant: "foehn", - TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - MaxCostSoFar: 23.8, - testCallcost: &CallCost{ - Category: "generic", - Tenant: "cgrates.org", - Subject: "1001", - Account: "1001", - Destination: "data", - ToR: "*data", - Cost: 0, - deductConnectFee: true, - Timespans: TimeSpans{ - { - TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), - TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), - DurationIndex: 0, - Increments: Increments{ - {Cost: 2, BalanceInfo: &DebitInfo{ - Monetary: &MonetaryInfo{UUID: "moneya"}}, - }}, - }, - }, - }, - FallbackSubject: "", - } - ub := &Account{ - ID: "vdf:broker", - BalanceMap: map[string]Balances{ - utils.MetaVoice: { - &Balance{Value: 20 * float64(time.Second), - DestinationIDs: utils.NewStringMap("NAT"), - Weight: 10, RatingSubject: "rif"}, - &Balance{Value: 100 * float64(time.Second), - DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, - }}, - } - moneyBalances := Balances{{ - Uuid: "uuid", - ID: "id", - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: true, - precision: 2, - }} - b := &Balance{ - Value: 12.22, - ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: false, - precision: 2, - RatingSubject: "*val34", - Factors: ValueFactors{ - "FACT_VAL": 20.22, - }, - } - expLog := `Nil RateInterval ERROR on TS` - if _, err := b.debitUnits(cd, ub, moneyBalances, true, true, true, nil); err == nil || err.Error() != "timespan with no rate interval assigned" { - t.Error(err) - } else if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog) { - t.Errorf("Logger %v doesn't contain %v", rcvLog, expLog) - } -}