
    siR                        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l
mZ d dlmZ d dlmZmZmZ d dlmZmZ d d	lmZ d d
lmZ  e	ee      d        Z e	ee      d        Zd Zd Z e	ee      d        Zd Zy)    )datetime	timedelta)apps)post_migrate	post_save
pre_delete)receiver)gettext_lazy)strtime_seconds)
AttendanceAttendanceGeneralSettingWorkRecords)CompanyPenaltyAccounts)Employee)get_horilla_model_class)senderc                    t        |j                        }t        |j                        }|j                  sdt	        d      }}n6||k\  rdt	        d      }}n#||dz  k\  rdt	        d      }}ndt	        d	      }}	 t
        j                  j                  |j                  |j                  
      \  }}|j                  _	        |j                  |_        |j                  |_        |j                  |_        ||_        ||_        ||_        ||_        d|_        ||_        |j6                  |_        |j                  r||dz  kD  rdnd|_        |j:                  r|dk(  rt	        d      n
t	        d      }|j<                  sdt	        d      }}||_        ||_        |j?                          y# t
        j                  $ rs t
        j                  j                  |j                  |j                  
      }	|	j                         }|	j                  |j                        j                          Y t         $ r}
t#        |
       Y d}
~
d}
~
ww xY w)z8
    Handle post-save actions for Attendance model.
    CONFzValidate the attendanceFDPPresent   HDPzIncomplete minimum hourABSzIncomplete half minimum hour)dateemployee_id)idNTg      ?g      ?zHalf day leavezAn approved leave existszCurrently working) r   minimum_hourattendance_worked_hourattendance_validated_r   objectsget_or_createattendance_dater   MultipleObjectsReturnedfilterfirstexcluder   delete	Exceptionprintr   at_workmin_hourmin_hour_secondat_work_secondwork_record_typemessageis_attendance_recordattendance_idshift_idday_percentageis_leave_recordattendance_clock_outsave)r   instancekwargsr.   r/   statusr1   work_recordcreatedwork_recordses              6/var/www/python-projects/worksol/attendance/signals.pyattendance_post_saverA      s0   
 &h&;&;<O$X%D%DEN(( !$=">	?	*9	?Q.	.#<!=#A!B*22@@)) ,,  A  
W '22K//K"99K#00K"1K!/K#)K !K'+K$ (K#,,K$$"_q%88Dd 	" ""#)U?A:T8U 	 ((#6!7#)K !KK .. 9"**11)) ,, 2 
 #((*/668 as   <8F/ /BI4I<IIc                 |    |j                   j                         D ]  }|j                  r|j                          ! y )N)workrecords_setallleave_request_idr)   )r   r9   r:   
workrecords       r@   handle_attendance_deletionrG   N   s5    ..224  
**     c                    | j                   dvry ddlm}m} 	 |j                  j                  dd      }|j                         sy |j                  j                         }|D ci c]  }|j                  |j                  f| }}g }|D ]T  }	|j                  |	j                  |	j                  f      }
|
r|
|	_        |j                  |	       E|	j                          V |r8|j                  j                  |dgd       t!        d	t#        |       d
       y y c c}w # t$        $ r}t!        d|        Y d }~y d }~ww xY w)N
attendanceleaver   )r   r   T)r2   attendance_id__isnullr3     
batch_sizeSuccessfully updated z* work records with attendance information.z-Error updating work records with attendance: )labelattendance.modelsr   r   r"   r&   existsrD   r   r$   getr   r3   appendr)   bulk_updater+   lenr*   )r   r:   r   r   r>   attendancesattattendance_maprecords_to_updaterecordrK   r?   s               r@   $add_missing_attendance_to_workrecordr^   V   s]   ||229C"**11!%T 2 
 ""$ ((,,.CN
<?S__c112C7
 
 " 	 F'++V-?-?,MNJ'1$!((0	  ++!O#4 ,  ',=(>'??ij	 
*  C=aSABBCs0   -D" D" % DBD" D" "	E+D>>Ec                    | j                   dvry 	 t        j                  j                  dd      }|j	                         sy g }|D ]`  }|j
                  r|j
                  j                  |_        n%|j                  j                  j                  |_        |j                  |       b |r<t        j                  j                  |dgd       t        dt        |       d       y y # t        $ r}t        d	|        Y d }~y d }~ww xY w)
NrJ   T)r2   shift_id__isnullr4   rN   rO   rQ   z% work records with shift information.z4Error updating work records with shift information: )rR   r   r"   r&   rT   r3   r4   r   employee_work_inforV   rW   r+   rX   r*   )r   r:   r>   r\   r]   r?   s         r@    add_missing_shift_to_work_recordrb      s   ||22J"**11!% 2 
 ""$" 	-F##"("6"6"?"?"("4"4"G"G"P"P$$V,	- ++!J<C ,  ',=(>'??de	   JDQCHIIJs   1C) B$C) )	D
2DD
c                     t         j                  j                  d       |r!t         j                  j                  |       yy)z
    Signal receiver that automatically creates an AttendanceGeneralSetting object
    whenever a new Company is created. This does NOT skip creation during
    loaddata, so the object will also be created when fixture data is loaded.
    N)
company_id)r   r"   r#   )r   r9   r=   rawr:   s        r@   create_attendance_settingrf      s:     $$22d2C ((66(6K rH   c           
         | j                   dvry t        j                  j                         }t        j                  j                         }|j                         r1|j                  d      j                  }|D ]  }	 |j                  j                  xs |}t        j                         j                         }t        t        j                  j                  |j                        j                  dd            }t!        ||z
  j"                        D 	ch c]  }	|t%        |	      z    }
}	|
|z
  }|D cg c]%  }t	        ||d|j                  j&                        ' }}|r"t        j                  j)                  |d	d
        y y c c}	w c c}w # t*        $ r}t-        d| d|        Y d }~?d }~ww xY w)N)rK   r   )r   T)flat)daysDFT)r   r   r0   r4   rN   )rP   ignore_conflictsz1Error creating missing work records for employee z: )rR   r   r"   rD   r   rT   earliestr   ra   date_joiningr   todaysetr&   r   values_listrangeri   r   r4   bulk_creater*   r+   )r   r:   	employeesr>   st_dateemployee
start_dateend_dateexisting_datesi	all_datesmissing_datesmissing_datework_records_to_creater?   s                  r@   create_missing_work_recordsr~      s   ||>)  $$&I&&**,L''/44! #	H"%88EEP
#>>+002!$''..8;;.GSST T " #Hz$9#?#?@ !22	  !*N : )6* %  $,)).!)!<!<!E!E	*& * *''33.3QU 4 9#	 *  GzQSTUSVW s7   ?BFF/F:*F$&F
F	F>"F99F>N) r   r   django.appsr   django.db.models.signalsr   r   r   django.dispatchr	   django.utils.translationr
   r!   attendance.methods.utilsr   rS   r   r   r   base.modelsr   r   employee.modelsr   horilla.methodsr   rA   rG   r^   rb   rf   r~    rH   r@   <module>r      s    )  H H $ 6 4 O O 0 $ 3 
)J'9 (9x 
*Z(  ) %CRJD 
)G$L %L-rH   