From ef2da5b401d5076d6b81655031f531993123bd4a Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Tue, 21 Nov 2023 15:51:31 +0000 Subject: [PATCH] Fixing tests and formatting --- addendas/addendas.go | 11 +- cfdi.go | 6 +- go.mod | 2 +- go.sum | 6 +- internal/lines.go | 2 +- taxes.go | 10 +- test/data/addenda-mabe.json | 326 +++++++++++--------- test/data/bare-minimum-addenda-mabe.json | 37 ++- test/data/out/addenda-mabe.xml | 2 +- test/data/out/bare-minimum-addenda-mabe.xml | 2 +- 10 files changed, 222 insertions(+), 182 deletions(-) diff --git a/addendas/addendas.go b/addendas/addendas.go index 8efb97c..4dc3e99 100644 --- a/addendas/addendas.go +++ b/addendas/addendas.go @@ -1,14 +1,19 @@ +// Package addendas adds additional functionality for "Addendas" to the CFDI documents. package addendas import "github.com/invopop/gobl/bill" // For returns a set of addenda objects for the given invoice. -func For(inv *bill.Invoice) []any { +func For(inv *bill.Invoice) ([]any, error) { list := make([]any, 0) if isMabe(inv) { - list = append(list, newMabe(inv)) + ad, err := newMabe(inv) + if err != nil { + return nil, err + } + list = append(list, ad) } - return list + return list, nil } diff --git a/cfdi.go b/cfdi.go index b41381a..d4bdf96 100644 --- a/cfdi.go +++ b/cfdi.go @@ -155,7 +155,11 @@ func addComplementos(doc *Document, complements []*schema.Object) error { } func addAddendas(doc *Document, inv *bill.Invoice) error { - for _, ad := range addendas.For(inv) { + ads, err := addendas.For(inv) + if err != nil { + return err + } + for _, ad := range ads { doc.Addendas = append(doc.Addendas, &ContentWrapper{ad}) } return nil diff --git a/go.mod b/go.mod index 1131173..77bb6df 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/invopop/gobl.cfdi go 1.20 require ( - github.com/invopop/gobl v0.62.2-0.20231121142901-bd3349ee3ec6 + github.com/invopop/gobl v0.62.2-0.20231121145421-5d7dce0fd42d github.com/joho/godotenv v1.5.1 github.com/magefile/mage v1.15.0 github.com/spf13/cobra v1.7.0 diff --git a/go.sum b/go.sum index 0657604..3cec26c 100644 --- a/go.sum +++ b/go.sum @@ -19,10 +19,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/invopop/gobl v0.62.1-0.20231121111925-64c5be7b0226 h1:Qca7HXgrRZOLuu++CdcFmlMWvLumkdiLR8/YfzjKuTE= -github.com/invopop/gobl v0.62.1-0.20231121111925-64c5be7b0226/go.mod h1:sEngvTr2gAxexosO0rmQInVSL8C613TUPvBcFT4xMyM= -github.com/invopop/gobl v0.62.2-0.20231121142901-bd3349ee3ec6 h1:O2U9Fkj+1F6kI0GO3Cx8IBRbG69jCmyC6QPjz2SJrk4= -github.com/invopop/gobl v0.62.2-0.20231121142901-bd3349ee3ec6/go.mod h1:sEngvTr2gAxexosO0rmQInVSL8C613TUPvBcFT4xMyM= +github.com/invopop/gobl v0.62.2-0.20231121145421-5d7dce0fd42d h1:tPpq1L2YH8IyGS452WCPGlbsbjw8u0okxmuhZ21wZi0= +github.com/invopop/gobl v0.62.2-0.20231121145421-5d7dce0fd42d/go.mod h1:sEngvTr2gAxexosO0rmQInVSL8C613TUPvBcFT4xMyM= github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI= github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= github.com/invopop/validation v0.3.0 h1:o260kbjXzoBO/ypXDSSrCLL7SxEFUXBsX09YTE9AxZw= diff --git a/internal/lines.go b/internal/lines.go index da55c9a..5d919d5 100644 --- a/internal/lines.go +++ b/internal/lines.go @@ -19,7 +19,7 @@ func ClaveUnidad(line *bill.Line) string { return string(line.Item.Unit.UNECE()) } -// ClaveProdServe determines the line's Product-Service code +// ClaveProdServ determines the line's Product-Service code func ClaveProdServ(line *bill.Line) string { if line.Item == nil { return "" diff --git a/taxes.go b/taxes.go index 1ec0032..7e074c3 100644 --- a/taxes.go +++ b/taxes.go @@ -1,9 +1,9 @@ package cfdi import ( + "github.com/invopop/gobl.cfdi/internal/format" "github.com/invopop/gobl/bill" "github.com/invopop/gobl/currency" - "github.com/invopop/gobl/num" "github.com/invopop/gobl/regimes/mx" "github.com/invopop/gobl/tax" ) @@ -88,7 +88,7 @@ func newImpuesto(rate *tax.RateTotal, currency *currency.Code, catDef *tax.Categ Base: rate.Base.Rescale(cu).String(), Importe: rate.Amount.Rescale(cu).String(), Impuesto: catDef.Map[mx.KeySATImpuesto].String(), - TasaOCuota: formatTaxPercent(rate.Percent), + TasaOCuota: format.TaxPercent(rate.Percent), TipoFactor: TipoFactorTasa, } @@ -130,13 +130,9 @@ func newConceptoImpuesto(line *bill.Line, tax *tax.Combo, catDef *tax.Category) Base: line.Total.String(), Importe: taxAmount.String(), Impuesto: catDef.Map[mx.KeySATImpuesto].String(), - TasaOCuota: formatTaxPercent(tax.Percent), + TasaOCuota: format.TaxPercent(tax.Percent), TipoFactor: TipoFactorTasa, } return i } - -func formatTaxPercent(percent *num.Percentage) string { - return percent.Amount.Rescale(6).String() -} diff --git a/test/data/addenda-mabe.json b/test/data/addenda-mabe.json index 6e752ba..75531da 100644 --- a/test/data/addenda-mabe.json +++ b/test/data/addenda-mabe.json @@ -1,155 +1,173 @@ { - "$schema": "https://gobl.org/draft-0/envelope", - "head": { - "uuid": "8a51fd30-2a27-11ee-be56-0242ac120002", - "dig": { - "alg": "sha256", - "val": "3aa3b38d147021722253109fa8a6a54cb543611b6ed7528cbdaa9030ecb21359" - }, - "draft": true - }, - "doc": { - "$schema": "https://gobl.org/draft-0/bill/invoice", - "type": "standard", - "series": "LMC", - "code": "0010", - "issue_date": "2023-05-29", - "currency": "MXN", - "supplier": { - "name": "ESCUELA KEMPER URGATE", - "tax_id": { - "country": "MX", - "zone": "26015", - "code": "EKU9003173C9" - }, - "ext": { - "mx-cfdi-fiscal-regime": "601", - "mx-mabe-provider-code": "123456" - } - }, - "customer": { - "name": "UNIVERSIDAD ROBOTICA ESPAÑOLA", - "tax_id": { - "country": "MX", - "zone": "65000", - "code": "URE180429TM6" - }, - "ext": { - "mx-cfdi-fiscal-regime": "601", - "mx-cfdi-use": "G01" - } - }, - "lines": [ - { - "i": 1, - "quantity": "2", - "item": { - "name": "Cigarros", - "price": "100.00", - "ext": { - "mx-cfdi-prod-serv": "50211502", - "mx-mabe-item-code": "CODE123" - } - }, - "sum": "200.00", - "discounts": [ - { - "percent": "10.0%", - "amount": "20.00" - } - ], - "taxes": [ - { - "cat": "VAT", - "rate": "standard", - "percent": "16.0%" - }, - { - "cat": "RVAT", - "percent": "10.6667%" - }, - { - "cat": "ISR", - "percent": "10%" - } - ], - "total": "180.00" - } - ], - "ordering": { - "code": "9100000000" - }, - "payment": { - "instructions": { - "key": "credit-transfer" - } - }, - "delivery": { - "receiver": { - "name": "ESTUFAS 30", - "addresses": [ - { - "street": "Calle 1", - "locality": "Mexico D.F.", - "code": "12345" - } - ], - "ext": { - "mx-mabe-delivery-plant": "S001" - } - } - }, - "totals": { - "sum": "180.00", - "total": "180.00", - "taxes": { - "categories": [ - { - "code": "VAT", - "rates": [ - { - "key": "standard", - "base": "180.00", - "percent": "16.0%", - "amount": "28.80" - } - ], - "amount": "28.80" - }, - { - "code": "RVAT", - "retained": true, - "rates": [ - { - "base": "180.00", - "percent": "10.6667%", - "amount": "19.20" - } - ], - "amount": "19.20" - }, - { - "code": "ISR", - "retained": true, - "rates": [ - { - "base": "180.00", - "percent": "10%", - "amount": "18.00" - } - ], - "amount": "18.00" - } - ], - "sum": "-8.40" - }, - "tax": "-8.40", - "total_with_tax": "171.60", - "payable": "171.60" - }, - "ext": { - "mx-mabe-reference-1": "123456", - "mx-mabe-reference-2": "789" - } - } -} \ No newline at end of file + "$schema": "https://gobl.org/draft-0/envelope", + "head": { + "uuid": "8a51fd30-2a27-11ee-be56-0242ac120002", + "dig": { + "alg": "sha256", + "val": "XXX" + }, + "draft": true + }, + "doc": { + "$schema": "https://gobl.org/draft-0/bill/invoice", + "type": "standard", + "series": "LMC", + "code": "0010", + "issue_date": "2023-05-29", + "currency": "MXN", + "supplier": { + "name": "ESCUELA KEMPER URGATE", + "tax_id": { + "country": "MX", + "zone": "26015", + "code": "EKU9003173C9" + }, + "identities": [ + { + "type": "MABE", + "code": "123456" + } + ], + "ext": { + "mx-cfdi-fiscal-regime": "601" + } + }, + "customer": { + "name": "UNIVERSIDAD ROBOTICA ESPAÑOLA", + "tax_id": { + "country": "MX", + "zone": "65000", + "code": "URE180429TM6" + }, + "ext": { + "mx-cfdi-fiscal-regime": "601", + "mx-cfdi-use": "G01" + } + }, + "lines": [ + { + "i": 1, + "quantity": "2", + "item": { + "name": "Cigarros", + "price": "100.00", + "identities": [ + { + "type": "MABE", + "code": "CODE123" + } + ], + "ext": { + "mx-cfdi-prod-serv": "50211502" + } + }, + "sum": "200.00", + "discounts": [ + { + "percent": "10.0%", + "amount": "20.00" + } + ], + "taxes": [ + { + "cat": "VAT", + "rate": "standard", + "percent": "16.0%" + }, + { + "cat": "RVAT", + "percent": "10.6667%" + }, + { + "cat": "ISR", + "percent": "10%" + } + ], + "total": "180.00" + } + ], + "ordering": { + "code": "9100000000", + "identities": [ + { + "type": "MABE-REF1", + "code": "123456" + } + ] + }, + "payment": { + "instructions": { + "key": "credit-transfer" + } + }, + "delivery": { + "receiver": { + "name": "ESTUFAS 30", + "addresses": [ + { + "street": "Calle 1", + "locality": "Mexico D.F.", + "code": "12345" + } + ], + "identities": [ + { + "type": "MABE-PLANT-ID", + "code": "S001" + } + ] + } + }, + "totals": { + "sum": "180.00", + "total": "180.00", + "taxes": { + "categories": [ + { + "code": "VAT", + "rates": [ + { + "key": "standard", + "base": "180.00", + "percent": "16.0%", + "amount": "28.80" + } + ], + "amount": "28.80" + }, + { + "code": "RVAT", + "retained": true, + "rates": [ + { + "base": "180.00", + "percent": "10.6667%", + "amount": "19.20" + } + ], + "amount": "19.20" + }, + { + "code": "ISR", + "retained": true, + "rates": [ + { + "base": "180.00", + "percent": "10%", + "amount": "18.00" + } + ], + "amount": "18.00" + } + ], + "sum": "-8.40" + }, + "tax": "-8.40", + "total_with_tax": "171.60", + "payable": "171.60" + }, + "ext": { + "mx-mabe-reference-2": "789" + } + } +} diff --git a/test/data/bare-minimum-addenda-mabe.json b/test/data/bare-minimum-addenda-mabe.json index 5158a25..045e9f6 100644 --- a/test/data/bare-minimum-addenda-mabe.json +++ b/test/data/bare-minimum-addenda-mabe.json @@ -4,7 +4,7 @@ "uuid": "8a51fd30-2a27-11ee-be56-0242ac120002", "dig": { "alg": "sha256", - "val": "a8749e521981bc47aa0ff5bc6389e0d809dbe07f8b05c278e7674c3e79b8fe77" + "val": "XXX" }, "draft": true }, @@ -22,9 +22,14 @@ "zone": "26015", "code": "EKU9003173C9" }, + "identities": [ + { + "type": "MABE", + "code": "123456" + } + ], "ext": { - "mx-cfdi-fiscal-regime": "601", - "mx-mabe-provider-code": "123456" + "mx-cfdi-fiscal-regime": "601" } }, "customer": { @@ -46,9 +51,14 @@ "item": { "name": "Cigarros", "price": "100.00", + "identities": [ + { + "type": "MABE", + "code": "CODE123" + } + ], "ext": { - "mx-cfdi-prod-serv": "50211502", - "mx-mabe-item-code": "CODE123" + "mx-cfdi-prod-serv": "50211502" } }, "sum": "200.00", @@ -63,7 +73,13 @@ } ], "ordering": { - "code": "91000000" + "code": "91000000", + "identities": [ + { + "type": "MABE-REF1", + "code": "900900" + } + ] }, "payment": { "instructions": { @@ -73,9 +89,12 @@ "delivery": { "receiver": { "name": "ESTUFAS 30", - "ext": { - "mx-mabe-delivery-plant": "S001" - } + "identities": [ + { + "type": "MABE-PLANT-ID", + "code": "S001" + } + ] } }, "totals": { diff --git a/test/data/out/addenda-mabe.xml b/test/data/out/addenda-mabe.xml index 603c44f..6d44a33 100644 --- a/test/data/out/addenda-mabe.xml +++ b/test/data/out/addenda-mabe.xml @@ -25,7 +25,7 @@ - + diff --git a/test/data/out/bare-minimum-addenda-mabe.xml b/test/data/out/bare-minimum-addenda-mabe.xml index aa49a7d..bad05b5 100644 --- a/test/data/out/bare-minimum-addenda-mabe.xml +++ b/test/data/out/bare-minimum-addenda-mabe.xml @@ -17,7 +17,7 @@ - +