From d9c99009c886c1f15fc7383a322c151e5221671d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luismi=20Cavall=C3=A9?= Date: Tue, 29 Oct 2024 16:00:34 +0000 Subject: [PATCH] Support customer post code from address --- parties.go | 7 ++++++- parties_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/parties.go b/parties.go index 845a65d..fde0c8f 100644 --- a/parties.go +++ b/parties.go @@ -58,11 +58,16 @@ func newReceptor(customer *org.Party, issuePlace string) *Receptor { } } + postcode := customer.Ext[cfdi.ExtKeyPostCode].String() // TODO: Drop support for ExtKeyPostCode + if postcode == "" && len(customer.Addresses) > 0 { + postcode = customer.Addresses[0].Code + } + return &Receptor{ Nombre: customer.Name, Rfc: customer.TaxID.Code.String(), RegimenFiscalReceptor: customer.Ext[cfdi.ExtKeyFiscalRegime].String(), UsoCFDI: customer.Ext[cfdi.ExtKeyUse].String(), - DomicilioFiscalReceptor: customer.Ext[cfdi.ExtKeyPostCode].String(), + DomicilioFiscalReceptor: postcode, } } diff --git a/parties_test.go b/parties_test.go index a317c11..dd9266a 100644 --- a/parties_test.go +++ b/parties_test.go @@ -4,6 +4,8 @@ import ( "testing" "github.com/invopop/gobl.cfdi/test" + "github.com/invopop/gobl/addons/mx/cfdi" + "github.com/invopop/gobl/org" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -62,4 +64,23 @@ func TestReceptor(t *testing.T) { assert.Equal(t, "9014514805", r.NumRegIdTrib) assert.Equal(t, "COL", r.ResidenciaFiscal) }) + + t.Run("should map DomicilioFiscal from customer post code", func(t *testing.T) { + inv, err := test.LoadTestInvoice("invoice-b2b-bare.json") + require.NoError(t, err) + + delete(inv.Customer.Ext, cfdi.ExtKeyPostCode) + inv.Customer.Addresses = []*org.Address{{Code: "21000"}} + + doc, err := test.GenerateCFDIFrom(inv) + require.NoError(t, err) + + r := doc.Receptor + + assert.Equal(t, "URE180429TM6", r.Rfc) + assert.Equal(t, "UNIVERSIDAD ROBOTICA ESPAÑOLA", r.Nombre) + assert.Equal(t, "21000", r.DomicilioFiscalReceptor) + assert.Equal(t, "601", r.RegimenFiscalReceptor) + assert.Equal(t, "G01", r.UsoCFDI) + }) }