Ë
    ü8„jû  ã                   óî   — d dl mZ d dlmZ d dlmZmZmZ ddlm	Z	  e	«       Z
dedz  fd„Zdeeef   dz  fd	„Zd
eeeef      defd„Z	 dd
eeeef      defd„Zdd
eeeef      deeee   f   fd„Zy)é    )ÚDecimal)Únormalize_country_code)ÚCountryBoundaryÚFreightRouteÚZoneé   )ÚShapelyBackendÚreturnNc                 ó²  — 	 t        | «      }t        |«      }ddlm}m}  |||«      }t        j                  j                  «       j                  «       D ]N  }	  ||j                  «      }|j                  |«      s|j                  |«      sŒ9t        |j                  «      c S  ddlm}	  |	| |«      S # t        t        f$ r Y yw xY w# t        $ r Y Œ€w xY w)zJResolve ISO alpha-2 from a lat/lon via country boundaries, then Nominatim.Nr   )ÚPointÚshape)Úreverse_geocode_country_code)ÚfloatÚ	TypeErrorÚ
ValueErrorÚshapely.geometryr   r   r   ÚobjectsÚallÚiteratorÚgeojsonÚ	ExceptionÚcoversÚcontainsr   Úcountry_codeÚcore.geocodingr   )
ÚlatÚlonÚlat_fÚlon_fr   r   ÚpointÚboundaryÚgeomr   s
             ú./var/www/html/truck-me/core/pricing/service.pyÚcountry_code_for_pointr$      sÐ   € ðÜ�c“
ˆÜ�c“
ˆ÷ .á�%˜Ó€EÜ#×+Ñ+×/Ñ/Ó1×:Ñ:Ó<ò Aˆð	Ù˜×)Ñ)Ó*ˆDð �;‰;�uÔ §¡¨uÕ!5Ü)¨(×*?Ñ*?Ó@Ò@ðAõ <á'¨¨SÓ1Ð1øô! ”zÐ"ò Ùðûô ò 	Ùð	ús#   ‚B5 ÁC
Â5CÃCÃ
	CÃCc                 óÖ   — t        | «      } t        |«      }| r|r| |k(  ryt        j                  j                  | |¬«      j	                  «       }|€y|j
                  |j                  fS )uN   Directional min freight for originâ†’destination. None if the pair is missing.N)Úorigin_country_codeÚdestination_country_code)r   r   r   ÚfilterÚfirstÚmin_freightÚcurrency)ÚoriginÚdestinationÚroutes      r#   Úfreight_route_pricer/   #   sq   € ä# FÓ+€FÜ(¨Ó5€KÙ™¨°+Ò(=ØÜ× Ñ ×'Ñ'Ø"Ø!,ð (ó ÷ �eƒgð 
ð €}ØØ×Ñ˜eŸn™nÐ,Ð,ó    Úroute_coordsc                 ó8  — ddl m} ddlm} ddl}| rt        | «      dk  rt        d«      S |j                  j                  ddd	¬
«      j                  } || || «      «      j                  }t        t        |dz  «      «      j                  t        d«      «      S )zNGeodesic-approx polyline length in km (EPSG:3857), matching zone intersection.r   )Ú
LineString)Ú	transformNé   z0.00z	EPSG:4326z	EPSG:3857T)Ú	always_xyiè  ú0.01)r   r3   Úshapely.opsr4   ÚpyprojÚlenr   ÚTransformerÚfrom_crsÚlengthÚstrÚquantize)r1   r3   r4   r9   ÚprojectÚlength_ms         r#   Úroute_distance_kmrB   2   sŠ   € å+Ý%Ûáœ3˜|Ó,¨qÒ0Ü�v‹ÐØ× Ñ ×)Ñ)Ø�[¨Dð *ó ç�ið ñ ˜¡*¨\Ó":Ó;×BÑB€HÜ”3�x $‘Ó'Ó(×1Ñ1´'¸&³/ÓBÐBr0   c                 óà  — ddl m} t        | |«      }t        ||«      } ||«      }	|rj|rh||k7  rct        |«      }
t	        ||«      }|€dt        |
«      |	||g dœS |\  }}t        |j                  t        d«      «      «      t        |
«      |	||g dœS t        ||¬«      \  }}t        d„ |D «       t        d«      «      j                  t        d«      «      }
t        |«      t        |
«      |	|d	œS )
uê   
    Same-country: Zone.rate_per_km Ã— intersecting km (raises ValueError if unpriced).
    Cross-border: directional FreightRoute.min_freight, or suggested_price None.
    Currency is always the pickup (origin) country currency.
    r   )Úcurrency_code_for_countryN)Úsuggested_priceÚdistance_kmr+   r&   r'   Úprice_breakdownr7   )Úbackendc              3   ój   K  — | ]+  }t        t        |j                  d «      xs d«      «      –— Œ- y­w)Úkmr   N)r   r>   Úget)Ú.0Úsegs     r#   ú	<genexpr>z+estimate_suggested_price.<locals>.<genexpr>l   s'   è ø€ Ò?¨cŒ”�S—W‘W˜T“]Ò' aÓ(×	)Ñ?ùs   ‚13Ú0)rE   rF   r+   rG   )
Úcore.currenciesrD   r$   rB   r/   r>   r?   r   Úcalculate_suggested_priceÚsum)Ú
pickup_latÚ
pickup_lonÚdelivery_latÚdelivery_lonr1   rH   rD   r,   r-   r+   rF   Úpricedr*   Ú_route_currencyÚpriceÚ	breakdowns                   r#   Úestimate_suggested_pricer[   A   s  € õ :ä# J°
Ó;€FÜ(¨°|ÓD€KÙ(¨Ó0€Há‘+ &¨KÒ"7Ü'¨Ó5ˆÜ$ V¨[Ó9ˆØˆ>à#'Ü" ;Ó/Ø$Ø'-Ø,7Ø#%ñð ð (.Ñ$ˆ�_ä" ;×#7Ñ#7¼À»Ó#HÓIÜ˜{Ó+Ø Ø#)Ø(3Ø!ñ
ð 	
ô 1°ÀwÔOÑ€Eˆ9ÜÙ?°YÔ?Ü�‹ó÷ �hŒw�v‹Óð ô
 ˜u›:Ü˜;Ó'ØØ$ñ	ð r0   c           	      ó`  — |xs t         }| rt        | «      dk  rt        d«      ‚t        t        j
                  j                  dd¬«      «      }t        t        j
                  j                  |¬«      «      }|st        d«      ‚|j                  | |«      }|st        d«      ‚t        j
                  j                  |D �cg c]  }|d   ‘Œ	 c}¬«      D �ci c]  }|j                  |“Œ }}t        d	«      }g }	|D ]}  }
|j                  |
d   «      }|r|j                  €Œ&|
d
   |j                  z  }||z  }|	j                  |
d   t        |
d
   «      t        |j                  «      t        |«      dœ«       Œ |	st        d«      ‚|j!                  t        d«      «      |	fS c c}w c c}w )u  
    route_coords: [(lng, lat), ...] â€” the actual driven route polyline
    from the routing provider, NOT just pickup/dropoff.

    Returns (total_price, breakdown). Raises ValueError if the route
    doesn't intersect any zone with a configured boundary and rate.
    r5   z8route_coords must contain at least two (lng, lat) pointsr   T)Úflat)Úcountry_code__inz4No country boundaries configured for any active zonez5Route does not intersect any configured zone boundaryrO   rJ   )r   rJ   Úrate_per_kmrY   z;Route intersects zones but none have rate_per_km configuredr7   )ÚDEFAULT_BACKENDr:   r   Úlistr   r   Úvalues_listr   r(   Úintersect_route_with_boundariesr   r   rK   r_   Úappendr   r?   )r1   rH   Úrelevant_codesÚ
boundariesÚsegmentsÚsÚzÚzones_by_codeÚtotal_pricerZ   rM   ÚzoneÚsegment_prices                r#   rQ   rQ   w   s¶  € ð Ò(œ€Gáœ3˜|Ó,¨qÒ0ÜÐSÓTÐTäœ$Ÿ,™,×2Ñ2°>ÈÐ2ÓMÓN€NÜ”o×-Ñ-×4Ñ4ÀnÐ4ÓUÓV€JáÜÐOÓPÐPà×6Ñ6°|ÀZÓP€HáÜÐPÓQÐQô —‘×$Ñ$ÐRZÖ6[ÈQ°q¸Ó7HÒ6[Ð$Ó\öàð 	
�‰˜Ñð€Mð ô
 ˜#“,€KØ€Iàò ˆØ× Ñ   ^Ñ!4Ó5ˆÙ�t×'Ñ'Ð/ØØ˜D™	 D×$4Ñ$4Ñ4ˆØ�}Ñ$ˆØ×ÑØ Ñ/Ü˜˜D™	Ó"Ü  ×!1Ñ!1Ó2Ü˜=Ó)ñ	
õ 	ðñ ÜÐVÓWÐWà×Ñ¤¨£Ó0°)Ð;Ð;ùò- 7\ùòs   ÃF&ÃF+)N)Údecimalr   Úcore.countriesr   Úcore.modelsr   r   r   Úbackendsr	   r`   r>   r$   Útupler/   ra   r   rB   Údictr[   rQ   © r0   r#   ú<module>ru      sË   ðÝ å 1ß ;Ñ ;å $á Ó"€ð2¨¨d©
ó 2ð0-°°g¸s°lÑ0CÀdÑ0Jó -ðC D¨¨u°e¨|Ñ)<Ñ$=ð CÀ'ó Cð* ñ3ð
 �u˜U E˜\Ñ*Ñ+ð3ð 
ó3ñl0<¨D°°u¸e°|Ñ1DÑ,Eð 0<ÐX]Ð^eÐgkÐlpÑgqÐ^qÑXrô 0<r0   