o
    h                     @   s   d dl mZ d dlmZ ddlmZmZmZmZ ddl	m
Z
 d dlmZ G dd dejZG d	d
 d
ejZG dd dejZG dd dejZdS )    )forms)now   )ProductCategoryBatchProductImage)MultiFileInput)appsc                       s6   e Zd ZG dd dZ fddZ fddZ  ZS )CategoryFormc                   @      e Zd ZeZg dZdS )zCategoryForm.Meta)nameparentdescription	thumbnailN)__name__
__module____qualname__r   modelfields r   r   $/var/www/html/pos/inventory/forms.pyMeta       r   c                    s<   t  j|i | | jjrtjj| jjd| jd _d S d S )N)pkr   )	super__init__instancer   r   objectsexcluder   queryset)selfargskwargs	__class__r   r   r      s   zCategoryForm.__init__c                    s0   t   }|d}|r|| jkrtd|S )Nr   z$A category cannot be its own parent.)r   cleangetr   r   ValidationError)r!   cleaned_datar   r$   r   r   r&      s   



zCategoryForm.clean)r   r   r   r   r   r&   __classcell__r   r   r$   r   r   
   s    r   c                       sp   e Zd ZejdddZejeddiddddZG d	d
 d
Z	dd Z
dd Z fddZd fdd	Z  ZS )ProductFormFzPrimary Image)requiredlabelmultipleTattrszAdditional Images)widgetr,   r-   c                   @   r   )zProductForm.Meta)r   barcodebuying_priceselling_pricestock_quantitylow_stock_thresholdpackaging_typeexpiry_datecategoryr   	is_activeimageN)r   r   r   r   r   r   r   r   r   r   r   &   r   r   c                 C   s.   | j d}|d ur|dk rtd|pdS )Nr6   r   z'Low stock threshold cannot be negative.r)   r'   r   r(   )r!   r6   r   r   r   clean_low_stock_threshold.   s   
z%ProductForm.clean_low_stock_thresholdc                 C   s,   | j d}|r|t  k rtd|S Nr8   z"Expiry date cannot be in the past.)r)   r'   r   dater   r(   r!   r8   r   r   r   clean_expiry_date4   s   
zProductForm.clean_expiry_datec                    sF   t   }|d}|d}|d ur!|d ur!||kr!| dd |S )Nr3   r4   z0Selling price must be greater than buying price.)r   r&   r'   	add_error)r!   r)   r3   r4   r$   r   r   r&   :   s   


zProductForm.cleanc                    s|   t  jdd}|r<|  | jdr)tjj|dd s)tjj|| jd dd | j	
d}|D ]
}tjj||d q1|S )	NF)commitr;   T)product
is_primary)rD   r;   rE   images)rD   r;   )r   saver)   r'   r   r   filterexistscreatefilesgetlist)r!   rC   r   rF   r;   r$   r   r   rG   C   s   zProductForm.save)T)r   r   r   r   
ImageFieldr;   	FileFieldr	   rF   r   r=   rA   r&   rG   r*   r   r   r$   r   r+      s    	r+   c                       sR   e Zd ZG dd dZ fddZdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )	BatchFormc                   @   r   )zBatchForm.Meta)rD   
batch_codequantityr3   r4   r8   suppliernotesN)r   r   r   r   r   r   r   r   r   r   r   X   r   r   c                    s   t  j|i | | j D ]}|jjddi q| jd jjddi | jd jjddi | jd jjdd	i | jd
 jjddi | jd jjddi | jd jjddi | jd jjddi d S )Nclasszform-controlrD   placeholderz-- Select a Product --rP   zEnter batch code (optional)rQ   zEnter quantity receivedr3   zEnter buying price per unitr4   zEnter selling price per unitr8   typer?   rS   z#Add any additional notes (optional))r   r   r   valuesr1   r0   update)r!   r"   r#   fieldr$   r   r   r   _   s   zBatchForm.__init__c                 C   *   | j d}|d u s|dkrtd|S )NrQ   r   z Quantity must be greater than 0.r<   )r!   rQ   r   r   r   clean_quantitym      
zBatchForm.clean_quantityc                 C   rZ   )Nr3   r   z$Buying price must be greater than 0.r<   )r!   r3   r   r   r   clean_buying_prices   r\   zBatchForm.clean_buying_pricec                 C   sL   | j d}| j d}|d u s|dkrtd|r$||kr$td|S )Nr4   r3   r   z%Selling price must be greater than 0.z4Selling price must be greater than the buying price.r<   )r!   r4   r3   r   r   r   clean_selling_pricey   s   

zBatchForm.clean_selling_pricec                 C   s@   | j d}|rtdd}|jj|jdd st	d|S )NrR   	suppliersSupplierT)r   r:   z-The selected supplier is inactive or invalid.)
r)   r'   r
   	get_modelr   rH   r   rI   r   r(   )r!   rR   r`   r   r   r   clean_supplier   s   
zBatchForm.clean_supplierc                 C   s.   | j d}|r|tjj k rtd|S r>   )r)   r'   r   datetimer?   todayr(   r@   r   r   r   rA      s   
zBatchForm.clean_expiry_date)r   r   r   r   r   r[   r]   r^   rb   rA   r*   r   r   r$   r   rO   W   s    
rO   c                   @   s0   e Zd ZejdejddidddZdd Zd	S )
ProductImportFormzUpload CSV or Excel Fileacceptz
.csv,.xlsxr/   T)r-   r1   r,   c                 C   s:   | j d}|jdstd|jdkrtd|S )Nimported_file)z.csvz.xlsxz&Only CSV or Excel files are supported.i  P z File size should not exceed 5MB.)r)   r'   r   endswithr   r(   size)r!   filer   r   r   clean_imported_file   s   


z%ProductImportForm.clean_imported_fileN)r   r   r   r   rN   	FileInputrg   rk   r   r   r   r   re      s    re   N)djangor   django.utils.timezoner   modelsr   r   r   r   widgetsr	   django.appsr
   	ModelFormr   r+   rO   Formre   r   r   r   r   <module>   s   9;