-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e991f3
commit 7c09454
Showing
11 changed files
with
890 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.