From 77677b3019a1f06648ee890664cd26ca3d3bbfbb Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 30 Dec 2020 20:54:16 +0100 Subject: [PATCH] AccountS - debitUnits for concreteBalance, Balance SetDecimalValue and DecimalValue export methods --- accounts/concretebalance.go | 18 ++++++++++++++++++ utils/accountprofile.go | 10 ++++++++++ utils/decimal.go | 4 ++++ 3 files changed, 32 insertions(+) diff --git a/accounts/concretebalance.go b/accounts/concretebalance.go index c190cf621..c15233f59 100644 --- a/accounts/concretebalance.go +++ b/accounts/concretebalance.go @@ -19,6 +19,7 @@ along with this program. If not, see package accounts import ( + "fmt" "time" "github.com/cgrates/cgrates/engine" @@ -48,3 +49,20 @@ func (cb *concreteBalance) debit(cgrEv *utils.CGREventWithOpts, } return } + +// debitUnits is a direct debit of balance units +func (cb *concreteBalance) debitUnits(unts *decimal.Big, incrm *decimal.Big) (dbted *decimal.Big, err error) { + // toDo: handle *balanceLimit + dbted = unts + if cb.blnCfg.DecimalValue().Cmp(unts) == -1 { + dbted = utils.DivideBig(cb.blnCfg.DecimalValue(), incrm).RoundToInt() + } + rmain := utils.SubstractBig(cb.blnCfg.DecimalValue(), dbted) + flt64, ok := rmain.Float64() + if !ok { + return nil, fmt.Errorf("failed representing decimal <%s> as float64", rmain) + } + cb.blnCfg.Value = flt64 + cb.blnCfg.SetDecimalValue(rmain) + return +} diff --git a/utils/accountprofile.go b/utils/accountprofile.go index 317b7e753..1ed9ad67f 100644 --- a/utils/accountprofile.go +++ b/utils/accountprofile.go @@ -87,6 +87,16 @@ func (b *Balance) Compile() (err error) { return } +// SetDecimalValue populates the internal decimal value +func (b *Balance) SetDecimalValue(dVal *decimal.Big) { + b.val = dVal +} + +// DecimalValue returns the internal decimal value +func (b *Balance) DecimalValue() *decimal.Big { + return b.val +} + // ActionProfiles is a sortable list of ActionProfiles type AccountProfiles []*AccountProfile diff --git a/utils/decimal.go b/utils/decimal.go index fefdfb29d..7e8026ef0 100644 --- a/utils/decimal.go +++ b/utils/decimal.go @@ -31,3 +31,7 @@ func MultiplyBig(x, y *decimal.Big) *decimal.Big { func AddBig(x, y *decimal.Big) *decimal.Big { return new(decimal.Big).Add(x, y) } + +func SubstractBig(x, y *decimal.Big) *decimal.Big { + return new(decimal.Big).Sub(x, y) +}