Refactoring codice
This commit is contained in:
236
main.py
236
main.py
@ -1,14 +1,21 @@
|
||||
import pandas as pd
|
||||
import math
|
||||
from utils.connection import create_connection
|
||||
from utils.connection import close_connection
|
||||
from utils.search import search_article
|
||||
from utils.update import update_records
|
||||
from utils.insert import insert_records
|
||||
from utils.search import search_article, search_barcode, search_lsa, search_lsv, search_um
|
||||
from utils.update import update_article, update_lsa, update_lsv
|
||||
from utils.insert import insert_barcode, insert_article, insert_listino_acquisto, insert_listino_vendita
|
||||
from utils.write import write_excel
|
||||
from utils.write_excel_insert import write_excel_insert
|
||||
from utils.upload import upload_excel
|
||||
|
||||
class bcolors:
|
||||
HEADER = '\033[95m'
|
||||
OKBLUE = '\033[94m'
|
||||
OKCYAN = '\033[96m'
|
||||
OKGREEN = '\033[92m'
|
||||
WARNING = '\033[93m'
|
||||
FAIL = '\033[91m'
|
||||
ENDC = '\033[0m'
|
||||
BOLD = '\033[1m'
|
||||
UNDERLINE = '\033[4m'
|
||||
|
||||
host = "62.86.98.170"
|
||||
user = "root"
|
||||
@ -17,118 +24,143 @@ database = "arc_rumo"
|
||||
port = 3307
|
||||
|
||||
#creo la connessione al database
|
||||
conn = create_connection(host, user, password, database, port)
|
||||
connection = create_connection(host, user, password, database, port)
|
||||
#leggo il file excel
|
||||
file_input = upload_excel()
|
||||
df = pd.read_excel(file_input, usecols='A:O', dtype={'FORNITORE': str, 'CODICE ALTERNATIVO': str})
|
||||
df = pd.read_excel(file_input, usecols='A:O', dtype={'CODICE': str, 'DESCRIZIONE': str, 'UN':str, 'SC1': str, 'RICARICA1': str, 'RICARICA2': str, 'RICARICA3': str, 'RICARICA4': str, 'SC2': str, 'CODICE_BARRE': str, 'CODICE ALTERNATIVO': str, 'GRUPPO STATISTICO': str, 'FORNITORE': str,})
|
||||
|
||||
updated_rows = []
|
||||
i=0
|
||||
#leggo le colonne del file excel
|
||||
for index, row in df.iterrows():
|
||||
codice = row['CODICE']
|
||||
descrizione = row['DESCRIZIONE']
|
||||
um = row['UN']
|
||||
costo = row['COSTO']
|
||||
|
||||
|
||||
if math.isnan(row['SC1']):
|
||||
sc1 = row['SC1']
|
||||
else:
|
||||
sc1 = int(row['SC1']) ## tsm_codice per la tabella lsa
|
||||
codice_articolo = '' if pd.isna(row['CODICE']) else row['CODICE']
|
||||
descrizione = '' if pd.isna(row['DESCRIZIONE']) else row['DESCRIZIONE']
|
||||
um = '' if pd.isna(row['UN']) else row['UN']
|
||||
costo = '' if pd.isna(row['COSTO']) else float(row['COSTO'])
|
||||
sc1 = '' if pd.isna(row['SC1']) else row['SC1']
|
||||
|
||||
if math.isnan(row['SC2']):
|
||||
sc2 = row['SC2']
|
||||
else :
|
||||
sc2 = int(row['SC2'])
|
||||
ricarica = {"1" : "", "2" : "", "4" : ""}
|
||||
|
||||
if math.isnan(row['RICARICA1']):
|
||||
ricarica1 = row['RICARICA1']
|
||||
else:
|
||||
ricarica1 = int(row['RICARICA1']) ## trl_codice per codice listino 1
|
||||
if not pd.isna(row['RICARICA1']):
|
||||
ricarica['1'] = row['RICARICA1']
|
||||
if not pd.isna(row['RICARICA2']):
|
||||
ricarica['2'] = row['RICARICA2']
|
||||
#if not pd.isna(row['RICARICA3']):
|
||||
# ricarica['3'] = row['RICARICA3']
|
||||
if not pd.isna(row['RICARICA4']):
|
||||
ricarica['4'] = row['RICARICA4']
|
||||
|
||||
if math.isnan(row['RICARICA2']):
|
||||
ricarica2 = row['RICARICA2']
|
||||
else:
|
||||
ricarica2 = int(row['RICARICA2']) ## trl_codice per codice listino 2
|
||||
|
||||
if math.isnan(row['RICARICA3']):
|
||||
ricarica3 = row['RICARICA3']
|
||||
else:
|
||||
ricarica3 = int(row['RICARICA3']) ## trl_codice per codice listino 3
|
||||
sc2 = '' if pd.isna(row['SC2']) else row['SC2']
|
||||
prezzo_pubblico = '' if pd.isna(row['PREZZO PUBBLICO']) else float(row['PREZZO PUBBLICO'])
|
||||
codice_barre = '' if pd.isna(row['CODICE_BARRE']) else row['CODICE_BARRE']
|
||||
codice_alternativo = '' if pd.isna(row['CODICE ALTERNATIVO']) else row['CODICE ALTERNATIVO']
|
||||
tsa_codice = '' if pd.isna(row['GRUPPO STATISTICO']) else row['GRUPPO STATISTICO']
|
||||
frn_codice = '' if pd.isna(row['FORNITORE']) else row['FORNITORE']
|
||||
|
||||
|
||||
if math.isnan(row['RICARICA4']):
|
||||
ricarica4 = row['RICARICA4']
|
||||
else:
|
||||
ricarica4 = int(row['RICARICA4']) ## trl_codice per codice listino 4
|
||||
|
||||
codice_barre = str(row['CODICE_BARRE'])
|
||||
codice_alternativo = str(row['CODICE ALTERNATIVO'])
|
||||
prezzo = float(row['COSTO'])
|
||||
|
||||
|
||||
if math.isnan(row['GRUPPO STATISTICO']):
|
||||
tsa_codice = row['GRUPPO STATISTICO']
|
||||
else:
|
||||
tsa_codice = int(row['GRUPPO STATISTICO'])
|
||||
|
||||
frn = str(row['FORNITORE'])
|
||||
|
||||
descrizione2 = ' '
|
||||
descrizione2 = ''
|
||||
## se decrizione supera 40
|
||||
len_descrizione = len(descrizione)
|
||||
if len_descrizione > 40 :
|
||||
if len(descrizione) > 40 :
|
||||
descrizione1 = descrizione [: 40]
|
||||
#print(descrizione1)
|
||||
descrizione2 = descrizione [40 :]
|
||||
#print(descrizione2)
|
||||
descrizione = descrizione1
|
||||
|
||||
if conn :
|
||||
if ricarica1 is not None and not math.isnan(ricarica1):
|
||||
listino = 1
|
||||
ricarica = ricarica1
|
||||
elif ricarica2 is not None and not math.isnan(ricarica2):
|
||||
listino = 2
|
||||
ricarica = ricarica2
|
||||
elif ricarica3 is not None and not math.isnan(ricarica3):
|
||||
listino = 3
|
||||
ricarica = ricarica3
|
||||
elif ricarica4 is not None and not math.isnan(ricarica4):
|
||||
listino = 4
|
||||
ricarica = ricarica4
|
||||
|
||||
art = search_article(conn,codice,listino) #cerco codice articolo
|
||||
updated_rows.append({'ART_CODICE': [0, codice_articolo], 'TUM_CODICE': [0, um], 'CODICE_BARRE': [0, codice_barre], 'LSA_ID': [0, ''], 'LSV_ID': {'1': [0, ''], '2': [0, ''], '4': [0, '']}})
|
||||
|
||||
if art == True : ## se l'atricolo esiste eseguo l'update dei valori
|
||||
value_update = update_records(conn,codice,prezzo,sc1,sc2,listino,ricarica,codice_barre,codice_alternativo,tsa_codice,frn)
|
||||
|
||||
if value_update is not None:
|
||||
write_excel(value_update)
|
||||
else: ## se non esiste o non esiste con quel listino effettuo l'inserimento
|
||||
##costo = 0.00
|
||||
if descrizione is None :
|
||||
descrizione = ' '
|
||||
if um is None:
|
||||
um = ' '
|
||||
if math.isnan(sc1):
|
||||
sc1 = ' '
|
||||
if math.isnan(sc2):
|
||||
sc2 = ' '
|
||||
if math.isnan(ricarica):
|
||||
ricarica = ' '
|
||||
if pd.isna(codice_alternativo):
|
||||
codice_alternativo = ' '
|
||||
if pd.isna(codice_barre):
|
||||
codice_barre = '0000000000000'
|
||||
if math.isnan(tsa_codice):
|
||||
tsa_codice = '0'
|
||||
if pd.isna(frn):
|
||||
frn = ' '
|
||||
|
||||
value_insert = insert_records(conn,codice,descrizione,descrizione2,um,prezzo,sc1,sc2,ricarica,listino,codice_alternativo,codice_barre,art,tsa_codice,frn)
|
||||
write_excel_insert(value_insert)
|
||||
|
||||
if conn :
|
||||
conn.close()
|
||||
if connection :
|
||||
|
||||
if search_um(connection, um) or um == '':
|
||||
updated_rows[i]['TUM_CODICE'] = [2, um]
|
||||
|
||||
if codice_articolo != '' and descrizione != '':
|
||||
if search_article(connection, codice_articolo):
|
||||
print(bcolors.OKGREEN+'Articolo trovato: '+codice_articolo, end =" -> ")
|
||||
|
||||
if update_article(connection, codice_articolo, descrizione, descrizione2, um, codice_alternativo, tsa_codice, frn_codice):
|
||||
print(bcolors.OKGREEN + 'Aggiornato')
|
||||
updated_rows[i]['ART_CODICE'] = [2, codice_articolo]
|
||||
else:
|
||||
print(bcolors.FAIL + "Aggiornamento non riuscito")
|
||||
updated_rows[i]['ART_CODICE'] = [0, codice_articolo]
|
||||
else:
|
||||
print(bcolors.WARNING+'Articolo non trovato: '+codice_articolo, end =" -> ")
|
||||
if insert_article(connection, codice_articolo, descrizione, descrizione2, um, codice_alternativo, tsa_codice, frn_codice):
|
||||
print(bcolors.OKGREEN + 'Inserito')
|
||||
updated_rows[i]['ART_CODICE'] = [1, codice_articolo]
|
||||
else:
|
||||
print(bcolors.FAIL + "Inserimento non riuscito")
|
||||
updated_rows[i]['ART_CODICE'] = [0, codice_articolo]
|
||||
continue
|
||||
else:
|
||||
continue
|
||||
else:
|
||||
print(bcolors.FAIL + 'Unità di misura non presente nel gestionale: ' + um)
|
||||
updated_rows[i]['ART_CODICE'] = [0, codice_articolo]
|
||||
updated_rows[i]['TUM_CODICE'] = [0, um]
|
||||
continue
|
||||
|
||||
|
||||
if codice_barre != '':
|
||||
if not search_barcode(connection, codice_articolo, codice_barre):
|
||||
print(bcolors.WARNING + 'Barcode non trovato: ' + codice_barre, end=" -> ")
|
||||
if insert_barcode(connection, codice_articolo, codice_barre):
|
||||
print(bcolors.OKGREEN + 'Inserito')
|
||||
updated_rows[i]['CODICE_BARRE'] = [1, codice_barre]
|
||||
else:
|
||||
print(bcolors.FAIL + "Inserimento non riuscito")
|
||||
updated_rows[i]['CODICE_BARRE'] = [0, codice_barre]
|
||||
else:
|
||||
print(bcolors.OKGREEN + 'Barcode già inserito: ' + codice_barre)
|
||||
updated_rows[i]['CODICE_BARRE'] = [2, codice_barre]
|
||||
|
||||
if costo != '':
|
||||
id_lsa = search_lsa(connection, codice_articolo, sc1)
|
||||
if id_lsa:
|
||||
print(bcolors.OKGREEN + 'Listino di acquisto trovato', end=" -> ")
|
||||
if update_lsa(connection, id_lsa[0], costo):
|
||||
print(bcolors.OKGREEN + 'Aggiornato')
|
||||
updated_rows[i]['LSA_ID'] = [2, id_lsa[0]]
|
||||
else:
|
||||
print(bcolors.FAIL + "Aggiornamento non riuscito")
|
||||
updated_rows[i]['LSA_ID'] = [0, id_lsa[0]]
|
||||
else:
|
||||
print(bcolors.WARNING + 'Listino di acquisto non trovato', end=" -> ")
|
||||
id_lsa = insert_listino_acquisto(connection, codice_articolo, costo, sc1)
|
||||
if id_lsa:
|
||||
print(bcolors.OKGREEN + 'Inserito')
|
||||
updated_rows[i]['LSA_ID'] = [1, id_lsa]
|
||||
else:
|
||||
print(bcolors.FAIL + "Inserimento non riuscito")
|
||||
|
||||
|
||||
for key, value in ricarica.items():
|
||||
id_lsv = search_lsv(connection, codice_articolo, value, key)
|
||||
if id_lsv:
|
||||
print(bcolors.OKGREEN + 'Listino di vendita trovato', end=" -> ")
|
||||
if update_lsv(connection, id_lsv[0], costo, prezzo_pubblico, key, sc1, sc2, value):
|
||||
print(bcolors.OKGREEN + 'Aggiornato')
|
||||
updated_rows[i]['LSV_ID'][key] = [2, id_lsv[0]]
|
||||
else:
|
||||
print(bcolors.FAIL + "Aggiornamento non riuscito")
|
||||
updated_rows[i]['LSV_ID'][key] = [0, id_lsv[0]]
|
||||
else:
|
||||
if value != '':
|
||||
print(bcolors.WARNING + 'Listino di vendita non trovato', end=" -> ")
|
||||
id_lsv = insert_listino_vendita(connection, codice_articolo, costo, prezzo_pubblico, key, sc1, sc2, value)
|
||||
if id_lsv:
|
||||
print(bcolors.OKGREEN + 'Inserito')
|
||||
updated_rows[i]['LSV_ID'][key] = [1, id_lsv]
|
||||
else:
|
||||
print(bcolors.FAIL + "Inserimento non riuscito")
|
||||
|
||||
connection.commit()
|
||||
|
||||
i += 1
|
||||
|
||||
write_excel(updated_rows)
|
||||
|
||||
if connection :
|
||||
connection.close()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user