AccountS - debitUnits for concreteBalance, Balance SetDecimalValue and DecimalValue export methods

This commit is contained in:
DanB
2020-12-30 20:54:16 +01:00
parent 56fbc84bea
commit 77677b3019
3 changed files with 32 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
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
}

View File

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

View File

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