diff --git a/source/app/models/main.py b/source/app/models/main.py index df6903e..b190ea9 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -4617,6 +4617,89 @@ class Facturas(BaseModel): return result + def _validate_import_32(self, data, sxml): + currencies = { + 'Peso Mexicano': 'MXN', + } + regimenes = { + 'REGIMEN GENERAL DE LEY PERSONAS MORALES': '601', + } + # ~ print(data) + invoice = data['invoice'] + receptor = data['receptor'] + name = receptor.get('nombre', '') + tipo_persona = 1 + if receptor['rfc'] == 'XEXX010101000': + tipo_persona = 4 + elif receptor['rfc'] == RFC_PUBLICO: + tipo_persona = 3 + elif len(receptor['rfc']) == 12: + tipo_persona = 2 + new = { + 'tipo_persona': tipo_persona, + 'rfc': receptor['rfc'], + 'slug': util.to_slug(name), + 'nombre': name, + 'es_cliente': True, + } + cliente, _ = Socios.get_or_create(**new) + + tipo_cambio = float(invoice.get('TipoCambio', '1.0')) + total = float(invoice['Total']) + invoice = { + 'cliente': cliente, + 'version': invoice['version'], + 'serie': invoice.get('serie', ''), + 'folio': int(invoice.get('folio', '0')), + 'fecha': invoice['fecha'], + 'fecha_timbrado': invoice['FechaTimbrado'], + 'forma_pago': invoice['formaDePago'], + 'condiciones_pago': invoice.get('CondicionesDePago', ''), + 'subtotal': float(invoice['SubTotal']), + 'descuento': float(invoice.get('Descuento', '0.0')), + 'moneda': currencies[invoice['Moneda']], + 'tipo_cambio': tipo_cambio, + 'total': total, + 'saldo': total, + 'total_mn': round(float(total * tipo_cambio), DECIMALES), + 'tipo_comprobante': invoice['TipoDeComprobante'], + 'metodo_pago': invoice['metodoDePago'], + 'lugar_expedicion': invoice['LugarExpedicion'], + # ~ 'uso_cfdi': invoice['UsoCFDI'], + 'total_retenciones': float(invoice.get('TotalImpuestosRetenidos', '0.0')), + 'total_trasladados': float(invoice.get('TotalImpuestosTrasladados', '0.0')), + 'xml': sxml, + 'uuid': invoice['uuid'], + 'estatus': 'Importada', + 'regimen_fiscal': regimenes[invoice['regimen_fiscal']], + 'pagada': False, + 'tipo_relacion': invoice.get('TipoRelacion', '') + } + # ~ donativo = BooleanField(default=False) + + conceptos = [] + for concepto in data['conceptos']: + valor_unitario = float(concepto['ValorUnitario']) + descuento = float(concepto.get('Descuento', '0.0')) + c = { + 'cantidad': float(concepto['Cantidad']), + 'valor_unitario': valor_unitario, + 'descuento': descuento, + 'precio_final': round(valor_unitario - descuento, DECIMALES), + 'importe': float(concepto['Importe']), + 'descripcion': concepto['Descripcion'], + 'unidad': concepto.get('Unidad', ''), + 'clave': concepto.get('NoIdentificacion', ''), + 'clave_sat': '01010101', + } + conceptos.append(c) + + new = { + 'invoice': invoice, + 'conceptos': conceptos, + } + return True, new + def _validate_import(self, data, sxml): try: emisor = Emisor.select()[0] @@ -4632,10 +4715,6 @@ class Facturas(BaseModel): return False, {} invoice = data['invoice'] - if invoice['version'] != '3.3': - msg = 'CFDI no es 3.3' - log.error(msg) - return False, {} w = (Facturas.uuid==invoice['uuid']) if Facturas.select().where(w).exists(): @@ -4643,6 +4722,14 @@ class Facturas(BaseModel): log.error(msg) return False, {} + if invoice['version'] != '3.3': + if invoice['version'] != '3.2': + msg = 'CFDI no soportado' + log.error(msg) + return False, {} + + return self._validate_import_32(self, data, sxml) + receptor = data['receptor'] name = receptor.get('nombre', '') tipo_persona = 1