
    si^F                     v   d 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 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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$ ddl%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-  ee#d       G d de+             Z. ee#d       G d de(             Z/ ee#d       G d de/             Z0 ee#d       G d de/             Z1 ee#d       G d de(             Z2 ee#d       G d de*             Z3 ee#d       G d  d!e)             Z4 ee#d       G d" d#e&             Z5 ee#d       G d$ d%e&             Z6 ee#d       G d& d'e&             Z7 ee#d       G d( d)e'             Z8 ee#d       e e$d*+      d       G d, d-e'                    Z9 G d. d/e'      Z:y0)1z
Request and allocation page
    )Any)messages)HttpResponse)render)reverse)method_decorator)gettext_lazy)AssetAllocationFilterAssetRequestFilterCustomAssetFilter)AssetAllocationFormAssetRequestForm)AssetAssetAssignmentAssetRequestReturnImages)filtersubordinates)Employee)_thread_locals)login_requiredpermission_required)HorillaDetailedViewHorillaFormViewHorillaListViewHorillaNavViewHorillaTabViewTemplateView)notifydispatch)namec                       e Zd ZdZdZy)RequestAndAllocationViewz)
    for request and allocation page
    z6cbv/request_and_allocation/request_and_allocation.htmlN)__name__
__module____qualname____doc__template_name     D/var/www/python-projects/worksol/asset/cbv/request_and_allocation.pyr"   r"       s     MMr)   r"   c                        e Zd ZdZdgZeZeZde	ddf fdZ
 ed      dd	f ed
      df ed      dfgZdddddZddgZdZdZ xZS )AllocationListz2
    For both  asset allocation and asset tab
    asset_id__expiry_datekwargsreturnNc                 D    t        |   di | t        d      | _        y )N
list-assetr(   super__init__r   
search_urlselfr.   	__class__s     r*   r4   zAllocationList.__init__6   s    "6"!,/r)   r   asset_id
get_avatarCategoryasset_id__asset_category_idExpiry DateH
                   style = "width:180px !important"
                   zH
                   style = "width:250px !important"
                   )actionasset_id__asset_namer<   r-   )r;   0asset_id__asset_category_id__asset_category_name)r=   r-   asset_actionz
                hx-get='{detail_view_asset}?instance_ids={ordered_ids}'
                hx-target="#genericModalBody"
                data-target="#genericModal"
                data-toggle="oh-modal-toggle"
                )r#   r$   r%   r&   bulk_update_fieldsr   modelr
   filter_classr   r4   _columnsheader_attrssortby_mappingaction_method	row_attrs__classcell__r8   s   @r*   r,   r,   )   s     22E(L0 0 0
 
7Z.	
:56	
=	23G!("L  	I0N
 #MIr)   r,   c                   &     e Zd ZdZ fdZdZ xZS )	AssetListz
    Asset tab
    c                     t         |          }| j                  j                  j                  }|j                  |      j                  d      }|S )zp
        Returns a queryset of AssetRequest objects filtered by
        the current user's employee ID.
        )assigned_to_employee_idF)return_status__isnull)r3   get_querysetrequestuseremployee_getfilterexclude)r7   querysetemployeer8   s      r*   rS   zAssetList.get_querysete   sR    
 7')<<$$11??8?DLL"' M 
 r)   assetlistInstances)r#   r$   r%   r&   rS   selected_instances_key_idrL   rM   s   @r*   rO   rO   ^   s    

 !5r)   rO   c                        e Zd ZdZdeddf fdZ ed      ddf ed	      d
f ed      df ed      dfgZg dZdZ	dZ
 xZS )AssetAllocationListz
    Asset allocation tab
    r.   r/   Nc                 D    t        |   di | t        d      | _        y )Nlist-asset-allocationr(   r2   r6   s     r*   r4   zAssetAllocationList.__init__z   s     "6"!"9:r)   Allocated UserrQ   #assigned_to_employee_id__get_avatarr   r9   Assigned Dateassigned_dateReturn Datereturn_status_col))ra   &assigned_to_employee_id__get_full_name)r   r@   )rc   rd   )re   rf   allocation_actionz
                hx-get='{detail_view_asset_allocation}?instance_ids={ordered_ids}'
                hx-target="#genericModalBody"
                data-target="#genericModal"
                data-toggle="oh-modal-toggle"
                )r#   r$   r%   r&   r   r4   rF   rG   rI   rJ   rK   rL   rM   s   @r*   r^   r^   t   sv    ; ; ; %1	

 
7Z 	
?	_-	
=	./	GN (MIr)   r^   c                        e Zd ZdZdeddf fdZeZeZ	 fdZ
 ed      dd	f ed
      df ed      df ed      dfgZddiZg dZdZ xZS )AssetRequestListz
    Asset Request Tab
    r.   r/   Nc                     t        |   di | t        d      | _        | j                  j
                  j                  d      rd| _        y y )Nlist-asset-requestzasset.add_assetassignment
action_colr(   )r3   r4   r   r5   rT   rU   has_permrJ   r6   s     r*   r4   zAssetRequestList.__init__   sG    "6"!"67<<%%&AB!-D Cr)   c                     t         |          }t        | j                  d|d      |j	                  | j                  j
                  j                        z  }|S )zx
        Returns a filtered queryset of AssetRequest objects
        based on user permissions and employee ID.
        zasset.view_assetrequestrequested_employee_id)rT   permrY   field)rp   )r3   rS   r   rT   rW   rU   rV   )r7   rY   r8   s     r*   rS   zAssetRequestList.get_queryset   sY     7')%LL*)	

 OO$,,2C2C2P2POQR r)   Request Userrp   !requested_employee_id__get_avatarAsset Categoryasset_category_idRequested Dateasset_request_dateStatus
status_colr?   r>   ))rs   $requested_employee_id__get_full_name)ru   &asset_category_id__asset_category_name)rw   rx   )ry   rz   z
                hx-get='{detail_view_asset_request}?instance_ids={ordered_ids}'
                hx-target="#genericModalBody"
                data-target="#genericModal"
                data-toggle="oh-modal-toggle"
                )r#   r$   r%   r&   r   r4   r   rD   r   rE   rS   rF   rG   rH   rI   rK   rL   rM   s   @r*   rj   rj      s    . . . E%L" n#/	

 
	12	
	23	
8l#	G 	 LNIr)   rj   c                   ,     e Zd ZdZdeddf fdZ xZS )RequestAndAllocationTabz
    Tab View
    r.   r/   Nc           	         t        |   di | t        d      t        d       dt        d      t        d       ddt        d       d	d
gdg| _        | j
                  j                  j                  d      rD| j                  j                  t        d      t        d       ddt        d       d	d
gd       y y )Nr   r1   )titleurlAsset Requestrl   zCreate Requestz
                                data-toggle="oh-modal-toggle"
                                data-target="#genericModal"
                                hx-get="zasset-request-creationz"
                                hx-target="#genericModalBody"
                                style="cursor: pointer;"
                                )r?   attrs)r   r   actionszasset.view_assetassignmentAsset Allocationr`   zCreate Allocationzasset-allocate-creationr(   )	r3   r4   rF   r   tabsrT   rU   rn   appendr6   s     r*   r4   z RequestAndAllocationTab.__init__   s    "6" 7!,/0
 ?+!"678 #3&) *11I)J(K L!"$	
	, <<%%&BCII12%&=>? ':*) *11J)K(L M!&$	  Dr)   )r#   r$   r%   r&   r   r4   rL   rM   s   @r*   r~   r~      s     * * * *r)   r~   c            
            e Zd ZdZdeddf fdZ ed      Z e       Z	dZ
dZd	Z fd
Zd ed      fd ed      fd ed      fd ed      fd ed      fd ed      fd ed      fgZ xZS )RequestAndAllocationNavz
    Nav bar
    r.   r/   Nc                 D    t        |   di | t        d      | _        y )Nztab-asset-request-allocationr(   r2   r6   s     r*   r4   z RequestAndAllocationNav.__init__  s     "6"!"@Ar)   r   asset_allocation_filter_formz&cbv/request_and_allocation/filter.htmlz#listContainerc                     t        |   di |}t               }t               }|j                  |d<   |j                  |d<   |S )z
        context data
        assets_filter_formasset_request_filter_formr(   )r3   get_context_datar   r   form)r7   r.   contextr   r   r8   s        r*   r   z(RequestAndAllocationNav.get_context_data  sP     '*4V4.0$6$8!(:(?(?$%/H/M/M+,r)   rp   zAsset Request / Employeerv   zAsset Request / Asset Categoryrx   zAsset Request / Request Dateasset_request_statuszAsset Request / StatusrQ   zAsset Allocation / Employeerd   z Asset Allocation / Assigned Datereturn_datezAsset Allocation / Return Date)r#   r$   r%   r&   r   r4   rF   	nav_titler
   filter_instancefilter_form_context_namefilter_body_templatesearch_swap_targetr   group_by_fieldsrL   rM   s   @r*   r   r     s    B B B '
I+-O=C)	 
!!$>"?@	a @AB	q!?@A	#;!<=	"A&C$DE	!>?@	:;<Or)   r   c                   P     e Zd ZdZdeddf fdZdZeZ e	d      Z
dd	d
dZ xZS )AssetDetailViewz"
    detail view of asset tab
    r.   r/   Nc                     t        |   di | t        d      dft        d      dft        d      dft        d      dft        d	      d
ft        d      dfg| _        y )NDescriptionasset_id__asset_descriptionzTracking Idasset_id__asset_tracking_idrc   rd   ry   asset_detail_statuszAssigned byassigned_by_employee_idzBatch Noasset_id__asset_lot_number_idr(   r3   r4   rF   bodyr6   s     r*   r4   zAssetDetailView.__init__9  sm    "6"}<=}<=1x[/0}89z];<
	r)   asset_detail_actionzAsset Informationr@   r<   r:   r   subtitleavatar)r#   r$   r%   r&   r   r4   rJ   r   rD   rF   r   headerrL   rM   s   @r*   r   r   3  sB    

 

 

 *ME!"E'1Fr)   r   c                   P     e Zd ZdZdeddf fdZeZ ed      Z	ddd	d
Z
dZ xZS )AssetRequestDetailViewz*
    detail view of asset request tab
    r.   r/   Nc                     t        |   d	i | t        d      dft        d      dft        d      dft        d      dfg| _        y )
Nru   rv   rw   rx   zRequest Descriptiondescriptionry   rz   r(   r   r6   s     r*   r4   zAssetRequestDetailView.__init__V  sT    "6" "56 "67$%}5x[,'	
	r)   Detailsrp   asset_request_detail_subtitlert   r   detail_action_col)r#   r$   r%   r&   r   r4   r   rD   rF   r   r   rJ   rL   rM   s   @r*   r   r   P  sB    
 
 
 EiLE(35F
 (Mr)   r   c                   P     e Zd ZdZdeddf fdZeZ ed      Z	ddd	d
Z
dZ xZS )AssetAllocationDetailViewz-
    detail view of asset allocation tab
    r.   r/   Nc           	          t        |   di | t        d      dft        d      dft        d      dft        d      dft        d	      d
ft        d      dft        d      dfg| _        y )NzReturned Statusreturn_statusra   r   zAllocated Daterd   re   r   r   r9   zReturn Descriptionreturn_conditionry   detail_statusr(   r   r6   s     r*   r4   z"AssetAllocationDetailView.__init__o  s{    "6" !?3 ";< /2}}-wZ$#$&89x[/*
	r)   r   rQ    asset_allocation_detail_subtitlerb   r   asset_allocation_detail_action)r#   r$   r%   r&   r   r4   r   rD   rF   r   r   rJ   rL   rM   s   @r*   r   r   i  sB    

 

 

 EiLE*67F
 5Mr)   r   c                   R     e Zd ZdZeZeZdZ e	d      Z
 fdZdedef fdZ xZS )AssetRequestCreateFormz
    Create Asset request
    z.cbv/request_and_allocation/forms/req_form.htmlr   c                 n   t        |   di |}| j                  j                  j	                  d      r| j                  j                  j	                  d      }t
        j                  j                  |      | j                  j                  d   _
        || j                  j                  d   _        |S )Npkidrp   r(   )r3   r   rT   GETgetr   objectsrW   r   fieldsrY   initial)r7   r.   r   r   r8   s       r*   r   z'AssetRequestCreateForm.get_context_data  s    '*4V4<<%!!%%d+B  ''2'. II45> ACDII45=r)   r   r/   c                     |j                         rKt        d      }|j                          t        j                  | j
                  |       | j                         S t        | !  |      S )zG
        Handles validation and saving of an AssetRequestForm.
        z"Asset Request Created Successfully)	is_validrF   saver   successrT   r   r3   
form_valid)r7   r   messager8   s      r*   r   z!AssetRequestCreateForm.form_valid  sU     ==?<=GIIKT\\73$$&&w!$''r)   )r#   r$   r%   r&   r   rD   r   
form_classr'   rF   new_display_titler   r   r   rL   rM   s   @r*   r   r     sB     E!JDM/*	(/ 	(L 	( 	(r)   r   zasset.add_asset)rq   c                   H     e Zd ZdZeZeZdZ e	d      Z
dedef fdZ xZS )AssetAllocationFormView!
    Create Asset Allocation
    z/cbv/request_and_allocation/forms/allo_form.htmlr   r   r/   c                 `   |j                         r|j                  j                  }d|_        |j	                          t        d      }|j	                          t        t        dd      }|j                  j                  d      }g }|j                  r\|D ]4  }t               }||_        |j	                          |j                  |       6  |j                  j                  j                  |  t        j                   | j"                  |       | j%                         S t&        	| Q  |      S )%
        form valid function
        In usezAsset allocated SuccessfullyrT   Nassign_images)r   instancer9   asset_statusr   rF   getattrr   FILESgetlistr   imager   r   addr   r   rT   r   r3   r   )
r7   r   assetr   rT   filesattachmentsfile
attachmentr8   s
            r*   r   z"AssetAllocationFormView.form_valid  s     ==?MM**E!)EJJL67GIIKni>GMM))/:EK}}! 3D!-J'+J$OO%&&z2	3
 0++//=T\\73$$&&w!$''r)   )r#   r$   r%   r&   r   rD   r   r   r'   rF   r   r   r   rL   rM   s   @r*   r   r     s>     E$JEM,-(2 (| ( (r)   r   c                   f     e Zd ZdZeZeZdZ e	d      Z
 fdZdedef fdZdedef fdZ xZS )	AssetApproveFormViewr   z8cbv/request_and_allocation/forms/asset_approve_form.htmlr   c                    t        |   di |}| j                  j                  d      }t        j
                  j                  |      j                         }|j                  }|j                  j                  d      }|| j                  j                  d   _        |j                  | j                  j                  d   _        | j                  j                   j"                  | j                  j                  d   _        |S )	Nreq_idr   	Available)r   r9   rQ   r   r(   )r3   r   r.   r   r   r   rW   firstrv   	asset_setr   r   rY   rp   r   rT   rU   rV   )r7   r.   r   r   asset_requestasset_categoryassetsr8   s          r*   r   z%AssetApproveFormView.get_context_data  s    '*4V4*$,,33v3>DDF&88))00k0J06		$-// 			23; LL** 			23; r)   r   r/   c                     |j                         s>|j                  j                         }t        | j                  | j
                  ||d      S t        |   |      S )N)r   errors)r   r   as_datar   rT   r'   r3   form_invalid)r7   r   r   r8   s      r*   r   z!AssetApproveFormView.form_invalid  sS    }}[[((*Fd00462R  w#D))r)   c                 n   | j                   j                  d      }t        j                  j	                  |      j                         }|j                         r|j                  j                  j                  }t        j                  j	                  |      j                         }d|_        |j                          |j                          d|_        |j                          t        t        dd      }|j                   j#                  d      }g }|j                   r\|D ]4  }t%               }	||	_        |	j                          |j)                  |	       6  |j                  j*                  j,                  |  t/        j0                  | j2                  t5        d             t7        j8                  | j2                  j:                  j<                  |j>                  j@                  d	d
dddtC        d      d|jD                   d|j                   z   d	       tG        d      S tH        
|   |      S )r   r   r   r   ApprovedrT   Nr   z%Asset request approved successfully!.zYour asset request approved!.uA   تم الموافقة على طلب الأصول الخاص بك!z"Ihr Antragsantrag wurde genehmigt!u*   ¡Su solicitud de activo ha sido aprobada!u*   Votre demande d'actif a été approuvée !zasset-request-allocation-viewz?asset_request_date=z&                &asset_request_status=z	bag-check)	recipientverbverb_arverb_deverb_esverb_frredirecticonz)<script>window.location.reload()</script>)&r.   r   r   r   rW   r   r   r   r9   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rT   rF   r   sendrU   rV   rp   employee_user_idr   rx   r   r3   r   )r7   r   r   r   r   rT   r   r   r   r   r8   s             r*   r   zAssetApproveFormView.form_valid  s    *$,,33v3>DDF==?MM**--EMM((E(288:E!)EJJL IIK1;M. ni>GMM))/:EK}}! 3D!-J'+J$OO%&&z2	3
 0++//=T\\1-T+UVKK!!..'==NN4[<DD !@A()I)I(J K''4'I'I&JLL !   KLLw!$''r)   )r#   r$   r%   r&   r   rD   r   r   r'   rF   r   r   r   r   r   r   rL   rM   s   @r*   r   r     sV     E$JNM,-* * *)(2 )(| )( )(r)   r   N);r&   typingr   django.contribr   django.httpr   django.shortcutsr   django.urlsr   django.utils.decoratorsr   django.utils.translationr	   rF   asset.filtersr
   r   r   asset.formsr   r   asset.modelsr   r   r   r   base.methodsr   employee.modelsr   horilla.horilla_middlewaresr   horilla_views.cbv_methodsr   r   horilla_views.generic.cbv.viewsr   r   r   r   r   r   notifications.signalsr   r"   r,   rO   r^   rj   r~   r   r   r   r   r   r   r   r(   r)   r*   <module>r	     s    # $ #  4 6 V V = K K + $ 6 I  ) .z2M| M 3M .z21_ 1 31h .z25 5 35* .z2". " 3"J .z2< < 3<~ .z2/n / 3/d .z2"n " 3"J .z2)  38 .z2(0 ( 3(0 .z25 3 5 356 .z2(_ ( 3(@ .z2%+<=JO (o  ( P 3 (FJ(? J(r)   