§
    <à™jx  ã                  ó`   — d dl mZ d dlZd dlmZ  ej        d¦  «        Z G d„ d¦  «        ZdS )é    )ÚannotationsN)ÚAnyzdscons.invoice_truth.queriesc                  ó6   — e Zd ZdZ	 	 	 	 	 ddd„Zdd„Zdd„ZdS )ÚTruthQueriesMixinzHQueries and analytics views for warehouse inventory and equipment truth.Né2   r   Úmaterial_groupú
str | NoneÚstatusÚsearchÚlimitÚintÚoffsetÚreturnú Tuple[List[Dict[str, Any]], int]c                óX  — |                       ¦   «         5 }|                     ¦   «         5 }dg}g }	|r*|                     d¦  «         |	                     |¦  «         |r*|                     d¦  «         |	                     |¦  «         |rFd|                     ¦   «         › d�}
|                     d¦  «         |	                     |
|
|
|
g¦  «         d                     |¦  «        }|                     d|› d�|	¦  «         |                     ¦   «         d	         }d
|› d�}|	                     ||g¦  «         |                     ||	¦  «         |                     ¦   «         }d„ |D ¦   «         |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 )zBFetch paginated warehouse inventory items with advanced filtering.z1=1zw.material_group = %szw.status = %sú%zn(w.item_name ILIKE %s OR w.item_code ILIKE %s OR w.supplier_name ILIKE %s OR w.invoice_series_number ILIKE %s)z AND z>SELECT COUNT(*) as total FROM erp_warehouse_inventory w WHERE ú;Útotala  
                    SELECT 
                        w.id, w.item_code, w.item_name, w.material_group,
                        w.unit, w.quantity_received, w.quantity_remaining,
                        w.unit_price_vnd, w.total_value_vnd,
                        w.supplier_name, w.supplier_tax_code,
                        w.source_invoice_id, w.invoice_series_number,
                        w.receipt_date, w.warehouse_location, w.status,
                        w.allocated_project_id, w.notes,
                        p.project_name as allocated_project_name,
                        p.project_code as allocated_project_code
                    FROM erp_warehouse_inventory w
                    LEFT JOIN projects p ON w.allocated_project_id = p.id
                    WHERE z}
                    ORDER BY w.receipt_date DESC, w.created_at DESC
                    LIMIT %s OFFSET %s;
                c                ó,   — g | ]}t          |¦  «        ‘ŒS © ©Údict©Ú.0Úrs     úLC:\Projects\DSCons\app\modules\invoices\application\invoice_truth\queries.pyú
<listcomp>z>TruthQueriesMixin.list_warehouse_inventory.<locals>.<listcomp>H   s   € Ð.Ð.Ð. A�˜Q™œÐ.Ð.Ð.ó    N)	Úget_connectionÚcursorÚappendÚstripÚextendÚjoinÚexecuteÚfetchoneÚfetchall)Úselfr   r
   r   r   r   ÚconnÚcurÚwhere_clausesÚparamsÚs_likeÚ	where_sqlÚ	total_cntÚqueryÚrowss                  r   Úlist_warehouse_inventoryz*TruthQueriesMixin.list_warehouse_inventory   sƒ  € ð × Ò Ñ"Ô"ð 3	: dØ—’‘”ð 2: #Ø!& �Ø$&�à!ð 2Ø!×(Ò(Ð)@ÑAÔAÐAØ—M’M .Ñ1Ô1Ð1àð *Ø!×(Ò(¨Ñ9Ô9Ð9Ø—M’M &Ñ)Ô)Ð)àð DØ2 §¢¡¤Ð2Ð2Ð2�FØ!×(Ò(ð Iñô ð ð —M’M 6¨6°6¸6Ð"BÑCÔCÐCà#ŸLšL¨Ñ7Ô7�	ð —’ØaÐU^ÐaÐaÐaØñô ð ð  ŸLšL™NœN¨7Ô3�	ðð %ðð ð �ð" —’˜u f˜oÑ.Ô.Ð.Ø—’˜E 6Ñ*Ô*Ð*Ø—|’|‘~”~�à.Ð.¨Ð.Ñ.Ô.°	Ð9ðe2:ð 2:ð 2:ð 2:ñ 2:ô 2:ð 2:ð3	:ð 3	:ð 3	:ð 3	:ñ 3	:ô 3	:ð 3	:ð 3	:ð2:ð 2:ð 2:ð 2:øøøð 2:ð 2:ð 2:ð 2:ð 2:ð3	:ð 3	:ð 3	:ñ 3	:ô 3	:ð 3	:ð 3	:ð 3	:ð 3	:ð 3	:ð 3	:ð 3	:øøøð 3	:ð 3	:ð 3	:ð 3	:ð 3	:ð 3	:s5   •FªEFÅ-FÆF
	Æ
FÆF
	ÆFÆF#Æ&F#úList[Dict[str, Any]]c                ó:  — |                       ¦   «         5 }|                     ¦   «         5 }|                     d¦  «         |                     ¦   «         }d„ |D ¦   «         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 )zIFetch all owned equipment extracted directly from real purchase invoices.aF  
                    SELECT 
                        e.id, e.equipment_code, e.equipment_name, e.equipment_type,
                        e.license_plate,
                        e.brand_model, e.ownership_type, e.hourly_rate_standard,
                        e.fuel_norm_per_hour, e.status, e.purchase_cost_vnd,
                        e.purchase_invoice_series_number, e.supplier_name,
                        e.source_invoice_id, e.notes, e.created_at,
                        p.project_name as current_project_name,
                        p.project_code as current_project_code
                    FROM erp_equipment e
                    LEFT JOIN projects p ON e.current_project_id = p.id
                    WHERE e.ownership_type = 'owned'
                    ORDER BY e.purchase_cost_vnd DESC, e.created_at DESC;
                c                ó,   — g | ]}t          |¦  «        ‘ŒS r   r   r   s     r   r   z:TruthQueriesMixin.list_truth_equipment.<locals>.<listcomp>]   s   € Ð*Ð*Ð* •D˜‘G”GÐ*Ð*Ð*r   N)r   r    r%   r'   )r(   r)   r*   r1   s       r   Úlist_truth_equipmentz&TruthQueriesMixin.list_truth_equipmentJ   s7  € à× Ò Ñ"Ô"ð 	+ d¨D¯KªK©M¬Mð 	+¸SØ�KŠKð ñ ô ð ð —<’<‘>”>ˆDØ*Ð* TÐ*Ñ*Ô*ð#	+ð 	+ð 	+ð 	+ñ 	+ô 	+ð 	+ð 	+ð 	+ð 	+ð 	+ñ 	+ô 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+øøøð 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+ñ 	+ô 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+øøøð 	+ð 	+ð 	+ð 	+ð 	+ð 	+s4   •Bª5A8ÁBÁ8A<	Á<BÁ?A<	Â BÂBÂBúDict[str, Any]c           	     ó^  — |                       ¦   «         5 }|                     ¦   «         5 }|                     d¦  «         |                     ¦   «         }|                     d¦  «         |                     ¦   «         }|                     d¦  «         |                     ¦   «         }|d         t          |d         ¦  «        |d         t          |d         ¦  «        |d         d	„ |D ¦   «         d
œ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 )zEAggregate summary metrics of Truth equipment and warehouse materials.a  
                    SELECT 
                        COUNT(*) as total_owned_equipment,
                        COALESCE(SUM(purchase_cost_vnd), 0) as total_equipment_value_vnd
                    FROM erp_equipment
                    WHERE ownership_type = 'owned';
                ub  
                    SELECT 
                        COUNT(*) as total_material_lots,
                        COALESCE(SUM(total_value_vnd), 0) as total_warehouse_value_vnd,
                        COUNT(*) FILTER (WHERE status = 'CHá»œ_XUáº¤T_CÃ”NG_TRÆ¯á»œNG') as pending_dispatch_count
                    FROM erp_warehouse_inventory;
                a  
                    SELECT material_group, COUNT(*) as count, COALESCE(SUM(total_value_vnd), 0) as total_value
                    FROM erp_warehouse_inventory
                    GROUP BY material_group
                    ORDER BY total_value DESC;
                Útotal_owned_equipmentÚtotal_equipment_value_vndÚtotal_material_lotsÚtotal_warehouse_value_vndÚpending_dispatch_countc                ó,   — g | ]}t          |¦  «        ‘ŒS r   r   )r   Úgs     r   r   z7TruthQueriesMixin.get_truth_summary.<locals>.<listcomp>Š   s   € Ð'IÐ'IÐ'I°A­¨Q©¬Ð'IÐ'IÐ'Ir   )r9   r:   r;   r<   r=   Úgroup_breakdownN)r   r    r%   r&   r'   Úfloat)r(   r)   r*   Úeq_statÚwh_statr@   s         r   Úget_truth_summaryz#TruthQueriesMixin.get_truth_summary_   sì  € à× Ò Ñ"Ô"ð *	 dØ—’‘”ð ) #à—’ð ñ ô ð ð Ÿ,š,™.œ.�ð —’ð ñ ô ð ð Ÿ,š,™.œ.�ð —’ð ñ ô ð ð #&§,¢,¡.¤.�ð .5Ð5LÔ-MÝ16ØÐ ;Ô<ñ2ô 2ð ,3Ð3HÔ+IÝ16ØÐ ;Ô<ñ2ô 2ð /6Ð6NÔ.OØ'IÐ'I¸Ð'IÑ'IÔ'Iðð ð=)ð )ð )ð )ñ )ô )ð )ð*	ð *	ð *	ð *	ñ *	ô *	ð *	ð *	ð)ð )ð )ð )øøøð )ð )ð )ð )ð )ð*	ð *	ð *	ñ *	ô *	ð *	ð *	ð *	ð *	ð *	ð *	ð *	øøøð *	ð *	ð *	ð *	ð *	ð *	s5   •D"ªCD	Ã0D"Ä	D	ÄD"ÄD	ÄD"Ä"D&Ä)D&)NNNr   r   )r   r	   r
   r	   r   r	   r   r   r   r   r   r   )r   r3   )r   r7   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r6   rD   r   r   r   r   r   	   sm   € € € € € ØRÐRð &*Ø!Ø!ØØð<:ð <:ð <:ð <:ð <:ð|+ð +ð +ð +ð*,ð ,ð ,ð ,ð ,ð ,r   r   )Ú
__future__r   ÚloggingÚtypingr   Ú	getLoggerÚloggerr   r   r   r   ú<module>rN      s�   ðØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø Ð Ð Ð Ð Ð à	ˆÔ	Ð9Ñ	:Ô	:€ðBð Bð Bð Bð Bñ Bô Bð Bð Bð Br   