Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +56 -59
src/streamlit_app.py
CHANGED
|
@@ -16,8 +16,8 @@ fixed_and_default_values = {
|
|
| 16 |
'codice_indirizzo_mittente': '', # Campo 9
|
| 17 |
'ragione_sociale_mittente': 'Poste Logistics x Technolt', # Campo 10
|
| 18 |
'contatto_mittente': 'Poste Logistics', # Campo 11
|
| 19 |
-
'tipo_via_mittente': 'Viale', # Campo 12
|
| 20 |
-
'via_mittente': '
|
| 21 |
'numero_civico_mittente': '-', # Campo 14
|
| 22 |
'numero_telefono_mittente': '0000000000', # Campo 15
|
| 23 |
'cap_mittente': '27018', # Campo 16
|
|
@@ -35,43 +35,42 @@ fixed_and_default_values = {
|
|
| 35 |
# 'excel_col': Il nome esatto della colonna nel file Excel di origine.
|
| 36 |
# NOTA IMPORTANTE: i nomi delle colonne qui devono corrispondere ESATTAMENTE
|
| 37 |
# a come appaiono nelle intestazioni dei tuoi file Excel, inclusa la capitalizzazione.
|
| 38 |
-
# La 'start_pos' è
|
| 39 |
-
# per tutti i campi, per correggere la traslazione generale.
|
| 40 |
dat_format_spec = [
|
| 41 |
# Id | Nome | Obbl | Tipo | Lungh | Inizio | Descrizione | Default | Campi
|
| 42 |
-
{'id': '1', 'name': 'codice_identificativo_cliente', 'length': 12, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Valore costante che identifica il cliente, viene comunicato al cliente al momento dell'attivazione del servizio, un esempio di codice è CLI 000000009", 'start_pos':
|
| 43 |
-
{'id': '2', 'name': 'id_ldv', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'shippingMark', 'description': "Fillare con la numerazione della ldv (uguale a shippingMark)", 'start_pos':
|
| 44 |
-
{'id': '3', 'name': 'tipo_spedizione', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Indica il tipo di spedizione N Porto Franco P porto assegnato T triangolazione", 'start_pos':
|
| 45 |
-
{'id': '4', 'name': 'attivazione_ritiro', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Indica se deve essere attivato il ritiro. In assenza di accordi con Poste impostare il valore costante N", 'start_pos':
|
| 46 |
-
{'id': '5', 'name': 'stampa_ldv', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Mettere N", 'start_pos':
|
| 47 |
-
{'id': '6', 'name': 'numero_riferimento_spedizione', 'length': 25, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'orderRef', 'description': "Numero Rif. Ord. Cliente, è il riferimento della spedizione per il cliente", 'start_pos':
|
| 48 |
-
{'id': '7', 'name': 'data_spedizione', 'length': 8, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'requestedShipment', 'description': "Data della spedizione in formato AAAAMMGG (Mese, Giorno)", 'start_pos':
|
| 49 |
-
{'id': '8', 'name': 'ritiro_mp', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Indica il tipo di ritiro. In assenza di accordi con Poste impostare il valore costante P", 'start_pos':
|
| 50 |
-
{'id': '9', 'name': 'codice_indirizzo_mittente', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Identifica la codifica dell'indirizzo di ritiro del mittente", 'start_pos':
|
| 51 |
-
{'id': '10', 'name': 'ragione_sociale_mittente', 'length': 40, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 52 |
-
{'id': '11', 'name': 'contatto_mittente', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 53 |
-
{'id': '12', 'name': 'tipo_via_mittente', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 54 |
-
{'id': '13', 'name': 'via_mittente', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 55 |
-
{'id': '14', 'name': 'numero_civico_mittente', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 56 |
-
{'id': '15', 'name': 'numero_telefono_mittente', 'length': 15, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 57 |
-
{'id': '16', 'name': 'cap_mittente', 'length': 9, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 58 |
-
{'id': '17', 'name': 'localita_mittente', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 59 |
-
{'id': '18', 'name': 'provincia_mittente', 'length': 2, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos':
|
| 60 |
# Filler per il gap tra Campo 18 (fine a 254) e Campo 20 (inizio a 275)
|
| 61 |
-
{'id': '', 'name': 'filler_gap_18_20', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': False, 'source': 'filler', 'excel_col': None, 'description': "Gap tra i campi 18 e 20", 'start_pos':
|
| 62 |
# Campi Destinatario e Colli
|
| 63 |
# Nota: Questi campi si riferiscono a colonne nel file ShipmentOrder_, i nomi sono mantenuti come nel tracciato
|
| 64 |
-
{'id': '20', 'name': 'ragione_sociale_destinatario', 'length': 40, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.name', 'description': "Ragione sociale del destinatario", 'start_pos':
|
| 65 |
-
{'id': '21', 'name': 'contatto_destinatario', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.name', 'description': "Nome Contatto presso il Destinatario", 'start_pos':
|
| 66 |
-
{'id': '22', 'name': 'tipo_via_destinatario', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Descrizione tipo via", 'start_pos':
|
| 67 |
-
{'id': '23', 'name': 'via_destinatario', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.address', 'description': "Indirizzo del destinatario", 'start_pos':
|
| 68 |
-
{'id': '24', 'name': 'numero_civico_destinatario', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.addressNumber', 'description': "Numero Civico Destinatario allineato a sinistra", 'start_pos':
|
| 69 |
-
{'id': '25', 'name': 'numero_telefono_destinatario','length': 15, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.phoneNumber', 'description': "Numero di telefono Destinatario", 'start_pos':
|
| 70 |
-
{'id': '26', 'name': 'cap_destinatario', 'length': 9, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.zipCode', 'description': "CAP del destinatario allineato a sinistra", 'start_pos':
|
| 71 |
-
{'id': '27', 'name': 'localita_destinatario', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.city', 'description': "Località del destinatario", 'start_pos':
|
| 72 |
-
{'id': '28', 'name': 'provincia_destinatario', 'length': 2, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Provincia del destinatario", 'start_pos':
|
| 73 |
-
{'id': '29', 'name': 'numero_colli', 'length': 3, 'numeric_pad_zero': True, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'totalLdv', 'description': "Numero dei colli totali che compongono la spedizione", 'start_pos':
|
| 74 |
-
{'id': '30', 'name': 'peso_colli', 'length': 7, 'numeric_pad_zero': True, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'totalWeight', 'description': "Peso espresso in grammi del singolo collo", 'start_pos':
|
| 75 |
# Altri Campi Obbligatori dal tracciato
|
| 76 |
# Questi campi erano già correttamente allineati secondo il tuo feedback precedente.
|
| 77 |
{'id': '34', 'name': 'codice_servizio', 'length': 3, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Vedi codifica", 'start_pos': 456},
|
|
@@ -80,13 +79,6 @@ dat_format_spec = [
|
|
| 80 |
{'id': '49', 'name': 'tipo_cliente', 'length': 4, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Valore costante: MGCS", 'start_pos': 627},
|
| 81 |
]
|
| 82 |
|
| 83 |
-
# Lista dei nomi dei campi che sono affetti dalla traslazione di una posizione
|
| 84 |
-
# Questa lista non è più necessaria per una correzione specifica "by one" se
|
| 85 |
-
# la start_pos di tutti i campi è stata uniformemente decrementata di 1.
|
| 86 |
-
# La logica del padding dinamico e l'uso delle start_pos aggiornate dovrebbero gestire tutto.
|
| 87 |
-
FIELDS_TO_CORRECT_ALIGNMENT_BY_ONE = [] # Vuota, la correzione è globale nella dat_format_spec
|
| 88 |
-
|
| 89 |
-
|
| 90 |
# --- Funzione per generare il file DAT e DIS ---
|
| 91 |
def generate_poste_dat_file(df_shipping_mark_input, df_shipment_order_input):
|
| 92 |
"""
|
|
@@ -137,12 +129,19 @@ def generate_poste_dat_file(df_shipping_mark_input, df_shipment_order_input):
|
|
| 137 |
st.warning("Nessun record corrispondente trovato dopo la join. Assicurati che 'orderRef' sia presente e corrisponda in entrambi i file.")
|
| 138 |
return None, None
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
# Genera il contenuto del file .dat
|
| 141 |
dat_content = []
|
| 142 |
for index, row in df_merged.iterrows():
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
current_line_length = 0
|
| 146 |
|
| 147 |
for field_spec in dat_format_spec:
|
| 148 |
field_name = field_spec['name']
|
|
@@ -152,15 +151,8 @@ def generate_poste_dat_file(df_shipping_mark_input, df_shipment_order_input):
|
|
| 152 |
mandatory = field_spec['mandatory']
|
| 153 |
source_type = field_spec['source']
|
| 154 |
excel_col_name = field_spec['excel_col'] # Nome della colonna come specificato nel dat_format_spec
|
| 155 |
-
#
|
| 156 |
-
target_0_indexed_pos = field_spec['start_pos']
|
| 157 |
-
|
| 158 |
-
# Calcola il padding necessario per raggiungere la posizione di inizio esatta
|
| 159 |
-
# Questo aggiunge spazi solo se la riga attuale è prima della posizione desiderata.
|
| 160 |
-
padding_needed = target_0_indexed_pos - current_line_length
|
| 161 |
-
if padding_needed > 0:
|
| 162 |
-
record_parts.append(' ' * padding_needed)
|
| 163 |
-
current_line_length += padding_needed
|
| 164 |
|
| 165 |
value_to_format = ''
|
| 166 |
if source_type == 'fixed':
|
|
@@ -251,10 +243,16 @@ def generate_poste_dat_file(df_shipping_mark_input, df_shipment_order_input):
|
|
| 251 |
|
| 252 |
# Trunca se la stringa è più lunga della lunghezza specificata
|
| 253 |
final_field_value = formatted_value[:length]
|
| 254 |
-
record_parts.append(final_field_value)
|
| 255 |
-
current_line_length += len(final_field_value)
|
| 256 |
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
# Genera il nome del file con timestamp (uguale per .dat e .dis)
|
| 260 |
now = datetime.now()
|
|
@@ -303,11 +301,10 @@ def generate_dat_format_spec_text():
|
|
| 303 |
spec_text += "Id\tNome Campo\tObbligatorio\tTipo\tLunghezza\tInizio\tDescrizione\tDefault / Fonte Dati\n"
|
| 304 |
spec_text += "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
|
| 305 |
# Ricalcolo delle start_pos per la documentazione basata sulle lunghezze
|
| 306 |
-
current_start_pos_for_spec =
|
| 307 |
for field_spec in dat_format_spec:
|
| 308 |
# Assegna la posizione iniziale calcolata per l'output della documentazione
|
| 309 |
-
|
| 310 |
-
field_spec['start_pos_for_spec_output'] = current_start_pos_for_spec + 1
|
| 311 |
# Incrementa la posizione iniziale per il campo successivo
|
| 312 |
current_start_pos_for_spec += field_spec['length']
|
| 313 |
|
|
|
|
| 16 |
'codice_indirizzo_mittente': '', # Campo 9
|
| 17 |
'ragione_sociale_mittente': 'Poste Logistics x Technolt', # Campo 10
|
| 18 |
'contatto_mittente': 'Poste Logistics', # Campo 11
|
| 19 |
+
'tipo_via_mittente': 'Viale ', # Campo 12
|
| 20 |
+
'via_mittente': ' delle Industrie 9-11-13', # Campo 13 (Aggiornato con spazio iniziale)
|
| 21 |
'numero_civico_mittente': '-', # Campo 14
|
| 22 |
'numero_telefono_mittente': '0000000000', # Campo 15
|
| 23 |
'cap_mittente': '27018', # Campo 16
|
|
|
|
| 35 |
# 'excel_col': Il nome esatto della colonna nel file Excel di origine.
|
| 36 |
# NOTA IMPORTANTE: i nomi delle colonne qui devono corrispondere ESATTAMENTE
|
| 37 |
# a come appaiono nelle intestazioni dei tuoi file Excel, inclusa la capitalizzazione.
|
| 38 |
+
# La 'start_pos' qui è la posizione 1-indexed come nel tuo tracciato originale.
|
|
|
|
| 39 |
dat_format_spec = [
|
| 40 |
# Id | Nome | Obbl | Tipo | Lungh | Inizio | Descrizione | Default | Campi
|
| 41 |
+
{'id': '1', 'name': 'codice_identificativo_cliente', 'length': 12, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Valore costante che identifica il cliente, viene comunicato al cliente al momento dell'attivazione del servizio, un esempio di codice è CLI 000000009", 'start_pos': 1},
|
| 42 |
+
{'id': '2', 'name': 'id_ldv', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'shippingMark', 'description': "Fillare con la numerazione della ldv (uguale a shippingMark)", 'start_pos': 12},
|
| 43 |
+
{'id': '3', 'name': 'tipo_spedizione', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Indica il tipo di spedizione N Porto Franco P porto assegnato T triangolazione", 'start_pos': 42},
|
| 44 |
+
{'id': '4', 'name': 'attivazione_ritiro', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Indica se deve essere attivato il ritiro. In assenza di accordi con Poste impostare il valore costante N", 'start_pos': 43},
|
| 45 |
+
{'id': '5', 'name': 'stampa_ldv', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Mettere N", 'start_pos': 44},
|
| 46 |
+
{'id': '6', 'name': 'numero_riferimento_spedizione', 'length': 25, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'orderRef', 'description': "Numero Rif. Ord. Cliente, è il riferimento della spedizione per il cliente", 'start_pos': 45},
|
| 47 |
+
{'id': '7', 'name': 'data_spedizione', 'length': 8, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'requestedShipment', 'description': "Data della spedizione in formato AAAAMMGG (Mese, Giorno)", 'start_pos': 70},
|
| 48 |
+
{'id': '8', 'name': 'ritiro_mp', 'length': 1, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Indica il tipo di ritiro. In assenza di accordi con Poste impostare il valore costante P", 'start_pos': 78},
|
| 49 |
+
{'id': '9', 'name': 'codice_indirizzo_mittente', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Identifica la codifica dell'indirizzo di ritiro del mittente", 'start_pos': 79},
|
| 50 |
+
{'id': '10', 'name': 'ragione_sociale_mittente', 'length': 40, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 99},
|
| 51 |
+
{'id': '11', 'name': 'contatto_mittente', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 139},
|
| 52 |
+
{'id': '12', 'name': 'tipo_via_mittente', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 159},
|
| 53 |
+
{'id': '13', 'name': 'via_mittente', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 164},
|
| 54 |
+
{'id': '14', 'name': 'numero_civico_mittente', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 194},
|
| 55 |
+
{'id': '15', 'name': 'numero_telefono_mittente', 'length': 15, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 199},
|
| 56 |
+
{'id': '16', 'name': 'cap_mittente', 'length': 9, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 214},
|
| 57 |
+
{'id': '17', 'name': 'localita_mittente', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 223},
|
| 58 |
+
{'id': '18', 'name': 'provincia_mittente', 'length': 2, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Ignorato se è valorizzato il campo 9", 'start_pos': 253},
|
| 59 |
# Filler per il gap tra Campo 18 (fine a 254) e Campo 20 (inizio a 275)
|
| 60 |
+
{'id': '', 'name': 'filler_gap_18_20', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': False, 'source': 'filler', 'excel_col': None, 'description': "Gap tra i campi 18 e 20", 'start_pos': 255},
|
| 61 |
# Campi Destinatario e Colli
|
| 62 |
# Nota: Questi campi si riferiscono a colonne nel file ShipmentOrder_, i nomi sono mantenuti come nel tracciato
|
| 63 |
+
{'id': '20', 'name': 'ragione_sociale_destinatario', 'length': 40, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.name', 'description': "Ragione sociale del destinatario", 'start_pos': 275},
|
| 64 |
+
{'id': '21', 'name': 'contatto_destinatario', 'length': 20, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.name', 'description': "Nome Contatto presso il Destinatario", 'start_pos': 315},
|
| 65 |
+
{'id': '22', 'name': 'tipo_via_destinatario', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Descrizione tipo via", 'start_pos': 335},
|
| 66 |
+
{'id': '23', 'name': 'via_destinatario', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.address', 'description': "Indirizzo del destinatario", 'start_pos': 340},
|
| 67 |
+
{'id': '24', 'name': 'numero_civico_destinatario', 'length': 5, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.addressNumber', 'description': "Numero Civico Destinatario allineato a sinistra", 'start_pos': 370},
|
| 68 |
+
{'id': '25', 'name': 'numero_telefono_destinatario','length': 15, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.phoneNumber', 'description': "Numero di telefono Destinatario", 'start_pos': 375},
|
| 69 |
+
{'id': '26', 'name': 'cap_destinatario', 'length': 9, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.zipCode', 'description': "CAP del destinatario allineato a sinistra", 'start_pos': 390},
|
| 70 |
+
{'id': '27', 'name': 'localita_destinatario', 'length': 30, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'shipment_order_file', 'excel_col': 'consegnee.partner.address.city', 'description': "Località del destinatario", 'start_pos': 399},
|
| 71 |
+
{'id': '28', 'name': 'provincia_destinatario', 'length': 2, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Provincia del destinatario", 'start_pos': 429},
|
| 72 |
+
{'id': '29', 'name': 'numero_colli', 'length': 3, 'numeric_pad_zero': True, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'totalLdv', 'description': "Numero dei colli totali che compongono la spedizione", 'start_pos': 431},
|
| 73 |
+
{'id': '30', 'name': 'peso_colli', 'length': 7, 'numeric_pad_zero': True, 'decimals': 0, 'mandatory': True, 'source': 'shipping_mark_file', 'excel_col': 'totalWeight', 'description': "Peso espresso in grammi del singolo collo", 'start_pos': 434},
|
| 74 |
# Altri Campi Obbligatori dal tracciato
|
| 75 |
# Questi campi erano già correttamente allineati secondo il tuo feedback precedente.
|
| 76 |
{'id': '34', 'name': 'codice_servizio', 'length': 3, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Vedi codifica", 'start_pos': 456},
|
|
|
|
| 79 |
{'id': '49', 'name': 'tipo_cliente', 'length': 4, 'numeric_pad_zero': False, 'decimals': 0, 'mandatory': True, 'source': 'fixed', 'excel_col': None, 'description': "Valore costante: MGCS", 'start_pos': 627},
|
| 80 |
]
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
# --- Funzione per generare il file DAT e DIS ---
|
| 83 |
def generate_poste_dat_file(df_shipping_mark_input, df_shipment_order_input):
|
| 84 |
"""
|
|
|
|
| 129 |
st.warning("Nessun record corrispondente trovato dopo la join. Assicurati che 'orderRef' sia presente e corrisponda in entrambi i file.")
|
| 130 |
return None, None
|
| 131 |
|
| 132 |
+
# Calcola la lunghezza massima della riga per pre-allocare l'array di caratteri
|
| 133 |
+
max_line_length = 0
|
| 134 |
+
for field_spec in dat_format_spec:
|
| 135 |
+
# start_pos è 1-indexed, quindi field_spec['start_pos'] + field_spec['length'] - 1 dà l'indice finale 0-indexed.
|
| 136 |
+
# Aggiungiamo 1 per ottenere la lunghezza totale che l'array deve avere per contenere l'ultimo carattere.
|
| 137 |
+
max_line_length = max(max_line_length, field_spec['start_pos'] + field_spec['length'] - 1)
|
| 138 |
+
max_line_length += 1 # Converti da indice massimo a lunghezza totale
|
| 139 |
+
|
| 140 |
# Genera il contenuto del file .dat
|
| 141 |
dat_content = []
|
| 142 |
for index, row in df_merged.iterrows():
|
| 143 |
+
# Inizializza la riga con spazi per la lunghezza massima
|
| 144 |
+
line_chars = [' '] * max_line_length
|
|
|
|
| 145 |
|
| 146 |
for field_spec in dat_format_spec:
|
| 147 |
field_name = field_spec['name']
|
|
|
|
| 151 |
mandatory = field_spec['mandatory']
|
| 152 |
source_type = field_spec['source']
|
| 153 |
excel_col_name = field_spec['excel_col'] # Nome della colonna come specificato nel dat_format_spec
|
| 154 |
+
# Converte la start_pos da 1-indexed (nel tracciato) a 0-indexed (per array Python)
|
| 155 |
+
target_0_indexed_pos = field_spec['start_pos'] - 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
value_to_format = ''
|
| 158 |
if source_type == 'fixed':
|
|
|
|
| 243 |
|
| 244 |
# Trunca se la stringa è più lunga della lunghezza specificata
|
| 245 |
final_field_value = formatted_value[:length]
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
# Inserisci il valore formattato nella lista di caratteri alla posizione esatta
|
| 248 |
+
# Assicurati di non andare oltre la lunghezza massima della riga
|
| 249 |
+
for i, char in enumerate(final_field_value):
|
| 250 |
+
if target_0_indexed_pos + i < max_line_length:
|
| 251 |
+
line_chars[target_0_indexed_pos + i] = char
|
| 252 |
+
else:
|
| 253 |
+
break # Evita OutOfBoundsError se il campo è troppo lungo alla fine della riga
|
| 254 |
+
|
| 255 |
+
dat_content.append("".join(line_chars))
|
| 256 |
|
| 257 |
# Genera il nome del file con timestamp (uguale per .dat e .dis)
|
| 258 |
now = datetime.now()
|
|
|
|
| 301 |
spec_text += "Id\tNome Campo\tObbligatorio\tTipo\tLunghezza\tInizio\tDescrizione\tDefault / Fonte Dati\n"
|
| 302 |
spec_text += "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n"
|
| 303 |
# Ricalcolo delle start_pos per la documentazione basata sulle lunghezze
|
| 304 |
+
current_start_pos_for_spec = 1 # Inizia da 1 per la visualizzazione 1-indexed
|
| 305 |
for field_spec in dat_format_spec:
|
| 306 |
# Assegna la posizione iniziale calcolata per l'output della documentazione
|
| 307 |
+
field_spec['start_pos_for_spec_output'] = current_start_pos_for_spec
|
|
|
|
| 308 |
# Incrementa la posizione iniziale per il campo successivo
|
| 309 |
current_start_pos_for_spec += field_spec['length']
|
| 310 |
|