o
    3wh-<                     @   s(  d dl mZmZmZ d dl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 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 ddlmZmZmZ d dl m!Z!m"Z"m#Z# d dl$Z$d dl%Z%d dl&Z&d dl'm(Z( d dl)m*Z* edddd Z+edddd Z,edddd Z-edddd Z.edddd Z/edddd Z0eddd d! Z1eddd"d# Z2eddd$d% Z3eddd&d' Z4eddd(d) Z5eddd*d+ Z6d d,l7m7Z7 eddd-d. Z8eddd/d0 Z9eeddd1d2 Z:eddd3d4 Z;dS )5    )renderredirectget_object_or_404)JsonResponseHttpResponse)	Paginator)messages)login_required)require_POST)reverse)canvas)code39)letter   )ProductCategoryBatchProductImage)ProductFormCategoryForm	BatchForm)SumQFN)transaction)IntegrityErrorz/auth/login/)	login_urlc                 C   sL   t jjdddd}t j ddddd}t }t| d|||dS )	NT)parent__isnullsubcategoriesproductsparentnamezinventory/category_list.html)
categoriesall_categoriesform)r   objectsfilterprefetch_relatedallorder_byr   r   )requestr"   r#   r$    r+   +/var/www/html/hyperkenya/inventory/views.pycategory_list   s   r-   c                 C   sD   | j dkrt| j}| r|  tdS nt }t| dd|iS )NPOSTr-   zinventory/add_category.htmlr$   )methodr   r.   is_validsaver   r   )r*   r$   r+   r+   r,   add_category%   s   

r2   c                 C   sX   t t|d}| jdkrt| j|d}| r|  tdS nt|d}t| dd|iS )Npkr.   instancer-   zinventory/edit_category.htmlr$   )	r   r   r/   r   r.   r0   r1   r   r   )r*   r4   categoryr$   r+   r+   r,   edit_category1   s   

r8   c                 C   sD   t t|d}| jdkr|  t| d|j d tdS tdS )Nr3   r.   z
Category "z " has been deleted successfully.r-   )r   r   r/   deleter   successr!   r   )r*   category_idr7   r+   r+   r,   delete_category>   s   
r<   c                 C   sh  t j }tj d}| jd}|r|j|d}| jd}|dkr,|jdd}n|dkr7|jd	d}n|d
krC|jtdd}| jd}|r]|t	|dt	|dB t	|dB }| jdd}|dkrn|d}n7|dkrx|d}n-|dkr|d}n#|dkr|d}n|dkr|d}n|dkr|d}n|d}||||||d}t
| d|S ) Nr!   r7   )r;   statusactiveT	is_activeinactiveF	low_stocklow_stock_threshold)stock_quantity__ltesearchname__icontainsbarcode__icontainsdescription__icontainssort	price_ascselling_price
price_descz-selling_price	stock_ascstock_quantity
stock_descz-stock_quantitydate_asc
created_at	date_descz-created_at)r   r"   current_categorycurrent_statuscurrent_sortsearch_queryzinventory/product_list.html)r   r%   r(   r   r)   GETgetr&   r   r   r   )r*   r"   r   r;   r=   rY   sort_bycontextr+   r+   r,   product_listH   sV   

	r^   c                 C   s   | j dd }t|dk rtg ddS tjjt|dt|dB t|dB d	d
dd }g }|D ]}|	|j
|j|jp>dt|j|jtd|j
gdd q2t|ddS )z AJAX endpoint for product searchq    FsaferF   rH   rJ   Tr?   N
   product_detail)args)idr!   barcodepricestockurl)rZ   r[   striplenr   r   r%   r&   r   appendrg   r!   rh   strrN   rQ   r   )r*   queryr   resultsproductr+   r+   r,   search_products_ajax   s0   
	rs   c                 C   s(   t t|d}|j }t| d||dS )Nr3   zinventory/product_detail.html)rr   batches)r   r   rt   r(   r   )r*   r4   rr   rt   r+   r+   r,   re      s   
re   c                 C   s   | j dkrIt| j| j}| rB|jdd}d| jv r!| jd |_|  | jd}|D ]
}tj	j
||d q-t| d tdS t| d	 nt }tj	 }t| d
||dS )Nr.   Fcommitimageimagesrr   rw   zProduct added successfully!r^   z&There was an error adding the product.zinventory/add_product.html)r$   r"   )r/   r   r.   FILESr0   r1   rw   getlistr   r%   creater   r:   r   errorr   r(   r   )r*   r$   rr   rx   rw   r"   r+   r+   r,   add_product   s&   


r~   c                 C   s   t t|d}| jdkrQt| j| j|d}| rJ|jdd}d| jv r)| jd |_|  | j	d}|D ]
}t
jj||d q5t| d	 td
S t| d nt|d}tj }t| d|||dS )Nr3   r.   r5   Fru   rw   rx   ry   zProduct updated successfully!r^   z(There was an error updating the product.zinventory/edit_product.html)r$   rr   r"   )r   r   r/   r   r.   rz   r0   r1   rw   r{   r   r%   r|   r   r:   r   r}   r   r(   r   )r*   r4   rr   r$   rx   rw   r"   r+   r+   r,   edit_product   s*   



r   c                 C   s   t t|d}|  tdS )Nr3   r^   )r   r   r9   r   )r*   r4   rr   r+   r+   r,   delete_product   s   r   c                 C   s   t t|d}| jdkr@t| j}| r?|jdd}||_| |_	|  | j
|j7  _
|  t| d td|jdS nt }t| d||d	S )
N)rg   r.   Fru   zStock received successfully!re   r3   zinventory/receive_stock.html)rr   r$   )r   r   r/   r   r.   r0   r1   rr   generate_batch_code
batch_coderQ   quantityr   r:   r   rg   r   )r*   
product_idrr   r$   batchr+   r+   r,   receive_stock   s$   


r   c                 C   sx   t j }tdd}d|d< t|}|g d |D ]}||j|j|j	|j
|j|j|j|jr5|jjndg q|S )Nztext/csvcontent_typez#attachment; filename="products.csv"Content-Disposition)BarcodeNamezBuying PricezSelling PricezStock QuantityzExpiry DatezPackaging Typer   Uncategorized)r   r%   r(   r   csvwriterwriterowrh   r!   buying_pricerN   rQ   expiry_datepackaging_typer7   )r*   r   responser   rr   r+   r+   r,   export_products  s"   


r   )datetimec                 C   sz  | j dkr9| jd}|jdst| d tdS z| 	d}t
|}t| t  ttj|ddd	d
D ]\}}zt|dk rTt| d| d W q=|d r^|d  nd }|d  }t|d	  }t|d  }	t|dkr|d  rt|d  nd }
t|dkr|d  rt|d  d nd }t|dkr|d  nd }t|dkr|d  nd}tjj|d\}}tjj|||
||	|||dd W q= ty } zt| d| dt|  W Y d }~q=d }~ww W d    n	1 sw   Y  t | d W tdS  ty8 } zt| dt|  W Y d }~tdS d }~ww tdS )Nr.   imported_filez.csvzFile is not CSV typer^   zUTF-8,)	delimiterra   )start   zRow z: Missing required fields.r   r         z%Y-%m-%d      r   )r!   )r!   r   rN   rQ   r   r   r7   )rh   defaultszError on row z: zProducts imported successfully!zError processing file: )!r/   rz   r[   r!   endswithr   r}   r   readdecodeioStringIOnextr   atomic	enumerater   readerrm   rl   floatintr   strptimedater   r%   get_or_creater   update_or_create	Exceptionro   r:   )r*   csv_filedata_set	io_string
row_numbercolumnrh   r!   rN   rQ   r   r   r   category_namer7   _er+   r+   r,   import_products)  sd   

,4
( r   c              
   C   s   | j dd }|r;ztjt|dt|dB ddddd	}t|}W n t	y: } zg }W Y d }~nd }~ww g }t
|d
dS )Nr_   r`   rF   rH   rg   r!   rh   rN   rQ   Frb   )rZ   r[   rl   r   r%   r&   r   valueslistr   r   )r*   rp   r   rq   r   r+   r+   r,   product_live_searchf  s   r   c              
   C   s   z.| j dd}|r|d}tjj|d  t| d nt	| d W tdS W tdS  t
yN } zt	| dt|  W Y d }~tdS d }~ww )	Nselected_productsr`   r   id__inz1Selected products have been deleted successfully.z'No products were selected for deletion.zAn error occurred: r^   )r.   r[   splitr   r%   r&   r9   r   r:   r}   r   ro   r   )r*   product_idsidsr   r+   r+   r,   delete_selected_productsx  s   
 r   c              	   C   s   | j dg }t| j dd}tjj|d}tdd}d|d< tj	|t
d	}t
\}}|d
 }|D ]:}	tj|	jdddd}
t|D ])}|dk rO|  |d
 }|
|d
| |d
|d |	j d|	j  |d
8 }qAq1|  |S )Nr   r   r   r   zapplication/pdfr   z#attachment; filename="barcodes.pdf"r   )pagesized      g      ?F)	barHeightbarWidthchecksum2   z - )rZ   r{   r   r[   r   r%   r&   r   r   Canvasr   r   
Extended39rh   rangeshowPagedrawOn
drawStringr!   r1   )r*   r   r   r   r   cwidthheight
y_positionrr   rh   r   r+   r+   r,   print_barcodes  s(   
 
r   )<django.shortcutsr   r   r   django.httpr   r   django.core.paginatorr   django.contribr   django.contrib.auth.decoratorsr	   django.views.decorators.httpr
   django.urlsr   reportlab.pdfgenr   reportlab.graphics.barcoder   reportlab.lib.pagesizesr   modelsr   r   r   r   formsr   r   r   django.db.modelsr   r   r   r   r   json	django.dbr   django.db.utilsr   r-   r2   r8   r<   r^   rs   re   r~   r   r   r   r   r   r   r   r   r   r+   r+   r+   r,   <module>   sh    



	
:




!



<
