Created
July 15, 2025 10:48
-
-
Save huogerac/bd16929acf1d4b7411bd517e3189118e to your computer and use it in GitHub Desktop.
um rascunho da importacao
This file contains hidden or 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
| def _obter_xml_data(attachments): | |
| for attachment in attachments: | |
| if attachment["mimeType"].lower() == "application/xml" or ".xml" in attachment["filename"].lower(): | |
| return attachment["data"] | |
| return None | |
| def _obter_pdf_data_e_nome_arquivo(attachments): | |
| for attachment in attachments: | |
| if attachment["mimeType"].lower() == "application/pdf" or ".pdf" in attachment["filename"].lower(): | |
| return attachment["data"], attachment["filename"] | |
| return None, None | |
| def _obter_pedido_entrega(xml_data): | |
| xml_string = base64.b64decode(xml_file.data).decode("utf-8") | |
| nf_dict = notafiscal_service.obter_valores_principais(xml_string) | |
| return entregas_service.obter_pedido_entrega(nf_dict.get("pedido_id")) | |
| def processar_email_importacao(email_id): | |
| # 1) obtem o email importado salvo pela importacao | |
| email_importacao = EmailNotasImportadas.objects.get(id=email_id) | |
| if email_importacao.status != EmailNotasImportadas.STATUS_VALIDADO: | |
| raise ValueError("Email não validado.") | |
| pdf_data, filename = _obter_pdf_data_e_nome_arquivo(email_importacao.attachments) | |
| xml_data = _obter_xml_data(email_importacao.attachments) | |
| if not xml_data: | |
| raise ValueError("Arquivo XMLF não encontrado.") | |
| if not pdf_data: | |
| raise ValueError("Arquivo PDF não encontrado.") | |
| # 2) Localiza o pedido de entrega referente ao anexo xml | |
| pedido_entrega = _obter_pedido_entrega(xml_data) | |
| if not pedido_entrega: | |
| raise ValueError("Pedido de entrega não encontrado.") | |
| # 3) valida se o pedido eh correto, valor, se nao tem naotem nota fiscal ainda etcc | |
| # nf_dict = notafiscal_service.obter_valores_principais(xml_string) | |
| # ped_entrega = entregas_service.obter_pedido_entrega(nf_dict.get("pedido_id")) | |
| # 4) Salva PDF no pedido de entrega | |
| decoded_file = base64.b64decode(pdf_data) | |
| from django.core.files.base import ContentFile | |
| pdf_file = ContentFile(decoded_file, name=filename) | |
| pagamento = finaceiro_service.ped_entrega_salvar_nota_fiscal(ped_entrega, pdf_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment