
    vg@                     j   d 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mZ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 ddlmZ d Z G d de      Z G d dej(                        Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d de      Z%y)z
forms.py
Asset Management Forms

This module contains Django ModelForms for handling various aspects of asset management,
including asset creation, allocation, return, category assignment, and batch handling.
    N)date)forms)ValidationError)gettext_lazy)AssetAssetAssignmentAssetCategoryAssetDocumentsAssetLotAssetReportAssetRequest)	ModelForm)reload_queryset)MultipleFileField)Employee)_thread_localsc                 ^    i }| j                   | j                   j                  d      |d<   |S )z3this is used to update change the date value formatz%Y-%m-%dasset_purchase_date)r   strftime)instanceinitials     /var/www/horilla/asset/forms.pyset_date_field_initialr       s9    G##/)1)E)E)N)N*
%& N    c                   <     e Zd ZdZ G d d      Z fdZd Z xZS )	AssetFormzB
    A ModelForm for creating and updating asset information.
    c                      e Zd ZdZeZdZddgZ ej                  ddd       ej                  d	 ed
      dddd       ej                  ddd       ej                  ddd       ej                  ddd       ej                  ddd       ej                  ddi       ej                  ddi       ej                  dddd      d	Zy)AssetForm.Metaa*  
        Specifies the model and fields to be used for the AssetForm.
        Attributes:
            model (class): The model class Asset to be used for the form.
            fields (str): A special value "__all__" to include all fields
                          of the model in the form.
        __all__	is_activeownerzMacbook Pro.oh-input w-100placeholderclassattrstextz#A powerful laptop for business use.+oh-input oh-input--textarea oh-input--block   (   typer$   r%   rowscolsLPT001r   zoh-input  w-100)r-   r%   z1200.00.)r%   r$   r%   z/oh-select oh-select-2 select2-hidden-accessiblez,oh-select oh-select--lg oh-select-no-search z1oh-select oh-select-2 select2-hidden-accessible  LOT001zbatchNoChange($(this)))r%   r$   onchange)	
asset_nameasset_descriptionasset_tracking_idr   expiry_dateasset_purchase_costasset_category_idasset_statusasset_lot_number_idN)__name__
__module____qualname____doc__r   modelfieldsexcluder   	TextInputTextarea_	DateInputNumberInputSelectwidgets r   r   Metar   0   s   	 ()%//&4?OP "0"#$%J#KJ" "1&.9IJ" $35??%0AB$ +5??%0AB $55#4#4 0L$ ".N"
 )ELL NO $05<<P#+ 8$C(
r   rJ   c                    t        t        d      }|j                  d      }|rt        |      |d<   t	        |   |i | t        | j                         | j                  d   j                  j                  j                  dt        t        j                               i       | j                  d   j                  j                  j                  dt        t        j                               i       | j                  d   j                  j                  j                  dt        t        j                               i       |j                  j                  d      rd	t!        d
      fgt#        | j                  d   j$                  j'                  dd            z   }|| j                  d   _        | j*                  j,                  /| j                  d   xj(                  dt!        d      fgz  c_        y y y )Nrequestr   r   r8   idr:   r9   zasset.add_assetlot z---Choose Batch No.---
lot_numbercreatezCreate new batch number)getattrr   getr   super__init__r   r@   widgetr'   updatestruuiduuid4userhas_permrD   listquerysetvalues_listchoicesr   pk)selfargskwargsrL   r   batch_no_choices	__class__s         r   rT   zAssetForm.__init__f   s   .)4::j) 6x @F9$)&)$'(//55<<dC

DU=VW)*1177>>3tzz|$%	
 	N#**0077s4::<?P8QR<<  !56!#Q'?%@ ABT12;;GG,F  
 :JDKK-.6}}'12::q!:;<? : ( 7r   c                 b   | j                   }t        j                  j                  |j                        j                         }|j                  r| j                  j                  dd       r`| j                  j                  dd       |j                  k7  r7|j                  j                  d      j                         rt        ddi      t        j                  j                  | j                  d         j                  |j                        j                         rt        ddi      y y )	NrM   r9   T)return_status__isnullz(Asset in use you can"t change the statusr5   )r5   z+Already asset with this tracking id exists.)r   r   objectsfilterr`   firstcleaned_datarR   r9   assetassignment_setexistsr   datarA   )ra   r   prev_instances      r   cleanzAssetForm.clean~   s   ==,,,<BBD;;!!%%nd;%%)).$? --. //66*. 7 &( *')ST  $$tyyAT7U$VHKK( &(*WX 	 r   )r;   r<   r=   r>   rJ   rT   rq   __classcell__re   s   @r   r   r   +   s    4
 4
l0r   r   c            
       x    e Zd ZdZ ej
                   ej                  dddddd      	      Z G d
 d      Zy)DocumentFormz
    Form for uploading documents related to an asset.

    Attributes:
    - file: A FileField with a TextInput widget for file upload, allowing multiple files.
    fileFilezform-controlTruez.jpeg, .jpg, .png, .pdf)namer-   r%   multipleacceptr&   )rU   c                        e Zd ZdZeZdgZdgZy)DocumentForm.Metaa  
        Metadata options for the DocumentForm.

        Attributes:
        - model: The model associated with this form (AssetDocuments).
        - fields: Fields to include in the form ('file').
        - exclude: Fields to exclude from the form ('is_active').
        rv   r    N)r;   r<   r=   r>   r
   r?   r@   rA   rI   r   r   rJ   r}      s!    	 
 -r   rJ   N)	r;   r<   r=   r>   r   	FileFieldrB   rv   rJ   rI   r   r   ru   ru      sE     5??u'"3

D   r   ru   c                   6     e Zd ZdZ G d d      Z fdZ xZS )AssetReportForma[  
    Form for creating and updating asset reports.

    Metadata:
    - model: The model associated with this form (AssetReport).
    - fields: Fields to include in the form ('title', 'asset_id').
    - exclude: Fields to exclude from the form ('is_active').

    Methods:
    - __init__: Initializes the form, disabling the 'asset_id' field.
    c                   "    e Zd ZdZeZddgZdgZy)AssetReportForm.Metaa  
        Metadata options for the AssetReportForm.

        Attributes:
        - model: The model associated with this form (AssetReport).
        - fields: Fields to include in the form ('title', 'asset_id').
        - exclude: Fields to exclude from the form ('is_active').
        titleasset_idr    N)r;   r<   r=   r>   r   r?   r@   rA   rI   r   r   rJ   r      s$    	 
 -r   rJ   c                 p    t        |   |i | d| j                  d   j                  j                  d<   y)z
        Initialize the AssetReportForm, disabling the 'asset_id' field.

        Args:
        - *args: Variable length argument list.
        - **kwargs: Arbitrary keyword arguments.
        disabledr   N)rS   rT   r@   rU   r'   ra   rb   rc   re   s      r   rT   zAssetReportForm.__init__   s6     	$)&);EJ&&,,Z8r   r;   r<   r=   r>   rJ   rT   rr   rs   s   @r   r   r      s    
   "	F 	Fr   r   c                   $    e Zd ZdZ G d d      Zy)AssetCategoryFormzC
    A form for creating and updating AssetCategory instances.
    c            	           e Zd ZdZeZdZdgZ ej                   e
d      dd       ej                  d e
d	      d
ddd      dZy)AssetCategoryForm.Metaa  
        Specifies the model and fields to be used for the AssetForm.
        Attributes:
            model (class): The model class AssetCategory to be used for the form.
            fields (str): A special value "__all__" to include all fields
                          of the model in the form.
            widgets (dict): A dictionary containing widget configurations for
                            specific form fields.
        r   r    z
Computers.r"   r#   r&   r(   z$A category for all types of laptops.r)   r*   r+   r,   )asset_category_nameasset_category_descriptionN)r;   r<   r=   r>   r	   r?   r@   rA   r   rB   rD   rC   rH   rI   r   r   rJ   r      sf    	 -#25??&'o@PQ$ +9%.."#$%K#LJ+	
r   rJ   N)r;   r<   r=   r>   rJ   rI   r   r   r   r      s    
 
r   r   c                   6     e Zd ZdZ G d d      Z fdZ xZS )AssetRequestFormzN
    A Django ModelForm for creating and updating AssetRequest instances.
    c                       e Zd ZdZeZdZdgZ ej                  ddi       ej                  ddi       ej                  dd ed	      d
ddd      dZy)AssetRequestForm.Metaa  
        Specifies the model and fields to be used for the AssetRequestForm.
        Attributes:
            model (class): The model class AssetRequest to be used for the form.
            fields (str): A special value "__all__" to include all fields
                          of the model in the form.
            widgets (dict): A dictionary containing widget configurations for
                            specific form fields.
        r   r    r%   z0oh-select  oh-select-2 select2-hidden-accessibler&   r(   objective_descriptionz6Requesting a laptop for software development purposes.r)   r*   r+   )r-   rM   r$   r%   r.   r/   )requested_employee_idr8   descriptionN)r;   r<   r=   r>   r   r?   r@   rA   r   rG   rC   rD   rH   rI   r   r   rJ   r     s    	 -%1U\\O&
 ".O"
 *5>>"1#$P$ K	
r   rJ   c                    |j                  dd       }t        |   |i | t        | j                         ||j                  d      rt        j                  j                         | j                  d   _	        t        j                  j                  |j                  j                        j                         | j                  d   _        nPt        j                  j                  |      | j                  d   _	        |j                  | j                  d   _        | j                  d   j                  j                   j#                  dt%        t'        j(                               i       y )NrZ   zasset.add_assetrequestr   rg   )employee_user_idr8   rM   )poprS   rT   r   r@   r[   r   ri   allr]   rj   employee_getrM   rk   r   rU   r'   rV   rW   rX   rY   )ra   rb   rc   rZ   re   s       r   rT   zAssetRequestForm.__init__5  s$   zz&$'$)&)$.F G<D<L<L<P<P<RDKK/09;C;K;K;R;R$$'' <S <eg KK/08 =E<L<L<S<S!% =T =DKK/09 <@;L;LDKK/08'(//55<<dC

DU=VWr   r   rs   s   @r   r   r   	  s    %
 %
NX Xr   r   c                   6     e Zd ZdZ fdZ G d d      Z xZS )AssetAllocationFormzQ
    A Django ModelForm for creating and updating AssetAssignment instances.
    c                    t        |   |i | t        | j                         t        j
                  j                  d      | j                  d   _        t               | j                  d<   d| j                  d   _	        y )N	Available)r9   r   assign_imagesT)
rS   rT   r   r@   r   ri   rj   r]   r   requiredr   s      r   rT   zAssetAllocationForm.__init__L  sn    $)&)$+0==+?+?$ ,@ ,
J( (9':O$04O$-r   c                       e Zd ZdZeZdZg dZ ej                  ddi       ej                  ddi       ej                  ddi      dZ
y)	AssetAllocationForm.Metaa  
        Specifies the model and fields to be used for the AssetAllocationForm.
        Attributes:
            model (class): The model class AssetAssignment to be used for the form.
            fields (str): A special value "__all__" to include all fields
                          of the model in the form.
            widgets (dict): A dictionary containing widget configurations for
                            specific form fields.
        r   )return_datereturn_conditionassigned_datereturn_imagesr    r%   zoh-select oh-select-2 r&   )r   assigned_to_employee_idassigned_by_employee_idN)r;   r<   r=   r>   r   r?   r@   rA   r   rG   rH   rI   r   r   rJ   r   V  si    	  
 %G5M+NO'3u|| 89( (4u||5(

r   rJ   r;   r<   r=   r>   rT   rJ   rr   rs   s   @r   r   r   G  s    5
 
r   r   c                   <     e Zd ZdZ G d d      Z fdZd Z xZS )AssetReturnFormzX
    A Django ModelForm for updating AssetAssignment instances during asset return.
    c            	           e Zd ZdZeZg dZ ej                  dddd       ej                  dd	d
 e
d      d       ej                  ddd      dZy)AssetReturnForm.Metaa  
        Specifies the model and fields to be used for the AssetReturnForm.
        Attributes:
            model (class): The model class AssetAssignment to be used for the form.
            fields (list): The fields to include in the form, referring to
                           related AssetAssignment fields.
            widgets (dict): A dictionary containing widget configurations for
                            specific form fields.
        )r   r   return_statusr   r   r"   true)r-   r%   r   r&   r)   r*   r+   z=on returns the laptop. However, it has suffered minor damage.)r%   r.   r/   r$   zoh-select oh-select-2)r%   r   )r   r   r   N)r;   r<   r=   r>   r   r?   r@   r   rE   rC   rD   rG   rH   rI   r   r   rJ   r     st    	  V*5??%0@fU !/J#$W$		! *U\\ 7VL
r   rJ   c                     t        |   |i | t        j                         | j                  d   _        t        d      | j                  d<   d| j                  d   _        y)z`
        Initializes the AssetReturnForm with initial values and custom field settings.
        r   Images)labelr   TN)rS   rT   r   todayr@   r   r   r   r   s      r   rT   zAssetReturnForm.__init__  sR     	$)&)-1ZZ\M"*'8x'HO$04O$-r   c                     | j                   j                  d      }|r5|t        j                         kD  rt	        j
                  t        d            |S )aF  
        Validates the 'return_date' field.

        Ensures that the return date is not in the future. If the return date is in the future,
        a ValidationError is raised.

        Returns:
        - The cleaned return date.

        Raises:
        - forms.ValidationError: If the return date is in the future.
        r   z$Return date cannot be in the future.)rl   rR   r   r   r   r   rD   )ra   r   s     r   clean_return_datez!AssetReturnForm.clean_return_date  sG     ''++M:;5''*P(QRRr   )r;   r<   r=   r>   rJ   rT   r   rr   rs   s   @r   r   r   z  s    
 
@5r   r   c                   8     e Zd ZdZd fdZ G d d      Z xZS )AssetBatchFormzI
    A Django ModelForm for creating or updating AssetLot instances.
    c                 N    t        |   |i | t        | j                         y )N)rS   rT   r   r@   r   s      r   rT   zAssetBatchForm.__init__  s!    $)&)$r   c            	           e Zd ZdZeZdZ ej                  ddd       ej                  d e
d      d	d
dd      dZy)AssetBatchForm.Metaa  
        Specifies the model and fields to be used for the AssetBatchForm.
        Attributes:
            model (class): The model class AssetLot to be used for the form.
            fields (str): A special value "__all__" to include all fields
                          of the model in the form.
            widgets (dict): A dictionary containing widget configurations for
                            specific form fields.
        r   zA12345.r"   r#   r&   r(   zhA batch of 50 laptops, consisting of Lenovo ThinkPad T480s                              and Dell XPS 13.r)   r*   r+   r,   )rO   lot_descriptionN)r;   r<   r=   r>   r   r?   r@   r   rB   rC   rD   rH   rI   r   r   rJ   r     sb    	 )%//&/:JK  .u~~"#$0$ K	 	
r   rJ   )returnNr   rs   s   @r   r   r     s    %
 
r   r   )&r>   rX   datetimer   djangor   django.core.exceptionsr   django.utils.translationr   rD   asset.modelsr   r   r	   r
   r   r   r   
base.formsr   base.methodsr   employee.formsr   employee.modelsr   horilla.horilla_middlewaresr   r   r   ru   r   r   r   r   r   r   rI   r   r   <module>r      s       2 6   ! ( , $ 6i	 iX" 5?? " J'Fi 'FT 
	  
F;Xy ;X|-
) -
fAi AH&
Y &
r   