From 9985fd198cd0fe01b1ec1b86ab869275f2f717af Mon Sep 17 00:00:00 2001 From: el Mau Date: Mon, 24 Oct 2022 20:35:26 -0500 Subject: [PATCH] Agregar addenda 2 de cliente --- source/cfdi-txt.py | 2 +- source/helper/util.py | 147 +++++++++++++++++++++++++----------------- 2 files changed, 89 insertions(+), 60 deletions(-) diff --git a/source/cfdi-txt.py b/source/cfdi-txt.py index 43b5ff7..618e995 100755 --- a/source/cfdi-txt.py +++ b/source/cfdi-txt.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python import argparse from helper import util diff --git a/source/helper/util.py b/source/helper/util.py index 6f9a0d7..edcc80f 100644 --- a/source/helper/util.py +++ b/source/helper/util.py @@ -10,9 +10,6 @@ from settings import DEBUG, log, PATH_XSLT, DELETE_FILES, PAC_AUTH from helper.comercio import PACComercioDigital as PAC -from . import util2 - - def _call(args): return subprocess.check_output(args, shell=True).decode() @@ -239,16 +236,17 @@ class DictToCfdi(): return def _addenda(self): - type_addenda = self._data['addenda'].get('type', '') + type_boveda = self._data['addenda'].get('type', '') + type_client = self._data['addenda'].get('type_client', '') data = self._data['addenda'].get('boveda', False) - self._boveda(data) - if type_addenda: + self._boveda(type_boveda, data) + if type_client: data = self._data['addenda'].get('cliente', False) - partes = self._data['addenda']['partes'] - getattr(self, f'_addenda_{type_addenda}')(data, partes) + partes = self._data['addenda'].get('partes', ()) + self._addenda_client(type_client, data, partes) return - def _boveda(self, data): + def _boveda(self, type_boveda, data): if not data: return @@ -269,6 +267,7 @@ class DictToCfdi(): node_name = f'{{{XMLNS}}}BOVEDAFISCAL' node = ET.SubElement(self._node_addenda, node_name, schema, nsmap=NSMAP) + # ~ Type 02 for k, v in data.items(): node_name = f'{{{XMLNS}}}{k}' n = ET.SubElement(node, node_name) @@ -276,14 +275,21 @@ class DictToCfdi(): return - def _addenda_02(self, data, partes): + def _addenda_client(self, type_client, data, partes): if not data: return - XMLNS = 'http://www.sas-automative/en/locations/local-offices-and-plants/mexico/plant-puebla.html' - NSMAP = {'PMT': XMLNS} - version = data.pop('version') - attr = {'version': version} + if type_client == '1': + XMLNS = 'http://www.vwnovedades.com/volkswagen/kanseilab/shcp/2009/Addenda/PMT' + NSMAP = {'PMT': XMLNS} + elif type_client == '2': + XMLNS = 'http://www.vwnovedades.com/volkswagen/kanseilab/shcp/2009/Addenda/PSV' + NSMAP = {'PSV': XMLNS} + elif type_client == '4': + XMLNS = 'http://www.sas-automative/en/locations/local-offices-and-plants/mexico/plant-puebla.html' + NSMAP = {'PMT': XMLNS} + + attr = data.pop('Factura') node_name = f'{{{XMLNS}}}Factura' node = ET.SubElement(self._node_addenda, node_name, **attr, nsmap=NSMAP) @@ -516,35 +522,36 @@ class DataToDict(): def _boveda(self, data): type_addenda = data[0] - fields = ( - 'Razon_Social_destino', - 'Calle_Destino', - 'Colonia_Destino', - 'Ciudad_Destino', - 'Estado_Destino', - 'Pais_Destino', - 'CP_Destino_consigan', - 'RFC_Destino_consigna', - 'Telefono_Receptor', - 'Peso_Bruto', - 'Peso_Neto', - 'Incoterm', - 'leyenda_pie', - 'R.vto', - 'TIPO_CAMBIO_FACTURA', - 'R.cte', - 'RI_Solicitante', - 'R.fefa', - 'Razon_Social_facturado', - 'Calle_facturado', - 'Colonia_facturado', - 'RFC_destino', - 'Telefono_facturado', - 'NUMCTAPAGO', - ) - boveda = {} - for i, f in enumerate(fields): - boveda[f] = data[i+1] + if type_addenda == '02': + fields = ( + 'Razon_Social_destino', + 'Calle_Destino', + 'Colonia_Destino', + 'Ciudad_Destino', + 'Estado_Destino', + 'Pais_Destino', + 'CP_Destino_consigan', + 'RFC_Destino_consigna', + 'Telefono_Receptor', + 'Peso_Bruto', + 'Peso_Neto', + 'Incoterm', + 'leyenda_pie', + 'R.vto', + 'TIPO_CAMBIO_FACTURA', + 'R.cte', + 'RI_Solicitante', + 'R.fefa', + 'Razon_Social_facturado', + 'Calle_facturado', + 'Colonia_facturado', + 'RFC_destino', + 'Telefono_facturado', + 'NUMCTAPAGO', + ) + boveda = {} + for i, f in enumerate(fields): + boveda[f] = data[i+1] self._cfdi['addenda']['type'] = type_addenda self._cfdi['addenda']['boveda'] = boveda @@ -553,8 +560,30 @@ class DataToDict(): def _addenda(self, header): self._type_header = header[1] - if self._type_header == '4': - data = {'version': header[3]} + self._cfdi['addenda']['type_client'] = self._type_header + data = {} + if self._type_header == '2': + data['Factura'] = { + 'version': header[3], + 'tipoDocumentoFiscal': header[4], + 'tipoDocumentoVWM': header[5], + 'division': header[6]} + data['Moneda'] = { + 'tipoMoneda': header[7], + 'tipoCambio': header[8]} + data['Proveedor'] = { + 'codigo': header[10], + 'nombre': header[11], + 'correoContacto': header[12]} + data['Origen'] = {'codigo': header[13]} + data['Destino'] = { + 'codigo': header[14], + 'naveReciboMaterial': header[15]} + data['Referencias'] = {'referenciaProveedor': header[16]} + data['Solicitante'] = {'correo': header[17]} + data['Archivo'] = {'datos': header[20], 'tipo': 'ZIP'} + elif self._type_header == '4': + data['Factura'] = {'version': header[3]} data['Moneda'] = {'tipoMoneda': header[7]} data['Proveedor'] = {'codigo': header[10], 'nombre': header[11]} data['Referencias'] = {'referenciaProveedor': header[16]} @@ -563,7 +592,20 @@ class DataToDict(): return def _addenda_partes(self, parte): - if self._type_header == '4': + if self._type_header == '2': + attr = {'posicion': parte[0], + 'numeroMaterial': parte[1], + 'descripcionMaterial': parte[2], + 'cantidadMaterial': parte[3], + 'unidadMedida': parte[4], + 'precioUnitario': parte[5], + 'montoLinea': parte[6], + 'codigoImpuesto': parte[8], + 'referencias': { + 'ordenCompra': parte[7], + } + } + elif self._type_header == '4': attr = {'posicion': parte[0], 'numeroMaterial': parte[1], 'descripcionMaterial': parte[2], @@ -659,16 +701,3 @@ def stamp_pac(source, target): f.write(result['xml']) log.info(f'\tTimbrada: {new_path}') return - - -# ~ To delete - -def _version33(path, target): - from .cfdi_xml import CFDI - - data = util2.load_data(path) - cfdi = CFDI() - xml = cfdi.get_xml(data) - _save_file(path, target, xml) - - return