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

     1  package models
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"time"
     7  
     8  	"github.com/mundipagg/boleto-api/util"
     9  )
    10  
    11  // Title título de cobrança de entrada
    12  type Title struct {
    13  	CreateDate     time.Time `json:"createDate,omitempty"`
    14  	ExpireDateTime time.Time `json:"expireDateTime,omitempty"`
    15  	ExpireDate     string    `json:"expireDate,omitempty"`
    16  	AmountInCents  uint64    `json:"amountInCents,omitempty"`
    17  	OurNumber      uint      `json:"ourNumber,omitempty"`
    18  	Instructions   string    `json:"instructions,omitempty"`
    19  	DocumentNumber string    `json:"documentNumber,omitempty"`
    20  	NSU            string    `json:"nsu,omitempty"`
    21  	BoletoType     string    `json:"boletoType,omitempty"`
    22  	Rules          *Rules    `json:"rules,omitempty"`
    23  	Fees           *Fees     `json:"fees,omitempty"`
    24  	BoletoTypeCode string
    25  }
    26  
    27  //ValidateInstructionsLength valida se texto das instruções possui quantidade de caracteres corretos
    28  func (t Title) ValidateInstructionsLength(max int) error {
    29  	if len(t.Instructions) > max {
    30  		return NewErrorResponse("MPInstructions", fmt.Sprintf("Instruções não podem passar de %d caracteres", max))
    31  	}
    32  	return nil
    33  }
    34  
    35  //ValidateDocumentNumber número do documento
    36  func (t *Title) ValidateDocumentNumber() error {
    37  	re := regexp.MustCompile("(\\D+)")
    38  	ad := re.ReplaceAllString(t.DocumentNumber, "")
    39  	if ad == "" {
    40  		t.DocumentNumber = ad
    41  	} else if len(ad) < 10 {
    42  		t.DocumentNumber = util.PadLeft(ad, "0", 10)
    43  	} else {
    44  		t.DocumentNumber = ad[:10]
    45  	}
    46  	return nil
    47  }
    48  
    49  //IsExpireDateValid retorna um erro se a data de expiração for inválida
    50  func (t *Title) IsExpireDateValid() error {
    51  	d, err := parseDate(t.ExpireDate)
    52  	if err != nil {
    53  		return NewErrorResponse("MPExpireDate", fmt.Sprintf("Data em um formato inválido, esperamos AAAA-MM-DD e recebemos %s", t.ExpireDate))
    54  	}
    55  	n, _ := parseDate(util.BrNow().Format("2006-01-02"))
    56  	t.CreateDate = n
    57  	t.ExpireDateTime = d
    58  	if t.CreateDate.After(t.ExpireDateTime) {
    59  		return NewErrorResponse("MPExpireDate", "Data de expiração não pode ser menor que a data de hoje")
    60  	}
    61  	return nil
    62  }
    63  
    64  //IsAmountInCentsValid retorna um erro se o valor em centavos for inválido
    65  func (t *Title) IsAmountInCentsValid() error {
    66  	if t.AmountInCents < 1 {
    67  		return NewErrorResponse("MPAmountInCents", "Valor não pode ser menor do que 1 centavo")
    68  	}
    69  	return nil
    70  }
    71  
    72  //HasRules Verifica se o nó de rules está preenchido
    73  func (t *Title) HasRules() bool {
    74  	return t.Rules != nil
    75  }
    76  
    77  //HasFees Verifica se o nó de fees está preenchido
    78  func (t Title) HasFees() bool {
    79  	return t.Fees != nil
    80  }
    81  
    82  func parseDate(t string) (time.Time, error) {
    83  	date, err := time.Parse("2006-01-02", t)
    84  	if err != nil {
    85  		return time.Now(), err
    86  	}
    87  	return date, nil
    88  }