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

     1  package validations
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  
     7  	"github.com/mundipagg/boleto-api/models"
     8  )
     9  
    10  func SumAccountDigits(a string, m []int) int {
    11  	sum := 0
    12  	for idx, c := range a {
    13  		i, _ := strconv.Atoi(string(c))
    14  		sum += i * m[idx]
    15  	}
    16  	return sum
    17  }
    18  
    19  func InvalidType(t interface{}) error {
    20  	return models.NewErrorResponse("MP500", "Tipo inválido")
    21  }
    22  
    23  func ModElevenCalculator(a string, m []int) string {
    24  	sum := SumAccountDigits(a, m)
    25  
    26  	digit := 11 - sum%11
    27  
    28  	if digit == 10 {
    29  		return "X"
    30  	}
    31  
    32  	if digit == 11 {
    33  		return "0"
    34  	}
    35  	return strconv.Itoa(digit)
    36  }
    37  
    38  //ValidateMaxExpirationDate O emissor Bradesco contém um bug na geração da linha digitável onde,
    39  // quando a data de vencimento é maior do que 21-02-2025 a linha digitável se torna inválida(O própio Bradesco não consegue ler a linha gerada) e não conseguimos gerar a visualização do boleto
    40  // Para evitarmos esse problema, adicionamos temporariamente essa trava que bloqueia a geração de boletos com data de vencimento após a data em questão.
    41  func ValidateMaxExpirationDate(b interface{}) error {
    42  	maxExpDate, _ := time.Parse("2006-01-02", "2025-02-21")
    43  
    44  	switch t := b.(type) {
    45  	case *models.BoletoRequest:
    46  		if t.Title.ExpireDateTime.After(maxExpDate) {
    47  			return models.NewErrorResponse("MPExpireDate", "Data de vencimento não pode ser maior que 21/02/2025")
    48  		}
    49  		return nil
    50  	default:
    51  		return InvalidType(t)
    52  	}
    53  }