-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from invopop/fuel-complement
Convert to EstadoDeCuentaCombustible complement
- Loading branch information
Showing
14 changed files
with
588 additions
and
2,427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package cfdi_test | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/invopop/gobl.cfdi/test" | ||
"github.com/lestrrat-go/libxml2" | ||
"github.com/lestrrat-go/libxml2/xsd" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var updateOut = flag.Bool("update", false, "Update the XML files in the test/data/out directory") | ||
|
||
func TestXMLGeneration(t *testing.T) { | ||
|
||
schema, err := loadSchema() | ||
require.NoError(t, err) | ||
|
||
examples, err := lookupExamples() | ||
require.NoError(t, err) | ||
|
||
for _, example := range examples { | ||
name := fmt.Sprintf("should convert %s example file successfully", example) | ||
|
||
t.Run(name, func(t *testing.T) { | ||
data, err := convertExample(example) | ||
require.NoError(t, err) | ||
|
||
outPath := filepath.Join(test.GetDataPath(), "out", strings.TrimSuffix(example, ".json")+".xml") | ||
|
||
if *updateOut { | ||
errs := validateDoc(schema, data) | ||
for _, e := range errs { | ||
assert.NoError(t, e) | ||
} | ||
if len(errs) > 0 { | ||
return | ||
} | ||
|
||
err = os.WriteFile(outPath, data, 0644) | ||
require.NoError(t, err) | ||
|
||
return | ||
} | ||
|
||
expected, err := os.ReadFile(outPath) | ||
|
||
require.False(t, os.IsNotExist(err), "output file %s missing, run tests with `--update` flag to create", filepath.Base(outPath)) | ||
require.NoError(t, err) | ||
require.Equal(t, string(expected), string(data), "output file %s does not match, run tests with `--update` flag to update", filepath.Base(outPath)) | ||
}) | ||
} | ||
} | ||
|
||
func loadSchema() (*xsd.Schema, error) { | ||
schemaPath := filepath.Join(test.GetTestPath(), "schema", "schema.xsd") | ||
schema, err := xsd.ParseFromFile(schemaPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return schema, nil | ||
} | ||
|
||
func lookupExamples() ([]string, error) { | ||
examples, err := filepath.Glob(filepath.Join(test.GetDataPath(), "*.json")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i, example := range examples { | ||
examples[i] = filepath.Base(example) | ||
} | ||
|
||
return examples, nil | ||
} | ||
|
||
func convertExample(example string) ([]byte, error) { | ||
doc, err := test.NewDocumentFrom(example) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return doc.Bytes() | ||
} | ||
|
||
func validateDoc(schema *xsd.Schema, doc []byte) []error { | ||
xmlDoc, err := libxml2.ParseString(string(doc)) | ||
if err != nil { | ||
return []error{err} | ||
} | ||
|
||
err = schema.Validate(xmlDoc) | ||
if err != nil { | ||
return err.(xsd.SchemaValidationError).Errors() | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package cfdi | ||
|
||
import ( | ||
"encoding/xml" | ||
|
||
"github.com/invopop/gobl/regimes/mx" | ||
) | ||
|
||
// ECC Schema constants | ||
const ( | ||
ECCVersion = "1.2" | ||
ECCTipoOperacion = "Tarjeta" | ||
ECCNamespace = "http://www.sat.gob.mx/EstadoDeCuentaCombustible12" | ||
ECCSchemaLocation = "http://www.sat.gob.mx/sitio_internet/cfd/EstadoDeCuentaCombustible/ecc12.xsd" | ||
) | ||
|
||
// EstadoDeCuentaCombustible stores the fuel account balance data | ||
type EstadoDeCuentaCombustible struct { | ||
XMLName xml.Name `xml:"ecc12:EstadoDeCuentaCombustible"` | ||
Version string `xml:",attr"` | ||
TipoOperacion string `xml:",attr"` | ||
|
||
NumeroDeCuenta string `xml:",attr"` | ||
SubTotal string `xml:",attr"` | ||
Total string `xml:",attr"` | ||
|
||
Conceptos []*ECCConcepto `xml:"ecc12:Conceptos>ecc12:ConceptoEstadoDeCuentaCombustible"` // nolint:misspell | ||
} | ||
|
||
// ECCConcepto stores the data of a fuel purchase | ||
type ECCConcepto struct { | ||
Identificador string `xml:",attr"` | ||
Fecha string `xml:",attr"` | ||
Rfc string `xml:",attr"` | ||
ClaveEstacion string `xml:",attr"` | ||
Cantidad string `xml:",attr"` | ||
TipoCombustible string `xml:",attr"` | ||
Unidad string `xml:",attr,omitempty"` | ||
NombreCombustible string `xml:",attr"` | ||
FolioOperacion string `xml:",attr"` | ||
ValorUnitario string `xml:",attr"` | ||
Importe string `xml:",attr"` | ||
|
||
Traslados []*ECCTraslado `xml:"ecc12:Traslados>ecc12:Traslado"` | ||
} | ||
|
||
// ECCTraslado stores the tax data of a fuel purchase | ||
type ECCTraslado struct { | ||
Impuesto string `xml:",attr"` | ||
TasaOCuota string `xml:",attr"` | ||
Importe string `xml:",attr"` | ||
} | ||
|
||
func addEstadoCuentaCombustible(doc *Document, fc *mx.FuelAccountBalance) { | ||
ecc := &EstadoDeCuentaCombustible{ | ||
Version: ECCVersion, | ||
TipoOperacion: ECCTipoOperacion, | ||
|
||
NumeroDeCuenta: fc.AccountNumber, | ||
SubTotal: fc.Subtotal.String(), | ||
Total: fc.Total.String(), | ||
|
||
Conceptos: newECCConceptos(fc.Lines), // nolint:misspell | ||
} | ||
|
||
doc.ECCNamespace = ECCNamespace | ||
doc.SchemaLocation = doc.SchemaLocation + " " + formatSchemaLocation(ECCNamespace, ECCSchemaLocation) | ||
doc.Complementos = append(doc.Complementos, ecc) | ||
} | ||
|
||
// nolint:misspell | ||
func newECCConceptos(lines []*mx.FuelAccountLine) []*ECCConcepto { | ||
cs := make([]*ECCConcepto, len(lines)) | ||
|
||
for i, l := range lines { | ||
cs[i] = &ECCConcepto{ | ||
Identificador: l.EWalletID.String(), | ||
Fecha: l.PurchaseDateTime.String(), | ||
Rfc: l.VendorTaxCode.String(), | ||
ClaveEstacion: l.ServiceStationCode.String(), | ||
Cantidad: l.Quantity.String(), | ||
TipoCombustible: l.Item.Type.String(), | ||
Unidad: l.Item.Unit.UNECE().String(), | ||
NombreCombustible: l.Item.Name, | ||
FolioOperacion: l.PurchaseCode.String(), | ||
ValorUnitario: l.Item.Price.String(), | ||
Importe: l.Total.String(), | ||
Traslados: newECCTraslados(l.Taxes), | ||
} | ||
} | ||
|
||
return cs | ||
} | ||
|
||
func newECCTraslados(taxes []*mx.FuelAccountTax) []*ECCTraslado { | ||
ts := make([]*ECCTraslado, len(taxes)) | ||
|
||
for i, t := range taxes { | ||
ts[i] = &ECCTraslado{ | ||
Impuesto: t.Code.String(), | ||
TasaOCuota: t.Rate.String(), | ||
Importe: t.Amount.String(), | ||
} | ||
} | ||
|
||
return ts | ||
} |
Oops, something went wrong.