diff --git a/entity/cliente.go b/entity/cliente.go index c2e3d68..c0e421a 100644 --- a/entity/cliente.go +++ b/entity/cliente.go @@ -1,6 +1,9 @@ package entity -import "regexp" +import ( + "encoding/json" + "regexp" +) type Cliente struct { TipoPessoa TipoPessoa `json:"tipoPessoa"` @@ -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, @@ -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 { diff --git a/entity/nfse.go b/entity/nfse.go index 7d47f77..0204ac0 100644 --- a/entity/nfse.go +++ b/entity/nfse.go @@ -1,6 +1,7 @@ package entity import ( + "encoding/json" "time" "github.com/hashicorp/go-uuid" @@ -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{ diff --git a/entity/nfse_test.go b/entity/nfse_test.go index 4ede7d4..b95c9bf 100644 --- a/entity/nfse_test.go +++ b/entity/nfse_test.go @@ -1,7 +1,9 @@ package entity import ( + "encoding/json" "testing" + "time" ) func TestNewNFSe(t *testing.T) { @@ -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: "john@doe.com", + 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":"john@doe.com","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)) + } + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..81adc27 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/padmoney/enotas-go + +go 1.15 + +require github.com/hashicorp/go-uuid v1.0.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9c04079 --- /dev/null +++ b/go.sum @@ -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=