From c056d1b5bf17fa01f8ca6d6145f67ca50a3cc7f2 Mon Sep 17 00:00:00 2001 From: Regis Date: Wed, 30 Nov 2016 15:52:54 +0100 Subject: [PATCH] adding connect fee only in first timestamp --- engine/account.go | 6 +++--- engine/balances.go | 9 +++++---- sessionmanager/smg_session.go | 5 +++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/engine/account.go b/engine/account.go index db6df337c..b8cbf9a6b 100644 --- a/engine/account.go +++ b/engine/account.go @@ -527,12 +527,12 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo utils.Logger.Err(fmt.Sprintf(" Going negative on account %s with AllowNegative: false", cd.GetAccountKey())) } leftCC.Timespans.Decompress() - for _, ts := range leftCC.Timespans { + for tsIndex, ts := range leftCC.Timespans { if ts.Increments == nil { ts.createIncrementsSlice() } - if ts.RateInterval.Rating.ConnectFee > 0 && ok { + if tsIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && ok { inc := &Increment{ Duration: 0, @@ -553,7 +553,7 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo for incIndex, increment := range ts.Increments { - if incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && ok { + if tsIndex == 0 && incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && ok { // go to nextincrement continue } diff --git a/engine/balances.go b/engine/balances.go index b3d046aab..3e677e690 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -411,7 +411,7 @@ func (b *Balance) debitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala return nil, errors.New("timespan with no rate interval assigned") } - if ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee { + if tsIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee && ok { inc := &Increment{ Duration: 0, @@ -433,7 +433,7 @@ func (b *Balance) debitUnits(cd *CallDescriptor, ub *Account, moneyBalances Bala maxCost, strategy := ts.RateInterval.GetMaxCost() for incIndex, inc := range ts.Increments { - if incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee { + if tsIndex == 0 && incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee && ok { // go to nextincrement continue } @@ -546,6 +546,7 @@ func (b *Balance) debitMoney(cd *CallDescriptor, ub *Account, moneyBalances Bala 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); !ok { // balance is blocker @@ -568,7 +569,7 @@ func (b *Balance) debitMoney(cd *CallDescriptor, ub *Account, moneyBalances Bala return nil, errors.New("timespan with no rate interval assigned") } - if ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee { + if tsIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && debitConnectFee && ok { inc := &Increment{ Duration: 0, @@ -594,7 +595,7 @@ func (b *Balance) debitMoney(cd *CallDescriptor, ub *Account, moneyBalances Bala // check standard subject tags //log.Printf("INC: %+v", inc) - if incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && ok { + if tsIndex == 0 && incIndex == 0 && ts.RateInterval.Rating.ConnectFee > 0 && ok { // go to nextincrement continue } diff --git a/sessionmanager/smg_session.go b/sessionmanager/smg_session.go index 0b1619f2c..5eb69a971 100644 --- a/sessionmanager/smg_session.go +++ b/sessionmanager/smg_session.go @@ -141,6 +141,7 @@ func (self *SMGSession) debit(dur time.Duration, lastUsed *time.Duration) (time. // Attempts to refund a duration, error on failure func (self *SMGSession) refund(refundDuration time.Duration) error { + if refundDuration == 0 { // Nothing to refund return nil } @@ -154,12 +155,16 @@ func (self *SMGSession) refund(refundDuration time.Duration) error { if refundDuration <= tsDuration { lastRefundedIncrementIndex := -1 + for j := len(ts.Increments) - 1; j >= 0; j-- { increment := ts.Increments[j] + if increment.Duration <= refundDuration { + refundIncrements = append(refundIncrements, increment) refundDuration -= increment.Duration lastRefundedIncrementIndex = j + } else { break //increment duration is larger, cannot refund increment }