o
    qvhB                     @   s  d dl mZ d dlmZ 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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mZ d dlmZ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) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0Z0d dl1m1Z1 d dlm	Z	 d dlm2Z2 e3e4Z5edddd Z6edddd Z7edddd Z8edddd  Z9eddd!d" Z:d#d$ Z;d%d& Z<eddd'd( Z=eddd)d* Z>edded+d, Z?d-d$ Z;d.d& Z<eed/d0 Z@d1d2 ZAdS )3    )login_required)	Paginator)renderget_object_or_404redirect)JsonResponseHttpResponse)messages)csrf_exemptN)Shop   )Order	OrderItemStockReceptionReceipt)ProductForm)	OrderFormStockReceptionForm)ProductBatch)Supplier)A4)SimpleDocTemplate	ParagraphSpacerTable
TableStyleImage)getSampleStyleSheet)inch)colors)datetime)r   )reversez/auth/login/)	login_urlc                 C   s@   t j d}t|d}| jd}||}t| dd|iS )z List all orders with pagination.z-created_at
   pagezorders/list_orders.htmlpage_obj)	r   objectsallorder_byr   GETgetget_pager   )requestorders	paginatorpage_numberr&    r1   (/var/www/html/hyperkenya/orders/views.pylist_orders   s
   

r3   c                 C   sl   t t|d}g }d}|j D ]}|j|j }||jj|j|j|d ||7 }q|||d}t	| d|S )z.Show order details including items and totals.order_numberr   )product_namequantitybuying_pricesubtotal)orderitemstotalzorders/order_detail.html)
r   r   r;   r(   r8   quantity_orderedappendproductnamer   )r-   r5   r:   r;   r<   itemr9   contextr1   r1   r2   order_detail'   s"   
rC   c           	   	   C   s   | j dkr_t| j}| jd}| r^|r^| }t|}|D ]5}tj	j|d d}|d }|d|j
}|du rLt| d|d	t d
d  S tj	j||||d q!td|jdS nt }t| d|t d
dS )z'Create a new order with multiple items.POST
items_data
product_id)idr7   r8   Nzorders/create_order.htmlz-Buying price is required for all order items.z	%b %d, %Y)formerror
today_date)r:   r?   r=   r8   rC   r4   )rH   rJ   )methodr   rD   r+   is_validsavejsonloadsr   r'   r8   r   r!   nowstrftimer   creater   r5   )	r-   rH   rE   r:   r;   rA   r?   r=   r8   r1   r1   r2   create_order@   s:   


rS   c           
      C   s8  t t|d}| jdkr|j D ]k}|j}t| jd|j	 |j
}t| jd|j	 |j}t| jd|j	 |j}| jd|j	 d}| j|7  _||_||_|  |jdd	 d
t d }	tjj||	|||rw|ndd qd|_|  t| d|j  td|jdS t| dd|iS )z;Receive stock for an order and create batches for products.r4   rD   quantity_received_buying_price_selling_price_expiry_date_N _.z%Y%m%d)r?   
batch_coder7   priceexpiry_dateTzStock received for order rC   zorders/receive_stock.htmlr:   )r   r   rK   r;   r(   r?   intrD   r+   rG   r=   floatr8   selling_pricestock_quantityrM   r@   replacer!   rP   rQ   r   r'   rR   is_receivedr	   successr5   r   r   )
r-   r5   r:   rA   r?   quantity_receivednew_buying_pricenew_selling_pricer]   r[   r1   r1   r2   receive_stockj   s2   
"
	rh   c                 C   sf   | j dkr(t| j}| r| }t|j|j|jdddS td|j	iddS t }t
| dd|iS )	zAdd a new product to the order.rD   )rG   r@   r8      statuserrors  zinventory/add_product.htmlrH   )rK   r   rD   rL   rM   r   rG   r@   r8   rl   r   )r-   rH   r?   r1   r1   r2   add_order_product   s   

rn   c                 C   B   | j dd }tjj|ddd }dd |D }t|dd	S )
zAJAX search for suppliers.q name__icontainsNr$   c                 S   s*   g | ]}|j |j|jpd |jpd dqS )rq   rG   r@   phoneemailrt   .0supplierr1   r1   r2   
<listcomp>   s    
$search_suppliers.<locals>.<listcomp>Fsafer*   r+   stripr   r'   filterr   )r-   query	suppliersresultsr1   r1   r2   search_suppliers      r   c                 C   ro   )
zAJAX search for products.rp   rq   rr   Nr$   c                 S   s0   g | ]}|j |j|j|jrt|jnd dqS )r   rG   r@   ra   r8   )rG   r@   ra   r8   r_   rx   r?   r1   r1   r2   rz      s    
#search_products.<locals>.<listcomp>Fr|   r*   r+   r   r   r'   r   r   )r-   r   productsr   r1   r1   r2   search_products   r   r   c                 C   2  t t|d}tdd}d|j d|d< t|td}g }t }tj	 }|j
rAt|j
jdt dt d	}|td
d || t|j|d td|j |d td|j |d |jrjtd|j |d ndg}	td|d td|jd |d td|j |d g}
|	|
gg}t|dt dt gd}|tddg || |td
d td|d t|jj|d t|jj|d td|jj d|jj |d td|jj |d gtd |d t|j|d t|j |d ggg}t|dt dt gd}|tdg || |td
d g d!g}d"}|j D ]!}|j}||j  }||7 }||j!jt"||j d#|d#g q+|ddd$|d#g t|dt dt dt d%t gd}|td&d'd(t#j$fd)d'd(t#j%fd*d+d,d&d-d.t#j&fd/d'd.d
t#j'fg || |(| |S )0z:Generate a PDF for the order's Local Purchase Order (LPO).r4   application/pdfcontent_typeattachment; filename="z	_LPO.pdf"Content-Dispositionpagesize      ?widthheightr      Title	Address: NormalPhone: 	Website: rq   z<b>LPO Details</b>DATE: %Y-%m-%dzPO #:       	colWidthsVALIGNr   r   r   TOPALIGNr   r   r   RIGHT   <b>SUPPLIER</b>Heading2City: , <b>SHIPPING ADDRESS</b>zProduct NameQuantityzBuying PriceSubtotalr   .2fTotal   
BACKGROUNDr   r   r   	TEXTCOLORr   r   r   CENTERFONTNAMEr   r   zHelvetica-BoldBOTTOMPADDINGr   r   r   r   r   r   GRID)r   r   r   r5   r   r   r   r   r'   firstlogor   pathr   r>   r   r   r@   addressru   website
updated_atrQ   r   setStyler   ry   citycountryr;   r(   r=   r8   r?   strr    green
whitesmokebeigeblackbuildr-   r5   r:   responsedocelementsstylesshopr   left_columnright_columndataheader_tablesupplier_infosupplier_tableorder_itemsr<   rA   r7   r9   order_tabler1   r1   r2   generate_lpo_pdf   |   




 


($
	
r   c                 C   r   )0z'Generate a PDF for the order's receipt.r4   r   r   r   z_Receipt.pdf"r   r   r   r   r   r   r   r   r   r   r   rq   z<b>Receipt Details</b>r   r   zReceipt #: r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   r1   r2   generate_receipt_pdf  r   r   c              
   C   s  | j dkrzt| j}|d}|dg }|r tjj|dnd}td| d|  |D ]f}|d}t	|d	d
}t
|dd
}t
|dd
}	|dd}
|rZ|d
krftddddd  W S tjj|d }td|  |stdd| dddd  W S tjj|||||	|
d q/td| d td}td|dW S  ty } ztdt|  tdt|dddW  Y d}~S d}~ww t| d S )!zE
    Handle stock reception with supplier and products via AJAX.
    rD   supplier_idstock_itemspkNzSupplier ID: z, Supplier: r6   r7   r   r8   r`   r]   FzInvalid product data.)rd   messagerm   rj   )r@   zProcessing Product: z	Product "z" not found.i  )r?   ry   re   r8   r`   r]   z.Stock items successfully created for supplier rZ   received_stock_listT)rd   redirect_urlzError occurred: i  zorders/stock.html)rK   rN   rO   bodyr+   r   r'   loggerdebugr^   r_   r   r   r   r   r   rR   infor"   	ExceptionrI   r   r   )r-   r   r   r   ry   rA   r6   r7   r8   r`   r]   r?   r   er1   r1   r2   stockg  sH   


 	"
r   c                 C   B   | j dd }tjj|dd d }dd |D }t|ddS )	Nrp   rq   rr   r$   c                 S   s   g | ]	}|j |jd qS )rG   r@   r   rw   r1   r1   r2   rz     s    r{   Fr|   r~   )r-   r   r   r   r1   r1   r2   r     s   c                 C   r   )	Nrp   rq   rr   r$   c                 S   s"   g | ]}|j |j|j|jd qS )r   r   r   r1   r1   r2   rz     s
    
r   Fr|   r   )r-   r   r   r   r1   r1   r2   r     s   c                 C   s    t j d}t| dd|iS )z;
    View to display a list of received stock entries.
    z-received_datezorders/received_stock_list.htmlstock_receptions)r   r'   r(   r)   r   )r-   r   r1   r1   r2   r     s   r   c                 C   s   t t|d}t| dd|iS )Nr   zorders/receipt_detail.htmlreceipt)r   r   r   )r-   r   r   r1   r1   r2   receipt_detail  s   r   )Bdjango.contrib.auth.decoratorsr   django.core.paginatorr   django.shortcutsr   r   r   django.httpr   r   django.contribr	   django.views.decorators.csrfr
   loggingsettings.modelsr   modelsr   r   r   r   inventory.formsr   formsr   r   inventory.modelsr   r   suppliers.modelsr   reportlab.lib.pagesizesr   reportlab.platypusr   r   r   r   r   r   reportlab.lib.stylesr   reportlab.lib.unitsr   reportlab.libr    rN   r!   r"   	getLogger__name__r   r3   rC   rS   rh   rn   r   r   r   r   r   r   r   r1   r1   r1   r2   <module>   s`     

	

)
&

S
S8	