Removing temp files at exit

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2021-01-06 23:30:33 +01:00
parent 96eb09949f
commit 0e85d8353b
3 changed files with 245 additions and 229 deletions

View File

@@ -42,6 +42,8 @@ def open_file(file_path):
def download_invoices(parent): def download_invoices(parent):
"""download invoices from CCSR""" """download invoices from CCSR"""
output_file_path = None
invoices_info = get_invoices_info(parent.input_file_path) invoices_info = get_invoices_info(parent.input_file_path)
invoices = invoices_info[1] invoices = invoices_info[1]
@@ -78,7 +80,7 @@ def download_invoices(parent):
parent.output_pdf_dialog.SetFilename("fatture_%s.pdf" % invoices_info[0]) parent.output_pdf_dialog.SetFilename("fatture_%s.pdf" % invoices_info[0])
if parent.output_pdf_dialog.ShowModal() == wx.ID_OK: if parent.output_pdf_dialog.ShowModal() == wx.ID_OK:
parent.output_file_path = parent.output_pdf_dialog.GetPath() output_file_path = parent.output_pdf_dialog.GetPath()
else: else:
#TODO: avviso errore file output #TODO: avviso errore file output
return return
@@ -87,11 +89,11 @@ def download_invoices(parent):
for invoice in invoices.values(): for invoice in invoices.values():
if invoice["good"]: if invoice["good"]:
merger.append(PyPDF2.PdfFileReader(open(invoice["path"], "rb"))) merger.append(PyPDF2.PdfFileReader(open(invoice["path"], "rb")))
merger.write(parent.output_file_path) merger.write(output_file_path)
open_file(parent.output_file_path) open_file(output_file_path)
shutil.rmtree(tmp_dir, ignore_errors=True) shutil.rmtree(tmp_dir, ignore_errors=True)
parent.log_dialog.log_text.AppendText("Download terminato. Il pdf contenente le fatture si trova in %s\n" % parent.output_file_path) parent.log_dialog.log_text.AppendText("Download terminato.\nIl pdf contenente le fatture si trova in %s\n" % output_file_path)
wx.Yield() wx.Yield()

View File

@@ -6,6 +6,7 @@ import wx
import wx.adv import wx.adv
import requests import requests
import requests_ntlm import requests_ntlm
import atexit
import downloader import downloader
import traf2000_converter import traf2000_converter
@@ -107,11 +108,13 @@ class LoginDialog(wx.Dialog):
class FattureCCSRFrame(wx.Frame): class FattureCCSRFrame(wx.Frame):
"""main application frame""" """main application frame"""
def __init__(self, parent, title): def __init__(self, parent, title):
atexit.register(self.exit_handler)
self._initial_locale = wx.Locale(wx.LANGUAGE_DEFAULT, wx.LOCALE_LOAD_DEFAULT) self._initial_locale = wx.Locale(wx.LANGUAGE_DEFAULT, wx.LOCALE_LOAD_DEFAULT)
self.input_file_path = None self.input_file_path = None
self.input_file_ext = None self.input_file_ext = None
self.output_file_path = None self.input_files = list()
self.log_dialog = None self.log_dialog = None
self.session = requests.Session() self.session = requests.Session()
@@ -195,16 +198,20 @@ class FattureCCSRFrame(wx.Frame):
"""event raised when a button is clicked""" """event raised when a button is clicked"""
btn_id = event.GetEventObject().GetId() btn_id = event.GetEventObject().GetId()
if btn_id == LOGIN_ACTION: if btn_id not in (LOGIN_ACTION, LOGOUT_ACTION, DOWNLOAD_ACTION, CONVERT_ACTION):
#TODO: error
return
elif btn_id == LOGIN_ACTION:
self.login_dlg.ShowModal() self.login_dlg.ShowModal()
if self.login_dlg.logged_in: if self.login_dlg.logged_in:
self.enable_on_login() self.enable_on_login()
return
elif btn_id == LOGOUT_ACTION: elif btn_id == LOGOUT_ACTION:
self.disable_on_logout() self.disable_on_logout()
else: return
if not self.login_dlg.logged_in: elif not self.login_dlg.logged_in:
#TODO: error #TODO: error
return None return
start_date = self.start_date_picker.GetValue().Format("%d/%m/%Y") start_date = self.start_date_picker.GetValue().Format("%d/%m/%Y")
end_date = self.end_date_picker.GetValue().Format("%d/%m/%Y") end_date = self.end_date_picker.GetValue().Format("%d/%m/%Y")
@@ -214,9 +221,10 @@ class FattureCCSRFrame(wx.Frame):
downloaded_input_file = self.session.get(input_file_url) downloaded_input_file = self.session.get(input_file_url)
if downloaded_input_file.status_code != 200: if downloaded_input_file.status_code != 200:
#TODO: error #TODO: error
return None 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)) 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: with open(input_file_descriptor, 'wb') as input_file:
input_file.write(downloaded_input_file.content) input_file.write(downloaded_input_file.content)
@@ -252,6 +260,11 @@ class FattureCCSRFrame(wx.Frame):
print(handled_exception.args[0]) print(handled_exception.args[0])
self.log_dialog.btn.Enable() self.log_dialog.btn.Enable()
def exit_handler(self):
"""clean the environment befor exiting"""
for input_file in self.input_files:
os.remove(input_file)
if __name__ == "__main__": if __name__ == "__main__":
app = wx.App() app = wx.App()
FattureCCSRFrame(None, "Utility FattureCCSR") FattureCCSRFrame(None, "Utility FattureCCSR")

View File

@@ -8,194 +8,195 @@ import wx
def import_csv(parent) -> dict: def import_csv(parent) -> dict:
"""Return a dict containing the invoices info""" """Return a dict containing the invoices info"""
fatture = dict() invoices = dict()
with open(parent.input_file_path, newline="") as csv_file: with open(parent.input_file_path, newline="") as csv_file:
lettore = csv.reader(csv_file, delimiter=",") csv_reader = csv.reader(csv_file, delimiter=",")
for _ in range(4): for _ in range(4):
next(lettore) next(csv_reader)
for linea in lettore: for line in csv_reader:
if len(linea) == 0: if len(line) == 0:
break break
num_fattura = linea[1] invoice_num = line[1]
tipo_fattura = linea[8] invoice_type = line[8]
importo = linea[15] amount = line[15]
segno = 1 sign = 1
if tipo_fattura == "Nota di credito" and '(' not in importo: if invoice_type == "Nota di credito" and '(' not in amount:
segno = -1 sign = -1
importo = int(linea[15].replace("", "").replace(",", "").replace(".", "").replace("(", "").replace(")", "")) * segno amount = int(line[15].replace("", "").replace(",", "").replace(".", "").replace("(", "").replace(")", "")) * sign
if num_fattura not in fatture: if invoice_num not in invoices:
fattura = { invoice = {
"numFattura": num_fattura, "numFattura": invoice_num,
"tipoFattura": tipo_fattura, "tipoFattura": invoice_type,
"rifFattura": linea[4], "rifFattura": line[4],
"dataFattura": linea[2].replace("/", ""), "dataFattura": line[2].replace("/", ""),
"ragioneSociale": unidecode.unidecode(linea[6] + " " + " ".join(linea[5].split()[0:2])), "ragioneSociale": unidecode.unidecode(line[6] + " " + " ".join(line[5].split()[0:2])),
"posDivide": str(len(linea[6]) + 1), "posDivide": str(len(line[6]) + 1),
"cf": linea[7], "cf": line[7],
"importoTotale": 0, "importoTotale": 0,
"ritenutaAcconto": 0, "ritenutaAcconto": 0,
"righe": dict() "righe": dict()
} }
fatture[num_fattura] = fattura invoices[invoice_num] = invoice
if linea[14] == "Ritenuta d'acconto": if line[14] == "Ritenuta d'acconto":
fatture[num_fattura]["ritenutaAcconto"] = importo invoices[invoice_num]["ritenutaAcconto"] = amount
else: else:
fatture[num_fattura]["importoTotale"] += importo invoices[invoice_num]["importoTotale"] += amount
fatture[num_fattura]["righe"][linea[14]] = importo invoices[invoice_num]["righe"][line[14]] = amount
parent.log_dialog.log_text.AppendText("Importata fattura n. %s\n" % num_fattura) parent.log_dialog.log_text.AppendText("Importata fattura n. %s\n" % invoice_num)
wx.Yield() wx.Yield()
return fatture return invoices
def import_xml(parent) -> dict: def import_xml(parent) -> dict:
"""Return a dict containing the invoices info""" """Return a dict containing the invoices info"""
fatture = dict() invoices = dict()
tree = xml.etree.ElementTree.parse(parent.input_file_path) tree = xml.etree.ElementTree.parse(parent.input_file_path)
root = tree.getroot() root = tree.getroot()
for fattura in root.iter('{STAT_FATTURATO_CTERZI}Dettagli'): for invoice in root.iter('{STAT_FATTURATO_CTERZI}Dettagli'):
righe = dict() lines = dict()
num_fattura = fattura.get('protocollo_fatturatestata') invoice_num = invoice.get('protocollo_fatturatestata')
tipo_fattura = fattura.get('fat_ndc') invoice_type = invoice.get('fat_ndc')
importo_totale = 0 total_amount = 0
ritenuta_acconto = 0 ritenuta_acconto = 0
for riga in fattura.iter('{STAT_FATTURATO_CTERZI}Dettagli2'): for line in invoice.iter('{STAT_FATTURATO_CTERZI}Dettagli2'):
desc = riga.get('descrizione_fatturariga1') desc = line.get('descrizione_fatturariga1')
segno = 1 sign = 1
if tipo_fattura == 'Nota di credito' and '-' not in riga.get('prezzounitario_fatturariga1'): if invoice_type == 'Nota di credito' and '-' not in line.get('prezzounitario_fatturariga1'):
segno = -1 sign = -1
importo = int(format(round(float(riga.get('prezzounitario_fatturariga1')), 2), '.2f').replace('.', '').replace('-', '')) * segno amount = int(format(round(float(line.get('prezzounitario_fatturariga1')), 2), '.2f').replace('.', '').replace('-', '')) * sign
if desc == "Ritenuta d'acconto": if desc == "Ritenuta d'acconto":
ritenuta_acconto = importo ritenuta_acconto = amount
else: else:
righe[desc] = importo lines[desc] = amount
importo_totale += importo total_amount += amount
fattura_elem = { invoice_elem = {
"numFattura": num_fattura, "numFattura": invoice_num,
"tipoFattura": tipo_fattura, "tipoFattura": invoice_type,
"rifFattura": fattura.get('protocollo_fatturatestata1'), "rifFattura": invoice.get('protocollo_fatturatestata1'),
"dataFattura": datetime.datetime.fromisoformat(fattura.get('data_fatturatestata')).strftime("%d%m%Y"), "dataFattura": datetime.datetime.fromisoformat(invoice.get('data_fatturatestata')).strftime("%d%m%Y"),
"ragioneSociale": unidecode.unidecode(fattura.get('cognome_cliente') + ' ' + ' '.join(fattura.get('nome_cliente').split()[0:2])), "ragioneSociale": unidecode.unidecode(invoice.get('cognome_cliente') + ' ' + ' '.join(invoice.get('nome_cliente').split()[0:2])),
"posDivide": str(len(fattura.get('cognome_cliente')) + 1), "posDivide": str(len(invoice.get('cognome_cliente')) + 1),
"cf": fattura.get('cf_piva_cliente'), "cf": invoice.get('cf_piva_cliente'),
"importoTotale": importo_totale, "importoTotale": total_amount,
"ritenutaAcconto": ritenuta_acconto, "ritenutaAcconto": ritenuta_acconto,
"righe": righe, "righe": lines,
} }
fatture[num_fattura] = fattura_elem invoices[invoice_num] = invoice_elem
parent.log_dialog.log_text.AppendText("Importata fattura n. %s\n" % num_fattura) parent.log_dialog.log_text.AppendText("Importata fattura n. %s\n" % invoice_num)
wx.Yield() wx.Yield()
return fatture return invoices
def convert(parent): def convert(parent):
"""Output to a file the TRAF2000 records""" """Output to a file the TRAF2000 records"""
output_file_path = None
if parent.input_file_ext == ".csv": if parent.input_file_ext == ".csv":
fatture = import_csv(parent) invoices = import_csv(parent)
elif parent.input_file_ext == ".xml": elif parent.input_file_ext == ".xml":
fatture = import_xml(parent) invoices = import_xml(parent)
if parent.output_traf2000_dialog.ShowModal() == wx.ID_OK: if parent.output_traf2000_dialog.ShowModal() == wx.ID_OK:
parent.output_file_path = parent.output_traf2000_dialog.GetPath() output_file_path = parent.output_traf2000_dialog.GetPath()
else: else:
#TODO: avviso errore file output #TODO: avviso errore file output
return return
with open(parent.output_file_path, "w") as traf2000_file: with open(output_file_path, "w") as traf2000_file:
parent.log_dialog.nc_text.AppendText("Note di credito:\n") parent.log_dialog.nc_text.AppendText("Note di credito:\n")
wx.Yield() wx.Yield()
for fattura in fatture.values(): for invoice in invoices.values():
if fattura["tipoFattura"] != "Fattura" and fattura["tipoFattura"] != "Nota di credito": if invoice["tipoFattura"] != "Fattura" and invoice["tipoFattura"] != "Nota di credito":
parent.log_dialog.log_text.AppendText("Errore: il documento %s può essere FATTURA o NOTA DI CREDITO\n" % fattura["numFattura"]) parent.log_dialog.log_text.AppendText("Errore: il documento %s può essere FATTURA o NOTA DI CREDITO\n" % invoice["numFattura"])
wx.Yield() wx.Yield()
continue continue
if len(fattura["cf"]) != 16 and len(fattura["cf"]) == 11: if len(invoice["cf"]) != 16 and len(invoice["cf"]) == 11:
parent.log_dialog.log_text.AppendText("Errore: il documento %s non ha cf/piva\n" % fattura["numFattura"]) parent.log_dialog.log_text.AppendText("Errore: il documento %s non ha cf/piva\n" % invoice["numFattura"])
wx.Yield() wx.Yield()
continue continue
if fattura["tipoFattura"] == "Nota di credito": if invoice["tipoFattura"] == "Nota di credito":
# As for now this script doesn't handle "Note di credito" # As for now this script doesn't handle "Note di credito"
parent.log_dialog.nc_text.AppendText(fattura["numFattura"]+"\n") parent.log_dialog.nc_text.AppendText(invoice["numFattura"]+"\n")
wx.Yield() wx.Yield()
continue continue
linea = ["04103", "3", "0", "00000"] # TRF-DITTA + TRF-VERSIONE + TRF-TARC + TRF-COD-CLIFOR line = ["04103", "3", "0", "00000"] # TRF-DITTA + TRF-VERSIONE + TRF-TARC + TRF-COD-CLIFOR
linea.append(fattura["ragioneSociale"][:32]+' '*(32-len(fattura["ragioneSociale"]))) # TRF-RASO line.append(invoice["ragioneSociale"][:32]+' '*(32-len(invoice["ragioneSociale"]))) # TRF-RASO
linea.append(' '*30) # TRF-IND line.append(' '*30) # TRF-IND
linea.append('00000') # TRF-CAP line.append('00000') # TRF-CAP
linea.append(' '*27) # TRF-CITTA + TRF-PROV line.append(' '*27) # TRF-CITTA + TRF-PROV
if len(fattura["cf"]) == 16: # se c.f. presente if len(invoice["cf"]) == 16: # se c.f. presente
linea.append(fattura["cf"]) # TRF-COFI line.append(invoice["cf"]) # TRF-COFI
linea.append('0'*11) # TRF-PIVA line.append('0'*11) # TRF-PIVA
linea.append('S') # TRF-PF line.append('S') # TRF-PF
else: # se piva presente else: # se piva presente
linea.append(' '*16) # TRF-COFI line.append(' '*16) # TRF-COFI
linea.append(fattura["cf"]) # TRF-PIVA line.append(invoice["cf"]) # TRF-PIVA
linea.append('N') # TRF-PF line.append('N') # TRF-PF
linea.append('0'*(2-len(fattura["posDivide"])) + fattura["posDivide"]) # TRF-DIVIDE line.append('0'*(2-len(invoice["posDivide"])) + invoice["posDivide"]) # TRF-DIVIDE
linea.append('0000') # TRF-PAESE line.append('0000') # TRF-PAESE
linea.append(' '*33) # TRF-PIVA-ESTERO + TRF-COFI-ESTERO + TRF-SESSO line.append(' '*33) # TRF-PIVA-ESTERO + TRF-COFI-ESTERO + TRF-SESSO
linea.append('0'*8) # TRF-DTNAS line.append('0'*8) # TRF-DTNAS
linea.append(' '*64) # TRF-COMNA + TRF-PRVNA + TRF-PREF + TRF-NTELE-NUM + TRF-FAX-PREF + TRF-FAX-NUM line.append(' '*64) # TRF-COMNA + TRF-PRVNA + TRF-PREF + TRF-NTELE-NUM + TRF-FAX-PREF + TRF-FAX-NUM
linea.append('0'*22) # TRF-CFCONTO + TRF-CFCODPAG + TRF-CFBANCA + TRF-CFAGENZIA + TRF-CFINTERM line.append('0'*22) # TRF-CFCONTO + TRF-CFCODPAG + TRF-CFBANCA + TRF-CFAGENZIA + TRF-CFINTERM
if fattura["tipoFattura"] == "Fattura": if invoice["tipoFattura"] == "Fattura":
linea.append('001') # TRF-CAUSALE line.append('001') # TRF-CAUSALE
linea.append("FATTURA VENDITA") # TRF-CAU-DES line.append("FATTURA VENDITA") # TRF-CAU-DES
else: else:
linea.append('002') # TRF-CAUSALE line.append('002') # TRF-CAUSALE
linea.append("N.C. A CLIENTE ") # TRF-CAU-DES line.append("N.C. A CLIENTE ") # TRF-CAU-DES
linea.append(' '*86) # TRF-CAU-AGG + TRF-CAU-AGG-1 + TRF-CAU-AGG-2 line.append(' '*86) # TRF-CAU-AGG + TRF-CAU-AGG-1 + TRF-CAU-AGG-2
linea.append(fattura["dataFattura"]*2) # TRF-DATA-REGISTRAZIONE + TRF-DATA-DOC line.append(invoice["dataFattura"]*2) # TRF-DATA-REGISTRAZIONE + TRF-DATA-DOC
linea.append('00000000') # TRF-NUM-DOC-FOR line.append('00000000') # TRF-NUM-DOC-FOR
linea.append(fattura["numFattura"][4:9]) # TRF-NDOC line.append(invoice["numFattura"][4:9]) # TRF-NDOC
linea.append('00') # TRF-SERIE line.append('00') # TRF-SERIE
linea.append('0'*72) # TRF-EC-PARTITA + TRF-EC-PARTITA-ANNO + TRF-EC-COD-VAL + TRF-EC-CAMBIO + TRF-EC-DATA-CAMBIO + TRF-EC-TOT-DOC-VAL + TRF-EC-TOT-IVA-VAL + TRF-PLAFOND line.append('0'*72) # TRF-EC-PARTITA + TRF-EC-PARTITA-ANNO + TRF-EC-COD-VAL + TRF-EC-CAMBIO + TRF-EC-DATA-CAMBIO + TRF-EC-TOT-DOC-VAL + TRF-EC-TOT-IVA-VAL + TRF-PLAFOND
conta = 0 count = 0
for desc, imponibile in fattura["righe"].items(): for desc, imponibile in invoice["righe"].items():
conta += 1 count += 1
imponibile = str(imponibile) imponibile = str(imponibile)
if '-' in imponibile: if '-' in imponibile:
imponibile.replace('-', '') imponibile.replace('-', '')
imponibile = '0'*(11-len(imponibile)) + imponibile + "-" imponibile = '0'*(11-len(imponibile)) + imponibile + "-"
else: else:
imponibile = '0'*(11-len(imponibile)) + imponibile + "+" imponibile = '0'*(11-len(imponibile)) + imponibile + "+"
linea.append(imponibile) # TRF-IMPONIB line.append(imponibile) # TRF-IMPONIB
if desc != "Bollo": if desc != "Bollo":
linea.append('308') # TRF-ALIQ line.append('308') # TRF-ALIQ
else: else:
linea.append('315') # TRF-ALIQ line.append('315') # TRF-ALIQ
linea.append('0'*16) # TRF-ALIQ-AGRICOLA + TRF-IVA11 + TRF-IMPOSTA line.append('0'*16) # TRF-ALIQ-AGRICOLA + TRF-IVA11 + TRF-IMPOSTA
for _ in range(8-conta): for _ in range(8-count):
linea.append('0'*31) line.append('0'*31)
totale = str(fattura["importoTotale"]) total = str(invoice["importoTotale"])
if '-' in totale: if '-' in total:
totale.replace('-', '') total.replace('-', '')
totale = '0'*(11-len(totale)) + totale + "-" total = '0'*(11-len(total)) + total + "-"
else: else:
totale = '0'*(11-len(totale)) + totale + "+" total = '0'*(11-len(total)) + total + "+"
linea.append(totale) # TRF-TOT-FAT line.append(total) # TRF-TOT-FAT
conta = 0 count = 0
for desc, imponibile in fattura["righe"].items(): for desc, imponibile in invoice["righe"].items():
conta += 1 count += 1
imponibile = str(imponibile) imponibile = str(imponibile)
if '-' in imponibile: if '-' in imponibile:
imponibile.replace('-', '') imponibile.replace('-', '')
@@ -203,96 +204,96 @@ def convert(parent):
else: else:
imponibile = '0'*(11-len(imponibile)) + imponibile + "+" imponibile = '0'*(11-len(imponibile)) + imponibile + "+"
if desc != "Bollo": if desc != "Bollo":
linea.append('4004300') # TRF-CONTO-RIC line.append('4004300') # TRF-CONTO-RIC
else: else:
linea.append('4004500') # TRF-CONTO-RIC line.append('4004500') # TRF-CONTO-RIC
linea.append(imponibile) # TRF-IMP-RIC line.append(imponibile) # TRF-IMP-RIC
for _ in range(8-conta): for _ in range(8-count):
linea.append('0'*19) line.append('0'*19)
linea.append('000') # TRF-CAU-PAG line.append('000') # TRF-CAU-PAG
linea.append(' '*83) # TRF-CAU-DES-PAGAM + TRF-CAU-AGG-1-PAGAM + TRF-CAU-AGG-2-PAGAM line.append(' '*83) # TRF-CAU-DES-PAGAM + TRF-CAU-AGG-1-PAGAM + TRF-CAU-AGG-2-PAGAM
linea.append(('0000000' + ' ' + '0'*12 + ' '*18 + '0'*26)*80) # TRF-CONTO + TRF-DA + TRF-IMPORTO + TRF-CAU-AGGIUNT + TRF-EC-PARTITA-PAG + TRF-EC-PARTITA-ANNO-PAG + TRF-EC-IMP-VAL line.append(('0000000' + ' ' + '0'*12 + ' '*18 + '0'*26)*80) # TRF-CONTO + TRF-DA + TRF-IMPORTO + TRF-CAU-AGGIUNT + TRF-EC-PARTITA-PAG + TRF-EC-PARTITA-ANNO-PAG + TRF-EC-IMP-VAL
linea.append((' ' + '0'*18)*10) # TRF-RIFER-TAB + TRF-IND-RIGA + TRF-DT-INI + TRF-DT-FIN line.append((' ' + '0'*18)*10) # TRF-RIFER-TAB + TRF-IND-RIGA + TRF-DT-INI + TRF-DT-FIN
linea.append('000000') # TRF-DOC6 line.append('000000') # TRF-DOC6
linea.append('N' + '0') # TRF-AN-OMONIMI + TRF-AN-TIPO-SOGG line.append('N' + '0') # TRF-AN-OMONIMI + TRF-AN-TIPO-SOGG
linea.append('00'*80) # TRF-EC-PARTITA-SEZ-PAG line.append('00'*80) # TRF-EC-PARTITA-SEZ-PAG
linea.append('0'*15) # TRF-NUM-DOC-PAG-PROF + TRF-DATA-DOC-PAG-PROF line.append('0'*15) # TRF-NUM-DOC-PAG-PROF + TRF-DATA-DOC-PAG-PROF
if fattura["ritenutaAcconto"] != 0: if invoice["ritenutaAcconto"] != 0:
imponibile = str(fattura["ritenutaAcconto"]) imponibile = str(invoice["ritenutaAcconto"])
imponibile = '0'*(11-len(imponibile)) + imponibile + "+" imponibile = '0'*(11-len(imponibile)) + imponibile + "+"
linea.append(imponibile) # TRF-RIT-ACC line.append(imponibile) # TRF-RIT-ACC
else: else:
linea.append('0'*12) # TRF-RIT-ACC line.append('0'*12) # TRF-RIT-ACC
linea.append('0'*60) # TRF-RIT-PREV + TRF-RIT-1 + TRF-RIT-2 + TRF-RIT-3 + TRF-RIT-4 line.append('0'*60) # TRF-RIT-PREV + TRF-RIT-1 + TRF-RIT-2 + TRF-RIT-3 + TRF-RIT-4
linea.append('00'*8) # TRF-UNITA-RICAVI line.append('00'*8) # TRF-UNITA-RICAVI
linea.append('00'*80) # TRF-UNITA-PAGAM line.append('00'*80) # TRF-UNITA-PAGAM
linea.append(' '*24) # TRF-FAX-PREF-1 + TRF-FAX-NUM-1 line.append(' '*24) # TRF-FAX-PREF-1 + TRF-FAX-NUM-1
linea.append(' ' + ' ') # TRF-SOLO-CLIFOR + TRF-80-SEGUENTE line.append(' ' + ' ') # TRF-SOLO-CLIFOR + TRF-80-SEGUENTE
linea.append('0000000') # TRF-CONTO-RIT-ACC line.append('0000000') # TRF-CONTO-RIT-ACC
linea.append('0'*35) # TRF-CONTO-RIT-PREV + TRF-CONTO-RIT-1 + TRF-CONTO-RIT-2 + TRF-CONTO-RIT-3 + TRF-CONTO-RIT-4 line.append('0'*35) # TRF-CONTO-RIT-PREV + TRF-CONTO-RIT-1 + TRF-CONTO-RIT-2 + TRF-CONTO-RIT-3 + TRF-CONTO-RIT-4
linea.append('N' + 'N' + '00000000' + '000') # TRF-DIFFERIMENTO-IVA + TRF-STORICO + TRF-STORICO-DATA + TRF-CAUS-ORI line.append('N' + 'N' + '00000000' + '000') # TRF-DIFFERIMENTO-IVA + TRF-STORICO + TRF-STORICO-DATA + TRF-CAUS-ORI
linea.append(' ' + ' ' + '0'*16 + ' ') # TRF-PREV-TIPOMOV + TRF-PREV-RATRIS + TRF-PREV-DTCOMP-INI + TRF-PREV-DTCOMP-FIN + TRF-PREV-FLAG-CONT line.append(' ' + ' ' + '0'*16 + ' ') # TRF-PREV-TIPOMOV + TRF-PREV-RATRIS + TRF-PREV-DTCOMP-INI + TRF-PREV-DTCOMP-FIN + TRF-PREV-FLAG-CONT
linea.append(' '*20 + '0'*21 + ' '*44 + '0'*8 + ' ' + '0'*6 + ' ' + '00' + ' ') # TRF-RIFERIMENTO + TRF-CAUS-PREST-ANA + TRF-EC-TIPO-PAGA + TRF-CONTO-IVA-VEN-ACQ + TRF-PIVA-VECCHIA + TRF-PIVA-ESTERO-VECCHIA + # TRF-RISERVATO + TRF-DATA-IVA-AGVIAGGI + TRF-DATI-AGG-ANA-REC4 + TRF-RIF-IVA-NOTE-CRED + TRF-RIF-IVA-ANNO-PREC + TRF-NATURA-GIURIDICA + TRF-STAMPA-ELENCO line.append(' '*20 + '0'*21 + ' '*44 + '0'*8 + ' ' + '0'*6 + ' ' + '00' + ' ') # TRF-RIFERIMENTO + TRF-CAUS-PREST-ANA + TRF-EC-TIPO-PAGA + TRF-CONTO-IVA-VEN-ACQ + TRF-PIVA-VECCHIA + TRF-PIVA-ESTERO-VECCHIA + # TRF-RISERVATO + TRF-DATA-IVA-AGVIAGGI + TRF-DATI-AGG-ANA-REC4 + TRF-RIF-IVA-NOTE-CRED + TRF-RIF-IVA-ANNO-PREC + TRF-NATURA-GIURIDICA + TRF-STAMPA-ELENCO
linea.append('000'*8 + ' '*20 + '0' + ' '*4 + '0'*6 + ' '*20 + 'S' + 'N') # TRF-PERC-FORF + TRF-SOLO-MOV-IVA + TRF-COFI-VECCHIO + TRF-USA-PIVA-VECCHIA + TRF-USA-PIVA-EST-VECCHIA + TRF-USA-COFI-VECCHIO + TRF-ESIGIBILITA-IVA + TRF-TIPO-MOV-RISCONTI + TRF-AGGIORNA-EC + TRF-BLACKLIST-ANAG + TRF-BLACKLIST-IVA-ANNO + TRF-CONTEA-ESTERO + TRF-ART21-ANAG + TRF-ART21-IVA line.append('000'*8 + ' '*20 + '0' + ' '*4 + '0'*6 + ' '*20 + 'S' + 'N') # TRF-PERC-FORF + TRF-SOLO-MOV-IVA + TRF-COFI-VECCHIO + TRF-USA-PIVA-VECCHIA + TRF-USA-PIVA-EST-VECCHIA + TRF-USA-COFI-VECCHIO + TRF-ESIGIBILITA-IVA + TRF-TIPO-MOV-RISCONTI + TRF-AGGIORNA-EC + TRF-BLACKLIST-ANAG + TRF-BLACKLIST-IVA-ANNO + TRF-CONTEA-ESTERO + TRF-ART21-ANAG + TRF-ART21-IVA
if fattura["tipoFattura"] == "Fattura": if invoice["tipoFattura"] == "Fattura":
linea.append('N') # TRF-RIF-FATTURA line.append('N') # TRF-RIF-FATTURA
else: else:
linea.append('S') # TRF-RIF-FATTURA line.append('S') # TRF-RIF-FATTURA
linea.append('S' + ' '*2 + 'S' + ' '*2) # TRF-RISERVATO-B + TRF-MASTRO-CF + TRF-MOV-PRIVATO + TRF-SPESE-MEDICHE + TRF-FILLER line.append('S' + ' '*2 + 'S' + ' '*2) # TRF-RISERVATO-B + TRF-MASTRO-CF + TRF-MOV-PRIVATO + TRF-SPESE-MEDICHE + TRF-FILLER
linea.append('\n') line.append('\n')
parent.log_dialog.log_text.AppendText("Creato record #0 per fattura n. %s\n" % fattura["numFattura"]) parent.log_dialog.log_text.AppendText("Creato record #0 per fattura n. %s\n" % invoice["numFattura"])
wx.Yield() wx.Yield()
#RECORD 5 per Tessera Sanitaria #RECORD 5 per Tessera Sanitaria
linea.append('04103' + '3' + '5') # TRF5-DITTA + TRF5-VERSIONE + TRF5-TARC line.append('04103' + '3' + '5') # TRF5-DITTA + TRF5-VERSIONE + TRF5-TARC
linea.append(' '*1200) # TRF-ART21-CONTRATTO line.append(' '*1200) # TRF-ART21-CONTRATTO
linea.append('0'*6 + fattura["cf"]) # TRF-A21CO-ANAG + # TRF-A21CO-COFI line.append('0'*6 + invoice["cf"]) # TRF-A21CO-ANAG + # TRF-A21CO-COFI
totale = str(fattura["importoTotale"]) total = str(invoice["importoTotale"])
totale = '0'*(13-len(totale)) + totale + "+" total = '0'*(13-len(total)) + total + "+"
linea.append(fattura["dataFattura"] + 'S' + '000' + totale + '0'*14 + '0' + fattura["numFattura"][4:9] + '00' + ' '*40) # TRF-A21CO-DATA + TRF-A21CO-FLAG + TRF-A21CO-ALQ + TRF-A21CO-IMPORTO + TRF-A21CO-IMPOSTA + TRF-A21CO-NDOC + TRF-A21CO-CONTRATTO line.append(invoice["dataFattura"] + 'S' + '000' + total + '0'*14 + '0' + invoice["numFattura"][4:9] + '00' + ' '*40) # TRF-A21CO-DATA + TRF-A21CO-FLAG + TRF-A21CO-ALQ + TRF-A21CO-IMPORTO + TRF-A21CO-IMPOSTA + TRF-A21CO-NDOC + TRF-A21CO-CONTRATTO
linea.append(('0'*6 + ' '*16 + '0'*8 + ' ' + '000' + '0'*14 + '0'*14 + '0'*8 + ' '*40)*49) # TRF-A21CO-DATA + TRF-A21CO-FLAG + TRF-A21CO-ALQ + TRF-A21CO-IMPORTO + TRF-A21CO-IMPOSTA + TRF-A21CO-NDOC + TRF-A21CO-CONTRATTO line.append(('0'*6 + ' '*16 + '0'*8 + ' ' + '000' + '0'*14 + '0'*14 + '0'*8 + ' '*40)*49) # TRF-A21CO-DATA + TRF-A21CO-FLAG + TRF-A21CO-ALQ + TRF-A21CO-IMPORTO + TRF-A21CO-IMPOSTA + TRF-A21CO-NDOC + TRF-A21CO-CONTRATTO
if fattura["tipoFattura"] == "Nota di credito": if invoice["tipoFattura"] == "Nota di credito":
linea.append('000' + fattura["rifFattura"][4:9]) # TRF-RIF-FATT-NDOC line.append('000' + invoice["rifFattura"][4:9]) # TRF-RIF-FATT-NDOC
linea.append('0'*8) # TRF-RIF-FATT-DDOC line.append('0'*8) # TRF-RIF-FATT-DDOC
else: else:
linea.append('0'*16) # TRF-RIF-FATT-NDOC + TRF-RIF-FATT-DDOC line.append('0'*16) # TRF-RIF-FATT-NDOC + TRF-RIF-FATT-DDOC
linea.append('F' + 'SR' + '2') # TRF-A21CO-TIPO + TRF-A21CO-TIPO-SPESA + TRF-A21CO-FLAG-SPESA line.append('F' + 'SR' + '2') # TRF-A21CO-TIPO + TRF-A21CO-TIPO-SPESA + TRF-A21CO-FLAG-SPESA
linea.append((' ' + ' ' + ' ')*49) # TRF-A21CO-TIPO + TRF-A21CO-TIPO-SPESA + TRF-A21CO-FLAG-SPESA line.append((' ' + ' ' + ' ')*49) # TRF-A21CO-TIPO + TRF-A21CO-TIPO-SPESA + TRF-A21CO-FLAG-SPESA
linea.append(' ' + 'S' + ' '*76) # TRF-SPESE-FUNEBRI + TRF-A21CO-PAGAM + FILLER + FILLER line.append(' ' + 'S' + ' '*76) # TRF-SPESE-FUNEBRI + TRF-A21CO-PAGAM + FILLER + FILLER
linea.append('\n') line.append('\n')
parent.log_dialog.log_text.AppendText("Creato record #5 per fattura n. %s\n" % fattura["numFattura"]) parent.log_dialog.log_text.AppendText("Creato record #5 per fattura n. %s\n" % invoice["numFattura"])
wx.Yield() wx.Yield()
#RECORD 1 per num. doc. originale #RECORD 1 per num. doc. originale
linea.append('04103' + '3' + '1') # TRF1-DITTA + TRF1-VERSIONE + TRF1-TARC line.append('04103' + '3' + '1') # TRF1-DITTA + TRF1-VERSIONE + TRF1-TARC
linea.append('0'*7 + ' '*3 + '0'*14) # TRF-NUM-AUTOFATT + TRF-SERIE-AUTOFATT + TRF-COD-VAL + TRF-TOTVAL line.append('0'*7 + ' '*3 + '0'*14) # TRF-NUM-AUTOFATT + TRF-SERIE-AUTOFATT + TRF-COD-VAL + TRF-TOTVAL
linea.append((' '*8 + '0'*24 + ' ' + '0'*36 + ' '*2 + '0'*9 + ' '*5)*20) # TRF-NOMENCLATURA + TRF-IMP-LIRE + TRF-IMP-VAL + TRF-NATURA + TRF-MASSA + TRF-UN-SUPPL + TRF-VAL-STAT + TRF-REGIME + TRF-TRASPORTO + TRF-PAESE-PROV + TRF-PAESE-ORIG + TRF-PAESE-DEST + TRF-PROV-DEST + TRF-PROV-ORIG + TRF-SEGNO-RET line.append((' '*8 + '0'*24 + ' ' + '0'*36 + ' '*2 + '0'*9 + ' '*5)*20) # TRF-NOMENCLATURA + TRF-IMP-LIRE + TRF-IMP-VAL + TRF-NATURA + TRF-MASSA + TRF-UN-SUPPL + TRF-VAL-STAT + TRF-REGIME + TRF-TRASPORTO + TRF-PAESE-PROV + TRF-PAESE-ORIG + TRF-PAESE-DEST + TRF-PROV-DEST + TRF-PROV-ORIG + TRF-SEGNO-RET
linea.append(' ' + '0'*6 + ' '*173) # TRF-INTRA-TIPO + TRF-MESE-ANNO-RIF + SPAZIO line.append(' ' + '0'*6 + ' '*173) # TRF-INTRA-TIPO + TRF-MESE-ANNO-RIF + SPAZIO
linea.append('0'*45 + ' '*4 + '0'*20 + ' '*28 + '0'*25) # TRF-RITA-TIPO + TRF-RITA-IMPON + TRF-RITA-ALIQ + TRF-RITA-IMPRA + TRF-RITA-PRONS + TRF-RITA-MESE + TRF-RITA-CAUSA + TRF-RITA-TRIBU + TRF-RITA-DTVERS + TRF-RITA-IMPAG + TRF-RITA-TPAG + TRF-RITA-SERIE + TRF-RITA-QUIETANZA + TRF-RITA-NUM-BOLL + TRF-RITA-ABI + TRF-RITA-CAB + TRF-RITA-AACOMP + TRF-RITA-CRED line.append('0'*45 + ' '*4 + '0'*20 + ' '*28 + '0'*25) # TRF-RITA-TIPO + TRF-RITA-IMPON + TRF-RITA-ALIQ + TRF-RITA-IMPRA + TRF-RITA-PRONS + TRF-RITA-MESE + TRF-RITA-CAUSA + TRF-RITA-TRIBU + TRF-RITA-DTVERS + TRF-RITA-IMPAG + TRF-RITA-TPAG + TRF-RITA-SERIE + TRF-RITA-QUIETANZA + TRF-RITA-NUM-BOLL + TRF-RITA-ABI + TRF-RITA-CAB + TRF-RITA-AACOMP + TRF-RITA-CRED
linea.append(' ' + '0'*44 + ' '*11 + '0'*64) #TRF-RITA-SOGG + TRF-RITA-BASEIMP + TRF-RITA-FRANCHIGIA + TRF-RITA-CTO-PERC + TRF-RITA-CTO-DITT + FILLER + TRF-RITA-DATA + TRF-RITA-TOTDOC + TRF-RITA-IMPVERS + TRF-RITA-DATA-I + TRF-RITA-DATA-F + TRF-EMENS-ATT + TRF-EMENS-RAP + TRF-EMENS-ASS + TRF-RITA-TOTIVA line.append(' ' + '0'*44 + ' '*11 + '0'*64) #TRF-RITA-SOGG + TRF-RITA-BASEIMP + TRF-RITA-FRANCHIGIA + TRF-RITA-CTO-PERC + TRF-RITA-CTO-DITT + FILLER + TRF-RITA-DATA + TRF-RITA-TOTDOC + TRF-RITA-IMPVERS + TRF-RITA-DATA-I + TRF-RITA-DATA-F + TRF-EMENS-ATT + TRF-EMENS-RAP + TRF-EMENS-ASS + TRF-RITA-TOTIVA
linea.append('0'*6 + ' '*178) # TRF-CAUS-PREST-ANA-B + TRF-RITA-CAUSA-B + FILLER line.append('0'*6 + ' '*178) # TRF-CAUS-PREST-ANA-B + TRF-RITA-CAUSA-B + FILLER
linea.append('0'*13 + ' '*30 + '0'*14) # TRF-POR-CODPAG + TRF-POR-BANCA + TRF-POR-AGENZIA + TRF-POR-DESAGENZIA + TRF-POR-TOT-RATE + TRF-POR-TOTDOC line.append('0'*13 + ' '*30 + '0'*14) # TRF-POR-CODPAG + TRF-POR-BANCA + TRF-POR-AGENZIA + TRF-POR-DESAGENZIA + TRF-POR-TOT-RATE + TRF-POR-TOTDOC
linea.append(('0'*65 + ' '*2)*12) # TRF-POR-NUM-RATA + TRF-POR-DATASCAD + TRF-POR-TIPOEFF + TRF-POR-IMPORTO-EFF + TRF-POR-IMPORTO-EFFVAL + TRF-POR-IMPORTO-BOLLI + TRF-POR-IMPORTO-BOLLIVAL + TRF-POR-FLAG + TRF-POR-TIPO-RD line.append(('0'*65 + ' '*2)*12) # TRF-POR-NUM-RATA + TRF-POR-DATASCAD + TRF-POR-TIPOEFF + TRF-POR-IMPORTO-EFF + TRF-POR-IMPORTO-EFFVAL + TRF-POR-IMPORTO-BOLLI + TRF-POR-IMPORTO-BOLLIVAL + TRF-POR-FLAG + TRF-POR-TIPO-RD
linea.append('0'*4 + ' '*336) # TRF-POR-CODAGE + TRF-POR-EFFETTO-SOSP + TRF-POR-CIG + TRF-POR-CUP + SPAZIO line.append('0'*4 + ' '*336) # TRF-POR-CODAGE + TRF-POR-EFFETTO-SOSP + TRF-POR-CIG + TRF-POR-CUP + SPAZIO
linea.append((' '*3 + '0'*16)*20) # TRF-COD-VAL-IV + TRF-IMP-VALUTA-IV line.append((' '*3 + '0'*16)*20) # TRF-COD-VAL-IV + TRF-IMP-VALUTA-IV
linea.append((' '*6 + '0'*35 + ' '*2 + '0'*20 + ' '*19 + '0'*16)*20) # TRF-CODICE-SERVIZIO + TRF-STATO-PAGAMENTO + TRF-SERV-IMP-EURO + TRF-SERV-IMP-VAL + TRF-DATA-DOC-ORIG + TRF-MOD-EROGAZIONE + TRF-MOD-INCASSO + TRF-PROT-REG + TRF-PROG-REG + TRF-COD-SEZ-DOG-RET + TRF-ANNO-REG-RET + TRF-NUM-DOC-ORIG + TRF-SERV-SEGNO-RET + TRF-SERV-COD-VAL-IV + TRF-SERV-IMP-VALUTA-IV line.append((' '*6 + '0'*35 + ' '*2 + '0'*20 + ' '*19 + '0'*16)*20) # TRF-CODICE-SERVIZIO + TRF-STATO-PAGAMENTO + TRF-SERV-IMP-EURO + TRF-SERV-IMP-VAL + TRF-DATA-DOC-ORIG + TRF-MOD-EROGAZIONE + TRF-MOD-INCASSO + TRF-PROT-REG + TRF-PROG-REG + TRF-COD-SEZ-DOG-RET + TRF-ANNO-REG-RET + TRF-NUM-DOC-ORIG + TRF-SERV-SEGNO-RET + TRF-SERV-COD-VAL-IV + TRF-SERV-IMP-VALUTA-IV
linea.append(' '*1 + '0'*6) # TRF-INTRA-TIPO-SERVIZIO + TRF-SERV-MESE-ANNO-RIF line.append(' '*1 + '0'*6) # TRF-INTRA-TIPO-SERVIZIO + TRF-SERV-MESE-ANNO-RIF
linea.append(' '*8) # TRF-CK-RCHARGE line.append(' '*8) # TRF-CK-RCHARGE
linea.append('0'*(15-len(fattura["numFattura"])) + fattura["numFattura"]) # TRF-XNUM-DOC-ORI line.append('0'*(15-len(invoice["numFattura"])) + invoice["numFattura"]) # TRF-XNUM-DOC-ORI
linea.append(' ' + '00' + ' '*1090) # TRF-MEM-ESIGIB-IVA + TRF-COD-IDENTIFICATIVO + TRF-ID-IMPORTAZIONE + TRF-XNUM-DOC-ORI-20 + SPAZIO + FILLER line.append(' ' + '00' + ' '*1090) # TRF-MEM-ESIGIB-IVA + TRF-COD-IDENTIFICATIVO + TRF-ID-IMPORTAZIONE + TRF-XNUM-DOC-ORI-20 + SPAZIO + FILLER
parent.log_dialog.log_text.AppendText("Creato record #1 per fattura n. %s\n" % fattura["numFattura"]) parent.log_dialog.log_text.AppendText("Creato record #1 per fattura n. %s\n" % invoice["numFattura"])
wx.Yield() wx.Yield()
linea = ''.join(linea) + '\n' line = ''.join(line) + '\n'
traf2000_file.write(linea) traf2000_file.write(line)
parent.log_dialog.log_text.AppendText("Convertita fattura n. %s\n" % fattura["numFattura"]) parent.log_dialog.log_text.AppendText("Convertita fattura n. %s\n" % invoice["numFattura"])
wx.Yield() wx.Yield()
parent.log_dialog.log_text.AppendText("Conversione terminata") parent.log_dialog.log_text.AppendText("Conversione terminata.\nTracciato TRAF2000 salvato in %s\n" % output_file_path)
wx.Yield() wx.Yield()