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

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