add write excel/insert
This commit is contained in:
58
insert.py
58
insert.py
@ -0,0 +1,58 @@
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
import pandas as pd
|
||||
import math
|
||||
import datetime
|
||||
|
||||
today = datetime.date.today()
|
||||
formatted_date = today.strftime("%Y-%m-%d")
|
||||
|
||||
data_str = "2999-12-31"
|
||||
data_fine = datetime.datetime.strptime(data_str, "%Y-%m-%d").date()
|
||||
|
||||
tla = 1
|
||||
|
||||
value_insert = []
|
||||
def insert_records(connection,codice,descrizione,um,costo,sc1,sc2,ricarica,listino,codice_alternativo,codice_barre,art):
|
||||
try:
|
||||
result = []
|
||||
cursor = connection.cursor()
|
||||
if art == False:
|
||||
query_art = "INSERT INTO art (codice,DESCRIZIONE1,TUM_CODICE,codice_alternativo,TUB_CODICE,TCM_CODICE,TGM_CODICE,TIV_CODICE_VENDITE,TIV_CODICE_ACQUISTI,TCA_CODICE,TAQ_CODICE,TS3_CODICE,TP2_CODICE,TSA_CODICE,TIN_CODICE,TNI_CODICE,TAA_CODICE) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
|
||||
cursor.execute(query_art,(codice,descrizione,um,codice_alternativo,'0','0','0','0','0','0','0','0','0','0','0','0','0'))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
query_bar = "INSERT INTO bar (ART_CODICE,CODICE_BARRE) VALUES (%s,%s)"
|
||||
cursor.execute(query_bar,(codice,codice_barre))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
if listino == 1:
|
||||
print('listino è uno')
|
||||
query_lsv = "INSERT INTO lsv (ART_CODICE,DATA_FINE,DATA_INIZIO,PREZZO,TSM_CODICE,TRL_CODICE,TLV_CODICE) VALUES (%s,%s,%s,%s,%s,%s,%s)"
|
||||
cursor.execute(query_lsv,(codice,data_fine,formatted_date,costo,sc2,ricarica,listino))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
query_lsa = "INSERT INTO lsa (ART_CODICE,TLA_CODICE,DATA_INIZIO,DATA_FINE,PREZZO,TSM_CODICE) VALUES (%s,%s,%s,%s,%s,%s)"
|
||||
cursor.execute(query_lsa,(codice,tla,formatted_date,data_fine,costo,sc1))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
else :
|
||||
query_lsv = "INSERT INTO lsv (ART_CODICE,DATA_FINE,DATA_INIZIO,PREZZO,TSM_CODICE,TRL_CODICE,TLV_CODICE) VALUES (%s,%s,%s,%s,%s,%s,%s)"
|
||||
cursor.execute(query_lsv,(codice,data_fine,formatted_date,costo,sc2,ricarica,listino))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
connection.commit()
|
||||
|
||||
if any(val != 0 for val in result):
|
||||
row = [codice,descrizione,um,costo,sc1,sc2,ricarica,codice_alternativo,codice_barre]
|
||||
value_insert.append(row)
|
||||
return value_insert
|
||||
|
||||
except Error as e:
|
||||
print(f"Errore durante l'inserimento: {e}")
|
||||
finally:
|
||||
cursor.close()
|
||||
112
main.py
112
main.py
@ -0,0 +1,112 @@
|
||||
import pandas as pd
|
||||
import mysql.connector
|
||||
import math
|
||||
import xlsxwriter
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog,messagebox
|
||||
from datetime import datetime
|
||||
from mysql.connector import Error
|
||||
from connection import create_connection
|
||||
from connection import close_connection
|
||||
from search import search_article
|
||||
from update import update_records
|
||||
from insert import insert_records
|
||||
from write import write_excel
|
||||
from write_excel_insert import write_excel_insert
|
||||
|
||||
|
||||
host = "62.86.98.170"
|
||||
user = "root"
|
||||
password = "masterkey"
|
||||
database = "arc_rumo"
|
||||
port = 3307
|
||||
|
||||
#creao la connessione al database
|
||||
conn = create_connection(host, user, password, database, port)
|
||||
#leggo il file excel
|
||||
df = pd.read_excel('TEST.xlsx', usecols='A:M')
|
||||
|
||||
#leggo le colonne del file excel
|
||||
for index, row in df.iterrows():
|
||||
codice = row['CODICE']
|
||||
descrizione = row['DESCRIZIONE']
|
||||
um = row['UN']
|
||||
costo = row['COSTO']
|
||||
sc1 = str(row['SC1']) ## tsm_codice per la tabella lsa
|
||||
sc2 = str(row['SC2']) ## tsm_codice per la tabella lsv
|
||||
|
||||
if math.isnan(row['RICARICA1']):
|
||||
ricarica1 = row['RICARICA1']
|
||||
else:
|
||||
ricarica1 = int(row['RICARICA1']) ## trl_codice per codice listino 1
|
||||
|
||||
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
|
||||
|
||||
|
||||
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 conn :
|
||||
## cerco codice articlo
|
||||
## se non esiste chiamo l funzione di insert
|
||||
## se esiste controllo cosice listino
|
||||
## se non esiste chiamo la funzione di insert
|
||||
## se esiste chiamo la funzione di update
|
||||
|
||||
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)
|
||||
|
||||
if art == True :
|
||||
value_update = update_records(conn,codice,prezzo,sc1,sc2,listino,ricarica,codice_barre,codice_alternativo)
|
||||
print(value_update)
|
||||
if value_update is not None:
|
||||
write_excel(value_update)
|
||||
else:
|
||||
costo = 0.00
|
||||
if descrizione is None :
|
||||
descrizione = ' '
|
||||
if um is None:
|
||||
um = ' '
|
||||
if sc1 is None:
|
||||
sc1 = ' '
|
||||
if sc2 is None:
|
||||
sc2 = ' '
|
||||
if math.isnan(ricarica):
|
||||
ricarica = ' '
|
||||
if codice_alternativo is None:
|
||||
codice_alternativo = ' '
|
||||
if codice_barre is None:
|
||||
codice_barre = ' '
|
||||
value_insert = insert_records(conn,codice,descrizione,um,costo,sc1,sc2,ricarica,listino,codice_alternativo,codice_barre,art)
|
||||
write_excel_insert(value_insert)
|
||||
|
||||
if conn :
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
17
search.py
17
search.py
@ -11,17 +11,26 @@ def search_article(connection,codice,listino):
|
||||
cursor_1 = connection.cursor()
|
||||
cursor_2 = connection.cursor()
|
||||
query = "SELECT codice FROM art WHERE codice = %s"
|
||||
cursor_1.execute(query,(codice))
|
||||
result_1 = cursor.fetchone()
|
||||
cursor_1.execute(query,(codice,))
|
||||
result_1 = cursor_1.fetchone()
|
||||
|
||||
print('result')
|
||||
print(result_1)
|
||||
|
||||
if result_1: ## se l'articolo esiste creco per codice listino
|
||||
print('articlo esiste')
|
||||
print(listino)
|
||||
query_lsv = "SELECT TLV_CODICE FROM lsv WHERE ART_CODICE = %s AND TLV_CODICE = %s"
|
||||
cursor_2.execute(query_lsv,(codice,listino))
|
||||
result_2 = cursor_2.fetchone()
|
||||
|
||||
print('ricerca per listino')
|
||||
print(result_2)
|
||||
if result_2: ## se il codice listino è stato trovato
|
||||
print('codice listino trovato')
|
||||
return True ## esistono sia articolo che codice listino
|
||||
else:
|
||||
return False ## articlo esiste ma non esiste il codice listino
|
||||
print('codice listino non trovato')
|
||||
return 'not code art'
|
||||
else:
|
||||
print('codice non esiste')
|
||||
return False ## l'articolo non esiste
|
||||
87
update.py
87
update.py
@ -0,0 +1,87 @@
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
import pandas as pd
|
||||
import math
|
||||
|
||||
value_update = []
|
||||
|
||||
def update_records(connection,codice,prezzo,sc1,sc2,listino,ricarica,codice_barre,codice_alternativo):
|
||||
|
||||
try:
|
||||
result = []
|
||||
cursor = connection.cursor()
|
||||
|
||||
if sc1 is not None :
|
||||
query_lsa = "UPDATE lsa SET TSM_CODICE = %s WHERE ART_CODICE = %s AND TLA_CODICE = '1'"
|
||||
cursor.execute(query_lsa, (sc1, codice))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
if sc2 is not None:
|
||||
query_lsv_tsm = "UPDATE lsv SET TSM_CODICE = %s WHERE ART_CODICE = %s AND TLV_CODICE = %s"
|
||||
cursor.execute(query_lsv_tsm, (sc2, codice,listino))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
if prezzo is not None and not math.isnan(prezzo):
|
||||
query_lsa = "UPDATE lsa SET PREZZO = %s WHERE ART_CODICE = %s AND TLA_CODICE = '1'"
|
||||
cursor.execute(query_lsa, (prezzo,codice))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
if ricarica is not None and not math.isnan(ricarica):
|
||||
query_lsv = "UPDATE lsv SET TRL_CODICE = %s WHERE ART_CODICE = %s AND TLV_CODICE = %s"
|
||||
cursor.execute(query_lsv, (ricarica, codice,listino))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
if codice_barre is not None:
|
||||
query_bar = "UPDATE bar SET CODICE_BARRE = %s WHERE ART_CODICE = %s"
|
||||
cursor.execute(query_bar, (codice_barre, codice))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
if not pd.isna(codice_alternativo):
|
||||
query_alt = "UPDATE art SET CODICE_ALTERNATIVO = %s WHERE codice = %s"
|
||||
cursor.execute(query_alt, (codice_alternativo, codice))
|
||||
count = cursor.rowcount
|
||||
result.append(count)
|
||||
|
||||
connection.commit()
|
||||
|
||||
if all(value == 0 for value in result):
|
||||
print(f"Record non aggiornati per ART_CODICE: {codice}")
|
||||
|
||||
if math.isnan(prezzo):
|
||||
prezzo = 0
|
||||
|
||||
if math.isnan(listino):
|
||||
listino = 0
|
||||
|
||||
if math.isnan(sc1):
|
||||
sc1 = 0
|
||||
|
||||
if math.isnan(sc2):
|
||||
sc2 = 0
|
||||
|
||||
if math.isnan(ricarica):
|
||||
ricarica = 0
|
||||
|
||||
|
||||
if math.isnan(codice_barre):
|
||||
codice_barre = 0
|
||||
|
||||
if pd.isna(codice_alternativo):
|
||||
codice_alternativo = 0;
|
||||
|
||||
row = [codice,prezzo,listino,sc1,ricarica,codice_barre,codice_alternativo,sc2]
|
||||
value_update.append(row)
|
||||
return value_update
|
||||
|
||||
else:
|
||||
print(f"Record aggiornati per ART_CODICE: {codice}")
|
||||
|
||||
except Error as e:
|
||||
print(f"Errore durante l'aggiornamento: {e}")
|
||||
finally:
|
||||
cursor.close()
|
||||
32
write.py
Normal file
32
write.py
Normal file
@ -0,0 +1,32 @@
|
||||
import pandas as pd
|
||||
import xlsxwriter
|
||||
import tkinter as tk
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
|
||||
def write_excel(value_update):
|
||||
today = datetime.now().strftime('%d-%m-%Y_%H-%M')
|
||||
filename = f'Articoli_Modificati{today}.xlsx'
|
||||
workbook = xlsxwriter.Workbook(filename)
|
||||
worksheet = workbook.add_worksheet()
|
||||
|
||||
worksheet.write('A1', 'CODICE (ART_CODICE)')
|
||||
worksheet.write('B1', 'COSTO (PREZZO)')
|
||||
worksheet.write('C1', 'CODICE LISTINO (TLV/TLA_CODICE)')
|
||||
worksheet.write('D1', 'SC1 (TSM_CODICE)')
|
||||
worksheet.write('E1', 'RICARICA1 (TRL_CODICE)')
|
||||
worksheet.write('F1', 'CODICE BARRE')
|
||||
worksheet.write('G1', 'CODICE ALTERNATIVO')
|
||||
worksheet.write('H1', 'TSM_CODICE_LSV')
|
||||
|
||||
for row_index, row_data in enumerate(value_update):
|
||||
|
||||
for col_index, value in enumerate(row_data):
|
||||
worksheet.write(row_index + 1, col_index, value)
|
||||
|
||||
root = tk.Tk()
|
||||
loading_label = tk.Label(root, text="")
|
||||
loading_label.pack(pady=20)
|
||||
loading_label.config(text="Aggiornamento in corso... Attendere...")
|
||||
workbook.close()
|
||||
34
write_excel_insert.py
Normal file
34
write_excel_insert.py
Normal file
@ -0,0 +1,34 @@
|
||||
import pandas as pd
|
||||
import xlsxwriter
|
||||
import tkinter as tk
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
|
||||
def write_excel_insert(value_insert):
|
||||
today = datetime.now().strftime('%d-%m-%Y_%H-%M')
|
||||
filename = f'Articoli_Inseriti{today}.xlsx'
|
||||
workbook = xlsxwriter.Workbook(filename)
|
||||
worksheet = workbook.add_worksheet()
|
||||
|
||||
worksheet.write('A1', 'CODICE (ART_CODICE)')
|
||||
worksheet.write('B1', 'DESCRIZIONE')
|
||||
worksheet.write('C1', 'UM')
|
||||
worksheet.write('D1', 'COSTO')
|
||||
worksheet.write('E1', 'SC1')
|
||||
worksheet.write('F1', 'SC2')
|
||||
worksheet.write('G1', 'RICARICA')
|
||||
worksheet.write('H1', 'CODICE_ALTERNATIVO')
|
||||
worksheet.write('I1', 'CODICE_BARRE')
|
||||
|
||||
|
||||
for row_index, row_data in enumerate(value_insert):
|
||||
|
||||
for col_index, value in enumerate(row_data):
|
||||
worksheet.write(row_index + 1, col_index, value)
|
||||
|
||||
root = tk.Tk()
|
||||
loading_label = tk.Label(root, text="")
|
||||
loading_label.pack(pady=20)
|
||||
loading_label.config(text="Aggiornamento in corso... Attendere...")
|
||||
workbook.close()
|
||||
Reference in New Issue
Block a user