mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
better balance modyfy event
This commit is contained in:
@@ -601,7 +601,7 @@ func (self *ApierV1) GetActions(actsId string, reply *[]*utils.TPAction) error {
|
||||
Weight: engAct.Weight,
|
||||
}
|
||||
if engAct.Balance != nil {
|
||||
act.Units = engAct.Balance.Value
|
||||
act.Units = engAct.Balance.GetValue()
|
||||
act.DestinationIds = engAct.Balance.DestinationIds
|
||||
act.RatingSubject = engAct.Balance.RatingSubject
|
||||
act.SharedGroup = engAct.Balance.SharedGroup
|
||||
|
||||
@@ -115,16 +115,16 @@ func (ub *Account) debitBalanceAction(a *Action, reset bool) error {
|
||||
}
|
||||
if b.MatchFilter(a.Balance) {
|
||||
if reset {
|
||||
b.Value = 0
|
||||
b.SetValue(0)
|
||||
}
|
||||
b.SubstractAmount(bClone.Value)
|
||||
b.SubstractValue(bClone.GetValue())
|
||||
found = true
|
||||
}
|
||||
}
|
||||
// if it is not found then we add it to the list
|
||||
if !found {
|
||||
if bClone.Value != 0 {
|
||||
bClone.Value = -bClone.Value
|
||||
if bClone.GetValue() != 0 {
|
||||
bClone.SetValue(-bClone.GetValue())
|
||||
}
|
||||
bClone.dirty = true // Mark the balance as dirty since we have modified and it should be checked by action triggers
|
||||
ub.BalanceMap[id] = append(ub.BalanceMap[id], bClone)
|
||||
@@ -150,7 +150,7 @@ func (ub *Account) debitBalanceAction(a *Action, reset bool) error {
|
||||
func (ub *Account) getBalancesForPrefix(prefix, category string, balances BalanceChain, sharedGroup string) BalanceChain {
|
||||
var usefulBalances BalanceChain
|
||||
for _, b := range balances {
|
||||
if b.IsExpired() || (b.SharedGroup == "" && b.Value <= 0) {
|
||||
if b.IsExpired() || (b.SharedGroup == "" && b.GetValue() <= 0) {
|
||||
continue
|
||||
}
|
||||
if sharedGroup != "" && b.SharedGroup != sharedGroup {
|
||||
@@ -345,7 +345,7 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo
|
||||
for _, increment := range ts.Increments {
|
||||
cost := increment.Cost
|
||||
defaultBalance := ub.GetDefaultMoneyBalance(leftCC.Direction)
|
||||
defaultBalance.SubstractAmount(cost)
|
||||
defaultBalance.SubstractValue(cost)
|
||||
increment.BalanceInfo.MoneyBalanceUuid = defaultBalance.Uuid
|
||||
increment.BalanceInfo.AccountId = ub.Id
|
||||
increment.paid = true
|
||||
@@ -390,7 +390,7 @@ func (ub *Account) refundIncrement(increment *Increment, direction, unitType str
|
||||
if balance = ub.BalanceMap[unitType+direction].GetBalance(increment.BalanceInfo.UnitBalanceUuid); balance == nil {
|
||||
return
|
||||
}
|
||||
balance.Value += increment.Duration.Seconds()
|
||||
balance.AddValue(increment.Duration.Seconds())
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceType: unitType, Direction: direction, Balance: &Balance{Value: -increment.Duration.Seconds()}})
|
||||
}
|
||||
@@ -400,7 +400,7 @@ func (ub *Account) refundIncrement(increment *Increment, direction, unitType str
|
||||
if balance = ub.BalanceMap[utils.MONETARY+direction].GetBalance(increment.BalanceInfo.MoneyBalanceUuid); balance == nil {
|
||||
return
|
||||
}
|
||||
balance.Value += increment.Cost
|
||||
balance.AddValue(increment.Cost)
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: direction, Balance: &Balance{Value: -increment.Cost}})
|
||||
}
|
||||
@@ -429,12 +429,12 @@ func (ub *Account) executeActionTriggers(a *Action) {
|
||||
if uc.BalanceType == at.BalanceType {
|
||||
for _, mb := range uc.Balances {
|
||||
if strings.Contains(at.ThresholdType, "*max") {
|
||||
if mb.MatchActionTrigger(at) && mb.Value >= at.ThresholdValue {
|
||||
if mb.MatchActionTrigger(at) && mb.GetValue() >= at.ThresholdValue {
|
||||
// run the actions
|
||||
at.Execute(ub, nil)
|
||||
}
|
||||
} else { //MIN
|
||||
if mb.MatchActionTrigger(at) && mb.Value <= at.ThresholdValue {
|
||||
if mb.MatchActionTrigger(at) && mb.GetValue() <= at.ThresholdValue {
|
||||
// run the actions
|
||||
at.Execute(ub, nil)
|
||||
}
|
||||
@@ -448,12 +448,12 @@ func (ub *Account) executeActionTriggers(a *Action) {
|
||||
continue
|
||||
}
|
||||
if strings.Contains(at.ThresholdType, "*max") {
|
||||
if b.MatchActionTrigger(at) && b.Value >= at.ThresholdValue {
|
||||
if b.MatchActionTrigger(at) && b.GetValue() >= at.ThresholdValue {
|
||||
// run the actions
|
||||
at.Execute(ub, nil)
|
||||
}
|
||||
} else { //MIN
|
||||
if b.MatchActionTrigger(at) && b.Value <= at.ThresholdValue {
|
||||
if b.MatchActionTrigger(at) && b.GetValue() <= at.ThresholdValue {
|
||||
// run the actions
|
||||
at.Execute(ub, nil)
|
||||
}
|
||||
@@ -513,7 +513,7 @@ func (ub *Account) countUnits(a *Action) {
|
||||
ub.UnitCounters = append(ub.UnitCounters, unitsCounter)
|
||||
}
|
||||
|
||||
unitsCounter.addUnits(a.Balance.Value, a.Balance.DestinationIds) // DestinationIds is actually a destination (number or prefix)
|
||||
unitsCounter.addUnits(a.Balance.GetValue(), a.Balance.DestinationIds) // DestinationIds is actually a destination (number or prefix)
|
||||
ub.executeActionTriggers(nil)
|
||||
}
|
||||
|
||||
@@ -539,7 +539,7 @@ func (ub *Account) initCounters() {
|
||||
ub.UnitCounters = append(ub.UnitCounters, uc)
|
||||
}
|
||||
b := a.Balance.Clone()
|
||||
b.Value = 0
|
||||
b.SetValue(0)
|
||||
uc.Balances = append(uc.Balances, b)
|
||||
uc.Balances.Sort()
|
||||
}
|
||||
@@ -634,8 +634,8 @@ func (acc *Account) DebitConnectionFee(cc *CallCost, usefulMoneyBalances Balance
|
||||
//log.Print("CONNECT FEE: %f", connectFee)
|
||||
connectFeePaid := false
|
||||
for _, b := range usefulMoneyBalances {
|
||||
if b.Value >= connectFee {
|
||||
b.SubstractAmount(connectFee)
|
||||
if b.GetValue() >= connectFee {
|
||||
b.SubstractValue(connectFee)
|
||||
// the conect fee is not refundable!
|
||||
if count {
|
||||
acc.countUnits(&Action{BalanceType: utils.MONETARY, Direction: cc.Direction, Balance: &Balance{Value: connectFee, DestinationIds: cc.Destination}})
|
||||
@@ -647,7 +647,7 @@ func (acc *Account) DebitConnectionFee(cc *CallCost, usefulMoneyBalances Balance
|
||||
// debit connect fee
|
||||
if connectFee > 0 && !connectFeePaid {
|
||||
// there are no money for the connect fee; go negative
|
||||
acc.GetDefaultMoneyBalance(cc.Direction).Value -= connectFee
|
||||
acc.GetDefaultMoneyBalance(cc.Direction).SubstractValue(connectFee)
|
||||
// the conect fee is not refundable!
|
||||
if count {
|
||||
acc.countUnits(&Action{BalanceType: utils.MONETARY, Direction: cc.Direction, Balance: &Balance{Value: connectFee, DestinationIds: cc.Destination}})
|
||||
|
||||
@@ -194,8 +194,8 @@ func TestDebitCreditZeroSecond(t *testing.T) {
|
||||
t.Logf("%+v", cc.Timespans[0])
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 21 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 21 {
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0])
|
||||
}
|
||||
}
|
||||
@@ -238,8 +238,8 @@ func TestDebitCreditZeroMinute(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 10 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 21 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 10 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 21 {
|
||||
t.Error("Error extracting minutes from balance: ",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0])
|
||||
}
|
||||
@@ -284,9 +284,9 @@ func TestDebitCreditZeroMixedMinute(t *testing.T) {
|
||||
cc.Timespans[1].Increments[0].BalanceInfo.UnitBalanceUuid != "testm" {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans)
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][1].Value != 0 ||
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 10 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 21 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][1].GetValue() != 0 ||
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 10 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 21 {
|
||||
t.Logf("TS0: %+v", cc.Timespans[0])
|
||||
t.Logf("TS1: %+v", cc.Timespans[1])
|
||||
t.Errorf("Error extracting minutes from balance: %+v", rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][1])
|
||||
@@ -335,7 +335,7 @@ func TestDebitCreditNoCredit(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 10 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 10 {
|
||||
t.Error("Error extracting minutes from balance: ",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0])
|
||||
}
|
||||
@@ -387,10 +387,10 @@ func TestDebitCreditHasCredit(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 10 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 30 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 10 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 30 {
|
||||
t.Errorf("Error extracting minutes from balance: %+v, %+v",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
if len(cc.Timespans) != 3 || cc.Timespans[0].GetDuration() != time.Minute {
|
||||
t.Error("Error truncating extra timespans: ", cc.Timespans)
|
||||
@@ -435,10 +435,10 @@ func TestDebitCreditSplitMinutesMoney(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != 1*time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0].Duration)
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 30 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 30 {
|
||||
t.Errorf("Error extracting minutes from balance: %+v, %+v",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
if len(cc.Timespans) != 2 || cc.Timespans[0].GetDuration() != 10*time.Second || cc.Timespans[1].GetDuration() != 20*time.Second {
|
||||
t.Error("Error truncating extra timespans: ", cc.Timespans[1].GetDuration())
|
||||
@@ -487,7 +487,7 @@ func TestDebitCreditMoreTimespans(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 30 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 30 {
|
||||
t.Error("Error extracting minutes from balance: ",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0])
|
||||
}
|
||||
@@ -536,8 +536,8 @@ func TestDebitCreditMoreTimespansMixed(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 10 ||
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][1].Value != 130 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 10 ||
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][1].GetValue() != 130 {
|
||||
t.Error("Error extracting minutes from balance: ",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][1], cc.Timespans[1])
|
||||
}
|
||||
@@ -631,7 +631,7 @@ func TestDebitCreditMoneyOnly(t *testing.T) {
|
||||
t.Logf("%+v", cc.Timespans[0].Increments)
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0].BalanceInfo)
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 0 {
|
||||
if rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 0 {
|
||||
t.Error("Error extracting minutes from balance: ",
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0])
|
||||
}
|
||||
@@ -685,10 +685,10 @@ func TestDebitCreditSubjectMinutes(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != 10*time.Second {
|
||||
t.Errorf("Error setting balance id to increment: %+v", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 180 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 280 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 180 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 280 {
|
||||
t.Errorf("Error extracting minutes from balance: %+v, %+v",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
if len(cc.Timespans) != 1 || cc.Timespans[0].GetDuration() != 70*time.Second {
|
||||
for _, ts := range cc.Timespans {
|
||||
@@ -738,9 +738,9 @@ func TestDebitCreditSubjectMoney(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != 10*time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 5 {
|
||||
if rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 5 {
|
||||
t.Errorf("Error extracting minutes from balance: %+v",
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
if len(cc.Timespans) != 1 || cc.Timespans[0].GetDuration() != 70*time.Second {
|
||||
t.Error("Error truncating extra timespans: ", cc.Timespans)
|
||||
@@ -790,10 +790,10 @@ func TestDebitCreditSubjectMoney(t *testing.T) {
|
||||
cc.Timespans[0].Increments[0].Duration != 10*time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 7 {
|
||||
if rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 7 {
|
||||
t.Errorf("Error extracting minutes from balance: %+v, %+v",
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
rifsBalance.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
if len(cc.Timespans) != 2 || cc.Timespans[0].GetDuration() != 40*time.Second {
|
||||
for _, ts := range cc.Timespans {
|
||||
@@ -827,7 +827,7 @@ func TestAccountdebitBalanceExists(t *testing.T) {
|
||||
newMb := &Balance{Value: -10, Weight: 20, DestinationIds: "NAT"}
|
||||
a := &Action{BalanceType: utils.VOICE, Direction: OUTBOUND, Balance: newMb}
|
||||
ub.debitBalanceAction(a, false)
|
||||
if len(ub.BalanceMap[utils.VOICE+OUTBOUND]) != 2 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 25 {
|
||||
if len(ub.BalanceMap[utils.VOICE+OUTBOUND]) != 2 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 25 {
|
||||
t.Error("Error adding minute bucket!")
|
||||
}
|
||||
}
|
||||
@@ -856,7 +856,7 @@ func TestAccountAddMinutBucketEmpty(t *testing.T) {
|
||||
}
|
||||
a = &Action{BalanceType: utils.VOICE, Direction: OUTBOUND, Balance: mb2}
|
||||
ub.debitBalanceAction(a, false)
|
||||
if len(ub.BalanceMap[utils.VOICE+OUTBOUND]) != 1 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 20 {
|
||||
if len(ub.BalanceMap[utils.VOICE+OUTBOUND]) != 1 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 20 {
|
||||
t.Error("Error adding minute bucket: ", ub.BalanceMap[utils.VOICE+OUTBOUND])
|
||||
}
|
||||
a = &Action{BalanceType: utils.VOICE, Direction: OUTBOUND, Balance: mb3}
|
||||
@@ -874,19 +874,19 @@ func TestAccountExecuteTriggeredActions(t *testing.T) {
|
||||
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: utils.MONETARY, BalanceDirection: OUTBOUND, ThresholdValue: 2, ThresholdType: TRIGGER_MAX_COUNTER, ActionsId: "TEST_ACTIONS"}},
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Balance: &Balance{Value: 1}})
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 110 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 20 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value, ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value)
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 110 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 20 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue(), ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue())
|
||||
}
|
||||
// are set to executed
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 1}})
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 110 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 20 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value, ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value)
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 110 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 20 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue(), ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue())
|
||||
}
|
||||
// we can reset them
|
||||
ub.ResetActionTriggers(nil)
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 120 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 30 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value, ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value)
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 120 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 30 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue(), ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -898,8 +898,8 @@ func TestAccountExecuteTriggeredActionsBalance(t *testing.T) {
|
||||
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: utils.MONETARY, BalanceDirection: OUTBOUND, ThresholdValue: 100, ThresholdType: TRIGGER_MIN_COUNTER, ActionsId: "TEST_ACTIONS"}},
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Balance: &Balance{Value: 1}})
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 110 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 20 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value, ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value)
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 110 || ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 20 {
|
||||
t.Error("Error executing triggered actions", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue(), ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -911,8 +911,8 @@ func TestAccountExecuteTriggeredActionsOrder(t *testing.T) {
|
||||
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: utils.MONETARY, BalanceDirection: OUTBOUND, ThresholdValue: 2, ThresholdType: TRIGGER_MAX_COUNTER, ActionsId: "TEST_ACTIONS_ORDER"}},
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 1}})
|
||||
if len(ub.BalanceMap[utils.MONETARY+OUTBOUND]) != 1 || ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 10 {
|
||||
t.Error("Error executing triggered actions in order", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
if len(ub.BalanceMap[utils.MONETARY+OUTBOUND]) != 1 || ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 10 {
|
||||
t.Error("Error executing triggered actions in order", ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -939,11 +939,11 @@ func TestCleanExpired(t *testing.T) {
|
||||
func TestAccountUnitCounting(t *testing.T) {
|
||||
ub := &Account{}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 10 {
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 10 {
|
||||
t.Error("Error counting units")
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 20 {
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 20 {
|
||||
t.Error("Error counting units")
|
||||
}
|
||||
}
|
||||
@@ -951,15 +951,15 @@ func TestAccountUnitCounting(t *testing.T) {
|
||||
func TestAccountUnitCountingOutbound(t *testing.T) {
|
||||
ub := &Account{}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 10 {
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 10 {
|
||||
t.Error("Error counting units")
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 20 {
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 20 {
|
||||
t.Error("Error counting units")
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 30 {
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 30 {
|
||||
t.Error("Error counting units")
|
||||
}
|
||||
}
|
||||
@@ -967,15 +967,15 @@ func TestAccountUnitCountingOutbound(t *testing.T) {
|
||||
func TestAccountUnitCountingOutboundInbound(t *testing.T) {
|
||||
ub := &Account{}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 10 {
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 10 {
|
||||
t.Errorf("Error counting units: %+v", ub.UnitCounters[0])
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: OUTBOUND, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 20 {
|
||||
if len(ub.UnitCounters) != 1 && ub.UnitCounters[0].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 20 {
|
||||
t.Error("Error counting units")
|
||||
}
|
||||
ub.countUnits(&Action{BalanceType: utils.MONETARY, Direction: INBOUND, Balance: &Balance{Value: 10}})
|
||||
if len(ub.UnitCounters) != 2 && ub.UnitCounters[1].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].Value != 20 || ub.UnitCounters[1].Balances[0].Value != 10 {
|
||||
if len(ub.UnitCounters) != 2 && ub.UnitCounters[1].BalanceType != utils.MONETARY || ub.UnitCounters[0].Balances[0].GetValue() != 20 || ub.UnitCounters[1].Balances[0].GetValue() != 10 {
|
||||
t.Error("Error counting units")
|
||||
}
|
||||
}
|
||||
@@ -1000,10 +1000,10 @@ func TestAccountRefund(t *testing.T) {
|
||||
for _, increment := range increments {
|
||||
ub.refundIncrement(increment, OUTBOUND, utils.VOICE, false)
|
||||
}
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 104 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 13 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][1].Value != 14 {
|
||||
t.Error("Error refounding money: ", ub.BalanceMap[utils.VOICE+OUTBOUND][1].Value)
|
||||
if ub.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 104 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 13 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][1].GetValue() != 14 {
|
||||
t.Error("Error refounding money: ", ub.BalanceMap[utils.VOICE+OUTBOUND][1].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1050,11 +1050,11 @@ func TestDebitShared(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if rif.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 0 {
|
||||
if rif.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 0 {
|
||||
t.Errorf("Error debiting from shared group: %+v", rif.BalanceMap[utils.MONETARY+OUTBOUND][0])
|
||||
}
|
||||
groupie, _ = accountingStorage.GetAccount("groupie")
|
||||
if groupie.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 10 {
|
||||
if groupie.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 10 {
|
||||
t.Errorf("Error debiting from shared group: %+v", groupie.BalanceMap[utils.MONETARY+OUTBOUND][0])
|
||||
}
|
||||
|
||||
@@ -1162,10 +1162,10 @@ func TestDebitSMS(t *testing.T) {
|
||||
if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testm" {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.SMS+OUTBOUND][0].Value != 99 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 21 {
|
||||
if rifsBalance.BalanceMap[utils.SMS+OUTBOUND][0].GetValue() != 99 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 21 {
|
||||
t.Log(cc.Timespans[0].Increments)
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.SMS+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.SMS+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1205,10 +1205,10 @@ func TestDebitGeneric(t *testing.T) {
|
||||
if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testm" {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.GENERIC+OUTBOUND][0].Value != 99 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 21 {
|
||||
if rifsBalance.BalanceMap[utils.GENERIC+OUTBOUND][0].GetValue() != 99 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 21 {
|
||||
t.Log(cc.Timespans[0].Increments)
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.GENERIC+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.GENERIC+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1260,10 +1260,10 @@ func TestDebitDataUnits(t *testing.T) {
|
||||
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 {
|
||||
if rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].GetValue() != 20 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 21 {
|
||||
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)
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1306,9 +1306,9 @@ func TestDebitDataMoney(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].Value != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 0 {
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].Value, rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].Value)
|
||||
if rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].GetValue() != 0 ||
|
||||
rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 0 {
|
||||
t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[utils.DATA+OUTBOUND][0].GetValue(), rifsBalance.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ func parseTemplateValue(rsrFlds utils.RSRFields, acnt *Account, action *Action)
|
||||
case "balance_id":
|
||||
parsedValue += rsrFld.ParseValue(action.Balance.Id)
|
||||
case "balance_value":
|
||||
parsedValue += rsrFld.ParseValue(strconv.FormatFloat(action.Balance.Value, 'f', -1, 64))
|
||||
parsedValue += rsrFld.ParseValue(strconv.FormatFloat(action.Balance.GetValue(), 'f', -1, 64))
|
||||
case "destination_id":
|
||||
parsedValue += rsrFld.ParseValue(action.Balance.DestinationIds)
|
||||
case "extra_params":
|
||||
@@ -375,8 +375,8 @@ func resetCountersAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Ac
|
||||
}
|
||||
|
||||
func genericMakeNegative(a *Action) {
|
||||
if a.Balance != nil && a.Balance.Value >= 0 { // only apply if not allready negative
|
||||
a.Balance.Value = -a.Balance.Value
|
||||
if a.Balance != nil && a.Balance.GetValue() >= 0 { // only apply if not allready negative
|
||||
a.Balance.SetValue(-a.Balance.GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -666,7 +666,7 @@ func TestActionResetTriggresExecutesThem(t *testing.T) {
|
||||
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: utils.MONETARY, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
|
||||
}
|
||||
resetTriggersAction(ub, nil, nil, nil)
|
||||
if ub.ActionTriggers[0].Executed == true || ub.BalanceMap[utils.MONETARY][0].Value == 12 {
|
||||
if ub.ActionTriggers[0].Executed == true || ub.BalanceMap[utils.MONETARY][0].GetValue() == 12 {
|
||||
t.Error("Reset triggers action failed!")
|
||||
}
|
||||
}
|
||||
@@ -723,7 +723,7 @@ func TestActionResetPrepaid(t *testing.T) {
|
||||
if !ub.AllowNegative ||
|
||||
ub.BalanceMap[utils.MONETARY].GetTotalValue() != 0 ||
|
||||
len(ub.UnitCounters) != 0 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 0 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 0 ||
|
||||
ub.ActionTriggers[0].Executed == true || ub.ActionTriggers[1].Executed == true {
|
||||
t.Log(ub.BalanceMap)
|
||||
t.Error("Reset prepaid action failed!")
|
||||
@@ -740,7 +740,7 @@ func TestActionResetPostpaid(t *testing.T) {
|
||||
resetAccountAction(ub, nil, nil, nil)
|
||||
if ub.BalanceMap[utils.MONETARY].GetTotalValue() != 0 ||
|
||||
len(ub.UnitCounters) != 0 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 0 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 0 ||
|
||||
ub.ActionTriggers[0].Executed == true || ub.ActionTriggers[1].Executed == true {
|
||||
t.Error("Reset postpaid action failed!")
|
||||
}
|
||||
@@ -888,7 +888,7 @@ func TestActionDebitMinutes(t *testing.T) {
|
||||
a := &Action{BalanceType: utils.VOICE, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationIds: "NAT"}}
|
||||
debitAction(ub, nil, a, nil)
|
||||
if ub.AllowNegative ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 5 ||
|
||||
ub.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 5 ||
|
||||
ub.BalanceMap[utils.MONETARY].GetTotalValue() != 100 ||
|
||||
len(ub.UnitCounters) != 1 ||
|
||||
len(ub.BalanceMap[utils.VOICE+OUTBOUND]) != 2 ||
|
||||
@@ -922,7 +922,7 @@ func TestActionResetAllCounters(t *testing.T) {
|
||||
t.FailNow()
|
||||
}
|
||||
mb := ub.UnitCounters[0].Balances[0]
|
||||
if mb.Weight != 20 || mb.Value != 0 || mb.DestinationIds != "NAT" {
|
||||
if mb.Weight != 20 || mb.GetValue() != 0 || mb.DestinationIds != "NAT" {
|
||||
t.Errorf("Balance cloned incorrectly: %v!", mb)
|
||||
}
|
||||
}
|
||||
@@ -954,7 +954,7 @@ func TestActionResetCounterMinutes(t *testing.T) {
|
||||
t.FailNow()
|
||||
}
|
||||
mb := ub.UnitCounters[1].Balances[0]
|
||||
if mb.Weight != 20 || mb.Value != 0 || mb.DestinationIds != "NAT" {
|
||||
if mb.Weight != 20 || mb.GetValue() != 0 || mb.DestinationIds != "NAT" {
|
||||
t.Errorf("Balance cloned incorrectly: %+v!", mb)
|
||||
}
|
||||
}
|
||||
@@ -1055,11 +1055,11 @@ func TestActionPlanLogging(t *testing.T) {
|
||||
func TestActionMakeNegative(t *testing.T) {
|
||||
a := &Action{Balance: &Balance{Value: 10}}
|
||||
genericMakeNegative(a)
|
||||
if a.Balance.Value > 0 {
|
||||
if a.Balance.GetValue() > 0 {
|
||||
t.Error("Failed to make negative: ", a)
|
||||
}
|
||||
genericMakeNegative(a)
|
||||
if a.Balance.Value > 0 {
|
||||
if a.Balance.GetValue() > 0 {
|
||||
t.Error("Failed to preserve negative: ", a)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ func (b *Balance) getMatchingPrefixAndDestId(dest string) (prefix, destId string
|
||||
// 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()
|
||||
availableDuration := time.Duration(b.Value) * time.Second
|
||||
availableDuration := time.Duration(b.GetValue()) * time.Second
|
||||
duration = availableDuration
|
||||
credit = initialCredit
|
||||
cc, err := b.GetCost(cd, false)
|
||||
@@ -280,32 +280,52 @@ func (b *Balance) GetCost(cd *CallDescriptor, getStandardIfEmpty bool) (*CallCos
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Balance) SubstractAmount(amount float64) {
|
||||
func (b *Balance) GetValue() float64 {
|
||||
return b.Value
|
||||
}
|
||||
|
||||
func (b *Balance) AddValue(amount float64) {
|
||||
b.SetValue(b.Value + amount)
|
||||
}
|
||||
|
||||
func (b *Balance) SubstractValue(amount float64) {
|
||||
b.SetValue(b.Value - amount)
|
||||
}
|
||||
|
||||
func (b *Balance) SetValue(amount float64) {
|
||||
b.Value = amount
|
||||
b.Value = utils.Round(b.GetValue(), globalRoundingDecimals, utils.ROUNDING_MIDDLE)
|
||||
b.dirty = true
|
||||
|
||||
// publish event
|
||||
accountId := ""
|
||||
allowNegative := ""
|
||||
disabled := ""
|
||||
if b.account != nil {
|
||||
accountId = b.account.Id
|
||||
allowNegative = strconv.FormatBool(b.account.AllowNegative)
|
||||
disabled = strconv.FormatBool(b.account.Disabled)
|
||||
}
|
||||
Publish(CgrEvent{
|
||||
"EventName": utils.EVT_ACCOUNT_BALANCE_MODIFIED,
|
||||
"Uuid": b.Uuid,
|
||||
"Id": b.Id,
|
||||
"Value": strconv.FormatFloat(b.Value, 'f', -1, 64),
|
||||
"ExpirationDate": b.ExpirationDate.String(),
|
||||
"Weight": strconv.FormatFloat(b.Weight, 'f', -1, 64),
|
||||
"DestinationIds": b.DestinationIds,
|
||||
"RatingSubject": b.RatingSubject,
|
||||
"Category": b.Category,
|
||||
"SharedGroup": b.SharedGroup,
|
||||
"TimingIDs": b.TimingIDs,
|
||||
"Account": accountId,
|
||||
"EventName": utils.EVT_ACCOUNT_BALANCE_MODIFIED,
|
||||
"Uuid": b.Uuid,
|
||||
"Id": b.Id,
|
||||
"Value": strconv.FormatFloat(b.Value, 'f', -1, 64),
|
||||
"ExpirationDate": b.ExpirationDate.String(),
|
||||
"Weight": strconv.FormatFloat(b.Weight, 'f', -1, 64),
|
||||
"DestinationIds": b.DestinationIds,
|
||||
"RatingSubject": b.RatingSubject,
|
||||
"Category": b.Category,
|
||||
"SharedGroup": b.SharedGroup,
|
||||
"TimingIDs": b.TimingIDs,
|
||||
"Account": accountId,
|
||||
"AccountAllowNegative": allowNegative,
|
||||
"AccountDisabled": disabled,
|
||||
})
|
||||
b.Value -= amount
|
||||
b.Value = utils.Round(b.Value, globalRoundingDecimals, utils.ROUNDING_MIDDLE)
|
||||
b.dirty = true
|
||||
}
|
||||
|
||||
func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances BalanceChain, count bool, dryRun bool) (cc *CallCost, err error) {
|
||||
if !b.IsActiveAt(cd.TimeStart) || b.Value <= 0 {
|
||||
if !b.IsActiveAt(cd.TimeStart) || b.GetValue() <= 0 {
|
||||
return
|
||||
}
|
||||
if duration, err := utils.ParseZeroRatingSubject(b.RatingSubject); err == nil {
|
||||
@@ -352,8 +372,8 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala
|
||||
if seconds == 1 {
|
||||
amount = inc.Duration.Seconds()
|
||||
}
|
||||
if b.Value >= amount {
|
||||
b.SubstractAmount(amount)
|
||||
if b.GetValue() >= amount {
|
||||
b.SubstractValue(amount)
|
||||
inc.BalanceInfo.UnitBalanceUuid = b.Uuid
|
||||
inc.BalanceInfo.AccountId = ub.Id
|
||||
inc.UnitInfo = &UnitInfo{cc.Destination, amount, cc.TOR}
|
||||
@@ -429,19 +449,19 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala
|
||||
}
|
||||
var moneyBal *Balance
|
||||
for _, mb := range moneyBalances {
|
||||
if mb.Value >= cost {
|
||||
if mb.GetValue() >= cost {
|
||||
moneyBal = mb
|
||||
break
|
||||
}
|
||||
}
|
||||
if (cost == 0 || moneyBal != nil) && b.Value >= seconds {
|
||||
b.SubstractAmount(seconds)
|
||||
if (cost == 0 || moneyBal != nil) && b.GetValue() >= seconds {
|
||||
b.SubstractValue(seconds)
|
||||
inc.BalanceInfo.UnitBalanceUuid = b.Uuid
|
||||
inc.BalanceInfo.AccountId = ub.Id
|
||||
inc.UnitInfo = &UnitInfo{cc.Destination, seconds, cc.TOR}
|
||||
if cost != 0 {
|
||||
inc.BalanceInfo.MoneyBalanceUuid = moneyBal.Uuid
|
||||
moneyBal.SubstractAmount(cost)
|
||||
moneyBal.SubstractValue(cost)
|
||||
cd.MaxCostSoFar += cost
|
||||
}
|
||||
inc.paid = true
|
||||
@@ -473,7 +493,7 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala
|
||||
}
|
||||
|
||||
func (b *Balance) DebitMoney(cd *CallDescriptor, ub *Account, count bool, dryRun bool) (cc *CallCost, err error) {
|
||||
if !b.IsActiveAt(cd.TimeStart) || b.Value <= 0 {
|
||||
if !b.IsActiveAt(cd.TimeStart) || b.GetValue() <= 0 {
|
||||
return
|
||||
}
|
||||
//log.Printf("}}}}}}} %+v", cd.testCallcost)
|
||||
@@ -531,8 +551,8 @@ func (b *Balance) DebitMoney(cd *CallDescriptor, ub *Account, count bool, dryRun
|
||||
continue
|
||||
}
|
||||
|
||||
if b.Value >= amount {
|
||||
b.SubstractAmount(amount)
|
||||
if b.GetValue() >= amount {
|
||||
b.SubstractValue(amount)
|
||||
cd.MaxCostSoFar += amount
|
||||
inc.BalanceInfo.MoneyBalanceUuid = b.Uuid
|
||||
inc.BalanceInfo.AccountId = ub.Id
|
||||
@@ -591,7 +611,7 @@ func (bc BalanceChain) Sort() {
|
||||
func (bc BalanceChain) GetTotalValue() (total float64) {
|
||||
for _, b := range bc {
|
||||
if !b.IsExpired() && b.IsActive() {
|
||||
total += b.Value
|
||||
total += b.GetValue()
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -603,12 +623,12 @@ func (bc BalanceChain) Debit(amount float64) float64 {
|
||||
if b.IsExpired() {
|
||||
continue
|
||||
}
|
||||
if b.Value >= amount || i == len(bc)-1 { // if last one go negative
|
||||
b.SubstractAmount(amount)
|
||||
if b.GetValue() >= amount || i == len(bc)-1 { // if last one go negative
|
||||
b.SubstractValue(amount)
|
||||
break
|
||||
}
|
||||
b.Value = 0
|
||||
amount -= b.Value
|
||||
b.SetValue(0)
|
||||
amount -= b.GetValue()
|
||||
}
|
||||
return bc.GetTotalValue()
|
||||
}
|
||||
|
||||
@@ -523,7 +523,7 @@ func (origCD *CallDescriptor) getMaxSessionDuration(origAcc *Account) (time.Dura
|
||||
defaultBalance := account.GetDefaultMoneyBalance(cd.Direction)
|
||||
|
||||
//use this to check what increment was payed with debt
|
||||
initialDefaultBalanceValue := defaultBalance.Value
|
||||
initialDefaultBalanceValue := defaultBalance.GetValue()
|
||||
|
||||
//Logger.Debug("ACCOUNT: " + utils.ToJSON(account))
|
||||
//Logger.Debug("DEFAULT_BALANCE: " + utils.ToJSON(defaultBalance))
|
||||
|
||||
@@ -699,11 +699,11 @@ func TestMaxDebitWithAccountShared(t *testing.T) {
|
||||
}
|
||||
acc, _ := cd.getAccount()
|
||||
balanceMap := acc.BalanceMap[utils.MONETARY+OUTBOUND]
|
||||
if len(balanceMap) != 1 || balanceMap[0].Value != 0 {
|
||||
if len(balanceMap) != 1 || balanceMap[0].GetValue() != 0 {
|
||||
t.Errorf("Wrong shared balance debited: %+v", balanceMap[0])
|
||||
}
|
||||
other, err := accountingStorage.GetAccount("*out:vdf:empty10")
|
||||
if err != nil || other.BalanceMap[utils.MONETARY+OUTBOUND][0].Value != 7.5 {
|
||||
if err != nil || other.BalanceMap[utils.MONETARY+OUTBOUND][0].GetValue() != 7.5 {
|
||||
t.Errorf("Error debiting shared balance: %+v", other.BalanceMap[utils.MONETARY+OUTBOUND][0])
|
||||
}
|
||||
}
|
||||
@@ -916,8 +916,8 @@ func TestDebitFromShareAndNormal(t *testing.T) {
|
||||
t.Errorf("Debit from share and normal error: %+v, %v", cc, err)
|
||||
}
|
||||
|
||||
if balanceMap[0].Value != 10 || balanceMap[1].Value != 27.5 {
|
||||
t.Errorf("Error debiting from right balance: %v %v", balanceMap[0].Value, balanceMap[1].Value)
|
||||
if balanceMap[0].GetValue() != 10 || balanceMap[1].GetValue() != 27.5 {
|
||||
t.Errorf("Error debiting from right balance: %v %v", balanceMap[0].GetValue(), balanceMap[1].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -944,8 +944,8 @@ func TestDebitFromEmptyShare(t *testing.T) {
|
||||
}
|
||||
acc, _ := cd.getAccount()
|
||||
balanceMap := acc.BalanceMap[utils.MONETARY+OUTBOUND]
|
||||
if len(balanceMap) != 2 || balanceMap[0].Value != 0 || balanceMap[1].Value != -2.5 {
|
||||
t.Errorf("Error debiting from empty share: %+v", balanceMap[1].Value)
|
||||
if len(balanceMap) != 2 || balanceMap[0].GetValue() != 0 || balanceMap[1].GetValue() != -2.5 {
|
||||
t.Errorf("Error debiting from empty share: %+v", balanceMap[1].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -973,16 +973,16 @@ func TestDebitNegatve(t *testing.T) {
|
||||
acc, _ := cd.getAccount()
|
||||
//utils.PrintFull(acc)
|
||||
balanceMap := acc.BalanceMap[utils.MONETARY+OUTBOUND]
|
||||
if len(balanceMap) != 1 || balanceMap[0].Value != -2.5 {
|
||||
t.Errorf("Error debiting from empty share: %+v", balanceMap[0].Value)
|
||||
if len(balanceMap) != 1 || balanceMap[0].GetValue() != -2.5 {
|
||||
t.Errorf("Error debiting from empty share: %+v", balanceMap[0].GetValue())
|
||||
}
|
||||
cc, err = cd.MaxDebit()
|
||||
//utils.PrintFull(cc)
|
||||
if err != nil || cc.Cost != 2.5 {
|
||||
t.Errorf("Debit from empty share error: %+v, %v", cc, err)
|
||||
}
|
||||
if len(balanceMap) != 1 || balanceMap[0].Value != -5 {
|
||||
t.Errorf("Error debiting from empty share: %+v", balanceMap[0].Value)
|
||||
if len(balanceMap) != 1 || balanceMap[0].GetValue() != -5 {
|
||||
t.Errorf("Error debiting from empty share: %+v", balanceMap[0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1059,8 +1059,8 @@ func TestMaxDebitConsumesMinutes(t *testing.T) {
|
||||
LoopIndex: 0,
|
||||
DurationIndex: 0}
|
||||
cd1.MaxDebit()
|
||||
if cd1.account.BalanceMap[utils.VOICE+OUTBOUND][0].Value != 20 {
|
||||
t.Error("Error using minutes: ", cd1.account.BalanceMap[utils.VOICE+OUTBOUND][0].Value)
|
||||
if cd1.account.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != 20 {
|
||||
t.Error("Error using minutes: ", cd1.account.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -99,13 +99,13 @@ func TestGetDerivedMaxSessionTime(t *testing.T) {
|
||||
t.Error(err)
|
||||
//} else if rifStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND].Equal(rifsAccount.BalanceMap[utils.VOICE+OUTBOUND]) {
|
||||
// t.Errorf("Expected: %+v, received: %+v", rifsAccount.BalanceMap[utils.VOICE+OUTBOUND][0], rifStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0])
|
||||
} else if rifStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].Value != rifsAccount.BalanceMap[utils.VOICE+OUTBOUND][0].Value {
|
||||
t.Error("BalanceValue: ", rifStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].Value)
|
||||
} else if rifStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != rifsAccount.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() {
|
||||
t.Error("BalanceValue: ", rifStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue())
|
||||
}
|
||||
if danStoredAcnt, err := accountingStorage.GetAccount(utils.ConcatenatedKey(utils.OUT, testTenant, "dan")); err != nil {
|
||||
t.Error(err)
|
||||
} else if danStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].Value != dansAccount.BalanceMap[utils.VOICE+OUTBOUND][0].Value {
|
||||
t.Error("BalanceValue: ", danStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].Value)
|
||||
} else if danStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() != dansAccount.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue() {
|
||||
t.Error("BalanceValue: ", danStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND][0].GetValue())
|
||||
}
|
||||
var dcs utils.DerivedChargers
|
||||
attrs := &utils.AttrDerivedChargers{Tenant: testTenant, Category: "call", Direction: "*out", Account: "dan", Subject: "dan"}
|
||||
|
||||
@@ -126,7 +126,7 @@ func (lbcs LowestBalanceChainSorter) Swap(i, j int) {
|
||||
}
|
||||
|
||||
func (lbcs LowestBalanceChainSorter) Less(i, j int) bool {
|
||||
return lbcs[i].Value < lbcs[j].Value
|
||||
return lbcs[i].GetValue() < lbcs[j].GetValue()
|
||||
}
|
||||
|
||||
type HighestBalanceChainSorter []*Balance
|
||||
@@ -140,7 +140,7 @@ func (hbcs HighestBalanceChainSorter) Swap(i, j int) {
|
||||
}
|
||||
|
||||
func (hbcs HighestBalanceChainSorter) Less(i, j int) bool {
|
||||
return hbcs[i].Value > hbcs[j].Value
|
||||
return hbcs[i].GetValue() > hbcs[j].GetValue()
|
||||
}
|
||||
|
||||
type RandomBalanceChainSorter []*Balance
|
||||
|
||||
@@ -60,7 +60,7 @@ func TestSharedPopBalanceByStrategyLow(t *testing.T) {
|
||||
if len(sbc) != 3 ||
|
||||
sbc[0].Value != 1.0 ||
|
||||
sbc[1].Value != 2.0 {
|
||||
t.Error("Error sorting balance chain: ", sbc[0].Value)
|
||||
t.Error("Error sorting balance chain: ", sbc[0].GetValue())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ func (uc *UnitsCounter) initBalances(ats []*ActionTrigger) {
|
||||
for _, a := range acs {
|
||||
if a.Balance != nil {
|
||||
b := a.Balance.Clone()
|
||||
b.Value = 0
|
||||
b.SetValue(0)
|
||||
if !uc.Balances.HasBalance(b) {
|
||||
uc.Balances = append(uc.Balances, b)
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (uc *UnitsCounter) addUnits(amount float64, prefix string) {
|
||||
if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil {
|
||||
destIds := x.(map[interface{}]struct{})
|
||||
if _, found := destIds[mb.DestinationIds]; found {
|
||||
mb.Value += amount
|
||||
mb.AddValue(amount)
|
||||
counted = true
|
||||
break
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func (uc *UnitsCounter) addUnits(amount float64, prefix string) {
|
||||
if !counted {
|
||||
// use general balance
|
||||
b := uc.GetGeneralBalance()
|
||||
b.Value += amount
|
||||
b.AddValue(amount)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestUnitsCounterAddBalanceExists(t *testing.T) {
|
||||
Balances: BalanceChain{&Balance{Value: 1}, &Balance{Value: 10, Weight: 20, DestinationIds: "NAT"}, &Balance{Weight: 10, DestinationIds: "RET"}},
|
||||
}
|
||||
uc.addUnits(5, "0723")
|
||||
if len(uc.Balances) != 3 || uc.Balances[1].Value != 15 {
|
||||
if len(uc.Balances) != 3 || uc.Balances[1].GetValue() != 15 {
|
||||
t.Error("Error adding minute bucket!")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user