Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +67 -64
src/streamlit_app.py
CHANGED
|
@@ -63,15 +63,15 @@ def convert_json_to_excel_data_outbound(json_file_content: str, customers_df: pd
|
|
| 63 |
order_type_json = outbound_info.get('orderType')
|
| 64 |
delivery_type_fixed = "DELIVERY" # Valore fisso
|
| 65 |
|
| 66 |
-
order_doc_date_str = outbound_info.get('orderDocDate')
|
| 67 |
-
|
| 68 |
-
due_date_str = outbound_info.get('orderDate') #
|
| 69 |
|
| 70 |
reason_code = outbound_info.get('reasonCode')
|
| 71 |
-
service_fixed = "PDB" #
|
| 72 |
-
service_type_fixed = "Standard" #
|
| 73 |
|
| 74 |
-
payment_info_value = outbound_info.get('paymentInfo', {}).get('value')
|
| 75 |
|
| 76 |
delivery_customer = outbound_info.get('deliveryCustomer', {})
|
| 77 |
customer_code = delivery_customer.get('code')
|
|
@@ -79,7 +79,12 @@ def convert_json_to_excel_data_outbound(json_file_content: str, customers_df: pd
|
|
| 79 |
|
| 80 |
# Consignee data (from JSON)
|
| 81 |
consignee_name = delivery_customer.get('name')
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
consignee_city = delivery_customer.get('city')
|
| 84 |
consignee_province = delivery_customer.get('province')
|
| 85 |
consignee_zip_code = delivery_customer.get('zipCode')
|
|
@@ -87,7 +92,7 @@ def convert_json_to_excel_data_outbound(json_file_content: str, customers_df: pd
|
|
| 87 |
|
| 88 |
# Customer data (from Excel lookup)
|
| 89 |
customer_name = ""
|
| 90 |
-
customer_address = ""
|
| 91 |
customer_city = ""
|
| 92 |
customer_province = ""
|
| 93 |
customer_zip_code = ""
|
|
@@ -95,18 +100,21 @@ def convert_json_to_excel_data_outbound(json_file_content: str, customers_df: pd
|
|
| 95 |
|
| 96 |
# Esegui la lookup solo se customers_df è fornito e non vuoto
|
| 97 |
if customers_df is not None and not customers_df.empty and customer_code and company_code_json:
|
| 98 |
-
# Filtra per
|
| 99 |
-
# Assicurati che le colonne di join siano dello stesso tipo, convertendo a str
|
| 100 |
customer_data = customers_df[
|
| 101 |
-
(customers_df['
|
| 102 |
(customers_df['company.code'].astype(str) == str(company_code_json))
|
| 103 |
]
|
| 104 |
|
| 105 |
if not customer_data.empty:
|
| 106 |
-
# Prendiamo la prima riga se ce ne sono più di una
|
| 107 |
customer_row = customer_data.iloc[0]
|
| 108 |
customer_name = customer_row.get('partner.name', '')
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
customer_city = customer_row.get('partner.address.city', '')
|
| 111 |
customer_province = customer_row.get('partner.address.province', '')
|
| 112 |
customer_zip_code = customer_row.get('partner.address.zipCode', '')
|
|
@@ -115,27 +123,27 @@ def convert_json_to_excel_data_outbound(json_file_content: str, customers_df: pd
|
|
| 115 |
st.warning(f"Attenzione: Dettagli cliente non trovati nell'Excel per Code: '{customer_code}' e Company: '{company_code_json}'.")
|
| 116 |
|
| 117 |
# Note fields
|
| 118 |
-
note_ddt = outbound_info.get('noteDdt')
|
| 119 |
-
note_warehouse = outbound_info.get('noteWarehouse')
|
|
|
|
| 120 |
note_delivery = outbound_info.get('noteDelivery')
|
| 121 |
note_shipment = outbound_info.get('noteShipment')
|
| 122 |
reason_code_shp = outbound_info.get('reasonCodeShp')
|
| 123 |
info_delivery = outbound_info.get('infoDelivery')
|
| 124 |
booking_info = outbound_info.get('bookingInfo')
|
| 125 |
|
| 126 |
-
# Formattazione delle date
|
| 127 |
-
|
| 128 |
if order_doc_date_str:
|
| 129 |
try:
|
| 130 |
-
# Rimuovi 'Z' se presente e converti a datetime senza info fuso orario, poi formatta
|
| 131 |
dt_object = datetime.fromisoformat(order_doc_date_str.replace('Z', ''))
|
| 132 |
-
|
| 133 |
except ValueError:
|
| 134 |
-
|
| 135 |
st.warning(f"Attenzione: Impossibile parsare 'orderDocDate' '{order_doc_date_str}'. Verrà usata la stringa originale.")
|
| 136 |
|
| 137 |
due_date_formatted = None
|
| 138 |
-
if due_date_str:
|
| 139 |
try:
|
| 140 |
dt_object = datetime.fromisoformat(due_date_str.replace('Z', ''))
|
| 141 |
due_date_formatted = dt_object.strftime('%d/%m/%Y')
|
|
@@ -153,38 +161,38 @@ def convert_json_to_excel_data_outbound(json_file_content: str, customers_df: pd
|
|
| 153 |
'orderNumber': order_number,
|
| 154 |
'orderType': order_type_json,
|
| 155 |
'deliveryType': delivery_type_fixed,
|
| 156 |
-
'
|
| 157 |
-
'
|
| 158 |
'dueDate': due_date_formatted,
|
| 159 |
'reasonCode': reason_code,
|
| 160 |
-
'service': service_fixed,
|
| 161 |
-
'serviceType': service_type_fixed,
|
| 162 |
-
'
|
| 163 |
'customer.code': customer_code,
|
| 164 |
'customer.name': customer_name, # Da Excel
|
| 165 |
-
'customer.address': customer_address, # Da Excel
|
| 166 |
'customer.city': customer_city, # Da Excel
|
| 167 |
'customer.province': customer_province, # Da Excel
|
| 168 |
'customer.zipCode': customer_zip_code, # Da Excel
|
| 169 |
'customer.country': customer_country, # Da Excel
|
| 170 |
'consignee.name': consignee_name, # Dal JSON
|
| 171 |
-
'consignee.address': consignee_address, # Dal JSON
|
| 172 |
'consignee.city': consignee_city, # Dal JSON
|
| 173 |
'consignee.province': consignee_province, # Dal JSON
|
| 174 |
'consignee.zipCode': consignee_zip_code, # Dal JSON
|
| 175 |
'consignee.country': consignee_country, # Dal JSON
|
| 176 |
'customer.mail': customer_mail, # Dal JSON
|
| 177 |
'details.positionId': item.get('positionId'),
|
| 178 |
-
'details.itemNumber': item.get('itemCode'),
|
| 179 |
-
'details.requestedQt': item.get('quantity'),
|
| 180 |
'details.lotId': item.get('lotId'),
|
| 181 |
'details.stockOwner': item.get('stockOwner'),
|
| 182 |
-
'details.magCli': item.get('warehouse'),
|
| 183 |
'details.availability': item.get('availability'),
|
| 184 |
'details.accountingStatus': item.get('accountingStatus'),
|
| 185 |
'details.refOrder': item.get('refOrder'),
|
| 186 |
-
'
|
| 187 |
-
'
|
| 188 |
'noteDelivery': note_delivery,
|
| 189 |
'noteShipment': note_shipment,
|
| 190 |
'reasonCodeShp': reason_code_shp,
|
|
@@ -213,11 +221,12 @@ st.title("Convertitore JSON Outbound a Template Excel Outbound")
|
|
| 213 |
st.write("Carica i tuoi file JSON degli ordini Outbound e un file Excel con i dati dei clienti.")
|
| 214 |
|
| 215 |
st.header("1. Carica il file Excel dei Clienti")
|
| 216 |
-
st.info("Questo file Excel deve contenere le colonne: `
|
| 217 |
uploaded_customers_file = st.file_uploader(
|
| 218 |
"Scegli il file Excel con i dettagli dei Clienti",
|
| 219 |
type=["xlsx", "xls"],
|
| 220 |
-
|
|
|
|
| 221 |
)
|
| 222 |
|
| 223 |
customers_df = pd.DataFrame()
|
|
@@ -225,7 +234,7 @@ if uploaded_customers_file:
|
|
| 225 |
try:
|
| 226 |
customers_df = pd.read_excel(uploaded_customers_file)
|
| 227 |
# Assicurati che le colonne per il merge siano stringhe per evitare problemi di tipo
|
| 228 |
-
customers_df['
|
| 229 |
customers_df['company.code'] = customers_df['company.code'].astype(str)
|
| 230 |
st.success("File Excel Clienti caricato con successo.")
|
| 231 |
except Exception as e:
|
|
@@ -237,7 +246,7 @@ uploaded_json_files = st.file_uploader(
|
|
| 237 |
"Scegli uno o più file JSON di tipo 'Ordine_Outbound'",
|
| 238 |
type=["json"],
|
| 239 |
accept_multiple_files=True,
|
| 240 |
-
key=st.session_state.file_uploader_key_json
|
| 241 |
)
|
| 242 |
|
| 243 |
all_processed_data = []
|
|
@@ -281,13 +290,14 @@ if uploaded_json_files and not customers_df.empty:
|
|
| 281 |
# Definizione delle colonne di testata
|
| 282 |
header_columns = [
|
| 283 |
'siteCode', 'companyCode', 'orderNumber', 'orderType', 'deliveryType',
|
| 284 |
-
'
|
| 285 |
-
'service', 'serviceType',
|
|
|
|
| 286 |
'customer.code', 'customer.name', 'customer.address', 'customer.city',
|
| 287 |
'customer.province', 'customer.zipCode', 'customer.country',
|
| 288 |
'consignee.name', 'consignee.address', 'consignee.city', 'consignee.province',
|
| 289 |
'consignee.zipCode', 'consignee.country', 'customer.mail',
|
| 290 |
-
'
|
| 291 |
'reasonCodeShp', 'infoDelivery', 'bookingInfo'
|
| 292 |
]
|
| 293 |
|
|
@@ -307,16 +317,14 @@ if uploaded_json_files and not customers_df.empty:
|
|
| 307 |
sheet_name_excel = 'OutboundOrder'
|
| 308 |
|
| 309 |
workbook = writer.book
|
| 310 |
-
|
| 311 |
-
worksheet = writer.sheets.get(sheet_name_excel)
|
| 312 |
-
if worksheet is None:
|
| 313 |
-
worksheet = workbook.add_worksheet(sheet_name_excel)
|
| 314 |
|
| 315 |
# Lista delle colonne finali nell'ordine desiderato
|
| 316 |
final_columns_order = [
|
| 317 |
'siteCode', 'companyCode', 'orderNumber', 'orderType', 'deliveryType',
|
| 318 |
-
'
|
| 319 |
-
'service', 'serviceType',
|
|
|
|
| 320 |
'customer.code', 'customer.name', 'customer.address', 'customer.city',
|
| 321 |
'customer.province', 'customer.zipCode', 'customer.country',
|
| 322 |
'consignee.name', 'consignee.address', 'consignee.city', 'consignee.province',
|
|
@@ -324,21 +332,24 @@ if uploaded_json_files and not customers_df.empty:
|
|
| 324 |
'details.positionId', 'details.itemNumber', 'details.requestedQt',
|
| 325 |
'details.lotId', 'details.stockOwner', 'details.magCli', 'details.availability',
|
| 326 |
'details.accountingStatus', 'details.refOrder',
|
| 327 |
-
'
|
| 328 |
'reasonCodeShp', 'infoDelivery', 'bookingInfo'
|
| 329 |
]
|
| 330 |
|
| 331 |
# Assicurati che il DataFrame abbia tutte le colonne nell'ordine desiderato
|
|
|
|
| 332 |
for col in final_columns_order:
|
| 333 |
if col not in final_df.columns:
|
| 334 |
final_df[col] = ""
|
|
|
|
| 335 |
final_df = final_df[final_columns_order]
|
| 336 |
|
| 337 |
# Larghezze delle colonne aggiornate (adattate per Outbound)
|
| 338 |
col_widths = {
|
| 339 |
'siteCode': 10, 'companyCode': 15, 'orderNumber': 15, 'orderType': 10, 'deliveryType': 12,
|
| 340 |
-
'
|
| 341 |
-
'service': 10, 'serviceType': 12,
|
|
|
|
| 342 |
'customer.code': 15, 'customer.name': 30, 'customer.address': 30, 'customer.city': 15,
|
| 343 |
'customer.province': 10, 'customer.zipCode': 10, 'customer.country': 10,
|
| 344 |
'consignee.name': 30, 'consignee.address': 30, 'consignee.city': 15, 'consignee.province': 10,
|
|
@@ -346,7 +357,7 @@ if uploaded_json_files and not customers_df.empty:
|
|
| 346 |
'details.positionId': 12, 'details.itemNumber': 20, 'details.requestedQt': 15,
|
| 347 |
'details.lotId': 10, 'details.stockOwner': 15, 'details.magCli': 15, 'details.availability': 15,
|
| 348 |
'details.accountingStatus': 18, 'details.refOrder': 15,
|
| 349 |
-
'
|
| 350 |
'reasonCodeShp': 15, 'infoDelivery': 20, 'bookingInfo': 20
|
| 351 |
}
|
| 352 |
|
|
@@ -363,12 +374,11 @@ if uploaded_json_files and not customers_df.empty:
|
|
| 363 |
else:
|
| 364 |
worksheet.set_column(col_num, col_num, 15)
|
| 365 |
|
| 366 |
-
# Applica il formato data alle colonne '
|
| 367 |
-
if col_name in ['
|
| 368 |
worksheet.set_column(col_num, col_num, col_widths[col_name], date_format)
|
| 369 |
|
| 370 |
# Scrivi i dati a partire dalla riga 3 (indice 2 in xlsxwriter)
|
| 371 |
-
# NOTA: header=False perché li abbiamo già scritti noi nella riga 2
|
| 372 |
final_df.to_excel(writer, sheet_name=sheet_name_excel, index=False, header=False, startrow=2)
|
| 373 |
|
| 374 |
writer.close()
|
|
@@ -392,17 +402,10 @@ if uploaded_json_files and not customers_df.empty:
|
|
| 392 |
|
| 393 |
# --- Funzione per il reset ---
|
| 394 |
def clear_inputs():
|
| 395 |
-
#
|
| 396 |
-
#
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
# Un reset più semplice e sicuro è usare una chiave che cambia ogni volta, ad esempio con il tempo corrente.
|
| 400 |
-
# Tuttavia, per il reset del file_uploader, Streamlit richiede un cambio della 'key'.
|
| 401 |
-
# Una pratica comune è resettare la chiave con un nuovo valore statico o una funzione che genera una nuova chiave.
|
| 402 |
-
# Per semplicità e robustezza, userò un approccio che "toggle" il valore o lo cambia.
|
| 403 |
-
# L'errore originale era '0', quindi ora usiamo stringhe diverse.
|
| 404 |
-
st.session_state.file_uploader_key_json = 'json_uploader_reset'
|
| 405 |
-
st.session_state.file_uploader_key_customers = 'customers_uploader_reset'
|
| 406 |
|
| 407 |
st.markdown("---")
|
| 408 |
st.button("Pulisci Input (RESET)", on_click=clear_inputs)
|
|
|
|
| 63 |
order_type_json = outbound_info.get('orderType')
|
| 64 |
delivery_type_fixed = "DELIVERY" # Valore fisso
|
| 65 |
|
| 66 |
+
order_doc_date_str = outbound_info.get('orderDocDate') # JSON field name
|
| 67 |
+
date_detail_json = outbound_info.get('orderDateDetail') # JSON field name, becomes 'dateDetail'
|
| 68 |
+
due_date_str = outbound_info.get('orderDate') # JSON field name, becomes 'dueDate'
|
| 69 |
|
| 70 |
reason_code = outbound_info.get('reasonCode')
|
| 71 |
+
# service_fixed = "PDB" # Rimosso
|
| 72 |
+
# service_type_fixed = "Standard" # Rimosso
|
| 73 |
|
| 74 |
+
payment_info_value = outbound_info.get('paymentInfo', {}).get('value') # JSON field name
|
| 75 |
|
| 76 |
delivery_customer = outbound_info.get('deliveryCustomer', {})
|
| 77 |
customer_code = delivery_customer.get('code')
|
|
|
|
| 79 |
|
| 80 |
# Consignee data (from JSON)
|
| 81 |
consignee_name = delivery_customer.get('name')
|
| 82 |
+
consignee_address_base = delivery_customer.get('address', '')
|
| 83 |
+
consignee_address_number = delivery_customer.get('addressNumber', '') # Nuovo campo da JSON
|
| 84 |
+
|
| 85 |
+
# Concatenazione indirizzo e civico per consignee.address
|
| 86 |
+
consignee_address = f"{consignee_address_base} {consignee_address_number}".strip() if consignee_address_base or consignee_address_number else ""
|
| 87 |
+
|
| 88 |
consignee_city = delivery_customer.get('city')
|
| 89 |
consignee_province = delivery_customer.get('province')
|
| 90 |
consignee_zip_code = delivery_customer.get('zipCode')
|
|
|
|
| 92 |
|
| 93 |
# Customer data (from Excel lookup)
|
| 94 |
customer_name = ""
|
| 95 |
+
customer_address = "" # Questo sarà concatenazione
|
| 96 |
customer_city = ""
|
| 97 |
customer_province = ""
|
| 98 |
customer_zip_code = ""
|
|
|
|
| 100 |
|
| 101 |
# Esegui la lookup solo se customers_df è fornito e non vuoto
|
| 102 |
if customers_df is not None and not customers_df.empty and customer_code and company_code_json:
|
| 103 |
+
# Filtra per 'code' e 'company.code' (assicurandosi che siano stringhe)
|
|
|
|
| 104 |
customer_data = customers_df[
|
| 105 |
+
(customers_df['code'].astype(str) == str(customer_code)) & # Modificato da 'Code' a 'code'
|
| 106 |
(customers_df['company.code'].astype(str) == str(company_code_json))
|
| 107 |
]
|
| 108 |
|
| 109 |
if not customer_data.empty:
|
|
|
|
| 110 |
customer_row = customer_data.iloc[0]
|
| 111 |
customer_name = customer_row.get('partner.name', '')
|
| 112 |
+
|
| 113 |
+
# Concatenazione partner.address.address e partner.address.addressNumber
|
| 114 |
+
excel_customer_address_base = customer_row.get('partner.address.address', '')
|
| 115 |
+
excel_customer_address_number = customer_row.get('partner.address.addressNumber', '')
|
| 116 |
+
customer_address = f"{excel_customer_address_base} {excel_customer_address_number}".strip() if excel_customer_address_base or excel_customer_address_number else ""
|
| 117 |
+
|
| 118 |
customer_city = customer_row.get('partner.address.city', '')
|
| 119 |
customer_province = customer_row.get('partner.address.province', '')
|
| 120 |
customer_zip_code = customer_row.get('partner.address.zipCode', '')
|
|
|
|
| 123 |
st.warning(f"Attenzione: Dettagli cliente non trovati nell'Excel per Code: '{customer_code}' e Company: '{company_code_json}'.")
|
| 124 |
|
| 125 |
# Note fields
|
| 126 |
+
note_ddt = outbound_info.get('noteDdt') # JSON field name
|
| 127 |
+
note_warehouse = outbound_info.get('noteWarehouse') # JSON field name
|
| 128 |
+
|
| 129 |
note_delivery = outbound_info.get('noteDelivery')
|
| 130 |
note_shipment = outbound_info.get('noteShipment')
|
| 131 |
reason_code_shp = outbound_info.get('reasonCodeShp')
|
| 132 |
info_delivery = outbound_info.get('infoDelivery')
|
| 133 |
booking_info = outbound_info.get('bookingInfo')
|
| 134 |
|
| 135 |
+
# Formattazione delle date
|
| 136 |
+
order_date_formatted = None # Cambiato nome variabile
|
| 137 |
if order_doc_date_str:
|
| 138 |
try:
|
|
|
|
| 139 |
dt_object = datetime.fromisoformat(order_doc_date_str.replace('Z', ''))
|
| 140 |
+
order_date_formatted = dt_object.strftime('%d/%m/%Y')
|
| 141 |
except ValueError:
|
| 142 |
+
order_date_formatted = order_doc_date_str
|
| 143 |
st.warning(f"Attenzione: Impossibile parsare 'orderDocDate' '{order_doc_date_str}'. Verrà usata la stringa originale.")
|
| 144 |
|
| 145 |
due_date_formatted = None
|
| 146 |
+
if due_date_str:
|
| 147 |
try:
|
| 148 |
dt_object = datetime.fromisoformat(due_date_str.replace('Z', ''))
|
| 149 |
due_date_formatted = dt_object.strftime('%d/%m/%Y')
|
|
|
|
| 161 |
'orderNumber': order_number,
|
| 162 |
'orderType': order_type_json,
|
| 163 |
'deliveryType': delivery_type_fixed,
|
| 164 |
+
'orderDate': order_date_formatted, # Cambiato nome colonna
|
| 165 |
+
'dateDetail': date_detail_json, # Cambiato nome colonna
|
| 166 |
'dueDate': due_date_formatted,
|
| 167 |
'reasonCode': reason_code,
|
| 168 |
+
# 'service': service_fixed, # Rimosso
|
| 169 |
+
# 'serviceType': service_type_fixed, # Rimosso
|
| 170 |
+
'paymentValue': payment_info_value, # Cambiato nome colonna
|
| 171 |
'customer.code': customer_code,
|
| 172 |
'customer.name': customer_name, # Da Excel
|
| 173 |
+
'customer.address': customer_address, # Da Excel (concatenato)
|
| 174 |
'customer.city': customer_city, # Da Excel
|
| 175 |
'customer.province': customer_province, # Da Excel
|
| 176 |
'customer.zipCode': customer_zip_code, # Da Excel
|
| 177 |
'customer.country': customer_country, # Da Excel
|
| 178 |
'consignee.name': consignee_name, # Dal JSON
|
| 179 |
+
'consignee.address': consignee_address, # Dal JSON (concatenato)
|
| 180 |
'consignee.city': consignee_city, # Dal JSON
|
| 181 |
'consignee.province': consignee_province, # Dal JSON
|
| 182 |
'consignee.zipCode': consignee_zip_code, # Dal JSON
|
| 183 |
'consignee.country': consignee_country, # Dal JSON
|
| 184 |
'customer.mail': customer_mail, # Dal JSON
|
| 185 |
'details.positionId': item.get('positionId'),
|
| 186 |
+
'details.itemNumber': item.get('itemCode'),
|
| 187 |
+
'details.requestedQt': item.get('quantity'),
|
| 188 |
'details.lotId': item.get('lotId'),
|
| 189 |
'details.stockOwner': item.get('stockOwner'),
|
| 190 |
+
'details.magCli': item.get('warehouse'),
|
| 191 |
'details.availability': item.get('availability'),
|
| 192 |
'details.accountingStatus': item.get('accountingStatus'),
|
| 193 |
'details.refOrder': item.get('refOrder'),
|
| 194 |
+
'noteDDT': note_ddt, # Cambiato nome colonna
|
| 195 |
+
'noteMag': note_warehouse, # Cambiato nome colonna
|
| 196 |
'noteDelivery': note_delivery,
|
| 197 |
'noteShipment': note_shipment,
|
| 198 |
'reasonCodeShp': reason_code_shp,
|
|
|
|
| 221 |
st.write("Carica i tuoi file JSON degli ordini Outbound e un file Excel con i dati dei clienti.")
|
| 222 |
|
| 223 |
st.header("1. Carica il file Excel dei Clienti")
|
| 224 |
+
st.info("Questo file Excel deve contenere le colonne: `code`, `company.code`, `partner.name`, `partner.address.address`, `partner.address.addressNumber`, `partner.address.city`, `partner.address.province`, `partner.address.zipCode`, `partner.address.country`.")
|
| 225 |
uploaded_customers_file = st.file_uploader(
|
| 226 |
"Scegli il file Excel con i dettagli dei Clienti",
|
| 227 |
type=["xlsx", "xls"],
|
| 228 |
+
accept_multiple_files=False, # Limitato a un solo file
|
| 229 |
+
key=st.session_state.file_uploader_key_customers
|
| 230 |
)
|
| 231 |
|
| 232 |
customers_df = pd.DataFrame()
|
|
|
|
| 234 |
try:
|
| 235 |
customers_df = pd.read_excel(uploaded_customers_file)
|
| 236 |
# Assicurati che le colonne per il merge siano stringhe per evitare problemi di tipo
|
| 237 |
+
customers_df['code'] = customers_df['code'].astype(str) # Modificato da 'Code' a 'code'
|
| 238 |
customers_df['company.code'] = customers_df['company.code'].astype(str)
|
| 239 |
st.success("File Excel Clienti caricato con successo.")
|
| 240 |
except Exception as e:
|
|
|
|
| 246 |
"Scegli uno o più file JSON di tipo 'Ordine_Outbound'",
|
| 247 |
type=["json"],
|
| 248 |
accept_multiple_files=True,
|
| 249 |
+
key=st.session_state.file_uploader_key_json
|
| 250 |
)
|
| 251 |
|
| 252 |
all_processed_data = []
|
|
|
|
| 290 |
# Definizione delle colonne di testata
|
| 291 |
header_columns = [
|
| 292 |
'siteCode', 'companyCode', 'orderNumber', 'orderType', 'deliveryType',
|
| 293 |
+
'orderDate', 'dateDetail', 'dueDate', 'reasonCode',
|
| 294 |
+
# 'service', 'serviceType', # Rimosso
|
| 295 |
+
'paymentValue',
|
| 296 |
'customer.code', 'customer.name', 'customer.address', 'customer.city',
|
| 297 |
'customer.province', 'customer.zipCode', 'customer.country',
|
| 298 |
'consignee.name', 'consignee.address', 'consignee.city', 'consignee.province',
|
| 299 |
'consignee.zipCode', 'consignee.country', 'customer.mail',
|
| 300 |
+
'noteDDT', 'noteMag', 'noteDelivery', 'noteShipment', # Modificati
|
| 301 |
'reasonCodeShp', 'infoDelivery', 'bookingInfo'
|
| 302 |
]
|
| 303 |
|
|
|
|
| 317 |
sheet_name_excel = 'OutboundOrder'
|
| 318 |
|
| 319 |
workbook = writer.book
|
| 320 |
+
worksheet = workbook.add_worksheet(sheet_name_excel) # Creiamo sempre un nuovo worksheet
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
# Lista delle colonne finali nell'ordine desiderato
|
| 323 |
final_columns_order = [
|
| 324 |
'siteCode', 'companyCode', 'orderNumber', 'orderType', 'deliveryType',
|
| 325 |
+
'orderDate', 'dateDetail', 'dueDate', 'reasonCode',
|
| 326 |
+
# 'service', 'serviceType', # Rimosso
|
| 327 |
+
'paymentValue',
|
| 328 |
'customer.code', 'customer.name', 'customer.address', 'customer.city',
|
| 329 |
'customer.province', 'customer.zipCode', 'customer.country',
|
| 330 |
'consignee.name', 'consignee.address', 'consignee.city', 'consignee.province',
|
|
|
|
| 332 |
'details.positionId', 'details.itemNumber', 'details.requestedQt',
|
| 333 |
'details.lotId', 'details.stockOwner', 'details.magCli', 'details.availability',
|
| 334 |
'details.accountingStatus', 'details.refOrder',
|
| 335 |
+
'noteDDT', 'noteMag', 'noteDelivery', 'noteShipment', # Modificati
|
| 336 |
'reasonCodeShp', 'infoDelivery', 'bookingInfo'
|
| 337 |
]
|
| 338 |
|
| 339 |
# Assicurati che il DataFrame abbia tutte le colonne nell'ordine desiderato
|
| 340 |
+
# Se una colonna non esiste nel DataFrame, la aggiungiamo vuota
|
| 341 |
for col in final_columns_order:
|
| 342 |
if col not in final_df.columns:
|
| 343 |
final_df[col] = ""
|
| 344 |
+
# Riordina le colonne del DataFrame
|
| 345 |
final_df = final_df[final_columns_order]
|
| 346 |
|
| 347 |
# Larghezze delle colonne aggiornate (adattate per Outbound)
|
| 348 |
col_widths = {
|
| 349 |
'siteCode': 10, 'companyCode': 15, 'orderNumber': 15, 'orderType': 10, 'deliveryType': 12,
|
| 350 |
+
'orderDate': 12, 'dateDetail': 15, 'dueDate': 12, 'reasonCode': 12, # Date solo gg/mm/aaaa
|
| 351 |
+
# 'service': 10, 'serviceType': 12, # Rimosso
|
| 352 |
+
'paymentValue': 15, # Modificato
|
| 353 |
'customer.code': 15, 'customer.name': 30, 'customer.address': 30, 'customer.city': 15,
|
| 354 |
'customer.province': 10, 'customer.zipCode': 10, 'customer.country': 10,
|
| 355 |
'consignee.name': 30, 'consignee.address': 30, 'consignee.city': 15, 'consignee.province': 10,
|
|
|
|
| 357 |
'details.positionId': 12, 'details.itemNumber': 20, 'details.requestedQt': 15,
|
| 358 |
'details.lotId': 10, 'details.stockOwner': 15, 'details.magCli': 15, 'details.availability': 15,
|
| 359 |
'details.accountingStatus': 18, 'details.refOrder': 15,
|
| 360 |
+
'noteDDT': 20, 'noteMag': 20, 'noteDelivery': 20, 'noteShipment': 20, # Modificati
|
| 361 |
'reasonCodeShp': 15, 'infoDelivery': 20, 'bookingInfo': 20
|
| 362 |
}
|
| 363 |
|
|
|
|
| 374 |
else:
|
| 375 |
worksheet.set_column(col_num, col_num, 15)
|
| 376 |
|
| 377 |
+
# Applica il formato data alle colonne 'orderDate' e 'dueDate'
|
| 378 |
+
if col_name in ['orderDate', 'dueDate']: # Modificato 'orderDocDate' in 'orderDate'
|
| 379 |
worksheet.set_column(col_num, col_num, col_widths[col_name], date_format)
|
| 380 |
|
| 381 |
# Scrivi i dati a partire dalla riga 3 (indice 2 in xlsxwriter)
|
|
|
|
| 382 |
final_df.to_excel(writer, sheet_name=sheet_name_excel, index=False, header=False, startrow=2)
|
| 383 |
|
| 384 |
writer.close()
|
|
|
|
| 402 |
|
| 403 |
# --- Funzione per il reset ---
|
| 404 |
def clear_inputs():
|
| 405 |
+
# Per forzare il reset del file uploader, cambiamo la sua chiave
|
| 406 |
+
# Usiamo un timestamp per generare una chiave sempre nuova e unica
|
| 407 |
+
st.session_state.file_uploader_key_json = f'json_uploader_{datetime.now().timestamp()}'
|
| 408 |
+
st.session_state.file_uploader_key_customers = f'customers_uploader_{datetime.now().timestamp()}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
| 410 |
st.markdown("---")
|
| 411 |
st.button("Pulisci Input (RESET)", on_click=clear_inputs)
|