diff --git a/fatture_ccsr.py b/fatture_ccsr.py index f041643..a62d581 100644 --- a/fatture_ccsr.py +++ b/fatture_ccsr.py @@ -2,6 +2,10 @@ import os import wx +import wx.adv +import requests +import requests_ntlm +import tempfile import downloader import traf2000_converter @@ -57,7 +61,7 @@ class LoginDialog(wx.Dialog): """constructor""" super(LoginDialog, self).__init__(parent, wx.ID_ANY, title, style=wx.DEFAULT_DIALOG_STYLE) - self.logged_id = False + self.logged_in = False user_sizer = wx.BoxSizer(wx.HORIZONTAL) user_lbl = wx.StaticText(self, wx.ID_ANY, "Username:") @@ -85,9 +89,93 @@ class LoginDialog(wx.Dialog): def on_login(self, _): """check credentials and login""" if self.username.GetValue() not in ("", None) and self.password.GetValue() not in ("", None): - self.logged_id = True + self.logged_in = True self.Close() +# class FilePickerDialog(wx.Dialog): +# """file picker dialog for downloaded ccsr files""" +# def __init__(self, parent, title): +# super(FilePickerDialog, self).__init__(parent, wx.ID_ANY, title, style=wx.DEFAULT_DIALOG_STYLE) + +# self.input_file_path = None +# self.input_file_ext = None +# self.output_file_path = None +# self.log_dialog = None + +# main_sizer = wx.BoxSizer(wx.VERTICAL) +# input_sizer = wx.BoxSizer(wx.HORIZONTAL) +# btn_sizer = wx.BoxSizer(wx.HORIZONTAL) + +# input_file_text = wx.StaticText(self, wx.ID_ANY, "Seleziona il file scaricato dal portale della CCSR") +# input_file_text.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)) + +# input_file_doc = wx.StaticText(self, wx.ID_ANY, "Per scaricare le fatture selezionare il file .xlsx mentre per convertire i record in TRAF2000 selezionare il file .xml o .csv") + +# self.input_file_picker = wx.FilePickerCtrl(self, 101, "", "Seleziona il .xlsx, .csv o .xml", "*.xlsx;*.csv;*.xml", style=wx.FLP_DEFAULT_STYLE) +# input_sizer.Add(self.input_file_picker, 1, wx.ALL|wx.EXPAND, 2) +# self.input_file_picker.Bind(wx.EVT_FILEPICKER_CHANGED, self.file_picker_changed) + +# self.download_btn = wx.Button(self, DOWNLOAD_ACTION, "Scarica Fatture") +# btn_sizer.Add(self.download_btn, 0, wx.ALL|wx.CENTER, 2) +# self.download_btn.Bind(wx.EVT_BUTTON, self.btn_onclick) +# self.download_btn.Disable() + +# self.traf2000_btn = wx.Button(self, CONVERT_ACTION, "Genera TRAF2000") +# btn_sizer.Add(self.traf2000_btn, 0, wx.ALL|wx.CENTER, 2) +# self.traf2000_btn.Bind(wx.EVT_BUTTON, self.btn_onclick) +# self.traf2000_btn.Disable() + +# main_sizer.Add(input_file_text, 0, wx.ALL|wx.CENTER, 2) +# main_sizer.Add(input_file_doc, 0, wx.ALL|wx.CENTER, 2) +# main_sizer.Add(input_sizer, 0, wx.ALL|wx.EXPAND, 2) +# main_sizer.Add(btn_sizer, 0, wx.ALL|wx.CENTER, 2) + +# self.SetSizerAndFit(main_sizer) + +# def file_picker_changed(self, event): +# """event raised when the input file path is changed""" +# self.input_file_path = event.GetEventObject().GetPath() +# 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]) +# except exc.NoFileExtensionError as handled_exception: +# print(handled_exception.args[0]) +# except exc.WrongFileExtensionError as handled_exception: +# print(handled_exception.args[0]) +# if self.input_file_ext == ".xlsx": +# self.download_btn.Enable() +# self.traf2000_btn.Disable() +# elif self.input_file_ext in (".csv", ".xml"): +# self.traf2000_btn.Enable() +# self.download_btn.Disable() +# else: +# self.download_btn.Disable() +# self.traf2000_btn.Disable() + +# def btn_onclick(self, event): +# """event raised when a button is clicked""" +# btn_id = event.GetEventObject().GetId() +# if btn_id == DOWNLOAD_ACTION: +# self.login_dlg.ShowModal() +# if self.login_dlg.logged_id: +# self.log_dialog = LogDialog(self, "Download delle fatture dal portale CCSR", DOWNLOAD_ACTION) +# self.log_dialog.Show() +# downloader.download_invoices(self) +# self.log_dialog.btn.Enable() +# elif btn_id == CONVERT_ACTION: +# self.log_dialog = LogDialog(self, "Conversione delle fatture in TRAF2000", CONVERT_ACTION) +# self.log_dialog.Show() +# 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]) +# self.log_dialog.btn.Enable() + class FattureCCSRFrame(wx.Frame): """main application frame""" def __init__(self, parent, title): @@ -97,21 +185,23 @@ class FattureCCSRFrame(wx.Frame): self.input_file_ext = None self.output_file_path = None self.log_dialog = None + self.session = None super(FattureCCSRFrame, self).__init__(parent, wx.ID_ANY, title, size=(650, 150)) panel = wx.Panel(self) main_sizer = wx.BoxSizer(wx.VERTICAL) - input_sizer = wx.BoxSizer(wx.HORIZONTAL) + date_sizer = wx.BoxSizer(wx.HORIZONTAL) btn_sizer = wx.BoxSizer(wx.HORIZONTAL) - input_file_text = wx.StaticText(panel, wx.ID_ANY, "Seleziona il file scaricato dal portale della CCSR") - input_file_text.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)) + title_text = wx.StaticText(panel, wx.ID_ANY, "Utility per scaricare le fatture dal portale CCSR e generare il tracciato TeamSystem TRAF2000") + title_text.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)) - input_file_doc = wx.StaticText(panel, wx.ID_ANY, "Per scaricare le fatture selezionare il file .xlsx mentre per convertire i record in TRAF2000 selezionare il file .xml o .csv") + desc_text = wx.StaticText(panel, wx.ID_ANY, "Seleziona le date di inizio e fine periodo delle fatture da gestire") - self.input_file_picker = wx.FilePickerCtrl(panel, 101, "", "Seleziona il .xlsx, .csv o .xml", "*.xlsx;*.csv;*.xml", style=wx.FLP_DEFAULT_STYLE) - input_sizer.Add(self.input_file_picker, 1, wx.ALL|wx.EXPAND, 2) - self.input_file_picker.Bind(wx.EVT_FILEPICKER_CHANGED, self.file_picker_changed) + self.start_date_picker = wx.adv.DatePickerCtrl(panel, dt=wx.DefaultDateTime) + self.end_date_picker = wx.adv.DatePickerCtrl(panel, dt=wx.DefaultDateTime) + date_sizer.Add(self.start_date_picker, 0, wx.ALL|wx.CENTER, 2) + date_sizer.Add(self.end_date_picker, 0, wx.ALL|wx.CENTER, 2) self.download_btn = wx.Button(panel, DOWNLOAD_ACTION, "Scarica Fatture") btn_sizer.Add(self.download_btn, 0, wx.ALL|wx.CENTER, 2) @@ -127,47 +217,61 @@ class FattureCCSRFrame(wx.Frame): self.output_traf2000_dialog = wx.FileDialog(panel, "Scegli dove salvare il file TRAF2000", defaultFile="TRAF2000", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) self.output_pdf_dialog = wx.FileDialog(panel, "Scegli dove salvare il .pdf con le fatture scaricate", defaultFile="fatture.pdf", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) - main_sizer.Add(input_file_text, 0, wx.ALL|wx.CENTER, 2) - main_sizer.Add(input_file_doc, 0, wx.ALL|wx.CENTER, 2) - main_sizer.Add(input_sizer, 0, wx.ALL|wx.EXPAND, 2) + main_sizer.Add(title_text, 0, wx.ALL|wx.CENTER, 2) + main_sizer.Add(desc_text, 0, wx.ALL|wx.CENTER, 2) + main_sizer.Add(date_sizer, 0, wx.ALL|wx.EXPAND, 2) main_sizer.Add(btn_sizer, 0, wx.ALL|wx.CENTER, 2) panel.SetSizer(main_sizer) self.Show() - def file_picker_changed(self, event): - """event raised when the input file path is changed""" - self.input_file_path = event.GetEventObject().GetPath() - #TODO: error frame - 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]) - except exc.NoFileExtensionError as handled_exception: - print(handled_exception.args[0]) - except exc.WrongFileExtensionError as handled_exception: - print(handled_exception.args[0]) - if self.input_file_ext == ".xlsx": + self.login_dlg.ShowModal() + if self.login_dlg.logged_in: self.download_btn.Enable() - self.traf2000_btn.Disable() - elif self.input_file_ext in (".csv", ".xml"): self.traf2000_btn.Enable() - self.download_btn.Disable() - else: - self.download_btn.Disable() - self.traf2000_btn.Disable() def btn_onclick(self, event): """event raised when a button is clicked""" btn_id = event.GetEventObject().GetId() + if not self.login_dlg.logged_in: + return None + + self.session = requests.Session() + self.session.auth = requests_ntlm.HttpNtlmAuth("sr\\"+self.login_dlg.username.GetValue(), self.login_dlg.password.GetValue()) + + 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='+('EXCELOPENXML' if btn_id == DOWNLOAD_ACTION else 'XML') + print(input_file_url) + downloaded_input_file = self.session.get(input_file_url) + if downloaded_input_file.status_code != 200: + #TODO: error + print(downloaded_input_file.status_code) + print(downloaded_input_file.content) + return + input_file_descriptor, self.input_file_path = tempfile.mkstemp(suffix=('.xlsx' if btn_id == DOWNLOAD_ACTION else '.xml')) + 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: - self.login_dlg.ShowModal() - if self.login_dlg.logged_id: - self.log_dialog = LogDialog(self, "Download delle fatture dal portale CCSR", DOWNLOAD_ACTION) - self.log_dialog.Show() - downloader.download_invoices(self) - self.log_dialog.btn.Enable() + self.log_dialog = LogDialog(self, "Download delle fatture dal portale CCSR", DOWNLOAD_ACTION) + self.log_dialog.Show() + downloader.download_invoices(self) + self.log_dialog.btn.Enable() + elif btn_id == CONVERT_ACTION: self.log_dialog = LogDialog(self, "Conversione delle fatture in TRAF2000", CONVERT_ACTION) self.log_dialog.Show()