Update balance factor to depend on category

Updated unit tests to reflect that also.
This commit is contained in:
ionutboangiu
2024-01-25 11:19:39 -05:00
committed by Dan Christian Bogos
parent dca2c4b6a1
commit 5c96ecf27e
3 changed files with 15 additions and 7 deletions

View File

@@ -1703,6 +1703,7 @@ func TestDebitGeneric(t *testing.T) {
func TestDebitGenericBalance(t *testing.T) {
cc := &CallCost{
Category: "call",
Destination: "0723045326",
Timespans: []*TimeSpan{
{
@@ -1722,6 +1723,7 @@ func TestDebitGenericBalance(t *testing.T) {
ToR: utils.MetaVoice,
}
cd := &CallDescriptor{
Category: "call",
TimeStart: cc.Timespans[0].TimeStart,
TimeEnd: cc.Timespans[0].TimeEnd,
Destination: cc.Destination,
@@ -1734,7 +1736,7 @@ func TestDebitGenericBalance(t *testing.T) {
utils.MetaGeneric: {
&Balance{Uuid: "testm", Value: 100, Weight: 5,
DestinationIDs: utils.StringMap{"NAT": true},
Factor: ValueFactor{utils.MetaVoice: 60 * float64(time.Second)}}},
Factor: ValueFactor{"call": 60 * float64(time.Second)}}},
utils.MetaMonetary: {&Balance{Value: 21}},
}}
var err error
@@ -1756,6 +1758,7 @@ func TestDebitGenericBalance(t *testing.T) {
func TestDebitGenericBalanceWithRatingSubject(t *testing.T) {
cc := &CallCost{
Category: "call",
Destination: "0723045326",
Timespans: []*TimeSpan{
{
@@ -1774,6 +1777,7 @@ func TestDebitGenericBalanceWithRatingSubject(t *testing.T) {
ToR: utils.MetaVoice,
}
cd := &CallDescriptor{
Category: "call",
TimeStart: cc.Timespans[0].TimeStart,
TimeEnd: cc.Timespans[0].TimeEnd,
Destination: cc.Destination,
@@ -1786,7 +1790,7 @@ func TestDebitGenericBalanceWithRatingSubject(t *testing.T) {
utils.MetaGeneric: {
&Balance{Uuid: "testm", Value: 100,
Weight: 5, DestinationIDs: utils.StringMap{"NAT": true},
Factor: ValueFactor{utils.MetaVoice: 60 * float64(time.Second)},
Factor: ValueFactor{"call": 60 * float64(time.Second)},
RatingSubject: "free"}},
utils.MetaMonetary: {&Balance{Value: 21}},
}}

View File

@@ -770,8 +770,8 @@ func (bc Balances) SaveDirtyBalances(acc *Account, initBal map[string]float64) {
type ValueFactor map[string]float64
func (f ValueFactor) GetValue(tor string) float64 {
if value, ok := f[tor]; ok {
func (f ValueFactor) GetValue(category string) float64 {
if value, ok := f[category]; ok {
return value
}
return 1.0
@@ -876,7 +876,7 @@ func (b *Balance) debit(cd *CallDescriptor, ub *Account, moneyBalances Balances,
//log.Printf("INCREMENET: %+v", inc)
amount := float64(inc.Duration)
if b.Factor != nil {
amount = utils.Round(amount/b.Factor.GetValue(tor),
amount = utils.Round(amount/b.Factor.GetValue(cd.Category),
globalRoundingDecimals, utils.MetaRoundingUp)
}
if b.GetValue() >= amount {
@@ -887,7 +887,8 @@ func (b *Balance) debit(cd *CallDescriptor, ub *Account, moneyBalances Balances,
Value: b.Value,
DestinationID: cc.Destination,
Consumed: amount,
ToR: tor, //aici
Category: cd.Category,
ToR: tor,
RateInterval: nil,
}
inc.BalanceInfo.AccountID = ub.ID
@@ -1019,7 +1020,7 @@ func (b *Balance) debit(cd *CallDescriptor, ub *Account, moneyBalances Balances,
var moneyBal *Balance
if isUnitBal {
if b.Factor != nil {
amount = utils.Round(amount/b.Factor.GetValue(cd.ToR), globalRoundingDecimals, utils.MetaRoundingUp)
amount = utils.Round(amount/b.Factor.GetValue(cd.Category), globalRoundingDecimals, utils.MetaRoundingUp)
}
for _, mb := range moneyBalances {
if mb.GetValue() >= cost {
@@ -1056,6 +1057,7 @@ func (b *Balance) debit(cd *CallDescriptor, ub *Account, moneyBalances Balances,
Value: b.Value,
DestinationID: cc.Destination,
Consumed: amount,
Category: cc.Category,
ToR: cc.ToR,
RateInterval: ts.RateInterval,
}

View File

@@ -108,6 +108,7 @@ type UnitInfo struct {
Value float64
DestinationID string
Consumed float64
Category string
ToR string
RateInterval *RateInterval
}
@@ -128,6 +129,7 @@ func (ui *UnitInfo) Equal(other *UnitInfo) bool {
ui.DestinationID == other.DestinationID &&
ui.Consumed == other.Consumed &&
ui.ToR == other.ToR &&
ui.Category == other.Category &&
reflect.DeepEqual(ui.RateInterval, other.RateInterval)
}