
    #A3i                         S SK JrJrJr  S SKJrJr  S SKJrJrJ	r	J
r
  S SKJr  S SKJr  S SKJr  S SKJr   " S S	5      rg
)    )ListDictOptional)datedatetime)QSumCountF)timezone)BalanceDetail)Paycode)Userc                   X    \ rS rSrSr\ SS\\   S\S\S\	\\      S\
4
S	 jj5       rS
rg)PaycodeAggregationService
   z8Service for aggregating paycode hours from BalanceDetailNuser_idsperiod_start
period_end	stamp_idsreturnc           
         [        U UUS9n[        R                  R                  U5      R	                  SS5      R                  SSS5      n0 n0 nU GH  n[        UR                  R                  5      n	UR                  R                   SUR                  R                   3n
X;  a  U	U
0 S.Xi'   UR                  nUR                  nUR                  nXU	   S   ;  a  UUSSS	.Xi   S   U'   UR                  =(       d    UR                  =(       d    SnXi   S   U   S
==   U-  ss'   Xi   S   U   S==   S-  ss'   X;  a  UUSS.X|'   X|   S
==   U-  ss'   GM     / nSnUR                  5        H  u  n	n/ nSnUS   R                  5        H>  u  nnUS
   S-  nUU-  nUU-  nUR!                  UUS   [#        US5      US   S.5        M@     UR!                  U	US   [%        US S9[#        US5      S.5        M     UR'                  S S9  [%        UR)                  5       S S9 Vs/ s H  nUS   US   [#        US
   S-  S5      S.PM!     nnU[+        U5      [#        US5      US.S.$ s  snf )aO  
Aggregate paycode hours for multiple users in a period from BalanceDetail

Args:
    user_ids: List of user UUIDs
    period_start: Start date of period
    period_end: End date of period
    stamp_ids: Optional list of specific stamp IDs (not used for BalanceDetail aggregation)
    
Returns:
    Dictionary with aggregated data:
    {
        'employees': [
            {
                'user_id': str,
                'user_name': str,
                'paycodes': [
                    {
                        'paycode': str,
                        'paycode_name': str,
                        'total_hours': float,
                        'stamp_count': int  # Number of days/records
                    }
                ],
                'total_hours': float
            }
        ],
        'summary': {
            'total_employees': int,
            'total_hours': float,
            'paycode_totals': [
                {
                    'paycode': str,
                    'paycode_name': str,
                    'total_hours': float
                }
            ]
        }
    }
)user_id__in	date__gte	date__lteuserpaycoder    )user_id	user_namepaycodesr!   r   )r   paycode_nametotal_secondsrecord_countr#   r$      )r   r"   r#   g        g      @r"      )r   r"   total_hoursstamp_countr    c                     U S   $ Nr"    xs    5D:\TimeStamps\payroll\services\aggregation_service.py<lambda>CPaycodeAggregationService.aggregate_paycode_hours.<locals>.<lambda>   s	    .@Q    )key)r   r    r!   r'   c                     U S   $ )Nr    r+   r,   s    r.   r/   r0      s    AkNr1   c                     U S   $ r*   r+   r,   s    r.   r/   r0      s	    AnDUr1   )r   r"   r'   )total_employeesr'   paycode_totals)	employeessummary)r   r   objectsfilterselect_relatedorder_bystrr   id	firstnamelastnamer   namenet_work_secondstotal_work_secondsitemsappendroundsortedsortvalueslen)r   r   r   r   querybalance_detailsemployee_datar6   balance_detailr   r    r   paycode_valuer"   work_secondsresult_employeestotal_hours_all	user_datauser_paycodesuser_total_hourspaycode_datapaycode_hourspcpaycode_totals_lists                           r.   aggregate_paycode_hours1PaycodeAggregationService.aggregate_paycode_hours   s   `  " 
 (//66u=LLI

(69f
- 	
 -N.--001G)..889>;N;N;W;W:XYI +&!* "*& %,,G#OOM"<<L '$::$FF,$0%&$%	E&z2=A *::dn>_>_dcdL":.}=oNR^^N":.}=nMQRRM 2,$0%&1-
 )/:lJ:O .T "/"5"5"7GYM"/8/D/J/J/L+| ,_ = F M1 =0$$,$0$@#(#:#/#?	&  0M ##"&{3"=6QR$%5q9	% # #82 	":; ^224:UV
 W	 i= "> 2$R%86%A1E
 W 	 
 *#&'7#8$_a8"5
 	

s   0&I4r+   )N)__name__
__module____qualname____firstlineno____doc__staticmethodr   r=   r   r   r   rZ   __static_attributes__r+   r1   r.   r   r   
   s^    B
 *.	W
s)W
W
 W
 DI&	W

 
W
 W
r1   r   N)typingr   r   r   r   r   django.db.modelsr   r	   r
   r   django.utilsr   balancedetail.modelsr   paycode.modelsr   user.modelsr   r   r+   r1   r.   <module>ri      s,    ' ' # - - ! . " [
 [
r1   