From 1429d20951701adca15e99e85ae64565e7e368ba Mon Sep 17 00:00:00 2001 From: Ettore Dreucci Date: Sat, 9 Jan 2021 00:45:27 +0100 Subject: [PATCH] Added errors in log Signed-off-by: Ettore Dreucci --- fatture_ccsr/downloader.py | 62 ++++++++++++++++++++++------ fatture_ccsr/fatture_ccsr.py | 65 +++--------------------------- fatture_ccsr/traf2000_converter.py | 39 +++++++++++++++--- 3 files changed, 89 insertions(+), 77 deletions(-) diff --git a/fatture_ccsr/downloader.py b/fatture_ccsr/downloader.py index e91abd5..515ffcc 100644 --- a/fatture_ccsr/downloader.py +++ b/fatture_ccsr/downloader.py @@ -6,6 +6,24 @@ import openpyxl import PyPDF2 import wx +def download_input_file(parent): + """download input file""" + start_date = parent.start_date_picker.GetValue().Format("%d/%m/%Y") + end_date = parent.end_date_picker.GetValue().Format("%d/%m/%Y") + input_file_url = 'https://report.casadicurasanrossore.it:8443/reportserver?/STAT_FATTURATO_CTERZI&dataI='+start_date+'&dataF='+end_date+'&rs:Format=EXCELOPENXML' + downloaded_input_file = parent.session.get(input_file_url) + if downloaded_input_file.status_code != 200: + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("ERRORE: impossibile scaricare il file di input.\nControllare la connessione ad internet e l'operatività del portale CCSR. Code %d\n" % downloaded_input_file.status_code) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() + return + + input_file_descriptor, parent.input_file_path = tempfile.mkstemp(suffix='.xlsx') + parent.input_files.append(parent.input_file_path) + with open(input_file_descriptor, 'wb') as input_file: + input_file.write(downloaded_input_file.content) + def get_invoices_info(input_file_path: str) -> tuple: """extract invoices IDs and URLs from xlsx input file""" xlsx_file = openpyxl.load_workbook(input_file_path) @@ -33,6 +51,10 @@ def download_invoices(parent): """download invoices from CCSR""" output_file_path = None + parent.log_dialog.log_text.AppendText("Download file input\n") + wx.Yield() + download_input_file(parent) + invoices_info = get_invoices_info(parent.input_file_path) invoices = invoices_info[1] @@ -53,7 +75,9 @@ def download_invoices(parent): try: PyPDF2.PdfFileReader(open(invoice["path"], "rb")) except (PyPDF2.utils.PdfReadError, OSError): + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) parent.log_dialog.log_text.AppendText("Errore: fattura %s corrotta!\n" % invoice_id) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield() invoice["good"] = False else: @@ -62,25 +86,37 @@ def download_invoices(parent): wx.Yield() invoice["good"] = True else: + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) parent.log_dialog.log_text.AppendText("Errore: impossibile scaricare fattura %s: %d\n" % (invoice_id, resp.status_code)) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield() invoice["good"] = False + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.BLACK, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("Download terminato.") + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() + parent.output_pdf_dialog.SetFilename("fatture_%s.pdf" % invoices_info[0]) if parent.output_pdf_dialog.ShowModal() == wx.ID_OK: output_file_path = parent.output_pdf_dialog.GetPath() + merger = PyPDF2.PdfFileMerger() + for invoice in invoices.values(): + if invoice["good"]: + merger.append(PyPDF2.PdfFileReader(open(invoice["path"], "rb"))) + merger.write(output_file_path) + + shutil.rmtree(tmp_dir, ignore_errors=True) + + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.BLACK, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("Il pdf contenente le fatture si trova in %s\n" % output_file_path) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() + parent.log_dialog.open_file_btn.Enable() + else: - #TODO: avviso errore file output - return - - merger = PyPDF2.PdfFileMerger() - for invoice in invoices.values(): - if invoice["good"]: - merger.append(PyPDF2.PdfFileReader(open(invoice["path"], "rb"))) - merger.write(output_file_path) - - shutil.rmtree(tmp_dir, ignore_errors=True) - - parent.log_dialog.log_text.AppendText("Download terminato.\nIl pdf contenente le fatture si trova in %s\n" % output_file_path) - wx.Yield() + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("Non è stata eseguita l'unione delle fatture in un singolo pdf.\nLe singole fatture si trovano in %s\n" % tmp_dir) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() diff --git a/fatture_ccsr/fatture_ccsr.py b/fatture_ccsr/fatture_ccsr.py index 57f356a..5dd0b00 100644 --- a/fatture_ccsr/fatture_ccsr.py +++ b/fatture_ccsr/fatture_ccsr.py @@ -3,7 +3,6 @@ import os import sys import subprocess -import tempfile import atexit import wx import wx.adv @@ -12,24 +11,12 @@ import requests_ntlm import downloader import traf2000_converter -import exc LOGIN_ACTION = 0 LOGOUT_ACTION = 1 DOWNLOAD_ACTION = 10 CONVERT_ACTION = 20 -def file_extension(file_path: str, allowed_ext: set = None) -> str: - """Return the file extension if that's in the allowed extension set""" - if file_path in (None, ""): - raise exc.NoFileError() - file_ext = os.path.splitext(file_path)[1] - if file_ext in (None, ""): - raise exc.NoFileExtensionError - if allowed_ext is not None and file_ext not in allowed_ext: - raise exc.WrongFileExtensionError - return file_ext - class FattureCCSRFrame(wx.Frame): """main application frame""" def __init__(self, *args, **kwds): @@ -41,8 +28,6 @@ class FattureCCSRFrame(wx.Frame): self._initial_locale = wx.Locale(wx.LANGUAGE_DEFAULT, wx.LOCALE_LOAD_DEFAULT) - self.input_file_path = None - self.input_file_ext = None self.input_files = list() self.log_dialog = None self.session = requests.Session() @@ -133,66 +118,28 @@ class FattureCCSRFrame(wx.Frame): btn_id = event.GetEventObject().GetId() if btn_id not in (LOGIN_ACTION, LOGOUT_ACTION, DOWNLOAD_ACTION, CONVERT_ACTION): - #TODO: error - return + pass elif btn_id == LOGIN_ACTION: self.login_dlg.ShowModal() if self.login_dlg.logged_in: self.enable_on_login() - return + elif btn_id == LOGOUT_ACTION: self.disable_on_logout() - return + elif not self.login_dlg.logged_in: - #TODO: error - return + pass - start_date = self.start_date_picker.GetValue().Format("%d/%m/%Y") - end_date = self.end_date_picker.GetValue().Format("%d/%m/%Y") - input_file_url = 'https://report.casadicurasanrossore.it:8443/reportserver?/STAT_FATTURATO_CTERZI&dataI='+start_date+'&dataF='+end_date+'&rs:Format=' - input_file_url += ('EXCELOPENXML' if btn_id == DOWNLOAD_ACTION else 'XML' if btn_id == CONVERT_ACTION else None) - - downloaded_input_file = self.session.get(input_file_url) - if downloaded_input_file.status_code != 200: - #TODO: error - return - - input_file_descriptor, self.input_file_path = tempfile.mkstemp(suffix=('.xlsx' if btn_id == DOWNLOAD_ACTION else '.xml' if btn_id == CONVERT_ACTION else None)) - self.input_files.append(self.input_file_path) - with open(input_file_descriptor, 'wb') as input_file: - input_file.write(downloaded_input_file.content) - - try: - self.input_file_ext = file_extension(self.input_file_path, (".xml", ".csv", ".xlsx")) - except exc.NoFileError as handled_exception: - print(handled_exception.args[0]) - return - except exc.NoFileExtensionError as handled_exception: - print(handled_exception.args[0]) - return - except exc.WrongFileExtensionError as handled_exception: - print(handled_exception.args[0]) - return - - if btn_id == DOWNLOAD_ACTION: + elif btn_id == DOWNLOAD_ACTION: self.log_dialog = LogDialog(self, "Download delle fatture dal portale CCSR", DOWNLOAD_ACTION) self.log_dialog.Show() downloader.download_invoices(self) self.log_dialog.close_btn.Enable() - self.log_dialog.open_file_btn.Enable() elif btn_id == CONVERT_ACTION: self.log_dialog = LogDialog(self, "Conversione delle fatture in TRAF2000", CONVERT_ACTION) self.log_dialog.Show() - #TODO: error frame - try: - traf2000_converter.convert(self) - except exc.NoFileError as handled_exception: - print(handled_exception.args[0]) - except exc.NoFileExtensionError as handled_exception: - print(handled_exception.args[0]) - except exc.WrongFileExtensionError as handled_exception: - print(handled_exception.args[0]) + traf2000_converter.convert(self) self.log_dialog.close_btn.Enable() def exit_handler(self): diff --git a/fatture_ccsr/traf2000_converter.py b/fatture_ccsr/traf2000_converter.py index aaa2649..787f0bb 100755 --- a/fatture_ccsr/traf2000_converter.py +++ b/fatture_ccsr/traf2000_converter.py @@ -2,10 +2,29 @@ import datetime import csv +import tempfile import xml.etree.ElementTree import unidecode import wx +def download_input_file(parent): + """download input file""" + start_date = parent.start_date_picker.GetValue().Format("%d/%m/%Y") + end_date = parent.end_date_picker.GetValue().Format("%d/%m/%Y") + input_file_url = 'https://report.casadicurasanrossore.it:8443/reportserver?/STAT_FATTURATO_CTERZI&dataI='+start_date+'&dataF='+end_date+'&rs:Format=XML' + downloaded_input_file = parent.session.get(input_file_url) + if downloaded_input_file.status_code != 200: + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("ERRORE: impossibile scaricare il file di input.\nControllare la connessione ad internet e l'operatività del portale CCSR. Code %d\n" % downloaded_input_file.status_code) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() + return + + input_file_descriptor, parent.input_file_path = tempfile.mkstemp(suffix='.xml') + parent.input_files.append(parent.input_file_path) + with open(input_file_descriptor, 'wb') as input_file: + input_file.write(downloaded_input_file.content) + def import_csv(parent) -> dict: """Return a dict containing the invoices info""" invoices = dict() @@ -98,16 +117,19 @@ def convert(parent): """Output to a file the TRAF2000 records""" output_file_path = None - if parent.input_file_ext == ".csv": - invoices = import_csv(parent) + parent.log_dialog.log_text.AppendText("Download file input\n") + wx.Yield() + download_input_file(parent) - elif parent.input_file_ext == ".xml": - invoices = import_xml(parent) + invoices = import_xml(parent) if parent.output_traf2000_dialog.ShowModal() == wx.ID_OK: output_file_path = parent.output_traf2000_dialog.GetPath() else: - #TODO: avviso errore file output + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) + parent.log_dialog.log_text.AppendText("ERRORE: non è stato selezionato il file di output del tracciato.\n") + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) + wx.Yield() return with open(output_file_path, "w") as traf2000_file: @@ -116,12 +138,16 @@ def convert(parent): for invoice in invoices.values(): if invoice["tipoFattura"] != "Fattura" and invoice["tipoFattura"] != "Nota di credito": + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) parent.log_dialog.log_text.AppendText("Errore: il documento %s può essere FATTURA o NOTA DI CREDITO\n" % invoice["numFattura"]) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield() continue if len(invoice["cf"]) != 16 and len(invoice["cf"]) == 11: + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.RED, font=wx.Font(wx.FontInfo(8).Bold()))) parent.log_dialog.log_text.AppendText("Errore: il documento %s non ha cf/piva\n" % invoice["numFattura"]) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield() continue @@ -295,5 +321,8 @@ def convert(parent): traf2000_file.write(line) parent.log_dialog.log_text.AppendText("Convertita fattura n. %s\n" % invoice["numFattura"]) wx.Yield() + + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr(wx.BLACK, font=wx.Font(wx.FontInfo(8).Bold()))) parent.log_dialog.log_text.AppendText("Conversione terminata.\nTracciato TRAF2000 salvato in %s\n" % output_file_path) + parent.log_dialog.log_text.SetDefaultStyle(wx.TextAttr()) wx.Yield()