mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-21 15:18:44 +05:00
SessionS.refundSession to add TOR for proper refunds
This commit is contained in:
@@ -798,7 +798,7 @@ func (cdrS *CDRServer) V2StoreSessionCost(args *ArgsV2CDRSStoreSMCost, reply *st
|
||||
nil, true, utils.NonTransactional)
|
||||
}
|
||||
// end of RPC caching
|
||||
cc := args.Cost.CostDetails.AsCallCost()
|
||||
cc := args.Cost.CostDetails.AsCallCost(utils.EmptyString)
|
||||
cc.Round()
|
||||
roundIncrements := cc.GetRoundIncrements()
|
||||
if len(roundIncrements) != 0 {
|
||||
|
||||
@@ -323,14 +323,17 @@ func (ec *EventCost) AsRefundIncrements(tor string) (cd *CallDescriptor) {
|
||||
}
|
||||
|
||||
// AsCallCost converts an EventCost into a CallCost
|
||||
func (ec *EventCost) AsCallCost() *CallCost {
|
||||
func (ec *EventCost) AsCallCost(tor string) *CallCost {
|
||||
cc := &CallCost{
|
||||
Cost: ec.GetCost(), RatedUsage: float64(ec.GetUsage().Nanoseconds()),
|
||||
TOR: utils.FirstNonEmpty(tor, utils.VOICE),
|
||||
Cost: ec.GetCost(),
|
||||
RatedUsage: float64(ec.GetUsage().Nanoseconds()),
|
||||
AccountSummary: ec.AccountSummary}
|
||||
cc.Timespans = make(TimeSpans, len(ec.Charges))
|
||||
for i, cIl := range ec.Charges {
|
||||
ts := &TimeSpan{Cost: cIl.Cost(),
|
||||
DurationIndex: *cIl.Usage(), CompressFactor: cIl.CompressFactor}
|
||||
DurationIndex: *cIl.Usage(),
|
||||
CompressFactor: cIl.CompressFactor}
|
||||
if cIl.ecUsageIdx == nil { // index was not populated yet
|
||||
ec.ComputeEventCostUsageIndexes()
|
||||
}
|
||||
|
||||
@@ -1076,6 +1076,7 @@ func TestECAsCallCost(t *testing.T) {
|
||||
},
|
||||
}
|
||||
eCC := &CallCost{
|
||||
TOR: utils.VOICE,
|
||||
Cost: 0.85,
|
||||
RatedUsage: 120000000000,
|
||||
AccountSummary: acntSummary,
|
||||
@@ -1214,7 +1215,7 @@ func TestECAsCallCost(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}
|
||||
cc := ec.AsCallCost()
|
||||
cc := ec.AsCallCost(utils.EmptyString)
|
||||
if !reflect.DeepEqual(eCC, cc) {
|
||||
t.Errorf("Expecting: %+v, received: %+v", utils.ToJSON(eCC), utils.ToJSON(cc))
|
||||
}
|
||||
|
||||
@@ -149,29 +149,38 @@ func runSession(acntID string) (err error) {
|
||||
originID := utils.GenUUID() // each test with it's own OriginID
|
||||
|
||||
// topup as much as we know we need for one session
|
||||
topupDur := time.Duration(90) * time.Hour
|
||||
mainTopup := time.Duration(90) * time.Second
|
||||
var addBlcRply string
|
||||
argsAddBalance := &v1.AttrAddBalance{
|
||||
Tenant: "cgrates.org",
|
||||
Account: acntID,
|
||||
BalanceType: utils.VOICE,
|
||||
Value: float64(topupDur.Nanoseconds())}
|
||||
Value: float64(mainTopup.Nanoseconds()),
|
||||
Balance: map[string]interface{}{
|
||||
utils.ID: "MAIN",
|
||||
utils.Weight: 10,
|
||||
},
|
||||
}
|
||||
if err = sCncrRPC.Call(utils.ApierV1AddBalance, argsAddBalance, &addBlcRply); err != nil {
|
||||
return
|
||||
} else if addBlcRply != utils.OK {
|
||||
return fmt.Errorf("received: <%s> to ApierV1.AddBalance", addBlcRply)
|
||||
}
|
||||
bufferTopup := time.Duration(8760) * time.Hour
|
||||
argsAddBalance = &v1.AttrAddBalance{
|
||||
Tenant: "cgrates.org",
|
||||
Account: acntID,
|
||||
BalanceType: utils.VOICE,
|
||||
Value: float64(bufferTopup.Nanoseconds()),
|
||||
Balance: map[string]interface{}{
|
||||
utils.ID: "BUFFER",
|
||||
},
|
||||
}
|
||||
if err = sCncrRPC.Call(utils.ApierV1AddBalance, argsAddBalance, &addBlcRply); err != nil {
|
||||
return
|
||||
} else if addBlcRply != utils.OK {
|
||||
return fmt.Errorf("received: <%s> to ApierV1.AddBalance", addBlcRply)
|
||||
}
|
||||
/*
|
||||
var acnt *engine.Account
|
||||
acntAttrs := &utils.AttrGetAccount{
|
||||
Tenant: "cgrates.org",
|
||||
Account: acntID}
|
||||
if err = sCncrRPC.Call(utils.ApierV2GetAccount, acntAttrs, &acnt); err != nil {
|
||||
return
|
||||
} else if vcBlnc := acnt.BalanceMap[utils.VOICE].GetTotalValue(); vcBlnc != float64(topupDur.Nanoseconds()) {
|
||||
return fmt.Errorf("unexpected voice balance received: %+v", utils.ToIJSON(acnt))
|
||||
}
|
||||
*/
|
||||
time.Sleep(time.Duration(
|
||||
utils.RandomInteger(0, 100)) * time.Millisecond) // randomize between tests
|
||||
|
||||
@@ -291,19 +300,19 @@ func runSession(acntID string) (err error) {
|
||||
}
|
||||
time.Sleep(time.Duration(
|
||||
utils.RandomInteger(0, 100)) * time.Millisecond)
|
||||
/*
|
||||
// make sure the account was properly refunded
|
||||
var acnt *engine.Account
|
||||
acntAttrs := &utils.AttrGetAccount{
|
||||
Tenant: "cgrates.org",
|
||||
Account: acntID}
|
||||
if err = sCncrRPC.Call(utils.ApierV2GetAccount, acntAttrs, &acnt); err != nil {
|
||||
return
|
||||
} else if vcBlnc := acnt.BalanceMap[utils.VOICE].GetTotalValue(); vcBlnc != 0 {
|
||||
return fmt.Errorf("unexpected voice balance received: %+v", utils.ToIJSON(acnt))
|
||||
} else if mnBlnc := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); mnBlnc != 0 {
|
||||
return fmt.Errorf("unexpected voice balance received: %+v", utils.ToIJSON(acnt))
|
||||
}
|
||||
*/
|
||||
|
||||
// make sure the account was properly refunded
|
||||
var acnt *engine.Account
|
||||
acntAttrs := &utils.AttrGetAccount{
|
||||
Tenant: "cgrates.org",
|
||||
Account: acntID}
|
||||
if err = sCncrRPC.Call(utils.ApierV2GetAccount, acntAttrs, &acnt); err != nil {
|
||||
return
|
||||
} else if vcBlnc := acnt.BalanceMap[utils.VOICE].GetTotalValue(); float64(bufferTopup.Nanoseconds())-vcBlnc > 100.0 { // eliminate rounding errors
|
||||
return fmt.Errorf("unexpected voice balance received: %+v", utils.ToIJSON(acnt))
|
||||
} else if mnBlnc := acnt.BalanceMap[utils.MONETARY].GetTotalValue(); mnBlnc != 0 {
|
||||
return fmt.Errorf("unexpected voice balance received: %+v", utils.ToIJSON(acnt))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -562,7 +562,7 @@ func (sS *SessionS) refundSession(s *Session, sRunIdx int, rUsage time.Duration)
|
||||
} else if srplsEC == nil {
|
||||
return
|
||||
}
|
||||
sCC := srplsEC.AsCallCost()
|
||||
sCC := srplsEC.AsCallCost(sr.CD.TOR)
|
||||
var incrmts engine.Increments
|
||||
for _, tmspn := range sCC.Timespans {
|
||||
for _, incr := range tmspn.Increments {
|
||||
@@ -585,7 +585,7 @@ func (sS *SessionS) refundSession(s *Session, sRunIdx int, rUsage time.Duration)
|
||||
Subject: sr.CD.Subject,
|
||||
Account: sr.CD.Account,
|
||||
Destination: sr.CD.Destination,
|
||||
TOR: sr.CD.TOR,
|
||||
TOR: utils.FirstNonEmpty(sr.CD.TOR, utils.VOICE),
|
||||
Increments: incrmts,
|
||||
}
|
||||
var acnt engine.Account
|
||||
|
||||
Reference in New Issue
Block a user