
    `je                    r   d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	mZm
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dlmZ d dlmZm Z m!Z!m"Z" d dl#m$Z$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4m5Z5 d dl6m7Z7m8Z8 d dl9m:Z: i dddddddddd d!dd"d#d$dd%d#d&d#d'd#d(dd)d#d*dd+d,d-d.d/d#dddd0d0d1d dd d2	Z;d3Z<g d4Z=g d5Z>ed6fgZ?d7d8d9d:Z@dad;ZAdbd<ZBdcddd=ZCded>ZDdfd?ZEdgd@ZFdadAZGdhdBZHdhdCZIdidDZJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 djdEZKdF ZL ed G      dH        ZMdkdIZNdJ ZOdldKZPdL ZQdkdMZRdmdNZSdkdOZTdndPZUdodQZVdpdRZWdqdSZXdrdTZYdsdUZZdsdVZ[dW Z\dtdXZ]dudYZ^dudZZ_dud[Z`dud\Za	 	 	 	 	 	 dvd]Zb	 dw	 	 	 	 	 	 	 dxd^Zcdyd_Zddyd`Zey)z    )annotationsN)Counterdefaultdict)datedatetime	timedelta)	lru_cache)BytesIO)Iterable)urlopen)ZipFile)IntegrityErrortransaction)timezone)Booking)Guest)ISTAT_NOT_SPECIFIEDISTAT_NOT_SPECIFIED_DISPLAY istat_not_specified_export_valuenormalize_guest_for_istat)normalize_municipality_nameresolve_residencederive_guest_type_codes)Property)IstatCountryIstatMunicipalityIstatProvinceIstatReservationPositionIstatTourismTypeIstatTransportType)PROVINCE_MAPPINGS)normalize_countryresolve_istat_country_code)generate_guest_nightswith_guest_night_prefetch)	Structure
guest_type   arrival_date
   surname2   name   gender   
birth_datebirth_municipality_code	   birth_province_codecountry_of_birth_codenationality_coderesidence_municipality_coderesidence_province_codecountry_codeaddressdocument_type   document_number   document_issue_place      )	departure_datetourism_typetransport_typerooms_occupiedrooms_availablebeds_availablecity_taxposition_codemodezkhttps://www.istat.it/wp-content/uploads/2024/03/Elenco-codici-e-denominazioni-unita-territoriali-estere.zip))Cultural	Culturale)BeachBalneare)zConference/BusinesszCongressuale/Affari)z
Trade Fair
Fieristico)zSport/FitnesszSportivo/Fitness)School
Scolastico)	Religious	Religioso)SocialSociale)zTheme ParkszParchi Tematici)zSpa/Health TreatmentszTermale/Trattamenti salute)zFood and WineEnogastronomico)zCycle TourismCicloturismo)zExcursion/NaturezEscursionistico/Naturalistico)zOther reasonzAltro motivozNot SpecifiedNon Specificato))CarAuto)PlaneAereo)zPlane + BuszAereo+Pullman)zPlane + Shuttle/Taxi/CarzAereo+Navetta/Taxi/Auto)zPlane + TrainzAereo+Treno)TrainTreno)BusPullman)CaravanzCaravan/Autocaravan)BoatzBarca/Nave/Traghetto)
MotorcycleMoto)Bicycle
Bicicletta)WalkingzA piedi)OtherzAltro mezzorY   rZ   country_of_birthnationalitycountry)birth_countrycitizenshipresidence_countryc                    | yt        |       j                  dd      j                  dd      j                  dd      j                         S )N  
asciiignore)strreplaceencodedecodevalues    )/backend/services/istat_export_service.py_asciir   ~   sD    }u:dC(00s;BB7HU\\^^    c                `    t        |       }t        |      |kD  r|d | }|j                  |      S N)r   lenljustr}   lengthraws      r~   
_pad_rightr      s1    
-C
3x&'6l99Vr   c                d    t        |       }t        |      |kD  r|| d  }|j                  ||      S r   )r   r   rjust)r}   r   padr   s       r~   	_pad_leftr      s5    
-C
3x&6'(m99VS!!r   c                L   | syt        | t              r| j                         } t        | t              r| j                  d      S t        | t              rA| j                         }|sydD ])  }	 t        j                  ||      j                  d      c S  y# t        $ r Y 8w xY w)Nrr   %d/%m/%Y)z%Y-%m-%dr   )
isinstancer   r   strftimerx   stripstrptime
ValueError)r}   r   fmts      r~   _format_dater      s    %"

%~~j))%kkm+C((c2;;JGG ,
   s   .$B	B#"B#c                   t        | j                  t              r| j                  ni }|j                  d      xs dj	                         }|j                  d      xs dj	                         }|s|r||fS | j
                  xs dj	                         }|sy|j                         }t        |      dk(  r|d   dfS dj                  |dd        |d   fS )N
first_namerr   	last_name)rr   rr   r1   r   rt   )	r   
extra_datadictgetr   	full_namesplitr   join)guestextrar   r   fullpartss         r~   _split_namer      s     *5+;+;T BEE))L)/R668J;'-2446IY*$$OO!r((*DJJLE
5zQQx|88E!"Ia((r   c                   t        | t              r| S t        | t              r>| j                         }|si S 	 t	        j
                  |      }t        |t              r|S i S i S # t        t        t        j                  f$ r i cY S w xY wr   )	r   r   rx   r   jsonloads	TypeErrorr   JSONDecodeError)r}   r   parseds      r~   _coerce_extra_datar      s~    %%kkmI	ZZ_F $FD1v9r9I :t';';< 	I	s   A# # BBc                P    t        |       j                         j                         S r   )r   r   lowerr|   s    r~   
_normalizer      s    %= &&((r   c                V    i }| D ]!  \  }}||t        |      <   ||t        |      <   # |S r   r   )pairsmappingenglish_valueitalian_values       r~   _build_value_mapr      s;     G(-$}-:
=)*-:
=)* ). Nr   c                &    t        t        | z         S r   )r   NOT_SPECIFIED_VALUE_PAIRS)r   s    r~   _build_istat_choice_value_mapr      s    5=>>r   c                .    t         j                  | |       S r   )ERROR_KEY_ALIASESr   keys    r~   _normalize_error_keyr      s      c**r   c                   | sy t        |       j                         }|sy |j                         r|j                  d      }t	        |      }|j                  ||       |j                         r%|j                  |j                  d      xs d|       |D ]&  }	t	        |	xs d      }
|
s|j                  |
|       ( d}|D ]/  }t	        |xs d      }|s|j                  ||       |r*|xs d}1 |r|j                  ||       y y )Nr4   0rr   )rx   r   isdigitzfillr   
setdefaultlstrip)codenames	iso_codesby_codeby_isoby_namename_by_codecanonical_codenormalized_codeiso_codenormalized_isodisplay_namer.   normalized_names                 r~   _register_country_entryr      s    Y__&N'--a0 0O7 ?11#6=#~N#HN3nn= 
 L$TZR0?N;:2L  > r   c            
         i } i }i }i }t         j                  j                         D ]4  }t        |j                  |j
                  f|j                  f| |||       6 | |||fS )Nr   r   r   r   r   r   r   )r   objectsallr   r   r.   r   )r   r   r   r   rm   s        r~   _build_country_lookupr     sp    GFGL''++-<</'')%	
 . FG\11r   )maxsizec            
        	 t        t        d      5 } t        t        | j	                                     }d d d        t        d j                         D        d       }|si i i i fS 	 |j	                  |      j                  d      }i }i }i }i }t        j                  |j                         d	      }|D ]  }	|	j                  d
      xs dj                         }
|	j                  d      xs dj                         }|	j                  d      xs dj                         }|	j                  d      xs dj                         }|	j                  d      xs dj                         }|
r|s|st        |
||f||f||||        ||||fS # 1 sw Y   ZxY w# t
        $ r	 i i i i fcY S w xY w# t        $ r& |j	                  |      j                  dd      }Y Vw xY w)Nr+   )timeoutc              3  `   K   | ]&  }|j                         j                  d       s#| ( yw)z.csvN)r   endswith.0r.   s     r~   	<genexpr>z0_load_official_country_lookup.<locals>.<genexpr>  s&     N,$

0E0Ef0M,s   $..z	utf-8-siglatin1ry   )errors;)	delimiterzCodice ISTATrr   zDenominazione ITzDenominazione ENzCodice ISO 3166 alpha2zCodice ISO 3166 alpha3r   )r   ISTAT_COUNTRY_REFERENCE_URLr   r
   read	Exceptionnextnamelistr{   UnicodeDecodeErrorcsv
DictReader
splitlinesr   r   r   )responsearchivecsv_nametextr   r   r   r   readerrowr   name_itname_en
iso_alpha2
iso_alpha3s                  r~   _load_official_country_lookupr     s   0"=ghmmo67G >
 N'**,NH 2r2~I||H%,,[9 GFGL^^DOO-=F'-244677-.4";;=77-.4";;=gg67=2DDF
gg67=2DDF
GwG$!:.%	
 ( FG\11U >= 2r2~  I||H%,,Xi,HIs9   F #FF ( F0 FF F-,F-0+GGc                $   | sy t        |       }|r|S t        |       }|sy |\  }}}}|j                         r7|j                  |      xs  |j                  |j	                  d            }|r*|S |j                  |      xs |j                  |      }|r|S t               }	|	sy |	\  }
}}}|j                         r3|
j                  |      xs  |
j                  |j	                  d            S |j                  |      xs |j                  |      S )Nr4   )r$   r   r   r   r   r   )r}   lookupsnormalized_istat_coder   r   r   r   _resolvedfallbackfallback_by_codefallback_by_isofallback_by_names                r~   _resolve_country_coder   D  s   6u=$$
U
C")GVWa
{{};;s#@w{{399Q<'@O::c?6gkk#&6O,.H=E:o'7
{{}##C(N,<,@,@1,NNs#@'7';';C'@@r   c                 <   i } i }i }t         j                  j                  d      D ]  }|j                  rf|| t	        |j                        <   t        |j                        dk(  r6|j                  j                  d      r|| t	        |j                  d d       <   |j                  r||t        |j                        <   |j                  r|||j                  <   |j                  s|j                  s|j                  |t	        |j                        <    | ||fS )NT)	is_activer4   000   )r   r   filterr   r   r   r   r.   r   r   province)r   r   province_by_codemunicipalitys       r~   _build_municipality_lookupr  a  s   GG)118848H5AGJ|0012<$$%*|/@/@/I/I%/P=I
<#4#4Ra#89:FRG/0A0ABC''4@GL001!6!6>J>S>SZ(9(9:; I G---r   c                   | syt        |       }|sy|\  }}}|j                         rt        |      dk  r|j                  d      n|}|j                         rt        |      dk  r|j                  d      n|}|j                         r8||v s||v r0|j	                  |      xs ||   }|j
                  |j                  fS t        |       }	|	|v r||	   }|j
                  |j                  fS y)N)NNr4   r  )r   r   r   r   r   r   r  r   )
r}   r   r   r   r   r  code_keyshort_code_keyr  r   s
             r~   _resolve_municipalityr  s  s    
U
C)0&GW&"{{}SAsyy|3H%([[]s3x!|SYYq\N
{{}(g-71J{{8,G0G  ,"7"7771%8O'!/  ,"7"777r   c                    i } i }t         j                  j                         D ]_  }|j                  r"|j                  | t	        |j                        <   |j
                  s>|j                  |t	        |j
                        <   a | |fS r   )r   r   r   r   r   r.   )r   r   r  s      r~   _build_province_lookupr    sn    GG!))--/==19GJx}}-.==19GJx}}-.	 0
 Gr   c                b    | sy t        |       }|sy |\  }}||v r||   S |j                  |      S r   )r   r   )r}   r   r   r   r   s        r~   _resolve_province_coder    sB    
U
CGW
g~s|;;sr   c                    | syt        |       }|sy|\  }}}}||v r	||   xs dS t               }|sy|d   j                  |d      xs dS )Nrr   rA   )r   r   r   )r   r   r   r   r   r   s         r~   _country_name_for_coder    sf    
T
C#Aq!\
lC &B&,.HA;??3#)r)r   c                4    | sy t        |       }|dv ry|dv ryy )N>   mmalemaschio1>   ffemalefemmina2r   )r}   r   s     r~   _gender_coder    s-    
U
C
&&
((r   c                J    t        |       } | sg S | j                  d        | S )Nc                4    | j                    | j                  fS r   )is_main_guestid)gs    r~   <lambda>z!_ordered_guests.<locals>.<lambda>  s    1??2ADD9r   r   )listsort)guestss    r~   _ordered_guestsr&    s'    &\F	
KK9K:Mr   c                    t        | |      S r   r   )
group_typecounts     r~   _guest_type_codesr*    s    ":u55r   c                    | t        d|      S t        |       j                         }|st        d|      S t        ||d      S )Nrr   r   )r   )r   rx   r   r   r   s      r~   _format_numericr,    sE    }"f%%
e*


C"f%%S&c**r   c                x    | j                   r$| j                   dkD  rt        | j                         S t        |      S Nr   )total_unitsintr   )	structure
propertiess     r~   _rooms_availabler3    s3    !6!6!:9(())z?r   c                    d}| D ][  }|j                   s|t        |j                   j                  xs d      z  }|t        |j                   j                  xs d      z  }] |S r.  )property_typer0  num_bedsnum_sofa_beds)r2  totalprops      r~   _beds_availabler:    sd    E!!T''005A66T''55:;;	 
 Lr   c           
         |t        d      z   }t        t        t        j                  j                  | d||      j                  d            j                  dd            S )Nr1   daysT)r1  is_checked_incheck_in_date__ltcheck_out_date__gtr1  check_in_dater   )r   r#  r&   r   r   r  select_relatedorder_by)r1  
start_dateend_dateend_date_exclusives       r~   _statistical_bookingsrG    sd    !I1$55!OO""#""4#-	 # 
 n[)	
 (?D
)	 	r   c           
     V   t        j                         }t        ||      }|t        d      z   }|t        d      z   }t	        t
        j                  j                  | d|||d      j                  ddd      j                  d	      j                         j                  d
d            S )Nr1   r<  TF)r1  r>  r?  r@  check_out_date__lteguests__isnullpropertyr5  r1  r%  rA  r   )r   	localdateminr   r#  r   r   r  rB  prefetch_relateddistinctrC  )r1  rD  rE  todaycompleted_end_datecompleted_end_date_exclusiverF  s          r~   _exportable_bookingsrS    s     EXu-#5	q8I#I !I1$550) <  	 	
 

O[	A		(	#		/4	( r   c                *   t        d       }t               }| D ]	  }|j                  j                  |j                  j                  f}||   }|d   j                  |j                  |j                  f       |dxx   dz  cc<   |j                  r|dxx   dz  cc<   |j                  r|dxx   dz  cc<   |j                  r9|d   j                  |j                         |j                  |j                         |j                  s|d   |j                  xx   dz  cc<    g }d	}d	}d	}	d	}
t        |j                               D ]  \  \  }}}t        |d         }||z  }||d   z  }|	|d   z  }	|
|d   z  }
|j                  ||t        ||d      j!                  d
      ||d   |d   |d   t        |d         t#        t        |d   j                         d             d	        |rt%        ||z  d      nd	}||||	|
t        |      |dS )Nc                 D    t               dddt               t               dS )Nr   )guest_occurrencesnights_stayedarrivals
departuresproperties_includednationality_counts)setr    r   r~   r"  z/_build_guest_night_statistics.<locals>.<lambda>  s    !$#&5"))
r   rV  rW  r1   rX  rY  rZ  r[  r   z%B %Yc                    | d   S r.  r]  )items    r~   r"  z/_build_guest_night_statistics.<locals>.<lambda>3  s    SWXYSZr   r   )	yearmonthlabeltotal_guestsrW  rX  rY  rZ  nationalities)rc  rW  avg_stay_lengthrX  rY  rZ  monthly)r   r\  r   r`  ra  add
booking_id	guest_keyis_arrival_nightis_departure_nightproperty_idrl   sorteditemsr   appendr   r   round)guest_nights
month_datarZ  guest_night	month_keypayloadmonthly_statsrc  total_nightstotal_arrivalstotal_departuresr`  ra  month_guest_countre  s                  r~   _build_guest_night_statisticsr{     sW   	
	J %# %%**K,<,<,B,BC	Y'#$((+*@*@+BWBW)XY A% ''J1$))L!Q&!"")*..{/F/FG##K$;$;<""()+*A*ABaGB $ MLLN"()9)9);"<uw(; <=))00'*--GL11dE1-66w? 1!(!9#J/%l3'*73H+I'J!%7#78>>@FZ["	
 #=, @LeL<7;QRO %%*"&"#67  r   c           
        t         j                  j                  | |      j                         }|r|j                  S | j
                   d|d}t        |      t        d   kD  ret        j                  | j
                   d|j
                   j                  d            j                         }|d t        d    j                         }t        d      D ]O  }	 t        j                         5  t         j                  j!                  | ||      }d d d        j                  c S  |d t        d    S # 1 sw Y   $xY w# t"        $ r t        j                  | j
                   d|j
                   dt%        j&                         j)                          j                  d            j                         }|d t        d    j                         }Y w xY w)N)reservationr   -02drJ   zutf-8rA   )r}  r   istat_position_code)r   r   r  firstr  r   r   FIELD_LENGTHShashlibsha1rz   	hexdigestupperranger   atomiccreater   r   now	timestamp)bookingr   	seq_indexexistingbasedigestr   createds           r~   _ensure_position_coder  E  s   '//665 7 eg  +++jj\9S/*D
4y=11AehhZ8??HISSU6o67==?1X	D##%2::AA '(, B  & ...  0-011 &%  	D\\::,az8<<>+C+C+E*FGNNwWik  :M/:;AACD		Ds+   D?-#D3D?3D<	8D??BGGc                    | j                   xs |j                  d      }|r|S |j                  d      }|rt        ||      r|S y )Nrk   place_of_birth)rk   r   r   )r   r   country_lookupsexplicit_valuelegacy_place_of_births        r~   _birth_country_sourcer  c  sL    ++Luyy9K/LN!II&67!67Lo!^$$r   c                    |j                  d      xs2 |j                  d      xs |j                  d      xs | j                  S )N
birth_citybirth_municipalityr  r   cityr   r   s     r~   _birth_city_sourcer  o  sE    		, 	99)*	99%&	 ::	r   c                    |j                  d      xsE |j                  d      xs2 |j                  d      xs | j                  xs |j                  d      S )Nresidence_municipalityresidence_cityr  r  r  r  s     r~   _residence_municipality_sourcer  x  sY    		*+ 	'99%&	'99V	' ::	' 99%&r   c                    |j                  d      xs2 |j                  d      xs |j                  d      xs | j                  S )Nresidence_provincer  state)r   regionr  s     r~   _residence_province_sourcer    sD    		&' 	99Z 	99W	 <<	r   c                .    t        t        | |      |      S r   )r  r  )r   r   municipality_lookupss      r~   _resolve_residence_municipalityr    s    
 !&ue4 r   c                6    |xs t        t        | |      |      S r   )r  r  )r   r   province_lookupsmunicipality_province_codes       r~   _resolve_residence_provincer    s&     & )?"5%0* r   c                    t        | ||      }|d   }dj                  |      }|r|dz  }|d    dt        j                         j	                  d       d}|j                  d      |t        |      |d	   d
S )N)structure_idrD  rE  recordsz
structure_coder   z%Y%m%dz.txtrv   invalid_records)contentfilenamevalid_countr  )build_istat_previewr   r   rL  r   rz   r   )r  rD  rE  export_datar  r  r  s          r~   generate_istat_exportr    s    %!K
 )$Gkk'"G6./0(2D2D2F2O2OPX2Y1ZZ^_H>>'*7|&'89	 r   c                    t         j                  j                  |       j                         }|st	        d      |j
                  st	        d      t        |||      }t        |||t        d      z         }t        |      }t        |||      }|D ch c]  }|j                   }	}t        t        j                  j                  |	      j                  d
            }
t        ||
      }t!        |
      }t#               }t%               }t'               }t)        d t*        j                  j-                  dd      D              }t)        d t.        j                  j-                  dd      D              }t1        t2              }t1        t4              }t7        d|      xs t7        d|      xs t7        d|      }g }g }g }|D ]  }t9        |j:                  j=                               }|s*t?        |j@                  tC        |            }tE        |d      D ]W  \  }}g }tG        |      }tI        |jJ                        }tM        |jN                  xs |jQ                  d      xs d      jS                         }|xs |dz
  tC        |      k  r||dz
     nd }tU        |jQ                  d      xs |jV                        } tY        |jQ                  d      xs |jZ                        }!t7        t]        |||      |      }"t7        |jQ                  d      xs |j^                  |      }#t7        |jQ                  d      xs |j`                  |      }$tc        ||      }%te        |%|      \  }&}'|'s)tg        |jQ                  d      xs |jh                  |      }'tk        |jQ                  d      xs |j`                  |jl                  |jQ                  d      |jh                  |      }(|(jn                  })|(jp                  }*|jr                  s|ju                  d       | s|ju                  d       |!s|ju                  d       |"s|ju                  d       |#s|ju                  d       |$s|ju                  d       |r|d vr|ju                  d       |r?|$|k(  r:|)s|ju                  d!       |*r|*tv        vs|(jx                  s|ju                  d"       |jz                  xs# |jQ                  d#      xs |jz                  xs d}+|j|                  xs# |jQ                  d$      xs |j|                  xs d},t        |+      }-t        |,      }.|jQ                  t        |+            }/|jQ                  t        |,            }0|+r|/s|ju                  d#       n-|r+t        |-      |vrt        |/      |vr|ju                  d#       |,r|0s|ju                  d$       n-|r+t        |.      |vrt        |0      |vr|ju                  d$       t        |||      }1|1s|ju                  d%       t        |      \  }2}3|2j                         }2|3j                         }3|D 4cg c]  }4t        |4       }5}4d&d'd(d)d*d+}6|r%| d,|6jQ                  |d       jS                         nd}7| d-k(  rd.n| d/k(  rd0nd}8t        |j                  xs |jQ                  d            }9t        |jQ                  d      xs |j^                        }:t        |jQ                  d      xs |j`                        };d1}<|5r+d2}<|ju                  |j                  |j                  |5d3       |ju                  i d4|j                  d5|j                  dtY        |jr                        d6tY        |j                        d7|2d|3d8|j                  r|j                  j                  ndd|7d|8d|!d9|9xs dd|:xs dd:|;xs dd#|+d$|,d;|1d<d=|<|5d>       |5r|j                  |	vrd}=|d?v rd}>d}?d}@n|=}>|}?|}@t        |t        d         t        tY        |jr                        t        d         t        |2t        d7         t        |3t        d         t        | t        d         t        |!t        d         t        |&t        d@         t        |'xs dt        dA         t        |"t        dB         t        |#t        dC         t        |)t        dD         t        |*xs dt        dE         t        |$t        dF         t        dt        dG         t        dt        dH         t        dt        dI         t        dt        dJ         t        tY        |j                        t        d6         t        |/xs dt        d#         t        |0xs dt        d$         t        |>t        dK         t        |?t        dL         t        @t        dM         t        |j                  xs dNdNkD  rdndNt        dO         t        |1t        d%         t        d-t        d<         g}Adj                  |A      }B|ju                  |B       Z  |j
                  ||||dP   |dQ   |dR   |dS   |dT   |dU   |dV   t        dW |D              tC        |      tC        |      dX
dYS c c}w c c}4w )ZN)r   zStructure not found.z2Structure.istat_code is required for ISTAT export.)r1  rD  rE  r1   r<  )period_startperiod_end_exclusive)rq  )r1  r5  c              3  2   K   | ]  }t        |        y wr   r   r   s     r~   r   z&build_istat_preview.<locals>.<genexpr>  s      KD 	4K   r.   T)flatc              3  2   K   | ]  }t        |        y wr   r   r   s     r~   r   z&build_istat_preview.<locals>.<genexpr>  s      MD 	4Mr  ITItalyItalia)startr(   rr   r0   date_of_birthrl   rm   r  r  )rm   r  r  r  r   r*   r2   rk   >   1617181920r  r  rD   rE   rJ   Singlez	Head fam.zGroup leaderzFam. memberzGroup member)r  r  r  r  r  rt   r  Mr  Fvalidexcluded)rh  guest_idr   rh  r  rC   r,   property_namern   rp   position_idrK   z1 New)statusr   >   r  r  r3   r5   r6   r7   r8   r9   r:   r;   r<   r>   r@   rF   rG   rH   r   rI   rc  rW  re  rX  rY  rZ  rf  c              3  J   K   | ]  }|j                  d       dk(  sd  yw)r  r  r1   N)r   )r   ru  s     r~   r   z&build_istat_preview.<locals>.<genexpr>  s$      !0gGKK4IW4Ts   ##)
rc  rW  re  rX  rY  rZ  rf  
valid_rowsexcluded_rowswarnings)r  r  record_payloadsr  summary)Pr'   r   r  r  r   
istat_coderG  r%   r   r{  rS  r   r#  r   rB  r3  r:  r   r  r  r\  r    values_listr!   r   TOURISM_VALUE_PAIRSTRANSPORT_VALUE_PAIRSr   r&  r%  r   r*  guest_group_typer   	enumerater   r   r   rx   r(   r   r   r  r0   r   r  r  rl   rm   r  r  r  r  r   r  municipality_codeprovince_coderA  ro  r"   province_matchesrD   rE   r   r   r  r   titler   r#   rk   check_out_daterK  r.   r,  r  r   rI   r   sum)Cr  rD  rE  r1  statistical_bookingsrq  
statisticsexportable_bookingsr  exportable_booking_idsr2  rG   rH   r  r  r  tourism_allowedtransport_allowedtourism_maptransport_map
italy_coder  r  r  r%  guest_type_codesidxr   r   r   raw_guest_typer(   gender_coder2   r6   r7   r:   r  r3   r5   	residencer8   r9   tourism_valuetransport_valuetourism_allowed_valuetransport_allowed_valuetourism_export_valuetransport_export_valuerJ   r,   r.   errornormalized_errorsguest_type_labelsguest_type_labelgender_labelbirth_country_iso2nationality_iso2residence_country_iso2r  rF   rooms_occupied_valuerooms_available_valuebeds_available_valuefieldsrecordsC                                                                      r~   r  r    sW   !!((L(9??AI/00MNN0
 )%	q(99L
 /LIJ.
 9LL8KWgjj8KL)4CCOTJ 'y*=O$Z0N+-O57-/ $,,88d8K O  &..::6:M  00CDK12GHM 	dO4 	< /:	< ?;  GOO' !3!3!56,W-E-Es6{S#F!44JCF-e4E&u'7'78E   AEIIl$;Areg  ( -01Ws;K7L-L q)RV  'uyy':'JellKK%eii&@&WEDWDWXJ$9%eUOD%!  5		-(=E,=,=  1		)$5L
 ,E59J;P0<8#%8 '&<IIg&6%,,8H'# *		),=ZZ:.|| I +4*E*E'&/&=&=#((n-h'l+(01#m,i(3Q!Ql+lj82MM":;/.6GG$55MM"67 "" 99^,'' 	  $$ 99-.)) 	  %E]$S!&F&W##.??:m3L#M %2%6%6z/7R%S" (<n- 01H34OKn-"*@./"23;LL56>OO./1'5#FM o.'.MGTmmoG::<DJP Q&!5e!<& Q !$#$!  ,a 1 5 5j" EFGMMO 
 #."43PSAS#Y[L!2&&G%))4F*G"  1		-(=E,=,=  &7		)$5&" F #&&&-jj$)HH"3 "" '** #L1F1F$G %l73I3I&J	
 w D $g>N>NW%5%5%:%:TV !"2 l !* $%7%=2 "#3#9r ()?)E2 #M %o  "=!" G#$ %/'0 !zz!77N\)')$(*%')$'5$(7%'5$  
M,,GH<(=(=>n@]^7M)$<=4v!67]8-DE:}\'BC+];T-U '-2}=R/S  )=9P+Q   0-@R2ST/?\1] +1r!";<  mN.KL2}Y782}_=>2}->?@2}-CDE<(>(>?O_A`a/52}^7TU17RGW9XY 4mDT6UV 5}EV7WX 4mDT6UVg&6&6&;!q%@aWaIbc=-*HI]6%:;K&FP WWV_FNN6"C 5 (T $..**&~6'8)*;<":.$\2#-.C#D!), !0  !1O,
 S Md !Rs   k#k)r}   rx   returnrx   )r}   rx   r   r0  r  rx   )r   )r}   rx   r   r0  r   rx   r  rx   )r  rx   )r   r   r  ztuple[str, str])r  r   )r   zlist[tuple[str, str]]r  dict[str, str])r   rx   r  rx   )r   
str | Noner   Iterable[str | None]r   r  r   r	  r   r	  r   r	  r   r	  )r}   r
  r  r
  )r}   r
  r  tuple[str | None, str | None])r   r
  r  rx   )r%  zIterable[Guest]r  zlist[Guest])r(  rx   r)  r0  r  z	list[str])r}   zint | str | Noner   r0  r  rx   )r1  r'   r2  list[Property]r  r0  )r2  r  r  r0  )r1  r'   rD  r   rE  r   )r  r   r   r   r  r0  r  rx   )r   r   r   r   r  r
  )r   r   r   r   r  r  r   )r   r   r   r   r  r
  r  r
  )r  r0  rD  r   rE  r   )f
__future__r   r   r  r   collectionsr   r   r   r   r   	functoolsr	   ior
   typingr   urllib.requestr   zipfiler   	django.dbr   r   django.utilsr   bookings.modelsr   guests.modelsr   guests.guest_defaultsr   r   r   r   istat.municipalitiesr   r   guests.istat_utilsr   properties.modelsr   istat.modelsr   r   r   r   r    r!   #istat.xml_export.mappings.provincesr"   services.country_utilsr#   r$   services.guest_night_servicer%   r&   structures.modelsr'   r  r   r  r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r&  r*  r,  r3  r:  rG  rS  r{  r  r  r  r  r  r  r  r  r  r]  r   r~   <module>r"     s   " 
   , . .    "  1 ! #   7 &  B P Y '!B r B	
 a " q 1 Q  "1 q A r Q  r!" A#$ 5<B 
 $ $ +,  ( " _"&) )?+'?
'?  '? $	'?
 '? '? '? !'?T2$ 1,2 ,2^A:.$&	* 6+*BJ2<	 #	 .2				 !+		
 	*tr   