Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +26 -31
src/streamlit_app.py
CHANGED
|
@@ -68,13 +68,16 @@ def convert_json_to_excel_data(json_file_content: str):
|
|
| 68 |
|
| 69 |
processed_data_for_this_file = []
|
| 70 |
for item in details:
|
|
|
|
| 71 |
cancel_pos_oda_val = item.get('cancelPosOdA')
|
| 72 |
-
if cancel_pos_oda_val is None
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
else:
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
row = {
|
| 80 |
'site.code': site_code_json,
|
|
@@ -83,8 +86,8 @@ def convert_json_to_excel_data(json_file_content: str):
|
|
| 83 |
'supplier.partnerRef.code': partner_code,
|
| 84 |
'supplier.partnerRef.company.code': company_code,
|
| 85 |
'docDate': order_date_formatted,
|
| 86 |
-
'status': 'CREATED',
|
| 87 |
-
'positions.status':
|
| 88 |
'positions.positionId': item.get('positionId'),
|
| 89 |
'positions.stock.item.itemNumber': item.get('itemCode'),
|
| 90 |
'positions.stock.quantity': item.get('quantity'),
|
|
@@ -95,7 +98,7 @@ def convert_json_to_excel_data(json_file_content: str):
|
|
| 95 |
'positions.spare.string1': item.get('refOrder'),
|
| 96 |
'positions.reason.code': item.get('reasonCode'), # Naming corretto
|
| 97 |
'positions.reason.company.code': company_code,
|
| 98 |
-
'positions.stock.cancelPosOda'
|
| 99 |
}
|
| 100 |
processed_data_for_this_file.append(row)
|
| 101 |
|
|
@@ -164,27 +167,22 @@ if uploaded_files:
|
|
| 164 |
|
| 165 |
# --- Post-processing per la formattazione Excel ---
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
final_df['positions.stock.cancelPosOda'] = final_df['positions.stock.cancelPosOda'].apply(lambda x: str(x).capitalize())
|
| 169 |
|
| 170 |
-
#
|
| 171 |
header_columns = [
|
| 172 |
'site.code', 'docNumber', 'company.code', 'supplier.partnerRef.code',
|
| 173 |
'supplier.partnerRef.company.code', 'docDate', 'status'
|
| 174 |
]
|
| 175 |
|
| 176 |
-
#
|
| 177 |
final_df = final_df.sort_values(by=['docNumber', 'positions.positionId']).reset_index(drop=True)
|
| 178 |
|
| 179 |
-
#
|
| 180 |
-
# (cioè tutte le righe tranne la prima per ogni orderNumber)
|
| 181 |
-
# Keep='first' significa che la prima occorrenza di ogni docNumber sarà False (non duplicata)
|
| 182 |
-
# e tutte le successive saranno True (duplicate).
|
| 183 |
-
# Vogliamo azzerare i campi di testata solo per le righe True.
|
| 184 |
rows_to_clear_headers = final_df.duplicated(subset=['docNumber'], keep='first')
|
| 185 |
|
| 186 |
-
#
|
| 187 |
-
# Usiamo .loc per modificare il DataFrame in modo sicuro
|
| 188 |
final_df.loc[rows_to_clear_headers, header_columns] = ""
|
| 189 |
|
| 190 |
# --- Creazione file Excel ---
|
|
@@ -192,10 +190,9 @@ if uploaded_files:
|
|
| 192 |
writer = pd.ExcelWriter(output, engine='xlsxwriter')
|
| 193 |
|
| 194 |
sheet_name_excel = 'PurchaseOrder'
|
| 195 |
-
# Non scriviamo ancora su excel, lo faremo manualmente per la riga 1 e 2
|
| 196 |
|
| 197 |
workbook = writer.book
|
| 198 |
-
worksheet = writer.sheets.get(sheet_name_excel)
|
| 199 |
if worksheet is None:
|
| 200 |
worksheet = workbook.add_worksheet(sheet_name_excel)
|
| 201 |
|
|
@@ -208,7 +205,7 @@ if uploaded_files:
|
|
| 208 |
'supplier.partnerRef.company.code',
|
| 209 |
'docDate',
|
| 210 |
'status',
|
| 211 |
-
'positions.status',
|
| 212 |
'positions.positionId',
|
| 213 |
'positions.stock.item.itemNumber',
|
| 214 |
'positions.stock.quantity',
|
|
@@ -219,11 +216,10 @@ if uploaded_files:
|
|
| 219 |
'positions.spare.string1',
|
| 220 |
'positions.reason.code', # Naming corretto
|
| 221 |
'positions.reason.company.code',
|
| 222 |
-
'positions.stock.cancelPosOda'
|
| 223 |
]
|
| 224 |
|
| 225 |
# Assicurati che il DataFrame abbia tutte le colonne nell'ordine desiderato
|
| 226 |
-
# e aggiungi quelle mancanti come stringhe vuote se necessario, anche se non dovrebbero mancare
|
| 227 |
for col in final_columns_order:
|
| 228 |
if col not in final_df.columns:
|
| 229 |
final_df[col] = "" # Aggiungi colonne mancanti come vuote
|
|
@@ -236,9 +232,9 @@ if uploaded_files:
|
|
| 236 |
'company.code': 15,
|
| 237 |
'supplier.partnerRef.code': 18,
|
| 238 |
'supplier.partnerRef.company.code': 25,
|
| 239 |
-
'docDate': 20,
|
| 240 |
'status': 10,
|
| 241 |
-
'positions.status': 12,
|
| 242 |
'positions.positionId': 12,
|
| 243 |
'positions.stock.item.itemNumber': 25,
|
| 244 |
'positions.stock.quantity': 10,
|
|
@@ -247,9 +243,9 @@ if uploaded_files:
|
|
| 247 |
'positions.stock.accountingInfo.availability': 15,
|
| 248 |
'positions.stock.item.company.code': 25,
|
| 249 |
'positions.spare.string1': 15,
|
| 250 |
-
'positions.reason.code': 15,
|
| 251 |
'positions.reason.company.code': 25,
|
| 252 |
-
'positions.stock.cancelPosOda'
|
| 253 |
}
|
| 254 |
|
| 255 |
# Scrivi i nomi delle colonne nella riga 2 (indice 1 in xlsxwriter)
|
|
@@ -263,7 +259,6 @@ if uploaded_files:
|
|
| 263 |
worksheet.set_column(col_num, col_num, 15) # Larghezza di default
|
| 264 |
|
| 265 |
# Scrivi i dati a partire dalla riga 3 (indice 2 in xlsxwriter)
|
| 266 |
-
# header=False per evitare che to_excel scriva gli header (li abbiamo già scritti noi)
|
| 267 |
final_df.to_excel(writer, sheet_name=sheet_name_excel, index=False, header=False, startrow=2)
|
| 268 |
|
| 269 |
writer.close()
|
|
@@ -283,11 +278,11 @@ if uploaded_files:
|
|
| 283 |
else:
|
| 284 |
st.info("Nessun dato valido estratto dai file selezionati.")
|
| 285 |
else:
|
| 286 |
-
st.info("Carica i tuoi file JSON per iniziare.")
|
| 287 |
|
| 288 |
# --- Funzione per il reset ---
|
| 289 |
def clear_inputs():
|
| 290 |
-
st.session_state.file_uploader_key += 1
|
| 291 |
|
| 292 |
st.markdown("---")
|
| 293 |
st.button("Pulisci Input (RESET)", on_click=clear_inputs)
|
|
|
|
| 68 |
|
| 69 |
processed_data_for_this_file = []
|
| 70 |
for item in details:
|
| 71 |
+
# NUOVA LOGICA PER positions.status BASATA SU cancelPosOdA
|
| 72 |
cancel_pos_oda_val = item.get('cancelPosOdA')
|
| 73 |
+
if cancel_pos_oda_val is None or \
|
| 74 |
+
(isinstance(cancel_pos_oda_val, bool) and not cancel_pos_oda_val) or \
|
| 75 |
+
(isinstance(cancel_pos_oda_val, str) and cancel_pos_oda_val.lower() == 'false'):
|
| 76 |
+
# Se è None, False (booleano), o "false" (stringa)
|
| 77 |
+
positions_status_value = 'CREATED'
|
| 78 |
else:
|
| 79 |
+
# Altrimenti (True, "true", o qualsiasi altro valore non falso)
|
| 80 |
+
positions_status_value = 'CANCELLED'
|
| 81 |
|
| 82 |
row = {
|
| 83 |
'site.code': site_code_json,
|
|
|
|
| 86 |
'supplier.partnerRef.code': partner_code,
|
| 87 |
'supplier.partnerRef.company.code': company_code,
|
| 88 |
'docDate': order_date_formatted,
|
| 89 |
+
'status': 'CREATED', # Questo si riferisce allo status dell'intera testata dell'ordine
|
| 90 |
+
'positions.status': positions_status_value, # Qui applichiamo il valore condizionale
|
| 91 |
'positions.positionId': item.get('positionId'),
|
| 92 |
'positions.stock.item.itemNumber': item.get('itemCode'),
|
| 93 |
'positions.stock.quantity': item.get('quantity'),
|
|
|
|
| 98 |
'positions.spare.string1': item.get('refOrder'),
|
| 99 |
'positions.reason.code': item.get('reasonCode'), # Naming corretto
|
| 100 |
'positions.reason.company.code': company_code,
|
| 101 |
+
# RIMOZIONE COMPLETA DI 'positions.stock.cancelPosOda'
|
| 102 |
}
|
| 103 |
processed_data_for_this_file.append(row)
|
| 104 |
|
|
|
|
| 167 |
|
| 168 |
# --- Post-processing per la formattazione Excel ---
|
| 169 |
|
| 170 |
+
# Rimuoviamo la riga per cancelPosOda in quanto non serve più
|
| 171 |
+
# final_df['positions.stock.cancelPosOda'] = final_df['positions.stock.cancelPosOda'].apply(lambda x: str(x).capitalize())
|
| 172 |
|
| 173 |
+
# Definizione delle colonne di testata
|
| 174 |
header_columns = [
|
| 175 |
'site.code', 'docNumber', 'company.code', 'supplier.partnerRef.code',
|
| 176 |
'supplier.partnerRef.company.code', 'docDate', 'status'
|
| 177 |
]
|
| 178 |
|
| 179 |
+
# Ordina il DataFrame per docNumber e positionId per raggruppare gli ordini
|
| 180 |
final_df = final_df.sort_values(by=['docNumber', 'positions.positionId']).reset_index(drop=True)
|
| 181 |
|
| 182 |
+
# Crea una maschera booleana per identificare le righe duplicate per docNumber
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
rows_to_clear_headers = final_df.duplicated(subset=['docNumber'], keep='first')
|
| 184 |
|
| 185 |
+
# Azzerare le colonne di testata per le righe identificate
|
|
|
|
| 186 |
final_df.loc[rows_to_clear_headers, header_columns] = ""
|
| 187 |
|
| 188 |
# --- Creazione file Excel ---
|
|
|
|
| 190 |
writer = pd.ExcelWriter(output, engine='xlsxwriter')
|
| 191 |
|
| 192 |
sheet_name_excel = 'PurchaseOrder'
|
|
|
|
| 193 |
|
| 194 |
workbook = writer.book
|
| 195 |
+
worksheet = writer.sheets.get(sheet_name_excel)
|
| 196 |
if worksheet is None:
|
| 197 |
worksheet = workbook.add_worksheet(sheet_name_excel)
|
| 198 |
|
|
|
|
| 205 |
'supplier.partnerRef.company.code',
|
| 206 |
'docDate',
|
| 207 |
'status',
|
| 208 |
+
'positions.status', # Questo campo ora ha la logica condizionale
|
| 209 |
'positions.positionId',
|
| 210 |
'positions.stock.item.itemNumber',
|
| 211 |
'positions.stock.quantity',
|
|
|
|
| 216 |
'positions.spare.string1',
|
| 217 |
'positions.reason.code', # Naming corretto
|
| 218 |
'positions.reason.company.code',
|
| 219 |
+
# RIMOZIONE COMPLETA DI 'positions.stock.cancelPosOda' dalla lista delle colonne
|
| 220 |
]
|
| 221 |
|
| 222 |
# Assicurati che il DataFrame abbia tutte le colonne nell'ordine desiderato
|
|
|
|
| 223 |
for col in final_columns_order:
|
| 224 |
if col not in final_df.columns:
|
| 225 |
final_df[col] = "" # Aggiungi colonne mancanti come vuote
|
|
|
|
| 232 |
'company.code': 15,
|
| 233 |
'supplier.partnerRef.code': 18,
|
| 234 |
'supplier.partnerRef.company.code': 25,
|
| 235 |
+
'docDate': 20,
|
| 236 |
'status': 10,
|
| 237 |
+
'positions.status': 12, # Larghezza per status
|
| 238 |
'positions.positionId': 12,
|
| 239 |
'positions.stock.item.itemNumber': 25,
|
| 240 |
'positions.stock.quantity': 10,
|
|
|
|
| 243 |
'positions.stock.accountingInfo.availability': 15,
|
| 244 |
'positions.stock.item.company.code': 25,
|
| 245 |
'positions.spare.string1': 15,
|
| 246 |
+
'positions.reason.code': 15,
|
| 247 |
'positions.reason.company.code': 25,
|
| 248 |
+
# RIMOZIONE COMPLETA DI 'positions.stock.cancelPosOda' dalla lista delle larghezze
|
| 249 |
}
|
| 250 |
|
| 251 |
# Scrivi i nomi delle colonne nella riga 2 (indice 1 in xlsxwriter)
|
|
|
|
| 259 |
worksheet.set_column(col_num, col_num, 15) # Larghezza di default
|
| 260 |
|
| 261 |
# Scrivi i dati a partire dalla riga 3 (indice 2 in xlsxwriter)
|
|
|
|
| 262 |
final_df.to_excel(writer, sheet_name=sheet_name_excel, index=False, header=False, startrow=2)
|
| 263 |
|
| 264 |
writer.close()
|
|
|
|
| 278 |
else:
|
| 279 |
st.info("Nessun dato valido estratto dai file selezionati.")
|
| 280 |
else:
|
| 281 |
+
st.info("Carica i tuoi file JSON per iniziare. I file devono essere della stessa tipologia!")
|
| 282 |
|
| 283 |
# --- Funzione per il reset ---
|
| 284 |
def clear_inputs():
|
| 285 |
+
st.session_state.file_uploader_key += 1
|
| 286 |
|
| 287 |
st.markdown("---")
|
| 288 |
st.button("Pulisci Input (RESET)", on_click=clear_inputs)
|