
    vg1                       d Z ddlZddlZddlZddlmZmZ ddlm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 dd
lmZmZ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"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZE ddlFmGZGmHZH ddlImJZJ ddlKmLZLmMZMmNZNmOZOmPZP ddlQmRZR ddlSmTZT ddlUmVZV ddlWmXZX d ZYeMeL ePd      d                      ZZeMd_d       Z[eMeL ePd      d                      Z\eMeLd                Z]eM ePd!"      d#               Z^eMeLd$               Z_eMeL ePd%"      d&                      Z`eMeL ePd'"      d(                      ZaeM ePd)"      d*               Zbd+ ZceM ePd,"      d-               ZdeM ePd,"      d.               Zed/ ZfeMeLd0               ZgeMeL ePd1"      d2                      Zhd3 ZieM ePd1"      d4               ZjeM ePd1"      d5               ZkeMd6        ZleM ePd7"      d8               Zmd9 ZneMd:        ZoeMd;        ZpeMeLd<               ZqeMeLd=               ZreMeLd>               Zsd? Zt ed@A      ZudB ZvdC ZweM ePd"      dD               ZxeMdE        ZyeM ePd,      dF               ZzeM ePdG"      dH               Z{eM ePdI"      dJ               Z|eM ePdK"      dL               Z}eM ePdM"      dN               Z~eMeLdO               ZeMdP        ZeM ePd,"      dQ               ZeM ePdR"      dS               ZeM ePdT"      dU               ZeM ePd,"      dV               ZeM ePd,"      dW               ZeM ePdT"      dX               ZeM ePdT"      dY               ZeM ePdT"      dZ               ZeM eOd[eG      d\               ZeMeLd]               ZeMeLd^               Zy)`z#"
asset.py

This module is used to     N)datedatetime)parse_qs)messages)ContentFile)FileSystemStorage)	Paginator)ProtectedError)HttpResponseHttpResponseRedirectJsonResponse)get_object_or_404redirectrender)reverse)gettext_lazy)
AssetAllocationFilterAssetAllocationReGroupAssetCategoryFilterAssetExportFilterAssetFilterAssetHistoryFilterAssetHistoryReGroupAssetRequestFilterAssetRequestReGroupCustomAssetFilter)AssetAllocationFormAssetBatchFormAssetCategoryForm	AssetFormAssetReportFormAssetRequestFormAssetReturnForm)AssetAssetAssignmentAssetCategoryAssetDocumentsAssetLotAssetRequestReturnImages)closest_numberseval_validatefiltersubordinatesget_key_instancesget_paginationpaginator_qrysortby)Company)EmployeeEmployeeWorkInformation)settings)hx_request_requiredlogin_requiredmanager_can_enterowner_can_enterpermission_required)group_by_queryset)HORILLA_DATE_FORMATS)horilla_users_with_perms)notifyc                     	 |j                          t        j                  | t        d             y# t        $ r" t        j
                  | t        d             Y yw xY w)zJ
    Handle the deletion of an asset and provide message to the user.
    zAsset deleted successfullyzYou cannot delete this asset.N)deleter   success_r
   error)requestassets     /var/www/horilla/asset/views.py	asset_delrG   N   sL    D!$@"AB Dw"A BCDs   /2 (AAzasset.add_assetc                    d|i}| j                   j                  d      rt        | j                   |      nt        |      }| j                  dk(  rct        | j                  |      }|j                         r<|j                          t        j                  | t        d             t        d|      S d|i}t        | d	|      S )
a{  
    View function for creating a new asset object.
    Args:
        request (HttpRequest): A Django HttpRequest object that contains information
        about the current request.
        asset_category_id (int): An integer representing the ID of the asset category for which
        the asset is being created.

    Returns:
        If the request method is 'POST' and the form is valid, the function saves the
        new asset object to the database
        and redirects to the asset creation page with a success message.
        If the form is not valid, the function returns the asset creation page with the
        form containing the invalid data.
        If the request method is not 'POST', the function renders the asset creation
        page with the form initialized with
        the ID of the asset category for which the asset is being created.
    Raises:
        None
    asset_category_idcsrfmiddlewaretokeninitialPOSTzAsset created successfullyasset-creation)rI   asset_creation_formzasset/asset_creation.html)GETgetr    methodrM   is_validsaver   rA   rB   r   r   )rD   rI   initial_dataformcontexts        rF   asset_creationrX   Y   s    0 ():;L ;;??01 	'++|4|, 	
 ~~|<==?IIKWa(D&EF,@QRR$d+G'6@@    c                    t               }|rt        j                  j                  |      }t        d|i      }| j                  j                  d      sm| j
                  j                  t        j                  j                  |d      j                  k(  s| j
                  j                  d      rnt        t              S | j                  dk(  rt        | j                  | j                  d|i      }|j                         r|j!                         }t#        j$                  | t'        d	             |j                         rr| j                  rf| j                  j)                  d
      D ]#  }t*        j                  j-                  ||       % t/        | j0                  j                  dd            S t3        | d||d      S )z7
    Function for adding asset report to the asset
    idasset_idrK   
asset_listT)r]   return_date__isnullasset.change_assetrM   zReport added successfully.file)asset_reportra   HTTP_REFERER/zasset/asset_report_form.html)asset_report_formr]   )r!   r$   objectsrQ   rP   useremployee_getr%   assigned_to_employee_idhas_permr   asset_request_allocation_viewrR   rM   FILESrS   rT   r   rA   rB   getlistr'   creater   METAr   )rD   r]   re   rE   rb   ra   s         rF   add_asset_reportrp      s   
 ()!!X!.+Z4GH{{|,||((O,C,C,G,G!t -H -%%&)0)>)>?S)T =>>~~+LL'--*h1G
 %%',113LWa(D&EF ))+#MM11&9 XD"**11|RV1WX ,GLL,<,<^S,QRR &/XF rY   r`   c                 N   | j                   dk(  r| j                  j                  d      }n*| j                   dk(  r| j                  j                  d      }sd}t        j
                  j                  |      }t        |      }| j                  j                         }| j                   dk(  rVt        | j                  |      }|j                         r/|j                          t        j                  | t        d             |||||j                  j                  d}| j                  j                  d	      }|r[t        j                   |      }| j                  j#                         }	|	j%                  d	d
       |	j                         }||d	<   ||d<   t'        | d|      S )an  
    Updates an asset with the given ID.
    If the request method is GET, it displays the form to update the asset. If the
    request method is POST and the form is valid, it updates the asset and
    redirects to the asset list view for the asset's category.
    Args:
    - request: the HTTP request object
    - id (int): the ID of the asset to be updated
    Returns:
    - If the request method is GET, the rendered 'asset_update.html' template
      with the form to update the asset.
    - If the request method is POST and the form is valid, a redirect to the asset
      list view for the asset's category.
    rP   asset_underrM   asset_categoryr[   instancezAsset Updated)ru   
asset_formrr   pgasset_cat_idrequests_idsNpdzasset/asset_update.htmlrW   )rR   rP   rQ   rM   r$   rf   r    	urlencoderS   rT   r   rA   rB   rI   r\   jsonloadscopypopr   )
rD   r]   rr   ru   rv   previous_datarW   requests_ids_jsonry   request_copys
             rF   asset_updater      s`   & ~~kkoom4	6	!ll&&}5&}}  H -HH-JKK))+M~~w||h?
 OOWa&89 " 2255G  7zz"34{{'').$..0".%'4gFFrY   c                    t         j                  j                  |      }d|i}| j                  j                  d      }|r3t	        j
                  |      }t        ||      \  }}||d<   ||d<   ||d<   t        | d|      S )a  
    Display information about a specific Asset object.
    Args:
        request: the HTTP request object
        asset_id (int): the ID of the Asset object to retrieve
    Returns:
        A rendered HTML template displaying the information about the requested Asset object.
    r[   rE   ry   previousnextzasset/asset_information.html)r$   rf   rQ   rP   r}   r~   r+   r   )rD   r]   rE   rW   r   ry   previous_idnext_ids           rF   asset_informationr      s     MM*EG7zz"34.|XFW"3)
!'97CCrY   zasset.delete_asset)permc           	         | j                   j                         }|j                  dd       |j                         }	 t        j
                  j                  |      }|j                  j                  }|j                  }| j                   j                  d      }t         j
                  j#                  |      j%                         }|r0d	}	t        j
                  j'                         }
| j                   j                         }t)        | j                   |

      }|j*                  }t-        |t/                     }| j                   j                  d      }|j1                  |      }|||j                  j                  |	d}|dk(  r t        j2                  | t        d             n.|r t        j                  | t        d             nt5        | |       t        | j                  j                  dd            S | j                   j                  dd      }t7        |      }|dk(  r>t        j2                  | t        d             t9        d|j                   d| d| d      S |r>t        j                  | t        d             t9        d|j                   d| d| d      S t5        | |       t;        t7        |            dk  rt=        d      S t	        j>                  |j                        rt9        d|j                   d| d| d      S | j                   j                  d      }tA        jB                  |      }||v r|jE                  |       tG        tA        jB                  |      |      \  }}t9        d| d| d| d      S # t        j                  $ rG t        j                  | t        d             t        | j                  j                  dd            cY S w xY w)aq  Delete the asset with the given id.
    If the asset is currently in use, display an info message and
    redirect to the asset list.
    Otherwise, delete the asset and display a success message.
    Args:
        request: HttpRequest object representing the current request.
        asset_id: int representing the id of the asset to be deleted.
    Returns:
        If the asset is currently in use or the asset list filter is
        applied, render the asset list template
        with the corresponding context.
        Otherwise, redirect to the asset list view for the asset
        category of the deleted asset.
    ry   Nr[   zAsset not foundrc   rd   r^   )r]   asset_filterquerysetpage)assetsrw   rI   rr   In usezAsset is in usezAsset is used in allocation!.[]z/asset/asset-information/z/?z&requests_ids=z&asset_info=true   *<script>window.location.reload();</script>)$rP   r   r   r|   r$   rf   rQ   DoesNotExistr   rC   rB   r   ro   rI   r\   asset_statusr%   filterfirstallr   qsr	   r/   get_pageinforG   r,   r   lenr   findr}   r~   remover+   )rD   r]   r   r   rE   rx   statusasset_list_filterasset_allocationrr   r   asset_filteredr^   	paginatorpage_numberpage_objrW   instances_idsinstances_listprevious_instancenext_instances                        rF   asset_deleter      sr   $ ;;##%L^T* **,MK!!X!. **--LF5&..55u5EKKM$""$--/$W[[6B#&&
j.*:;	kkoof-%%k2!&!8!8!;!;&	
 XMM'1%6#78NN7A&E$FGgu%#GLL$4$4^S$IJJKKOOND9M"=1Ngq!234'zM?.Q_P``pq
 	
 
w"A BC'zM?.Q_P``pq
 	
 	'5!}]+,1 LMM::ehh+EHH:RnUcTddtu  $KKOON;M!ZZ6N>)%%h/'6

=!8($} 
#M?"]O>R`Qaaqr y  Kw"3 45#GLL$4$4^S$IJJKs    N AO('O(c                    i }d}t        | j                        }|j                  }t        |t	                     }| j                  j                  d      }|j                  |      }t        j                  |j                  D 	cg c]  }	|	j                   c}	      }
| j                  j                         }t        |      }t        t        |       ||||t        |      xs d||
d}t!        | d|      S c c}	w )a  
    View function is used as asset list inside a category and also in
    filter asset list
    Args:
        request (HttpRequest): A Django HttpRequest object that contains
        information about the  current request.
        cat_id (int): An integer representing the id of the asset category
        to list assets for.
    Returns:
        A rendered HTML template that displays a paginated list of assets in the given
        asset  category.
    Raises:
        None
     r   N)r   rw   rI   rr   asset_countfilter_dictry   zasset/asset_list.html)r   rP   r   r	   r/   rQ   r   r}   dumpsobject_listr\   r|   r   r.   r$   r   r   )rD   cat_idrW   rr   r   r^   r   r   r   ru   ry   r   	data_dicts                rF   r^   r^   R  s    " GK -N""J*n&67I++//&)K!!+.H::8;O;OPxx{{PQLKK))+M'IeY'#":.$ $G '2G<< Qs   C6zasset.add_assetcategoryc                    t               }| j                  dk(  rt        | j                        }|j                         rs|j	                          t        j                  | t        d             t               }t        j                  j                         j                         dk(  rt        d      S d|i}t        | d|      S )z
    Allow a user to create a new AssetCategory object using a form.
    Args:
        request: the HTTP request object
    Returns:
        A rendered HTML template displaying the AssetCategory creation form.
    rM   z#Asset category created successfullyr   r   asset_category_formz%category/asset_category_creation.html)r   rR   rM   rS   rT   r   rA   rB   r&   rf   r   countr   r   )rD   r   rW   s      rF   asset_category_creationr   |  s     ,-~~/='')$$&Wa(M&NO"3"5$$++-335:#$PQQ$&9:G'BGLLrY   zasset.change_assetcategoryc                    | j                   j                         }t        j                  j	                  |      }t        |      }||d}| j                  dk(  r\t        | j                  |      }|j                         r0|j                          t        j                  | t        d             n||d<   t        | d|      S )z
    This view is used to update an existing asset category.
    Args:
        request: HttpRequest object.
        id: int value representing the id of the asset category to update.
    Returns:
        Rendered HTML template.
    r[   rt   )asset_category_update_formrw   rM   z#Asset category updated successfullyr   z#category/asset_category_update.html)rP   r|   r&   rf   rQ   r   rR   rM   rS   rT   r   rA   rB   r   )rD   r   r   rs   r   rW   s         rF   asset_category_updater     s     KK))+M"**..&.9N+^D-@VG~~/~V'')$$&Wa(M&NO-@G)*'@'JJrY   zasset.delete_assetcategoryc                    | j                   j                         }	 t        j                  j	                  |      j                          t        j                  | t        d             t        j                  j                         st        d      S t        d|       S #  t        j                  | t        d             Y ZxY w)z6
    This method is used to delete asset category
    r[   zAsset category deleted.z(Assets are located within this category.r   z)/asset/asset-category-view-search-filter?)rP   r|   r&   rf   rQ   r@   r   rA   rB   rC   r   r   r   )rD   r   r   s      rF   delete_asset_categoryr     s     KK))+MO!!V!,335!$=">?   '')HII?OPP	Ow"L MNs   AB   !Cc           
         | j                   j                  dd      }| j                   j                         }t        j                  j                         }| j                   r0t        | j                   |      }|j                  }|j                  }nd}t        |t                     }| j                   j                  d      }|j                  |      }t        |      }	t        t        |	       t               }
|	j                  d      r|	d= t               }t!               }|
||||j                  ||	| j                   j                  d      dS )	zI
    This view is used for pagination and filtering asset categories
    searchr   r   Nr   type	dashboard)rO   r   asset_categoriesasset_category_filter_formasset_filter_formrw   r   r   )rP   rQ   r|   r&   rf   r   r   r   rV   r	   r/   r   r   r.   r    r   r   )rD   r   r   asset_category_querysetasset_category_filteredasset_category_filtered_formasset_category_paginatorr   r   r   rO   r   r   s                rF    filter_pagination_asset_categoryr     s+    [[__Xr*FKK))+M+33779{{"5KK"9#
 $&& 	  (?'C'C$'+$  ))@.BRS++//&)K/88E'ImY/#+}}Vf+-#22,&B.33 [[__[1	 	rY   zasset.view_assetcategoryc                     t         j                  j                         }|j                         rd}nd}t	        |       }t        | ||      S )aH  
    View function for rendering a paginated list of asset categories.
    Args:
        request (HttpRequest): A Django HttpRequest object that contains information
        about the current request.
    Returns:
        A rendered HTML template that displays a paginated list of asset categories.
    Raises:
        None
    z!category/asset_category_view.htmlzcategory/asset_empty.html)r&   rf   r   existsr   r   )rD   r   templaterW   s       rF   asset_category_viewr     sE     $$((*H6..w7G'8W--rY   c                 2    t        |       }t        | d|      S )a  
    View function for rendering a paginated list of asset categories with search and filter options.
    Args:
        request (HttpRequest): A Django HttpRequest object that contains information
        about the current request.
    Returns:
        A rendered HTML template that displays a paginated list of asset
        categories  with search and filter options.
    Raises:
        None
    zcategory/asset_category.html)r   r   )rD   rW   s     rF   !asset_category_view_search_filterr     s     /w7G'97CCrY   c                    ddj                  | j                  d      dd       z   } ddddd}|j                  | d      \  }}|d	k(  r't        |d
|j                  j
                  i      }||fS |rt        |      nd}||fS )zf
    Determines the hx_url and hx_target based on the referer path
    for asset request creation
    rd      N)asset-dashboard-requestsdashboardAssetRequests)+asset-request-allocation-view-search-filterasset_request_allocation_list)profile-asset-tabasset_target)rd   /asset/dashboard/z%/asset/asset-request-allocation-view/z/employee/employee-profile/)NNr   emp_idkwargs)joinsplitrQ   r   rh   r\   )refererrg   hx_maphx_url	hx_targets        rF   request_creation_hx_returnsr     s    
 CHHW]]3/344G DS2
(
F 

FI $$43D3D3G3G(HI 9 %+9rY   c                    | j                   j                  dd      }t        || j                        \  }}t	        | j                        }|||d}| j
                  dk(  ret	        | j                  | j                        }|j                         r/|j                          t        j                  | t        d             ||d<   t        | d|      S )	a,  
    Creates a new AssetRequest object and saves it to the database.
    Renders the asset_request_creation.html template if the request method is GET.
    If the request method is POST and the form data is valid, the new
    AssetRequest is saved to the database and
    the user is redirected to the asset_request_view_search_filter view.
    If the form data is invalid, or if the request method is POST but the
    form data is not present, the user is
    presented with the asset_request_creation.html template with error
    messages displayed.
    rc   rd   )rg   )asset_request_formr   r   rM   zAsset request created!r   z.request_allocation/asset_request_creation.html)ro   rQ   r   rg   r"   rR   rM   rS   rT   r   rA   rB   r   )rD   r   r   r   rV   rW   s         rF   asset_request_creationr   9  s      ll~s3G3GW\\JFI.D%)V)TG~~7<<@==?IIKWa(@&AB(,$%'KWUUrY   zasset.add_assetassignmentc           
         t        j                  |      }| j                  d      }d| d}|s*t        j                  | t        d             t        |      S |j                  j                  j                  d      }| j                  dk(  r| j                  j                         }|j                  |d<   | j                  j                  |d	<   t!        || j"                        }|j%                         r4	 |j&                  d
   }d|_        |j+                          |j+                  d      }	| j                  j                  |	_        |	j+                          d|_        |j+                          t1        j2                  | j                  j                  |	j4                  j6                  t        d      t9        d      d|j:                   d|j.                   z   d       t        j<                  | t        d             t        d      S t!               }||jB                  d
   _"        ||d}tG        | d|      S # t>        $ r@}
t        j                  | t        d      tA        |
      z          t        |      cY d}
~
S d}
~
ww xY w)z}
    Approves an asset request with the given ID and updates the corresponding asset record
    to mark it as allocated.
    rd   z <script>window.location.href = "z";</script>zAsset request does not exist.	Availabler   rM   ri   assigned_by_employee_idr]   r   F)commitApprovedz%Your asset request has been approved!asset-request-allocation-view?asset_request_date=z&asset_request_status=	bag-check)	recipientverbr   iconz$Asset request approved successfully!r   zAn error occurred: N)asset_allocation_formr\   z%request_allocation/asset_approve.html)$r)   r   build_absolute_urir   rC   rB   r   rI   	asset_setr   rR   rM   r   requested_employee_idrg   rh   r   rl   rS   cleaned_datar   rT   r   asset_request_statusr>   sendri   employee_user_idr   asset_request_daterA   	Exceptionstrfieldsr   r   )rD   req_idasset_requesthomepage_urlerror_responser   	post_datarV   rE   
allocationerW   s               rF   asset_request_approver  W  s9    !%%f-M--c2L
.|nOL  w"A BCN++,,66==;=WF~~LL%%'	/</R/R	+,/6||/H/H	+,"9gmm<==?4))*5%-"

!YYeY4
5<\\5N5N
2!5?2""$LL--(@@QQBC$%DE,]-M-M,N O,,9,N,N+OQQ %   !,R*ST#$PQQ
 #$+1J((,F;G'BGLL  4w*?(@3q6(IJ#N334s   9DH> >	J5J<JJc                 6   | j                   j                  d      s%t        | j                   j                  dd            S | j                   j                  d      }|dk(  rN	 t        j                  | j
                  j                  dd            }t        t        dd	|i
      d| z         S | j                   j                  dd      }ddj                  |j                  d      dd        z   }|j                  d      r"t        d|j                  j                         S |j                  d      s|dk(  rt        t        d            S t        t        d       d| j
                  j                                S # t        j                  $ r g }Y w xY w)NHTTP_HX_REQUESTrc   rd   HTTP_HX_TARGETobjectDetailsModalW25Targetry   r   zasset-request-individual-viewasset_request_idr   z?requests_ids=r   r   z/employee/employee-view/z/asset/asset-request-tab/r   r   r   ?)ro   rQ   r   r}   r~   rP   JSONDecodeErrorr   r   r   r   
startswithr   r\   endswithr|   )rD   r   r   r   ry   referrers         rF   reject_request_returnr    s   <<-.#GLL$4$4^S$IJJ  !12I11	::gkkoond&KLL /9KV8T |n-.
 	
 ||3HSXXhnnS1!"566H56'(K(K(N(N'OP
 	
 ,-S :;<<@A
B!GKKDYDYD[C\] ' ## 	L	s   "/E? ?FFc                    t         j                  j                  |      }|j                          t	        j
                  | t        d             t        j                  | j                  j                  |j                  j                  dddddt        d      d	|j                   d
|j                   z   d	       t!        | ||      S )a  
    View function to reject an asset request.
    Parameters:
    request (HttpRequest): the request object sent by the client
    req_id (int): the id of the AssetRequest object to reject

    Returns:
    HttpResponse: a redirect to the asset request list view with a success
        message if the asset request is rejected successfully, or a redirect to the
        asset request detail view with an error message if the asset request is not
        found or already rejected
    r[   z Asset request has been rejected.zYour asset request rejected!.u0   تم رفض طلب الأصول الخاص بك!z"Ihr Antragsantrag wurde abgelehnt!u)   ¡Se ha rechazado su solicitud de activo!u(   Votre demande d'actif a été rejetée !r   r   z        &asset_request_status=r   r   r   verb_arverb_deverb_esverb_frr   r   )r)   rf   rQ   rT   r   r   rB   r>   r   rg   rh   r   r   r   r   r   r  )rD   r   r   s      rF   asset_request_rejectr    s     !((,,,7MMM'1?@A
KK!!55FF,B4;:89 !A!A B C,AABDD  !-@@rY   c                 @   t        d| j                  j                  i      }d|i}| j                  dk(  r\t        | j                        }|j                         r1|j                  j                  j                  }t        j                  j                  |      j                         }d|_        |j                          |j                         }| j                  j!                  d      }g }| j                  rR|D ]4  }t#               }||_        |j                          |j'                  |       6  |j(                  j*                  |  t        d| j                  j                  i      }t-        j.                  | t1        d             ||d<   t3        | d	|      S )
zY
    View function to create asset allocation.
    Returns:
    - to allocated view.
    r   rK   r   rM   r[   r   assign_imageszAsset allocated successfully!.z1request_allocation/asset_allocation_creation.html)r   rg   rh   rR   rM   rS   ru   r]   r\   r$   rf   r   r   r   rT   rl   rm   r*   imageappendr  addr   rA   rB   r   )	rD   rV   rW   rE   ru   filesattachmentsra   
attachments	            rF   asset_allocate_creationr     s^    *GLL,E,EFD '-G~~"7<<0==?MM**--EMM((E(288:E!)EJJLyy{HMM))/:EK}}! 3D!-J'+J$OO%&&z2	3
 +&&**K8&2GLL4M4MND Wa(H&IJ+/'('NPWXXrY   c                    | j                   j                         }t        j                  j	                  |      }d|_        |j                          t        d      j                  |j                        }t        j                  | |       t        d      }t        j                  | j                  j                   |d|j                   d|j"                   d|j                   d|j"                   d	|j                   d
|j"                   dd|j                   d|j"                   d|j                   d|j"                   t%        d      d|j"                   d|j                   d|j&                   z   d	       | j(                  j	                  d      dk(  rt%        d      }t+        | d|       S t-        | j(                  j	                  dd            S )zK
    Handle the initiation of a return request for an allocated asset.
    r[   Tz Return request for {} initiated.asset.change_assetassignmentzReturn request for z initiated from            u/   تم بدء طلب الإرجاع للمورد u               من الموظف u   Rückgabewunsch für z vom Mitarbeiter            z
 initiiertu   Solicitud de devolución para z%            iniciada por el empleado zDemande de retour pour u$               initiée par l'employé r   z?assigned_to_employee_id=z&        asset_id=z&assigned_date=r   r  r  truer   r  rc   rd   )rP   r|   r%   rf   rQ   return_requestrT   rB   formatr]   r   rA   r=   r>   r   rg   rh   ri   r   assigned_datero   r   r   )rD   r]   r   asset_assignmessagepermed_usersurls          rF   asset_allocate_return_requestr+    s   
 KK))+M"**..(.;L"&L23::<;P;PQGWg&+,JKL
KK!!"<#8#8"9 :1124A,BWBWAX Y+CCDF'(=(='> ?112*>01F1F0G H&&2&J&J%KM),*?*?)@ A%%1%I%I$JL89%l&J&J%K L''(8R8R7SUU !$ ||)*f4CD3%q011 0 0 EFFrY   r"  c                    t               }t        j                  j                  |d      j	                         }| j
                  dk(  r
t        | j                  | j                        }|j                         rt        j                  j                  |      j	                         }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }| j                  j                  d      }g }	||d	}
t        | d
|
      }|dk(  rt        j                  j                  |d      j	                         }||_        ||_        ||_        d|_        |j#                          | j                  rR|D ]4  }t%               }||_        |j#                          |	j)                  |       6  |j*                  j,                  |	  d|_        |j#                          t1        j2                  | t5        d             t7        |j8                  j;                  d      dz         S d|_        |j#                          t        j                  j                  |d      j	                         }||_        ||_        ||_        |j#                          | j                  rR|D ]4  }t%               }||_        |j#                          |	j)                  |       6  |j*                  j,                  |	  t1        j2                  | t5        d             t7        |j8                  j;                  d      dz         S ||d	}
||
d<   t        | d
|
      S )z
    View function to return asset.
    Args:
    - asset_id: integer value representing the ID of the asset
    Returns:
    - message of the return
    T)r]   return_status__isnullrM   r[   return_statusreturn_datereturn_conditionreturn_images)asset_return_formr]   zasset/asset_return_form.htmlHealthyFr   zAsset Return Successful !.zutf-8#<script>location.reload();</script>Not-AvailablezAsset Return Successful!.asset_alocation)r#   r%   rf   r   r   rR   rM   rl   rS   r$   rQ   rm   r   r/  r.  r0  r$  rT   r*   r  r  r1  r  r   r   r   rB   r   contentdecode)rD   r]   r2  r   rE   asset_return_statusasset_return_dateasset_return_conditionr  r  rW   responsera   r  s                 rF   asset_allocate_returnr=  !  s     ()&..55 6 eg  ~~+GLL'--H%%'MM((H(5;;=E"),,"2"2?"C ' 0 0 ?%,\\%5%56H%I"MM))/:EK,=8TGg'EwOH"i/#2#:#:#A#A%T $B $%' ! 0A ,1D .4J 127 / %%'== % 7%1^
+/
(")#**:6	7
 7$2266D%0"

gq)E'FG#$$++G4;<  "1EJJL.66==!  >  eg  ,=(-@*0F-!!#}}! 3D!-J'+J$OO%&&z2	3
 3 ..22K@MM'1%@#AB  ''03XX  %68LG!1G'97CCrY   c                 J
   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }|d}| j                  j                  }t        j
                  j                         }t        | dt        j
                  j                         d      t        j
                  j                  | j                  j                        z  }|j                         }| j                   j                  d	      rt        | |d	      }| j                   j                  d
      rt        | |d
      }|j                  |      j                  d      j                  |      }| j                   j                         }t        | j                   |      }	t        | j                   |      j                   }
|dk7  r|t#        |
|| j                   j                  d      d      }
|
D cg c]  }|d   	 }}g }|D ].  }|j$                  D ]  }|j'                  |j(                          0 t+        j,                  t/        |            }n\t1        |
| j                   j                  d            }
t+        j,                  |
j$                  D cg c]  }|j(                   c}      }t3        | j                   |      j                   }|dk7  r|t#        ||| j                   j                  d      d      }|D cg c]  }|d   	 }}g }|D ].  }|j$                  D ]  }|j'                  |j(                          0 t+        j,                  t/        |            }n\t1        || j                   j                  d            }t+        j,                  |j$                  D cg c]  }|j(                   c}      }t1        || j                   j                  d            }t+        j,                  |j$                  D cg c]  }|j(                   c}      }t5        |	j                   t7                     }| j                   j                  d      }|j9                  |      }t;        |      }t=        t        |       t=        t        |       t=        t>        |       ||
||	j@                  t               j@                  t3               j@                  ||tB        jD                  tF        jD                  |||||dS c c}w c c}w c c}w c c}w c c}w )a  
    Filter and paginate asset request and allocation data based on search criteria and sort options.

    This function handles the retrieval, filtering, and pagination of asset request and allocation
    data.It processes GET parameters to search, sort, and filter asset requests and allocations,
    and returns a context dictionary with the filtered data and associated forms for rendering in
    a template.
    r   request_fieldallocation_fieldr   asset.view_assetrequestr   )rD   r   r   field)r   assign_sortbyrequest_sortby)ri   F)r-  )asset_id__asset_name__icontainsr   r   list)r   asset_requestsasset_allocationsassets_filter_formasset_request_filter_formasset_allocation_filter_formrw   r   gp_request_fieldsgp_Allocation_fieldsr?  r@  ry   allocations_ids	asset_ids)$rP   rQ   rg   rh   r%   rf   r   r-   r)   r   distinctr1   excluder|   r   r   r   r;   r   r  r\   r}   r   rF  r0   r   r	   r/   r   r   r.   r$   rV   r   r   r   )rD   asset_request_allocation_searchr?  r@  employeeasset_assignmentr   r   r   assets_filteredasset_request_filteredentrylist_valuesid_listvalueru   ry   asset_allocation_filteredrN  
assets_ids	assets_idasset_paginatorr   r   s                           rF   *filter_pagination_asset_request_allocationr_  m  si    '.kkooh&?#KKOOO4M{{'9:&.*,'||((H&..224&&%%))+%	
 	##',,:S:S#TUM "**,M{{'!'+;_M{{'(w7GH 	A	u	-	0O	P  KK))+M'fEO/mTWW}8!2"M7;;??63JF"
 3IIuV}II  	,E!-- ,x{{+,	, zz$w-0 "/"GKKOOF$;"
 zz)?)K)KLXX[[L
 !6.!b  2"2">$5%'79PRX%
! 3LLuV}LL  	,E!-- ,x{{+,	, **T']3 %2%w{{v'>%
! **)B)N)NOXX[[O
 vw{{v'>?J


8N8NOHHKKOPI 2 2N4DEO++//&)K%%k2F'IlI.oy1eY'06-22%7%9%>%>(=(?(D(D 077 6 = =&,$* c J M M P Ps    T$TT5TT c                    t        |       }d}| j                  j                  d      dk7  r| j                  j                  d      9| j                  j                  d      dk7  r| j                  j                  d      d}t        | ||      S )z
    This view is used to display a paginated list of asset allocation requests.
    Args:
        request (HttpRequest): The HTTP request object.
    Returns:
        HttpResponse: The HTTP response object with the rendered HTML template.
    z5request_allocation/asset_request_allocation_view.htmlr?  r   r@   request_allocation/group_by.htmlr_  rP   rQ   r   rD   rW   r   s      rF   rk   rk     s{     9AGFH 	(B.KKOOO,8;;??-."4KKOO./;5'8W--rY   c                    t        |       }d}| j                  j                  d      dk7  r| j                  j                  d      9| j                  j                  d      dk7  r| j                  j                  d      d}t        | ||      S )a  
    This view handles the search and filter functionality for the asset request allocation list.
    Args:
        request: HTTP request object.
    Returns:
        Rendered HTTP response with the filtered and paginated asset request allocation list.
    z5request_allocation/asset_request_allocation_list.htmlr?  r   r@  ra  rb  rc  s      rF   +asset_request_alloaction_view_search_filterre    sy     9AGFH(B.KKOOO,8;;??-."4KKOO./;5'8W--rY   c                    t         j                  j                  |      }|j                  }||d}| j                  j                  d      }|r3t        j                  |      }t        ||      \  }}||d<   ||d<   ||d<   t        | d|      S )z
    This function is responsible for view the individual own asset

    Args:
        request : HTTP request object
        id (int): Id of the asset assignment
    r[   )rE   rT  r\  r   r   z&request_allocation/individual_own.html)	r%   rf   rQ   r]   rP   r}   r~   r+   r   )	rD   r]   rT  rE   rW   r   ry   r   r   s	            rF   own_asset_individual_viewrg    s     '..22h2?%%E,G  5zz"34.|XFW 1)
!'CWMMrY   c                 ^   | j                   j                  dd      j                  d       }t        j                  j                  |      }||d}| j
                  j                  d      }|r3t        j                  |      }t        ||      \  }}||d<   ||d<   ||d<   t        | d	|      S )
ai  
    Display the details of an individual asset request.

    This view retrieves the asset request with the given ID and renders it in the
    'individual_request.html' template. If a JSON-encoded list of request IDs is
    provided in the GET parameters, the view also determines the previous and next
    request IDs for easy navigation.

    Args:
        request (HttpRequest): The HTTP request object containing metadata about the request.
        id (int): The ID of the asset request to be viewed.

    Returns:
        HttpResponse: The rendered 'individual_request.html' template with the context data.
    HTTP_HX_CURRENT_URLr   zasset-request-allocation-view/r[   )r   r   ry   r   r   z*request_allocation/individual_request.html)
ro   rQ   r  r)   rf   rP   r}   r~   r+   r   )	rD   r
  r   r   rW   r   ry   r   r   s	            rF   asset_request_individual_viewrj  !  s    $ LL$$%:B?HH( I !((,,0@,AM&G  7zz"34.|=MNW"3)
!'GQQrY   c                    t         j                  j                  |      }d|i}| j                  j                  d      }|r3t	        j
                  |      }t        ||      \  }}||d<   ||d<   ||d<   t        | d|      S )a~  
    Display the details of an individual asset allocation.

    This view retrieves the asset allocation with the given ID and renders it in the
    'individual_allocation.html' template. If a JSON-encoded list of allocation IDs is
    provided in the GET parameters, the view also determines the previous and next
    allocation IDs for easy navigation.

    Args:
        request (HttpRequest): The HTTP request object containing metadata about the request.
        id (int): The ID of the asset allocation to be viewed.

    Returns:
        HttpResponse: The rendered 'individual_allocation.html' template with the context data.
    r[   r   rN  r   r   z-request_allocation/individual allocation.html)r%   rf   rQ   rP   r}   r~   r+   r   )rD   asset_allocation_idr   rW   allocation_ids_jsonallocation_idsr   r   s           rF    asset_allocation_individual_viewro  E  s    $ '..226I2J!#34G!++//*;<$78.~?RSW%8!")
!'JGTTrY   c                 2    t        j                  |       ry| S )z%
    Convert NaN values to None.
    N)rz   isna)vals    rF   convert_nanrs  c  s     
wws|JrY   zcsv_tmp/)locationc                    t        | j                               }t        j                  d|      }t        j	                  |      }t        |d      5 }t        j                  |      }t        |       g }|D ]u  }|\  }}	}
}}}}}t        j                  j                  |      \  }}t        j                  j                  |      \  }}|j                  t        ||	|
|||||             w 	 d d d        t        j                  j                         t         j                  j#                  |      rt!        j$                  |       y y # 1 sw Y   ^xY w)Nz_tmp.csvignore)errorsasset_category_name
lot_number)
asset_nameasset_descriptionasset_tracking_idasset_purchase_dateasset_purchase_costr   rI   asset_lot_number_id)r   readfsrT   pathopencsvreaderr   r&   rf   get_or_creater(   r  r$   bulk_createosr   r   )ra   file_content	file_nametmp_filecsv_filer  r^   rowr|  r}  r~  r  r  ry  r   asset_lot_numberrs   rB   	asset_lots                      rF   csv_asset_importr  o  sI   tyy{+L
L1Iwwy!H	hx	( "HH%V
 	C 	!!###  !. 5 5 C C$7 !D !NA $++99EU9VLIq)&7&7(;(;!-&4(1	%	"J 
MMj) 
ww~~h
		(  Q" "s   BEEc                    | j                         D ]  \  }}t        |d         }t        |d         }t        |d         }t        |d         }t        |d         }t        |d         }t        |d         }	t        |d         }
t        j                  j	                  |	      \  }}t
        j                  j	                  |	
      \  }}t        j                  j                  |||||||
|        y )N
Asset nameDescriptionTracking idPurchase datePurchase costCategoryBatch numberStatusrx  rz  r|  r}  r~  r  r  rI   r   r  )iterrowsrs  r&   rf   r  r(   r$   rn   )	dataframeindexr  r|  r}  r~  purchase_datepurchase_costcategory_namer{  r   rs   rn   r  s                 rF   spreadsheetml_asset_importr    s
   ((* 

s \!23
'M(:;'M(:;#C$89#C$89#C
O4 ^!45
S]+!.!6!6!D!D - "E "
 $,#3#3#A#AZ#A#X &!// - -, 0 	 		

rY   c                 j   | j                   j                  d      rt        | d      S 	 | j                  dk(  r| j                  j                  d      }|;|j
                  dk(  r,	 t        |       t        j                  | t        d             nq|P|j
                  dk(  rA	 t        j                  |      }t        |       t        j                  | t        d             nt        j                  | t        d	             t!        t"              S 	 t!        t"              S # t        $ r!}t        j                  | |        Y d}~Dd}~ww xY w# t        $ r!}t        j                  | |        Y d}~qd}~ww xY w# t        $ r/}t        j                  | |        Y d}~t!        t"              S d}~ww xY w)
a/  
    Handle the import of asset data from an uploaded Excel file.

    This view processes a POST request containing an Excel file, reads the data,
    creates Asset objects from the data, and saves them to the database. If the
    import is successful, a success message is displayed. Otherwise, appropriate
    error messages are shown.

    Args:
        request (HttpRequest): The HTTP request object containing metadata about the request.

    Returns:
        HttpResponseRedirect: A redirect to the asset category view after processing the import.
    r  zasset/asset_import.htmlrM   asset_importNztext/csvzSuccessfully imported AssetszAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetz
File Error)ro   rQ   r   rR   rl   content_typer  r   rA   rB   r   rC   rz   
read_excelr  KeyErrorr   r   )rD   ra   	exceptionr  s       rF   r  r    sw   " ||)*g8990>>V#==$$^4DD$5$5$C<$T*$$Wa0N.OP  %%VW< "d 3I.y9$$Wa0N.OP w,8/00+ $0 '((% ! <NN7yk;;<   <NN7yk;;<
  0w9+//'((0sk   ;E: %*D  E: "?E !.E:  	E
)E E: E

E: 	E7E2-E: 2E77E: :	F2F--F2c                     	 g d}t        j                  |      }t        d      }d|d<   |j                  |d       |S # t        $ r}t        |      cY d	}~S d	}~ww xY w)
zasset excel download view)r  r  r  r  r  r  r  r  columnszapplication/ms-excelr  z)attachment; filename="my_excel_file.xlsx"Content-DispositionFr  N)rz   	DataFramer   to_excelr   )_requestr  r  r<  r  s        rF   asset_excelr    sg    '	
 LL1	-CD*U&'851 'I&&'s   ?A 	A 
AA A c                 .   t        | j                  t        j                  j	                               }| j
                  dk(  rBt        j                  j	                         }|s*t        j                  | t        d             t        d      S t        | j                  |      j                  }g g g g g g g g d}g d}|D ]>  }|D ]5  }t        ||      }t        |t              r| j                  }	|	j                   }
t"        j                  j%                  |
      }|j'                         rT|D ]  }|j(                  } t*        j                  j%                        }|j-                         }|r|j.                  nd}nd}t1        j2                  t5        |      d	      j                         }t7        j8                         D ]  \  }}||k(  s|j;                  |      } ||   j=                  ||nd
       8 A |D ])  }||   d
gt?        |      t?        ||         z
  z  z   ||<   + tA        jB                  |      }|jE                  ddddddddd      }tG        d      }d|d<   |jI                  |d       |S d|i}tK        | d|      S )zasset export viewr   rM   zThere are no assets to export.zasset-category-viewr  )employee_id)companyzMMM. D, YYYYz%Y-%m-%dNr  r  r  r  r  r  r  r  r  zapplication/vnd.ms-excelr  z"attachment; filename="assets.xlsx"r  Fr  asset_export_filterz!category/asset_filter_export.html)&r   rP   r$   rf   r   rR   r   warningrB   r   rM   r   getattr
isinstancer   rg   rh   r4   r   r   
company_idr2   r   date_formatr   strptimer   r<   itemsstrftimer  r   rz   r  renamer   r  r   )rD   r  queryset_allr   datafields_to_checkrE   rB  rZ  rg   empr   iemployee_companycompany_nameemp_companyr  
start_dateformat_nameformat_stringkeyr  r<  rW   s                           rF   asset_export_excelr    s    ,GKK%--BSBSBUV~~}}((*Wa(H&IJ122$W\\LILL !#!##%#%!##%	
	
  !	IE(  Iu-eT*"<<D++C 3::AAcARD{{}!% <A/0||,<'.'='=FV'='W&2&8&8&: 8CK33 $ '5 "*!2!23u:z!J!O!O!QJ 7K6P6P6R G2]&+5$.$7$7$FEG
 U""E,=54HA I!	IH  	NCS	TFc(mc$s)n.L$MMDI	N LL&	 $$*%2%2'6'6%/ ('5	 % 
	  -GH*N&'851$&9:G'>HHrY   zasset.add_assetlotc                    | j                   j                  d      r| j                   j                  d      n| j                   j                         }t               }||ddd}| j                  dk(  rUt        | j
                        }|j                         r*|j                          t               }t        j                  | t        d             t        j                  j                         j                         dk(  r|st        d      S |r| j                   j                  d      }t!        d	|g
      }t        j                  j#                         j%                  d      j'                         }| j                   j)                         }t+        |j,                        |d<   | d|j                          |d<   d|d<   ||d<   t/        | d|      S )z asset batch number creation viewr  N)asset_batch_formhx_valshx_getr   rM   z"Batch number created successfully.r   r4  rI   rN   )args-idr  r  r  z#objectCreateModalTargetr   r  z&batch/asset_batch_number_creation.html)rP   rQ   r|   r   rR   rM   rS   rT   r   rA   rB   r(   rf   r   r   r   r   r   order_byr   r   r   r\   r   )rD   r  r  rW   category_idr*  ru   mutable_gets           rF   asset_batch_number_creationr  j  s   
 $+;;??6#:@U@U@W  &',	G ~~)',,7$$&!!#-/Wa(L&MN&&(..0A5g#$IJJ%kkoo.AB.k]C#++//1::5AGGI%kk..0585E12'*e1[-B-B-D,E$F!'A$&6"#'CWMMrY   zasset.view_assetlotc                 V   t         j                  j                         }| j                  j	                         }t        |t                     }| j                  j                  d      }|j                  |      }t               }|j                         rd}nd}|||d}t        | ||      S )z{
    View function to display details of all batch numbers.

    Returns:
    -  all asset batch numbers based on page
    r   z"batch/asset_batch_number_view.htmlzbatch/asset_batch_empty.html)batch_numbersr  rw   )r(   rf   r   rP   r|   r	   r/   rQ   r   r   r   r   )	rD   asset_batchesr   $asset_batch_numbers_search_paginatorr   asset_batch_numbersr  r   rW   s	            rF   asset_batch_viewr    s     $$((*MKK))+M+4]NDT+U(++//&)K>GGT%'71,,G
 '8W--rY   zasset.change_assetlotc                    t         j                  j                  |      }t         j                  j                  |      }t        |      }d|i}t        j                  j                  |      }|rdt        |      }|d   j                  j                  j                  j                  ddi       ||d<   | j                  dk(  rt        d      nd	|d
<   | j                  dk(  r[t        | j                  |      }|j                         r/|j                          t        j                   | t        d             ||d<   t#        | d|      S )
    View function to return asset.
    Args:
    - batch_id: integer value representing the ID of the asset
    Returns:
    - message of the return
    r[   rt   asset_batch_update_formr  r{  readonlyrP   z#This batch number is already in-useNin_use_messagerM   zBatch updated successfully.z$batch/asset_batch_number_update.html)r(   rf   rQ   r   r$   r   rB  widgetattrsupdaterR   rB   rM   rS   rT   r   r   r   )rD   batch_idasset_batch_numberasset_batchr  rW   assigned_batch_numbers          rF   asset_batch_updater    s@    "))---:""&&(&3K%{;!#3G "MM00EW0X);?&,,3399@@$	
 .>)* ~~& 34 	 !
 ~~)',,AST$$&!!#MM'1%B#CD-=)*'A7KKrY   zasset.delete_assetlotc                    | j                   j                         }	 t        j                  j	                  |      }t
        j                  j                  |      }|r-t        j                  | t        d             t        d|       S |j                          t        j                  | t        d             t        j                  j                         st        d      S t        d|       S # t        j                  $ r" t        j                  | t        d             Y kt        $ r" t        j                  | t        d             Y w xY w)	r  r[   r  zBatch number in-usez!/asset/asset-batch-number-search?zBatch number deletedzBatch number not foundz$You cannot delete this Batch number.r4  )rP   r|   r(   rf   rQ   r$   r   r   rC   rB   r   r@   rA   r   r
   r   )rD   r  r   r  r  s        rF   asset_batch_number_deleter    s    KK))+MK%--11X1> % 4 4 2 !5 !
 !NN7A&;$<=?OPP!!#!$:";<
 ""$ABB7GHH    =w": ;< Kw"H IJKs   A.C1 /C1 12E%'EEc                 t   | j                   j                  d      }|d}t        j                  j	                         j                  |      }| j                   j                         }t        |t                     }| j                   j                  d      }|j                  |      }||d}t        | d|      S )z
    View function to return search  data of asset batch number.

    Args:
    - id: integer value representing the ID of the asset

    Returns:
    - message of the return
    r   r   )lot_number__icontainsr   )r  rw   z"batch/asset_batch_number_list.html)rP   rQ   r(   rf   r   r   r|   r	   r/   r   r   )rD   search_queryr  r   r  r   r  rW   s           rF   asset_batch_number_searchr    s     ;;??8,L$$((*111UMKK))+M+4]NDT+U(++//&)K>GGT -G
 '?IIrY   c                     | j                   dk(  rb| j                  j                  d      }|Et        j                  j                  |      }|j
                  j                         }t        |      S t        d      S )z
    View function to return update asset count at asset category.
    Args:
    - id: integer value representing the ID of the asset category
    Returns:
    - count of asset inside the category
    rM   rI   r[   rC   )rR   rM   rQ   r&   rf   r   r   r   )rD   r  categoryr   s       rF   asset_count_updater  	  sn     ~~ll&&':;"$,,00K0@H",,224K,,  rY   c                     t         j                  j                         }t         j                  j                  d      }||d}t	        | d|      S )zJ
    This method is used to render the dashboard of the asset module.
    r   r   )r   asset_in_usezasset/dashboard.html)r$   rf   r   r   r   )rD   r   r  rW   s       rF   asset_dashboardr    sN     ]] F==''X'>L $G '17;;rY   rA  c                     t         j                  j                  dd      }t        j                  |D cg c]  }|j
                   c}      }||d}t        | d|      S c c}w )aN  
    Handles the asset request approval dashboard view.

    This view fetches and filters asset requests that are currently in the
    "Requested" status and belong to employees who are active.

    The filtered asset requests are then passed to the template for rendering,
    along with a JSON-encoded list of the request IDs.
    	RequestedT)r    requested_employee_id__is_active)rG  ry   z#asset/dashboard_asset_requests.html)r)   rf   r   r}   r   r\   r   )rD   rG  ru   ry   rW   s        rF   asset_dashboard_requestsr  +  sh     "))00(4 1 N ::>Jxx{{JKL($G '@'JJ Ks   A!zasset.view_assetassignmentc                 f    t         j                  j                  dd      }d|i}t        | d|      S )Nr   T)asset_id__asset_status"assigned_to_employee_id__is_activerH  z%asset/dashboard_allocated_assets.html)r%   rf   r   r   )rD   rH  rW   s      rF   asset_dashboard_allocatesr  B  sF     (//66'D 7  	.G 'BGLLrY   c                 ~   t         j                  j                  d      }t         j                  j                  d      }t         j                  j                  d      }g d}t        d      t	        |      t	        |      t	        |      gdg}||t        d      d	t
        j                   d
d}t        |      S )zb
    This function returns the response for the available asset chart in the asset dashboard.
    r   r   r5  r   )r   r   r5  rE   labelr  Oops!! No Asset found...rd   images/ui/asset.pnglabelsdatasetr(  emptyImageSrc)r$   rf   r   rB   r   r5   
STATIC_URLr   )r  asset_availableasset_unavailabler  r  r  r<  s          rF   asset_available_chartr  N  s     mm***DO,,/,J==''X'>L5F wZ&O(<cBS>TU	
G /0X0011DE	H !!rY   c                 t   t         j                  j                         }g }|D ]:  }d}t        |j                  j                  d            }|j                  |       < |D cg c]  }|j                   }}t        d      |dg}||t        d      dt        j                   dd	}t        |      S c c}w )
za
    This function returns the response for the asset category chart in the asset dashboard.
    r   r   r   zassets in user  r  rd   r  r  )r&   rf   r   r   r   r   r  ry  rB   r5   r	  r   )	r  r   r  rs   category_countr  r  r  r<  s	            rF   asset_category_chartr  i  s     %,,002D* $^55<<(<STN#$
 <LLxh**LFL '	
G /0X0011DE	H !! Ms   $B5c                    | j                   j                         dz   }t        ddi      j                  j	                  d      }t        |      }t        t        |       t        || j                   j                  d            }t        j                  |j                  D cg c]  }|j                   c}      }|t               |t               j                  ||d}t!        | d|      S c c}w )z
    This function is responsible for loading the asset history view

    Args:


    Returns:
        returns asset history view template
    z&returned_assets=Truereturned_assetsTruer  r   )asset_assignmentsfr   	gp_fieldsrz   ry   z%asset_history/asset_history_view.html)rP   r|   r   r   r  r   r.   r%   r0   rQ   r}   r   r   r\   r   r   r   )rD   r   r  r   ru   ry   rW   s          rF   asset_historyr    s     KK))+.EEM*,=v+FGJJSS 'Ioy1%&79PQ::%6%B%BCCL /! (*11$G 'BGLL 	Ds   !C+c                     t        t        |      }d|i}| j                  j                  d      }|r3t	        j
                  |      }t        ||      \  }}||d<   ||d<   ||d<   t        | d|      S )z
    this method is used to view details of individual asset assignments

    Args:
        request (HTTPrequest): http request
        asset_id (int): ID of the asset assignment

    Returns:
        html: Returns asset history single view template
    r[   rT  ry   r   r   z,asset_history/asset_history_single_view.html)r   r%   rP   rQ   r}   r~   r+   r   )rD   r]   rT  rW   r   ry   r   r   s           rF   asset_history_single_viewr    s     )XF!#34G7zz"34.|XFW"3)
!6 rY   c           
      :   | j                   j                         }t        | j                         j                  j	                  d      }t        | |d      }d}| j                   j                  d      }|dk7  r|t        ||| j                   j                  d      d      }d}|D cg c]  }|d   	 }}g }|D ].  }|j                  D ]  }	|j                  |	j                          0 t        j                  t        |            }
n\t        || j                   j                  d            }t        j                  |j                  D 	cg c]  }	|	j                   c}	      }
t        |      }t!        t"        |       t%        | ||||||
d	      S c c}w c c}	w )
z
    This method is used to filter the asset history view or to group by the datas.

    Args:
        request (HTTPrequest):http request

    Returns:
        returns asset history list or group by
    r  r1   z%asset_history/asset_history_list.htmlrB  r   r   zasset_history/group_by.htmlrF  )r  r   rB  rz   ry   )rP   r|   r   r   r  r1   rQ   r;   r   r  r\   r}   r   rF  r0   r   r.   r%   r   )rD   r   r  r   rB  rW  rX  rY  rZ  ru   ry   r   s               rF   asset_history_searchr    s    KK))+M*7;;7::CCEJw(98D6HKKOOG$E{u(-ugkkoof&=v
 12CDuV}DD  	,E!-- ,x{{+,	, zz$w-0)*;W[[__V=TUzz):)F)FGXX[[G
 'Ioy1!2$(	

 
! E Hs   'FFzasset.view_assetc                 f   t         j                  j                  |      }|j                  j	                         }|j
                  j	                         }|r-t        j                  |D cg c]  }|j                   c}      nt        j                  g       }||||d}t        | d|      S c c}w )z
    This function is used to view asset tab of an employee in employee individual view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee.

    Returns: return asset-tab template

    r[   )r   requestsr\  rS  ztabs/asset-tab.htmlr{   )
r3   rf   rQ   requested_employeer   allocated_employeer}   r   r\   r   )rD   r   rS  assets_requestsr   ru   r\  rW   s           rF   	asset_tabr     s     ##v#.H11557O((,,.F<B

7HHKK78

SU  # 	G '0'BB 8s   *B.c                     t         j                  j                  |      }|j                  j	                         }t        j                  |D cg c]  }|j                   c}      }||d}t        | d|      S c c}w )z
    This function is used to view asset tab of an employee in employee profile view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee.

    Returns: return profile-asset-tab template

    r[   )r   r\  ztabs/profile-asset-tab.htmlr{   )	r3   rf   rQ   r  r   r}   r   r\   r   )rD   r   rS  r   ru   r\  rW   s          rF   profile_asset_tabr"    ss     ##v#.H((,,.F@XX[[@AJ G '8'JJ As   A;c                    t         j                  j                  |      }|j                  j	                         }t        j                  |D cg c]  }|j                   c}      }|||d}t        | d|      S c c}w )a  
    This function is used to view asset request tab of an employee in employee individual view.

    Parameters:
    request (HttpRequest): The HTTP request object.
    emp_id (int): The id of the employee.

    Returns: return asset-request-tab template

    r[   )rG  r   ry   ztabs/asset_request_tab.htmlr{   )	r3   rf   rQ   r  r   r}   r   r\   r   )rD   r   rS  r  ru   ry   rW   s          rF   asset_request_tabr$  +  sv     ##v#.H11557O::?Kxx{{KLL)$G
 '8'JJ Ls   A<)N)__doc__r  r}   r  r   r   urllib.parser   pandasrz   django.contribr   django.core.files.baser   django.core.files.storager   django.core.paginatorr	   django.db.modelsr
   django.httpr   r   r   django.shortcutsr   r   r   django.urlsr   django.utils.translationr   rB   asset.filtersr   r   r   r   r   r   r   r   r   r   asset.formsr   r   r   r    r!   r"   r#   asset.modelsr$   r%   r&   r'   r(   r)   r*   base.methodsr+   r,   r-   r.   r/   r0   r1   base.modelsr2   employee.modelsr3   r4   horillar5   horilla.decoratorsr6   r7   r8   r9   r:   horilla.group_byr;   horilla.horilla_settingsr<   horilla.methodsr=   notifications.signalsr>   rG   rX   rp   r   r   r   r^   r   r   r   r   r   r   r   r   r  r  r  r   r+  r=  r_  rk   re  rg  rj  ro  rs  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r"  r$   rY   rF   <module>r>     sY  
   	 # !  # . 7 + + H H @ @  6           =   / 9 4 (D &'#A (  #AL $ $N )*2G +  2Gj D  D, ./S 0 Sl %=  %=P 34M 5  M, 67K 8  K2 67Q 8 Q+\ 45. 6 .* 45D 6 D @ V  V8 566M 7  6Mr@ 56A 7 AB 56!Y 7 !YH !G !GH 89GD : GDTjZ . ., . .* N  N0 R  RD U  U8 
+.b
6 +,*) - *)Z ' '2 /0aI 1 aIH ./N 0 N@ /0. 1 .4 12 L 3  LF 12I 3 I8 J  J8 ! !" 45< 6 < 34K 5 K* 67M 8 M 45" 6 "2 45" 6 "8 67M 8 M> 67 8 6 67. 8 .b #X.C / C4 K  K* K  KrY   