
    {ii                        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mZmZ d dlmZ ddZej$                  d	d
dd       Zy	)    )annotations)transaction)Max)PermissionDeniedValidationError)Booking)InvoiceInvoiceLineItemReceipt)	Structurec                P   t         j                  j                         j                  | j                         t
        j                  j                  |       j                  t        d            d   xs ddz   }| j                  xs dj                         xs d}|| d	|d
fS )Npk)	structuresequence)max_sequencer   r      INV-05d)r   objectsselect_for_updategetr   r	   filter	aggregater   invoice_prefixstrip)r   next_sequenceprefixs      $/backend/services/invoice_service.pyget_invoice_number_componentsr!      s    '')--->3==3z?=[	
 	 	
M &&/%668AEFVHAmC%8999    N)extra_detailsc                   t         j                  j                         j                  d      j	                  |       j                         }|t        ddi      |j                  j                  |j                  k7  rt        d      |j                  j                  st        d      t        j                  j                         j                  dd      j                  d      j	                  |	      j                         }|t        d
      t        j                  j	                  |	      j                         rt        d      t!        |j                        \  }}t        j                  j#                  |j                  |||||j$                  |j&                  |j(                  |j*                  |xs i 
      }t,        j                  j/                  |j0                  j3                         D cg c]P  }t-        ||j4                  |j6                  |j8                  |j:                  |j<                  |j>                        R c}       d|_         |jC                  ddg       |S c c}w )Nr   r   reservation_idzReservation not found.z+You do not have access to this reservation.z)Invoicing is disabled for this structure.reservation
line_items)r&   z,Receipt does not exist for this reservation.z/An invoice already exists for this reservation.)
r   r&   receiptnumberr   total_gross	total_net	total_vatcity_tax_amountr#   )invoice	line_typedescriptionvat_rategross_amount
net_amount
vat_amountTis_finalized
updated_at)update_fields)"r   r   r   select_relatedr   firstr   r   user_ididr   invoicing_enabledr   prefetch_relatedr	   existsr!   creater*   r+   r,   r-   r
   bulk_creater'   allr/   r0   r1   r2   r3   r4   r5   save)	r%   userr#   bookingr(   r   r)   r.   	line_items	            r    create_invoice_for_reservationrF      s&    	))+		$	>	"		  /1IJKK  DGG+LMM..IJJ 	))+	]	3		,	'	G	$	  LMM'299;OPP4W5F5FGHfoo$$##''####//#)r % G '' %//335	
 6	 #--%11"++&33$//$// 6	
  GLL=L>N	
s   7AI0)r   r   returnztuple[int, str])r%   intr#   zdict | NonerG   r	   )
__future__r   	django.dbr   django.db.modelsr   rest_framework.exceptionsr   r   bookings.modelsr   invoicing.modelsr	   r
   r   structures.modelsr   r!   atomicrF    r"   r    <module>rR      s?    " !   G # > > '	: ^b ; ;r"   