fixed connect fee issues

This commit is contained in:
Radu Ioan Fericean
2014-02-28 23:24:55 +02:00
parent 9a06d5e537
commit b557e2a5a4
3 changed files with 19 additions and 10 deletions

View File

@@ -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}})
}
}
}

View File

@@ -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())
}

View File

@@ -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