From b557e2a5a4d066d8062ce5c77d2574da4ea0b4ed Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 28 Feb 2014 23:24:55 +0200 Subject: [PATCH] fixed connect fee issues --- engine/account.go | 14 +++++++------- engine/callcost.go | 6 +++++- engine/calldesc.go | 9 +++++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/engine/account.go b/engine/account.go index 961cd471c..53eac07fc 100644 --- a/engine/account.go +++ b/engine/account.go @@ -236,27 +236,27 @@ func (ub *Account) debitCreditBalance(cc *CallCost, count bool) (err error) { } CONNECT_FEE: if cc.deductConnectFee { - amount := cc.GetConnectFee() + connectFee := cc.GetConnectFee() connectFeePaid := false for _, b := range usefulMoneyBalances { - if b.Value >= amount { - b.Value -= amount + if b.Value >= connectFee { + b.Value -= connectFee b.Value = utils.Round(b.Value, roundingDecimals, utils.ROUNDING_MIDDLE) // the conect fee is not refundable! if count { - ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}}) + ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: connectFee, DestinationId: cc.Destination}}) } connectFeePaid = true break } } // debit connect fee - if cc.GetConnectFee() > 0 && !connectFeePaid { + if connectFee > 0 && !connectFeePaid { // there are no money for the connect fee; go negative - defaultMoneyBalance.Value -= amount + defaultMoneyBalance.Value -= connectFee // the conect fee is not refundable! if count { - ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}}) + ub.countUnits(&Action{BalanceType: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: connectFee, DestinationId: cc.Destination}}) } } } diff --git a/engine/callcost.go b/engine/callcost.go index f5b4f0ede..41cdd5ba6 100644 --- a/engine/callcost.go +++ b/engine/callcost.go @@ -33,7 +33,11 @@ type CallCost struct { // Pretty printing for call cost func (cc *CallCost) String() (r string) { - r = fmt.Sprintf("%v[%v] : %s(%s) -> %s (", cc.Cost, cc.GetConnectFee(), cc.Subject, cc.Account, cc.Destination) + connectFee := 0.0 + if cc.deductConnectFee { + connectFee = cc.GetConnectFee() + } + r = fmt.Sprintf("%v[%v] : %s(%s) - > %s (", cc.Cost, connectFee, cc.Subject, cc.Account, cc.Destination) for _, ts := range cc.Timespans { r += fmt.Sprintf(" %v,", ts.GetDuration()) } diff --git a/engine/calldesc.go b/engine/calldesc.go index fbb70a735..cf56d7aec 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -473,7 +473,9 @@ func (origCD *CallDescriptor) GetMaxSessionDuration() (time.Duration, error) { cd.TimeStart = cd.TimeStart.Add(availableDuration) // substract the connect fee cc, err := cd.GetCost() - availableCredit -= cc.GetConnectFee() + if cc.deductConnectFee { + availableCredit -= cc.GetConnectFee() + } if err != nil { Logger.Err(fmt.Sprintf("Could not get cost for %s: %s.", cd.GetKey(cd.Subject), err.Error())) return 0, err @@ -516,11 +518,14 @@ func (cd *CallDescriptor) Debit() (cc *CallCost, err error) { //Logger.Debug(fmt.Sprintf("Account: %s", ub)) //cCost, _ := json.Marshal(cc) //Logger.Debug(fmt.Sprintf("CallCost: %s", cCost)) - if cc.Cost != 0 || cc.GetConnectFee() != 0 { + if cc.Cost != 0 || (cc.deductConnectFee && cc.GetConnectFee() != 0) { userBalance.debitCreditBalance(cc, true) } cost := 0.0 // re-calculate call cost after balances + if cc.deductConnectFee { // add back the connectFee + cost += cc.GetConnectFee() + } for _, ts := range cc.Timespans { cost += ts.getCost() cost = utils.Round(cost, roundingDecimals, utils.ROUNDING_MIDDLE) // just get rid of the extra decimals