Ë
    �Ókt  ã                   ó  — d Z ddlZddlZddl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  ej                  d«      Z ej                   d	«      Zd
Zdee   defd„Zdedefd„Zdefd„Zdefd„Zdefd„Zdee   fd„Zdefd„Zy)uM  
shipping_service.py â€” home delivery of the orders placed AFTER a school's deadline (client 2026-11-23).

At the deadline every order is delivered to the school in one go. A family that orders later would cost a trip of its
own, so a school may offer delivery to a French address for a fixed fee per order (`home_delivery`,
`home_delivery_fee`). Switched off, nothing changes: the portal closes at the deadline as it always did.

Rules:
  * The fee is charged only when the order holds something physical: a digital-only order has nothing to send.
  * `orders.total` stays what Stripe charges (products + fee), so the webhook's amount check needs no exception. The fee
    is not a sale of the school: `goods_total` is the base of every sales figure and of the school's share.
  * This is the ONE place where a personal name may be stored (project rule nÂ°1 has this exception, agreed 2026-11-23):
    the ADULT who receives the parcel, never the child. It is never returned to the family portal, which anyone holding
    the code can open, and it is erased by `purge_addresses` once it has served.
é    N)Ú	timedelta)ÚOptional)Úget_db)Úutcnow)Úaudit_servicezmeeconic.shippingz1^(?:(?:0[1-9]|[1-8]\d|9[0-5])\d{3}|97[1-6]\d{2})$éZ   ÚvalueÚreturnc                 óH   — dj                  | xs dj                  «       «      S )Nú Ú )ÚjoinÚsplit)r	   s    ú4/var/www/photos/backend/services/shipping_service.pyÚ_cleanr   #   s   € Ø�8‰8�U’[˜b×'Ñ'Ó)Ó*Ð*ó    Úrawc           	      óZ  — dD �ci c]  }|t        | j                  |«      «      “Œ }}t        |d   «      dk  rt        d«      ‚t        |d   «      dk  rt        d«      ‚t        j                  |d   «      st        d	«      ‚t        |d
   «      dk  rt        d«      ‚i |¥ddi¥S c c}w )zTThe address as it is stored. Raises ValueError with the message shown to the family.)ÚnameÚaddressÚaddress2ÚpostcodeÚcityr   é   z-Nom du destinataire requis pour la livraison.r   é   zAdresse de livraison requise.r   z6Livraison en France uniquement : code postal invalide.r   zVille de livraison requise.ÚcountryÚFR)r   ÚgetÚlenÚ
ValueErrorÚ_POSTCODE_REÚmatch)r   ÚkÚouts      r   Ú	normalizer%   '   s´   € à*]Ö
^ Qˆ1Œf�S—W‘W˜Q“ZÓ Ñ Ð
^€CÐ
^Ü
ˆ3ˆv‰;Ó˜!ÒÜÐHÓIÐIÜ
ˆ3ˆy‰>Ó˜QÒÜÐ8Ó9Ð9Ü×Ñ˜c *™oÔ.ÜÐQÓRÐRÜ
ˆ3ˆv‰;Ó˜!ÒÜÐ6Ó7Ð7Ø#ˆcÐ#�9˜dÑ#Ð#ùò _s   …"B(c                 ó6   — t        | j                  d«      «      S )zaDelivered to a home, so it must stay out of what is handed to the school (labels, delivery slip).Úhome_delivery)Úboolr   ©Úrows    r   Ú
ships_homer+   5   s   € ä�—‘˜Ó(Ó)Ð)r   c                 ó>   — t        | j                  d«      xs d«      S )NÚshipping_feer   )Úfloatr   r)   s    r   Úfee_ofr/   :   s   € Ü�—‘˜Ó(Ò-¨AÓ.Ð.r   c                 óJ   — t        t        | d   «      t        | «      z
  d«      S )zbWhat the products of the order add up to: the base of the sales figures, without the delivery fee.Útotalr   )Úroundr.   r/   r)   s    r   Úgoods_totalr3   >   s!   € ä”�s˜7‘|Ó$¤v¨c£{Ñ2°AÓ6Ð6r   c                 ó´   — t        | «      r| j                  d«      sy	 t        j                  | d   «      }t        |t        «      r|S dS # t        $ r Y yw xY w)zjThe stored address, or None (not a home delivery, or already erased). Older rows hold free text: not ours.Úshipping_addressN)r+   r   ÚjsonÚloadsr    Ú
isinstanceÚdict)r*   Údatas     r   Ú
address_ofr;   C   s[   € ä�cŒ? #§'¡'Ð*<Ô"=ØðÜ�z‰z˜#Ð0Ñ1Ó2ˆô ˜d¤DÔ)ˆ4Ð3¨tÐ3øô ò Ùðús   ŸA Á	AÁAc            
      ó`  — ddl m}  d}t        «       }| j                  «       D ]ä  }|d   }t	        | j                  |«      j                  d«      xs d«      }|t        |¬«      z
  j                  «       }|t        t        |t        «      ¬«      z
  j                  «       }t        «       5 }|j                  d|||f«      j                  }	|j                  «        ddd«       	sŒÂ||	z  }t        j                   d	d
|||	|dœd¬«       Œæ |rt"        j%                  d|«       |S # 1 sw Y   ŒKxY w)zÌDaily (cleanup_service): erases the addresses that have served, at the school's RGPD retention. An order still
    being prepared keeps its address, up to MAX_KEEP_DAYS. The order itself and its fee stay.r   )Úschool_serviceÚidÚ	rgpd_daysé   )ÚdayszßUPDATE orders SET shipping_address=NULL WHERE school_id=? AND home_delivery=1 AND shipping_address IS NOT NULL AND created_at < ? AND (batch_status IN ('delivered','cancelled') OR payment_status != 'paid' OR created_at < ?)Nzorder.address_rgpd_deleteÚschool)Ú	addressesÚretention_daysÚcritical)Úresource_typeÚresource_idÚ	school_idÚdetailsÚseverityz$GDPR: %d delivery address(es) erased)Úservicesr=   r   Úlist_schoolsÚintÚget_settingsr   r   Ú	isoformatÚmaxÚMAX_KEEP_DAYSr   ÚexecuteÚrowcountÚcommitr   Ú	log_eventÚlogÚinfo)
r=   ÚerasedÚnowrB   ÚsidrA   ÚcutoffÚhardÚconnÚns
             r   Úpurge_addressesr_   N   s/  € õ (Ø€FÜ
‹(€CØ ×-Ñ-Ó/ò kˆØ�T‰lˆÜ�>×.Ñ.¨sÓ3×7Ñ7¸ÓDÒJÈÓKˆØœ	 tÔ,Ñ,×7Ñ7Ó9ˆØ”i¤S¨¬}Ó%=Ô>Ñ>×IÑIÓKˆÜ‹Xð 	˜Ø—‘ðpð �f˜dÐ#ó	%÷ &.¡Xð	 ð
 �K‰KŒM÷	ò Ø�a‰KˆFÜ×#Ñ#Ð$?ÈxÐehÐtwØ:;ÈtÑ,TÐ_iökðkñ  Ü�‰Ð7¸Ô@Ø€M÷	ð 	ús   Â*0D$Ä$D-	)Ú__doc__r6   ÚloggingÚreÚdatetimer   Útypingr   Úcore.dbr   Úcore.timeutilr   rK   r   Ú	getLoggerrV   Úcompiler!   rQ   Ústrr   r9   r%   r(   r+   r.   r/   r3   r;   rM   r_   © r   r   ú<module>rk      s¹   ðñó Û Û 	Ý Ý å Ý  Ý "à€g×ÑÐ+Ó,€ð ˆr�z‰zÐNÓO€à€ð+�(˜3‘-ð + Có +ð$�4ð $˜Dó $ð*�tó *ð
/�5ó /ð7˜ó 7ð
4�x ‘~ó 4ð˜ô r   