Skip to content

Commit

Permalink
Add remessa
Browse files Browse the repository at this point in the history
  • Loading branch information
martinusso committed Jun 18, 2019
1 parent f3b6a8a commit d86e2ec
Show file tree
Hide file tree
Showing 23 changed files with 968 additions and 332 deletions.
14 changes: 14 additions & 0 deletions beneficiario.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cobranca

type Beneficiario struct {
Nome string
Documento string
}

func (b Beneficiario) TipoInscricao() string {
return tipoInscricao(b.Documento)
}

func (b Beneficiario) GetDocumento() string {
return SemMascara(b.Documento)
}
2 changes: 0 additions & 2 deletions boleto/boleto.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type Boleto struct {
nossoNumero string
campoLivre string
conta cobranca.Conta
pagador Pagador
avalista Avalista
codigoBarras string
linhaDigitavel string
}
Expand Down
8 changes: 4 additions & 4 deletions boleto/boleto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestNewBoleto(t *testing.T) {
today := time.Now().Local().Format(dateFormat)
valor := 273.71

c := cobranca.NewConta(cobranca.CodigoSantander, "4042", "61900", "101", "0282033")
c := NewConta(cobranca.CodigoSantander, "4042", "61900", "101", "0282033")

b, _ := NewBoleto(valor, venc, 1984, c)

Expand Down Expand Up @@ -52,14 +52,14 @@ func TestNewBoleto(t *testing.T) {
}

func TestBoletoBancoNaoSuportado(t *testing.T) {
c := cobranca.NewConta("999", "4042", "61900", "101", "0282033")
c := NewConta("999", "4042", "61900", "101", "0282033")
_, err := NewBoleto(1, time.Now(), 1984, c)

if err == nil {
t.Errorf("Should'n be nil")
}

if err.Error() != bancoNaoSuportado {
t.Errorf("Expected '%s' got '%s'", bancoNaoSuportado, err.Error())
if err.Error() != cobranca.BancoNaoSuportado {
t.Errorf("Expected '%s' got '%s'", cobranca.BancoNaoSuportado, err.Error())
}
}
8 changes: 5 additions & 3 deletions boleto/codigo_barras.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math"
"strconv"
"time"

"github.com/padmoney/cobranca"
)

type CodigoBarras struct {
Expand All @@ -15,10 +17,10 @@ type CodigoBarras struct {
}

func (cb CodigoBarras) String() string {
banco := StrPad(cb.Banco, 3, "0", StrPadLeft)
banco := cobranca.StrPad(cb.Banco, 3, "0", cobranca.StrPadLeft)
val := math.Round(cb.Valor * 100)
valStr := strconv.Itoa(int(val))
valStr = Zeros(valStr, 10)
valStr = cobranca.Zeros(valStr, 10)
fatorVencimento := FatorVencimento(cb.DataVencimento)

s := banco + cb.Moeda() + fatorVencimento + valStr + cb.CampoLivre
Expand All @@ -42,5 +44,5 @@ func FatorVencimento(d time.Time) string {
delta := d.Sub(base)
days := delta.Hours() / 24
s := strconv.Itoa(int(days))
return Zeros(s, 4)
return cobranca.Zeros(s, 4)
}
12 changes: 12 additions & 0 deletions boleto/conta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package boleto

import "github.com/padmoney/cobranca"

func NewConta(banco string, agencia, contaCorrente, carteira, convenio string) cobranca.Conta {
return cobranca.Conta{
Banco: banco,
Agencia: agencia,
ContaCorrente: contaCorrente,
Carteira: carteira,
Convenio: convenio}
}
10 changes: 6 additions & 4 deletions boleto/santander.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package boleto

import (
"strconv"

"github.com/padmoney/cobranca"
)

type Santander struct {
Expand All @@ -13,9 +15,9 @@ func NewSantander(boleto Boleto) Santander {
}

func (s Santander) CampoLivre() string {
convenio := Zeros(s.boleto.Conta().Convenio, 7)
carteira := Zeros(s.boleto.Conta().Carteira, 3)
nossoNumero := OnlyNumbers(s.NossoNumero())
convenio := cobranca.Zeros(s.boleto.Conta().Convenio, 7)
carteira := cobranca.Zeros(s.boleto.Conta().Carteira, 3)
nossoNumero := cobranca.OnlyNumbers(s.NossoNumero())
return "9" + convenio + nossoNumero + "0" + carteira
}

Expand All @@ -30,5 +32,5 @@ func (s Santander) NossoNumero() string {

func (s Santander) codigoBarrasSemDV() string {
n := strconv.FormatInt(s.boleto.Numero(), 10)
return Zeros(n, 12)
return cobranca.Zeros(n, 12)
}
2 changes: 1 addition & 1 deletion boleto/santander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func TestBoletoSantanderSemRegistro(t *testing.T) {
}

func contaSantanderFixture(carteira, convenio string) cobranca.Conta {
return cobranca.NewConta(cobranca.CodigoSantander, "4042", "61900", carteira, convenio)
return NewConta(cobranca.CodigoSantander, "4042", "61900", carteira, convenio)
}
98 changes: 0 additions & 98 deletions boleto/utils.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
package boleto

import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
)

type PadType string

const (
StrPadLeft PadType = "str_pad_left"
StrPadRight PadType = "str_pad_right"
)

var (
transliterations = map[string]*regexp.Regexp{
"A": regexp.MustCompile(`À|Á|Â|Ã|Ä|à|á|â|ã|ä`),
"AA": regexp.MustCompile(`Å|å`),
"AE": regexp.MustCompile(`Æ|æ`),
"C": regexp.MustCompile(`Ç|ç`),
"E": regexp.MustCompile(`È|É|Ê|Ë|è|é|ê|ë`),
"D": regexp.MustCompile(`Ð|ð`),
"I": regexp.MustCompile(`Ì|Í|Î|Ï|ì|í|î|ï`),
"L": regexp.MustCompile(`Ł|ł`),
"N": regexp.MustCompile(`Ñ|ñ|ń`),
"O": regexp.MustCompile(`Ò|Ó|Ô|Õ|Ö|ò|ó|ô|õ|ö|ō`),
"OE": regexp.MustCompile(`Œ|Ø|œ|ø`),
"Th": regexp.MustCompile(`Þ`),
"U": regexp.MustCompile(`Ù|Ú|Û|Ü|ù|ú|û|ü|ũ|ū|ŭ|ů|ű|ų`),
"Y": regexp.MustCompile(`Ý|ý|ÿ`),
"S": regexp.MustCompile(`ś`),
"SS": regexp.MustCompile(`ß`),
"Z": regexp.MustCompile(`ż`),
"TH": regexp.MustCompile(`þ`),
}
)

func Brancos(s string, l int) string {
s = Sanitize(s)
if len(s) > l {
return s[len(s)-l:]
}
return StrPad(s, l, " ", StrPadRight)
}

func Date(year, month, day int) time.Time {
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}
Expand All @@ -63,11 +22,6 @@ func Explode(str string) ([]int, error) {
return data, nil
}

func OnlyNumbers(s string) string {
reg, _ := regexp.Compile("[^0-9]+")
return reg.ReplaceAllString(s, "")
}

func ParseDate(s string) time.Time {
if s == "000000" {
return time.Time{}
Expand Down Expand Up @@ -100,55 +54,3 @@ func Round(v float64, places int) float64 {
}
return round / pow
}

// Sanitize replaces non-ASCII characters with an ASCII approximation, or if none exists, to “?”.
func Sanitize(word string) string {
for repl, regex := range transliterations {
word = regex.ReplaceAllString(word, repl)
}

var safe string
for _, r := range word {
if isAscii(r) {
safe += string(r)
} else {
safe += "?"
}
}
return strings.ToUpper(safe)
}

func isAscii(s rune) bool {
return int(s) >= 32 && int(s) <= 126
}

func StrPad(s string, padLen int, padStr string, padType PadType) string {
LenS := len(s)
if LenS == padLen {
return s
}

if LenS > padLen {
if padType == StrPadLeft {
return s[LenS-padLen:]
} else {
return s[0:padLen]
}
}

c := (padLen - len(s)) / len(padStr)
r := strings.Repeat(padStr, c)
if padType == StrPadLeft {
return r + s
} else {
return s + r
}
}

func Zeros(s string, l int) string {
if len(s) > l {
return s[len(s)-l:]
}
f := "%0" + strconv.Itoa(l) + "s"
return fmt.Sprintf(f, s)
}
Loading

0 comments on commit d86e2ec

Please sign in to comment.