Skip to content

Commit

Permalink
refactor dataCompetencia format (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinusso authored Jan 7, 2021
1 parent bc0c3de commit 5537831
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
33 changes: 27 additions & 6 deletions entity/cliente.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package entity

import "regexp"
import (
"encoding/json"
"regexp"
)

type Cliente struct {
TipoPessoa TipoPessoa `json:"tipoPessoa"`
Expand All @@ -14,6 +17,22 @@ type Cliente struct {
Endereco Endereco `json:"endereco"`
}

func (c Cliente) MarshalJSON() ([]byte, error) {
tp := c.TipoPessoa
if string(c.TipoPessoa) == "" {
tp = tipoPessoa(c.CpfCnpj)
}

type Alias Cliente
return json.Marshal(&struct {
TipoPessoa TipoPessoa `json:"tipoPessoa"`
Alias
}{
Alias: (Alias)(c),
TipoPessoa: tp,
})
}

func NewCliente(n string, d string, end Endereco) Cliente {
c := Cliente{
Nome: n,
Expand All @@ -25,12 +44,14 @@ func NewCliente(n string, d string, end Endereco) Cliente {
}

func (c *Cliente) AutoTipoPessoa() {
switch len(onlyNumbers(c.CpfCnpj)) {
case 11:
c.TipoPessoa = PessoaFisica
case 14:
c.TipoPessoa = PessoaJuridica
c.TipoPessoa = tipoPessoa(c.CpfCnpj)
}

func tipoPessoa(doc string) TipoPessoa {
if len(onlyNumbers(doc)) == 14 {
return PessoaJuridica
}
return PessoaFisica
}

func onlyNumbers(s string) string {
Expand Down
17 changes: 17 additions & 0 deletions entity/nfse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entity

import (
"encoding/json"
"time"

"github.com/hashicorp/go-uuid"
Expand All @@ -26,6 +27,22 @@ type NFSe struct {
Servico Servico `json:"servico"`
}

func (n NFSe) MarshalJSON() ([]byte, error) {
var dataCompetencia string
if n.DataCompetencia != nil {
dataCompetencia = n.DataCompetencia.Format("2006-01-02T15:04Z")
}

type Alias NFSe
return json.Marshal(&struct {
Alias
DataCompetencia string `json:"dataCompetencia,omitempty"`
}{
Alias: (Alias)(n),
DataCompetencia: dataCompetencia,
})
}

// NewNFSe cria um nova nota fiscal
func NewNFSe(c Cliente, s Servico, v float64, a Ambiente) *NFSe {
return &NFSe{
Expand Down
31 changes: 31 additions & 0 deletions entity/nfse_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package entity

import (
"encoding/json"
"testing"
"time"
)

func TestNewNFSe(t *testing.T) {
Expand All @@ -18,3 +20,32 @@ func TestNewNFSe(t *testing.T) {
t.Errorf("EnviarPorEmail should not be false")
}
}

func TestNFSeMarshalJSON(t *testing.T) {
dataCompetencia, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")

n := NFSe{
ID: "e93ceafb-6fd9-4543-9106-e0aebac9ef8d",
Ambiente: Homologacao,
DataCompetencia: &dataCompetencia,
Numero: 42,
EnviarPorEmail: false,
ValorTotal: 3.47,
Cliente: Cliente{
Nome: "John Doe",
Email: "[email protected]",
CpfCnpj: "47142365471",
Endereco: Endereco{},
},
}
b, err := json.Marshal(n)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

expected := `{"id":"e93ceafb-6fd9-4543-9106-e0aebac9ef8d","ambienteEmissao":"Homologacao","numero":42,"enviarPorEmail":false,"valorTotal":3.47,"cliente":{"tipoPessoa":"F","nome":"John Doe","email":"[email protected]","cpfCnpj":"47142365471","endereco":{"logradouro":"","numero":"","bairro":"","cep":"","cidade":"","uf":""}},"servico":{"cnae":"","codigoServicoMunicicio":"","descricao":"","aliquotaIss":0,"issRetidoFonte":false,"valorPis":0,"valorCofins":0,"valorCsll":0,"valorInss":0,"valorIr":0},"dataCompetencia":"2006-01-02T15:04Z"}`
if string(b) != expected {
t.Errorf("Expected '%s', got %s", expected, string(b))
}

}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/padmoney/enotas-go

go 1.15

require github.com/hashicorp/go-uuid v1.0.2
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=

0 comments on commit 5537831

Please sign in to comment.