Skip to content

Commit

Permalink
🚀 Add Layout AFDV3. AB#7497
Browse files Browse the repository at this point in the history
  • Loading branch information
wallissonmarinho committed Mar 16, 2021
1 parent 8e991f3 commit 7c09454
Show file tree
Hide file tree
Showing 11 changed files with 890 additions and 148 deletions.
16 changes: 16 additions & 0 deletions file/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func NewRemessaFile(remessa *model.Remessa, fileName string) RemessaFile {
}
}

if remessa.GetLayout() == model.LayoutAFDV3 {
return &remessaAFDV3File{
model: remessa,
fileName: fileName,
encoder: NewEncoder(remessa),
}
}

if remessa.GetLayout() == model.LayoutAEJ {
return &remessaAEJFile{
model: remessa,
Expand Down Expand Up @@ -113,6 +121,14 @@ func NewRetornoFile(layout model.Layout, content io.Reader) (RetornoFile, error)
}, nil
}

if layout.GetLayout() == model.LayoutAFDV3 {
return &retornoAFDV3File{
layout: layout,
content: content,
decoder: NewDecoder(layout),
}, nil
}

if layout.GetLayout() == model.LayoutAEJ {
return &retornoAEJFile{
layout: layout,
Expand Down
74 changes: 74 additions & 0 deletions file/remessa_afd_v3_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package file

import (
"fmt"
"log"
"os"

"github.com/helloticket/superfile/model"
)

type remessaAFDV3File struct {
model *model.Remessa
fileName string
encoder *Encoder
}

func (w *remessaAFDV3File) Write() *os.File {
file, err := os.Create(w.fileName)
if err != nil {
panic(err)
}

header := w.encodeFileHeader()
writeString(file, header)
writeString(file, "\r\n")

for _, lote := range w.encodeLotes() {
writeString(file, lote)
writeString(file, "\r\n")
}

trailer := w.encodeFileTrailer()
writeString(file, trailer)
writeString(file, "\r\n")

if err := file.Close(); err != nil {
log.Println(err)
}

return file
}

func (w *remessaAFDV3File) encodeLotes() []string {
encoded := []string{}

for _, lote := range w.model.Lotes {
for _, registro := range lote.Segmentos() {
block := fmt.Sprintf("detalhes.%s", registro.Nome)
layout := w.getLayoutFor("detalhes")
layout = layout[registro.Nome].(map[interface{}]interface{})
bodyEncoded := w.encoder.ParseAndEncode(block, registro.Valores, layout)
encoded = append(encoded, bodyEncoded)
}
}

return encoded
}

func (w *remessaAFDV3File) encodeFileHeader() string {
config := w.model.GetRemessaLayout()
layout := config["header_arquivo"].(map[interface{}]interface{})
return w.encoder.ParseAndEncode("header_arquivo", w.model.Header, layout)
}

func (w *remessaAFDV3File) encodeFileTrailer() string {
config := w.model.GetRemessaLayout()
layout := config["trailer_arquivo"].(map[interface{}]interface{})
return w.encoder.ParseAndEncode("trailer_arquivo", w.model.Trailer, layout)
}

func (w *remessaAFDV3File) getLayoutFor(name string) map[interface{}]interface{} {
config := w.model.GetRemessaLayout()
return config[name].(map[interface{}]interface{})
}
62 changes: 62 additions & 0 deletions file/retorno_afd_v3_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package file

import (
"fmt"
"io"

"github.com/helloticket/superfile/helper"
"github.com/helloticket/superfile/model"
)

type retornoAFDV3File struct {
content io.Reader
layout model.Layout
decoder *Decoder
}

func (r *retornoAFDV3File) Read() *model.Retorno {
const registroHeaderArquivo = 1
const registroTrailerArquivo = 999999999

numeroSegmentos := int64(0)
retorno := model.NewRetorno(r.layout)
loteCorrente := retorno.NovoLote(1)
detalheCorrente := loteCorrente.NovoDetalhe()
loteCorrente.InserirDetalhe(detalheCorrente)
retorno.InserirLote(loteCorrente)

NewReader(r.content).Mapper(func(pos int64, linha string) {
tipoRegistro := helper.ToSafeInt(linha[9:10])
codigoRegistro := helper.ToSafeInt(linha[0:9])

if registroHeaderArquivo == tipoRegistro {
retorno.Header = r.decodeFileHeader(pos, retorno, linha)
} else if codigoRegistro == registroTrailerArquivo && tipoRegistro == 0 {
retorno.Trailer = r.decodeFileTrailer(pos, retorno, linha)
} else {
numeroSegmentos++
segmento := r.decodeSegmento(pos, retorno, linha)
detalheCorrente[fmt.Sprintf("%d.%s", numeroSegmentos, segmento.Nome)] = segmento.Valores
}
})

return retorno
}

func (r *retornoAFDV3File) decodeSegmento(pos int64, retorno *model.Retorno, row string) model.Segmento {
decode := DecodeRetorno{decoder: r.decoder, layout: r.layout}

return decode.Segmento(pos, 9, 10, row, retorno)
}

func (r *retornoAFDV3File) decodeFileHeader(pos int64, retorno *model.Retorno, row string) map[string]interface{} {
decode := DecodeRetorno{decoder: r.decoder, layout: r.layout}

return decode.HeaderArquivo(pos, row, retorno)
}

func (r *retornoAFDV3File) decodeFileTrailer(pos int64, retorno *model.Retorno, row string) map[string]interface{} {
decode := DecodeRetorno{decoder: r.decoder, layout: r.layout}

return decode.TrailerArquivo(pos, row, retorno)
}
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ module github.com/helloticket/superfile
go 1.13

require (
github.com/AlekSi/gocov-xml v0.0.0-20190121064608-3a14fb1c4737 // indirect
github.com/axw/gocov v1.0.0 // indirect
github.com/helderfarias/superfile v0.0.0-20200424021745-cc762154993d // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/piquette/finance-go v1.0.0
github.com/securego/gosec v0.0.0-20200401082031-e946c8c39989 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/viper v1.7.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.5.1
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.8
)
Loading

0 comments on commit 7c09454

Please sign in to comment.