§
    á°“jQ  ã                  óz   — d dl mZ 	 d dlZd dlmZ ddlmZ ddlmZ  ej	        e
¦  «        Z G d„ d¦  «        ZdS )	é    )ÚannotationsN)ÚAnyé   )Úbuild_list_invoices_filters©Úmap_invoice_rowc                  ó@   — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!d"d „ZdS )#ÚInvoiceQueryListMixinz(Mixin for list_invoices query execution.NÚ
issue_dateÚDESCé2   r   Ú	directionú
str | NoneÚstatusÚsearchÚ	from_dateÚto_dateÚreconciliation_statusÚ
project_idÚvat_rateÚ
min_amountúfloat | NoneÚ
max_amountÚsignature_validúbool | NoneÚinvoice_seriesÚsort_byÚstrÚ
sort_orderÚinvoice_type_filterÚcategoryÚlimitÚintÚoffsetÚreturnú tuple[list[dict[str, Any]], int]c                óP  ‡ — ‰                       ¦   «         5 }|                     ¦   «         5 }t          |||||||||	|
||||¬¦  «        \  }}d|› d�}|                     ||¦  «         |j        dk    r|                     ¦   «         d         nd}dddd	d
ddddddddœ}|                     |d¦  «        }t          |¦  «                             ¦   «          	                    ¦   «         dk    rdnd}d|› d|› d|› d�}t          |¦  «        }|                     ||g¦  «         |                     ||¦  «         |                     ¦   «         }ˆ fd„|D ¦   «         }|t          |¦  «        fcddd¦  «         cddd¦  «         S # 1 swxY w Y   	 ddd¦  «         dS # 1 swxY w Y   dS )uo   Láº¥y danh sÃ¡ch hÃ³a Ä‘Æ¡n theo bá»™ lá»�c nÃ¢ng cao chuáº©n meInvoice/Excel kÃ¨m sáº¯p xáº¿p vÃ  phÃ¢n trang.)r   r   r   r   r   r   r   r   r   r   r   r   r    r!   z¾
                    SELECT COUNT(*) AS total_count
                    FROM erp_invoices i
                    LEFT JOIN projects p ON i.matched_project_id = p.id
                    WHERE z;
                r   Útotal_countzi.issue_datezi.total_amount_vndzi.subtotal_amount_vndzi.vat_amount_vndzi.invoice_numberzi.invoice_serieszi.seller_namezi.buyer_namezi.directionzi.signature_validzi.reconciliation_statuszi.created_at)r   Útotal_amount_vndÚsubtotal_amount_vndÚvat_amount_vndÚinvoice_numberr   Úseller_nameÚ
buyer_namer   r   r   Ú
created_atÚASCr   a»  
                    SELECT
                        i.id,
                        i.direction,
                        i.invoice_type,
                        i.invoice_number,
                        i.invoice_series,
                        i.template_code,
                        i.issue_date,
                        i.seller_tax_code,
                        i.seller_name,
                        i.buyer_tax_code,
                        i.buyer_name,
                        i.subtotal_amount_vnd,
                        i.vat_rate_percent,
                        i.vat_amount_vnd,
                        i.total_amount_vnd,
                        i.amount_in_words,
                        i.status,
                        i.tax_authority_code,
                        i.signature_valid,
                        i.signed_by,
                        i.signed_at,
                        i.source_channel,
                        i.reconciliation_status,
                        i.matched_project_id,
                        p.project_name AS matched_project_name,
                        p.project_code AS matched_project_code,
                        i.payment_status,
                        i.payment_due_date,
                        i.remaining_amount_vnd,
                        i.created_at,
                        (SELECT COUNT(*) FROM erp_invoice_items WHERE invoice_id = i.id) AS item_count,
                        (SELECT string_agg(item_name, ' | ') FROM (SELECT item_name FROM erp_invoice_items WHERE invoice_id = i.id LIMIT 5) sub) AS item_names_summary
                    FROM erp_invoices i
                    LEFT JOIN projects p ON i.matched_project_id = p.id
                    WHERE z
                    ORDER BY ú zL, i.created_at DESC
                    LIMIT %s OFFSET %s;
                c                ó0   •— g | ]}t          ‰|¦  «        ‘ŒS © r   )Ú.0ÚrÚselfs     €úTC:\Projects\DSCons\app\modules\invoices\application\invoice_processing\query_list.pyú
<listcomp>z7InvoiceQueryListMixin.list_invoices.<locals>.<listcomp>„   s#   ø€ ÐAÐAÐA°q�/¨$°Ñ2Ô2ÐAÐAÐAó    N)Úget_connectionÚcursorr   ÚexecuteÚrowcountÚfetchoneÚgetr   ÚstripÚupperÚlistÚextendÚfetchallr#   ) r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r$   ÚconnÚcurÚ	where_sqlÚparamsÚ	count_sqlr(   Úsafe_sort_mapÚorder_columnÚorder_directionÚsqlÚquery_paramsÚrowsÚresults    `                               r7   Úlist_invoicesz#InvoiceQueryListMixin.list_invoices   sö  ø€ ð, × Ò Ñ"Ô"ð ]	0 dØ—’‘”ð \0 #Ý$?Ø'Ø!Ø!Ø'Ø#Ø*?Ø)Ø%Ø)Ø)Ø$3Ø#1Ø(;Ø%ð%ñ %ô %Ñ!�	˜6ð"ð %ð	ð ð �	ð —’˜I vÑ.Ô.Ð.Ø?B¼|ÈaÒ?OÐ?O˜cŸlšl™nœn¨]Ô;Ð;ÐUV�ð #1Ø(<Ø+BØ&8Ø&8Ø&8Ø#2Ø"0Ø!.Ø':Ø-FØ"0ð!ð !�ð  -×0Ò0°¸.ÑIÔI�å  ™_œ_×2Ò2Ñ4Ô4×:Ò:Ñ<Ô<ÀÒEÐE�E�EÈ6ð  ð'ðH %ðI'ð 'ðJ +ðK'ð 'ðJ .=ðK'ð 'ð '�õP  $ F™|œ|�Ø×#Ò# U¨F OÑ4Ô4Ð4Ø—’˜C Ñ.Ô.Ð.Ø—|’|‘~”~�àAÐAÐAÐA¸DÐAÑAÔA�Ø�s ;Ñ/Ô/Ð/ðy\0ð \0ð \0ð \0ñ \0ô \0ð \0ð]	0ð ]	0ð ]	0ð ]	0ñ ]	0ô ]	0ð ]	0ð ]	0ð\0ð \0ð \0ð \0øøøð \0ð \0ð \0ð \0ð \0ð]	0ð ]	0ð ]	0ñ ]	0ô ]	0ð ]	0ð ]	0ð ]	0ð ]	0ð ]	0ð ]	0ð ]	0øøøð ]	0ð ]	0ð ]	0ð ]	0ð ]	0ð ]	0s5   –F«D>FÅ)FÆF	ÆFÆ	F	Æ
FÆFÆ"F)NNNNNNNNNNNNr   r   NNr   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   r    r   r!   r   r"   r#   r$   r#   r%   r&   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rQ   r3   r9   r7   r
   r
      sr   € € € € € Ø2Ð2ð !%Ø!Ø!Ø $Ø"Ø,0Ø!%Ø#Ø#'Ø#'Ø'+Ø%)Ø#Ø Ø*.Ø#ØØð's0ð s0ð s0ð s0ð s0ð s0ð s0r9   r
   )Ú
__future__r   ÚloggingÚtypingr   Úquery_builderr   Úquery_mapperr   Ú	getLoggerrR   Úloggerr
   r3   r9   r7   ú<module>r]      s©   ðØ "Ð "Ð "Ð "Ð "Ð "à ?ð €€€Ø Ð Ð Ð Ð Ð à 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø )Ð )Ð )Ð )Ð )Ð )à	ˆÔ	˜8Ñ	$Ô	$€ðv0ð v0ð v0ð v0ð v0ñ v0ô v0ð v0ð v0ð v0r9   