o
    qvh                     @   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/hyperkenya/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                    sT   t   }|d}|r(|| jkrtdt| jdr(|| j v r(td|S )Nr   z$A category cannot be its own parent.get_ancestorsz>Circular references are not allowed in the category hierarchy.)r   cleangetr   r   ValidationErrorhasattrr&   )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   variantbarcodebuying_priceselling_pricewholesale_pricestock_quantitylow_stock_thresholdpackaging_typeexpiry_datecategoryr   is_featureddiscount_price	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 )Nr:   r   z'Low stock threshold cannot be negative.r+   r(   r   r)   )r!   r:   r   r   r   clean_low_stock_threshold0   s   
z%ProductForm.clean_low_stock_thresholdc                 C   s,   | j d}|r|t  k rtd|S Nr<   z"Expiry date cannot be in the past.)r+   r(   r   dater   r)   r!   r<   r   r   r   clean_expiry_date6   s   
zProductForm.clean_expiry_datec                    sl   t   }|d}|d}|d}|d ur&|d ur&||kr&| dd |d ur4||kr4| dd |S )Nr6   r7   r?   z0Selling price must be greater than buying price.z3Discount price must be less than the selling price.)r   r'   r(   	add_error)r!   r+   r6   r7   r?   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)commitrA   T)product
is_primary)rJ   rA   rK   images)rJ   rA   )r   saver+   r(   r   r   filterexistscreatefilesgetlist)r!   rI   r   rL   rA   r$   r   r   rM   H   s   zProductForm.save)T)r   r   r   r   
ImageFieldrA   	FileFieldr	   rL   r   rC   rG   r'   rM   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)rJ   
batch_codequantityr6   r7   r<   suppliernotesN)r   r   r   r   r   r   r   r   r   r   r   ]   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-controlrJ   placeholderz-- Select a Product --rV   zEnter batch code (optional)rW   zEnter quantity receivedr6   zEnter buying price per unitr7   zEnter selling price per unitr<   typerE   rY   z#Add any additional notes (optional))r   r   r   valuesr3   r2   update)r!   r"   r#   fieldr$   r   r   r   d   s   zBatchForm.__init__c                 C   *   | j d}|d u s|dkrtd|S )NrW   r   z Quantity must be greater than 0.rB   )r!   rW   r   r   r   clean_quantityt      
zBatchForm.clean_quantityc                 C   r`   )Nr6   r   z$Buying price must be greater than 0.rB   )r!   r6   r   r   r   clean_buying_pricez   rb   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 )Nr7   r6   r   z%Selling price must be greater than 0.z4Selling price must be greater than the buying price.rB   )r!   r7   r6   r   r   r   clean_selling_price   s   

zBatchForm.clean_selling_pricec                 C   s@   | j d}|rtdd}|jj|jdd st	d|S )NrX   	suppliersSupplierT)r   r@   z-The selected supplier is inactive or invalid.)
r+   r(   r
   	get_modelr   rN   r   rO   r   r)   )r!   rX   rf   r   r   r   clean_supplier   s   
zBatchForm.clean_supplierc                 C   s.   | j d}|r|tjj k rtd|S rD   )r+   r(   r   datetimerE   todayr)   rF   r   r   r   rG      s   
zBatchForm.clean_expiry_date)r   r   r   r   r   ra   rc   rd   rh   rG   r,   r   r   r$   r   rU   \   s    
rU   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,.xlsxr1   T)r/   r3   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   rT   	FileInputrm   rq   r   r   r   r   rk      s    rk   N)djangor   django.utils.timezoner   modelsr   r   r   r   widgetsr	   django.appsr
   	ModelFormr   r-   rU   Formrk   r   r   r   r   <module>   s    <=