Skip to content

Commit

Permalink
Merge pull request #20 from invopop/mx-metodo-pago
Browse files Browse the repository at this point in the history
Determine MetodoPago based on the total amount due
  • Loading branch information
samlown authored Nov 29, 2023
2 parents af2be09 + bb750c3 commit 138386d
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 11 deletions.
15 changes: 12 additions & 3 deletions cfdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
FakeNoCertificado = "00000000000000000000"
ExportacionNoAplica = "01"
MetodoPagoUnaExhibicion = "PUE"
MetodoPagoParcialidades = "PPD"
ObjetoImpSi = "02"
ImpuestoIVA = "002"
TipoFactorTasa = "Tasa"
Expand Down Expand Up @@ -100,8 +101,8 @@ func NewDocument(env *gobl.Envelope) (*Document, error) {
Total: inv.Totals.TotalWithTax.String(),
Moneda: string(inv.Currency),
Exportacion: ExportacionNoAplica,
MetodoPago: MetodoPagoUnaExhibicion,
FormaPago: lookupFormaPago(inv),
MetodoPago: metodoPago(inv),
FormaPago: formaPago(inv),
CondicionesDePago: paymentTermsNotes(inv),

NoCertificado: FakeNoCertificado,
Expand Down Expand Up @@ -196,7 +197,15 @@ func lookupTipoDeComprobante(inv *bill.Invoice) string {
return code.String()
}

func lookupFormaPago(inv *bill.Invoice) string {
func metodoPago(inv *bill.Invoice) string {
if inv.Totals.Due != nil && inv.Totals.Due.IsZero() {
return MetodoPagoUnaExhibicion
}

return MetodoPagoParcialidades
}

func formaPago(inv *bill.Invoice) string {
r := inv.TaxRegime()
if r == nil {
return ""
Expand Down
31 changes: 31 additions & 0 deletions cfdi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/invopop/gobl.cfdi/test"
"github.com/invopop/gobl/num"
"github.com/invopop/gobl/pay"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -34,6 +36,35 @@ func TestComprobanteIngreso(t *testing.T) {

assert.Nil(t, doc.Complemento)
})

t.Run("should return the proper MetodoPago", func(t *testing.T) {
inv, _ := test.LoadTestInvoice("invoice.json")

// No advances
inv.Payment.Advances = nil
doc, _ := test.GenerateCFDIFrom(inv)
assert.Equal(t, "PPD", doc.MetodoPago)

// Partial settlement
inv.Payment.Advances = []*pay.Advance{
{
Amount: inv.Totals.Payable.Divide(num.MakeAmount(2, 0)),
Description: "Partial settlement",
},
}
doc, _ = test.GenerateCFDIFrom(inv)
assert.Equal(t, "PPD", doc.MetodoPago)

// Full settlement
inv.Payment.Advances = []*pay.Advance{
{
Amount: inv.Totals.Payable,
Description: "Full settlement",
},
}
doc, _ = test.GenerateCFDIFrom(inv)
assert.Equal(t, "PUE", doc.MetodoPago)
})
}

func TestComprobanteEgreso(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions test/data/invoice.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"uuid": "c4ed7c55-fef6-11ed-98ea-e6a7901137ed",
"dig": {
"alg": "sha256",
"val": "fb0df5bea43b678ae4966c14dfd25998e3be7787e61c7351ff007d0b954c4bef"
"val": "e9a3636402ea508b69819bf1b271af26bad73c564f35d813dc8d6b9e553b8849"
},
"draft": true
},
Expand Down Expand Up @@ -82,6 +82,13 @@
"terms": {
"notes": "Pago a 30 días."
},
"advances": [
{
"desc": "Top-up payment",
"percent": "100%",
"amount": "190.86"
}
],
"instructions": {
"key": "credit-transfer"
}
Expand Down Expand Up @@ -132,7 +139,9 @@
},
"tax": "-9.34",
"total_with_tax": "190.86",
"payable": "190.86"
"payable": "190.86",
"advance": "190.86",
"due": "0.00"
}
}
}
2 changes: 1 addition & 1 deletion test/data/out/addenda-mabe.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="I" Serie="LMC" Folio="0010" Fecha="2023-05-29T00:00:00" LugarExpedicion="26015" SubTotal="200.00" Descuento="20.00" Total="171.60" Moneda="MXN" Exportacion="01" MetodoPago="PUE" FormaPago="03" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="I" Serie="LMC" Folio="0010" Fecha="2023-05-29T00:00:00" LugarExpedicion="26015" SubTotal="200.00" Descuento="20.00" Total="171.60" Moneda="MXN" Exportacion="01" MetodoPago="PPD" FormaPago="03" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Emisor Rfc="EKU9003173C9" Nombre="ESCUELA KEMPER URGATE" RegimenFiscal="601"></cfdi:Emisor>
<cfdi:Receptor Rfc="URE180429TM6" Nombre="UNIVERSIDAD ROBOTICA ESPAÑOLA" DomicilioFiscalReceptor="65000" RegimenFiscalReceptor="601" UsoCFDI="G01"></cfdi:Receptor>
<cfdi:Conceptos>
Expand Down
2 changes: 1 addition & 1 deletion test/data/out/bare-minimum-addenda-mabe.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="I" Serie="LMC" Folio="0010" Fecha="2023-05-29T00:00:00" LugarExpedicion="26015" SubTotal="200.00" Total="232.00" Moneda="MXN" Exportacion="01" MetodoPago="PUE" FormaPago="03" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="I" Serie="LMC" Folio="0010" Fecha="2023-05-29T00:00:00" LugarExpedicion="26015" SubTotal="200.00" Total="232.00" Moneda="MXN" Exportacion="01" MetodoPago="PPD" FormaPago="03" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Emisor Rfc="EKU9003173C9" Nombre="ESCUELA KEMPER URGATE" RegimenFiscal="601"></cfdi:Emisor>
<cfdi:Receptor Rfc="URE180429TM6" Nombre="UNIVERSIDAD ROBOTICA ESPAÑOLA" DomicilioFiscalReceptor="65000" RegimenFiscalReceptor="601" UsoCFDI="G01"></cfdi:Receptor>
<cfdi:Conceptos>
Expand Down
2 changes: 1 addition & 1 deletion test/data/out/bare-minimum-invoice.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="I" Serie="LMC" Folio="0010" Fecha="2023-05-29T00:00:00" LugarExpedicion="26015" SubTotal="200.00" Total="232.00" Moneda="MXN" Exportacion="01" MetodoPago="PUE" FormaPago="03" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="I" Serie="LMC" Folio="0010" Fecha="2023-05-29T00:00:00" LugarExpedicion="26015" SubTotal="200.00" Total="232.00" Moneda="MXN" Exportacion="01" MetodoPago="PPD" FormaPago="03" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Emisor Rfc="EKU9003173C9" Nombre="ESCUELA KEMPER URGATE" RegimenFiscal="601"></cfdi:Emisor>
<cfdi:Receptor Rfc="URE180429TM6" Nombre="UNIVERSIDAD ROBOTICA ESPAÑOLA" DomicilioFiscalReceptor="65000" RegimenFiscalReceptor="601" UsoCFDI="G01"></cfdi:Receptor>
<cfdi:Conceptos>
Expand Down
2 changes: 1 addition & 1 deletion test/data/out/credit-note.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="E" Serie="CN" Folio="0003" Fecha="2023-07-26T00:00:00" LugarExpedicion="21000" SubTotal="1.77" Total="1.77" Moneda="MXN" Exportacion="01" MetodoPago="PUE" FormaPago="05" CondicionesDePago="Condiciones de pago" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd" Version="4.0" TipoDeComprobante="E" Serie="CN" Folio="0003" Fecha="2023-07-26T00:00:00" LugarExpedicion="21000" SubTotal="1.77" Total="1.77" Moneda="MXN" Exportacion="01" MetodoPago="PPD" FormaPago="05" CondicionesDePago="Condiciones de pago" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:CfdiRelacionados TipoRelacion="01">
<cfdi:CfdiRelacionado UUID="1fac4464-1111-0000-1111-cd37179db12e"></cfdi:CfdiRelacionado>
</cfdi:CfdiRelacionados>
Expand Down
2 changes: 1 addition & 1 deletion test/data/out/food-vouchers.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:valesdedespensa="http://www.sat.gob.mx/valesdedespensa" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.sat.gob.mx/valesdedespensa http://www.sat.gob.mx/sitio_internet/cfd/valesdedespensa/valesdedespensa.xsd" Version="4.0" TipoDeComprobante="I" Serie="TEST" Folio="00002" Fecha="2023-07-10T00:00:00" LugarExpedicion="21000" SubTotal="10.00" Total="11.60" Moneda="MXN" Exportacion="01" MetodoPago="PUE" FormaPago="05" CondicionesDePago="Condiciones de pago" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:valesdedespensa="http://www.sat.gob.mx/valesdedespensa" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.sat.gob.mx/valesdedespensa http://www.sat.gob.mx/sitio_internet/cfd/valesdedespensa/valesdedespensa.xsd" Version="4.0" TipoDeComprobante="I" Serie="TEST" Folio="00002" Fecha="2023-07-10T00:00:00" LugarExpedicion="21000" SubTotal="10.00" Total="11.60" Moneda="MXN" Exportacion="01" MetodoPago="PPD" FormaPago="05" CondicionesDePago="Condiciones de pago" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Emisor Rfc="EKU9003173C9" Nombre="ESCUELA KEMPER URGATE" RegimenFiscal="601"></cfdi:Emisor>
<cfdi:Receptor Rfc="URE180429TM6" Nombre="UNIVERSIDAD ROBOTICA ESPAÑOLA" DomicilioFiscalReceptor="86991" RegimenFiscalReceptor="601" UsoCFDI="G01"></cfdi:Receptor>
<cfdi:Conceptos>
Expand Down
2 changes: 1 addition & 1 deletion test/data/out/fuel-account-balance.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecc12="http://www.sat.gob.mx/EstadoDeCuentaCombustible12" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.sat.gob.mx/EstadoDeCuentaCombustible12 http://www.sat.gob.mx/sitio_internet/cfd/EstadoDeCuentaCombustible/ecc12.xsd" Version="4.0" TipoDeComprobante="I" Serie="TEST" Folio="00002" Fecha="2023-07-10T00:00:00" LugarExpedicion="21000" SubTotal="10.00" Total="11.60" Moneda="MXN" Exportacion="01" MetodoPago="PUE" FormaPago="05" CondicionesDePago="Condiciones de pago" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ecc12="http://www.sat.gob.mx/EstadoDeCuentaCombustible12" xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd http://www.sat.gob.mx/EstadoDeCuentaCombustible12 http://www.sat.gob.mx/sitio_internet/cfd/EstadoDeCuentaCombustible/ecc12.xsd" Version="4.0" TipoDeComprobante="I" Serie="TEST" Folio="00002" Fecha="2023-07-10T00:00:00" LugarExpedicion="21000" SubTotal="10.00" Total="11.60" Moneda="MXN" Exportacion="01" MetodoPago="PPD" FormaPago="05" CondicionesDePago="Condiciones de pago" Sello="" NoCertificado="00000000000000000000" Certificado="">
<cfdi:Emisor Rfc="EKU9003173C9" Nombre="ESCUELA KEMPER URGATE" RegimenFiscal="601"></cfdi:Emisor>
<cfdi:Receptor Rfc="URE180429TM6" Nombre="UNIVERSIDAD ROBOTICA ESPAÑOLA" DomicilioFiscalReceptor="86991" RegimenFiscalReceptor="601" UsoCFDI="G01"></cfdi:Receptor>
<cfdi:Conceptos>
Expand Down
21 changes: 21 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/invopop/gobl"
cfdi "github.com/invopop/gobl.cfdi"
"github.com/invopop/gobl/bill"
)

// NewDocumentFrom creates a CFDI Document from a GOBL file in the `test/data` folder
Expand All @@ -22,6 +23,16 @@ func NewDocumentFrom(name string) (*cfdi.Document, error) {
return cfdi.NewDocument(env)
}

// LoadTestInvoice returns a GOBL Invoice from a file in the `test/data` folder
func LoadTestInvoice(name string) (*bill.Invoice, error) {
env, err := LoadTestEnvelope(name)
if err != nil {
return nil, err
}

return env.Extract().(*bill.Invoice), nil
}

// LoadTestEnvelope returns a GOBL Envelope from a file in the `test/data` folder
func LoadTestEnvelope(name string) (*gobl.Envelope, error) {
src, _ := os.Open(filepath.Join(GetDataPath(), name))
Expand All @@ -39,6 +50,16 @@ func LoadTestEnvelope(name string) (*gobl.Envelope, error) {
return env, nil
}

// GenerateCFDIFrom returns a CFDI Document from a GOBL Invoice
func GenerateCFDIFrom(inv *bill.Invoice) (*cfdi.Document, error) {
env, err := gobl.Envelop(inv)
if err != nil {
return nil, err
}

return cfdi.NewDocument(env)
}

// GetDataPath returns the path to the `test/data` folder
func GetDataPath() string {
return filepath.Join(GetTestPath(), "data")
Expand Down

0 comments on commit 138386d

Please sign in to comment.