
    siR                        d dl Z d dlmZmZ d dlmZ d dlmZ 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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mZ d dl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+m,Z, d dl-m.Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z=m>Z> d dl?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF d dlGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZP de%fdZQi g fdZRd ZSe* e5dd       d!               ZTe*e) e5dd       d"                      ZUe*e) e,d#      d$                      ZVe* e,d%      d&               ZWe* e7d'      d(               ZXe* e7d)      d*               ZYe* e5d+      d,               ZZe* e,d-      d.               Z[e* e,d/      d0               Z\e*e) e5d1      d2                      Z]e*e) e5d1      d3                      Z^e*e) e5d4d       d]d5                     Z_e*d6        Z`e* e+d78      d9               Zae* e,d:      d;               Zbe* e8d<      d=               Zce* e5d>d       d?               Zde* e5d@      dA               Zee* e8dB      dC               Zfe*e)dD               Zge* e,dE       e6dF      dG                      Zhe* e,dE      dH               Zie*e) e6dF      dI                      Zje*e) e6dF      dJ                      Zke* e6dF      dK               Zle*e) e6dF      dL                      Zme* e6dF       e,dM      dN                      Zne*e) e,dO      dP                      Zoe* e,d+      dQ               ZpdR Zqe* e5g dS8      dT               Zre* e5ddUg      dV               Zs ej                  dW      re* e5dg      dX               Zu ej                  dY      re* e5d      dZ               Zve* e5d      d[               Zwe* e5d      d\               Zxy)^    N)datetime	timedelta)parse_qs)apps)messages)User)	Paginator)HttpResponseJsonResponse)redirectrender)reverse)gettext_lazy)intial_notice_period)closest_numberseval_validatepaginator_qrysortby)
DepartmentJobPosition)general_settings)Employee)horilla_middlewares)hx_request_requiredlogin_requiredmanager_can_enterpermission_required)group_by_queryset)get_horilla_model_class)notify)any_manager_can_entercheck_feature_enabledoffboarding_manager_can_enter&offboarding_or_stage_manager_can_enter)LetterFilterLetterReGroupPipelineEmployeeFilterPipelineFilterPipelineStageFilter)NoteFormOffboardingEmployeeFormOffboardingFormOffboardingStageFormResignationLetterFormStageSelectFormTaskForm)	EmployeeTaskOffboardingOffboardingEmployeeOffboardingGeneralSettingOffboardingNoteOffboardingStageOffboardingStageMultipleFileOffboardingTaskResignationLetteremployeec                    t         j                  j                  |       j                         t        j                  j                  |       j                         z  t
        j                  j                  |       j                         z  S )zh
    This method is used to check the employee is in managers
    employee: Employee model instance
    )managers)r2   objectsfilterexistsr6   r8   )r:   s    5/var/www/python-projects/worksol/offboarding/views.pyany_managerrA   @   su     	""H"5<<>

"
"
)
)8
)
<
C
C
E	F

!
!
(
((
(
;
B
B
D	E    c           	      J   g }t        t        j                  dd       }|D ]  }g }t        | |j                  j                               j                  j                  d      }g }|g g d}|D ]H  }	|j                  |	g d       t        | t        j                  j                  |	            j                  j                  d      }
|r`|j                  j                  d      sEt        |j                  j                         s&|
j                  |j                  j                   	      }
d
|	j"                  z   t%        |j&                        z   }t)        |
d| j+                  |      |      j,                  }||	j.                  j                         D cg c]  }|j&                   c}z   }|d   |z   |d<   K g }|d   }|D ]>  }	d}|D ]  }|d   |	k(  s|j                  |       d} n |r,|j                  d|	i       @ |||d}|j                  |        |S c c}w )Nrequest)querysetid)offboardingstages	employees)grouperliststage_idstage_id__idoffboarding.view_offboardingemployee_idpagerM   rH   FrJ   T)rG   rH   employee_ids)getattrr   _thread_localsr)   offboardingstage_setallqsorder_byappendr'   r3   r=   r>   userhas_permrA   employee_gettitlestrrF   group_bygetobject_listoffboardingemployee_set)filtersoffboardingsgroupsrD   rG   rI   rH   all_stages_grouperdatastagestage_employees	page_nameemployee_grouperr:   ordered_datagroupersfoundrJ   s                     r@   pipeline_grouperrp   L   sC   F)88)TJG# 4	$k>>BBD

"XXd^ 	  *brJ 	?E%%%&DE4#++22E2B b.) 
 %%&DEw||889"1"8"8 ' 9 9 #9 # ,s;>>/BBI'I&	 
 k  ",1,I,I,M,M,O% (% I "(^.>>DN3	?6  > 	8EE# 9%. ''0 E	
 ##Y$67	8 '"%

 	di4l M3%s   H 
c                 @    t        | d      }|j                  |      } | S )zA
    This method is used to generate common paginator limit.
       )r	   get_page)qrysetpage_number	paginators      r@   !paginator_qry_offboarding_limitedrw      s%     &!$I,FMrB   rO   T)offboarding_employee_can_enterc                    t               j                  }t        || j                  j	                  d            }t        i |      }|D ]  }t        |d   d|d           i }|D ]$  }t        |      |t        |j                        <   & t        | j                  j                               }t        | d||t               t               t               ||t        j                          j#                         d      S )z#
    Offboarding pipeline view
    rR   rG   rH   rG   z"offboarding/pipeline/pipeline.html)re   paginated_offboardingsemployee_filterpipeline_filterstage_filterstage_formsfilter_dicttoday)r(   rX   rw   GETra   rp   setattrr/   r_   rF   r   	urlencoder   r'   r)   r   r   date)rD   re   r{   rf   itemr   rG   r   s           r@   pipeliner      s     "#&&L>gkkoof-
 b"89F ?]#XtH~>? K- T+:{+SC'(T 7;;0023K,"&<57-//1&&^^%**,		
 rB   c                    t        | j                        j                  }t        || j                  j	                  d            }t        | j                  |      }|D ]  }t        |d   d|d           i }|D ]$  }t        |      |t        |j                        <   & t        | d|||t        | j                  j                               d      S )z8
    This method is used filter offboarding process
    rR   rG   rH   rz   z&offboarding/pipeline/offboardings.html)re   r{   r   r   )r(   r   rX   rw   ra   rp   r   r/   r_   rF   r   r   r   )rD   re   r{   rf   r   r   rG   s          r@   filter_pipeliner      s     "'++.11L>gkkoof- gkk+ABF ?]#XtH~>?K- T+:{+SC'(T0"&<&#GKK$9$9$;<		
	 	rB   zoffboarding.add_offboardingc                    t        t        | j                  j                  d                  }d}|r>t	        |t
              r.t        j                  j                  |      j                         }t        |      }| j                  dk(  rt        | j                  |      }|j                         r|j                         }t        j                   | t#        d             |j$                  j'                         D cg c]  }|j(                   }}t+        j,                  | j.                  j0                  |ddd	d
ddt3        d      	       t5        d      S t7        | dd|i      S c c}w )z!
    Create offboarding view
    instance_idNrF   instancePOSTzOffboarding savedz(You are chosen as an offboarding manageruA   لقد تم اختيارك كمدير عملية المغادرةu.   Sie wurden als Offboarding-Manager ausgewähltz,Has sido elegido como gerente de offboardingu@   Vous avez été choisi comme responsable du processus de départpeople-circleoffboarding-pipeline	recipientverbverb_arverb_deverb_esverb_friconr   )<script>window.location.reload()</script>zoffboarding/pipeline/form.htmlform)r   r_   r   ra   
isinstanceintr2   r=   r>   firstr,   methodr   is_validsaver   success_r<   rW   employee_user_idr    sendr[   r]   r   r
   r   )rD   r   r   r   off_boardingr:   userss          r@   create_offboardingr      s;     GKKOOM$B CDKHz+s3&&---=CCEH-D~~w||h?==?99;LWa(;&<=:F:O:O:S:S:U.6))E  KK))?[HFZ$ !78
   KLL(D	
 #s   =E'zoffboarding.delete_offboardingc                 X   	 t         j                  j                  |      }|j                          t	        j
                  | t        d             t        t              S # t         j                  t        f$ r0 t	        j                  | t        d             Y t        t              S w xY w)z4
    This method is used to delete offboardings
    r   zOffboarding deletedzOffboarding not found)r2   r=   ra   deleter   r   r   DoesNotExistOverflowErrorerrorr   r   )rD   rF   rG   s      r@   delete_offboardingr     s    <!))---4!$9":; O$$ $$m4 <w"9 :;O$$<s   AA   8B)(B)z offboarding.add_offboardingstagec                    | j                   d   }t        t        | j                   j                  d                  }d}|r0t	        |t
              r t        j                  j                  |      }t        j                  j                  |      }t        |      }||j                  _        | j                  dk(  rt        | j                  |      }|j                         r|j                  d      }||_        |j                          |j                   j#                  |j$                  j'                  d	             t)        j*                  | t-        d
             |j                   j/                         D cg c]  }|j0                   }}t3        j4                  | j6                  j8                  |ddddddt;        d      	       t=        d      S t?        | dd|i      S c c}w )z?
    This method is used to create stages for offboardings
    offboarding_idr   Nr   r   r   Fcommitr<   zStage savedz+You are chosen as offboarding stage manageruN   لقد تم اختيارك كمدير لمرحلة عملية المغادرةu8   Sie wurden als Manager der Offboarding-Phase ausgewähltz8Has sido elegido como gerente de la etapa de offboardingu?   Vous avez été choisi comme responsable de l'étape de départr   r   r   r   zoffboarding/stage/form.htmlr   ) r   r   r_   ra   r   r   r6   r=   r2   r-   r   r   r   r   r   r   r<   setrh   getlistr   r   r   rW   r   r    r   r[   r]   r   r
   r   )rD   r   r   r   rG   r   r:   r   s           r@   create_stager     s    [[!12NGKKOOM$B CDKHz+s3#++//;/?%%))^)<K2D#.DMM ~~#GLL8D==?yyy.H&1H#MMO!!$))"3"3J"?@Wa&67?G?P?P?T?T?VW8X..WEWKK))BhRRY$ !78
   KLL'864.II Xs   =G'z#offboarding.change_offboardingstagec                 x   t         j                  j                  |      }| j                  dk(  r	 t	        j
                  | j                  j                  dd            }t        |      D ];  \  }}|j                  j                  |      }|dz   |_	        |j                          = t        j                  | d       t        ddi      S |j                  j#                  d      }t%        | d||d      S # t        $ r9}t        j                  | d	       t        d
t!        |      dd      cY d}~S d}~ww xY w)zM
    This method is used to update the stage sequence of the offboarding
    r   r   orderz[]   zSequence Updated Successfullystatusr   zError Updating Sequence..r   )r   messagei  r   Nsequencez!cbv/exit_process/stage_order.html)rH   rG   )r2   r=   ra   r   jsonloadsr   	enumeraterV   r   r   r   r   r   	Exceptionr   r_   rY   r   )	rD   pkrG   r   indexrM   ri   erH   s	            r@   update_stage_orderr   ;  s%    %%))R)0K~~
	TJJw||//>?E#,U#3 x#88<<<I!&

 W&EF9 566
 --66zBF+&	
   	TNN7$?@7s1v FsSS	Ts   BC7 7	D9 .D4.D94D9z#offboarding.add_offboardingemployeec                 X   t        |       d   rt        |       d   nd}t        j                         t        |      z   }| j                  d   }t        t        | j                  j                  d                  }d}|r0t        |t              r t        j                  j                  |      }t        j                  j                  |      }t        ||d|	      }||j                  _        | j                   d
k(  rt        | j"                  |      }|j%                         r|j'                  d      }||_        |j'                          t)        j*                  | t-        d             |st/        j0                  | j2                  j4                  |j6                  j8                  d| d|j:                   d| d|j:                   d| d|j:                   dd| d|j:                   d| d|j:                   t=        d      d	       t?        d      S tA        | dd|i      S )z:
    This method is used to add employee to the stage
    get_initial_notice_periodr   daysrM   r   Nr   )rM   notice_period_endsinitialr   r   r   Fr   zEmployee savedzYou have been added to the z of u"   لقد تمت إضافتك إلى u    من zDu wurdest zu z von u    hinzugefügtu   Has sido añadido a z de u   Vous avez été ajouté à r   informationr   r   r   r   r   r   r   r   r   zoffboarding/employee/form.htmlr   )!r   r   r   r   r   r   r_   ra   r   r   r3   r=   r6   r+   r   rM   r   r   r   r   r   r   r   r    r   r[   r]   rQ   r   r   r   r
   r   )rD   default_notice_periodend_daterM   r   r   ri   r   s           r@   add_employeer   \  s     ()DE 	W%&AB 
 ~~)1F"GGH{{:&HGKKOOM$B CDKHz+s3&..22k2B$$((H(5E""(ChD #DMM~~&w||hG==?yyy.H %HMMOWa(8&9:LL--&22CC6ugT%BVBVAWX@veNbNbMcd,UG59M9M8Nm\25'e>R>R=ST9%UEYEYDZ[$%;<&
   KLL';fd^LLrB   z&offboarding.delete_offboardingemployeec                 (   | j                   j                  d      }t        j                  j	                  |      }|r|j                          t        j                  | t        d             t        j                  | j                  j                  t        j                  j	                  |j                  dd            ddd	d
dt        d      d	       t#        t$              S t        j                   | t        d             t#        t$              S )z@
    This method is used to delete the offboarding employee
    rS   id__inzOffboarding employee deletedemployee_id__employee_user_idTflatz*You have been removed from the offboardingu7   لقد تمت إزالتك من إنهاء الخدمةz'Du wurdest aus dem Offboarding entferntz"Has sido eliminado del offboardingu(   Vous avez été retiré de l'offboardingr   r   r   zEmployees not found)r   r   r3   r=   r>   r   r   r   r   r    r   r[   r]   r   values_listr   r   r   r   )rD   rS   	instancess      r@   delete_employeer     s     ;;&&~6L#++22,2GI!$B"CDLL%%ll)) ,,-LSW,X *  >M=8>34	
 O$$ 	w"7 89O$$rB   z#offboarding.delete_offboardingstagec                    | j                   j                  d      }	 t        j                  j	                  |      }|r0|j                          t        j                  | t        d             nt        j                  | t        d             t        d      S # t        $ r, t        j                  | t        d             Y t        d      S w xY w)z>
    This method  is used to delete the offboarding stage
    idsr   zStage deletedzStage not foundr   )r   r   r6   r=   r>   r   r   r   r   r   r   r
   )rD   r   r   s      r@   delete_stager     s     ++

e
$C6$,,3333?	Wa&89NN7A&7$89 CDD  6w"3 45CDD6s   A1B (CCzoffboarding.change_offboardingc                    | j                   j                  d      }| j                   d   }t        j                  j	                  |      }t
        j                  j                  |      }|D ]  }||_        |j                           |j                  dk(  rdnd}|j                  dd	      }t        j                  j	                  || 
      j                  |       i }t        |j                        |t        |j                  j                         <   t#        j$                  | j&                  j(                  t*        j                  j	                  |j                  dd	            dddddt-        d      d	       t/        i |j                  g      }|D ]  }	t1        |	d   d|	d           t3        | d|d   |t5        d      t7        j8                         j;                         d      S )B
    This method is used to update the stages of the employee
    rS   rM   r   r   archivedFTemployee_id__idr   )r   	is_activer   rz   r   "Offboarding stage has been changed2   تم تغيير مرحلة إنهاء الخدمة%   Die Offboarding-Stufe wurde geändert&Se ha cambiado la etapa de offboarding'   L'étape d'offboarding a été changéer   r   r   rG   rH   'offboarding/stage/offboarding_body.htmlr   zstage changed successfully.rG   r   response_messager   )r   r   r3   r=   r>   r6   ra   rM   r   typer   r   updater/   r   r_   rF   r    r   r[   r]   r   r   rp   r   r   r   r   r   r   )
rD   rS   rM   rI   ri   r:   target_stater   rf   r   s
             r@   change_stager     s    ;;&&~6L{{:&H#++22,2GI$$((H(5E  ! "JJ*45$L(():(FL""   f|f$K0?((1KE((++,- KK!!,,%%(()Ht(T & 
 2D789/0 b5#7#7"89F ?]#XtH~>?1!!9& !"? @^^%**,		
	 	rB   c                    | j                   j                  d      }| j                   d   }t        j                  j	                  |      }t
        j                  j                  |      }|D ]  }||_        |j                           |j                  dk(  rAt        j                  j	                  |j                  dd            j                  d	
       i }t        |j                        |t        |j                  j                         <   t#        j$                  | j&                  j(                  t*        j                  j	                  |j                  dd            dddddt-        d      d	       t/        i |j                  g      }|D ]  }t1        |d   d|d           ddlm}	 |	j7                         S )r   rS   rM   r   r   r   r   Tr   Fr   rz   r   r   r   r   r   r   r   r   r   rG   rH   r   )HorillaFormView)r   r   r3   r=   r>   r6   ra   rM   r   r   r   r   r   r/   r   r_   rF   r    r   r[   r]   r   r   rp   r   horilla_views.generic.cbv.viewsr   r
   )
rD   rS   rM   rI   ri   r:   r   rf   r   r   s
             r@   change_offboarding_stager     s    ;;&&~6L{{:&H#++22,2GI$$((H(5E  ! zzZ(():(F 	  	

&5&
!K0?((1KE((++,- KK!!,,%%(()Ht(T & 
 2D789/0 b5#7#7"89F ?]#XtH~>??''))rB   z offboarding.view_offboardingnotec                    | j                   r| j                   j                  d      }| j                  d   }t        j                  j                  |      }g }|D ]4  }t               }||_        |j                          |j                  |       6  |j                  j                  |  |}t        j                  j                  |      }	t        | dd|	i      S )zE
    This method is used to render all the notes of the employee
    filesnote_idr    offboarding/note/view_notes.htmlr:   )FILESr   r   r5   r=   ra   r7   
attachmentr   rZ   attachmentsaddr3   r   )
rD   rQ   r   r   noter   filer   offboarding_employee_idr:   s
             r@   
view_notesr     s     }}%%g.++i(&&**g*6 	+D57J$(J!OOz*		+
 	k*)"**..2I.JH*	
 rB   c                    | j                   d   }t        j                  j                  |      }t	               }| j
                  dk(  rt	        | j                  | j                        }||j                  _	        |j                         rF|j                          t        j                  | t        d             t        d|j                         S t#        | d||d      S )	zI
    This method is used to create note for the offboarding employee
    rQ   r   r   zNote added successfullyzview-offboarding-noterP   r   )r   r:   )r   r3   r=   ra   r*   r   r   r   r   rQ   r   r   r   r   r   r   rF   r   )rD   rQ   r:   r   s       r@   add_noter   >  s     ++m,K"**..+.>H:D~~gmm4$,!==?IIKWa(A&BC3MM* 	
 rB   z"offboarding.delete_offboardingNote)permc                 D   d}	 t         j                  j                  |      }|j                          t	        j
                  | t        d             t        |      S # t         j                  $ r. t	        j                  | t        d             d}Y t        |      S w xY w)z<
    This method is used to delete the offboarding note
     r   z'The note has been successfully deleted.zNote not found.r   )
r5   r=   ra   r   r   r   r   r   r   r
   )rD   r   scriptr   s       r@   offboarding_note_deleter  X  s     F=&&**g*6!$M"NO
 	 '' =w"3 45<	=s   AA 4BBz"offboarding.delete_offboardingnotec                     d}| j                   j                  d      }t        j                  j	                  |      j                          t        j                  | t        d             t        |      S )z#
    Used to delete attachment
    r  r   r   zFile deleted successfully)
r   r   r7   r=   r>   r   r   r   r   r
   )rD   r  r   s      r@   delete_attachmentr  j  s^     F
++

e
$C ((//s/;BBDWa ;<=rB   zoffboarding.add_offboardingtaskc                 D   | j                   j                  d      }t        t        | j                   j                  d                  }t        j
                  j                  |      }d}|r.t        j
                  j                  |      j                         }t        ||d|      }| j                  dk(  rYt        | j                  |d|i	      }|j                         r/|j                          t        j                  | t!        d
             t#        | dd|i      S )z6
    This method is used to add offboarding tasks
    rM   r   rL   Nr   )rM   tasks_tor   r   )r   r   z
Task Addedzoffboarding/task/form.htmlr   )r   ra   r   r_   r3   r=   r>   r8   r   r0   r   r   r   r   r   r   r   r   )rD   rM   r   rI   r   r   s         r@   add_taskr
  w  s     {{z*HGKKOOM$B CDK#++22H2EIH"**11[1AGGI !
 D ~~LLH
 ==?IIKWao6$D	
 rB   zoffboarding.change_employeetaskc                    | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  j                  ||      }|j                  |       t        j                  | t        d             t        j                  | j                  j                  t        j                  j                  |j                  dd	
            dddddt!        d      d	       t"        j                  j                  |      }i }	t%        |j&                        |	t)        |j&                  j*                        <   t-        i |j&                  g      }
|
D ]  }t/        |d   d|d           t1        | d|
d   |	t        d      d      S )zA
    This method is used to update the assigned tasks status
    rM   rS   task_idtask_status)employee_id__id__intask_id__idr   z#Task status updated successfully...#task_id__managers__employee_user_idTr   r   z(Offboarding Task status has been updatedu9   تم تحديث حالة مهمة إنهاء الخدمةz5Der Status der Offboarding-Aufgabe wurde aktualisiertz6Se ha actualizado el estado de la tarea de offboardingu8   Le statut de la tâche d'offboarding a été mis à jourr   r   r   r   rz   rG   rH   r   r   z!Task status changed successfully.)rG   r   r   )r   ra   r   r1   r=   r>   r   r   r   r   r    r   r[   r]   r   r   r   r6   r/   r   r_   rF   rp   r   r   )rD   argskwargsrM   rS   r  r   employee_taskri   r   rf   r   s               r@   update_task_statusr    s    {{z*H;;&&~6Lkkooi(G[[__]+F ((//(g 0 M 'Wa EFG
KK!!,,%% ,,5D -  & 

 8KGHJ/0 $$((H(5EK0?((1KE((++,- b5#7#7"89F ?]#XtH~>?1!!9& !"E F	
 rB   zoffboarding.add_employeetaskc           	         | j                   j                  d      }| j                   d   }t        j                  j	                  |      }t
        j                  j                  |      }|D ]+  }	 t               }||_        ||_	        |j                          - |j                         j                  j                  }i }t        |      |t        |j                         <   t#        i |j                  j                  g      }	|	D ]  }
t%        |
d   d|
d           t'        | d|	d	   |t)        d
      t+        j,                         j/                         d      S #  Y xY w)z9
    This method is used to assign task to employees
    rS   r  r   r   rz   rG   rH   r   r   zTask Assignedr   )r   r   r3   r=   r>   r8   ra   r1   rQ   r  r   r   rM   r   r/   r_   rF   rp   r   r   r   r   r   r   )rD   rS   r  rI   taskr:   assigned_taskrG   r   rf   r   s              r@   task_assignr    sJ    ;;&&~6Lkk)$G#++22,2GI""&&'&2D 	(NM(0M%$(M!  //#,,;;KK'6;'OKKNN#$b4==#?#?"@AF ?]#XtH~>?1!!9& !/ 2^^%**,		
	 		s   1(EE z"offboarding.delete_offboardingtaskc                 T   | j                   j                  d      }t        j                  j	                  |      }|r>|j                          t        j                  | t        d             t        t              S t        j                  | t        d             t        t              S )z0
    This method is used to delete the task
    task_idsr   zTask deletedzTask not found)r   r   r8   r=   r>   r   r   r   r   r   r   r   )rD   r  taskss      r@   delete_taskr    s     {{"":.H##**(*;E!N"34 O$$ 	w"2 34O$$rB   c                 h   t         j                  j                  |      }t        j                  j	                  |      }i }t
        j                  j	                  |j                  j                        }t        |j                  j                        |t        |j                  j                  j                        <   ||t        j                  ||d}| j                  j                  d      }|r3t        j                  |      }t        ||      \  }	}
||d<   |	|d<   |
|d<   t!        | d	|      S )
z
    This method is used to get the individual view of the offboarding employees
    parameters:
        emp_id(int): the id of the offboarding employee
    r   rP   )r   rz   )r:   r  choicesoffboarding_stagesr   requests_idspreviousnextz)offboarding/pipeline/individual_view.html)r3   r=   ra   r1   r>   r6   rM   r   r/   r_   rF   statusesr   r   r   r   r   )rD   emp_idr:   r  r   r  contextrequests_ids_jsonr   previous_idnext_ids              r@   offboarding_individual_viewr)    s!    #**..&.9H  ''F';EK)1188((77 9  =L%%44=KH%%447789 ((0"G  7zz"34.|VDW"3)
!'FPPrB   "offboarding.view_resignationletterresignation_requestc           
      $   ddi}t               }t        j                  j                         }t        j                  j                         }t        | dt        || j                  j                  d            |ddgi|t        j                  d      S )z=
    This method is used to view the resignation request
    r   	requestedz*offboarding/resignation/requests_view.htmlrR   	Requested)lettersfr   re   	gp_fields)r%   r9   r=   rW   r2   r   r   r   ra   r&   fields)rD   defatul_filterfilter_instancer/  re   s        r@   request_viewr5  (  s     ,N"nO''++-G&&**,L4$Wgkkoof.EF ${m4(&--	

 
rB   c                    t         j                  j                  |      }d|i}| j                  j                  d      }|r3t	        j
                  |      }t        ||      \  }}||d<   ||d<   ||d<   t        | d|      S )Nr   letterr   r!  r"  z0offboarding/resignation/request_single_view.html)r9   r=   ra   r   r   r   r   r   )rD   rF   r7  r%  r&  r   r'  r(  s           r@   request_single_viewr8  A  s     &&**b*1F&G  7zz"34.|R@W"3)
!: rB   c                 8   | j                   j                  d      r t        | j                        j                  }n*t
        j                  j                  | j                         }| j                  j                  d      }t        | j                  j                               }d}| j                  j                  d      dk(  rd}| j                  j                  d      rt        | |d      }|j                  d       |d	k7  r|t        ||| j                  j                  d
      d
      }|D cg c]  }|d   	 }}g }|D ].  }|j                  D ]  }	|j                  |	j                           0 t#        j$                  t'        |            }
d}n\t)        || j                  j                  d
            }t#        j$                  |j                  D 	cg c]  }	|	j                    c}	      }
| j                  j                  d      r|j                  d       | j*                  j                  d      r,| j*                  j                  d      j-                  d      rdnd}t/        | |||| j                  j                         ||
|d      S c c}w c c}	w )z9
    This method is used to search/filter the letter
    r*  )r   fieldz*offboarding/resignation/request_cards.htmlviewrK   z)offboarding/resignation/request_list.htmlr   r  rR   z%offboarding/resignation/group_by.htmlHTTP_REFERERemployee-profile/FT)r/  r   pd
paginationr   r:  )r[   r\   r%   r   rX   r9   r=   r>   ra   r   r   r   popr`   rb   rZ   rF   r   dumpsrK   r   METAendswithr   )rD   r/  r:  	data_dicttemplateentrylist_valuesid_listvaluer   r   r?  s               r@   search_resignation_requestrJ  V  s2    ||ABw{{+..#++22*1,, 3 
 KKOOG$E..01I;H{{v&(>{{x '84h{u(7E7;;??6+BFK29:uV}::  	,E!-- ,x{{+,	, zz$w-0:  )@Azzw?R?R"S88;;"ST{{vf <<N+LL^,556IJ 	 	  $++'')$(	
 + ; #Ts   1JJc                     t         j                  j                  |      }t        j                  j	                  |      }t        | d||d      S )NrP   r   z$cbv/resignation/resignation_tab.html)r/  r:   )r9   r=   r>   r   ra   r   )rD   r   r/  r:   s       r@   resignation_tabrL    sQ    
  ''..2.>G##r#*H.2 rB   c                    | j                   j                  d      }t        j                  j	                  |      j                          t        j                  | t        d             | j                  j                  d      r5| j                  j                  d      j                  d      rt        d      S t        d      S )zC
    This method is used to delete resignation letter instance
    
letter_idsr   zResignation letter deletedr<  r=  z/employee/employee-profile/resignation-request-view)r   r   r9   r=   r>   r   r   r   r   rB  ra   rC  r   )rD   r   s     r@   delete_resignation_requestrP    s     ++

l
+C$$C$0779Wa <=>||'GLL,<,<^,L,U,U- 566233rB   c                    t        t        | j                  j                  d                  }d}|r t        j
                  j                  |      }t        |      }| j                  dk(  rat        | j                  |      }|j                         r:|j                          t        j                  | t        d             t        d      S t        | dd	|i      S )
zK
    This method is used to render form to create resignation requests
    r   Nr   r   r   zResignation letter savedr   z!offboarding/resignation/form.htmlr   )r   r_   r   ra   r9   r=   r.   r   r   r   r   r   r   r   r
   r   )rD   r   r   r   s       r@   create_resignation_requestrR    s      GKKOOM$B CDKH$,,00K0@ (3D~~$W\\HE==?IIKWa(B&CD KLL'>OOrB   z$offboarding.change_resignationletterc                    | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        j                  d      r6t        dd      j                  j                  |d	      j                         nd
}|rt        j                  j                  |      }| j                   j                  d      }| j                   j                  dd
      }|r$t        j                  |d      j                         }t        j                         }	|r%t        j                  |d      j                         }n|r|t        |j                        z   }nd
}|s|	}t         j                  j                  |      }
|dv r|
D ]  }||_        |j%                          |dk(  r|j'                         t)        j*                  | d|j-                                 t/        j0                  | j2                  j4                  |j6                  j8                  d|j-                          d|j-                          dd|j-                          d|j-                          d|j-                          dd	        t;        t=        d            S )zH
    This method is used to update the status of resignation letter
    rN  r   rQ   r   payrollcontract	app_labelmodelactive)rQ   contract_statusNr   notice_period_startsr   %Y-%m-%dr   r   )approvedrejectedr]  zResignation request has been u   تم u    طلب الاستقالةu   Der Rücktrittsantrag wurde z!La solicitud de renuncia ha sido u!   La demande de démission a été #r   r   rO  )r   r   ra   r   is_installedr   r=   r>   r   r2   r   strptimer   r   r   notice_period_in_daysr9   r   r   to_offboarding_employeer   r   get_status_displayr    r   r[   r]   rQ   r   r   r   )rD   r   r   rQ   r   contract_notice_end_daterG   r[  r   r   r/  r7  s               r@   update_statusrf    s    ++

l
+C[[__X&F++//-0K[[__%56N
 Y' 	 ):F	KJ	  !))---@&{{/EF$[[__-A4H#+#4#4$j$df !  !)!2!2"J"df  (%9I1GG= &" &*"##( ''..c.:G)) 	F"FMKKM#..!57I 89R9R9T8UV KK)) ,,==4V5N5N5P4QR 9 9 ;<<VW6v7P7P7R6ST;F<U<U<W;XY;F<U<U<W;XY"
	, G6788rB   z)offboarding.add_offboardinggeneralsettingc                    t         j                  j                         }|r|n	t               }d| j                  j	                         v |_        |j                          |j
                  rdnd}t        j                  | t        d      j                  |             | j                  j                  d      rt        d      S t        t              S )z3
    Enable disable resignation letter feature
    r+  enableddisabledz5Resignation Request setting has been {} successfully.HTTP_HX_REQUESTaK  
                            <span hx-trigger="load"
                            hx-get="/"
                            hx-swap="outerHTML"
                            hx-select="#offboardingGenericNav"
                            hx-target="#offboardingGenericNav">
                            </span>
                            )r4   r=   r   r   keysr+  r   r   r   r   formatrB  ra   r
   r   r   )rD   resignation_request_featuremessage_texts      r@   enable_resignation_requestro    s     #<"C"C"I"I"K ' 	$&(   	!1!1!33  3  $$&0DD	*  	
ABII,W ||)* 	
 		
 $%%rB   c                 6   | j                   d   }t        j                  d      rt        dd      }|j                  j                  |d      j                         r9|j                  j                  d      j                  |      j                         n*|j                  j                  |d      j                         }nd	}t        |       d
   dt        t        j                         j                               d}|r|j                  |d<   t        |      S )zF
    This method is used to get initial details for notice period
    rQ   rT  rU  rV  rY  )r   rZ  z-id)r   Nr   month)notice_periodunitr[  rr  )r   r   r`  r   r=   r>   r   rY   r   r_   r   r   r   rb  r   )rD   rQ   Contractemployee_contractresponses        r@   get_notice_periodrw  3  s    ++m,K#*YjQ && +X ' eg	   ))%04
 !!(( +X ) eg 	 ! .g67RS #HNN$4$9$9$; <H
 $5$K$K!!!rB   c                     | j                   j                  d      }t        j                  |d      j	                         }t        |       d   }|t        |      z   }d|i}t        |      S )zd
    Calculates and returns the end date of the notice period based on the provided start date.
    
start_dater\  r   r   r   )r   ra   r   ra  r   r   r   r   )rD   ry  rr  r   rv  s        r@   get_notice_period_end_daterz  V  si     .J"":z:??AJ(12MNMI=99HHH !!rB   )rO    offboarding.view_offboardingtaskz$offboarding.view_offboardingemployeec                    g }t        j                  d      r)t        dd      }|j                  j	                  dd      }t
        j                  j                         }t        j                  j                         }|j	                  d      }|j	                  d      j                  d	      }|j                         d
kD  r!|j                         |j                         z  nd
}t        |d      |||t        |      d}t        | d|      S )zG
    This method is used to render the offboarding dashboard page.
    recruitment	candidateFTonboarding_stage__isnullconverted_employee_id__isnullr   stage_id__typeresignationletter__isnull#offboardingemployee__stage_id__typer      )
exit_ratiorI   archived_employeesresigning_employeesonboarding_employeesz$offboarding/dashboard/dashboard.html)r   r`  r   r=   r>   r   entirer3   excludecountroundlenr   )	rD   r  	CandidaterI   offboarding_employeesr  r  r  r%  s	            r@   offboarding_dashboardr  d  s    '+M;G	(0077%*$  8  
   '')I/77>>@.55Z5P#**U*KSS,6 T 
 =FOO<MPQ<Q		!	!	#ioo&7	7WX 
 J*02 #$8 9G 'A7KKrB   r{  c                 \    t         j                  j                         }t        | dd|i      S )zV
    This method is used to render the employee task table page in the dashboard.
    z.offboarding/dashboard/employee_task_table.htmlrI   )r3   r=   r  r   )rD   rI   s     r@   dashboard_task_tabler    s5     $++224I8	
 rB   assetc                     t        dd      }t        j                  j                         j	                  dd      }|j                  j                         j                  d|      }t        | dd	|i      S )
`
        This method is used to render the employee assets table page in the dashboard.
        r  assetassignmentrV  r   Tr   )return_status__isnullassigned_to_employee_id__inz/offboarding/dashboard/asset_returned_table.htmlassets)r   r3   r=   r  r   r>   r   )rD   AssetAssignmentr  r  s       r@   dashboard_asset_tabler    s     2%6
 !4 ; ; B B D P PD !Q !
 !((//188"&(= 9 
 =v
 	
rB   pmsc                 J   t        dd      }t        j                  j                         j	                  dd      }|rt        t        t        |       \  }}ng g }}|j                  j                         j                  |      j                  d      }t        | d	d
|i      S )r  r  feedbackrV  r   r[  )employee_id__inClosedr   z2offboarding/dashboard/employee_feedback_table.html	feedbacks)r   r3   r=   r  r   maprK   zipr>   r  r   )rD   Feedbackr  rH  	date_listr  s         r@   dashboard_feedback_tabler    s     +U*M 3 ; ; B B D P P5!
 !!$T30E+F!GGY!#RYG ##%VGV,WHW% 	
 @)$
 	
rB   c                 (   t         j                  j                         }t        j                  j                         }|j	                  d      }|j	                  d      j                  d      }ddg}|j                         |j                         g}t        j                  d      rZt        dd	      }|j                  j	                  dd
      }|j                  d       |j                  |j                                ||d}	t        |	      S )zH
    This method is used to render the joining - offboarding chart.
    r   r  Fr  r  	resigningr}  r~  rV  Tr  New)labelsitems)r   r=   r  r3   r>   r  r  r   r`  r   rZ   r   )
rD   rI   r  r  r  r  r  r  r  rv  s
             r@   dashboard_join_chartr    s      '')I/77>>@.55Z5P#**U*KSS,6 T  :&F!!#  "E '+m;W	(0077%*$  8  
 	e)//12 H !!rB   c           	      @   t         j                  j                         }t        j                  j	                         }|D cg c].  }|j                  |j                        j                         r|0 }}t        j                  j                  |j                  d      j                               }|D cg c]  }|j                   }}g }|D ]  }|j                  }	|	|vrdgt        |      z  }
|j                  |	j                        }|j                  |j                        j                         }||
|<   |j!                  |j"                   d|	j                   d|
dt%        |j"                        d	z   d
d        t'        ||d      S c c}w c c}w )zL
    This method is used to render the department - job position chart.
    ).employee_id__employee_work_info__department_id0employee_id__employee_work_info__job_position_idr   r   )r  z ()zhsl(ih  z, 70%, 50%, 0.6))labelrh   backgroundColor)r  datasets)r   r=   rW   r3   r  r>   rF   r?   r   valuesdistinct
departmentdepartment_idr  r   r  rZ   job_positionhashr   )rD   departmentsr  deptselected_departmentsjob_positionsr  r  jobjob_deptrh   
dept_indexr  s                r@   department_job_postion_chartr    s    $$((*K/77>>@   '';?77 ( 

&( 	   ''..$++>

(* / M +??$doo?F?H 
$$//sS-..\\("5"56
%,,=@VV - 

%' 	 !Z,,-R0C0C/DAF%)$s/?/?*@3*F)GGW#X	

* 6x@AAM @s   3F7F)N)yr   r   r   urllib.parser   django.appsr   django.contribr   django.contrib.auth.modelsr   django.core.paginatorr	   django.httpr
   r   django.shortcutsr   r   django.urlsr   django.utils.translationr   r   base.context_processorsr   base.methodsr   r   r   r   base.modelsr   r   
base.viewsr   employee.modelsr   horillar   horilla.decoratorsr   r   r   r   horilla.group_byr   r`   horilla.methodsr   notifications.signalsr    offboarding.decoratorsr!   r"   r#   r$   offboarding.filtersr%   r&   r'   r(   r)   offboarding.formsr*   r+   r,   r-   r.   r/   r0   offboarding.modelsr1   r2   r3   r4   r5   r6   r7   r8   r9   rA   rp   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  r  r  r)  r5  r8  rJ  rL  rP  rR  rf  ro  rw  rz  r  r  r`  r  r  r  r   rB   r@   <module>r     s    ( !  # + + 2 -  6 8 N N / ' $ '  ; 3 (    
 
 
	( 	  b 9x "4# #L "4  
6 23% 4  %P 56
% 7 
% AB!J C !JH DE F > <=(M > (MV =>% ? %6 :;E < E" 782 9  2j 78'* 9  '*T &t  
6  0 <=  >    9:  ;   '(IJ" K "J %d, ,^ 56 7 D '(LM% N % Q  QB 9:,- . ; , 9: ; & ,-6 .  6r ,- .   ,-4 . 4 ,-P .  P$ ,-;<A9 = . A9H @A & B   &F :;" < "B" 
L LB #%GH  4W:;<
 = 
, 4U9:
 ; 
8 56" 7 "@ 56.B 7 .BrB   