From 41cb3ab7d1bf589eb4ebb00209e733d00568fb97 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 9 Feb 2015 13:07:31 +0200 Subject: [PATCH] work in progress --- engine/account.go | 8 +- engine/account_test.go | 232 +++++++++++++++++++++++++++++++++-------- engine/balances.go | 10 +- engine/callcost.go | 16 --- 4 files changed, 197 insertions(+), 69 deletions(-) diff --git a/engine/account.go b/engine/account.go index a5f34031f..e0f0bde38 100644 --- a/engine/account.go +++ b/engine/account.go @@ -227,10 +227,10 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo unitBalanceChecker = false //log.Printf("InitialCD: %+v", cd) for _, balance := range usefulUnitBalances { - // log.Printf("Unit balance: %+v", balance) + //log.Printf("Unit balance: %+v", balance) // log.Printf("CD BEFORE UNIT: %+v", cd) partCC, _ := balance.DebitUnits(cd, count, balance.account, usefulMoneyBalances) - // log.Printf("CD AFTER UNIT: %+v", cd) + //log.Printf("CD AFTER UNIT: %+v", cd) if partCC != nil { //log.Printf("partCC: %+v", partCC.Timespans[0]) initialLength = len(cc.Timespans) @@ -262,9 +262,9 @@ func (ub *Account) debitCreditBalance(cd *CallDescriptor, count bool, dryRun boo moneyBalanceChecker = false for _, balance := range usefulMoneyBalances { //log.Printf("Money balance: %+v", balance) - // log.Printf("CD BEFORE MONEY: %+v", cd) + //log.Printf("CD BEFORE MONEY: %+v", cd.test_callcost) partCC, _ := balance.DebitMoney(cd, count, balance.account) - // log.Printf("CD AFTER MONEY: %+v", cd) + //log.Printf("CD AFTER MONEY: %+v", cd.test_callcost) //log.Printf("partCC: %+v", partCC) //log.Printf("CD: %+v", cd) if partCC != nil { diff --git a/engine/account_test.go b/engine/account_test.go index 5ca9399c4..3c3ecf30e 100644 --- a/engine/account_test.go +++ b/engine/account_test.go @@ -21,6 +21,8 @@ package engine import ( "testing" "time" + + "github.com/cgrates/cgrates/cache2go" ) var ( @@ -341,7 +343,7 @@ func TestDebitCreditNoCredit(t *testing.T) { } } -/*func TestDebitCreditHasCredit(t *testing.T) { +func TestDebitCreditHasCredit(t *testing.T) { b1 := &Balance{Uuid: "testb", Value: 70, Weight: 10, DestinationId: "NAT", RatingSubject: "*zero1m"} cc := &CallCost{ Direction: OUTBOUND, @@ -351,7 +353,7 @@ func TestDebitCreditNoCredit(t *testing.T) { TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), DurationIndex: 0, - RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, + RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 1, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, }, &TimeSpan{ TimeStart: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), @@ -373,7 +375,7 @@ func TestDebitCreditNoCredit(t *testing.T) { } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, - CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 50}}, + CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 110}}, }} var err error cc, err = rifsBalance.debitCreditBalance(cd, false, false) @@ -389,12 +391,12 @@ func TestDebitCreditNoCredit(t *testing.T) { t.Errorf("Error extracting minutes from balance: %+v, %+v", rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) } - if len(cc.Timespans) != 1 || cc.Timespans[0].GetDuration() != time.Minute { + if len(cc.Timespans) != 3 || cc.Timespans[0].GetDuration() != time.Minute { t.Error("Error truncating extra timespans: ", cc.Timespans) } -}*/ +} -/*func TestDebitCreditSplitMinutesMoney(t *testing.T) { +func TestDebitCreditSplitMinutesMoney(t *testing.T) { b1 := &Balance{Uuid: "testb", Value: 10, Weight: 10, DestinationId: "NAT", RatingSubject: "*zero1s"} cc := &CallCost{ Direction: OUTBOUND, @@ -410,6 +412,15 @@ func TestDebitCreditNoCredit(t *testing.T) { }, TOR: MINUTES, } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 50}}, @@ -420,16 +431,16 @@ func TestDebitCreditNoCredit(t *testing.T) { t.Error("Error debiting balance: ", err) } if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testb" || - cc.Timespans[0].Increments[0].Duration != 10*time.Second { - t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) + 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[MINUTES+OUTBOUND][0].Value != 0 || - rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 40 { + rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 30 { t.Errorf("Error extracting minutes from balance: %+v, %+v", rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) } - if len(cc.Timespans) != 2 || cc.Timespans[0].GetDuration() != 10*time.Second || cc.Timespans[1].GetDuration() != 10*time.Second { - t.Error("Error truncating extra timespans: ", cc.Timespans) + 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()) } } @@ -454,6 +465,15 @@ func TestDebitCreditMoreTimespans(t *testing.T) { }, TOR: MINUTES, } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[1].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, }} @@ -494,6 +514,15 @@ func TestDebitCreditMoreTimespansMixed(t *testing.T) { }, TOR: MINUTES, } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[1].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1, b2}, }} @@ -532,7 +561,17 @@ func TestDebitCreditNoConectFeeCredit(t *testing.T) { RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 1, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, }, }, - TOR: MINUTES, + TOR: MINUTES, + deductConnectFee: true, + } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[1].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, @@ -543,8 +582,8 @@ func TestDebitCreditNoConectFeeCredit(t *testing.T) { t.Error("Error showing debiting balance error: ", err) } - if len(cc.Timespans) != 2 || rifsBalance.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != -20 { - t.Error("Error cutting at no connect fee: ", rifsBalance.BalanceMap[CREDIT+OUTBOUND].GetTotalValue()) + if len(cc.Timespans) != 1 || rifsBalance.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 0 { + t.Error("Error cutting at no connect fee: ", rifsBalance.BalanceMap[CREDIT+OUTBOUND]) } } @@ -569,6 +608,15 @@ func TestDebitCreditMoneyOnly(t *testing.T) { }, TOR: MINUTES, } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[1].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "money", Value: 50}}, }} @@ -582,20 +630,19 @@ 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[CREDIT+OUTBOUND][0].Value != -30 { + if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 0 { t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[CREDIT+OUTBOUND][0]) } - if len(cc.Timespans) != 3 || + if len(cc.Timespans) != 2 || cc.Timespans[0].GetDuration() != 10*time.Second || - cc.Timespans[1].GetDuration() != 40*time.Second || - cc.Timespans[2].GetDuration() != 30*time.Second { - t.Error("Error truncating extra timespans: ", cc.Timespans[2].GetDuration()) + cc.Timespans[1].GetDuration() != 40*time.Second { + t.Error("Error truncating extra timespans: ", cc.Timespans) } } func TestDebitCreditSubjectMinutes(t *testing.T) { - b1 := &Balance{Uuid: "testb", Value: 250, Weight: 10, DestinationId: "NAT", RatingSubject: "minu"} + b1 := &Balance{Uuid: "testb", Category: "0", Value: 250, Weight: 10, DestinationId: "NAT", RatingSubject: "minu"} cc := &CallCost{ Tenant: "vdf", Category: "0", @@ -606,12 +653,23 @@ func TestDebitCreditSubjectMinutes(t *testing.T) { TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), TimeEnd: time.Date(2013, 9, 24, 10, 49, 10, 0, time.UTC), DurationIndex: 0, - RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, + RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 1, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, }, }, TOR: MINUTES, deductConnectFee: true, } + cd := &CallDescriptor{ + Tenant: cc.Tenant, + Category: "0", + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 350}}, @@ -623,11 +681,11 @@ func TestDebitCreditSubjectMinutes(t *testing.T) { } if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testb" || cc.Timespans[0].Increments[0].BalanceInfo.MoneyBalanceUuid != "moneya" || - cc.Timespans[0].Increments[0].Duration != time.Second { - t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) + 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[MINUTES+OUTBOUND][0].Value != 180 || - rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 279 { + rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 280 { t.Errorf("Error extracting minutes from balance: %+v, %+v", rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) } @@ -650,25 +708,36 @@ func TestDebitCreditSubjectMoney(t *testing.T) { TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), TimeEnd: time.Date(2013, 9, 24, 10, 49, 10, 0, time.UTC), DurationIndex: 0, - RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, + RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 1, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, }, }, TOR: MINUTES, deductConnectFee: true, } + cd := &CallDescriptor{ + Tenant: cc.Tenant, + Category: cc.Category, + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 75, DestinationId: "NAT", RatingSubject: "minu"}}, }} var err error - err = rifsBalance.debitCreditBalance(cd, false, false) + cc, err = rifsBalance.debitCreditBalance(cd, false, false) if err != nil { t.Error("Error debiting balance: ", err) } if cc.Timespans[0].Increments[0].BalanceInfo.MoneyBalanceUuid != "moneya" || - cc.Timespans[0].Increments[0].Duration != time.Second { + 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[CREDIT+OUTBOUND][0].Value != 4 { + if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 5 { t.Errorf("Error extracting minutes from balance: %+v", rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) } @@ -677,7 +746,7 @@ func TestDebitCreditSubjectMoney(t *testing.T) { } } -func TestDebitCreditSubjectMixed(t *testing.T) { +/*func TestDebitCreditSubjectMixed(t *testing.T) { b1 := &Balance{Uuid: "testb", Value: 40, Weight: 10, DestinationId: "NAT", RatingSubject: "minu"} cc := &CallCost{ Tenant: "vdf", @@ -689,28 +758,39 @@ func TestDebitCreditSubjectMixed(t *testing.T) { TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), TimeEnd: time.Date(2013, 9, 24, 10, 48, 55, 0, time.UTC), DurationIndex: 55 * time.Second, - RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, + RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 1, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}, }, }, TOR: MINUTES, deductConnectFee: true, } + cd := &CallDescriptor{ + Tenant: cc.Tenant, + Category: cc.Category, + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, - CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 150, RatingSubject: "minu"}}, + CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 19500, RatingSubject: "minu"}}, }} var err error - err = rifsBalance.debitCreditBalance(cd, false, false) + cc, err = rifsBalance.debitCreditBalance(cd, false, false) if err != nil { t.Error("Error debiting balance: ", err) } if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testb" || cc.Timespans[0].Increments[0].BalanceInfo.MoneyBalanceUuid != "moneya" || - cc.Timespans[0].Increments[0].Duration != time.Second { + 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[MINUTES+OUTBOUND][0].Value != 0 || - rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 94 { + rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 7 { t.Errorf("Error extracting minutes from balance: %+v, %+v", rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) } @@ -720,7 +800,7 @@ func TestDebitCreditSubjectMixed(t *testing.T) { } t.Error("Error truncating extra timespans: ", len(cc.Timespans), cc.Timespans[0].GetDuration()) } -} +}*/ func TestDebitCreditSubjectMixedMoreTS(t *testing.T) { b1 := &Balance{Uuid: "testb", Value: 70, Weight: 10, DestinationId: "NAT", RatingSubject: "minu"} @@ -746,17 +826,28 @@ func TestDebitCreditSubjectMixedMoreTS(t *testing.T) { TOR: MINUTES, deductConnectFee: true, } + cd := &CallDescriptor{ + Tenant: cc.Tenant, + Category: cc.Category, + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[1].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 50, RatingSubject: "minu"}}, }} var err error - err = rifsBalance.debitCreditBalance(cd, false, false) + cc, err = rifsBalance.debitCreditBalance(cd, false, false) if err == nil { t.Error("Error showing debiting balance error: ", err) } //t.Logf("%+v %+v", cc.Timespans[0], cc.Timespans[1]) - if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testb" || + /*if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testb" || cc.Timespans[0].Increments[0].Duration != time.Second { t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) } @@ -772,7 +863,7 @@ func TestDebitCreditSubjectMixedMoreTS(t *testing.T) { t.Log(ts.GetDuration()) } t.Error("Error truncating extra timespans: ", len(cc.Timespans)) - } + }*/ } func TestDebitCreditSubjectMixedPartPay(t *testing.T) { @@ -799,17 +890,28 @@ func TestDebitCreditSubjectMixedPartPay(t *testing.T) { TOR: MINUTES, deductConnectFee: true, } + cd := &CallDescriptor{ + Tenant: cc.Tenant, + Category: cc.Category, + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[1].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ MINUTES + OUTBOUND: BalanceChain{b1}, CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 75, RatingSubject: "minu"}}, }} var err error - err = rifsBalance.debitCreditBalance(cd, false, false) + cc, err = rifsBalance.debitCreditBalance(cd, false, false) if err == nil { t.Error("Error showing debiting balance error: ", err) } //t.Logf("%+v %+v", cc.Timespans[0], cc.Timespans[1]) - if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testb" || + /*if cc.Timespans[0].Increments[0].BalanceInfo.UnitBalanceUuid != "testb" || cc.Timespans[0].Increments[0].Duration != time.Second { t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0]) } @@ -825,7 +927,7 @@ func TestDebitCreditSubjectMixedPartPay(t *testing.T) { t.Log(ts.GetDuration()) } t.Error("Error truncating extra timespans: ", len(cc.Timespans)) - } + }*/ } func TestAccountdebitBalance(t *testing.T) { @@ -1048,6 +1150,17 @@ func TestDebitShared(t *testing.T) { }, deductConnectFee: true, } + cd := &CallDescriptor{ + Tenant: cc.Tenant, + Category: cc.Category, + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rif := &Account{Id: "rif", BalanceMap: map[string]BalanceChain{ CREDIT + OUTBOUND: BalanceChain{&Balance{Uuid: "moneya", Value: 60, SharedGroup: "SG_TEST"}}, }} @@ -1060,7 +1173,7 @@ func TestDebitShared(t *testing.T) { accountingStorage.SetAccount(groupie) accountingStorage.SetSharedGroup(sg) cache2go.Cache(SHARED_GROUP_PREFIX+"SG_TEST", sg) - err := rif.debitCreditBalance(cd, false, false) + cc, err := rif.debitCreditBalance(cd, false, false) if err != nil { t.Error("Error debiting balance: ", err) } @@ -1106,12 +1219,21 @@ func TestDebitSMS(t *testing.T) { }, TOR: SMS, } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ SMS + OUTBOUND: BalanceChain{&Balance{Uuid: "testm", Value: 100, Weight: 5, DestinationId: "NAT"}}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, }} var err error - err = rifsBalance.debitCreditBalance(cd, false, false) + cc, err = rifsBalance.debitCreditBalance(cd, false, false) if err != nil { t.Error("Error debiting balance: ", err) } @@ -1147,12 +1269,21 @@ func TestDebitDataUnits(t *testing.T) { }, TOR: DATA, } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ DATA + OUTBOUND: BalanceChain{&Balance{Uuid: "testm", Value: 100, Weight: 5, DestinationId: "NAT"}}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, }} var err error - err = rifsBalance.debitCreditBalance(cd, false, false) + cc, err = rifsBalance.debitCreditBalance(cd, false, false) if err != nil { t.Error("Error debiting balance: ", err) } @@ -1187,12 +1318,21 @@ func TestDebitDataMoney(t *testing.T) { }, TOR: DATA, } + cd := &CallDescriptor{ + TimeStart: cc.Timespans[0].TimeStart, + TimeEnd: cc.Timespans[0].TimeEnd, + Direction: cc.Direction, + Destination: cc.Destination, + TOR: cc.TOR, + DurationIndex: cc.GetDuration(), + test_callcost: cc, + } rifsBalance := &Account{Id: "other", BalanceMap: map[string]BalanceChain{ DATA + OUTBOUND: BalanceChain{&Balance{Uuid: "testm", Value: 0, Weight: 5, DestinationId: "NAT"}}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 160}}, }} var err error - err = rifsBalance.debitCreditBalance(cd, false, false) + cc, err = rifsBalance.debitCreditBalance(cd, false, false) if err != nil { t.Error("Error debiting balance: ", err) } @@ -1201,7 +1341,7 @@ func TestDebitDataMoney(t *testing.T) { t.Error("Error extracting minutes from balance: ", rifsBalance.BalanceMap[DATA+OUTBOUND][0].Value, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value) } } -*/ + func TestAccountGetDefaultMoneyBalanceEmpty(t *testing.T) { acc := &Account{} defBal := acc.GetDefaultMoneyBalance(OUTBOUND) diff --git a/engine/balances.go b/engine/balances.go index e427a9406..8ed980edc 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -220,7 +220,7 @@ func (b *Balance) GetMinutesForCredit(origCD *CallDescriptor, initialCredit floa func (b *Balance) GetCost(cd *CallDescriptor, getStandarIfEmpty bool) (*CallCost, error) { // testing only if cd.test_callcost != nil { - return cd.test_callcost.Clone(), nil + return cd.test_callcost, nil } if b.RatingSubject != "" && !strings.HasPrefix(b.RatingSubject, utils.ZERO_RATING_SUBJECT_PREFIX) { origSubject := cd.Subject @@ -315,6 +315,7 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, count bool, ub *Account, moneyB //log.Printf("::::::: %+v", cd) cc, err = b.GetCost(cd, true) cc.Timespans.Decompress() + //log.Printf("CC: %+v", cc) if err != nil { return nil, fmt.Errorf("Error getting new cost for balance subject: %v", err) } @@ -327,6 +328,7 @@ func (b *Balance) DebitUnits(cd *CallDescriptor, count bool, ub *Account, moneyB // debit minutes and money seconds := inc.Duration.Seconds() cost := inc.Cost + //log.Printf("INC: %+v", inc) var moneyBal *Balance for _, mb := range moneyBalances { if mb.Value >= cost { @@ -375,7 +377,7 @@ func (b *Balance) DebitMoney(cd *CallDescriptor, count bool, ub *Account) (cc *C if !b.IsActiveAt(cd.TimeStart) || b.Value <= 0 { return } - //log.Printf("}}}}}}} %+v", cd) + //log.Printf("}}}}}}} %+v", cd.test_callcost) cc, err = b.GetCost(cd, true) cc.Timespans.Decompress() //log.Printf("CallCost In Debit: %+v", cc) @@ -389,9 +391,10 @@ func (b *Balance) DebitMoney(cd *CallDescriptor, count bool, ub *Account) (cc *C if ts.Increments == nil { ts.createIncrementsSlice() } - + //log.Printf("TS: %+v", ts) for incIndex, inc := range ts.Increments { // check standard subject tags + //log.Printf("INC: %+v", inc) amount := inc.Cost if b.Value >= amount { b.SubstractAmount(amount) @@ -418,6 +421,7 @@ func (b *Balance) DebitMoney(cd *CallDescriptor, count bool, ub *Account) (cc *C } } } + //log.Printf("END: %+v", cd.test_callcost) if len(cc.Timespans) == 0 { cc = nil } diff --git a/engine/callcost.go b/engine/callcost.go index 6643cd93d..a4de48e95 100644 --- a/engine/callcost.go +++ b/engine/callcost.go @@ -157,19 +157,3 @@ func (cc *CallCost) GetLongestRounding() (roundingDecimals int, roundingMethod s } return } - -func (cc *CallCost) Clone() *CallCost { - newCC := &CallCost{ - Direction: cc.Direction, - Category: cc.Category, - Tenant: cc.Tenant, - Subject: cc.Subject, - Account: cc.Account, - Destination: cc.Destination, - TOR: cc.TOR, - Cost: cc.Cost, - deductConnectFee: cc.deductConnectFee, - } - copy(newCC.Timespans, cc.Timespans) - return cc -}