
    siu:                     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/python-projects/worksol/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eZdZdgZ ej                  ddd       ej                  ddd       ej                  ddi      d	Z
d
ddddddddd	Zy)AssetForm.Meta__all__	is_activer   oh-input w-100)typeclassattrsonchangezbatchNoChange($(this)))r   expiry_dateasset_lot_number_idz
Asset NameDescriptionzTracking IDzPurchase DatezExpiry DateCostCategoryStatuszBatch Number)	
asset_nameasset_descriptionasset_tracking_idr   r'   asset_purchase_costasset_category_idasset_statusr(   N)__name__
__module____qualname__r   modelfieldsexcluder   	DateInputSelectwidgetslabels r   r   Metar   0   s    -#25??%0@A$ +5??%0@A $05<<!#;<$

 '!.!.#2(#)!+$#1

r   r>   c                    t        t        dd       }|j                  d      }|r|j                  dt	        |             t        	|   |i | dD ci c]   }|t        t        j                               " }}|j                         D ]+  \  }}|| j                  |   j                  j                  d<   - |r|j                  j                  d      rt!        | j                  d   j"                  j%                  dd            }|j'                  d	d
t)        d      f       | j*                  j,                  s|j/                  dt)        d      f       || j                  d   _        y y y c c}w )Nrequestr   r   )r1   r(   r2   idzasset.add_assetlotr(   
lot_numberr    z---Choose Batch No.---createzCreate new batch number)getattrr   get
setdefaultr   super__init__struuiduuid4itemsr7   widgetr%   userhas_permlistquerysetvalues_listinsert_r   pkappendchoices)
selfargskwargsr@   r   fielduuid_map
uuid_valuebatch_no_choices	__class__s
            r   rI   zAssetForm.__init__K   s]   .)T:::j)i)?)IJ$)&) V
 3tzz|$$
 
 "*!1 	?E:4>DKK%%++D1	? w||,,-AB#12;;GG, 
 ##AA.F,G'HI==## ''15N3O(PQ9IDKK-.6 C7
s   %E.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rA   r2   T)return_status__isnullz(Asset in use you can"t change the statusr/   )r/   z+Already asset with this tracking id exists.)r   r   objectsfilterrV   firstcleaned_datarF   r2   assetassignment_setexistsr   datar8   )rY   r   prev_instances      r   cleanzAssetForm.cleanh   s   ==,,,<BBD;;!!%%nd;%%)).$? --. //66*. 7 &( *')ST  $$tyyAT7U$VHKK( &(*WX 	 r   )r3   r4   r5   __doc__r>   rI   rl   __classcell__r`   s   @r   r   r   +   s    
 
6J: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Fileform-controlTrue.jpeg, .jpg, .png, .pdfnamer"   r#   multipleacceptr$   )rN   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').
        rr   r    N)r3   r4   r5   rm   r
   r6   r7   r8   r=   r   r   r>   r|      s!    	 
 -r   r>   N)	r3   r4   r5   rm   r   	FileField	TextInputrr   r>   r=   r   r   rq   rq      sE     5??u'"3

D   r   rq   c                        e Zd ZdZ ej
                  d ej                  ddddd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.
    Frr   rs   rt   ru   rv   rw   r$   )requiredrN   c                   "    e Zd ZdZeZg d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_idrr   r    N)r3   r4   r5   rm   r   r6   r7   r8   r=   r   r   r>   r      s    	 .-r   r>   c                 $    t        |   |i | y)z
        Initialize the AssetReportForm, disabling the 'asset_id' field.

        Args:
        - *args: Variable length argument list.
        - **kwargs: Arbitrary keyword arguments.
        N)rH   rI   rY   rZ   r[   r`   s      r   rI   zAssetReportForm.__init__   s     	$)&)r   )r3   r4   r5   rm   r   r}   r~   rr   r>   rI   rn   ro   s   @r   r   r      sS    
 5??u'"3
D   * *r   r   c                   0    e Zd ZdZddddZ G d d      Zy)AssetCategoryFormzC
    A form for creating and updating AssetCategory instances.
       )asset_category_nameasset_category_description
company_idc                       e Zd ZdZeZdZdgZy)AssetCategoryForm.Metaa2  
        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.
        r   r    N)r3   r4   r5   rm   r	   r6   r7   r8   r=   r   r   r>   r      s    	 -r   r>   Nr3   r4   r5   rm   colsr>   r=   r   r   r   r      s#    
  "&(D   r   r   c                   B     e Zd ZdZddddZ G d d      Z fdZ xZS )AssetRequestFormzN
    A Django ModelForm for creating and updating AssetRequest instances.
    r   requested_employee_idr1   descriptionc                       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$   textobjective_descriptionz6Requesting a laptop for software development purposes.+oh-input oh-input--textarea oh-input--block   (   )r"   rA   placeholderr#   rowsr   r   N)r3   r4   r5   rm   r   r6   r7   r8   r   r:   TextarearU   r;   r=   r   r   r>   r      s    	 -%1U\\O&
 ".O"
 *5>>"1#$P$ K	
r   r>   c                    t        t        dd       }|j                  }t        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 )Nr@   zasset.add_assetrequestr   rb   )employee_user_idr1   rA   )rE   r   rO   rH   r   rI   r   r7   rP   r   rd   allrR   re   employee_getrA   rf   r   rN   r%   updaterJ   rK   rL   rY   rZ   r[   r@   rO   r`   s        r   rI   zAssetRequestForm.__init__!  s6   .)T:||.	
	
 	$.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   )r3   r4   r5   rm   r   r>   rI   rn   ro   s   @r   r   r      s.     &(bQSTD%
 %
NX Xr   r   c                   B     e Zd ZdZddddZ fdZ G d d      Z xZS )AssetAllocationFormzQ
    A Django ModelForm for creating and updating AssetAssignment instances.
    r   )assigned_to_employee_idr   assigned_by_employee_idc                 `   t        t        dd       }|j                  }t        t        |   |i | t        | j                         t        j                  j                  d      | j                  d   _        t        t        d            | j                  d<   d| j                  d   _        y )	Nr@   	Available)r2   r   zAssign Condition Imageslabelassign_imagesT)rE   r   rO   rH   r   rI   r   r7   r   rd   re   rR   r   rU   r   r   s        r   rI   zAssetAllocationForm.__init__D  s    .)T:||!414B6B$+0==+?+?$ ,@ ,
J( (9-.(
O$ 15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    r#   zoh-select oh-select-2 r$   )r   r   r   N)r3   r4   r5   rm   r   r6   r7   r8   r   r:   r;   r=   r   r   r>   r   R  si    	  
 %G5M+NO'3u|| 89( (4u||5(

r   r>   )r3   r4   r5   rm   r   rI   r>   rn   ro   s   @r   r   r   9  s)    
 $&#%D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)r3   r4   r5   rm   r   r6   r7   r   r9   r   rU   r:   r;   r=   r   r   r>   r   |  st    	  V*5??%0@fU !/J#$W$		! *U\\ 7VL
r   r>   c                     t        |   |i | t        j                         | j                  d   _        t        t        d            | j                  d<   d| j                  d   _        y)z`
        Initializes the AssetReturnForm with initial values and custom field settings.
        r   zReturn Condition Imagesr   r   TN)	rH   rI   r   todayr7   r   r   rU   r   r   s      r   rI   zAssetReturnForm.__init__  s]     	$)&)-1ZZ\M"*'8-.(
O$ 15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.)rg   rF   r   r   r   r   rU   )rY   r   s     r   clean_return_datez!AssetReturnForm.clean_return_date  sG     ''++M:;5''*P(QRRr   )r3   r4   r5   rm   r>   rI   r   rn   ro   s   @r   r   r   w  s    
 
@
5r   r   c                   .    e Zd ZdZdddZ G d d      Zy)AssetBatchFormzI
    A Django ModelForm for creating or updating AssetLot instances.
    r   )lot_descriptionrB   c                       e Zd ZdZeZ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   N)r3   r4   r5   rm   r   r6   r7   r=   r   r   r>   r     s    	 r   r>   Nr   r=   r   r   r   r     s      "4D r   r   )&rm   rK   datetimer   djangor   django.core.exceptionsr   django.utils.translationr   rU   asset.modelsr   r   r	   r
   r   r   r   
base.formsr   base.methodsr   employee.formsr   employee.modelsr   horilla.horilla_middlewaresr   r   r   rq   r   r   r   r   r   r   r=   r   r   <module>r      s       2 6   ! ( , $ 6S	 Sl" 5?? " J0*i 0*h 	  2CXy CXL8
) 8
|Ci CLY r   