Send balance update event from rals to threshold ( in case of negative ) only once

This commit is contained in:
TeoV
2020-10-20 13:27:48 +03:00
committed by Dan Christian Bogos
parent 51f950ac85
commit a2144f6867

View File

@@ -381,7 +381,7 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
// debit minutes
unitBalanceChecker := true
for unitBalanceChecker {
// try every balance multiple times in case one becomes active or ratig changes
// try every balance multiple times in case one becomes active or rating changes
unitBalanceChecker = false
for _, balance := range usefulUnitBalances {
partCC, debitErr := balance.debitUnits(cd, balance.account,
@@ -397,7 +397,7 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
cc.Timespans = append(cc.Timespans, partCC.Timespans...)
cc.negativeConnectFee = partCC.negativeConnectFee
cd.TimeStart = cc.GetEndTime()
// check if the calldescriptor is covered
// check if the call descriptor is covered
if cd.GetDuration() <= 0 {
goto COMMIT
}
@@ -411,7 +411,6 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
}
// check for blocker
if dryRun && balance.Blocker {
//log.Print("BLOCKER!")
return // don't go to next balances
}
}
@@ -419,28 +418,20 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
// debit money
moneyBalanceChecker := true
for moneyBalanceChecker {
// try every balance multiple times in case one becomes active or ratig changes
// try every balance multiple times in case one becomes active or rating changes
moneyBalanceChecker = false
for _, balance := range usefulMoneyBalances {
//utils.Logger.Info(fmt.Sprintf("Money balance: %+v", balance))
//utils.Logger.Info(fmt.Sprintf("CD BEFORE MONEY: %+v", cd))
partCC, debitErr := balance.debitMoney(cd, balance.account,
usefulMoneyBalances, count, dryRun, len(cc.Timespans) == 0)
if debitErr != nil {
return nil, debitErr
}
//utils.Logger.Info(fmt.Sprintf("CD AFTER MONEY: %+v", cd))
if partCC != nil {
cc.Timespans = append(cc.Timespans, partCC.Timespans...)
cc.negativeConnectFee = partCC.negativeConnectFee
/*for i, ts := range cc.Timespans {
log.Printf("cc.times[an[%d]: %+v\n", i, ts)
}*/
cd.TimeStart = cc.GetEndTime()
//log.Printf("CD: %+v", cd)
//log.Printf("CD: %+v - %+v", cd.TimeStart, cd.TimeEnd)
// check if the calldescriptor is covered
// check if the call descriptor is covered
if cd.GetDuration() <= 0 {
goto COMMIT
}
@@ -453,13 +444,10 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
}
// check for blocker
if dryRun && balance.Blocker {
//log.Print("BLOCKER!")
return // don't go to next balances
}
}
}
//log.Printf("END CD: %+v", cd)
//log.Print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
}
//log.Printf("After balances CD: %+v", cd)
if hadBalanceSubj {
@@ -493,8 +481,7 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
// this is the first add, debit the connect fee
ok, debitedConnectFeeBalance = acc.DebitConnectionFee(cc, usefulMoneyBalances, count, true)
}
//log.Printf("Left CC: %+v ", leftCC)
// get the default money balanance
// get the default money balance
// and go negative on it with the amount still unpaid
if len(leftCC.Timespans) > 0 && leftCC.Cost > 0 && !acc.AllowNegative && !dryRun {
utils.Logger.Warning(fmt.Sprintf("<Rater> Going negative on account %s with AllowNegative: false", cd.GetAccountKey()))
@@ -525,37 +512,14 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
}
for incIndex, increment := range ts.Increments {
// connect fee was processed and skip it
if tsIndex == 0 && incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && cc.deductConnectFee && ok {
// go to nextincrement
continue
}
cost := increment.Cost
defaultBalance := acc.GetDefaultMoneyBalance()
defaultBalance.SubstractValue(cost)
//send default balance to thresholdS to be processed
if len(config.CgrConfig().RalsCfg().ThresholdSConns) != 0 {
acntTnt := utils.NewTenantID(acc.ID)
thEv := &ArgsProcessEvent{
CGREvent: &utils.CGREvent{
Tenant: acntTnt.Tenant,
ID: utils.GenUUID(),
Event: map[string]interface{}{
utils.EventType: utils.BalanceUpdate,
utils.EventSource: utils.AccountService,
utils.Account: acntTnt.ID,
utils.BalanceID: defaultBalance.ID,
utils.Units: defaultBalance.Value}}}
var tIDs []string
if err := connMgr.Call(config.CgrConfig().RalsCfg().ThresholdSConns, nil,
utils.ThresholdSv1ProcessEvent, thEv, &tIDs); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
utils.Logger.Warning(
fmt.Sprintf("<AccountS> error: <%s> processing balance event <%+v> with ThresholdS.",
err.Error(), utils.ToJSON(thEv)))
}
}
increment.BalanceInfo.Monetary = &MonetaryInfo{
UUID: defaultBalance.Uuid,
@@ -576,6 +540,30 @@ func (acc *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun bo
}
}
}
// in case of going to negative we send the default balance to thresholdS to be processed
if len(config.CgrConfig().RalsCfg().ThresholdSConns) != 0 {
defaultBalance := acc.GetDefaultMoneyBalance()
acntTnt := utils.NewTenantID(acc.ID)
thEv := &ArgsProcessEvent{
CGREvent: &utils.CGREvent{
Tenant: acntTnt.Tenant,
ID: utils.GenUUID(),
Event: map[string]interface{}{
utils.EventType: utils.BalanceUpdate,
utils.EventSource: utils.AccountService,
utils.Account: acntTnt.ID,
utils.BalanceID: defaultBalance.ID,
utils.Units: defaultBalance.Value}}}
var tIDs []string
if err := connMgr.Call(config.CgrConfig().RalsCfg().ThresholdSConns, nil,
utils.ThresholdSv1ProcessEvent, thEv, &tIDs); err != nil &&
err.Error() != utils.ErrNotFound.Error() {
utils.Logger.Warning(
fmt.Sprintf("<AccountS> error: <%s> processing balance event <%+v> with ThresholdS.",
err.Error(), utils.ToJSON(thEv)))
}
}
}
COMMIT: