
    vg8                        d Z ddlZddl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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mZ ddlm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z* e'd        Z+e'e& e(d      d                      Z,e'e&d               Z-e'e&d               Z.e' e(d      d               Z/e' e(d      d               Z0e' e(d      d               Z1e'd        Z2e'd        Z3d Z4d Z5d Z4d  Z5d! Z6e'e& e(d"      d#                      Z7e'e& e(d$      d%                      Z8e'e& e(d$      d&                      Z9e'e& e(d'      d(                      Z:e'd)        Z;e'd*        Z<e'e&d+               Z=e'd,        Z>y)-zI
policies.py

This module is used to write operation related to policies
    N)	timedelta)parse_qs)messages)User)HttpResponseJsonResponse)get_object_or_404redirectrender)gettext_lazy)eval_validatefiltersubordinatesget_key_instancespaginator_qry)DisciplinaryActionFilterPolicyFilter)DisciplinaryActionForm
PolicyForm)
ActiontypeDisciplinaryActionEmployeePolicyPolicyMultipleFile)hx_request_requiredlogin_requiredpermission_required)notifyc                     t         j                  j                         }| j                  j	                  d      s|j                  d      }t        | ddt        || j                  j                  d            i      S )zG
    Method is used render template to view all the policy records
    employee.view_policyTis_visible_to_allzpolicies/view_policies.htmlpoliciespage)
r   objectsalluserhas_permfilterr   r   GETgetrequestr"   s     %/var/www/horilla/employee/policies.pyview_policiesr.   %   sh    
 ~~!!#H<<  !78??T?:%	]8W[[__V-DEF     zemployee.add_policyc                    | j                   j                  d      }d}t        t        t	        |            t
              r.t        j                  j                  |      j                         }t        |      }| j                  dk(  rbt        | j                  | j                  |      }|j                         r0|j                          t!        j"                  | d       t               }t%        | dd|i      S )	z4
    Method is used to create/update new policy
    instance_idNidinstancePOSTzPolicy savedzpolicies/form.htmlform)r)   r*   
isinstancer   strintr   r$   r(   firstr   methodr6   FILESis_validsaver   successr   )r,   r1   r5   r7   s       r-   create_policyrA   4   s     ++//-0KH-K 0137>>((K(8>>@x(D~~',,I==?IIKWn5<D'/&$@@r/   c           
      0   t        | j                        j                  }| j                  j	                  d      s|j                  d      }t        | dt        || j                  j                  d            | j                  j                         d      S )z3
    This method is used to search in policies
    r   Tr    zpolicies/records.htmlr#   )r"   pd)
r   r)   qsr&   r'   r(   r   r   r*   	urlencoder+   s     r-   search_policiesrF   J   sz     GKK(++H<<  !78??T?:%h0GH++'')	
 r/   c                     | j                   d   }t        j                  j                  |      j	                         }t        | dd|i      S )z0
    This method is used to view the policy
    r1   r2   zpolicies/view_policy.htmlpolicy)r)   r   r$   r(   r;   r   )r,   r1   rH   s      r-   view_policyrI   ]   sO     ++m,K^^""k"288:F#f	
 r/   zemployee.delete_policyc                    	 | j                   j                  d      }t        j                  j	                  |      j                         \  }}|dk(  r t        j                  | t        d             nt        j                  | d       t        t              S # t        $ r0 t        j                  | t        d             Y t        t              S w xY w)z)
    This method is to delete policy
    idsid__inr   zPolicies Not FoundzPolicies deleted)r)   getlistr   r$   r(   deleter   error_r@   
ValueErrorr
   r.   )r,   rK   countdicts       r-   delete_policiesrU   n   s    9kk!!%(nn++3+7>>@tA:NN7A&:$;<W&89 M""  9w"6 78M""9s   BB (CCzemployee.add_policymultiplefilec                    | j                   j                  d      }| j                  d   }g }|D ]4  }t               }||_        |j                          |j                  |       6 t        j                  j                  |      } |j                  j                  |  t        j                  | d       t        | dd|i      S )z9
    This method is used to add attachment to policy
    files	policy_idr2   zAttachments addedpolicies/attachments.htmlrH   )r=   rN   r)   r   
attachmentr?   appendr   r$   r*   attachmentsaddr   r@   r   )r,   rW   rX   r\   filerZ   rH   s          r-   add_attachmentr_      s     MM!!'*EK(IK '')
 $
:&	'
 ^^9-FFK(W12'668JKKr/   z"employee.delete_policymultiplefilec                    | j                   j                  d      }| j                   d   }t        j                  j	                  |      }t
        j                  j                  |      j                          t        | dd|i      S )z7
    This method is used to remove the attachments
    rK   rX   r2   rL   rY   rH   )	r)   rN   r   r$   r*   r   r(   rO   r   )r,   rK   rX   rH   s       r-   remove_attachmentra      sr     ++

e
$CK(I^^9-F%%S%188:'668JKKr/   c                 ~    | j                   d   }t        j                  j                  |      }t	        | dd|i      S )zK
    This method is used to view all the attachments inside the policy
    rX   r2   rY   rH   )r)   r   r$   r*   r   )r,   rH   s     r-   get_attachmentsrc      s>    
 [[%F^^6*F'668JKKr/   c                    t         j                  j                  | j                        j	                         }| j                  j                  d      rt        j                  j                         }nht        | t        j                  j                         d      j                         }|t        j                  j                  |      j                         z  }t        | j                  |      }| j                  j                  d      }t        |j                  |      }| j                  j                         }t!        | d|||d      S )	z<
    This method is used to view all Disciplinaryaction
    )employee_user_idz employee.view_disciplinaryactionzbase.add_disciplinaryaction)employee_id)querysetr#   z*disciplinary_actions/disciplinary_nav.html)datarC   f)r   r$   r(   r&   r;   r'   r   r%   r   distinctr   r)   r*   r   rD   rE   r   )r,   employeedis_actionsr7   page_numberpage_objprevious_datas          r-   disciplinary_actionsrp      s   
 &&&EKKMH||?@(00446('//3357T

(* 	  ((//H/ENNPQ 	
 $GKK+FD++//&)KTWWk2HKK))+M4	
 r/   c                 `    t         j                  j                  | d         }|j                  S ]
    This function is used to get the action type by the selection of title in the form.
    actiontitler   r$   r*   action_type	action_idrt   s     r-   get_action_typer{      .     ##)H*=#>Fr/   c                 Z    t         j                  j                  |       }|j                  S rs   ru   rw   ry   s     r-   get_action_type_deleter      )     ##)#4Fr/   c                 `    t         j                  j                  | d         }|j                  S rr   rw   ry   s     r-   r{   r{      r|   r/   c                 Z    t         j                  j                  |       }|j                  S r~   rw   ry   s     r-   r   r      r   r/   c                     t        t        |       }|st        t              S t        t        |j
                  j                        }|st        t              S ||_        |j                          t        d      S )Nr2   )<script>window.location.reload()</script>)
r	   r   r
   rp   r   re   r3   	is_activer?   r   )emp_idresultrk   r&   s       r-   employee_account_block_unblockr      s`     f5H,--Th&?&?&B&BCD,--DNIIKCDDr/   zemployee.add_disciplinaryactionc                    t               }g }| j                  j                  d      dk7  r| j                  j                  d      nd}| j                  rt        | j                        }| j                  dk(  rt        | j                  | j
                        }|j                         r|j                  d   }|D ]  }|j                  }|j                  |       ! |j                          t        j                  | t        d             t        j                  | j                   j"                  |ddd	d
ddd	       t$        j&                  j)                         }t+        |      dk(  rt-        d      S t/        | d||d      S )z5
    Method is used to create Disciplinaryaction
    dynamicNoneNr6   rf   zDisciplinary action taken.$Disciplinary action is taken on you.2   تم اتخاذ إجراء disziplinarisch ضدك.7   Disziplinarische Maßnahmen wurden gegen Sie ergriffen.0   Se ha tomado acción disciplinaria en tu contra.>   Des mesures disciplinaires ont été prises à votre encontre./employee/disciplinary-actions/chatbox-ellipses	recipientverbverb_arverb_deverb_esverb_frr
   icon   r   zdisciplinary_actions/form.html)r7   r   )r   r)   r*   r<   r6   r=   r>   cleaned_datare   r[   r?   r   r@   rQ   r   sendr&   employee_getr   r$   r%   lenr   r   )r,   r7   	employeesr   employee_idsrk   r&   diss           r-   create_actionsr      sN    "#DI&-kkooi&@F&J	"PT  {{%gkk2~~%gllGMMB==?,,];L( '00  &' IIKWa(D&EFKK))#;LQJX:'
 !((,,.s8q= KLL1DW3U r/   z"employee.change_disciplinaryactionc                 &   t         j                  j                  |      }t        |      }g }| j                  dk(  rt        | j
                  | j                  |      }|j                         r|j                  d   }|D ]  }|j                  }|j                  |       ! |j                          t        j                  | t        d             t        j                   | j"                  j$                  |dddd	d
dd	       t'        | dd|i      S )z5
    Method is used to update Disciplinaryaction
    r2   r4   r6   rf   zDisciplinary action updated.r   r   r   r   r   r   r   r   z%disciplinary_actions/update_form.htmlr7   )r   r$   r*   r   r<   r6   r=   r>   r   re   r[   r?   r   r@   rQ   r   r   r&   r   r   )r,   rz   rt   r7   r   r   rk   names           r-   update_actionsr   '  s      ''++y+9F!62DI~~%gllGMMFS==?,,];L( '00  &' IIKWa(F&GHKK))#;LQJX:'
 'BVTNSSr/   c                    t         j                  j                  |      }t        j                  j                  |      }t	        |j
                        }|dk(  s|dk(  ryt        t        |      }t        t        |j                  j                        }|j                  rn6t        j                  | t        d             d|_
        |j                          |j                  j!                  |       t#        |j                  j%                               }|dk(  r|j'                          t        j(                  | t        d             t+        d|j                         S )	Nr2   	dismissal
suspension.Employees login credentials will be unblocked.Tr   z7Employee removed from disciplinary action successfully.z,/employee/disciplinary-filter-view?click_id=)r   r$   r*   r   r   rt   r	   r   re   r3   r   r   warningrQ   r?   rf   remover   r%   rO   r@   r
   )	r,   rz   r   
dis_actionrk   rx   empr&   r   s	            r-   #remove_employee_disciplinary_actionr   M  s     $++//9/=J##v#.H():):;Kk![L%@V4 #*>*>*A*AB>>KL "DNIIK!!(+J**..01IA~LM B:==/RSSr/   z"employee.delete_disciplinaryactionc                    t         j                  j                  |      }t        |j                        }|j
                  j                         D ]  }|dk(  s|dk(  st        t        |j                        }t        t        |j                  j                        }|j                  r[t        j                  | t        d             d|_        |j!                           |j#                          t        j$                  | t        d             t         j                  j                         }|j'                         rt)        t*              S t-        d      S )z;
    This method is used to delete Disciplinary action
    r2   r   r   r   TzDisciplinary action deleted.r   )r   r$   r*   r   rt   rf   r%   r	   r   r3   r   re   r   r   r   rQ   r?   rO   r@   existsr
   disciplinary_filter_viewr   )r,   rz   r   rx   dis_emprk   r&   rl   s           r-   delete_actionsr   o  s    
$
$
(
(I
(
6C(4K??&&( +%)D(gjjAH$Th.G.G.J.JKD~~  QOP "&		 JJLWa >?@$,,002K011CDDr/   c                     | j                   d   }t        j                  j                  |      }|j                  }t        d|i      S )z[
    This method is used to get the action type by the selection of title in the form.
    rx   r2   )r6   r   r$   r*   rx   r   )r,   rz   rt   rx   s       r-   action_type_detailsr     sF    
 ]+I##y#1F$$K455r/   c                 :    | j                   d   }t        d|i      S )z_
    This method is used to get the action type name by the selection of type in the form.
    rx   )r6   r   )r,   rx   s     r-   action_type_namer     s"    
 ,,}-K455r/   c           
         | j                   j                         }| j                   j                  d      r| j                   j                  d      nd}t        | j                         j                  }| j                   j                  d      }t        ||      }t        |      }t        t        |       t        | d|||| j                   j                  d      |d      S )z<
    This method is used to filter Disciplinary Action.
    click_idNr#   .disciplinary_actions/disciplinary_records.html	dashboard)rh   rC   filter_dictr   rz   )
r)   rE   r*   r   rD   r   r   r   r   r   )r,   ro   rz   
dis_filterrm   rn   	data_dicts          r-   r   r     s     KK))+M/6{{z/J
+PTI)'++699J++//&)KZ5H'I()48$ 5"	

 
r/   c           
          t        | j                        j                  }t        | dt	        || j                  j                  d            | j                  j                         d      S )z?
    This method is used to search in Disciplinary Actions
    r   r#   )rh   rC   )r   r)   rD   r   r   r*   rE   )r,   disciplinarys     r-   search_disciplinaryr     sW    
 ,GKK8;;L8!,0GH++'')	
 r/   )?__doc__datetimejsonr   urllib.parser   django.contribr   django.contrib.auth.modelsr   django.httpr   r   django.shortcutsr	   r
   r   django.utils.translationr   rQ   base.methodsr   r   r   r   employee.filtersr   r   employee.formsr   r   employee.modelsr   r   r   r   r   horilla.decoratorsr   r   r   notifications.signalsr   r.   rA   rF   rI   rU   r_   ra   rc   rp   r{   r   r   r   r   r   r   r   r   r   r    r/   r-   <module>r      s      ! # + 2 @ @ 6  D =  X W (   *+A ,  A&   "    -.# / #  67L 8 L$ 9:L ; L L L  @
E 67( 8  (V 9: T ;   TF 9:T ;  T> 9:E ;  E@ 6 6 6 6   2  r/   