github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/models/interest.go (about)

     1  package models
     2  
     3  import "fmt"
     4  
     5  const (
     6  	defaultInterestAmountInCents      = 0
     7  	defaultInterestPercentagePerMonth = 0.0
     8  	minDaysToStartChargingInterest    = 1
     9  )
    10  
    11  //Interest Representa as informações sobre juros
    12  type Interest struct {
    13  	DaysAfterExpirationDate uint    `json:"daysAfterExpirationDate,omitempty"`
    14  	AmountPerDayInCents     uint64  `json:"amountPerDayInCents,omitempty"`
    15  	PercentagePerMonth      float64 `json:"percentagePerMonth,omitempty"`
    16  }
    17  
    18  //HasAmountPerDayInCents Verifica se há AmountPerDayInCents
    19  func (interest *Interest) HasAmountPerDayInCents() bool {
    20  	return interest.AmountPerDayInCents > defaultInterestAmountInCents
    21  }
    22  
    23  //HasPercentagePerMonth Verifica se há PercentagePerMonth
    24  func (interest *Interest) HasPercentagePerMonth() bool {
    25  	return interest.PercentagePerMonth > defaultInterestPercentagePerMonth
    26  }
    27  
    28  //HasDaysAfterExpirationDate Verifica se há DaysAfterExpirationDate
    29  func (interest *Interest) HasDaysAfterExpirationDate() bool {
    30  	return interest.DaysAfterExpirationDate >= minDaysToStartChargingInterest
    31  }
    32  
    33  //HasExclusiveRateValues Verifica se foi informados os valores reverente aos juros de forma exclusiva
    34  func (interest *Interest) HasExclusiveRateValues() bool {
    35  	return (interest.HasAmountPerDayInCents() || interest.HasPercentagePerMonth()) && !(interest.HasAmountPerDayInCents() && interest.HasPercentagePerMonth())
    36  }
    37  
    38  //HasInterest Verifica se algum dado de juros está preenchido
    39  func (interest *Interest) HasInterest() bool {
    40  	return interest != nil
    41  }
    42  
    43  //Validate Valida as regras de negócio da struct Interest
    44  //Caso haja alguma violação retorna o erro caso a regra infrigida, caso contrário retorna nulo
    45  func (interest *Interest) Validate() error {
    46  	if interest.HasInterest() {
    47  		if !interest.HasExclusiveRateValues() {
    48  			return NewErrorResponse("MP400", "Para o campo Interest deve ser informado exclusivamente o parâmetro AmountInCents ou PercentagePerMonth maiores que zero")
    49  		}
    50  		if !interest.HasDaysAfterExpirationDate() {
    51  			return NewErrorResponse("MP400", fmt.Sprintf("Para o campo Interest o parâmetro DaysAfterExpirationDate precisa ser no mínimo %d", minDaysToStartChargingInterest))
    52  		}
    53  	}
    54  
    55  	return nil
    56  }